|
| 1 | +// Copyright (c) 2015 GitHub, Inc. |
| 2 | +// Use of this source code is governed by the MIT license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "atom/browser/api/atom_api_download_item.h" |
| 6 | + |
| 7 | +#include <map> |
| 8 | + |
| 9 | +#include "atom/common/native_mate_converters/callback.h" |
| 10 | +#include "atom/common/native_mate_converters/file_path_converter.h" |
| 11 | +#include "atom/common/native_mate_converters/gurl_converter.h" |
| 12 | +#include "atom/common/node_includes.h" |
| 13 | +#include "base/memory/linked_ptr.h" |
| 14 | +#include "base/strings/utf_string_conversions.h" |
| 15 | +#include "native_mate/dictionary.h" |
| 16 | +#include "net/base/filename_util.h" |
| 17 | + |
| 18 | +namespace mate { |
| 19 | + |
| 20 | +template<> |
| 21 | +struct Converter<content::DownloadItem::DownloadState> { |
| 22 | + static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, |
| 23 | + content::DownloadItem::DownloadState state) { |
| 24 | + std::string download_state; |
| 25 | + switch (state) { |
| 26 | + case content::DownloadItem::COMPLETE: |
| 27 | + download_state = "completed"; |
| 28 | + break; |
| 29 | + case content::DownloadItem::CANCELLED: |
| 30 | + download_state = "cancelled"; |
| 31 | + break; |
| 32 | + case content::DownloadItem::INTERRUPTED: |
| 33 | + download_state = "interrputed"; |
| 34 | + break; |
| 35 | + default: |
| 36 | + break; |
| 37 | + } |
| 38 | + return ConvertToV8(isolate, download_state); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +} // namespace mate |
| 43 | + |
| 44 | +namespace atom { |
| 45 | + |
| 46 | +namespace api { |
| 47 | + |
| 48 | +namespace { |
| 49 | +// The wrapDownloadItem funtion which is implemented in JavaScript |
| 50 | +using WrapDownloadItemCallback = base::Callback<void(v8::Local<v8::Value>)>; |
| 51 | +WrapDownloadItemCallback g_wrap_download_item; |
| 52 | + |
| 53 | +char kDownloadItemSavePathKey[] = "DownloadItemSavePathKey"; |
| 54 | + |
| 55 | +std::map<uint32, linked_ptr<v8::Global<v8::Value>>> g_download_item_objects; |
| 56 | +} // namespace |
| 57 | + |
| 58 | +DownloadItem::SavePathData::SavePathData(const base::FilePath& path) : |
| 59 | + path_(path) { |
| 60 | +} |
| 61 | + |
| 62 | +const base::FilePath& DownloadItem::SavePathData::path() { |
| 63 | + return path_; |
| 64 | +} |
| 65 | + |
| 66 | +DownloadItem::DownloadItem(content::DownloadItem* download_item) : |
| 67 | + download_item_(download_item) { |
| 68 | + download_item_->AddObserver(this); |
| 69 | +} |
| 70 | + |
| 71 | +DownloadItem::~DownloadItem() { |
| 72 | + Destroy(); |
| 73 | +} |
| 74 | + |
| 75 | +void DownloadItem::Destroy() { |
| 76 | + if (download_item_) { |
| 77 | + download_item_->RemoveObserver(this); |
| 78 | + auto iter = g_download_item_objects.find(download_item_->GetId()); |
| 79 | + if (iter != g_download_item_objects.end()) |
| 80 | + g_download_item_objects.erase(iter); |
| 81 | + download_item_ = nullptr; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +bool DownloadItem::IsDestroyed() const { |
| 86 | + return download_item_ == nullptr; |
| 87 | +} |
| 88 | + |
| 89 | +void DownloadItem::OnDownloadUpdated(content::DownloadItem* item) { |
| 90 | + download_item_->IsDone() ? Emit("done", item->GetState()) : Emit("updated"); |
| 91 | +} |
| 92 | + |
| 93 | +void DownloadItem::OnDownloadDestroyed(content::DownloadItem* download) { |
| 94 | + Destroy(); |
| 95 | +} |
| 96 | + |
| 97 | +int64 DownloadItem::GetReceivedBytes() { |
| 98 | + return download_item_->GetReceivedBytes(); |
| 99 | +} |
| 100 | + |
| 101 | +int64 DownloadItem::GetTotalBytes() { |
| 102 | + return download_item_->GetTotalBytes(); |
| 103 | +} |
| 104 | + |
| 105 | +const GURL& DownloadItem::GetUrl() { |
| 106 | + return download_item_->GetURL(); |
| 107 | +} |
| 108 | + |
| 109 | +std::string DownloadItem::GetMimeType() { |
| 110 | + return download_item_->GetMimeType(); |
| 111 | +} |
| 112 | + |
| 113 | +bool DownloadItem::HasUserGesture() { |
| 114 | + return download_item_->HasUserGesture(); |
| 115 | +} |
| 116 | + |
| 117 | +std::string DownloadItem::GetFilename() { |
| 118 | + return base::UTF16ToUTF8(net::GenerateFileName(GetUrl(), |
| 119 | + GetContentDisposition(), |
| 120 | + std::string(), |
| 121 | + download_item_->GetSuggestedFilename(), |
| 122 | + GetMimeType(), |
| 123 | + std::string()).LossyDisplayName()); |
| 124 | +} |
| 125 | + |
| 126 | +std::string DownloadItem::GetContentDisposition() { |
| 127 | + return download_item_->GetContentDisposition(); |
| 128 | +} |
| 129 | + |
| 130 | +void DownloadItem::SetSavePath(const base::FilePath& path) { |
| 131 | + download_item_->SetUserData(UserDataKey(), new SavePathData(path)); |
| 132 | +} |
| 133 | + |
| 134 | +void DownloadItem::Pause() { |
| 135 | + download_item_->Pause(); |
| 136 | +} |
| 137 | + |
| 138 | +void DownloadItem::Resume() { |
| 139 | + download_item_->Resume(); |
| 140 | +} |
| 141 | + |
| 142 | +void DownloadItem::Cancel() { |
| 143 | + download_item_->Cancel(true); |
| 144 | +} |
| 145 | + |
| 146 | +mate::ObjectTemplateBuilder DownloadItem::GetObjectTemplateBuilder( |
| 147 | + v8::Isolate* isolate) { |
| 148 | + return mate::ObjectTemplateBuilder(isolate) |
| 149 | + .SetMethod("pause", &DownloadItem::Pause) |
| 150 | + .SetMethod("resume", &DownloadItem::Resume) |
| 151 | + .SetMethod("cancel", &DownloadItem::Cancel) |
| 152 | + .SetMethod("getReceivedBytes", &DownloadItem::GetReceivedBytes) |
| 153 | + .SetMethod("getTotalBytes", &DownloadItem::GetTotalBytes) |
| 154 | + .SetMethod("getUrl", &DownloadItem::GetUrl) |
| 155 | + .SetMethod("getMimeType", &DownloadItem::GetMimeType) |
| 156 | + .SetMethod("hasUserGesture", &DownloadItem::HasUserGesture) |
| 157 | + .SetMethod("getFilename", &DownloadItem::GetFilename) |
| 158 | + .SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition) |
| 159 | + .SetMethod("setSavePath", &DownloadItem::SetSavePath); |
| 160 | +} |
| 161 | + |
| 162 | +void SetWrapDownloadItem(const WrapDownloadItemCallback& callback) { |
| 163 | + g_wrap_download_item = callback; |
| 164 | +} |
| 165 | + |
| 166 | +void ClearWrapDownloadItem() { |
| 167 | + g_wrap_download_item.Reset(); |
| 168 | +} |
| 169 | + |
| 170 | +// static |
| 171 | +mate::Handle<DownloadItem> DownloadItem::Create( |
| 172 | + v8::Isolate* isolate, content::DownloadItem* item) { |
| 173 | + auto handle = mate::CreateHandle(isolate, new DownloadItem(item)); |
| 174 | + g_wrap_download_item.Run(handle.ToV8()); |
| 175 | + g_download_item_objects[item->GetId()] = make_linked_ptr( |
| 176 | + new v8::Global<v8::Value>(isolate, handle.ToV8())); |
| 177 | + return handle; |
| 178 | +} |
| 179 | + |
| 180 | +// static |
| 181 | +void* DownloadItem::UserDataKey() { |
| 182 | + return &kDownloadItemSavePathKey; |
| 183 | +} |
| 184 | + |
| 185 | +} // namespace api |
| 186 | + |
| 187 | +} // namespace atom |
| 188 | + |
| 189 | +namespace { |
| 190 | + |
| 191 | +void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, |
| 192 | + v8::Local<v8::Context> context, void* priv) { |
| 193 | + v8::Isolate* isolate = context->GetIsolate(); |
| 194 | + mate::Dictionary dict(isolate, exports); |
| 195 | + dict.SetMethod("_setWrapDownloadItem", &atom::api::SetWrapDownloadItem); |
| 196 | + dict.SetMethod("_clearWrapDownloadItem", &atom::api::ClearWrapDownloadItem); |
| 197 | +} |
| 198 | + |
| 199 | +} // namespace |
| 200 | + |
| 201 | +NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_download_item, Initialize); |
0 commit comments