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/launch_services_util.h" | 17 #include "base/mac/launch_services_util.h" |
17 #include "base/mac/mac_logging.h" | 18 #include "base/mac/mac_logging.h" |
18 #include "base/mac/mac_util.h" | 19 #include "base/mac/mac_util.h" |
19 #include "base/mac/scoped_nsautorelease_pool.h" | 20 #include "base/mac/scoped_nsautorelease_pool.h" |
20 #include "base/mac/scoped_nsobject.h" | 21 #include "base/mac/scoped_nsobject.h" |
21 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
22 #include "base/path_service.h" | 23 #include "base/path_service.h" |
23 #include "base/strings/sys_string_conversions.h" | 24 #include "base/strings/sys_string_conversions.h" |
24 #include "base/threading/thread.h" | 25 #include "base/threading/thread.h" |
26 #include "chrome/common/chrome_constants.h" | |
25 #include "chrome/common/chrome_paths.h" | 27 #include "chrome/common/chrome_paths.h" |
26 #include "chrome/common/chrome_paths_internal.h" | 28 #include "chrome/common/chrome_paths_internal.h" |
27 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/mac/app_mode_common.h" | 30 #include "chrome/common/mac/app_mode_common.h" |
31 #include "grit/generated_resources.h" | |
29 #include "ipc/ipc_channel_proxy.h" | 32 #include "ipc/ipc_channel_proxy.h" |
30 #include "ipc/ipc_listener.h" | 33 #include "ipc/ipc_listener.h" |
31 #include "ipc/ipc_message.h" | 34 #include "ipc/ipc_message.h" |
35 #include "ui/base/resource/resource_bundle.h" | |
36 #include "ui/base/l10n/l10n_util.h" | |
32 | 37 |
33 namespace { | 38 namespace { |
34 | 39 |
35 const app_mode::ChromeAppModeInfo* g_info; | 40 const app_mode::ChromeAppModeInfo* g_info; |
36 base::Thread* g_io_thread = NULL; | 41 base::Thread* g_io_thread = NULL; |
37 | 42 |
38 } // namespace | 43 } // namespace |
39 | 44 |
40 class AppShimController; | 45 class AppShimController; |
41 | 46 |
(...skipping 15 matching lines...) Expand all Loading... | |
57 | 62 |
58 // The AppShimController is responsible for communication with the main Chrome | 63 // The AppShimController is responsible for communication with the main Chrome |
59 // process, and generally controls the lifetime of the app shim process. | 64 // process, and generally controls the lifetime of the app shim process. |
60 class AppShimController : public IPC::Listener { | 65 class AppShimController : public IPC::Listener { |
61 public: | 66 public: |
62 AppShimController(); | 67 AppShimController(); |
63 | 68 |
64 // Connects to Chrome and sends a LaunchApp message. | 69 // Connects to Chrome and sends a LaunchApp message. |
65 void Init(); | 70 void Init(); |
66 | 71 |
72 // Builds main menu bar items. | |
73 void SetUpMenu(); | |
74 | |
67 void SendSetAppHidden(bool hidden); | 75 void SendSetAppHidden(bool hidden); |
68 | 76 |
69 void SendQuitApp(); | 77 void SendQuitApp(); |
70 | 78 |
71 // Called when the app is activated, either by the user clicking on it in the | 79 // Called when the app is activated, either by the user clicking on it in the |
72 // dock or by Cmd+Tabbing to it. | 80 // dock or by Cmd+Tabbing to it. |
73 void ActivateApp(bool is_reopen); | 81 void ActivateApp(bool is_reopen); |
74 | 82 |
75 private: | 83 private: |
76 // IPC::Listener implemetation. | 84 // IPC::Listener implemetation. |
77 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 85 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
78 virtual void OnChannelError() OVERRIDE; | 86 virtual void OnChannelError() OVERRIDE; |
79 | 87 |
80 // If Chrome failed to launch the app, |success| will be false and the app | 88 // If Chrome failed to launch the app, |success| will be false and the app |
81 // shim process should die. | 89 // shim process should die. |
82 void OnLaunchAppDone(apps::AppShimLaunchResult result); | 90 void OnLaunchAppDone(apps::AppShimLaunchResult result); |
83 | 91 |
84 | |
85 // Terminates the app shim process. | 92 // Terminates the app shim process. |
86 void Close(); | 93 void Close(); |
87 | 94 |
88 IPC::ChannelProxy* channel_; | 95 IPC::ChannelProxy* channel_; |
89 base::scoped_nsobject<AppShimDelegate> nsapp_delegate_; | 96 base::scoped_nsobject<AppShimDelegate> nsapp_delegate_; |
90 bool launch_app_done_; | 97 bool launch_app_done_; |
91 | 98 |
92 DISALLOW_COPY_AND_ASSIGN(AppShimController); | 99 DISALLOW_COPY_AND_ASSIGN(AppShimController); |
93 }; | 100 }; |
94 | 101 |
95 AppShimController::AppShimController() : channel_(NULL), | 102 AppShimController::AppShimController() : channel_(NULL), |
96 launch_app_done_(false) {} | 103 launch_app_done_(false) {} |
97 | 104 |
98 void AppShimController::Init() { | 105 void AppShimController::Init() { |
99 DCHECK(g_io_thread); | 106 DCHECK(g_io_thread); |
107 | |
108 SetUpMenu(); | |
109 | |
110 // Open an IPC channel to Chrome and send the initial app launch message. | |
100 NSString* chrome_bundle_path = | 111 NSString* chrome_bundle_path = |
101 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); | 112 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); |
102 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; | 113 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; |
103 base::FilePath user_data_dir; | 114 base::FilePath user_data_dir; |
104 if (!chrome::GetUserDataDirectoryForBrowserBundle(chrome_bundle, | 115 if (!chrome::GetUserDataDirectoryForBrowserBundle(chrome_bundle, |
105 &user_data_dir)) { | 116 &user_data_dir)) { |
106 Close(); | 117 Close(); |
107 return; | 118 return; |
108 } | 119 } |
109 | 120 |
110 base::FilePath socket_path = | 121 base::FilePath socket_path = |
111 user_data_dir.Append(app_mode::kAppShimSocketName); | 122 user_data_dir.Append(app_mode::kAppShimSocketName); |
112 IPC::ChannelHandle handle(socket_path.value()); | 123 IPC::ChannelHandle handle(socket_path.value()); |
113 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 124 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
114 this, g_io_thread->message_loop_proxy().get()); | 125 this, g_io_thread->message_loop_proxy().get()); |
115 | 126 |
116 channel_->Send(new AppShimHostMsg_LaunchApp( | 127 channel_->Send(new AppShimHostMsg_LaunchApp( |
117 g_info->profile_dir, g_info->app_mode_id, | 128 g_info->profile_dir, g_info->app_mode_id, |
118 CommandLine::ForCurrentProcess()->HasSwitch(app_mode::kNoLaunchApp) ? | 129 CommandLine::ForCurrentProcess()->HasSwitch(app_mode::kNoLaunchApp) ? |
119 apps::APP_SHIM_LAUNCH_REGISTER_ONLY : apps::APP_SHIM_LAUNCH_NORMAL)); | 130 apps::APP_SHIM_LAUNCH_REGISTER_ONLY : apps::APP_SHIM_LAUNCH_NORMAL)); |
120 | 131 |
121 nsapp_delegate_.reset([[AppShimDelegate alloc] initWithController:this]); | 132 nsapp_delegate_.reset([[AppShimDelegate alloc] initWithController:this]); |
122 DCHECK(![NSApp delegate]); | 133 DCHECK(![NSApp delegate]); |
123 [NSApp setDelegate:nsapp_delegate_]; | 134 [NSApp setDelegate:nsapp_delegate_]; |
124 } | 135 } |
125 | 136 |
137 void AppShimController::SetUpMenu() { | |
138 NSString* title = base::SysUTF16ToNSString(g_info->app_mode_name); | |
139 | |
140 // Create a main menu since [NSApp mainMenu] is nil. | |
141 base::scoped_nsobject<NSMenu> main_menu([[NSMenu alloc] initWithTitle:title]); | |
142 | |
143 // The title of the first item is replaced by OSX with the name of the app and | |
144 // bold styling. Create a dummy item for this and make it hidden. | |
145 NSMenuItem* dummy_item = [main_menu addItemWithTitle:title | |
146 action:nil | |
147 keyEquivalent:@""]; | |
148 base::scoped_nsobject<NSMenu> dummy_submenu( | |
149 [[NSMenu alloc] initWithTitle:title]); | |
150 [dummy_item setSubmenu:dummy_submenu]; | |
151 [dummy_item setHidden:YES]; | |
152 | |
153 // Construct an unbolded app menu, to match how it appears in the Chrome menu | |
154 // bar when the app is focused. | |
155 NSMenuItem* item = [main_menu addItemWithTitle:title | |
156 action:nil | |
157 keyEquivalent:@""]; | |
158 base::scoped_nsobject<NSMenu> submenu([[NSMenu alloc] initWithTitle:title]); | |
159 [item setSubmenu:submenu]; | |
160 | |
161 // Add a quit entry. | |
162 NSString* quit_localized_string = | |
163 l10n_util::GetNSStringF(IDS_EXIT_MAC, g_info->app_mode_name); | |
164 [submenu addItemWithTitle:quit_localized_string | |
165 action:@selector(terminate:) | |
166 keyEquivalent:@"q"]; | |
167 | |
168 [NSApp setMainMenu:main_menu]; | |
169 } | |
170 | |
126 void AppShimController::SendQuitApp() { | 171 void AppShimController::SendQuitApp() { |
127 channel_->Send(new AppShimHostMsg_QuitApp); | 172 channel_->Send(new AppShimHostMsg_QuitApp); |
128 } | 173 } |
129 | 174 |
130 bool AppShimController::OnMessageReceived(const IPC::Message& message) { | 175 bool AppShimController::OnMessageReceived(const IPC::Message& message) { |
131 bool handled = true; | 176 bool handled = true; |
132 IPC_BEGIN_MESSAGE_MAP(AppShimController, message) | 177 IPC_BEGIN_MESSAGE_MAP(AppShimController, message) |
133 IPC_MESSAGE_HANDLER(AppShimMsg_LaunchApp_Done, OnLaunchAppDone) | 178 IPC_MESSAGE_HANDLER(AppShimMsg_LaunchApp_Done, OnLaunchAppDone) |
134 IPC_MESSAGE_UNHANDLED(handled = false) | 179 IPC_MESSAGE_UNHANDLED(handled = false) |
135 IPC_END_MESSAGE_MAP() | 180 IPC_END_MESSAGE_MAP() |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 RAW_LOG(ERROR, "App Mode Loader too old."); | 397 RAW_LOG(ERROR, "App Mode Loader too old."); |
353 return 1; | 398 return 1; |
354 } | 399 } |
355 if (info->major_version > app_mode::kCurrentChromeAppModeInfoMajorVersion) { | 400 if (info->major_version > app_mode::kCurrentChromeAppModeInfoMajorVersion) { |
356 RAW_LOG(ERROR, "Browser Framework too old to load App Shortcut."); | 401 RAW_LOG(ERROR, "Browser Framework too old to load App Shortcut."); |
357 return 1; | 402 return 1; |
358 } | 403 } |
359 | 404 |
360 g_info = info; | 405 g_info = info; |
361 | 406 |
407 // Set bundle paths. This loads the bundles. | |
408 base::mac::SetOverrideOuterBundlePath(g_info->chrome_outer_bundle_path); | |
409 base::mac::SetOverrideFrameworkBundlePath( | |
410 g_info->chrome_versioned_path.Append(chrome::kFrameworkName)); | |
411 | |
412 // Calculate the preferred locale used by Chrome. | |
413 // We can't use l10n_util::OverrideLocaleWithCocoaLocale() because | |
414 // [base::mac::OuterBundle() preferredLocalizations] seems to be equivalent to | |
tapted
2013/07/23 05:24:35
'seems to be' -> 'is' ? (i.e. we should make sure
jackhou1
2013/07/23 06:22:22
Updated the comment.
| |
415 // [[NSBundle mainBundle] preferredLocalizations]. | |
416 NSArray* preferred_languages = [NSLocale preferredLanguages]; | |
417 NSArray* supported_languages = [base::mac::OuterBundle() localizations]; | |
418 std::string preferred_localization; | |
419 for (NSString* language in preferred_languages) { | |
420 if ([supported_languages containsObject:language]) { | |
421 preferred_localization = base::SysNSStringToUTF8(language); | |
422 break; | |
423 } | |
424 } | |
425 std::string locale = l10n_util::NormalizeLocale( | |
426 l10n_util::GetApplicationLocale(preferred_localization)); | |
427 | |
428 // Load localized strings. | |
429 ResourceBundle::InitSharedInstanceLocaleOnly(locale, NULL); | |
430 | |
362 // Launch the IO thread. | 431 // Launch the IO thread. |
363 base::Thread::Options io_thread_options; | 432 base::Thread::Options io_thread_options; |
364 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 433 io_thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
365 base::Thread *io_thread = new base::Thread("CrAppShimIO"); | 434 base::Thread *io_thread = new base::Thread("CrAppShimIO"); |
366 io_thread->StartWithOptions(io_thread_options); | 435 io_thread->StartWithOptions(io_thread_options); |
367 g_io_thread = io_thread; | 436 g_io_thread = io_thread; |
368 | 437 |
369 // Find already running instances of Chrome. | 438 // Find already running instances of Chrome. |
370 NSString* chrome_bundle_path = | 439 NSString* chrome_bundle_id = [base::mac::OuterBundle() bundleIdentifier]; |
371 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); | |
372 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; | |
373 NSArray* existing_chrome = [NSRunningApplication | 440 NSArray* existing_chrome = [NSRunningApplication |
374 runningApplicationsWithBundleIdentifier:[chrome_bundle bundleIdentifier]]; | 441 runningApplicationsWithBundleIdentifier:chrome_bundle_id]; |
375 | 442 |
376 // Launch Chrome if it isn't already running. | 443 // Launch Chrome if it isn't already running. |
377 ProcessSerialNumber psn; | 444 ProcessSerialNumber psn; |
378 if ([existing_chrome count] > 0) { | 445 if ([existing_chrome count] > 0) { |
379 OSStatus status = GetProcessForPID( | 446 OSStatus status = GetProcessForPID( |
380 [[existing_chrome objectAtIndex:0] processIdentifier], &psn); | 447 [[existing_chrome objectAtIndex:0] processIdentifier], &psn); |
381 if (status) | 448 if (status) |
382 return 1; | 449 return 1; |
383 | 450 |
384 } else { | 451 } else { |
385 CommandLine command_line(CommandLine::NO_PROGRAM); | 452 CommandLine command_line(CommandLine::NO_PROGRAM); |
386 command_line.AppendSwitch(switches::kSilentLaunch); | 453 command_line.AppendSwitch(switches::kSilentLaunch); |
387 command_line.AppendSwitchPath(switches::kProfileDirectory, | 454 command_line.AppendSwitchPath(switches::kProfileDirectory, |
388 info->profile_dir); | 455 info->profile_dir); |
389 bool success = | 456 bool success = |
390 base::mac::OpenApplicationWithPath(info->chrome_outer_bundle_path, | 457 base::mac::OpenApplicationWithPath(base::mac::OuterBundlePath(), |
391 command_line, | 458 command_line, |
392 &psn); | 459 &psn); |
393 if (!success) | 460 if (!success) |
394 return 1; | 461 return 1; |
395 } | 462 } |
396 | 463 |
397 // This code abuses the fact that Apple Events sent before the process is | 464 // This code abuses the fact that Apple Events sent before the process is |
398 // fully initialized don't receive a reply until its run loop starts. Once | 465 // fully initialized don't receive a reply until its run loop starts. Once |
399 // the reply is received, Chrome will have opened its IPC port, guaranteed. | 466 // the reply is received, Chrome will have opened its IPC port, guaranteed. |
400 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; | 467 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; |
401 | 468 |
402 base::MessageLoopForUI main_message_loop; | 469 base::MessageLoopForUI main_message_loop; |
403 main_message_loop.set_thread_name("MainThread"); | 470 main_message_loop.set_thread_name("MainThread"); |
404 base::PlatformThread::SetName("CrAppShimMain"); | 471 base::PlatformThread::SetName("CrAppShimMain"); |
405 main_message_loop.Run(); | 472 main_message_loop.Run(); |
406 return 0; | 473 return 0; |
407 } | 474 } |
OLD | NEW |