electron/shell/browser/api/electron_api_download_item.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

302 lines
8.5 KiB
C++
Raw Normal View History

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/api/electron_api_download_item.h"
2020-04-03 00:22:46 +00:00
#include <memory>
2015-09-24 07:55:45 +00:00
#include "base/strings/utf_string_conversions.h"
#include "net/base/filename_util.h"
#include "shell/browser/electron_browser_main_parts.h"
#include "shell/common/gin_converters/file_dialog_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
#include "url/gurl.h"
namespace gin {
2018-04-18 01:55:30 +00:00
template <>
struct Converter<download::DownloadItem::DownloadState> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
download::DownloadItem::DownloadState state) {
std::string download_state;
switch (state) {
case download::DownloadItem::IN_PROGRESS:
2016-06-09 10:42:32 +00:00
download_state = "progressing";
break;
case download::DownloadItem::COMPLETE:
download_state = "completed";
break;
case download::DownloadItem::CANCELLED:
download_state = "cancelled";
break;
case download::DownloadItem::INTERRUPTED:
download_state = "interrupted";
break;
default:
break;
}
return ConvertToV8(isolate, download_state);
}
};
} // namespace gin
2022-06-29 19:55:47 +00:00
namespace electron::api {
namespace {
2016-02-02 11:51:15 +00:00
2020-04-03 00:22:46 +00:00
// Ordinarily base::SupportsUserData only supports strong links, where the
// thing to which the user data is attached owns the user data. But we can't
// make the api::DownloadItem owned by the DownloadItem, since it's owned by
// V8. So this makes a weak link. The lifetimes of download::DownloadItem and
// api::DownloadItem are fully independent, and either one may be destroyed
// before the other.
struct UserDataLink : base::SupportsUserData::Data {
explicit UserDataLink(base::WeakPtr<DownloadItem> item)
: download_item(item) {}
base::WeakPtr<DownloadItem> download_item;
};
const void* kElectronApiDownloadItemKey = &kElectronApiDownloadItemKey;
2016-02-02 11:51:15 +00:00
} // namespace
2020-04-03 00:22:46 +00:00
gin::WrapperInfo DownloadItem::kWrapperInfo = {gin::kEmbedderNativeGin};
// static
DownloadItem* DownloadItem::FromDownloadItem(download::DownloadItem* item) {
2020-04-03 00:22:46 +00:00
// ^- say that 7 times fast in a row
auto* data = static_cast<UserDataLink*>(
item->GetUserData(kElectronApiDownloadItemKey));
2020-04-03 00:22:46 +00:00
return data ? data->download_item.get() : nullptr;
}
DownloadItem::DownloadItem(v8::Isolate* isolate, download::DownloadItem* item)
: download_item_(item), isolate_(isolate) {
download_item_->AddObserver(this);
2020-04-03 00:22:46 +00:00
download_item_->SetUserData(
kElectronApiDownloadItemKey,
std::make_unique<UserDataLink>(weak_factory_.GetWeakPtr()));
}
DownloadItem::~DownloadItem() {
2016-02-02 11:51:15 +00:00
if (download_item_) {
// Destroyed by either garbage collection or destroy().
download_item_->RemoveObserver(this);
download_item_->Remove();
}
2020-04-03 00:22:46 +00:00
}
2016-02-02 11:51:15 +00:00
2020-04-03 00:22:46 +00:00
bool DownloadItem::CheckAlive() const {
if (!download_item_) {
gin_helper::ErrorThrower(isolate_).ThrowError(
"DownloadItem used after being destroyed");
2020-04-03 00:22:46 +00:00
return false;
}
return true;
2015-09-20 11:28:33 +00:00
}
void DownloadItem::OnDownloadUpdated(download::DownloadItem* item) {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return;
if (download_item_->IsDone()) {
Emit("done", item->GetState());
2020-04-03 00:22:46 +00:00
Unpin();
} else {
Emit("updated", item->GetState());
}
2015-09-24 07:55:45 +00:00
}
void DownloadItem::OnDownloadDestroyed(download::DownloadItem* /*item*/) {
2016-02-02 11:51:15 +00:00
download_item_ = nullptr;
2020-04-03 00:22:46 +00:00
Unpin();
2015-09-20 11:28:33 +00:00
}
2016-02-02 12:11:39 +00:00
void DownloadItem::Pause() {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return;
2016-02-02 12:11:39 +00:00
download_item_->Pause();
}
2016-06-09 11:51:01 +00:00
bool DownloadItem::IsPaused() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return false;
2016-06-09 11:51:01 +00:00
return download_item_->IsPaused();
}
2016-02-02 12:11:39 +00:00
void DownloadItem::Resume() {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return;
download_item_->Resume(true /* user_gesture */);
}
2016-06-09 11:51:01 +00:00
bool DownloadItem::CanResume() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return false;
2016-06-09 11:51:01 +00:00
return download_item_->CanResume();
}
2016-02-02 12:11:39 +00:00
void DownloadItem::Cancel() {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return;
2016-02-02 12:11:39 +00:00
download_item_->Cancel(true);
}
2016-03-08 14:28:53 +00:00
int64_t DownloadItem::GetReceivedBytes() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return 0;
2016-02-02 12:11:39 +00:00
return download_item_->GetReceivedBytes();
}
2016-03-08 14:28:53 +00:00
int64_t DownloadItem::GetTotalBytes() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return 0;
2016-02-02 12:11:39 +00:00
return download_item_->GetTotalBytes();
}
2016-02-02 12:11:39 +00:00
std::string DownloadItem::GetMimeType() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return "";
return download_item_->GetMimeType();
}
2016-02-02 12:11:39 +00:00
bool DownloadItem::HasUserGesture() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return false;
return download_item_->HasUserGesture();
}
2016-02-02 12:11:39 +00:00
std::string DownloadItem::GetFilename() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return "";
2018-04-18 01:55:30 +00:00
return base::UTF16ToUTF8(
net::GenerateFileName(GetURL(), GetContentDisposition(), std::string(),
download_item_->GetSuggestedFilename(),
GetMimeType(), "download")
.LossyDisplayName());
}
2016-02-02 12:11:39 +00:00
std::string DownloadItem::GetContentDisposition() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return "";
return download_item_->GetContentDisposition();
}
2016-02-02 12:11:39 +00:00
const GURL& DownloadItem::GetURL() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return GURL::EmptyGURL();
2016-02-02 12:11:39 +00:00
return download_item_->GetURL();
}
v8::Local<v8::Value> DownloadItem::GetURLChain() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return v8::Local<v8::Value>();
return gin::ConvertToV8(isolate_, download_item_->GetUrlChain());
}
download::DownloadItem::DownloadState DownloadItem::GetState() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return download::DownloadItem::IN_PROGRESS;
2016-06-09 10:42:32 +00:00
return download_item_->GetState();
}
2016-06-09 11:51:01 +00:00
bool DownloadItem::IsDone() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return false;
2016-06-09 11:51:01 +00:00
return download_item_->IsDone();
}
2016-02-02 12:11:39 +00:00
void DownloadItem::SetSavePath(const base::FilePath& path) {
save_path_ = path;
}
2016-02-02 12:11:39 +00:00
base::FilePath DownloadItem::GetSavePath() const {
return save_path_;
}
file_dialog::DialogSettings DownloadItem::GetSaveDialogOptions() const {
return dialog_options_;
}
void DownloadItem::SetSaveDialogOptions(
const file_dialog::DialogSettings& options) {
dialog_options_ = options;
}
std::string DownloadItem::GetLastModifiedTime() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return "";
return download_item_->GetLastModifiedTime();
}
std::string DownloadItem::GetETag() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return "";
return download_item_->GetETag();
}
double DownloadItem::GetStartTime() const {
2020-04-03 00:22:46 +00:00
if (!CheckAlive())
return 0;
chore: bump chromium to 120.0.6099.0 (main) (#40316) * chore: bump chromium in DEPS to 120.0.6086.0 * chore: update patches * chore: rename FrameSubscriber::OnNewCropVersion() Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4905819 just a simple renaming * chore: rename ToJsTime() to .InMillisecondsFSinceUnixEpoch() Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4956111 function renamed upstream * chore: rename ToDoubleT() to .InSecondsFSinceUnixEpoch() Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4956111 function renamed upstream * chore: rename FromDoubleT() to .FromSecondsSinceUnixEpoch() Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4956111 function renamed upstream * chore: bump chromium in DEPS to 120.0.6088.2 * chore: update patches * chore: regen filenames.libcxx.gni * chore: migrate from (removed upstream) inputFormType to formControlType * chore: bump chromium in DEPS to 120.0.6089.0 * chore: update allow_disabling_blink_scheduler_throttling_per_renderview.patch Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4828507 manually sync to upstream changes + reduce diff size * chore: update patches * chore: bump chromium in DEPS to 120.0.6090.0 * chore: update fix_disabling_background_throttling_in_compositor.patch no manual changes; patch applied with fuzz 2 (4 lines) Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4944206 * chore: update fix_handle_no_top_level_aura_window_in_webcontentsimpl.patch Xref: https://chromium-review.googlesource.com/c/chromium/src/+/4946653 do not patch WebContentsImpl::SetWindowShowState() any longer because it has been removed * chore: update patches * chore: bump chromium in DEPS to 120.0.6091.0 * chore: update patches * chore: bump chromium in DEPS to 120.0.6093.0 * chore: bump chromium in DEPS to 120.0.6095.0 * chore: bump chromium in DEPS to 120.0.6096.0 * chore: bump chromium in DEPS to 120.0.6097.0 * chore: update patches * chore: update patch after rebase * 4961495: [document pip] Focus the window when opened manually https://chromium-review.googlesource.com/c/chromium/src/+/4961495 * [Extensions UserScripts] Store extensions with user scripts in tracker | https://chromium-review.googlesource.com/c/chromium/src/+/4950530 * chore: bump chromium in DEPS to 120.0.6099.0 * chore: update patches * chore: update filenames.libcxx.gni * chore: remove trailing space --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2023-11-01 14:02:12 +00:00
return download_item_->GetStartTime().InSecondsFSinceUnixEpoch();
}
// static
2020-04-03 00:22:46 +00:00
gin::ObjectTemplateBuilder DownloadItem::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<DownloadItem>::GetObjectTemplateBuilder(
isolate)
.SetMethod("pause", &DownloadItem::Pause)
2016-06-09 11:51:01 +00:00
.SetMethod("isPaused", &DownloadItem::IsPaused)
.SetMethod("resume", &DownloadItem::Resume)
2016-06-09 11:51:01 +00:00
.SetMethod("canResume", &DownloadItem::CanResume)
.SetMethod("cancel", &DownloadItem::Cancel)
2015-09-24 07:55:45 +00:00
.SetMethod("getReceivedBytes", &DownloadItem::GetReceivedBytes)
.SetMethod("getTotalBytes", &DownloadItem::GetTotalBytes)
.SetMethod("getMimeType", &DownloadItem::GetMimeType)
.SetMethod("hasUserGesture", &DownloadItem::HasUserGesture)
.SetMethod("getFilename", &DownloadItem::GetFilename)
2015-09-24 07:55:45 +00:00
.SetMethod("getContentDisposition", &DownloadItem::GetContentDisposition)
2016-02-02 12:11:39 +00:00
.SetMethod("getURL", &DownloadItem::GetURL)
.SetMethod("getURLChain", &DownloadItem::GetURLChain)
2016-06-09 10:42:32 +00:00
.SetMethod("getState", &DownloadItem::GetState)
2016-06-09 11:51:01 +00:00
.SetMethod("isDone", &DownloadItem::IsDone)
2016-02-02 12:11:39 +00:00
.SetMethod("setSavePath", &DownloadItem::SetSavePath)
.SetMethod("getSavePath", &DownloadItem::GetSavePath)
.SetProperty("savePath", &DownloadItem::GetSavePath,
&DownloadItem::SetSavePath)
.SetMethod("setSaveDialogOptions", &DownloadItem::SetSaveDialogOptions)
.SetMethod("getSaveDialogOptions", &DownloadItem::GetSaveDialogOptions)
.SetMethod("getLastModifiedTime", &DownloadItem::GetLastModifiedTime)
.SetMethod("getETag", &DownloadItem::GetETag)
.SetMethod("getStartTime", &DownloadItem::GetStartTime);
}
2020-04-03 00:22:46 +00:00
const char* DownloadItem::GetTypeName() {
return "DownloadItem";
}
2015-09-24 07:55:45 +00:00
// static
2020-04-03 00:22:46 +00:00
gin::Handle<DownloadItem> DownloadItem::FromOrCreate(
v8::Isolate* isolate,
download::DownloadItem* item) {
DownloadItem* existing = FromDownloadItem(item);
if (existing)
2020-04-03 00:22:46 +00:00
return gin::CreateHandle(isolate, existing);
auto handle = gin::CreateHandle(isolate, new DownloadItem(isolate, item));
2016-02-02 11:51:15 +00:00
2020-04-03 00:22:46 +00:00
handle->Pin(isolate);
return handle;
}
2022-06-29 19:55:47 +00:00
} // namespace electron::api