auto_updater_mac.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "shell/browser/auto_updater.h"
  5. #include <string>
  6. #import <ReactiveCocoa/NSObject+RACPropertySubscribing.h>
  7. #import <ReactiveCocoa/RACCommand.h>
  8. #import <ReactiveCocoa/RACSignal.h>
  9. #import <Squirrel/Squirrel.h>
  10. #include "base/bind.h"
  11. #include "base/strings/sys_string_conversions.h"
  12. #include "base/time/time.h"
  13. #include "gin/arguments.h"
  14. #include "shell/browser/browser.h"
  15. #include "shell/common/gin_converters/value_converter_gin_adapter.h"
  16. #include "shell/common/gin_helper/dictionary.h"
  17. #include "shell/common/gin_helper/error_thrower.h"
  18. namespace auto_updater {
  19. namespace {
  20. // The gloal SQRLUpdater object.
  21. SQRLUpdater* g_updater = nil;
  22. } // namespace
  23. namespace {
  24. bool g_update_available = false;
  25. std::string update_url_ = ""; // NOLINT(runtime/string)
  26. } // namespace
  27. std::string AutoUpdater::GetFeedURL() {
  28. return update_url_;
  29. }
  30. // static
  31. void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) {
  32. gin_helper::ErrorThrower thrower(args->isolate());
  33. gin_helper::Dictionary opts;
  34. std::string feed;
  35. HeaderMap requestHeaders;
  36. std::string serverType = "default";
  37. if (args->GetNext(&opts)) {
  38. if (!opts.Get("url", &feed)) {
  39. thrower.ThrowError(
  40. "Expected options object to contain a 'url' string property in "
  41. "setFeedUrl call");
  42. return;
  43. }
  44. opts.Get("headers", &requestHeaders);
  45. opts.Get("serverType", &serverType);
  46. if (serverType != "default" && serverType != "json") {
  47. thrower.ThrowError("Expected serverType to be 'default' or 'json'");
  48. return;
  49. }
  50. } else if (args->GetNext(&feed)) {
  51. args->GetNext(&requestHeaders);
  52. } else {
  53. thrower.ThrowError(
  54. "Expected an options object with a 'url' property to be provided");
  55. return;
  56. }
  57. Delegate* delegate = GetDelegate();
  58. if (!delegate)
  59. return;
  60. update_url_ = feed;
  61. NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
  62. NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
  63. for (const auto& it : requestHeaders) {
  64. [urlRequest setValue:base::SysUTF8ToNSString(it.second)
  65. forHTTPHeaderField:base::SysUTF8ToNSString(it.first)];
  66. }
  67. if (g_updater)
  68. [g_updater release];
  69. // Initialize the SQRLUpdater.
  70. @try {
  71. if (serverType == "json") {
  72. NSString* nsAppVersion =
  73. base::SysUTF8ToNSString(electron::Browser::Get()->GetVersion());
  74. g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest
  75. forVersion:nsAppVersion];
  76. } else {
  77. // default
  78. g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
  79. }
  80. } @catch (NSException* error) {
  81. delegate->OnError(base::SysNSStringToUTF8(error.reason));
  82. return;
  83. }
  84. [[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
  85. subscribeNext:^(NSNumber* stateNumber) {
  86. int state = [stateNumber integerValue];
  87. // Dispatching the event on main thread.
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. if (state == SQRLUpdaterStateCheckingForUpdate)
  90. delegate->OnCheckingForUpdate();
  91. else if (state == SQRLUpdaterStateDownloadingUpdate)
  92. delegate->OnUpdateAvailable();
  93. });
  94. }];
  95. }
  96. // static
  97. void AutoUpdater::CheckForUpdates() {
  98. Delegate* delegate = GetDelegate();
  99. if (!delegate)
  100. return;
  101. [[[[g_updater.checkForUpdatesCommand execute:nil]
  102. // Send a `nil` after everything...
  103. concat:[RACSignal return:nil]]
  104. // But only take the first value. If an update is sent, we'll get that.
  105. // Otherwise, we'll get our inserted `nil` value.
  106. take:1]
  107. subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
  108. if (downloadedUpdate) {
  109. g_update_available = true;
  110. SQRLUpdate* update = downloadedUpdate.update;
  111. // There is a new update that has been downloaded.
  112. delegate->OnUpdateDownloaded(
  113. base::SysNSStringToUTF8(update.releaseNotes),
  114. base::SysNSStringToUTF8(update.releaseName),
  115. base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
  116. base::SysNSStringToUTF8(update.updateURL.absoluteString));
  117. } else {
  118. g_update_available = false;
  119. // When the completed event is sent with no update, then we know there
  120. // is no update available.
  121. delegate->OnUpdateNotAvailable();
  122. }
  123. }
  124. error:^(NSError* error) {
  125. NSMutableString* failureString =
  126. [NSMutableString stringWithString:error.localizedDescription];
  127. if (error.localizedFailureReason) {
  128. [failureString appendString:@": "];
  129. [failureString appendString:error.localizedFailureReason];
  130. }
  131. if (error.localizedRecoverySuggestion) {
  132. if (![failureString hasSuffix:@"."])
  133. [failureString appendString:@"."];
  134. [failureString appendString:@" "];
  135. [failureString appendString:error.localizedRecoverySuggestion];
  136. }
  137. delegate->OnError(base::SysNSStringToUTF8(failureString), error.code,
  138. base::SysNSStringToUTF8(error.domain));
  139. }];
  140. }
  141. void AutoUpdater::QuitAndInstall() {
  142. Delegate* delegate = AutoUpdater::GetDelegate();
  143. if (g_update_available) {
  144. [[g_updater relaunchToInstallUpdate] subscribeError:^(NSError* error) {
  145. if (delegate)
  146. delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription),
  147. error.code, base::SysNSStringToUTF8(error.domain));
  148. }];
  149. } else {
  150. if (delegate)
  151. delegate->OnError("No update available, can't quit and install");
  152. }
  153. }
  154. } // namespace auto_updater