electron/browser/auto_updater_mac.mm

76 lines
2.2 KiB
Text
Raw Normal View History

// 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-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"
#include "browser/auto_updater_delegate.h"
2014-01-21 14:13:34 +00:00
namespace auto_updater {
namespace {
2014-01-21 14:13:34 +00:00
// The gloal SQRLUpdater object.
static SQRLUpdater* g_updater = nil;
2014-01-21 14:13:34 +00:00
static void RelaunchToInstallUpdate() {
if (g_updater != nil)
[g_updater relaunchToInstallUpdate];
}
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) {
// Initialize the SQRLUpdater.
NSURL* url = [NSURL URLWithString:base::SysUTF8ToNSString(feed)];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url];
g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest];
AutoUpdaterDelegate* delegate = GetDelegate();
if (!delegate)
return;
2014-01-21 14:13:34 +00:00
// Subscribe to events.
__block bool has_update = false;
2014-01-21 14:13:34 +00:00
[g_updater.updates subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) {
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;
2014-01-21 14:13:34 +00:00
}];
}
}
// static
void AutoUpdater::CheckForUpdates() {
2014-01-21 14:13:34 +00:00
if (g_updater != nil) {
[g_updater.checkForUpdatesCommand execute:nil];
}
}
} // namespace auto_updater