Run Tizen Webapps in single process mode
[platform/framework/web/crosswalk-tizen.git] / atom / browser / api / atom_api_auto_updater.cc
1 // Copyright (c) 2013 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/atom_api_auto_updater.h"
6
7 #include "atom/browser/browser.h"
8 #include "atom/browser/native_window.h"
9 #include "atom/browser/window_list.h"
10 #include "atom/common/api/event_emitter_caller.h"
11 #include "atom/common/native_mate_converters/callback.h"
12 #include "atom/common/node_includes.h"
13 #include "base/time/time.h"
14 #include "native_mate/dictionary.h"
15 #include "native_mate/object_template_builder.h"
16 #include "tizen/common/env_variables.h"
17
18 namespace mate {
19
20 template<>
21 struct Converter<base::Time> {
22   static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
23                                    const base::Time& val) {
24     v8::MaybeLocal<v8::Value> date = v8::Date::New(
25         isolate->GetCurrentContext(), val.ToJsTime());
26     if (date.IsEmpty())
27       return v8::Null(isolate);
28     else
29       return date.ToLocalChecked();
30   }
31 };
32
33 }  // namespace mate
34
35 namespace atom {
36
37 namespace api {
38
39 AutoUpdater::AutoUpdater(v8::Isolate* isolate) {
40   auto_updater::AutoUpdater::SetDelegate(this);
41   Init(isolate);
42 }
43
44 AutoUpdater::~AutoUpdater() {
45   auto_updater::AutoUpdater::SetDelegate(nullptr);
46 }
47
48 void AutoUpdater::OnError(const std::string& message) {
49   if (!::tizen::is_single_process)
50     v8::Locker locker(isolate());
51   v8::HandleScope handle_scope(isolate());
52   auto error = v8::Exception::Error(mate::StringToV8(isolate(), message));
53   mate::EmitEvent(
54       isolate(),
55       GetWrapper(),
56       "error",
57       error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(),
58       // Message is also emitted to keep compatibility with old code.
59       message);
60 }
61
62 void AutoUpdater::OnCheckingForUpdate() {
63   Emit("checking-for-update");
64 }
65
66 void AutoUpdater::OnUpdateAvailable() {
67   Emit("update-available");
68 }
69
70 void AutoUpdater::OnUpdateNotAvailable() {
71   Emit("update-not-available");
72 }
73
74 void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
75                                      const std::string& release_name,
76                                      const base::Time& release_date,
77                                      const std::string& url) {
78   Emit("update-downloaded", release_notes, release_name, release_date, url,
79        // Keep compatibility with old APIs.
80        base::Bind(&AutoUpdater::QuitAndInstall, base::Unretained(this)));
81 }
82
83 void AutoUpdater::OnWindowAllClosed() {
84   QuitAndInstall();
85 }
86
87 void AutoUpdater::SetFeedURL(const std::string& url, mate::Arguments* args) {
88   auto_updater::AutoUpdater::HeaderMap headers;
89   args->GetNext(&headers);
90   auto_updater::AutoUpdater::SetFeedURL(url, headers);
91 }
92
93 void AutoUpdater::QuitAndInstall() {
94   // If we don't have any window then quitAndInstall immediately.
95   if (WindowList::IsEmpty()) {
96     auto_updater::AutoUpdater::QuitAndInstall();
97     return;
98   }
99
100   // Otherwise do the restart after all windows have been closed.
101   WindowList::AddObserver(this);
102   WindowList::CloseAllWindows();
103 }
104
105 // static
106 mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
107   return mate::CreateHandle(isolate, new AutoUpdater(isolate));
108 }
109
110 // static
111 void AutoUpdater::BuildPrototype(
112     v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
113   prototype->SetClassName(mate::StringToV8(isolate, "AutoUpdater"));
114   mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
115       .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
116       .SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL)
117       .SetMethod("setFeedURL", &AutoUpdater::SetFeedURL)
118       .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
119 }
120
121 }  // namespace api
122
123 }  // namespace atom
124
125
126 namespace {
127
128 using atom::api::AutoUpdater;
129
130 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
131                 v8::Local<v8::Context> context, void* priv) {
132   v8::Isolate* isolate = context->GetIsolate();
133   mate::Dictionary dict(isolate, exports);
134   dict.Set("autoUpdater", AutoUpdater::Create(isolate));
135   dict.Set("AutoUpdater", AutoUpdater::GetConstructor(isolate)->GetFunction());
136 }
137
138 }  // namespace
139
140 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_auto_updater, Initialize)