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