Merge pull request #6497 from electron/windows-thumbnail
[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(v8::Isolate* isolate) {
38   auto_updater::AutoUpdater::SetDelegate(this);
39   Init(isolate);
40 }
41
42 AutoUpdater::~AutoUpdater() {
43   auto_updater::AutoUpdater::SetDelegate(nullptr);
44 }
45
46 void AutoUpdater::OnError(const std::string& message) {
47   v8::Locker locker(isolate());
48   v8::HandleScope handle_scope(isolate());
49   auto error = v8::Exception::Error(mate::StringToV8(isolate(), message));
50   EmitCustomEvent(
51       "error",
52       error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(),
53       // Message is also emitted to keep compatibility with old code.
54       message);
55 }
56
57 void AutoUpdater::OnCheckingForUpdate() {
58   Emit("checking-for-update");
59 }
60
61 void AutoUpdater::OnUpdateAvailable() {
62   Emit("update-available");
63 }
64
65 void AutoUpdater::OnUpdateNotAvailable() {
66   Emit("update-not-available");
67 }
68
69 void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
70                                      const std::string& release_name,
71                                      const base::Time& release_date,
72                                      const std::string& url) {
73   Emit("update-downloaded", release_notes, release_name, release_date, url,
74        // Keep compatibility with old APIs.
75        base::Bind(&AutoUpdater::QuitAndInstall, base::Unretained(this)));
76 }
77
78 void AutoUpdater::OnWindowAllClosed() {
79   QuitAndInstall();
80 }
81
82 void AutoUpdater::SetFeedURL(const std::string& url, mate::Arguments* args) {
83   auto_updater::AutoUpdater::HeaderMap headers;
84   args->GetNext(&headers);
85   auto_updater::AutoUpdater::SetFeedURL(url, headers);
86 }
87
88 void AutoUpdater::QuitAndInstall() {
89   // If we don't have any window then quitAndInstall immediately.
90   WindowList* window_list = WindowList::GetInstance();
91   if (window_list->size() == 0) {
92     auto_updater::AutoUpdater::QuitAndInstall();
93     return;
94   }
95
96   // Otherwise do the restart after all windows have been closed.
97   window_list->AddObserver(this);
98   for (NativeWindow* window : *window_list)
99     window->Close();
100 }
101
102 // static
103 mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
104   return mate::CreateHandle(isolate, new AutoUpdater(isolate));
105 }
106
107 // static
108 void AutoUpdater::BuildPrototype(
109     v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> prototype) {
110   mate::ObjectTemplateBuilder(isolate, prototype)
111       .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
112       .SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL)
113       .SetMethod("setFeedURL", &AutoUpdater::SetFeedURL)
114       .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
115 }
116
117 }  // namespace api
118
119 }  // namespace atom
120
121
122 namespace {
123
124 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
125                 v8::Local<v8::Context> context, void* priv) {
126   v8::Isolate* isolate = context->GetIsolate();
127   mate::Dictionary dict(isolate, exports);
128   dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate));
129 }
130
131 }  // namespace
132
133 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_auto_updater, Initialize)