Chromium Code Reviews| 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 { | |
|
tapted
2013/05/24 08:43:51
NSObject -> NSObject<NSApplicationDelegate> to say
jackhou1
2013/05/27 01:02:47
Done.
| |
| 43 @private | |
| 44 AppShimController* appShimController_; | |
|
tapted
2013/05/24 08:43:51
needs a comment " // Weak. Owns us."
jackhou1
2013/05/27 01:02:47
Done.
| |
| 45 BOOL terminateNow_; | |
| 46 } | |
| 47 | |
| 48 @property(assign, nonatomic) AppShimController* appShimController; | |
| 49 | |
| 50 - (void)terminateNow; | |
| 51 | |
| 52 @end | |
| 53 | |
| 39 // The AppShimController is responsible for communication with the main Chrome | 54 // The AppShimController is responsible for communication with the main Chrome |
| 40 // process, and generally controls the lifetime of the app shim process. | 55 // process, and generally controls the lifetime of the app shim process. |
| 41 class AppShimController : public IPC::Listener { | 56 class AppShimController : public IPC::Listener { |
| 42 public: | 57 public: |
| 43 AppShimController(); | 58 AppShimController(); |
| 44 | 59 |
| 45 // Connects to Chrome and sends a LaunchApp message. | 60 // Connects to Chrome and sends a LaunchApp message. |
| 46 void Init(); | 61 void Init(); |
| 47 | 62 |
| 63 // Sends a QuitApp message to Chrome. | |
| 64 void QuitApp(); | |
| 65 | |
| 48 private: | 66 private: |
| 49 // IPC::Listener implemetation. | 67 // IPC::Listener implemetation. |
| 50 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 68 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 51 virtual void OnChannelError() OVERRIDE; | 69 virtual void OnChannelError() OVERRIDE; |
| 52 | 70 |
| 53 // If Chrome failed to launch the app, |success| will be false and the app | 71 // If Chrome failed to launch the app, |success| will be false and the app |
| 54 // shim process should die. | 72 // shim process should die. |
| 55 void OnLaunchAppDone(bool success); | 73 void OnLaunchAppDone(bool success); |
| 56 | 74 |
| 57 // Called when the app is activated, either by the user clicking on it in the | 75 // Called when the app is activated, either by the user clicking on it in the |
| 58 // dock or by Cmd+Tabbing to it. | 76 // dock or by Cmd+Tabbing to it. |
| 59 void OnDidActivateApplication(); | 77 void OnDidActivateApplication(); |
| 60 | 78 |
| 61 // Quits the app shim process. | 79 // Quits the app shim process. |
| 62 void Quit(); | 80 void Quit(); |
| 63 | 81 |
| 64 IPC::ChannelProxy* channel_; | 82 IPC::ChannelProxy* channel_; |
| 83 scoped_nsobject<AppShimDelegate> nsapp_delegate_; | |
| 65 | 84 |
| 66 DISALLOW_COPY_AND_ASSIGN(AppShimController); | 85 DISALLOW_COPY_AND_ASSIGN(AppShimController); |
| 67 }; | 86 }; |
| 68 | 87 |
| 69 AppShimController::AppShimController() : channel_(NULL) { | 88 AppShimController::AppShimController() : channel_(NULL) { |
| 70 } | 89 } |
| 71 | 90 |
| 72 void AppShimController::Init() { | 91 void AppShimController::Init() { |
| 73 DCHECK(g_io_thread); | 92 DCHECK(g_io_thread); |
| 74 NSString* chrome_bundle_path = | 93 NSString* chrome_bundle_path = |
| 75 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); | 94 base::SysUTF8ToNSString(g_info->chrome_outer_bundle_path.value()); |
| 76 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; | 95 NSBundle* chrome_bundle = [NSBundle bundleWithPath:chrome_bundle_path]; |
| 77 base::FilePath user_data_dir; | 96 base::FilePath user_data_dir; |
| 78 if (!chrome::GetUserDataDirectoryForBrowserBundle(chrome_bundle, | 97 if (!chrome::GetUserDataDirectoryForBrowserBundle(chrome_bundle, |
| 79 &user_data_dir)) { | 98 &user_data_dir)) { |
| 80 Quit(); | 99 Quit(); |
| 81 return; | 100 return; |
| 82 } | 101 } |
| 83 | 102 |
| 84 base::FilePath socket_path = | 103 base::FilePath socket_path = |
| 85 user_data_dir.Append(app_mode::kAppShimSocketName); | 104 user_data_dir.Append(app_mode::kAppShimSocketName); |
| 86 IPC::ChannelHandle handle(socket_path.value()); | 105 IPC::ChannelHandle handle(socket_path.value()); |
| 87 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, | 106 channel_ = new IPC::ChannelProxy(handle, IPC::Channel::MODE_NAMED_CLIENT, |
| 88 this, g_io_thread->message_loop_proxy()); | 107 this, g_io_thread->message_loop_proxy()); |
| 89 | 108 |
| 90 channel_->Send(new AppShimHostMsg_LaunchApp( | 109 channel_->Send(new AppShimHostMsg_LaunchApp( |
| 91 g_info->profile_dir.value(), g_info->app_mode_id)); | 110 g_info->profile_dir.value(), g_info->app_mode_id)); |
| 111 | |
| 112 nsapp_delegate_.reset([[AppShimDelegate alloc] init]); | |
|
tapted
2013/05/24 08:43:51
perhaps initWithController rather than the separat
jackhou1
2013/05/27 01:02:47
Done.
| |
| 113 [nsapp_delegate_ setAppShimController: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; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 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::Quit() { |
|
tapted
2013/05/24 08:43:51
will it be less confusing if this is called 'Close
jackhou1
2013/05/27 01:02:47
Yeah, agreed.
| |
| 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 @synthesize appShimController = appShimController_; | |
| 164 | |
| 165 - (id)init { | |
|
tapted
2013/05/24 08:43:51
init functions all follow the pattern
- (id) init
jackhou1
2013/05/27 01:02:47
Done.
| |
| 166 terminateNow_ = NO; | |
|
tapted
2013/05/24 08:43:51
don't need to initialize objective C members that
jackhou1
2013/05/27 01:02:47
Done.
| |
| 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 return NSTerminateCancel; | |
|
tapted
2013/05/24 08:43:51
I think this needs to set a flag like 'terminateRe
jackhou1
2013/05/27 01:02:47
Done.
| |
| 178 } | |
| 179 | |
| 180 - (void)terminateNow { | |
| 181 terminateNow_ = YES; | |
|
tapted
2013/05/24 08:43:51
...then after this check
if (terminateRequested_)
jackhou1
2013/05/27 01:02:47
Done.
| |
| 182 [NSApp terminate:nil]; | |
| 183 } | |
| 184 | |
| 185 @end | |
| 186 | |
| 134 //----------------------------------------------------------------------------- | 187 //----------------------------------------------------------------------------- |
| 135 | 188 |
| 136 // A ReplyEventHandler is a helper class to send an Apple Event to a process | 189 // A ReplyEventHandler is a helper class to send an Apple Event to a process |
| 137 // and call a callback when the reply returns. | 190 // and call a callback when the reply returns. |
| 138 // | 191 // |
| 139 // This is used to 'ping' the main Chrome process -- once Chrome has sent back | 192 // 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 | 193 // an Apple Event reply, it's guaranteed that it has opened the IPC channel |
| 141 // that the app shim will connect to. | 194 // that the app shim will connect to. |
| 142 @interface ReplyEventHandler : NSObject { | 195 @interface ReplyEventHandler : NSObject { |
| 143 base::Callback<void(bool)> onReply_; | 196 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 | 351 // 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. | 352 // the reply is received, Chrome will have opened its IPC port, guaranteed. |
| 300 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; | 353 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; |
| 301 | 354 |
| 302 base::MessageLoopForUI main_message_loop; | 355 base::MessageLoopForUI main_message_loop; |
| 303 main_message_loop.set_thread_name("MainThread"); | 356 main_message_loop.set_thread_name("MainThread"); |
| 304 base::PlatformThread::SetName("CrAppShimMain"); | 357 base::PlatformThread::SetName("CrAppShimMain"); |
| 305 main_message_loop.Run(); | 358 main_message_loop.Run(); |
| 306 return 0; | 359 return 0; |
| 307 } | 360 } |
| OLD | NEW |