Commit f255d87c authored by zengster's avatar zengster Committed by Commit bot

Migrate mac installer to delegate-driven model

BUG=

Review-Url: https://codereview.chromium.org/2148293005
Cr-Commit-Position: refs/heads/master@{#407180}
parent 84204fda
......@@ -10,7 +10,7 @@ group("mac") {
public_deps = [
":copies",
":make_signers",
"app:downloader",
"app:mac_installer",
]
}
......
......@@ -2,16 +2,28 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
executable("downloader") {
static_library("mac_installer_base") {
sources = [
"DownloadDelegate.m",
"Downloader.m",
"MainDelegate.m",
"NetworkCommunication.m",
"OmahaCommunication.m",
"OmahaXMLParser.m",
"OmahaXMLRequest.m",
"SystemInfo.m",
]
}
executable("mac_installer") {
sources = [
"main.m",
]
libs = [ "Foundation.framework" ]
deps = [
":mac_installer_base",
]
libs = [
"AppKit.framework",
"CoreFoundation.framework",
"Foundation.framework",
]
}
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <assert.h>
#import "DownloadDelegate.h"
#import "Downloader.h"
@implementation DownloadDelegate : NSObject
// Skeleton of delegate method to provide download progress updates.
// TODO: Make use of (totalBytesWritten/totalBytesExpectedToWrite)*100
// to generate download progress percentage.
- (void)URLSession:(NSURLSession*)session
downloadTask:(NSURLSessionDownloadTask*)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
}
// Delegate method to move downloaded disk image to user's Download directory.
- (void)URLSession:(NSURLSession*)session
downloadTask:(NSURLSessionDownloadTask*)downloadTask
didFinishDownloadingToURL:(NSURL*)location {
assert([location isFileURL]);
NSFileManager* manager = [[NSFileManager alloc] init];
NSURL* downloadsDirectory =
[[NSURL alloc] initFileURLWithPath:[Downloader getDownloadsFilePath]];
if ([manager fileExistsAtPath:location.path]) {
[manager copyItemAtURL:location toURL:downloadsDirectory error:nil];
} else {
// TODO: Error Handling
}
}
- (void)URLSession:(NSURLSession*)session
task:(NSURLSessionTask*)task
didCompleteWithError:(NSError*)error {
// TODO: Error Handling
}
@end
......@@ -7,7 +7,14 @@
#import <Foundation/Foundation.h>
@interface Downloader : NSObject<NSXMLParserDelegate>
@protocol DownloaderDelegate
- (void)onDownloadSuccess;
@end
@interface Downloader
: NSObject<NSXMLParserDelegate, NSURLSessionDownloadDelegate>
@property(nonatomic, assign) id<DownloaderDelegate> delegate;
// Returns a path to a user's home download folder.
+ (NSString*)getDownloadsFilePath;
......
......@@ -2,15 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Foundation/Foundation.h>
#import "DownloadDelegate.h"
#import "Downloader.h"
#include <assert.h>
#import "NetworkCommunication.h"
#import "OmahaXMLParser.h"
@implementation Downloader
@synthesize delegate = delegate_;
// TODO: make this overrideable with commandline argument? or enviro variable
+ (NSString*)getDownloadsFilePath {
NSArray* downloadPaths = NSSearchPathForDirectoriesInDomains(
......@@ -59,9 +61,8 @@
// Downloads contents of chromeURL to downloads folders and delegates the work
// to the DownloadDelegate class.
- (BOOL)writeChromeImageToDownloadsDirectory:(NSURL*)chromeURL {
DownloadDelegate* delegate = [[DownloadDelegate alloc] init];
NetworkCommunication* downloadTask =
[[NetworkCommunication alloc] initWithDelegate:delegate];
[[NetworkCommunication alloc] initWithDelegate:self];
// TODO: What if file already exists?
[downloadTask createRequestWithUrlAsString:[chromeURL absoluteString]
......@@ -81,4 +82,37 @@
return writeWasSuccessful;
}
// Skeleton of delegate method to provide download progress updates.
// TODO: Make use of (totalBytesWritten/totalBytesExpectedToWrite)*100
// to generate download progress percentage.
- (void)URLSession:(NSURLSession*)session
downloadTask:(NSURLSessionDownloadTask*)downloadTask
didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
}
// Delegate method to move downloaded disk image to user's Download directory.
- (void)URLSession:(NSURLSession*)session
downloadTask:(NSURLSessionDownloadTask*)downloadTask
didFinishDownloadingToURL:(NSURL*)location {
assert([location isFileURL]);
NSFileManager* manager = [[NSFileManager alloc] init];
NSURL* downloadsDirectory =
[[NSURL alloc] initFileURLWithPath:[Downloader getDownloadsFilePath]];
if ([manager fileExistsAtPath:location.path]) {
[manager moveItemAtURL:location toURL:downloadsDirectory error:nil];
} else {
// TODO: Error Handling
}
}
- (void)URLSession:(NSURLSession*)session
task:(NSURLSessionTask*)task
didCompleteWithError:(NSError*)error {
// TODO: Error Handling
[delegate_ onDownloadSuccess];
}
@end
......@@ -2,13 +2,22 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_INSTALLER_MAC_APP_DOWNLOADDELEGATE_H_
#define CHROME_INSTALLER_MAC_APP_DOWNLOADDELEGATE_H_
#ifndef CHROME_INSTALLER_MAC_APP_MAINDELEGATE_H_
#define CHROME_INSTALLER_MAC_APP_MAINDELEGATE_H_
#import <Foundation/Foundation.h>
@interface DownloadDelegate : NSObject<NSURLSessionDownloadDelegate>
#import "Downloader.h"
#import "OmahaCommunication.h"
// TODO: move this into the unpacking file when created
@protocol UnpackDelegate
- (void)onUnpackSuccess;
@end
#endif // CHROME_INSTALLER_MAC_APP_DOWNLOADDELEGATE_H_
@interface MainDelegate
: NSObject<OmahaCommunicationDelegate, DownloaderDelegate, UnpackDelegate>
- (void)runApplication;
@end
#endif // CHROME_INSTALLER_MAC_APP_MAINDELEGATE_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "MainDelegate.h"
@implementation MainDelegate
- (void)runApplication {
OmahaCommunication* messenger = [[OmahaCommunication alloc] init];
messenger.delegate = self;
[messenger sendRequest];
}
- (void)onOmahaSuccessWithResponseBody:(NSData*)responseBody
AndError:(NSError*)error {
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
exit(1);
}
Downloader* download = [[Downloader alloc] init];
download.delegate = self;
[download downloadChromeImageToDownloadsDirectory:responseBody];
}
- (void)onDownloadSuccess {
// TODO: replace the line of code below with real code someday to unpack dmg
exit(0);
}
- (void)onUnpackSuccess {
}
@end
......@@ -7,16 +7,14 @@
#import <Foundation/Foundation.h>
typedef void (^DataTaskCompletionHandler)(NSData*, NSURLResponse*, NSError*);
typedef void (^DownloadTaskCompletionHandler)(NSURL*, NSURLResponse*, NSError*);
// TODO: talk to @sdy about this class
@interface NetworkCommunication : NSObject
typedef void (^DataTaskCompletionHandler)(NSData*, NSURLResponse*, NSError*);
@property(nonatomic, copy) NSMutableURLRequest* request;
@property(nonatomic, copy) NSURLSession* session;
@property(nonatomic, copy) DataTaskCompletionHandler dataResponseHandler;
@property(nonatomic, copy)
DownloadTaskCompletionHandler downloadResponseHandler;
- (id)init;
- (id)initWithDelegate:(id)delegate;
......@@ -26,8 +24,7 @@ typedef void (^DownloadTaskCompletionHandler)(NSURL*, NSURLResponse*, NSError*);
- (NSMutableURLRequest*)createRequestWithUrlAsString:(NSString*)urlString
andXMLBody:(NSXMLDocument*)body;
// Adds a data task to the run loop using the request instance variable.
- (void)sendDataRequestWithCompletionHandler:
(DataTaskCompletionHandler)completionHandler;
- (void)sendDataRequest;
// Adds a download task to the run loop using the request instance variable.
- (void)sendDownloadRequest;
......
......@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Foundation/Foundation.h>
#import "NetworkCommunication.h"
@interface NSURLSession (PartialAvailability)
......@@ -24,7 +22,6 @@ downloadTaskWithRequest:(NSURLRequest*)request
@synthesize session = session_;
@synthesize request = request_;
@synthesize dataResponseHandler = dataResponseHandler_;
@synthesize downloadResponseHandler = downloadResponseHandler_;
- (id)init {
return [self initWithDelegate:nil];
......@@ -54,24 +51,21 @@ downloadTaskWithRequest:(NSURLRequest*)request
return request_;
}
- (void)sendDataRequestWithCompletionHandler:
(DataTaskCompletionHandler)completionHandler {
dataResponseHandler_ = completionHandler;
NSURLSessionDataTask* dataTask =
[session_ dataTaskWithRequest:request_
completionHandler:dataResponseHandler_];
- (void)sendDataRequest {
NSURLSessionDataTask* dataTask;
if (dataResponseHandler_) {
dataTask = [session_ dataTaskWithRequest:request_
completionHandler:dataResponseHandler_];
} else {
dataTask = [session_ dataTaskWithRequest:request_];
}
[dataTask resume];
}
- (void)sendDownloadRequest {
NSURLSessionDownloadTask* downloadTask;
if (downloadResponseHandler_) {
downloadTask = [session_ downloadTaskWithRequest:request_
completionHandler:downloadResponseHandler_];
} else {
downloadTask = [session_ downloadTaskWithRequest:request_];
}
NSURLSessionDownloadTask* downloadTask =
[session_ downloadTaskWithRequest:request_];
[downloadTask resume];
}
......
......@@ -7,20 +7,27 @@
#import <Foundation/Foundation.h>
#include "NetworkCommunication.h"
#import "NetworkCommunication.h"
typedef void (^OmahaRequestCompletionHandler)(NSData*, NSError*);
@protocol OmahaCommunicationDelegate
- (void)onOmahaSuccessWithResponseBody:(NSData*)responseBody
AndError:(NSError*)error;
@end
@interface OmahaCommunication : NSObject
@interface OmahaCommunication : NSObject<NSURLSessionDataDelegate> {
id<OmahaCommunicationDelegate> _delegate;
}
@property(nonatomic, copy) NSXMLDocument* requestXMLBody;
// TODO: talk to @sdy about use of NetworkCommunication
@property(nonatomic, copy) NetworkCommunication* sessionHelper;
@property(nonatomic, assign) id<OmahaCommunicationDelegate> delegate;
- (id)init;
- (id)initWithBody:(NSXMLDocument*)xmlBody;
// Sends the request created using the session helper.
- (void)sendRequestWithBlock:(OmahaRequestCompletionHandler)block;
- (void)sendRequest;
@end
......
......@@ -2,22 +2,23 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Foundation/Foundation.h>
#import "OmahaCommunication.h"
#include "OmahaCommunication.h"
#import "OmahaXMLRequest.h"
@implementation OmahaCommunication : NSObject
@implementation OmahaCommunication
@synthesize requestXMLBody = requestXMLBody_;
@synthesize sessionHelper = sessionHelper_;
@synthesize delegate = delegate_;
- (id)init {
return [self initWithBody:[[NSXMLDocument alloc] init]];
return [self initWithBody:[OmahaXMLRequest createXMLRequestBody]];
}
- (id)initWithBody:(NSXMLDocument*)xmlBody {
if ((self = [super init])) {
sessionHelper_ = [[NetworkCommunication alloc] init];
sessionHelper_ = [[NetworkCommunication alloc] initWithDelegate:self];
requestXMLBody_ = xmlBody;
[self createOmahaRequest];
}
......@@ -33,27 +34,13 @@
return request;
}
- (void)sendRequestWithBlock:(OmahaRequestCompletionHandler)block {
DataTaskCompletionHandler cHandler =
^(NSData* _Nullable data, NSURLResponse* _Nullable response,
NSError* _Nullable error) {
if (error) {
NSLog(@"%@", error);
block(data, error);
return;
}
NSHTTPURLResponse* HTTPResponse = (NSHTTPURLResponse*)response;
if (HTTPResponse.statusCode != 200) {
// TODO: make these logging statements more rare
NSLog(@"HTTP response: %ld", (unsigned long)HTTPResponse.statusCode);
}
block(data, error);
};
[sessionHelper_ sendDataRequestWithCompletionHandler:cHandler];
- (void)sendRequest {
[sessionHelper_ setDataResponseHandler:^(NSData* _Nullable data,
NSURLResponse* _Nullable response,
NSError* _Nullable error) {
[delegate_ onOmahaSuccessWithResponseBody:data AndError:error];
}];
[sessionHelper_ sendDataRequest];
}
@end
\ No newline at end of file
......@@ -2,9 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Foundation/Foundation.h>
#include "OmahaXMLRequest.h"
#include "SystemInfo.h"
@implementation OmahaXMLRequest : NSObject
......
......@@ -4,31 +4,12 @@
#import <Foundation/Foundation.h>
#import "Downloader.h"
#import "OmahaCommunication.h"
#import "OmahaXMLRequest.h"
#import "SystemInfo.h"
#import "MainDelegate.h"
// TODO: add a class that takes care of what main is doing now
void talkToOmahaThenExecuteBlock(OmahaRequestCompletionHandler block) {
NSXMLDocument* requestBody = [OmahaXMLRequest createXMLRequestBody];
OmahaCommunication* messenger =
[[OmahaCommunication alloc] initWithBody:requestBody];
[messenger sendRequestWithBlock:block];
}
int main() {
talkToOmahaThenExecuteBlock(^(NSData* data, NSError* error) {
if (error) {
NSLog(@"%@", [error localizedDescription]);
return;
}
Downloader* download = [[Downloader alloc] init];
[download downloadChromeImageToDownloadsDirectory:data];
});
int main(int argc, const char* argv[]) {
MainDelegate* delegate = [[MainDelegate alloc] init];
[delegate runApplication];
// [[NSRunLoop mainRunLoop] run];
[[NSRunLoop mainRunLoop]
runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
[[NSRunLoop mainRunLoop] run];
return 1;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment