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.
5 #include "atom/browser/api/save_page_handler.h"
9 #include "atom/browser/atom_browser_context.h"
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "content/public/browser/web_contents.h"
13 #include "tizen/common/env_variables.h"
19 SavePageHandler::SavePageHandler(content::WebContents* web_contents,
20 const SavePageCallback& callback)
21 : web_contents_(web_contents),
25 SavePageHandler::~SavePageHandler() {
28 void SavePageHandler::OnDownloadCreated(content::DownloadManager* manager,
29 content::DownloadItem* item) {
30 // OnDownloadCreated is invoked during WebContents::SavePage, so the |item|
31 // here is the one stated by WebContents::SavePage.
32 item->AddObserver(this);
35 bool SavePageHandler::Handle(const base::FilePath& full_path,
36 const content::SavePageType& save_type) {
37 auto download_manager = content::BrowserContext::GetDownloadManager(
38 web_contents_->GetBrowserContext());
39 download_manager->AddObserver(this);
40 // Chromium will create a 'foo_files' directory under the directory of saving
41 // page 'foo.html' for holding other resource files of 'foo.html'.
42 base::FilePath saved_main_directory_path = full_path.DirName().Append(
43 full_path.RemoveExtension().BaseName().value() +
44 FILE_PATH_LITERAL("_files"));
45 bool result = web_contents_->SavePage(full_path,
46 saved_main_directory_path,
48 download_manager->RemoveObserver(this);
49 // If initialization fails which means fail to create |DownloadItem|, we need
50 // to delete the |SavePageHandler| instance to avoid memory-leak.
56 void SavePageHandler::OnDownloadUpdated(content::DownloadItem* item) {
58 v8::Isolate* isolate = v8::Isolate::GetCurrent();
59 if (!::tizen::is_single_process)
60 v8::Locker locker(isolate);
61 v8::HandleScope handle_scope(isolate);
62 if (item->GetState() == content::DownloadItem::COMPLETE) {
63 callback_.Run(v8::Null(isolate));
65 v8::Local<v8::String> error_message = v8::String::NewFromUtf8(
66 isolate, "Fail to save page");
67 callback_.Run(v8::Exception::Error(error_message));
73 void SavePageHandler::Destroy(content::DownloadItem* item) {
74 item->RemoveObserver(this);