| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 #include "chrome/browser/mac/relauncher.h" | 5 #include "chrome/browser/mac/relauncher.h" |
| 6 | 6 |
| 7 #include <ApplicationServices/ApplicationServices.h> | 7 #import <AppKit/AppKit.h> |
| 8 |
| 8 #include <AvailabilityMacros.h> | 9 #include <AvailabilityMacros.h> |
| 9 #include <crt_externs.h> | 10 #include <crt_externs.h> |
| 10 #include <dlfcn.h> | 11 #include <dlfcn.h> |
| 11 #include <stddef.h> | 12 #include <stddef.h> |
| 12 #include <string.h> | 13 #include <string.h> |
| 13 #include <sys/event.h> | 14 #include <sys/event.h> |
| 14 #include <sys/time.h> | 15 #include <sys/time.h> |
| 15 #include <sys/types.h> | 16 #include <sys/types.h> |
| 16 #include <unistd.h> | 17 #include <unistd.h> |
| 17 | 18 |
| 18 #include <string> | 19 #include <string> |
| 19 #include <vector> | 20 #include <vector> |
| 20 | 21 |
| 21 #include "base/files/file_util.h" | 22 #include "base/files/file_util.h" |
| 22 #include "base/files/scoped_file.h" | 23 #include "base/files/scoped_file.h" |
| 23 #include "base/logging.h" | 24 #include "base/logging.h" |
| 24 #include "base/mac/mac_logging.h" | 25 #include "base/mac/mac_logging.h" |
| 25 #include "base/mac/mac_util.h" | 26 #include "base/mac/mac_util.h" |
| 26 #include "base/mac/scoped_cftyperef.h" | 27 #include "base/mac/scoped_nsautorelease_pool.h" |
| 28 #include "base/mac/scoped_nsobject.h" |
| 27 #include "base/path_service.h" | 29 #include "base/path_service.h" |
| 28 #include "base/posix/eintr_wrapper.h" | 30 #include "base/posix/eintr_wrapper.h" |
| 29 #include "base/process/launch.h" | 31 #include "base/process/launch.h" |
| 30 #include "base/strings/stringprintf.h" | 32 #include "base/strings/stringprintf.h" |
| 31 #include "base/strings/sys_string_conversions.h" | 33 #include "base/strings/sys_string_conversions.h" |
| 32 #include "chrome/browser/mac/install_from_dmg.h" | 34 #include "chrome/browser/mac/install_from_dmg.h" |
| 33 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
| 34 #include "content/public/common/content_paths.h" | 36 #include "content/public/common/content_paths.h" |
| 35 #include "content/public/common/content_switches.h" | 37 #include "content/public/common/content_switches.h" |
| 36 #include "content/public/common/main_function_params.h" | 38 #include "content/public/common/main_function_params.h" |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 << ", fflags " << event.fflags << ", ident " << event.ident; | 240 << ", fflags " << event.fflags << ", ident " << event.ident; |
| 239 return; | 241 return; |
| 240 } | 242 } |
| 241 } | 243 } |
| 242 | 244 |
| 243 } // namespace | 245 } // namespace |
| 244 | 246 |
| 245 namespace internal { | 247 namespace internal { |
| 246 | 248 |
| 247 int RelauncherMain(const content::MainFunctionParams& main_parameters) { | 249 int RelauncherMain(const content::MainFunctionParams& main_parameters) { |
| 250 base::mac::ScopedNSAutoreleasePool pool; |
| 251 |
| 248 // CommandLine rearranges the order of the arguments returned by | 252 // CommandLine rearranges the order of the arguments returned by |
| 249 // main_parameters.argv(), rendering it impossible to determine which | 253 // main_parameters.argv(), rendering it impossible to determine which |
| 250 // arguments originally came before kRelauncherArgSeparator and which came | 254 // arguments originally came before kRelauncherArgSeparator and which came |
| 251 // after. It's crucial to distinguish between these because only those | 255 // after. It's crucial to distinguish between these because only those |
| 252 // after the separator should be given to the relaunched process; it's also | 256 // after the separator should be given to the relaunched process; it's also |
| 253 // important to not treat the path to the relaunched process as a "loose" | 257 // important to not treat the path to the relaunched process as a "loose" |
| 254 // argument. NXArgc and NXArgv are pointers to the original argc and argv as | 258 // argument. NXArgc and NXArgv are pointers to the original argc and argv as |
| 255 // passed to main(), so use those. Access them through _NSGetArgc and | 259 // passed to main(), so use those. Access them through _NSGetArgc and |
| 256 // _NSGetArgv because NXArgc and NXArgv are normally only available to a | 260 // _NSGetArgv because NXArgc and NXArgv are normally only available to a |
| 257 // main executable via crt1.o and this code will run from a dylib, and | 261 // main executable via crt1.o and this code will run from a dylib, and |
| (...skipping 16 matching lines...) Expand all Loading... |
| 274 LOG(ERROR) << "relauncher process invoked with unexpected arguments"; | 278 LOG(ERROR) << "relauncher process invoked with unexpected arguments"; |
| 275 return 1; | 279 return 1; |
| 276 } | 280 } |
| 277 | 281 |
| 278 RelauncherSynchronizeWithParent(); | 282 RelauncherSynchronizeWithParent(); |
| 279 | 283 |
| 280 // The capacity for relaunch_args is 4 less than argc, because it | 284 // The capacity for relaunch_args is 4 less than argc, because it |
| 281 // won't contain the argv[0] of the relauncher process, the | 285 // won't contain the argv[0] of the relauncher process, the |
| 282 // RelauncherTypeArg() at argv[1], kRelauncherArgSeparator, or the | 286 // RelauncherTypeArg() at argv[1], kRelauncherArgSeparator, or the |
| 283 // executable path of the process to be launched. | 287 // executable path of the process to be launched. |
| 284 base::ScopedCFTypeRef<CFMutableArrayRef> relaunch_args( | 288 base::scoped_nsobject<NSMutableArray> relaunch_args( |
| 285 CFArrayCreateMutable(NULL, argc - 4, &kCFTypeArrayCallBacks)); | 289 [[NSMutableArray alloc] initWithCapacity:argc - 4]); |
| 286 if (!relaunch_args) { | |
| 287 LOG(ERROR) << "CFArrayCreateMutable"; | |
| 288 return 1; | |
| 289 } | |
| 290 | 290 |
| 291 // Figure out what to execute, what arguments to pass it, and whether to | 291 // Figure out what to execute, what arguments to pass it, and whether to |
| 292 // start it in the background. | 292 // start it in the background. |
| 293 bool background = false; | 293 bool background = false; |
| 294 bool in_relaunch_args = false; | 294 bool in_relaunch_args = false; |
| 295 std::string dmg_bsd_device_name; | 295 std::string dmg_bsd_device_name; |
| 296 bool seen_relaunch_executable = false; | 296 bool seen_relaunch_executable = false; |
| 297 std::string relaunch_executable; | 297 std::string relaunch_executable; |
| 298 const std::string relauncher_arg_separator(kRelauncherArgSeparator); | 298 const std::string relauncher_arg_separator(kRelauncherArgSeparator); |
| 299 const std::string relauncher_dmg_device_arg = | 299 const std::string relauncher_dmg_device_arg = |
| (...skipping 19 matching lines...) Expand all Loading... |
| 319 } | 319 } |
| 320 } else { | 320 } else { |
| 321 if (!seen_relaunch_executable) { | 321 if (!seen_relaunch_executable) { |
| 322 // The first argument after kRelauncherBackgroundArg is the path to | 322 // The first argument after kRelauncherBackgroundArg is the path to |
| 323 // the executable file or .app bundle directory. The Launch Services | 323 // the executable file or .app bundle directory. The Launch Services |
| 324 // interface wants this separate from the rest of the arguments. In | 324 // interface wants this separate from the rest of the arguments. In |
| 325 // the relaunched process, this path will still be visible at argv[0]. | 325 // the relaunched process, this path will still be visible at argv[0]. |
| 326 relaunch_executable.assign(arg); | 326 relaunch_executable.assign(arg); |
| 327 seen_relaunch_executable = true; | 327 seen_relaunch_executable = true; |
| 328 } else { | 328 } else { |
| 329 base::ScopedCFTypeRef<CFStringRef> arg_cf( | 329 |
| 330 base::SysUTF8ToCFStringRef(arg)); | 330 NSString* arg_string = base::SysUTF8ToNSString(arg); |
| 331 if (!arg_cf) { | 331 if (!arg_string) { |
| 332 LOG(ERROR) << "base::SysUTF8ToCFStringRef failed for " << arg; | 332 LOG(ERROR) << "base::SysUTF8ToNSString failed for " << arg; |
| 333 return 1; | 333 return 1; |
| 334 } | 334 } |
| 335 CFArrayAppendValue(relaunch_args, arg_cf); | 335 [relaunch_args addObject:arg_string]; |
| 336 } | 336 } |
| 337 } | 337 } |
| 338 } | 338 } |
| 339 | 339 |
| 340 if (!seen_relaunch_executable) { | 340 if (!seen_relaunch_executable) { |
| 341 LOG(ERROR) << "nothing to relaunch"; | 341 LOG(ERROR) << "nothing to relaunch"; |
| 342 return 1; | 342 return 1; |
| 343 } | 343 } |
| 344 | 344 |
| 345 FSRef app_fsref; | 345 NSString* path = base::SysUTF8ToNSString(relaunch_executable); |
| 346 if (!base::mac::FSRefFromPath(relaunch_executable, &app_fsref)) { | 346 base::scoped_nsobject<NSURL> url([[NSURL alloc] initFileURLWithPath:path]); |
| 347 LOG(ERROR) << "base::mac::FSRefFromPath failed for " << relaunch_executable; | 347 NSDictionary* configuration = |
| 348 @{NSWorkspaceLaunchConfigurationArguments : (relaunch_args.get())}; |
| 349 |
| 350 NSRunningApplication *application = [[NSWorkspace sharedWorkspace] |
| 351 launchApplicationAtURL:url |
| 352 options:NSWorkspaceLaunchDefault | |
| 353 NSWorkspaceLaunchWithErrorPresentation | |
| 354 (background ? NSWorkspaceLaunchWithoutActivation |
| 355 : 0) | |
| 356 NSWorkspaceLaunchNewInstance |
| 357 configuration:configuration |
| 358 error:nil]; |
| 359 if (!application) { |
| 360 LOG(ERROR) << "Failed to relaunch " << relaunch_executable; |
| 348 return 1; | 361 return 1; |
| 349 } | 362 } |
| 350 | 363 |
| 351 LSApplicationParameters ls_parameters = { | |
| 352 0, // version | |
| 353 kLSLaunchDefaults | kLSLaunchAndDisplayErrors | kLSLaunchNewInstance | | |
| 354 (background ? kLSLaunchDontSwitch : 0), | |
| 355 &app_fsref, | |
| 356 NULL, // asyncLaunchRefCon | |
| 357 NULL, // environment | |
| 358 relaunch_args, | |
| 359 NULL // initialEvent | |
| 360 }; | |
| 361 | |
| 362 OSStatus status = LSOpenApplication(&ls_parameters, NULL); | |
| 363 if (status != noErr) { | |
| 364 OSSTATUS_LOG(ERROR, status) << "LSOpenApplication"; | |
| 365 return 1; | |
| 366 } | |
| 367 | |
| 368 // The application should have relaunched (or is in the process of | 364 // The application should have relaunched (or is in the process of |
| 369 // relaunching). From this point on, only clean-up tasks should occur, and | 365 // relaunching). From this point on, only clean-up tasks should occur, and |
| 370 // failures are tolerable. | 366 // failures are tolerable. |
| 371 | 367 |
| 372 if (!dmg_bsd_device_name.empty()) { | 368 if (!dmg_bsd_device_name.empty()) { |
| 373 EjectAndTrashDiskImage(dmg_bsd_device_name); | 369 EjectAndTrashDiskImage(dmg_bsd_device_name); |
| 374 } | 370 } |
| 375 | 371 |
| 376 return 0; | 372 return 0; |
| 377 } | 373 } |
| 378 | 374 |
| 379 } // namespace internal | 375 } // namespace internal |
| 380 | 376 |
| 381 } // namespace mac_relauncher | 377 } // namespace mac_relauncher |
| OLD | NEW |