Pass real Error object in error event
[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/auto_updater.h"
9 #include "atom/browser/browser.h"
10 #include "atom/common/node_includes.h"
11 #include "native_mate/dictionary.h"
12 #include "native_mate/object_template_builder.h"
13
14 namespace atom {
15
16 namespace api {
17
18 AutoUpdater::AutoUpdater() {
19   auto_updater::AutoUpdater::SetDelegate(this);
20 }
21
22 AutoUpdater::~AutoUpdater() {
23   auto_updater::AutoUpdater::SetDelegate(NULL);
24 }
25
26 void AutoUpdater::OnError(const std::string& message) {
27   v8::Locker locker(isolate());
28   v8::HandleScope handle_scope(isolate());
29   auto error = v8::Exception::Error(mate::StringToV8(isolate(), message));
30   EmitCustomEvent(
31       "error",
32       error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(),
33       // Message is also emitted to keep compatibility with old code.
34       message);
35 }
36
37 void AutoUpdater::OnCheckingForUpdate() {
38   Emit("checking-for-update");
39 }
40
41 void AutoUpdater::OnUpdateAvailable() {
42   Emit("update-available");
43 }
44
45 void AutoUpdater::OnUpdateNotAvailable() {
46   Emit("update-not-available");
47 }
48
49 void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
50                                      const std::string& release_name,
51                                      const base::Time& release_date,
52                                      const std::string& update_url,
53                                      const base::Closure& quit_and_install) {
54   quit_and_install_ = quit_and_install;
55   Emit("update-downloaded-raw", release_notes, release_name,
56        release_date.ToJsTime(), update_url);
57 }
58
59 mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(
60     v8::Isolate* isolate) {
61   return mate::ObjectTemplateBuilder(isolate)
62       .SetMethod("setFeedUrl", &auto_updater::AutoUpdater::SetFeedURL)
63       .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
64       .SetMethod("_quitAndInstall", &AutoUpdater::QuitAndInstall);
65 }
66
67 void AutoUpdater::QuitAndInstall() {
68   if (quit_and_install_.is_null())
69     Browser::Get()->Shutdown();
70   else
71     quit_and_install_.Run();
72 }
73
74 // static
75 mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
76   return CreateHandle(isolate, new AutoUpdater);
77 }
78
79 }  // namespace api
80
81 }  // namespace atom
82
83
84 namespace {
85
86 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
87                 v8::Local<v8::Context> context, void* priv) {
88   v8::Isolate* isolate = context->GetIsolate();
89   mate::Dictionary dict(isolate, exports);
90   dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate));
91 }
92
93 }  // namespace
94
95 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_auto_updater, Initialize)