From a1dc4b88be7b10b135308ef3be57eed534caad13 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 3 Jun 2013 21:51:46 +0800 Subject: [PATCH] Add 'ready-for-update-on-quit' event for auto-updater. --- browser/api/atom_api_auto_updater.cc | 23 ++++++++++++++++++++--- browser/api/atom_api_auto_updater.h | 5 +++++ browser/api/lib/auto-updater.coffee | 2 ++ browser/auto_updater_delegate.cc | 5 +++++ browser/auto_updater_delegate.h | 5 +++++ browser/auto_updater_mac.mm | 14 ++++++++++++++ 6 files changed, 51 insertions(+), 3 deletions(-) diff --git a/browser/api/atom_api_auto_updater.cc b/browser/api/atom_api_auto_updater.cc index 6bd9555233e..e5e2013b6ad 100644 --- a/browser/api/atom_api_auto_updater.cc +++ b/browser/api/atom_api_auto_updater.cc @@ -23,16 +23,25 @@ AutoUpdater::~AutoUpdater() { 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) - continue_update_ = install; - else + if (!prevent_default) install.Run(); } +void AutoUpdater::ReadyForUpdateOnQuit(const std::string& version, + 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); +} + // static v8::Handle AutoUpdater::New(const v8::Arguments &args) { v8::HandleScope scope; @@ -87,6 +96,13 @@ v8::Handle AutoUpdater::ContinueUpdate(const v8::Arguments &args) { return v8::Undefined(); } +// static +v8::Handle AutoUpdater::QuitAndInstall(const v8::Arguments &args) { + AutoUpdater* self = AutoUpdater::Unwrap(args.This()); + self->quit_and_install_.Run(); + return v8::Undefined(); +} + // static void AutoUpdater::Initialize(v8::Handle target) { v8::HandleScope scope; @@ -109,6 +125,7 @@ void AutoUpdater::Initialize(v8::Handle target) { CheckForUpdatesInBackground); NODE_SET_PROTOTYPE_METHOD(t, "continueUpdate", ContinueUpdate); + NODE_SET_PROTOTYPE_METHOD(t, "quitAndInstall", QuitAndInstall); target->Set(v8::String::NewSymbol("AutoUpdater"), t->GetFunction()); } diff --git a/browser/api/atom_api_auto_updater.h b/browser/api/atom_api_auto_updater.h index 7493112d535..d473624e36f 100644 --- a/browser/api/atom_api_auto_updater.h +++ b/browser/api/atom_api_auto_updater.h @@ -26,6 +26,9 @@ class AutoUpdater : public EventEmitter, virtual void WillInstallUpdate(const std::string& version, const base::Closure& install) OVERRIDE; + virtual void ReadyForUpdateOnQuit( + const std::string& version, + const base::Closure& quit_and_install) OVERRIDE; private: static v8::Handle New(const v8::Arguments &args); @@ -40,8 +43,10 @@ class AutoUpdater : public EventEmitter, const v8::Arguments &args); static v8::Handle ContinueUpdate(const v8::Arguments &args); + static v8::Handle QuitAndInstall(const v8::Arguments &args); base::Closure continue_update_; + base::Closure quit_and_install_; DISALLOW_COPY_AND_ASSIGN(AutoUpdater); }; diff --git a/browser/api/lib/auto-updater.coffee b/browser/api/lib/auto-updater.coffee index f7bb2898161..9d0e998b40d 100644 --- a/browser/api/lib/auto-updater.coffee +++ b/browser/api/lib/auto-updater.coffee @@ -6,5 +6,7 @@ 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() module.exports = autoUpdater diff --git a/browser/auto_updater_delegate.cc b/browser/auto_updater_delegate.cc index 671413ef7a8..7b5853ff080 100644 --- a/browser/auto_updater_delegate.cc +++ b/browser/auto_updater_delegate.cc @@ -13,4 +13,9 @@ void AutoUpdaterDelegate::WillInstallUpdate(const std::string& version, install.Run(); } +void AutoUpdaterDelegate::ReadyForUpdateOnQuit( + const std::string& version, + const base::Closure& quit_and_install) { +} + } // namespace auto_updater diff --git a/browser/auto_updater_delegate.h b/browser/auto_updater_delegate.h index 09f29d28314..2c0e450db86 100644 --- a/browser/auto_updater_delegate.h +++ b/browser/auto_updater_delegate.h @@ -13,9 +13,14 @@ 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); + protected: virtual ~AutoUpdaterDelegate() {} }; diff --git a/browser/auto_updater_mac.mm b/browser/auto_updater_mac.mm index eae85ffc442..2c622e8f26d 100644 --- a/browser/auto_updater_mac.mm +++ b/browser/auto_updater_mac.mm @@ -56,6 +56,20 @@ void CallNSInvocation(ScopedNSInvocation invocation) { 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 {