2015-10-14 04:41:31 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/save_page_handler.h"
|
2015-10-14 04:41:31 +00:00
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
#include <utility>
|
2015-10-14 04:41:31 +00:00
|
|
|
|
|
|
|
#include "base/files/file_path.h"
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/callback.h"
|
2015-10-14 04:41:31 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2015-10-14 04:41:31 +00:00
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2015-10-14 04:41:31 +00:00
|
|
|
|
|
|
|
SavePageHandler::SavePageHandler(content::WebContents* web_contents,
|
2019-11-01 06:10:32 +00:00
|
|
|
gin_helper::Promise<void> promise)
|
2019-02-21 12:32:44 +00:00
|
|
|
: web_contents_(web_contents), promise_(std::move(promise)) {}
|
2015-10-14 04:41:31 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
SavePageHandler::~SavePageHandler() = default;
|
2015-10-14 04:41:31 +00:00
|
|
|
|
|
|
|
void SavePageHandler::OnDownloadCreated(content::DownloadManager* manager,
|
2018-04-10 15:29:26 +00:00
|
|
|
download::DownloadItem* item) {
|
2015-10-14 04:41:31 +00:00
|
|
|
// OnDownloadCreated is invoked during WebContents::SavePage, so the |item|
|
|
|
|
// here is the one stated by WebContents::SavePage.
|
|
|
|
item->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SavePageHandler::Handle(const base::FilePath& full_path,
|
|
|
|
const content::SavePageType& save_type) {
|
2021-05-19 23:15:47 +00:00
|
|
|
auto* download_manager =
|
|
|
|
web_contents_->GetBrowserContext()->GetDownloadManager();
|
2015-10-14 04:41:31 +00:00
|
|
|
download_manager->AddObserver(this);
|
2015-10-18 03:28:05 +00:00
|
|
|
// Chromium will create a 'foo_files' directory under the directory of saving
|
|
|
|
// page 'foo.html' for holding other resource files of 'foo.html'.
|
|
|
|
base::FilePath saved_main_directory_path = full_path.DirName().Append(
|
|
|
|
full_path.RemoveExtension().BaseName().value() +
|
|
|
|
FILE_PATH_LITERAL("_files"));
|
2018-04-18 01:55:30 +00:00
|
|
|
bool result =
|
|
|
|
web_contents_->SavePage(full_path, saved_main_directory_path, save_type);
|
2015-10-14 04:41:31 +00:00
|
|
|
download_manager->RemoveObserver(this);
|
|
|
|
// If initialization fails which means fail to create |DownloadItem|, we need
|
|
|
|
// to delete the |SavePageHandler| instance to avoid memory-leak.
|
2019-02-21 12:32:44 +00:00
|
|
|
if (!result) {
|
|
|
|
promise_.RejectWithErrorMessage("Failed to save the page");
|
2015-10-14 04:41:31 +00:00
|
|
|
delete this;
|
2019-02-21 12:32:44 +00:00
|
|
|
}
|
2015-10-14 04:41:31 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-10 15:29:26 +00:00
|
|
|
void SavePageHandler::OnDownloadUpdated(download::DownloadItem* item) {
|
2015-10-14 04:41:31 +00:00
|
|
|
if (item->IsDone()) {
|
2019-02-14 17:03:28 +00:00
|
|
|
if (item->GetState() == download::DownloadItem::COMPLETE)
|
2019-02-21 12:32:44 +00:00
|
|
|
promise_.Resolve();
|
2019-02-14 17:03:28 +00:00
|
|
|
else
|
2019-02-21 12:32:44 +00:00
|
|
|
promise_.RejectWithErrorMessage("Failed to save the page.");
|
2015-10-14 04:41:31 +00:00
|
|
|
Destroy(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 15:29:26 +00:00
|
|
|
void SavePageHandler::Destroy(download::DownloadItem* item) {
|
2015-10-14 04:41:31 +00:00
|
|
|
item->RemoveObserver(this);
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|