Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "AppDelegate.h" | 5 #import "AppDelegate.h" |
| 6 | 6 |
| 7 #import "Downloader.h" | |
| 7 #import "InstallerWindowController.h" | 8 #import "InstallerWindowController.h" |
| 8 #import "NSError+ChromeInstallerAdditions.h" | 9 #import "NSError+ChromeInstallerAdditions.h" |
| 9 #import "NSAlert+ChromeInstallerAdditions.h" | 10 #import "NSAlert+ChromeInstallerAdditions.h" |
| 11 #import "OmahaCommunication.h" | |
| 12 #import "Unpacker.h" | |
| 10 | 13 |
| 11 @interface NSAlert () | 14 @interface NSAlert () |
| 12 - (void)beginSheetModalForWindow:(NSWindow*)sheetWindow | 15 - (void)beginSheetModalForWindow:(NSWindow*)sheetWindow |
| 13 completionHandler: | 16 completionHandler: |
| 14 (void (^__nullable)(NSModalResponse returnCode))handler; | 17 (void (^__nullable)(NSModalResponse returnCode))handler; |
| 15 @end | 18 @end |
| 16 | 19 |
| 17 @interface AppDelegate ()<OmahaCommunicationDelegate, DownloaderDelegate> { | 20 @interface AppDelegate ()<NSWindowDelegate, |
| 21 OmahaCommunicationDelegate, | |
| 22 DownloaderDelegate, | |
| 23 UnpackDelegate> { | |
| 18 InstallerWindowController* installerWindowController_; | 24 InstallerWindowController* installerWindowController_; |
| 25 BOOL preventTermination_; | |
| 19 } | 26 } |
| 20 @property(strong) NSWindow* window; | 27 @property(strong) NSWindow* window; |
| 28 - (void)exit; | |
| 21 @end | 29 @end |
| 22 | 30 |
| 23 @implementation AppDelegate | 31 @implementation AppDelegate |
| 24 @synthesize window = window_; | 32 @synthesize window = window_; |
| 25 | 33 |
| 26 // Sets up the main window and begins the downloading process. | 34 // Sets up the main window and begins the downloading process. |
| 27 - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { | 35 - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { |
| 36 window_.delegate = self; | |
| 28 installerWindowController_ = | 37 installerWindowController_ = |
| 29 [[InstallerWindowController alloc] initWithWindow:window_]; | 38 [[InstallerWindowController alloc] initWithWindow:window_]; |
| 39 | |
| 30 [self startDownload]; | 40 [self startDownload]; |
| 31 } | 41 } |
| 32 | 42 |
| 33 - (void)applicationWillTerminate:(NSNotification*)aNotification { | 43 - (void)applicationWillTerminate:(NSNotification*)aNotification { |
| 34 } | 44 } |
| 35 | 45 |
| 36 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender { | 46 // This function effectively takes the place of |
| 47 // applicationShouldTerminateAfterLastWindowClosed: to make sure that the | |
| 48 // application does correctly terminate after closing the installer, but does | |
| 49 // not terminate when we call orderOut: to hide the installer during its | |
| 50 // tear-down steps. | |
| 51 - (BOOL)windowShouldClose:(id)sender { | |
| 52 [self exit]; | |
|
Elly Fong-Jones
2016/08/23 21:57:40
does orderOut constitute a window close?
Anna Zeng
2016/08/24 01:30:10
The window is hidden when we call orderOut. To the
| |
| 37 return YES; | 53 return YES; |
| 38 } | 54 } |
| 39 | 55 |
| 56 - (NSApplicationTerminateReply)applicationShouldTerminate: | |
| 57 (NSApplication*)sender { | |
| 58 return preventTermination_ ? NSTerminateCancel : NSTerminateNow; | |
| 59 } | |
| 60 | |
| 61 - (void)exit { | |
| 62 preventTermination_ = NO; | |
| 63 [NSApp terminate:nil]; | |
| 64 } | |
| 65 | |
| 40 - (void)startDownload { | 66 - (void)startDownload { |
| 41 [installerWindowController_ updateStatusDescription:@"Initializing..."]; | 67 [installerWindowController_ updateStatusDescription:@"Initializing..."]; |
| 68 | |
| 42 OmahaCommunication* omahaMessenger = [[OmahaCommunication alloc] init]; | 69 OmahaCommunication* omahaMessenger = [[OmahaCommunication alloc] init]; |
| 43 omahaMessenger.delegate = self; | 70 omahaMessenger.delegate = self; |
| 44 | |
| 45 [omahaMessenger fetchDownloadURLs]; | 71 [omahaMessenger fetchDownloadURLs]; |
| 46 } | 72 } |
| 47 | 73 |
| 48 - (void)onOmahaSuccessWithURLs:(NSArray*)URLs { | 74 - (void)omahaCommunication:(OmahaCommunication*)messenger |
| 75 onSuccess:(NSArray*)URLs { | |
| 49 [installerWindowController_ updateStatusDescription:@"Downloading..."]; | 76 [installerWindowController_ updateStatusDescription:@"Downloading..."]; |
| 77 | |
| 50 Downloader* download = [[Downloader alloc] init]; | 78 Downloader* download = [[Downloader alloc] init]; |
| 51 download.delegate = self; | 79 download.delegate = self; |
| 52 [download downloadChromeImageToDownloadsDirectory:[URLs firstObject]]; | 80 [download downloadChromeImageFrom:[URLs firstObject]]; |
| 53 } | 81 } |
| 54 | 82 |
| 55 - (void)onOmahaFailureWithError:(NSError*)error { | 83 - (void)omahaCommunication:(OmahaCommunication*)messenger |
| 84 onFailure:(NSError*)error { | |
| 56 NSError* networkError = | 85 NSError* networkError = |
| 57 [NSError errorForAlerts:@"Network Error" | 86 [NSError errorForAlerts:@"Network Error" |
| 58 withDescription:@"Could not connect to Chrome server." | 87 withDescription:@"Could not connect to Chrome server." |
| 59 isRecoverable:YES]; | 88 isRecoverable:YES]; |
| 60 [self displayError:networkError]; | 89 [self displayError:networkError]; |
| 61 } | 90 } |
| 62 | 91 |
| 63 // Bridge method from Downloader to InstallerWindowController. Allows Downloader | 92 // Bridge method from Downloader to InstallerWindowController. Allows Downloader |
| 64 // to update the progressbar without having direct access to any UI obejcts. | 93 // to update the progressbar without having direct access to any UI obejcts. |
| 65 - (void)didDownloadData:(double)downloadProgressPercentage { | 94 - (void)downloader:(Downloader*)download percentProgress:(double)percentage { |
| 66 [installerWindowController_ | 95 [installerWindowController_ updateDownloadProgress:(double)percentage]; |
| 67 updateDownloadProgress:(double)downloadProgressPercentage]; | |
| 68 } | 96 } |
| 69 | 97 |
| 70 - (void)downloader:(Downloader*)download | 98 - (void)downloader:(Downloader*)download onSuccess:(NSURL*)diskImageURL { |
| 71 onDownloadSuccess:(NSURL*)diskImagePath { | 99 [installerWindowController_ updateStatusDescription:@"Installing..."]; |
| 72 [installerWindowController_ updateStatusDescription:@"Done."]; | 100 |
| 73 // TODO: replace the line of code below with real code someday | 101 Unpacker* unpacker = [[Unpacker alloc] init]; |
| 102 unpacker.delegate = self; | |
| 103 [unpacker mountDMGFromURL:diskImageURL]; | |
| 74 } | 104 } |
| 75 | 105 |
| 76 - (void)downloader:(Downloader*)download | 106 - (void)downloader:(Downloader*)download onFailure:(NSError*)error { |
| 77 onDownloadFailureWithError:(NSError*)error { | |
| 78 NSError* downloadError = | 107 NSError* downloadError = |
| 79 [NSError errorForAlerts:@"Download Failure" | 108 [NSError errorForAlerts:@"Download Failure" |
| 80 withDescription:@"Unable to download Google Chrome." | 109 withDescription:@"Unable to download Google Chrome." |
| 81 isRecoverable:NO]; | 110 isRecoverable:NO]; |
| 82 [self displayError:downloadError]; | 111 [self displayError:downloadError]; |
| 83 } | 112 } |
| 84 | 113 |
| 114 - (void)unpacker:(Unpacker*)unpacker onMountSuccess:(NSString*)tempAppPath { | |
| 115 // Calling this function will change the progress bar into an indeterminate | |
| 116 // one. We won't need to update the progress bar any more after this point. | |
| 117 [installerWindowController_ updateDownloadProgress:-1.0]; | |
| 118 // By disabling closing the window or quitting, we can tell the user that | |
| 119 // closing the application at this point is not a good idea. | |
| 120 window_.styleMask &= ~NSClosableWindowMask; | |
| 121 preventTermination_ = YES; | |
| 122 | |
| 123 // TODO: move the below code into AuthorizedInstall | |
| 124 | |
| 125 NSError* error = nil; | |
| 126 if ([[NSFileManager defaultManager] | |
| 127 fileExistsAtPath:@"/Applications/Google Chromo.app"]) { | |
|
Elly Fong-Jones
2016/08/23 21:57:40
This constant should probably not be repeated all
Anna Zeng
2016/08/24 01:30:10
Acknowledged. Fixed temporarily; this code would s
| |
| 128 [[NSFileManager defaultManager] | |
| 129 moveItemAtPath:@"/Applications/Google Chromo.app" | |
| 130 toPath:tempAppPath | |
| 131 error:nil]; | |
| 132 } | |
| 133 if (![[NSFileManager defaultManager] | |
| 134 moveItemAtPath:tempAppPath | |
| 135 toPath:@"/Applications/Google Chromo.app" | |
| 136 error:&error]) { | |
| 137 NSLog(@"%@", error); | |
| 138 } | |
| 139 | |
| 140 // TODO: edit the below code to feed in command line arguments | |
| 141 | |
| 142 [[NSWorkspace sharedWorkspace] | |
| 143 launchApplicationAtURL: | |
| 144 [NSURL fileURLWithPath:@"/Applications/Google Chromo.app" | |
| 145 isDirectory:NO] | |
| 146 options:NSWorkspaceLaunchDefault | |
| 147 configuration:@{} | |
| 148 error:&error]; | |
| 149 if (error) { | |
| 150 NSLog(@"Chromo failed to launch: %@", error); | |
| 151 } | |
| 152 | |
| 153 // Begin teardown stuff! | |
| 154 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 155 [window_ orderOut:nil]; | |
| 156 }); | |
| 157 | |
| 158 [unpacker unmountDMG]; | |
| 159 } | |
| 160 | |
| 161 - (void)unpacker:(Unpacker*)unpacker onMountFailure:(NSError*)error { | |
| 162 NSError* extractError = | |
| 163 [NSError errorForAlerts:@"Install Failure" | |
| 164 withDescription:@"Unable to add Google Chrome to Applications." | |
| 165 isRecoverable:NO]; | |
| 166 [self displayError:extractError]; | |
| 167 } | |
| 168 | |
| 169 - (void)unpacker:(Unpacker*)unpacker onUnmountSuccess:(NSString*)mountpath { | |
| 170 NSLog(@"we're done here!"); | |
| 171 [self exit]; | |
| 172 } | |
| 173 | |
| 174 - (void)unpacker:(Unpacker*)unpacker onUnmountFailure:(NSError*)error { | |
| 175 NSLog(@"error unmounting"); | |
| 176 // NOTE: since we are not deleting the temporary folder if the unmount fails, | |
| 177 // we'll just leave it up to the computer to delete the temporary folder on | |
| 178 // its own time, and to unmount the disk during a restart at some point. There | |
| 179 // is no other work to be done in the mean time. | |
| 180 [self exit]; | |
| 181 } | |
| 182 | |
| 85 // Displays an alert on the main window using the contents of the passed in | 183 // Displays an alert on the main window using the contents of the passed in |
| 86 // error. | 184 // error. |
| 87 - (void)displayError:(NSError*)error { | 185 - (void)displayError:(NSError*)error { |
| 88 NSAlert* alertForUser = [NSAlert alertWithError:error]; | 186 NSAlert* alertForUser = [NSAlert alertWithError:error]; |
| 89 | 187 dispatch_async(dispatch_get_main_queue(), ^{ |
| 90 dispatch_sync(dispatch_get_main_queue(), ^{ | |
| 91 [alertForUser beginSheetModalForWindow:window_ | 188 [alertForUser beginSheetModalForWindow:window_ |
| 92 completionHandler:^(NSModalResponse returnCode) { | 189 completionHandler:^(NSModalResponse returnCode) { |
| 93 if (returnCode != [alertForUser quitButton]) { | 190 if (returnCode != [alertForUser quitResponse]) { |
| 94 [self startDownload]; | 191 [self startDownload]; |
| 95 } else { | 192 } else { |
| 96 [NSApp terminate:nil]; | 193 [NSApp terminate:nil]; |
| 97 } | 194 } |
| 98 }]; | 195 }]; |
| 99 }); | 196 }); |
| 100 } | 197 } |
| 101 | 198 |
| 102 @end | 199 @end |
| OLD | NEW |