Merge remote-tracking branch 'refs/remotes/atom/master'
[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 "base/time/time.h"
8 #include "atom/browser/browser.h"
9 #include "atom/browser/native_window.h"
10 #include "atom/browser/window_list.h"
11 #include "atom/common/native_mate_converters/callback.h"
12 #include "atom/common/node_includes.h"
13 #include "native_mate/dictionary.h"
14 #include "native_mate/object_template_builder.h"
15
16 namespace mate {
17
18 template<>
19 struct Converter<base::Time> {
20   static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
21                                    const base::Time& val) {
22     v8::MaybeLocal<v8::Value> date = v8::Date::New(
23         isolate->GetCurrentContext(), val.ToJsTime());
24     if (date.IsEmpty())
25       return v8::Null(isolate);
26     else
27       return date.ToLocalChecked();
28   }
29 };
30
31 }  // namespace mate
32
33 namespace atom {
34
35 namespace api {
36
37 AutoUpdater::AutoUpdater() {
38   auto_updater::AutoUpdater::SetDelegate(this);
39 }
40
41 AutoUpdater::~AutoUpdater() {
42   auto_updater::AutoUpdater::SetDelegate(nullptr);
43 }
44
45 void AutoUpdater::OnError(const std::string& message) {
46   v8::Locker locker(isolate());
47   v8::HandleScope handle_scope(isolate());
48   auto error = v8::Exception::Error(mate::StringToV8(isolate(), message));
49   EmitCustomEvent(
50       "error",
51       error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(),
52       // Message is also emitted to keep compatibility with old code.
53       message);
54 }
55
56 void AutoUpdater::OnCheckingForUpdate() {
57   Emit("checking-for-update");
58 }
59
60 void AutoUpdater::OnUpdateAvailable() {
61   Emit("update-available");
62 }
63
64 void AutoUpdater::OnUpdateNotAvailable() {
65   Emit("update-not-available");
66 }
67
68 void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
69                                      const std::string& release_name,
70                                      const base::Time& release_date,
71                                      const std::string& url) {
72   Emit("update-downloaded", release_notes, release_name, release_date, url,
73        // Keep compatibility with old APIs.
74        base::Bind(&AutoUpdater::QuitAndInstall, base::Unretained(this)));
75 }
76
77 void AutoUpdater::OnWindowAllClosed() {
78   QuitAndInstall();
79 }
80
81 mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(
82     v8::Isolate* isolate) {
83   return mate::ObjectTemplateBuilder(isolate)
84       .SetMethod("setFeedURL", &auto_updater::AutoUpdater::SetFeedURL)
85       .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
86       .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
87 }
88
89 void AutoUpdater::QuitAndInstall() {
90   // If we don't have any window then quitAndInstall immediately.
91   WindowList* window_list = WindowList::GetInstance();
92   if (window_list->size() == 0) {
93     auto_updater::AutoUpdater::QuitAndInstall();
94     return;
95   }
96
97   // Otherwise do the restart after all windows have been closed.
98   window_list->AddObserver(this);
99   for (NativeWindow* window : *window_list)
100     window->Close();
101 }
102
103 // static
104 mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
105   return CreateHandle(isolate, new AutoUpdater);
106 }
107
108 }  // namespace api
109
110 }  // namespace atom
111
112
113 namespace {
114
115 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
116                 v8::Local<v8::Context> context, void* priv) {
117   v8::Isolate* isolate = context->GetIsolate();
118   mate::Dictionary dict(isolate, exports);
119   dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate));
120 }
121
122 }  // namespace
123
124 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_auto_updater, Initialize)