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..af7b2168232fe4f71cd2197ce9fef26e347506c6 |
--- /dev/null |
+++ b/chrome/installer/mac/app/Unpacker.m |
@@ -0,0 +1,174 @@ |
+// 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; |
+ |
+ // TODO: narrow the scope for these variables |
+ NSURL* temporaryDirectoryURL; |
+ NSURL* temporaryDiskImageURL; |
+ NSString* mountPath; |
Sidney San Martín
2016/08/13 12:37:18
I believe instance variables should always have tr
Anna Zeng
2016/08/16 23:07:39
Done.
|
+} |
+@end |
+ |
+@implementation Unpacker |
+ |
+@synthesize delegate = delegate_; |
+@synthesize appPath = appPath_; |
+ |
+// TODO: clean up error handling & pass up to appdelegate |
+static void unmount_callback(DADiskRef disk, |
Sidney San Martín
2016/08/13 12:37:19
These functions should not be in the @implementati
Anna Zeng
2016/08/16 23:07:39
Done.
|
+ DADissenterRef dissenter, |
+ void* context) { |
+ if (dissenter) { |
Sidney San Martín
2016/08/13 12:37:19
There's a similar block of code in eject_callback.
Anna Zeng
2016/08/16 23:07:40
Done.
|
+ DAReturn status = DADissenterGetStatus(dissenter); |
+ if (unix_err(status)) { |
+ int code = err_get_code(status); |
+ NSLog(@"Unmount error code %d", code); |
+ } |
+ } else { |
+ DADiskEject(disk, kDADiskEjectOptionDefault, eject_callback, context); |
+ } |
+} |
+ |
+static void eject_callback(DADiskRef disk, |
+ DADissenterRef dissenter, |
+ void* context) { |
+ NSError* error = nil; |
+ if (dissenter) { |
+ DAReturn status = DADissenterGetStatus(dissenter); |
+ if (unix_err(status)) { |
+ int code = err_get_code(status); |
+ NSLog(@"Eject error code %d", code); |
+ error = [NSError |
+ errorWithDomain:@"ChromeErrorDomain" |
+ code:code |
+ userInfo:@{ |
+ NSLocalizedDescriptionKey : |
+ (__bridge NSString*)DADissenterGetStatusString(dissenter) |
+ }]; |
+ } |
+ } else { |
+ Unpacker* unpacker = (__bridge Unpacker*)context; |
+ [unpacker didFinishEjectingDisk:disk WithError:error]; |
+ } |
+} |
+ |
+// 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) { |
+ [delegate_ unpacker:self onMountFailure:nil]; |
+ } |
+ |
+ temporaryDiskImageURL = |
+ [temporaryDirectoryURL URLByAppendingPathComponent:@"GoogleChrome.dmg"]; |
+ mountPath = [[temporaryDirectoryURL URLByAppendingPathComponent:@"mnt" |
+ isDirectory:YES] path]; |
+ [[NSFileManager defaultManager] createDirectoryAtPath:mountPath |
+ withIntermediateDirectories:YES |
+ attributes:nil |
+ error:&error]; |
+ if (error) { |
+ [delegate_ unpacker:self onMountFailure:nil]; |
+ } |
+ |
+ [[NSFileManager defaultManager] moveItemAtURL:fileURL |
+ toURL:temporaryDiskImageURL |
+ error:nil]; |
+ |
+ dispatch_semaphore_t mount_semaphore = dispatch_semaphore_create(0); |
+ NSString* path = @"/usr/bin/hdiutil"; |
+ NSArray* args = @[ |
+ @"mount", temporaryDiskImageURL, @"-nobrowse", @"-mountpoint", mountPath |
+ ]; |
+ |
+ NSTask* mountTask = [[NSTask alloc] init]; |
+ mountTask.launchPath = path; |
+ mountTask.arguments = args; |
+ mountTask.terminationHandler = ^void(NSTask* task) { |
+ // unpack_dq = dispatch_queue_create("com.google.chrome.unpack", |
Elly Fong-Jones
2016/08/16 15:26:18
does this commented code need to be here?
Anna Zeng
2016/08/16 23:07:40
This code was not completely deleted in case I nee
|
+ // DISPATCH_QUEUE_SERIAL); |
+ // dispatch_async(unpack_dq, ^{ |
+ dispatch_semaphore_signal(mount_semaphore); |
+ // }); |
+ }; |
+ [mountTask launch]; |
+ |
+ dispatch_semaphore_wait(mount_semaphore, DISPATCH_TIME_FOREVER); |
Elly Fong-Jones
2016/08/16 15:26:18
is this running on a work queue or somesuch? other
Anna Zeng
2016/08/16 23:07:40
Currently, it is not. Would you recommend running
|
+ [delegate_ unpacker:self onMountSuccess:mountPath]; |
+} |
+ |
+- (void)extractChrome { |
+ NSString* diskAppPath = [[temporaryDirectoryURL |
+ URLByAppendingPathComponent:@"mnt/Google Chrome.app" |
+ isDirectory:YES] path]; |
+ if (![[NSFileManager defaultManager] fileExistsAtPath:diskAppPath]) { |
+ NSLog(@"File in DMG doesn't exist"); |
+ [delegate_ unpacker:self onExtractFailure:appPath_]; |
+ } |
+ |
+ // TODO: add progress |
+ NSError* err; |
+ if (![[NSFileManager defaultManager] copyItemAtPath:diskAppPath |
+ toPath:appPath_ |
+ error:&err]) { |
+ NSLog(@"%@", err); |
+ [delegate_ unpacker:self onExtractFailure:appPath_]; |
+ } |
+ [delegate_ unpacker:self onExtractSuccess: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 URLWithString:mountPath]); |
+ DADiskRef whole_disk = DADiskCopyWholeDisk(child_disk); |
+ |
+ DADiskUnmount(whole_disk, kDADiskUnmountOptionWhole, unmount_callback, |
Sidney San Martín
2016/08/13 12:37:18
I might also pass `kDADiskUnmountOptionForce`. If
Anna Zeng
2016/08/16 23:07:40
Done.
|
+ (void*)self); |
+ |
+ CFRelease(whole_disk); |
+ CFRelease(child_disk); |
+} |
+ |
+- (void)didFinishEjectingDisk:(DADiskRef)disk WithError:(NSError*)error { |
+ DASessionSetDispatchQueue(session, NULL); |
+ dispatch_release(unpack_dq); |
+ if (error) { |
+ [delegate_ unpacker:self onUnmountFailure:error]; |
+ } else { |
+ [delegate_ unpacker:self onUnmountSuccess:nil]; |
+ } |
+} |
+ |
+@end |