2013-06-02 12:23:04 +00:00
|
|
|
// 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.h"
|
|
|
|
|
2014-01-21 14:13:34 +00:00
|
|
|
#import <ReactiveCocoa/RACCommand.h>
|
|
|
|
#import <ReactiveCocoa/RACSignal.h>
|
2014-02-01 00:11:11 +00:00
|
|
|
#import <ReactiveCocoa/NSObject+RACPropertySubscribing.h>
|
2014-01-21 00:22:49 +00:00
|
|
|
#import <Squirrel/Squirrel.h>
|
2013-06-02 12:23:04 +00:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
2014-01-21 14:13:34 +00:00
|
|
|
#include "base/time/time.h"
|
2013-06-02 12:23:04 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
|
|
|
#include "browser/auto_updater_delegate.h"
|
|
|
|
|
2014-01-21 14:13:34 +00:00
|
|
|
namespace auto_updater {
|
2013-06-02 12:23:04 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-01-21 14:13:34 +00:00
|
|
|
// The gloal SQRLUpdater object.
|
2014-01-31 13:10:51 +00:00
|
|
|
SQRLUpdater* g_updater = nil;
|
2013-06-02 12:23:04 +00:00
|
|
|
|
2014-01-31 13:10:51 +00:00
|
|
|
void RelaunchToInstallUpdate() {
|
2014-01-22 04:00:34 +00:00
|
|
|
[[g_updater relaunchToInstallUpdate] subscribeError:^(NSError* error) {
|
|
|
|
AutoUpdaterDelegate* delegate = AutoUpdater::GetDelegate();
|
|
|
|
if (delegate)
|
|
|
|
delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription));
|
|
|
|
}];
|
2013-06-02 12:23:04 +00:00
|
|
|
}
|
|
|
|
|
2014-01-21 14:13:34 +00:00
|
|
|
} // namespace
|
2013-06-02 12:23:04 +00:00
|
|
|
|
|
|
|
// static
|
2014-01-21 14:13:34 +00:00
|
|
|
void AutoUpdater::SetFeedURL(const std::string& feed) {
|
|
|
|
if (g_updater == nil) {
|
|
|
|
// Initialize the SQRLUpdater.
|
2014-02-01 00:02:38 +00:00
|
|
|
NSString *version = NSBundle.mainBundle.sqrl_bundleVersion;
|
|
|
|
NSString *urlString = [NSString stringWithFormat:@"%@?version=%@", base::SysUTF8ToNSString(feed), version];
|
|
|
|
NSURL* url = [NSURL URLWithString:urlString];
|
2014-01-21 14:13:34 +00:00
|
|
|
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
|
|
|
|
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
|
2014-01-31 13:47:59 +00:00
|
|
|
|
|
|
|
AutoUpdaterDelegate* delegate = GetDelegate();
|
|
|
|
if (!delegate)
|
|
|
|
return;
|
|
|
|
|
2014-02-01 00:13:01 +00:00
|
|
|
__block SQRLUpdate* update = nil;
|
2014-01-31 13:47:59 +00:00
|
|
|
// Subscribe to events.
|
|
|
|
[g_updater.updates subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
|
2014-02-01 00:13:01 +00:00
|
|
|
update = downloadedUpdate.update;
|
|
|
|
} completed:^() {
|
|
|
|
if (update) {
|
|
|
|
// There is a new update that has been downloaded.
|
|
|
|
delegate->OnUpdateDownloaded(
|
2014-01-31 13:47:59 +00:00
|
|
|
base::SysNSStringToUTF8(update.releaseNotes),
|
|
|
|
base::SysNSStringToUTF8(update.releaseName),
|
|
|
|
base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
|
|
|
|
base::SysNSStringToUTF8(update.updateURL.absoluteString),
|
|
|
|
base::Bind(RelaunchToInstallUpdate));
|
2014-02-01 00:13:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// When the completed event is sent with no update, then we know there
|
|
|
|
// is no update available.
|
2014-01-31 13:47:59 +00:00
|
|
|
delegate->OnUpdateNotAvailable();
|
2014-02-01 00:13:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update = nil;
|
|
|
|
}];
|
|
|
|
|
2014-02-01 00:11:11 +00:00
|
|
|
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
|
|
|
|
subscribeNext:^(NSNumber *stateNumber) {
|
|
|
|
int state = [stateNumber integerValue];
|
|
|
|
if (state == SQRLUpdaterStateCheckingForUpdate) {
|
|
|
|
delegate->OnCheckingForUpdate();
|
|
|
|
}
|
|
|
|
else if (state == SQRLUpdaterStateDownloadingUpdate) {
|
|
|
|
delegate->OnUpdateAvailable();
|
|
|
|
}
|
2014-01-31 13:47:59 +00:00
|
|
|
}];
|
2014-01-21 14:13:34 +00:00
|
|
|
}
|
2013-06-02 12:23:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:34:42 +00:00
|
|
|
// static
|
|
|
|
void AutoUpdater::CheckForUpdates() {
|
2014-01-31 13:31:42 +00:00
|
|
|
RACSignal* signal = [g_updater.checkForUpdatesCommand execute:nil];
|
|
|
|
|
|
|
|
AutoUpdaterDelegate* delegate = GetDelegate();
|
|
|
|
if (!delegate)
|
|
|
|
return;
|
|
|
|
|
2014-01-31 13:47:59 +00:00
|
|
|
[signal subscribeError:^(NSError* error) {
|
2014-01-31 13:31:42 +00:00
|
|
|
// Something wrong happened.
|
|
|
|
delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription));
|
|
|
|
}];
|
2013-06-02 12:23:04 +00:00
|
|
|
}
|
|
|
|
} // namespace auto_updater
|