]> khamidou — GitHub mirrors - electron.git/commitdiff
Implement auto_updater with Squirrel.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 21 Jan 2014 14:13:34 +0000 (22:13 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 21 Jan 2014 14:13:34 +0000 (22:13 +0800)
atom.gyp
browser/api/atom_api_auto_updater.cc
browser/api/atom_api_auto_updater.h
browser/api/lib/auto-updater.coffee
browser/auto_updater.h
browser/auto_updater_delegate.cc [deleted file]
browser/auto_updater_delegate.h
browser/auto_updater_mac.mm

index 765c8004cd66bde67f17843c10ad8222132a36d8..306733e4671d5019b9c66d217a69f14ac12ffbba 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -65,7 +65,6 @@
       'browser/api/atom_browser_bindings.h',
       'browser/auto_updater.cc',
       'browser/auto_updater.h',
-      'browser/auto_updater_delegate.cc',
       'browser/auto_updater_delegate.h',
       'browser/auto_updater_mac.mm',
       'browser/auto_updater_win.cc',
index bfb55252e3f9df7ccbb402019c78ba81210a5d72..f7b5f8e3cfc23996ad8bde8506a8ba170c7b9b8a 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "browser/api/atom_api_auto_updater.h"
 
+#include "base/time/time.h"
 #include "base/values.h"
 #include "browser/auto_updater.h"
 #include "common/v8/native_type_conversions.h"
@@ -17,32 +18,25 @@ namespace api {
 AutoUpdater::AutoUpdater(v8::Handle<v8::Object> wrapper)
     : EventEmitter(wrapper) {
   auto_updater::AutoUpdater::SetDelegate(this);
-  auto_updater::AutoUpdater::Init();
 }
 
 AutoUpdater::~AutoUpdater() {
   auto_updater::AutoUpdater::SetDelegate(NULL);
 }
 
-void AutoUpdater::WillInstallUpdate(const std::string& version,
-                                    const base::Closure& install) {
-  continue_update_ = install;
-
-  base::ListValue args;
-  args.AppendString(version);
-  bool prevent_default = Emit("will-install-update-raw", &args);
-
-  if (!prevent_default)
-    install.Run();
-}
-
-void AutoUpdater::ReadyForUpdateOnQuit(const std::string& version,
-                                       const base::Closure& quit_and_install) {
+void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
+                                     const std::string& release_name,
+                                     const base::Time& release_date,
+                                     const std::string& update_url,
+                                     const base::Closure& quit_and_install) {
   quit_and_install_ = quit_and_install;
 
   base::ListValue args;
-  args.AppendString(version);
-  Emit("ready-for-update-on-quit-raw", &args);
+  args.AppendString(release_notes);
+  args.AppendString(release_name);
+  args.AppendDouble(release_date.ToJsTime());
+  args.AppendString(update_url);
+  Emit("update-downloaded-raw", &args);
 }
 
 // static
@@ -60,39 +54,12 @@ void AutoUpdater::SetFeedURL(const v8::FunctionCallbackInfo<v8::Value>& args) {
   auto_updater::AutoUpdater::SetFeedURL(FromV8Value(args[0]));
 }
 
-// static
-void AutoUpdater::SetAutomaticallyChecksForUpdates(
-    const v8::FunctionCallbackInfo<v8::Value>& args) {
-  auto_updater::AutoUpdater::SetAutomaticallyChecksForUpdates(
-      FromV8Value(args[0]));
-}
-
-// static
-void AutoUpdater::SetAutomaticallyDownloadsUpdates(
-    const v8::FunctionCallbackInfo<v8::Value>& args) {
-  auto_updater::AutoUpdater::SetAutomaticallyDownloadsUpdates(
-      FromV8Value(args[0]));
-}
-
 // static
 void AutoUpdater::CheckForUpdates(
     const v8::FunctionCallbackInfo<v8::Value>& args) {
   auto_updater::AutoUpdater::CheckForUpdates();
 }
 
-// static
-void AutoUpdater::CheckForUpdatesInBackground(
-    const v8::FunctionCallbackInfo<v8::Value>& args) {
-  auto_updater::AutoUpdater::CheckForUpdatesInBackground();
-}
-
-// static
-void AutoUpdater::ContinueUpdate(
-    const v8::FunctionCallbackInfo<v8::Value>& args) {
-  AutoUpdater* self = AutoUpdater::Unwrap<AutoUpdater>(args.This());
-  self->continue_update_.Run();
-}
-
 // static
 void AutoUpdater::QuitAndInstall(
     const v8::FunctionCallbackInfo<v8::Value>& args) {
@@ -110,18 +77,7 @@ void AutoUpdater::Initialize(v8::Handle<v8::Object> target) {
   t->SetClassName(v8::String::NewSymbol("AutoUpdater"));
 
   NODE_SET_PROTOTYPE_METHOD(t, "setFeedUrl", SetFeedURL);
-  NODE_SET_PROTOTYPE_METHOD(t,
-                            "setAutomaticallyChecksForUpdates",
-                            SetAutomaticallyChecksForUpdates);
-  NODE_SET_PROTOTYPE_METHOD(t,
-                            "setAutomaticallyDownloadsUpdates",
-                            SetAutomaticallyDownloadsUpdates);
   NODE_SET_PROTOTYPE_METHOD(t, "checkForUpdates", CheckForUpdates);
-  NODE_SET_PROTOTYPE_METHOD(t,
-                            "checkForUpdatesInBackground",
-                            CheckForUpdatesInBackground);
-
-  NODE_SET_PROTOTYPE_METHOD(t, "continueUpdate", ContinueUpdate);
   NODE_SET_PROTOTYPE_METHOD(t, "quitAndInstall", QuitAndInstall);
 
   target->Set(v8::String::NewSymbol("AutoUpdater"), t->GetFunction());
index 03d649e2ba662a74c891d8c720af36e40fa40849..55d44c4ebc2635983b03470829047b90435ae1e6 100644 (file)
@@ -24,28 +24,23 @@ class AutoUpdater : public EventEmitter,
  protected:
   explicit AutoUpdater(v8::Handle<v8::Object> wrapper);
 
-  virtual void WillInstallUpdate(const std::string& version,
-                                 const base::Closure& install) OVERRIDE;
-  virtual void ReadyForUpdateOnQuit(
-      const std::string& version,
+  // AutoUpdaterDelegate implementations.
+  virtual void OnUpdateDownloaded(
+      const std::string& release_notes,
+      const std::string& release_name,
+      const base::Time& release_date,
+      const std::string& update_url,
       const base::Closure& quit_and_install) OVERRIDE;
 
  private:
   static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
 
   static void SetFeedURL(const v8::FunctionCallbackInfo<v8::Value>& args);
-  static void SetAutomaticallyChecksForUpdates(
-      const v8::FunctionCallbackInfo<v8::Value>& args);
-  static void SetAutomaticallyDownloadsUpdates(
-      const v8::FunctionCallbackInfo<v8::Value>& args);
   static void CheckForUpdates(const v8::FunctionCallbackInfo<v8::Value>& args);
-  static void CheckForUpdatesInBackground(
-      const v8::FunctionCallbackInfo<v8::Value>& args);
 
   static void ContinueUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
   static void QuitAndInstall(const v8::FunctionCallbackInfo<v8::Value>& args);
 
-  base::Closure continue_update_;
   base::Closure quit_and_install_;
 
   DISALLOW_COPY_AND_ASSIGN(AutoUpdater);
index 9d0e998b40d0dd6b5eeae5eda1b0a5075a8c407e..fc29bd3d9647fe5e26db3bcb2e20c507f3003928 100644 (file)
@@ -4,9 +4,8 @@ EventEmitter = require('events').EventEmitter
 AutoUpdater::__proto__ = EventEmitter.prototype
 
 autoUpdater = new AutoUpdater
-autoUpdater.on 'will-install-update-raw', (event, version) ->
-  @emit 'will-install-update', event, version, => @continueUpdate()
-autoUpdater.on 'ready-for-update-on-quit-raw', (event, version) ->
-  @emit 'ready-for-update-on-quit', event, version, => @quitAndInstall()
+autoUpdater.on 'update-downloaded-raw', (args...) ->
+  args[2] = new Date(args[2])  # releaseDate
+  @emit 'update-downloaded', args..., => @quitAndInstall()
 
 module.exports = autoUpdater
index 2166d49f71fd005de6c677e984b69d1984a0c39c..9ddbd35b7ce350d13ebe9e4236b121b85a9e6a5f 100644 (file)
@@ -19,13 +19,8 @@ class AutoUpdater {
   static AutoUpdaterDelegate* GetDelegate();
   static void SetDelegate(AutoUpdaterDelegate* delegate);
 
-  static void Init();
-
   static void SetFeedURL(const std::string& url);
-  static void SetAutomaticallyChecksForUpdates(bool yes);
-  static void SetAutomaticallyDownloadsUpdates(bool yes);
   static void CheckForUpdates();
-  static void CheckForUpdatesInBackground();
 
  private:
   static AutoUpdaterDelegate* delegate_;
diff --git a/browser/auto_updater_delegate.cc b/browser/auto_updater_delegate.cc
deleted file mode 100644 (file)
index 7b5853f..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright (c) 2013 GitHub, Inc. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "browser/auto_updater_delegate.h"
-
-#include "base/callback.h"
-
-namespace auto_updater {
-
-void AutoUpdaterDelegate::WillInstallUpdate(const std::string& version,
-                                            const base::Closure& install) {
-  install.Run();
-}
-
-void AutoUpdaterDelegate::ReadyForUpdateOnQuit(
-    const std::string& version,
-    const base::Closure& quit_and_install) {
-}
-
-}  // namespace auto_updater
index 2c0e450db866d885d245389329968998814e40a6..ba6e4ec933f47791719b7f45d8f0a98d143625f4 100644 (file)
@@ -9,17 +9,20 @@
 
 #include "base/callback_forward.h"
 
+namespace base {
+class Time;
+}
+
 namespace auto_updater {
 
 class AutoUpdaterDelegate {
  public:
-  // The application is going to relaunch to install update.
-  virtual void WillInstallUpdate(const std::string& version,
-                                 const base::Closure& install);
-
-  // User has chosen to update on quit.
-  virtual void ReadyForUpdateOnQuit(const std::string& version,
-                                    const base::Closure& quit_and_install);
+  // There is a new update which has been downloaded.
+  virtual void OnUpdateDownloaded(const std::string& release_notes,
+                                  const std::string& release_name,
+                                  const base::Time& release_date,
+                                  const std::string& update_url,
+                                  const base::Closure& quit_and_install) {}
 
  protected:
   virtual ~AutoUpdaterDelegate() {}
index 31fb5e45bf1900299f86329ad6d2dd44bec93cce..45c6e264fb16210a74d19f2cdbe237547685a5d0 100644 (file)
 
 #include "browser/auto_updater.h"
 
+#import <ReactiveCocoa/RACCommand.h>
+#import <ReactiveCocoa/RACSignal.h>
 #import <Squirrel/Squirrel.h>
 
 #include "base/bind.h"
-#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
 #include "base/strings/sys_string_conversions.h"
 #include "browser/auto_updater_delegate.h"
 
-using auto_updater::AutoUpdaterDelegate;
+namespace auto_updater {
 
 namespace {
 
-struct NSInvocationDeleter {
-  inline void operator()(NSInvocation* invocation) const {
-    [invocation release];
-  }
-};
-
-typedef scoped_ptr<NSInvocation, NSInvocationDeleter> ScopedNSInvocation;
-
-// We are passing the NSInvocation as scoped_ptr, because we want to make sure
-// whether or not the callback is called, the NSInvocation should always be
-// released, the only way to ensure it is to use scoped_ptr.
-// void CallNSInvocation(ScopedNSInvocation invocation) {
-//   [invocation.get() invoke];
-// }
-
-}  // namespace
+// The gloal SQRLUpdater object.
+static SQRLUpdater* g_updater = nil;
 
-// @interface SUUpdaterDelegate : NSObject {
-// }
-// @end
-//
-// @implementation SUUpdaterDelegate
-//
-// - (BOOL)updater:(SUUpdater*)updater
-//         shouldPostponeRelaunchForUpdate:(SUAppcastItem*)update
-//         untilInvoking:(NSInvocation*)invocation {
-//   AutoUpdaterDelegate* delegate = auto_updater::AutoUpdater::GetDelegate();
-//   if (!delegate)
-//     return NO;
-//
-//   std::string version(base::SysNSStringToUTF8([update versionString]));
-//   ScopedNSInvocation invocation_ptr([invocation retain]);
-//   delegate->WillInstallUpdate(
-//       version,
-//       base::Bind(&CallNSInvocation, base::Passed(invocation_ptr.Pass())));
-//
-//   return YES;
-// }
-//
-// - (void)updater:(SUUpdater*)updater
-//         willInstallUpdateOnQuit:(SUAppcastItem*)update
-//         immediateInstallationInvocation:(NSInvocation*)invocation {
-//   AutoUpdaterDelegate* delegate = auto_updater::AutoUpdater::GetDelegate();
-//   if (!delegate)
-//     return;
-//
-//   std::string version(base::SysNSStringToUTF8([update versionString]));
-//   ScopedNSInvocation invocation_ptr([invocation retain]);
-//   delegate->ReadyForUpdateOnQuit(
-//       version,
-//       base::Bind(&CallNSInvocation, base::Passed(invocation_ptr.Pass())));
-// }
-//
-// @end
-
-namespace auto_updater {
-
-// static
-void AutoUpdater::Init() {
-  // SUUpdaterDelegate* delegate = [[SUUpdaterDelegate alloc] init];
-  // [[SUUpdater sharedUpdater] setDelegate:delegate];
+static void RelaunchToInstallUpdate() {
+  if (g_updater != nil)
+    [g_updater relaunchToInstallUpdate];
 }
 
-// static
-void AutoUpdater::SetFeedURL(const std::string& url) {
-  // NSString* url_str(base::SysUTF8ToNSString(url));
-  // [[SUUpdater sharedUpdater] setFeedURL:[NSURL URLWithString:url_str]];
-}
-
-// static
-void AutoUpdater::SetAutomaticallyChecksForUpdates(bool yes) {
-  // [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:yes];
-}
+}  // namespace
 
 // static
-void AutoUpdater::SetAutomaticallyDownloadsUpdates(bool yes) {
-  // [[SUUpdater sharedUpdater] setAutomaticallyDownloadsUpdates:yes];
+void AutoUpdater::SetFeedURL(const std::string& feed) {
+  if (g_updater == nil) {
+    // Initialize the SQRLUpdater.
+    NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
+    NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
+    g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
+
+    // Subscribe to events.
+    [g_updater.updates subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
+       AutoUpdaterDelegate* delegate = GetDelegate();
+       if (!delegate)
+         return;
+
+       SQRLUpdate* update = downloadedUpdate.update;
+       delegate->OnUpdateDownloaded(
+           base::SysNSStringToUTF8(update.releaseNotes),
+           base::SysNSStringToUTF8(update.releaseName),
+           base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
+           base::SysNSStringToUTF8(update.updateURL.absoluteString),
+           base::Bind(RelaunchToInstallUpdate));
+    }];
+  }
 }
 
 // static
 void AutoUpdater::CheckForUpdates() {
-  // [[SUUpdater sharedUpdater] checkForUpdates:nil];
-}
-
-// static
-void AutoUpdater::CheckForUpdatesInBackground() {
-  // [[SUUpdater sharedUpdater] checkForUpdatesInBackground];
+  if (g_updater != nil) {
+    [g_updater.checkForUpdatesCommand execute:nil];
+  }
 }
 
 }  // namespace auto_updater