Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "AuthorizedInstall.h" | |
| 6 | |
| 7 // TODO: Move attemptAuthorizedInstall body into Unpacker method which will | |
| 8 // provide | |
| 9 // alternative in the case where authorization failed. | |
| 10 void attemptAuthorizedInstall() { | |
| 11 NSString* toolPath = | |
| 12 [[NSBundle mainBundle] pathForResource:@"install" ofType:@"sh"]; | |
| 13 NSFileManager* manager = [NSFileManager defaultManager]; | |
| 14 if (![manager fileExistsAtPath:toolPath]) { | |
| 15 exit(1); | |
|
Elly Fong-Jones
2016/08/16 14:59:42
that seems kind of abrupt? maybe we should signal
ivanhernandez
2016/08/16 17:18:38
Acknowledged.
| |
| 16 } | |
| 17 | |
| 18 NSString* appBundleName = @"Google Chrome.app"; | |
| 19 | |
| 20 NSArray* applicationDirectories = NSSearchPathForDirectoriesInDomains( | |
| 21 NSApplicationDirectory, NSLocalDomainMask, YES); | |
| 22 NSString* applicationDirectory = applicationDirectories.firstObject; | |
| 23 | |
| 24 // TODO: Change sourceAppBundlePath to the temporary folder that chrome will | |
| 25 // be downloaded and extracted to. | |
| 26 NSArray* downloadDirectories = NSSearchPathForDirectoriesInDomains( | |
| 27 NSDownloadsDirectory, NSUserDomainMask, YES); | |
| 28 NSString* sourceAppBundlePath = [NSString | |
| 29 pathWithComponents:@[ downloadDirectories.firstObject, appBundleName ]]; | |
| 30 NSString* destinationAppBundlePath = | |
| 31 [NSString pathWithComponents:@[ applicationDirectory, appBundleName ]]; | |
| 32 | |
| 33 const char* sourcePath = [sourceAppBundlePath UTF8String]; | |
| 34 const char* destinationPath = [applicationDirectory UTF8String]; | |
| 35 const char* fullDestinationPath = [destinationAppBundlePath UTF8String]; | |
| 36 const char* args[] = {sourcePath, destinationPath, fullDestinationPath, NULL}; | |
| 37 | |
| 38 AuthorizationRef authRef; | |
| 39 OSStatus status; | |
| 40 | |
| 41 if (attemptAuthorization(&authRef, &status)) { | |
| 42 authorizedMoveToApplications(&authRef, &status, [toolPath UTF8String], | |
| 43 args); | |
| 44 AuthorizationFree(authRef, kAuthorizationFlagDestroyRights); | |
| 45 } else { | |
| 46 // TODO: call on unauthorized install | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // Does the setup needed to authorize a tool to run as admin. | |
| 51 BOOL attemptAuthorization(AuthorizationRef* authRef, OSStatus* status) { | |
| 52 *status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, | |
| 53 kAuthorizationFlagDefaults, authRef); | |
| 54 | |
| 55 AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0}; | |
| 56 AuthorizationRights rights = {1, &items}; | |
| 57 AuthorizationFlags flags = | |
| 58 kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | | |
| 59 kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights; | |
| 60 | |
| 61 *status = AuthorizationCopyRights(*authRef, &rights, NULL, flags, NULL); | |
| 62 return (*status == errAuthorizationSuccess); | |
| 63 } | |
| 64 | |
| 65 BOOL authorizedMoveToApplications(AuthorizationRef* authRef, | |
| 66 OSStatus* status, | |
| 67 const char* toolPath, | |
| 68 const char** args) { | |
| 69 if (*status != errAuthorizationSuccess) | |
| 70 return false; | |
| 71 | |
| 72 #pragma clang diagnostic push | |
| 73 #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
| 74 *status = AuthorizationExecuteWithPrivileges( | |
|
Elly Fong-Jones
2016/08/16 14:59:42
Hm - if this is deprecated, is there a newer, shin
Mark Mentovai
2016/08/16 15:06:32
Elly Jones wrote:
| |
| 75 *authRef, toolPath, kAuthorizationFlagDefaults, (char* const*)args, NULL); | |
| 76 #pragma clang diagnostic pop | |
| 77 | |
| 78 return (*status == errAuthorizationSuccess); | |
| 79 } | |
| OLD | NEW |