Index: chrome/installer/mac/app/Unpacker.m |
diff --git a/chrome/installer/mac/app/Unpacker.m b/chrome/installer/mac/app/Unpacker.m |
new file mode 100644 |
index 0000000000000000000000000000000000000000..48024dc7a0e0c36d5dbdb59d071a1b0348828abf |
--- /dev/null |
+++ b/chrome/installer/mac/app/Unpacker.m |
@@ -0,0 +1,186 @@ |
+// 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 "Unpacker.h" |
+ |
+#include <DiskArbitration/DiskArbitration.h> |
+#include <dispatch/dispatch.h> |
+ |
+#import "Downloader.h" |
+ |
+@interface Unpacker () { |
+ DASessionRef session_; |
+ dispatch_queue_t unpack_dq_; |
+ |
+ NSURL* temporaryDirectoryURL_; |
+ NSString* mountPath_; |
+} |
+- (void)didFinishEjectingDisk:(DADiskRef)disk |
+ WithDissenter:(DADissenterRef)dissenter; |
Sidney San Martín
2016/08/17 22:09:06
W → w
Anna Zeng
2016/08/18 19:41:09
Done
|
+@end |
+ |
+static void eject_callback(DADiskRef disk, |
+ DADissenterRef dissenter, |
+ void* context) { |
+ Unpacker* unpacker = (__bridge Unpacker*)context; |
+ [unpacker didFinishEjectingDisk:disk WithDissenter:dissenter]; |
+} |
+ |
+static void unmount_callback(DADiskRef disk, |
+ DADissenterRef dissenter, |
+ void* context) { |
+ if (dissenter) { |
+ Unpacker* unpacker = (__bridge Unpacker*)context; |
+ [unpacker didFinishEjectingDisk:disk WithDissenter:dissenter]; |
+ } else { |
+ DADiskEject(disk, kDADiskEjectOptionDefault, eject_callback, context); |
+ } |
+} |
+ |
+@implementation Unpacker |
+ |
+@synthesize delegate = delegate_; |
+@synthesize appPath = appPath_; |
+ |
+// TODO: presumably we'd pass in the authenticated/unauthenticated path in here; |
+// however we will change this to a different model that will allow for a |
+// separate authenticated process to run |
+- (id)initWithFinalAppPath:(NSString*)appPath { |
+ if ((self = [super init])) { |
+ appPath_ = appPath; |
+ } |
+ return self; |
+} |
+ |
+// TODO: the failure delegate methods need to be revised to be more meaningfully |
+// deal with the errors (pipe in stderr / stdout) |
+- (void)mountDMGFromURL:(NSURL*)fileURL { |
+ NSError* error = nil; |
+ temporaryDirectoryURL_ = [[NSFileManager defaultManager] |
+ URLForDirectory:NSItemReplacementDirectory |
+ inDomain:NSUserDomainMask |
+ appropriateForURL:[NSURL fileURLWithPath:@"/" isDirectory:YES] |
+ create:YES |
+ error:&error]; |
+ if (error) { |
+ if ([delegate_ respondsToSelector:@selector(unpacker:onMountFailure:)]) { |
+ [delegate_ unpacker:self onMountFailure:error]; |
+ } else if ([delegate_ respondsToSelector:@selector(unpacker:onFailure:)]) { |
+ [delegate_ unpacker:self onFailure:error]; |
+ } else { |
+ NSLog(@"no methods to catch failure implemented"); |
+ } |
+ } |
+ |
+ NSURL* temporaryDiskImageURL = |
+ [temporaryDirectoryURL_ URLByAppendingPathComponent:@"GoogleChrome.dmg"]; |
+ mountPath_ = [[temporaryDirectoryURL_ URLByAppendingPathComponent:@"mnt" |
+ isDirectory:YES] path]; |
+ [[NSFileManager defaultManager] createDirectoryAtPath:mountPath_ |
+ withIntermediateDirectories:YES |
+ attributes:nil |
+ error:&error]; |
+ if (error) { |
+ if ([delegate_ respondsToSelector:@selector(unpacker:onMountFailure:)]) { |
+ [delegate_ unpacker:self onMountFailure:error]; |
+ } else if ([delegate_ respondsToSelector:@selector(unpacker:onFailure:)]) { |
+ [delegate_ unpacker:self onFailure:error]; |
+ } else { |
+ NSLog(@"no methods to catch failure implemented"); |
+ } |
+ } |
+ |
+ [[NSFileManager defaultManager] moveItemAtURL:fileURL |
+ toURL:temporaryDiskImageURL |
+ error:nil]; |
+ |
+ dispatch_semaphore_t mount_semaphore = dispatch_semaphore_create(0); |
+ NSString* path = @"/usr/bin/hdiutil"; |
+ NSArray* args = @[ |
+ @"attach", temporaryDiskImageURL, @"-nobrowse", @"-mountpoint", mountPath_ |
+ ]; |
+ |
+ // TODO: how can we make sure this task quits if the user exits the app early? |
+ NSTask* mountTask = [[NSTask alloc] init]; |
+ mountTask.launchPath = path; |
+ mountTask.arguments = args; |
+ mountTask.terminationHandler = ^void(NSTask* task) { |
+ dispatch_semaphore_signal(mount_semaphore); |
+ }; |
+ [mountTask launch]; |
+ dispatch_semaphore_wait(mount_semaphore, DISPATCH_TIME_FOREVER); |
+ |
+ if ([delegate_ respondsToSelector:@selector(unpacker:onMountSuccess:)]) { |
+ [delegate_ unpacker:self onMountSuccess:mountPath_]; |
+ } else { |
+ [self extractChrome]; |
+ } |
+} |
+ |
+- (void)extractChrome { |
+ NSString* diskAppPath = |
+ [NSString pathWithComponents:@[ mountPath_, @"Google Chrome.app" ]]; |
+ if (![[NSFileManager defaultManager] fileExistsAtPath:diskAppPath]) { |
+ NSLog(@"File in DMG doesn't exist"); |
+ [delegate_ unpacker:self onFailure:nil]; |
+ } |
+ |
+ // TODO: add progress |
+ NSError* error = nil; |
+ if (![[NSFileManager defaultManager] copyItemAtPath:diskAppPath |
+ toPath:appPath_ |
+ error:&error]) { |
+ NSLog(@"%@", error); |
+ [delegate_ unpacker:self onFailure:error]; |
+ } else { |
+ [delegate_ unpacker:self onSuccess:appPath_]; |
+ } |
+} |
+ |
+- (void)unmountDMG { |
+ session_ = DASessionCreate(nil); |
+ unpack_dq_ = |
+ dispatch_queue_create("com.google.chrome.unpack", DISPATCH_QUEUE_SERIAL); |
+ DASessionSetDispatchQueue(session_, unpack_dq_); |
+ DADiskRef child_disk = DADiskCreateFromVolumePath( |
+ nil, session_, |
+ (CFURLRef)[NSURL fileURLWithPath:mountPath_ isDirectory:YES]); |
+ DADiskRef whole_disk = DADiskCopyWholeDisk(child_disk); |
+ |
+ DADiskUnmount(whole_disk, |
+ kDADiskUnmountOptionWhole | kDADiskUnmountOptionForce, |
+ unmount_callback, (void*)self); |
+ |
+ CFRelease(whole_disk); |
+ CFRelease(child_disk); |
+} |
+ |
+- (void)didFinishEjectingDisk:(DADiskRef)disk |
+ WithDissenter:(DADissenterRef)dissenter { |
+ DASessionSetDispatchQueue(session_, NULL); |
+ dispatch_release(unpack_dq_); |
+ CFRelease(session_); |
+ NSError* error = nil; |
+ if (dissenter) { |
+ DAReturn status = DADissenterGetStatus(dissenter); |
+ error = [NSError |
+ errorWithDomain:@"ChromeErrorDomain" |
+ code:err_get_code(status) |
+ userInfo:@{ |
+ NSLocalizedDescriptionKey : |
+ (__bridge NSString*)DADissenterGetStatusString(dissenter) |
+ }]; |
+ [delegate_ unpacker:self onUnmountFailure:error]; |
+ } else { |
+ [[NSFileManager defaultManager] removeItemAtURL:temporaryDirectoryURL_ |
+ error:&error]; |
+ if (error) { |
+ [delegate_ unpacker:self onUnmountFailure:error]; |
+ } else { |
+ [delegate_ unpacker:self onUnmountSuccess:mountPath_]; |
+ } |
+ } |
+} |
+ |
+@end |