| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 // 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/launch_services_util.h" | 16 #include "base/mac/launch_services_util.h" |
| 17 #include "base/mac/mac_logging.h" | 17 #include "base/mac/mac_logging.h" |
| 18 #include "base/mac/mac_util.h" | 18 #include "base/mac/mac_util.h" |
| 19 #include "base/mac/scoped_nsautorelease_pool.h" | 19 #include "base/mac/scoped_nsautorelease_pool.h" |
| 20 #include "base/memory/scoped_nsobject.h" |
| 20 #include "base/message_loop.h" | 21 #include "base/message_loop.h" |
| 21 #include "base/path_service.h" | 22 #include "base/path_service.h" |
| 22 #include "base/strings/sys_string_conversions.h" | 23 #include "base/strings/sys_string_conversions.h" |
| 23 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
| 24 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
| 25 #include "chrome/common/chrome_paths_internal.h" | 26 #include "chrome/common/chrome_paths_internal.h" |
| 26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/mac/app_mode_common.h" | 28 #include "chrome/common/mac/app_mode_common.h" |
| 28 #include "ipc/ipc_channel_proxy.h" | 29 #include "ipc/ipc_channel_proxy.h" |
| 29 #include "ipc/ipc_listener.h" | 30 #include "ipc/ipc_listener.h" |
| 30 #include "ipc/ipc_message.h" | 31 #include "ipc/ipc_message.h" |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 const app_mode::ChromeAppModeInfo* g_info; | 35 const app_mode::ChromeAppModeInfo* g_info; |
| 35 base::Thread* g_io_thread = NULL; | 36 base::Thread* g_io_thread = NULL; |
| 36 | 37 |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 40 class AppShimController; |
| 41 |
| 42 @interface AppShimDelegate : NSObject<NSApplicationDelegate> { |
| 43 @private |
| 44 AppShimController* appShimController_; // Weak. Owns us. |
| 45 BOOL terminateNow_; |
| 46 BOOL terminateRequested_; |
| 47 } |
| 48 |
| 49 - (id)initWithController:(AppShimController*)controller; |
| 50 |
| 51 - (void)terminateNow; |
| 52 |
| 53 @end |
| 54 |
| 39 // The AppShimController is responsible for communication with the main Chrome | 55 // The AppShimController is responsible for communication with the main Chrome |
| 40 // process, and generally controls the lifetime of the app shim process. | 56 // process, and generally controls the lifetime of the app shim process. |
| 41 class AppShimController : public IPC::Listener { | 57 class AppShimController : public IPC::Listener { |
| 42 public: | 58 public: |
| 43 AppShimController(); | 59 AppShimController(); |
| 44 | 60 |
| 45 // Connects to Chrome and sends a LaunchApp message. | 61 // Connects to Chrome and sends a LaunchApp message. |
| 46 void Init(); | 62 void Init(); |
| 47 | 63 |
| 64 // Sends a QuitApp message to Chrome. |
| 65 void QuitApp(); |
| 66 |
| 48 private: | 67 private: |
| 49 // IPC::Listener implemetation. | 68 // IPC::Listener implemetation. |
| 50 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 69 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 51 virtual void OnChannelError() OVERRIDE; | 70 virtual void OnChannelError() OVERRIDE; |
| 52 | 71 |
| 53 // If Chrome failed to launch the app, |success| will be false and the app | 72 // If Chrome failed to launch the app, |success| will be false and the app |
| 54 // shim process should die. | 73 // shim process should die. |
| 55 void OnLaunchAppDone(bool success); | 74 void OnLaunchAppDone(bool success); |
| 56 | 75 |
| 57 // Called when the app is activated, either by the user clicking on it in the | 76 // Called when the app is activated, either by the user clicking on it in the |
| 58 // dock or by Cmd+Tabbing to it. | 77 // dock or by Cmd+Tabbing to it. |
| 59 void OnDidActivateApplication(); | 78 void OnDidActivateApplication(); |
| 60 | 79 |
| 61 // Quits the app shim process. | 80 // Terminates the app shim process. |
| 62 void Quit(); | 81 void Close(); |
| 63 | 82 |
| 64 IPC::ChannelProxy* channel_; | 83 IPC::ChannelProxy* channel_; |
| 84 scoped_nsobject<AppShimDelegate> nsapp_delegate_; |
| 65 | 85 |
| 66 DISALLOW_COPY_AND_ASSIGN(AppShimController); | 86 DISALLOW_COPY_AND_ASSIGN(AppShimController); |
| 67 }; | 87 }; |
| 68 | 88 |
| 69 AppShimController::AppShimController() : channel_(NULL) { | 89 AppShimController::AppShimController() : channel_(NULL) { |
| 70 } | 90 } |
| 71 | 91 |
| 72 void AppShimController::Init() { | 92 void AppShimController::Init() { |
| 73 DCHECK(g_io_thread); | 93 DCHECK(g_io_thread); |
| 74 NSString* chrome_bundle_path = | 94 NSString* chrome_bundle_path = |
| 75 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); | 95 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); |
| 76 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; | 96 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; |
| 77 base::FilePath user_data_dir; | 97 base::FilePath user_data_dir; |
| 78 if (!chrome::GetUserDataDirectoryForBrowserBundle(chrome_bundle, | 98 if (!chrome::GetUserDataDirectoryForBrowserBundle(chrome_bundle, |
| 79 &user_data_dir)) { | 99 &user_data_dir)) { |
| 80 Quit(); | 100 Close(); |
| 81 return; | 101 return; |
| 82 } | 102 } |
| 83 | 103 |
| 84 base::FilePath socket_path = | 104 base::FilePath socket_path = |
| 85 user_data_dir.Append(app_mode::kAppShimSocketName); | 105 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 86 IPC::ChannelHandle handle(socket_path.value()); | 106 IPC::ChannelHandle handle(socket_path.value()); |
| 87 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 107 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
| 88 this, g_io_thread->message_loop_proxy()); | 108 this, g_io_thread->message_loop_proxy()); |
| 89 | 109 |
| 90 channel_->Send(new AppShimHostMsg_LaunchApp( | 110 channel_->Send(new AppShimHostMsg_LaunchApp( |
| 91 g_info->profile_dir.value(), g_info->app_mode_id)); | 111 g_info->profile_dir.value(), g_info->app_mode_id)); |
| 112 |
| 113 nsapp_delegate_.reset([[AppShimDelegate alloc] initWithController:this]); |
| 114 [NSApp setDelegate:nsapp_delegate_]; |
| 115 } |
| 116 |
| 117 void AppShimController::QuitApp() { |
| 118 channel_->Send(new AppShimHostMsg_QuitApp); |
| 92 } | 119 } |
| 93 | 120 |
| 94 bool AppShimController::OnMessageReceived(const IPC::Message& message) { | 121 bool AppShimController::OnMessageReceived(const IPC::Message& message) { |
| 95 bool handled = true; | 122 bool handled = true; |
| 96 IPC_BEGIN_MESSAGE_MAP(AppShimController, message) | 123 IPC_BEGIN_MESSAGE_MAP(AppShimController, message) |
| 97 IPC_MESSAGE_HANDLER(AppShimMsg_LaunchApp_Done, OnLaunchAppDone) | 124 IPC_MESSAGE_HANDLER(AppShimMsg_LaunchApp_Done, OnLaunchAppDone) |
| 98 IPC_MESSAGE_UNHANDLED(handled = false) | 125 IPC_MESSAGE_UNHANDLED(handled = false) |
| 99 IPC_END_MESSAGE_MAP() | 126 IPC_END_MESSAGE_MAP() |
| 100 | 127 |
| 101 return handled; | 128 return handled; |
| 102 } | 129 } |
| 103 | 130 |
| 104 void AppShimController::OnChannelError() { | 131 void AppShimController::OnChannelError() { |
| 105 LOG(ERROR) << "App shim channel error."; | 132 LOG(ERROR) << "App shim channel error."; |
| 106 Quit(); | 133 Close(); |
| 107 } | 134 } |
| 108 | 135 |
| 109 void AppShimController::OnLaunchAppDone(bool success) { | 136 void AppShimController::OnLaunchAppDone(bool success) { |
| 110 if (!success) { | 137 if (!success) { |
| 111 Quit(); | 138 Close(); |
| 112 return; | 139 return; |
| 113 } | 140 } |
| 114 [[[NSWorkspace sharedWorkspace] notificationCenter] | 141 [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 115 addObserverForName:NSWorkspaceDidActivateApplicationNotification | 142 addObserverForName:NSWorkspaceDidActivateApplicationNotification |
| 116 object:nil | 143 object:nil |
| 117 queue:nil | 144 queue:nil |
| 118 usingBlock:^(NSNotification* notification) { | 145 usingBlock:^(NSNotification* notification) { |
| 119 NSRunningApplication* activated_app = | 146 NSRunningApplication* activated_app = |
| 120 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; | 147 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; |
| 121 if ([activated_app isEqual:[NSRunningApplication currentApplication]]) | 148 if ([activated_app isEqual:[NSRunningApplication currentApplication]]) |
| 122 OnDidActivateApplication(); | 149 OnDidActivateApplication(); |
| 123 }]; | 150 }]; |
| 124 } | 151 } |
| 125 | 152 |
| 126 void AppShimController::Quit() { | 153 void AppShimController::Close() { |
| 127 [NSApp terminate:nil]; | 154 [nsapp_delegate_ terminateNow]; |
| 128 } | 155 } |
| 129 | 156 |
| 130 void AppShimController::OnDidActivateApplication() { | 157 void AppShimController::OnDidActivateApplication() { |
| 131 channel_->Send(new AppShimHostMsg_FocusApp); | 158 channel_->Send(new AppShimHostMsg_FocusApp); |
| 132 } | 159 } |
| 133 | 160 |
| 161 @implementation AppShimDelegate |
| 162 |
| 163 - (id)initWithController:(AppShimController*)controller { |
| 164 if ((self = [super init])) { |
| 165 appShimController_ = controller; |
| 166 } |
| 167 return self; |
| 168 } |
| 169 |
| 170 - (NSApplicationTerminateReply) |
| 171 applicationShouldTerminate:(NSApplication*)sender { |
| 172 if (terminateNow_) |
| 173 return NSTerminateNow; |
| 174 |
| 175 appShimController_->QuitApp(); |
| 176 // Wait for the channel to close before terminating. |
| 177 terminateRequested_ = YES; |
| 178 return NSTerminateLater; |
| 179 } |
| 180 |
| 181 - (void)terminateNow { |
| 182 if (terminateRequested_) { |
| 183 [NSApp replyToApplicationShouldTerminate:NSTerminateNow]; |
| 184 return; |
| 185 } |
| 186 |
| 187 terminateNow_ = YES; |
| 188 [NSApp terminate:nil]; |
| 189 } |
| 190 |
| 191 @end |
| 192 |
| 134 //----------------------------------------------------------------------------- | 193 //----------------------------------------------------------------------------- |
| 135 | 194 |
| 136 // A ReplyEventHandler is a helper class to send an Apple Event to a process | 195 // A ReplyEventHandler is a helper class to send an Apple Event to a process |
| 137 // and call a callback when the reply returns. | 196 // and call a callback when the reply returns. |
| 138 // | 197 // |
| 139 // This is used to 'ping' the main Chrome process -- once Chrome has sent back | 198 // This is used to 'ping' the main Chrome process -- once Chrome has sent back |
| 140 // an Apple Event reply, it's guaranteed that it has opened the IPC channel | 199 // an Apple Event reply, it's guaranteed that it has opened the IPC channel |
| 141 // that the app shim will connect to. | 200 // that the app shim will connect to. |
| 142 @interface ReplyEventHandler : NSObject { | 201 @interface ReplyEventHandler : NSObject { |
| 143 base::Callback<void(bool)> onReply_; | 202 base::Callback<void(bool)> onReply_; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 // fully initialized don't receive a reply until its run loop starts. Once | 357 // fully initialized don't receive a reply until its run loop starts. Once |
| 299 // the reply is received, Chrome will have opened its IPC port, guaranteed. | 358 // the reply is received, Chrome will have opened its IPC port, guaranteed. |
| 300 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; | 359 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; |
| 301 | 360 |
| 302 base::MessageLoopForUI main_message_loop; | 361 base::MessageLoopForUI main_message_loop; |
| 303 main_message_loop.set_thread_name("MainThread"); | 362 main_message_loop.set_thread_name("MainThread"); |
| 304 base::PlatformThread::SetName("CrAppShimMain"); | 363 base::PlatformThread::SetName("CrAppShimMain"); |
| 305 main_message_loop.Run(); | 364 main_message_loop.Run(); |
| 306 return 0; | 365 return 0; |
| 307 } | 366 } |
| OLD | NEW |