Implement auto_updater with Squirrel.

This commit is contained in:
Cheng Zhao 2014-01-21 22:13:34 +08:00
parent ef8e5505c8
commit df399f7c8c
8 changed files with 63 additions and 182 deletions

View file

@ -65,7 +65,6 @@
'browser/api/atom_browser_bindings.h', 'browser/api/atom_browser_bindings.h',
'browser/auto_updater.cc', 'browser/auto_updater.cc',
'browser/auto_updater.h', 'browser/auto_updater.h',
'browser/auto_updater_delegate.cc',
'browser/auto_updater_delegate.h', 'browser/auto_updater_delegate.h',
'browser/auto_updater_mac.mm', 'browser/auto_updater_mac.mm',
'browser/auto_updater_win.cc', 'browser/auto_updater_win.cc',

View file

@ -4,6 +4,7 @@
#include "browser/api/atom_api_auto_updater.h" #include "browser/api/atom_api_auto_updater.h"
#include "base/time/time.h"
#include "base/values.h" #include "base/values.h"
#include "browser/auto_updater.h" #include "browser/auto_updater.h"
#include "common/v8/native_type_conversions.h" #include "common/v8/native_type_conversions.h"
@ -17,32 +18,25 @@ namespace api {
AutoUpdater::AutoUpdater(v8::Handle<v8::Object> wrapper) AutoUpdater::AutoUpdater(v8::Handle<v8::Object> wrapper)
: EventEmitter(wrapper) { : EventEmitter(wrapper) {
auto_updater::AutoUpdater::SetDelegate(this); auto_updater::AutoUpdater::SetDelegate(this);
auto_updater::AutoUpdater::Init();
} }
AutoUpdater::~AutoUpdater() { AutoUpdater::~AutoUpdater() {
auto_updater::AutoUpdater::SetDelegate(NULL); auto_updater::AutoUpdater::SetDelegate(NULL);
} }
void AutoUpdater::WillInstallUpdate(const std::string& version, void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
const base::Closure& install) { const std::string& release_name,
continue_update_ = install; const base::Time& release_date,
const std::string& update_url,
base::ListValue args; const base::Closure& quit_and_install) {
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) {
quit_and_install_ = quit_and_install; quit_and_install_ = quit_and_install;
base::ListValue args; base::ListValue args;
args.AppendString(version); args.AppendString(release_notes);
Emit("ready-for-update-on-quit-raw", &args); args.AppendString(release_name);
args.AppendDouble(release_date.ToJsTime());
args.AppendString(update_url);
Emit("update-downloaded-raw", &args);
} }
// static // static
@ -60,39 +54,12 @@ void AutoUpdater::SetFeedURL(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto_updater::AutoUpdater::SetFeedURL(FromV8Value(args[0])); 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 // static
void AutoUpdater::CheckForUpdates( void AutoUpdater::CheckForUpdates(
const v8::FunctionCallbackInfo<v8::Value>& args) { const v8::FunctionCallbackInfo<v8::Value>& args) {
auto_updater::AutoUpdater::CheckForUpdates(); 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 // static
void AutoUpdater::QuitAndInstall( void AutoUpdater::QuitAndInstall(
const v8::FunctionCallbackInfo<v8::Value>& args) { const v8::FunctionCallbackInfo<v8::Value>& args) {
@ -110,18 +77,7 @@ void AutoUpdater::Initialize(v8::Handle<v8::Object> target) {
t->SetClassName(v8::String::NewSymbol("AutoUpdater")); t->SetClassName(v8::String::NewSymbol("AutoUpdater"));
NODE_SET_PROTOTYPE_METHOD(t, "setFeedUrl", SetFeedURL); 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, "checkForUpdates", CheckForUpdates);
NODE_SET_PROTOTYPE_METHOD(t,
"checkForUpdatesInBackground",
CheckForUpdatesInBackground);
NODE_SET_PROTOTYPE_METHOD(t, "continueUpdate", ContinueUpdate);
NODE_SET_PROTOTYPE_METHOD(t, "quitAndInstall", QuitAndInstall); NODE_SET_PROTOTYPE_METHOD(t, "quitAndInstall", QuitAndInstall);
target->Set(v8::String::NewSymbol("AutoUpdater"), t->GetFunction()); target->Set(v8::String::NewSymbol("AutoUpdater"), t->GetFunction());

View file

@ -24,28 +24,23 @@ class AutoUpdater : public EventEmitter,
protected: protected:
explicit AutoUpdater(v8::Handle<v8::Object> wrapper); explicit AutoUpdater(v8::Handle<v8::Object> wrapper);
virtual void WillInstallUpdate(const std::string& version, // AutoUpdaterDelegate implementations.
const base::Closure& install) OVERRIDE; virtual void OnUpdateDownloaded(
virtual void ReadyForUpdateOnQuit( const std::string& release_notes,
const std::string& version, const std::string& release_name,
const base::Time& release_date,
const std::string& update_url,
const base::Closure& quit_and_install) OVERRIDE; const base::Closure& quit_and_install) OVERRIDE;
private: private:
static void New(const v8::FunctionCallbackInfo<v8::Value>& args); static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetFeedURL(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 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 ContinueUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
static void QuitAndInstall(const v8::FunctionCallbackInfo<v8::Value>& args); static void QuitAndInstall(const v8::FunctionCallbackInfo<v8::Value>& args);
base::Closure continue_update_;
base::Closure quit_and_install_; base::Closure quit_and_install_;
DISALLOW_COPY_AND_ASSIGN(AutoUpdater); DISALLOW_COPY_AND_ASSIGN(AutoUpdater);

View file

@ -4,9 +4,8 @@ EventEmitter = require('events').EventEmitter
AutoUpdater::__proto__ = EventEmitter.prototype AutoUpdater::__proto__ = EventEmitter.prototype
autoUpdater = new AutoUpdater autoUpdater = new AutoUpdater
autoUpdater.on 'will-install-update-raw', (event, version) -> autoUpdater.on 'update-downloaded-raw', (args...) ->
@emit 'will-install-update', event, version, => @continueUpdate() args[2] = new Date(args[2]) # releaseDate
autoUpdater.on 'ready-for-update-on-quit-raw', (event, version) -> @emit 'update-downloaded', args..., => @quitAndInstall()
@emit 'ready-for-update-on-quit', event, version, => @quitAndInstall()
module.exports = autoUpdater module.exports = autoUpdater

View file

@ -19,13 +19,8 @@ class AutoUpdater {
static AutoUpdaterDelegate* GetDelegate(); static AutoUpdaterDelegate* GetDelegate();
static void SetDelegate(AutoUpdaterDelegate* delegate); static void SetDelegate(AutoUpdaterDelegate* delegate);
static void Init();
static void SetFeedURL(const std::string& url); static void SetFeedURL(const std::string& url);
static void SetAutomaticallyChecksForUpdates(bool yes);
static void SetAutomaticallyDownloadsUpdates(bool yes);
static void CheckForUpdates(); static void CheckForUpdates();
static void CheckForUpdatesInBackground();
private: private:
static AutoUpdaterDelegate* delegate_; static AutoUpdaterDelegate* delegate_;

View file

@ -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

View file

@ -9,17 +9,20 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
namespace base {
class Time;
}
namespace auto_updater { namespace auto_updater {
class AutoUpdaterDelegate { class AutoUpdaterDelegate {
public: public:
// The application is going to relaunch to install update. // There is a new update which has been downloaded.
virtual void WillInstallUpdate(const std::string& version, virtual void OnUpdateDownloaded(const std::string& release_notes,
const base::Closure& install); const std::string& release_name,
const base::Time& release_date,
// User has chosen to update on quit. const std::string& update_url,
virtual void ReadyForUpdateOnQuit(const std::string& version, const base::Closure& quit_and_install) {}
const base::Closure& quit_and_install);
protected: protected:
virtual ~AutoUpdaterDelegate() {} virtual ~AutoUpdaterDelegate() {}

View file

@ -4,104 +4,59 @@
#include "browser/auto_updater.h" #include "browser/auto_updater.h"
#import <ReactiveCocoa/RACCommand.h>
#import <ReactiveCocoa/RACSignal.h>
#import <Squirrel/Squirrel.h> #import <Squirrel/Squirrel.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/scoped_ptr.h" #include "base/time/time.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "browser/auto_updater_delegate.h" #include "browser/auto_updater_delegate.h"
using auto_updater::AutoUpdaterDelegate; namespace auto_updater {
namespace { namespace {
struct NSInvocationDeleter { // The gloal SQRLUpdater object.
inline void operator()(NSInvocation* invocation) const { static SQRLUpdater* g_updater = nil;
[invocation release];
}
};
typedef scoped_ptr<NSInvocation, NSInvocationDeleter> ScopedNSInvocation; static void RelaunchToInstallUpdate() {
if (g_updater != nil)
// We are passing the NSInvocation as scoped_ptr, because we want to make sure [g_updater relaunchToInstallUpdate];
// 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 } // namespace
// @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 // static
void AutoUpdater::Init() { void AutoUpdater::SetFeedURL(const std::string& feed) {
// SUUpdaterDelegate* delegate = [[SUUpdaterDelegate alloc] init]; if (g_updater == nil) {
// [[SUUpdater sharedUpdater] setDelegate:delegate]; // Initialize the SQRLUpdater.
} NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
// static // Subscribe to events.
void AutoUpdater::SetFeedURL(const std::string& url) { [g_updater.updates subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
// NSString* url_str(base::SysUTF8ToNSString(url)); AutoUpdaterDelegate* delegate = GetDelegate();
// [[SUUpdater sharedUpdater] setFeedURL:[NSURL URLWithString:url_str]]; if (!delegate)
} return;
// static SQRLUpdate* update = downloadedUpdate.update;
void AutoUpdater::SetAutomaticallyChecksForUpdates(bool yes) { delegate->OnUpdateDownloaded(
// [[SUUpdater sharedUpdater] setAutomaticallyChecksForUpdates:yes]; base::SysNSStringToUTF8(update.releaseNotes),
} base::SysNSStringToUTF8(update.releaseName),
base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
// static base::SysNSStringToUTF8(update.updateURL.absoluteString),
void AutoUpdater::SetAutomaticallyDownloadsUpdates(bool yes) { base::Bind(RelaunchToInstallUpdate));
// [[SUUpdater sharedUpdater] setAutomaticallyDownloadsUpdates:yes]; }];
}
} }
// static // static
void AutoUpdater::CheckForUpdates() { void AutoUpdater::CheckForUpdates() {
// [[SUUpdater sharedUpdater] checkForUpdates:nil]; if (g_updater != nil) {
} [g_updater.checkForUpdatesCommand execute:nil];
}
// static
void AutoUpdater::CheckForUpdatesInBackground() {
// [[SUUpdater sharedUpdater] checkForUpdatesInBackground];
} }
} // namespace auto_updater } // namespace auto_updater