From: Cheng Zhao Date: Tue, 21 Jan 2014 14:50:46 +0000 (+0800) Subject: Add error and update-not-available events for auto updater. X-Git-Tag: v0.9.5~1^2~14 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=d4e362ec890b7a8acd77032415d789558551d780;p=electron.git Add error and update-not-available events for auto updater. --- diff --git a/browser/api/atom_api_auto_updater.cc b/browser/api/atom_api_auto_updater.cc index f7b5f8e3..5360a315 100644 --- a/browser/api/atom_api_auto_updater.cc +++ b/browser/api/atom_api_auto_updater.cc @@ -24,6 +24,16 @@ AutoUpdater::~AutoUpdater() { auto_updater::AutoUpdater::SetDelegate(NULL); } +void AutoUpdater::OnError(const std::string& error) { + base::ListValue args; + args.AppendString(error); + Emit("error", &args); +} + +void AutoUpdater::OnUpdateNotAvailable() { + Emit("update-not-available"); +} + void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes, const std::string& release_name, const base::Time& release_date, diff --git a/browser/api/atom_api_auto_updater.h b/browser/api/atom_api_auto_updater.h index 55d44c4e..2bb830c2 100644 --- a/browser/api/atom_api_auto_updater.h +++ b/browser/api/atom_api_auto_updater.h @@ -25,6 +25,8 @@ class AutoUpdater : public EventEmitter, explicit AutoUpdater(v8::Handle wrapper); // AutoUpdaterDelegate implementations. + virtual void OnError(const std::string& error) OVERRIDE; + virtual void OnUpdateNotAvailable() OVERRIDE; virtual void OnUpdateDownloaded( const std::string& release_notes, const std::string& release_name, diff --git a/browser/auto_updater_delegate.h b/browser/auto_updater_delegate.h index ba6e4ec9..063cc79d 100644 --- a/browser/auto_updater_delegate.h +++ b/browser/auto_updater_delegate.h @@ -17,6 +17,12 @@ namespace auto_updater { class AutoUpdaterDelegate { public: + // An error happened. + virtual void OnError(const std::string& error) {} + + // There is no available update. + virtual void OnUpdateNotAvailable() {} + // There is a new update which has been downloaded. virtual void OnUpdateDownloaded(const std::string& release_notes, const std::string& release_name, diff --git a/browser/auto_updater_mac.mm b/browser/auto_updater_mac.mm index 45c6e264..63747e9f 100644 --- a/browser/auto_updater_mac.mm +++ b/browser/auto_updater_mac.mm @@ -35,19 +35,32 @@ void AutoUpdater::SetFeedURL(const std::string& feed) { NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url]; g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest]; + AutoUpdaterDelegate* delegate = GetDelegate(); + if (!delegate) + return; + // Subscribe to events. + __block bool has_update = false; [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)); + has_update = true; + + // There is a new update that has been downloaded. + 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)); + } error:^(NSError* error) { + // Something wrong happened. + delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription)); + } completed:^() { + // When the completed event is sent with no update, then we know there + // is no update available. + if (!has_update) + delegate->OnUpdateNotAvailable(); + has_update = false; }]; } }