Run Tizen Webapps in single process mode
[platform/framework/web/crosswalk-tizen.git] / atom / browser / api / save_page_handler.cc
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/save_page_handler.h"
6
7 #include <string>
8
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"
14
15 namespace atom {
16
17 namespace api {
18
19 SavePageHandler::SavePageHandler(content::WebContents* web_contents,
20                                  const SavePageCallback& callback)
21     : web_contents_(web_contents),
22       callback_(callback) {
23 }
24
25 SavePageHandler::~SavePageHandler() {
26 }
27
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);
33 }
34
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,
47                                         save_type);
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.
51   if (!result)
52     delete this;
53   return result;
54 }
55
56 void SavePageHandler::OnDownloadUpdated(content::DownloadItem* item) {
57   if (item->IsDone()) {
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));
64     } else {
65       v8::Local<v8::String> error_message = v8::String::NewFromUtf8(
66           isolate, "Fail to save page");
67       callback_.Run(v8::Exception::Error(error_message));
68     }
69     Destroy(item);
70   }
71 }
72
73 void SavePageHandler::Destroy(content::DownloadItem* item) {
74   item->RemoveObserver(this);
75   delete this;
76 }
77
78 }  // namespace api
79
80 }  // namespace atom