atom_api_auto_updater.cc 4.8 KB

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