Merge pull request #2798 from jonatasfreitasv/master
[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& error) {
27   Emit("error", error);
28 }
29
30 void AutoUpdater::OnCheckingForUpdate() {
31   Emit("checking-for-update");
32 }
33
34 void AutoUpdater::OnUpdateAvailable() {
35   Emit("update-available");
36 }
37
38 void AutoUpdater::OnUpdateNotAvailable() {
39   Emit("update-not-available");
40 }
41
42 void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
43                                      const std::string& release_name,
44                                      const base::Time& release_date,
45                                      const std::string& update_url,
46                                      const base::Closure& quit_and_install) {
47   quit_and_install_ = quit_and_install;
48   Emit("update-downloaded-raw", release_notes, release_name,
49        release_date.ToJsTime(), update_url);
50 }
51
52 mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(
53     v8::Isolate* isolate) {
54   return mate::ObjectTemplateBuilder(isolate)
55       .SetMethod("setFeedUrl", &auto_updater::AutoUpdater::SetFeedURL)
56       .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
57       .SetMethod("_quitAndInstall", &AutoUpdater::QuitAndInstall);
58 }
59
60 void AutoUpdater::QuitAndInstall() {
61   if (quit_and_install_.is_null())
62     Browser::Get()->Shutdown();
63   else
64     quit_and_install_.Run();
65 }
66
67 // static
68 mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
69   return CreateHandle(isolate, new AutoUpdater);
70 }
71
72 }  // namespace api
73
74 }  // namespace atom
75
76
77 namespace {
78
79 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
80                 v8::Local<v8::Context> context, void* priv) {
81   v8::Isolate* isolate = context->GetIsolate();
82   mate::Dictionary dict(isolate, exports);
83   dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate));
84 }
85
86 }  // namespace
87
88 NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_auto_updater, Initialize)