electron/shell/browser/auto_updater_mac.mm

177 lines
5.6 KiB
Text
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/auto_updater.h"
#include <string>
2018-04-20 18:47:04 +00:00
#import <ReactiveCocoa/NSObject+RACPropertySubscribing.h>
2014-01-21 14:13:34 +00:00
#import <ReactiveCocoa/RACCommand.h>
#import <ReactiveCocoa/RACSignal.h>
2014-01-21 00:22:49 +00:00
#import <Squirrel/Squirrel.h>
#include "base/bind.h"
#include "base/strings/sys_string_conversions.h"
2018-04-20 18:47:04 +00:00
#include "base/time/time.h"
#include "native_mate/dictionary.h"
#include "shell/browser/browser.h"
#include "shell/common/native_mate_converters/map_converter.h"
#include "shell/common/native_mate_converters/value_converter.h"
2014-02-03 20:41:53 +00:00
2014-01-21 14:13:34 +00:00
namespace auto_updater {
namespace {
2014-01-21 14:13:34 +00:00
// The gloal SQRLUpdater object.
SQRLUpdater* g_updater = nil;
2014-01-21 14:13:34 +00:00
} // namespace
namespace {
bool g_update_available = false;
std::string update_url_ = ""; // NOLINT(runtime/string)
2018-04-20 18:47:04 +00:00
} // namespace
std::string AutoUpdater::GetFeedURL() {
return update_url_;
}
// static
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)) {
2018-04-20 18:47:04 +00:00
args->ThrowError(
"Expected options object to contain a 'url' string property in "
"setFeedUrl call");
return;
}
2018-02-16 02:41:21 +00:00
opts.Get("headers", &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 {
2018-04-20 18:47:04 +00:00
args->ThrowError(
"Expected an options object with a 'url' property to be provided");
return;
}
2018-04-20 18:47:04 +00:00
Delegate* delegate = GetDelegate();
if (!delegate)
return;
update_url_ = feed;
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
for (const auto& it : requestHeaders) {
[urlRequest setValue:base::SysUTF8ToNSString(it.second)
2018-04-20 18:47:04 +00:00
forHTTPHeaderField:base::SysUTF8ToNSString(it.first)];
}
if (g_updater)
[g_updater release];
// Initialize the SQRLUpdater.
@try {
if (serverType == "json") {
2018-04-20 18:47:04 +00:00
NSString* nsAppVersion =
base::SysUTF8ToNSString(electron::Browser::Get()->GetVersion());
2018-04-20 18:47:04 +00:00
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;
2014-01-21 14:13:34 +00:00
}
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
2018-04-20 18:47:04 +00:00
subscribeNext:^(NSNumber* stateNumber) {
int state = [stateNumber integerValue];
// Dispatching the event on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if (state == SQRLUpdaterStateCheckingForUpdate)
delegate->OnCheckingForUpdate();
else if (state == SQRLUpdaterStateDownloadingUpdate)
delegate->OnUpdateAvailable();
});
}];
}
// static
void AutoUpdater::CheckForUpdates() {
Delegate* delegate = GetDelegate();
if (!delegate)
return;
2018-04-20 18:47:04 +00:00
[[[[g_updater.checkForUpdatesCommand execute:nil]
// Send a `nil` after everything...
concat:[RACSignal return:nil]]
// But only take the first value. If an update is sent, we'll get that.
// Otherwise, we'll get our inserted `nil` value.
take:1]
2018-04-20 18:47:04 +00:00
subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
if (downloadedUpdate) {
g_update_available = true;
SQRLUpdate* update = downloadedUpdate.update;
// There is a new update that has been downloaded.
delegate->OnUpdateDownloaded(
2018-04-20 18:47:04 +00:00
base::SysNSStringToUTF8(update.releaseNotes),
base::SysNSStringToUTF8(update.releaseName),
base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
base::SysNSStringToUTF8(update.updateURL.absoluteString));
2014-02-02 11:58:25 +00:00
} else {
g_update_available = false;
// When the completed event is sent with no update, then we know there
// is no update available.
delegate->OnUpdateNotAvailable();
}
2018-04-20 18:47:04 +00:00
}
error:^(NSError* error) {
NSMutableString* failureString =
[NSMutableString stringWithString:error.localizedDescription];
if (error.localizedFailureReason) {
[failureString appendString:@": "];
[failureString appendString:error.localizedFailureReason];
}
if (error.localizedRecoverySuggestion) {
if (![failureString hasSuffix:@"."])
[failureString appendString:@"."];
[failureString appendString:@" "];
[failureString appendString:error.localizedRecoverySuggestion];
}
2017-07-27 01:52:19 +00:00
delegate->OnError(base::SysNSStringToUTF8(failureString), error.code,
base::SysNSStringToUTF8(error.domain));
}];
}
void AutoUpdater::QuitAndInstall() {
2016-04-26 02:15:36 +00:00
Delegate* delegate = AutoUpdater::GetDelegate();
if (g_update_available) {
[[g_updater relaunchToInstallUpdate] subscribeError:^(NSError* error) {
if (delegate)
2017-07-27 01:52:19 +00:00
delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription),
error.code, base::SysNSStringToUTF8(error.domain));
}];
} else {
if (delegate)
delegate->OnError("No update available, can't quit and install");
}
}
} // namespace auto_updater