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 DCHECK(![NSApp delegate]); |
| 115 [NSApp setDelegate:nsapp_delegate_]; |
| 116 } |
| 117 |
| 118 void AppShimController::QuitApp() { |
| 119 channel_->Send(new AppShimHostMsg_QuitApp); |
92 } | 120 } |
93 | 121 |
94 bool AppShimController::OnMessageReceived(const IPC::Message& message) { | 122 bool AppShimController::OnMessageReceived(const IPC::Message& message) { |
95 bool handled = true; | 123 bool handled = true; |
96 IPC_BEGIN_MESSAGE_MAP(AppShimController, message) | 124 IPC_BEGIN_MESSAGE_MAP(AppShimController, message) |
97 IPC_MESSAGE_HANDLER(AppShimMsg_LaunchApp_Done, OnLaunchAppDone) | 125 IPC_MESSAGE_HANDLER(AppShimMsg_LaunchApp_Done, OnLaunchAppDone) |
98 IPC_MESSAGE_UNHANDLED(handled = false) | 126 IPC_MESSAGE_UNHANDLED(handled = false) |
99 IPC_END_MESSAGE_MAP() | 127 IPC_END_MESSAGE_MAP() |
100 | 128 |
101 return handled; | 129 return handled; |
102 } | 130 } |
103 | 131 |
104 void AppShimController::OnChannelError() { | 132 void AppShimController::OnChannelError() { |
105 LOG(ERROR) << "App shim channel error."; | 133 LOG(ERROR) << "App shim channel error."; |
106 Quit(); | 134 Close(); |
107 } | 135 } |
108 | 136 |
109 void AppShimController::OnLaunchAppDone(bool success) { | 137 void AppShimController::OnLaunchAppDone(bool success) { |
110 if (!success) { | 138 if (!success) { |
111 Quit(); | 139 Close(); |
112 return; | 140 return; |
113 } | 141 } |
114 [[[NSWorkspace sharedWorkspace] notificationCenter] | 142 [[[NSWorkspace sharedWorkspace] notificationCenter] |
115 addObserverForName:NSWorkspaceDidActivateApplicationNotification | 143 addObserverForName:NSWorkspaceDidActivateApplicationNotification |
116 object:nil | 144 object:nil |
117 queue:nil | 145 queue:nil |
118 usingBlock:^(NSNotification* notification) { | 146 usingBlock:^(NSNotification* notification) { |
119 NSRunningApplication* activated_app = | 147 NSRunningApplication* activated_app = |
120 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; | 148 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; |
121 if ([activated_app isEqual:[NSRunningApplication currentApplication]]) | 149 if ([activated_app isEqual:[NSRunningApplication currentApplication]]) |
122 OnDidActivateApplication(); | 150 OnDidActivateApplication(); |
123 }]; | 151 }]; |
124 } | 152 } |
125 | 153 |
126 void AppShimController::Quit() { | 154 void AppShimController::Close() { |
127 [NSApp terminate:nil]; | 155 [nsapp_delegate_ terminateNow]; |
128 } | 156 } |
129 | 157 |
130 void AppShimController::OnDidActivateApplication() { | 158 void AppShimController::OnDidActivateApplication() { |
131 channel_->Send(new AppShimHostMsg_FocusApp); | 159 channel_->Send(new AppShimHostMsg_FocusApp); |
132 } | 160 } |
133 | 161 |
| 162 @implementation AppShimDelegate |
| 163 |
| 164 - (id)initWithController:(AppShimController*)controller { |
| 165 if ((self = [super init])) { |
| 166 appShimController_ = controller; |
| 167 } |
| 168 return self; |
| 169 } |
| 170 |
| 171 - (NSApplicationTerminateReply) |
| 172 applicationShouldTerminate:(NSApplication*)sender { |
| 173 if (terminateNow_) |
| 174 return NSTerminateNow; |
| 175 |
| 176 appShimController_->QuitApp(); |
| 177 // Wait for the channel to close before terminating. |
| 178 terminateRequested_ = YES; |
| 179 return NSTerminateLater; |
| 180 } |
| 181 |
| 182 - (void)terminateNow { |
| 183 if (terminateRequested_) { |
| 184 [NSApp replyToApplicationShouldTerminate:NSTerminateNow]; |
| 185 return; |
| 186 } |
| 187 |
| 188 terminateNow_ = YES; |
| 189 [NSApp terminate:nil]; |
| 190 } |
| 191 |
| 192 @end |
| 193 |
134 //----------------------------------------------------------------------------- | 194 //----------------------------------------------------------------------------- |
135 | 195 |
136 // A ReplyEventHandler is a helper class to send an Apple Event to a process | 196 // A ReplyEventHandler is a helper class to send an Apple Event to a process |
137 // and call a callback when the reply returns. | 197 // and call a callback when the reply returns. |
138 // | 198 // |
139 // This is used to 'ping' the main Chrome process -- once Chrome has sent back | 199 // 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 | 200 // an Apple Event reply, it's guaranteed that it has opened the IPC channel |
141 // that the app shim will connect to. | 201 // that the app shim will connect to. |
142 @interface ReplyEventHandler : NSObject { | 202 @interface ReplyEventHandler : NSObject { |
143 base::Callback<void(bool)> onReply_; | 203 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 | 358 // 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. | 359 // the reply is received, Chrome will have opened its IPC port, guaranteed. |
300 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; | 360 [ReplyEventHandler pingProcess:psn andCall:base::Bind(&OnPingChromeReply)]; |
301 | 361 |
302 base::MessageLoopForUI main_message_loop; | 362 base::MessageLoopForUI main_message_loop; |
303 main_message_loop.set_thread_name("MainThread"); | 363 main_message_loop.set_thread_name("MainThread"); |
304 base::PlatformThread::SetName("CrAppShimMain"); | 364 base::PlatformThread::SetName("CrAppShimMain"); |
305 main_message_loop.Run(); | 365 main_message_loop.Run(); |
306 return 0; | 366 return 0; |
307 } | 367 } |
OLD | NEW |