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.
5 #include "atom/browser/api/atom_api_auto_updater.h"
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"
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());
26 return v8::Null(isolate);
28 return date.ToLocalChecked();
38 AutoUpdater::AutoUpdater(v8::Isolate* isolate) {
39 auto_updater::AutoUpdater::SetDelegate(this);
43 AutoUpdater::~AutoUpdater() {
44 auto_updater::AutoUpdater::SetDelegate(nullptr);
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));
55 error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(),
56 // Message is also emitted to keep compatibility with old code.
60 void AutoUpdater::OnCheckingForUpdate() {
61 Emit("checking-for-update");
64 void AutoUpdater::OnUpdateAvailable() {
65 Emit("update-available");
68 void AutoUpdater::OnUpdateNotAvailable() {
69 Emit("update-not-available");
72 void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
73 const std::string& release_name,
74 const base::Time& release_date,
75 const std::string& url) {
76 Emit("update-downloaded", release_notes, release_name, release_date, url,
77 // Keep compatibility with old APIs.
78 base::Bind(&AutoUpdater::QuitAndInstall, base::Unretained(this)));
81 void AutoUpdater::OnWindowAllClosed() {
85 void AutoUpdater::SetFeedURL(const std::string& url, mate::Arguments* args) {
86 auto_updater::AutoUpdater::HeaderMap headers;
87 args->GetNext(&headers);
88 auto_updater::AutoUpdater::SetFeedURL(url, headers);
91 void AutoUpdater::QuitAndInstall() {
92 // If we don't have any window then quitAndInstall immediately.
93 if (WindowList::IsEmpty()) {
94 auto_updater::AutoUpdater::QuitAndInstall();
98 // Otherwise do the restart after all windows have been closed.
99 WindowList::AddObserver(this);
100 WindowList::CloseAllWindows();
104 mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
105 return mate::CreateHandle(isolate, new AutoUpdater(isolate));
109 void AutoUpdater::BuildPrototype(
110 v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
111 prototype->SetClassName(mate::StringToV8(isolate, "AutoUpdater"));
112 mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
113 .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
114 .SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL)
115 .SetMethod("setFeedURL", &AutoUpdater::SetFeedURL)
116 .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
126 using atom::api::AutoUpdater;
128 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
129 v8::Local<v8::Context> context, void* priv) {
130 v8::Isolate* isolate = context->GetIsolate();
131 mate::Dictionary dict(isolate, exports);
132 dict.Set("autoUpdater", AutoUpdater::Create(isolate));
133 dict.Set("AutoUpdater", AutoUpdater::GetConstructor(isolate)->GetFunction());
138 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_auto_updater, Initialize)