From 6318cd781dd923aa94f732c264cdbed77f235eee Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Thu, 15 Feb 2018 13:58:59 +1100 Subject: [PATCH] Update to use Squirrel.Mac that supports CDN releases --- atom/browser/api/atom_api_auto_updater.cc | 6 +- atom/browser/api/atom_api_auto_updater.h | 2 +- atom/browser/auto_updater.cc | 3 +- atom/browser/auto_updater.h | 4 +- atom/browser/auto_updater_mac.mm | 37 +++++++++++- docs/api/auto-updater.md | 9 ++- .../api/auto-updater/auto-updater-win.js | 10 +++- spec/api-auto-updater-spec.js | 59 +++++++++++++++++++ 8 files changed, 114 insertions(+), 16 deletions(-) diff --git a/atom/browser/api/atom_api_auto_updater.cc b/atom/browser/api/atom_api_auto_updater.cc index bc708b128f73..706931fe61eb 100644 --- a/atom/browser/api/atom_api_auto_updater.cc +++ b/atom/browser/api/atom_api_auto_updater.cc @@ -99,10 +99,8 @@ void AutoUpdater::OnWindowAllClosed() { QuitAndInstall(); } -void AutoUpdater::SetFeedURL(const std::string& url, mate::Arguments* args) { - auto_updater::AutoUpdater::HeaderMap headers; - args->GetNext(&headers); - auto_updater::AutoUpdater::SetFeedURL(url, headers); +void AutoUpdater::SetFeedURL(mate::Arguments* args) { + auto_updater::AutoUpdater::SetFeedURL(args); } void AutoUpdater::QuitAndInstall() { diff --git a/atom/browser/api/atom_api_auto_updater.h b/atom/browser/api/atom_api_auto_updater.h index e9e5c53ade0a..305313df52e5 100644 --- a/atom/browser/api/atom_api_auto_updater.h +++ b/atom/browser/api/atom_api_auto_updater.h @@ -47,7 +47,7 @@ class AutoUpdater : public mate::EventEmitter, private: std::string GetFeedURL(); - void SetFeedURL(const std::string& url, mate::Arguments* args); + void SetFeedURL(mate::Arguments* args); void QuitAndInstall(); DISALLOW_COPY_AND_ASSIGN(AutoUpdater); diff --git a/atom/browser/auto_updater.cc b/atom/browser/auto_updater.cc index 8ada3ff6ab3d..4dc1818cfb83 100644 --- a/atom/browser/auto_updater.cc +++ b/atom/browser/auto_updater.cc @@ -21,8 +21,7 @@ std::string AutoUpdater::GetFeedURL() { return ""; } -void AutoUpdater::SetFeedURL(const std::string& url, - const HeaderMap& requestHeaders) { +void AutoUpdater::SetFeedURL(mate::Arguments* args) { } void AutoUpdater::CheckForUpdates() { diff --git a/atom/browser/auto_updater.h b/atom/browser/auto_updater.h index 389e39d31eb0..ae9178585a7e 100644 --- a/atom/browser/auto_updater.h +++ b/atom/browser/auto_updater.h @@ -10,6 +10,7 @@ #include "base/macros.h" #include "build/build_config.h" +#include "native_mate/arguments.h" namespace base { class Time; @@ -53,8 +54,7 @@ class AutoUpdater { static void SetDelegate(Delegate* delegate); static std::string GetFeedURL(); - static void SetFeedURL(const std::string& url, - const HeaderMap& requestHeaders); + static void SetFeedURL(mate::Arguments* args); static void CheckForUpdates(); static void QuitAndInstall(); diff --git a/atom/browser/auto_updater_mac.mm b/atom/browser/auto_updater_mac.mm index e0317f42b03d..9398ad9b7e63 100644 --- a/atom/browser/auto_updater_mac.mm +++ b/atom/browser/auto_updater_mac.mm @@ -9,9 +9,13 @@ #import #import +#include "atom/browser/browser.h" +#include "atom/common/native_mate_converters/value_converter.h" #include "base/bind.h" #include "base/time/time.h" #include "base/strings/sys_string_conversions.h" +#include "native_mate/converter.h" +#include "native_mate/dictionary.h" namespace auto_updater { @@ -34,8 +38,29 @@ std::string AutoUpdater::GetFeedURL() { } // static -void AutoUpdater::SetFeedURL(const std::string& feed, - const HeaderMap& requestHeaders) { +void AutoUpdater::SetFeedURL(mate::Arguments* args) { + mate::Dictionary opts; + std::string feed; + HeaderMap requestHeaders; + std::string serverType = "default"; + if (args->GetNext(&opts)) { + if (!opts.Get("url", &feed)) { + args->ThrowError("Expected options object to contain a 'url' string property in setFeedUrl call"); + return; + } + opts.Get("requestHeaders", &requestHeaders); + opts.Get("serverType", &serverType); + if (serverType != "default" && serverType != "json") { + args->ThrowError("Expected serverType to be 'default' or 'json'"); + return; + } + } else if (args->GetNext(&feed)) { + args->GetNext(&requestHeaders); + } else { + args->ThrowError("Expected an options object with a 'url' property to be provided"); + return; + } + Delegate* delegate = GetDelegate(); if (!delegate) return; @@ -55,7 +80,13 @@ void AutoUpdater::SetFeedURL(const std::string& feed, // Initialize the SQRLUpdater. @try { - g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest]; + if (serverType == "json") { + NSString* nsAppVersion = base::SysUTF8ToNSString(atom::Browser::Get()->GetVersion()); + g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest forVersion:nsAppVersion]; + } else { + // default + g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest]; + } } @catch (NSException* error) { delegate->OnError(base::SysNSStringToUTF8(error.reason)); return; diff --git a/docs/api/auto-updater.md b/docs/api/auto-updater.md index 8f4a4128b425..8a907fefe6d1 100644 --- a/docs/api/auto-updater.md +++ b/docs/api/auto-updater.md @@ -88,10 +88,13 @@ On Windows only `releaseName` is available. The `autoUpdater` object has the following methods: -### `autoUpdater.setFeedURL(url[, requestHeaders])` +### `autoUpdater.setFeedURL(options)` -* `url` String -* `requestHeaders` Object (optional) _macOS_ - HTTP request headers. +* `options` Object + * `url` String + * `requestHeaders` Object (optional) _macOS_ - HTTP request headers. + * `serverType` String (optional) _macOS_ - Either `json` or `default`, see the [Squirrel.Mac][squirrel-mac] + README for more information. Sets the `url` and initialize the auto updater. diff --git a/lib/browser/api/auto-updater/auto-updater-win.js b/lib/browser/api/auto-updater/auto-updater-win.js index fced12587977..97e6ef002842 100644 --- a/lib/browser/api/auto-updater/auto-updater-win.js +++ b/lib/browser/api/auto-updater/auto-updater-win.js @@ -17,7 +17,15 @@ class AutoUpdater extends EventEmitter { return this.updateURL } - setFeedURL (updateURL, headers) { + setFeedURL (options) { + let updateURL + if (typeof options === 'string') { + updateURL = options + } else if (typeof options === 'object' && typeof options.url === 'string') { + updateURL = options.url + } else { + throw new Error('Expected options.url to be a string but none was provided') + } this.updateURL = updateURL } diff --git a/spec/api-auto-updater-spec.js b/spec/api-auto-updater-spec.js index a9324c1705bd..baa8835600dd 100644 --- a/spec/api-auto-updater-spec.js +++ b/spec/api-auto-updater-spec.js @@ -30,7 +30,45 @@ describe('autoUpdater module', function () { }) describe('setFeedURL', function () { + describe('on Mac or Windows', () => { + const noThrow = (fn) => { + try { fn() } catch (err) {} + } + + before(function () { + if (process.platform !== 'win32' && process.platform !== 'darwin') { + this.skip() + } + }) + + it('sets url successfully using old (url, headers) syntax', () => { + noThrow(() => autoUpdater.setFeedURL('http://electronjs.org', { header: 'val' })) + assert.equal(autoUpdater.getFeedURL(), 'http://electronjs.org') + }) + + it('throws if no url is provided when using the old style', () => { + assert.throws( + () => autoUpdater.setFeedURL(), + err => err.message.includes('Expected an options object with a \'url\' property to be provided') // eslint-disable-line + ) + }) + + it('sets url successfully using new ({ url }) syntax', () => { + noThrow(() => autoUpdater.setFeedURL({ url: 'http://mymagicurl.local' })) + assert.equal(autoUpdater.getFeedURL(), 'http://mymagicurl.local') + }) + + it('throws if no url is provided when using the new style', () => { + assert.throws( + () => autoUpdater.setFeedURL({ noUrl: 'lol' }), + err => err.message.includes('Expected options object to contain a \'url\' string property in setFeedUrl call') // eslint-disable-line + ) + }) + }) + describe('on Mac', function () { + const isServerTypeError = (err) => err.message.includes('Expected serverType to be \'default\' or \'json\'') + before(function () { if (process.platform !== 'darwin') { this.skip() @@ -44,6 +82,27 @@ describe('autoUpdater module', function () { }) autoUpdater.setFeedURL('') }) + + it('does not throw if default is the serverType', () => { + assert.doesNotThrow( + () => autoUpdater.setFeedURL({ url: '', serverType: 'default' }), + isServerTypeError + ) + }) + + it('does not throw if json is the serverType', () => { + assert.doesNotThrow( + () => autoUpdater.setFeedURL({ url: '', serverType: 'default' }), + isServerTypeError + ) + }) + + it('does throw if an unknown string is the serverType', () => { + assert.throws( + () => autoUpdater.setFeedURL({ url: '', serverType: 'weow' }), + isServerTypeError + ) + }) }) })