OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // On Mac, one can't make shortcuts with command-line arguments. Instead, we | 5 // On Mac, one can't make shortcuts with command-line arguments. Instead, we |
6 // produce small app bundles which locate the Chromium framework and load it, | 6 // produce small app bundles which locate the Chromium framework and load it, |
7 // passing the appropriate data. This is the entry point into the framework for | 7 // passing the appropriate data. This is the entry point into the framework for |
8 // those app bundles. | 8 // those app bundles. |
9 | 9 |
10 #import <Cocoa/Cocoa.h> | 10 #import <Cocoa/Cocoa.h> |
11 | 11 |
12 #include "apps/app_shim/app_shim_messages.h" | 12 #include "apps/app_shim/app_shim_messages.h" |
13 #include "base/at_exit.h" | 13 #include "base/at_exit.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/mac/bundle_locations.h" | 16 #include "base/mac/bundle_locations.h" |
17 #include "base/mac/launch_services_util.h" | 17 #include "base/mac/launch_services_util.h" |
18 #include "base/mac/mac_logging.h" | 18 #include "base/mac/mac_logging.h" |
19 #include "base/mac/mac_util.h" | 19 #include "base/mac/mac_util.h" |
20 #include "base/mac/scoped_nsautorelease_pool.h" | 20 #include "base/mac/scoped_nsautorelease_pool.h" |
21 #include "base/mac/scoped_nsobject.h" | 21 #include "base/mac/scoped_nsobject.h" |
22 #include "base/message_loop/message_loop.h" | 22 #include "base/message_loop/message_loop.h" |
23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 24 #include "base/strings/string_number_conversions.h" |
24 #include "base/strings/sys_string_conversions.h" | 25 #include "base/strings/sys_string_conversions.h" |
25 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
26 #include "chrome/common/chrome_constants.h" | 27 #include "chrome/common/chrome_constants.h" |
27 #include "chrome/common/chrome_paths.h" | 28 #include "chrome/common/chrome_paths.h" |
28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/mac/app_mode_common.h" | 30 #include "chrome/common/mac/app_mode_common.h" |
30 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
31 #include "ipc/ipc_channel_proxy.h" | 32 #include "ipc/ipc_channel_proxy.h" |
32 #include "ipc/ipc_listener.h" | 33 #include "ipc/ipc_listener.h" |
33 #include "ipc/ipc_message.h" | 34 #include "ipc/ipc_message.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 base::FilePath user_data_dir = | 112 base::FilePath user_data_dir = |
112 g_info->user_data_dir.DirName().DirName().DirName(); | 113 g_info->user_data_dir.DirName().DirName().DirName(); |
113 CHECK(!user_data_dir.empty()); | 114 CHECK(!user_data_dir.empty()); |
114 | 115 |
115 base::FilePath socket_path = | 116 base::FilePath socket_path = |
116 user_data_dir.Append(app_mode::kAppShimSocketName); | 117 user_data_dir.Append(app_mode::kAppShimSocketName); |
117 IPC::ChannelHandle handle(socket_path.value()); | 118 IPC::ChannelHandle handle(socket_path.value()); |
118 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 119 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
119 this, g_io_thread->message_loop_proxy().get()); | 120 this, g_io_thread->message_loop_proxy().get()); |
120 | 121 |
| 122 bool launched_by_chrome = |
| 123 CommandLine::ForCurrentProcess()->HasSwitch( |
| 124 app_mode::kLaunchedByChromeProcessId); |
121 channel_->Send(new AppShimHostMsg_LaunchApp( | 125 channel_->Send(new AppShimHostMsg_LaunchApp( |
122 g_info->profile_dir, g_info->app_mode_id, | 126 g_info->profile_dir, g_info->app_mode_id, |
123 CommandLine::ForCurrentProcess()->HasSwitch(app_mode::kNoLaunchApp) ? | 127 launched_by_chrome ? |
124 apps::APP_SHIM_LAUNCH_REGISTER_ONLY : apps::APP_SHIM_LAUNCH_NORMAL)); | 128 apps::APP_SHIM_LAUNCH_REGISTER_ONLY : apps::APP_SHIM_LAUNCH_NORMAL)); |
125 | 129 |
126 nsapp_delegate_.reset([[AppShimDelegate alloc] initWithController:this]); | 130 nsapp_delegate_.reset([[AppShimDelegate alloc] initWithController:this]); |
127 DCHECK(![NSApp delegate]); | 131 DCHECK(![NSApp delegate]); |
128 [NSApp setDelegate:nsapp_delegate_]; | 132 [NSApp setDelegate:nsapp_delegate_]; |
129 } | 133 } |
130 | 134 |
131 void AppShimController::SetUpMenu() { | 135 void AppShimController::SetUpMenu() { |
132 NSString* title = base::SysUTF16ToNSString(g_info->app_mode_name); | 136 NSString* title = base::SysUTF16ToNSString(g_info->app_mode_name); |
133 | 137 |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 ResourceBundle::InitSharedInstanceLocaleOnly(locale, NULL); | 429 ResourceBundle::InitSharedInstanceLocaleOnly(locale, NULL); |
426 | 430 |
427 // Launch the IO thread. | 431 // Launch the IO thread. |
428 base::Thread::Options io_thread_options; | 432 base::Thread::Options io_thread_options; |
429 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 433 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
430 base::Thread *io_thread = new base::Thread("CrAppShimIO"); | 434 base::Thread *io_thread = new base::Thread("CrAppShimIO"); |
431 io_thread->StartWithOptions(io_thread_options); | 435 io_thread->StartWithOptions(io_thread_options); |
432 g_io_thread = io_thread; | 436 g_io_thread = io_thread; |
433 | 437 |
434 // Find already running instances of Chrome. | 438 // Find already running instances of Chrome. |
435 NSString* chrome_bundle_id = [base::mac::OuterBundle() bundleIdentifier]; | 439 pid_t pid = -1; |
436 NSArray* existing_chrome = [NSRunningApplication | 440 std::string chrome_process_id = CommandLine::ForCurrentProcess()-> |
437 runningApplicationsWithBundleIdentifier:chrome_bundle_id]; | 441 GetSwitchValueASCII(app_mode::kLaunchedByChromeProcessId); |
| 442 if (!chrome_process_id.empty()) { |
| 443 if (!base::StringToInt(chrome_process_id, &pid)) |
| 444 LOG(FATAL) << "Invalid PID: " << chrome_process_id; |
| 445 } else { |
| 446 NSString* chrome_bundle_id = [base::mac::OuterBundle() bundleIdentifier]; |
| 447 NSArray* existing_chrome = [NSRunningApplication |
| 448 runningApplicationsWithBundleIdentifier:chrome_bundle_id]; |
| 449 if ([existing_chrome count] > 0) |
| 450 pid = [[existing_chrome objectAtIndex:0] processIdentifier]; |
| 451 } |
438 | 452 |
439 // Launch Chrome if it isn't already running. | 453 // Launch Chrome if it isn't already running. |
440 ProcessSerialNumber psn; | 454 ProcessSerialNumber psn; |
441 if ([existing_chrome count] > 0) { | 455 if (pid > -1) { |
442 OSStatus status = GetProcessForPID( | 456 OSStatus status = GetProcessForPID(pid, &psn); |
443 [[existing_chrome objectAtIndex:0] processIdentifier], &psn); | |
444 if (status) | 457 if (status) |
445 return 1; | 458 return 1; |
446 | 459 |
447 } else { | 460 } else { |
448 CommandLine command_line(CommandLine::NO_PROGRAM); | 461 CommandLine command_line(CommandLine::NO_PROGRAM); |
449 command_line.AppendSwitch(switches::kSilentLaunch); | 462 command_line.AppendSwitch(switches::kSilentLaunch); |
450 command_line.AppendSwitchPath(switches::kProfileDirectory, | 463 command_line.AppendSwitchPath(switches::kProfileDirectory, |
451 info->profile_dir); | 464 info->profile_dir); |
452 bool success = | 465 bool success = |
453 base::mac::OpenApplicationWithPath(base::mac::OuterBundlePath(), | 466 base::mac::OpenApplicationWithPath(base::mac::OuterBundlePath(), |
454 command_line, | 467 command_line, |
455 kLSLaunchDefaults, | 468 kLSLaunchDefaults, |
456 &psn); | 469 &psn); |
457 if (!success) | 470 if (!success) |
458 return 1; | 471 return 1; |
459 } | 472 } |
460 | 473 |
461 // This code abuses the fact that Apple Events sent before the process is | 474 // This code abuses the fact that Apple Events sent before the process is |
462 // fully initialized don't receive a reply until its run loop starts. Once | 475 // fully initialized don't receive a reply until its run loop starts. Once |
463 // the reply is received, Chrome will have opened its IPC port, guaranteed. | 476 // the reply is received, Chrome will have opened its IPC port, guaranteed. |
464 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; | 477 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; |
465 | 478 |
466 base::MessageLoopForUI main_message_loop; | 479 base::MessageLoopForUI main_message_loop; |
467 main_message_loop.set_thread_name("MainThread"); | 480 main_message_loop.set_thread_name("MainThread"); |
468 base::PlatformThread::SetName("CrAppShimMain"); | 481 base::PlatformThread::SetName("CrAppShimMain"); |
469 main_message_loop.Run(); | 482 main_message_loop.Run(); |
470 return 0; | 483 return 0; |
471 } | 484 } |
OLD | NEW |