// 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" #import #import #import #import #include "base/bind.h" #include "base/time/time.h" #include "base/strings/sys_string_conversions.h" #include "browser/auto_updater_delegate.h" namespace auto_updater { namespace { // The gloal SQRLUpdater object. SQRLUpdater* g_updater = nil; void RelaunchToInstallUpdate() { [[g_updater relaunchToInstallUpdate] subscribeError:^(NSError* error) { AutoUpdaterDelegate* delegate = AutoUpdater::GetDelegate(); if (delegate) delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription)); }]; } } // namespace // static void AutoUpdater::SetFeedURL(const std::string& feed) { if (g_updater == nil) { // Initialize the SQRLUpdater. NSString *version = NSBundle.mainBundle.sqrl_bundleVersion; NSString *urlString = [NSString stringWithFormat:@"%@?version=%@", base::SysUTF8ToNSString(feed), version]; NSURL* url = [NSURL URLWithString:urlString]; NSURLRequest* urlRequest = [NSURLRequest requestWithURL:url]; g_updater = [[SQRLUpdater alloc] initWithUpdateRequest:urlRequest]; AutoUpdaterDelegate* delegate = GetDelegate(); if (!delegate) return; __block SQRLUpdate* update = nil; // Subscribe to events. [g_updater.updates subscribeNext:^(SQRLDownloadedUpdate* downloadedUpdate) { update = downloadedUpdate.update; } completed:^() { if (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)); } else { // When the completed event is sent with no update, then we know there // is no update available. delegate->OnUpdateNotAvailable(); } update = nil; }]; [[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(); } }]; } } // static void AutoUpdater::CheckForUpdates() { RACSignal* signal = [g_updater.checkForUpdatesCommand execute:nil]; AutoUpdaterDelegate* delegate = GetDelegate(); if (!delegate) return; [signal subscribeError:^(NSError* error) { // Something wrong happened. delegate->OnError(base::SysNSStringToUTF8(error.localizedDescription)); }]; } } // namespace auto_updater