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