Chromium Code Reviews| 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_nsobject.h" |
| 27 #include "base/path_service.h" | 28 #include "base/path_service.h" |
| 28 #include "base/posix/eintr_wrapper.h" | 29 #include "base/posix/eintr_wrapper.h" |
| 29 #include "base/process/launch.h" | 30 #include "base/process/launch.h" |
| 30 #include "base/strings/stringprintf.h" | 31 #include "base/strings/stringprintf.h" |
| 31 #include "base/strings/sys_string_conversions.h" | 32 #include "base/strings/sys_string_conversions.h" |
| 32 #include "chrome/browser/mac/install_from_dmg.h" | 33 #include "chrome/browser/mac/install_from_dmg.h" |
| 33 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 34 #include "content/public/common/content_paths.h" | 35 #include "content/public/common/content_paths.h" |
| 35 #include "content/public/common/content_switches.h" | 36 #include "content/public/common/content_switches.h" |
| 36 #include "content/public/common/main_function_params.h" | 37 #include "content/public/common/main_function_params.h" |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 LOG(ERROR) << "relauncher process invoked with unexpected arguments"; | 275 LOG(ERROR) << "relauncher process invoked with unexpected arguments"; |
| 275 return 1; | 276 return 1; |
| 276 } | 277 } |
| 277 | 278 |
| 278 RelauncherSynchronizeWithParent(); | 279 RelauncherSynchronizeWithParent(); |
| 279 | 280 |
| 280 // The capacity for relaunch_args is 4 less than argc, because it | 281 // The capacity for relaunch_args is 4 less than argc, because it |
| 281 // won't contain the argv[0] of the relauncher process, the | 282 // won't contain the argv[0] of the relauncher process, the |
| 282 // RelauncherTypeArg() at argv[1], kRelauncherArgSeparator, or the | 283 // RelauncherTypeArg() at argv[1], kRelauncherArgSeparator, or the |
| 283 // executable path of the process to be launched. | 284 // executable path of the process to be launched. |
| 284 base::ScopedCFTypeRef<CFMutableArrayRef> relaunch_args( | 285 base::scoped_nsobject<NSMutableArray> relaunch_args( |
| 285 CFArrayCreateMutable(NULL, argc - 4, &kCFTypeArrayCallBacks)); | 286 [[NSMutableArray alloc] initWithCapacity:argc - 4]); |
| 286 if (!relaunch_args) { | |
| 287 LOG(ERROR) << "CFArrayCreateMutable"; | |
| 288 return 1; | |
| 289 } | |
| 290 | 287 |
| 291 // Figure out what to execute, what arguments to pass it, and whether to | 288 // Figure out what to execute, what arguments to pass it, and whether to |
| 292 // start it in the background. | 289 // start it in the background. |
| 293 bool background = false; | 290 bool background = false; |
| 294 bool in_relaunch_args = false; | 291 bool in_relaunch_args = false; |
| 295 std::string dmg_bsd_device_name; | 292 std::string dmg_bsd_device_name; |
| 296 bool seen_relaunch_executable = false; | 293 bool seen_relaunch_executable = false; |
| 297 std::string relaunch_executable; | 294 std::string relaunch_executable; |
| 298 const std::string relauncher_arg_separator(kRelauncherArgSeparator); | 295 const std::string relauncher_arg_separator(kRelauncherArgSeparator); |
| 299 const std::string relauncher_dmg_device_arg = | 296 const std::string relauncher_dmg_device_arg = |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 319 } | 316 } |
| 320 } else { | 317 } else { |
| 321 if (!seen_relaunch_executable) { | 318 if (!seen_relaunch_executable) { |
| 322 // The first argument after kRelauncherBackgroundArg is the path to | 319 // The first argument after kRelauncherBackgroundArg is the path to |
| 323 // the executable file or .app bundle directory. The Launch Services | 320 // the executable file or .app bundle directory. The Launch Services |
| 324 // interface wants this separate from the rest of the arguments. In | 321 // interface wants this separate from the rest of the arguments. In |
| 325 // the relaunched process, this path will still be visible at argv[0]. | 322 // the relaunched process, this path will still be visible at argv[0]. |
| 326 relaunch_executable.assign(arg); | 323 relaunch_executable.assign(arg); |
| 327 seen_relaunch_executable = true; | 324 seen_relaunch_executable = true; |
| 328 } else { | 325 } else { |
| 329 base::ScopedCFTypeRef<CFStringRef> arg_cf( | 326 |
| 330 base::SysUTF8ToCFStringRef(arg)); | 327 NSString* arg_string = base::SysUTF8ToNSString(arg); |
|
Mark Mentovai
2016/08/22 17:56:47
Are we sure we have an autorelease pool when this
erikchen
2016/08/22 18:05:21
I don't believe so. Here's a stack trace:
"""
Sta
| |
| 331 if (!arg_cf) { | 328 if (!arg_string) { |
| 332 LOG(ERROR) << "base::SysUTF8ToCFStringRef failed for " << arg; | 329 LOG(ERROR) << "base::SysUTF8ToNSString failed for " << arg; |
| 333 return 1; | 330 return 1; |
| 334 } | 331 } |
| 335 CFArrayAppendValue(relaunch_args, arg_cf); | 332 [relaunch_args addObject:arg_string]; |
| 336 } | 333 } |
| 337 } | 334 } |
| 338 } | 335 } |
| 339 | 336 |
| 340 if (!seen_relaunch_executable) { | 337 if (!seen_relaunch_executable) { |
| 341 LOG(ERROR) << "nothing to relaunch"; | 338 LOG(ERROR) << "nothing to relaunch"; |
| 342 return 1; | 339 return 1; |
| 343 } | 340 } |
| 344 | 341 |
| 345 FSRef app_fsref; | 342 NSString* path = base::SysUTF8ToNSString(relaunch_executable); |
| 346 if (!base::mac::FSRefFromPath(relaunch_executable, &app_fsref)) { | 343 base::scoped_nsobject<NSURL> url([[NSURL alloc] initFileURLWithPath:path]); |
| 347 LOG(ERROR) << "base::mac::FSRefFromPath failed for " << relaunch_executable; | 344 NSDictionary* configuration = |
| 345 @{NSWorkspaceLaunchConfigurationArguments : (relaunch_args.get())}; | |
| 346 | |
| 347 NSRunningApplication *application = [[NSWorkspace sharedWorkspace] | |
| 348 launchApplicationAtURL:url | |
| 349 options:NSWorkspaceLaunchDefault | | |
| 350 NSWorkspaceLaunchWithErrorPresentation | | |
| 351 (background ? NSWorkspaceLaunchWithoutActivation | |
| 352 : 0) | | |
| 353 NSWorkspaceLaunchNewInstance | |
| 354 configuration:configuration | |
| 355 error:nil]; | |
| 356 if (!application) { | |
| 357 LOG(ERROR) << "Failed to relaunch " << relaunch_executable; | |
| 348 return 1; | 358 return 1; |
| 349 } | 359 } |
| 350 | 360 |
| 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 | 361 // The application should have relaunched (or is in the process of |
| 369 // relaunching). From this point on, only clean-up tasks should occur, and | 362 // relaunching). From this point on, only clean-up tasks should occur, and |
| 370 // failures are tolerable. | 363 // failures are tolerable. |
| 371 | 364 |
| 372 if (!dmg_bsd_device_name.empty()) { | 365 if (!dmg_bsd_device_name.empty()) { |
| 373 EjectAndTrashDiskImage(dmg_bsd_device_name); | 366 EjectAndTrashDiskImage(dmg_bsd_device_name); |
| 374 } | 367 } |
| 375 | 368 |
| 376 return 0; | 369 return 0; |
| 377 } | 370 } |
| 378 | 371 |
| 379 } // namespace internal | 372 } // namespace internal |
| 380 | 373 |
| 381 } // namespace mac_relauncher | 374 } // namespace mac_relauncher |
| OLD | NEW |