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
|
|
|
|
|
|
|
#include <string>
|
2019-02-21 12:32:44 +00:00
|
|
|
#include <utility>
|
2015-10-14 04:41:31 +00:00
|
|
|
|
|
|
|
#include "base/callback.h"
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/atom_browser_context.h"
|
2015-10-14 04:41:31 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
SavePageHandler::SavePageHandler(content::WebContents* web_contents,
|
2019-02-21 12:32:44 +00:00
|
|
|
util::Promise promise)
|
|
|
|
: web_contents_(web_contents), promise_(std::move(promise)) {}
|
2015-10-14 04:41:31 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
SavePageHandler::~SavePageHandler() {}
|
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) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* download_manager = content::BrowserContext::GetDownloadManager(
|
2015-10-14 04:41:31 +00:00
|
|
|
web_contents_->GetBrowserContext());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|