electron/atom/browser/auto_updater_mac.mm

104 lines
3.3 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.
2014-03-16 00:30:26 +00:00
#include "atom/browser/auto_updater.h"
2014-01-21 14:13:34 +00:00
#import <ReactiveCocoa/RACCommand.h>
#import <ReactiveCocoa/RACSignal.h>
#import <ReactiveCocoa/NSObject+RACPropertySubscribing.h>
2014-01-21 00:22:49 +00:00
#import <Squirrel/Squirrel.h>
#include "base/bind.h"
2014-01-21 14:13:34 +00:00
#include "base/time/time.h"
#include "base/strings/sys_string_conversions.h"
2014-03-16 00:30:26 +00:00
#include "atom/browser/auto_updater_delegate.h"
2014-02-03 20:41:53 +00:00
#include <iostream>
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;
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));
}];
}
2014-01-21 14:13:34 +00:00
} // namespace
// static
2014-01-21 14:13:34 +00:00
void AutoUpdater::SetFeedURL(const std::string& feed) {
if (g_updater == nil) {
AutoUpdaterDelegate* delegate = GetDelegate();
if (!delegate)
return;
2014-01-21 14:13:34 +00:00
// Initialize the SQRLUpdater.
2014-02-01 00:14:16 +00:00
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
2014-01-21 14:13:34 +00:00
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
@try {
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
} @catch (NSException* error) {
delegate->OnError(base::SysNSStringToUTF8(error.reason));
return;
}
[[g_updater rac_valuesForKeyPath:@"state" observer:g_updater]
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();
});
}];
2014-01-21 14:13:34 +00:00
}
}
// static
void AutoUpdater::CheckForUpdates() {
AutoUpdaterDelegate* delegate = GetDelegate();
if (!delegate)
return;
[[[[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]
subscribeNext:^(SQRLDownloadedUpdate *downloadedUpdate) {
if (downloadedUpdate) {
SQRLUpdate* update = downloadedUpdate.update;
// There is a new update that has been downloaded.
delegate->OnUpdateDownloaded(
base::SysNSStringToUTF8(update.releaseNotes),
base::SysNSStringToUTF8(update.releaseName),
base::Time::FromDoubleT(update.releaseDate.timeIntervalSince1970),
base::SysNSStringToUTF8(update.updateURL.absoluteString),
base::Bind(RelaunchToInstallUpdate));
2014-02-02 11:58:25 +00:00
} else {
// When the completed event is sent with no update, then we know there
// is no update available.
delegate->OnUpdateNotAvailable();
}
} error:^(NSError *error) {
delegate->OnError(base::SysNSStringToUTF8(
[NSString stringWithFormat:@"%@: %@",
error.localizedDescription, error.localizedFailureReason]));
}];
}
} // namespace auto_updater