OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "win8/viewer/metro_viewer_process_host.h" | |
6 | |
7 #include <shlobj.h> | |
8 #include <stdint.h> | |
9 | |
10 #include "base/command_line.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/path_service.h" | |
15 #include "base/process/process.h" | |
16 #include "base/strings/string16.h" | |
17 #include "base/synchronization/waitable_event.h" | |
18 #include "base/time/time.h" | |
19 #include "base/win/scoped_comptr.h" | |
20 #include "base/win/windows_version.h" | |
21 #include "ipc/ipc_channel_proxy.h" | |
22 #include "ipc/ipc_message.h" | |
23 #include "ipc/ipc_message_macros.h" | |
24 #include "ui/aura/remote_window_tree_host_win.h" | |
25 #include "ui/metro_viewer/metro_viewer_messages.h" | |
26 #include "win8/viewer/metro_viewer_constants.h" | |
27 | |
28 namespace { | |
29 | |
30 const int kViewerProcessConnectionTimeoutSecs = 60; | |
31 | |
32 } // namespace | |
33 | |
34 namespace win8 { | |
35 | |
36 // static | |
37 MetroViewerProcessHost* MetroViewerProcessHost::instance_ = NULL; | |
38 | |
39 MetroViewerProcessHost::InternalMessageFilter::InternalMessageFilter( | |
40 MetroViewerProcessHost* owner) | |
41 : owner_(owner) { | |
42 } | |
43 | |
44 void MetroViewerProcessHost::InternalMessageFilter::OnChannelConnected( | |
45 int32_t peer_pid) { | |
46 owner_->NotifyChannelConnected(); | |
47 } | |
48 | |
49 MetroViewerProcessHost::InternalMessageFilter::~InternalMessageFilter() { | |
50 } | |
51 | |
52 MetroViewerProcessHost::MetroViewerProcessHost( | |
53 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { | |
54 DCHECK(!instance_); | |
55 instance_ = this; | |
56 | |
57 channel_ = IPC::ChannelProxy::Create(kMetroViewerIPCChannelName, | |
58 IPC::Channel::MODE_NAMED_SERVER, | |
59 this, | |
60 ipc_task_runner); | |
61 } | |
62 | |
63 MetroViewerProcessHost::~MetroViewerProcessHost() { | |
64 if (!channel_) { | |
65 instance_ = NULL; | |
66 return; | |
67 } | |
68 | |
69 base::ProcessId viewer_process_id = GetViewerProcessId(); | |
70 channel_->Close(); | |
71 if (message_filter_.get()) { | |
72 // Wait for the viewer process to go away. | |
73 if (viewer_process_id != base::kNullProcessId) { | |
74 base::Process viewer_process = | |
75 base::Process::OpenWithAccess( | |
76 viewer_process_id, | |
77 PROCESS_QUERY_INFORMATION | SYNCHRONIZE); | |
78 if (viewer_process.IsValid()) { | |
79 int exit_code; | |
80 viewer_process.WaitForExit(&exit_code); | |
81 } | |
82 } | |
83 channel_->RemoveFilter(message_filter_.get()); | |
84 } | |
85 instance_ = NULL; | |
86 } | |
87 | |
88 base::ProcessId MetroViewerProcessHost::GetViewerProcessId() { | |
89 if (channel_) | |
90 return channel_->GetPeerPID(); | |
91 return base::kNullProcessId; | |
92 } | |
93 | |
94 bool MetroViewerProcessHost::LaunchViewerAndWaitForConnection( | |
95 const base::string16& app_user_model_id) { | |
96 DCHECK_EQ(base::kNullProcessId, channel_->GetPeerPID()); | |
97 | |
98 channel_connected_event_.reset(new base::WaitableEvent(false, false)); | |
99 | |
100 message_filter_ = new InternalMessageFilter(this); | |
101 channel_->AddFilter(message_filter_.get()); | |
102 | |
103 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
104 base::win::ScopedComPtr<IApplicationActivationManager> activator; | |
105 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); | |
106 if (SUCCEEDED(hr)) { | |
107 DWORD pid = 0; | |
108 // Use the "connect" verb to | |
109 hr = activator->ActivateApplication( | |
110 app_user_model_id.c_str(), kMetroViewerConnectVerb, AO_NONE, &pid); | |
111 } | |
112 | |
113 LOG_IF(ERROR, FAILED(hr)) << "Tried and failed to launch Metro Chrome. " | |
114 << "hr=" << std::hex << hr; | |
115 } else { | |
116 // For Windows 7 we need to launch the viewer ourselves. | |
117 base::FilePath chrome_path; | |
118 if (!PathService::Get(base::DIR_EXE, &chrome_path)) | |
119 return false; | |
120 // TODO(cpu): launch with "-ServerName:DefaultBrowserServer" | |
121 // note that the viewer might try to launch chrome again. | |
122 CHECK(false); | |
123 } | |
124 | |
125 // Having launched the viewer process, now we wait for it to connect. | |
126 bool success = | |
127 channel_connected_event_->TimedWait(base::TimeDelta::FromSeconds( | |
128 kViewerProcessConnectionTimeoutSecs)); | |
129 channel_connected_event_.reset(); | |
130 return success; | |
131 } | |
132 | |
133 bool MetroViewerProcessHost::Send(IPC::Message* msg) { | |
134 return channel_->Send(msg); | |
135 } | |
136 | |
137 bool MetroViewerProcessHost::OnMessageReceived( | |
138 const IPC::Message& message) { | |
139 DCHECK(CalledOnValidThread()); | |
140 bool handled = true; | |
141 IPC_BEGIN_MESSAGE_MAP(MetroViewerProcessHost, message) | |
142 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileSaveAsDone, | |
143 OnFileSaveAsDone) | |
144 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileOpenDone, | |
145 OnFileOpenDone) | |
146 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MultiFileOpenDone, | |
147 OnMultiFileOpenDone) | |
148 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_OpenURL, OnOpenURL) | |
149 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SearchRequest, OnHandleSearchRequest) | |
150 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SelectFolderDone, | |
151 OnSelectFolderDone) | |
152 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetTargetSurface, OnSetTargetSurface) | |
153 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowSizeChanged, | |
154 OnWindowSizeChanged) | |
155 IPC_MESSAGE_UNHANDLED(handled = false) | |
156 IPC_END_MESSAGE_MAP() | |
157 return handled ? true : | |
158 aura::RemoteWindowTreeHostWin::Instance()->OnMessageReceived(message); | |
159 } | |
160 | |
161 // static | |
162 void MetroViewerProcessHost::HandleActivateDesktop( | |
163 const base::FilePath& path, | |
164 bool ash_exit) { | |
165 if (instance_) { | |
166 instance_->Send( | |
167 new MetroViewerHostMsg_ActivateDesktop(path, ash_exit)); | |
168 } | |
169 } | |
170 | |
171 // static | |
172 void MetroViewerProcessHost::HandleMetroExit() { | |
173 if (instance_) | |
174 instance_->Send(new MetroViewerHostMsg_MetroExit()); | |
175 } | |
176 | |
177 // static | |
178 void MetroViewerProcessHost::HandleOpenFile( | |
179 const base::string16& title, | |
180 const base::FilePath& default_path, | |
181 const base::string16& filter, | |
182 const OpenFileCompletion& on_success, | |
183 const FileSelectionCanceled& on_failure) { | |
184 if (instance_) { | |
185 instance_->HandleOpenFileImpl(title, default_path, filter, on_success, | |
186 on_failure); | |
187 } | |
188 } | |
189 | |
190 // static | |
191 void MetroViewerProcessHost::HandleOpenMultipleFiles( | |
192 const base::string16& title, | |
193 const base::FilePath& default_path, | |
194 const base::string16& filter, | |
195 const OpenMultipleFilesCompletion& on_success, | |
196 const FileSelectionCanceled& on_failure) { | |
197 if (instance_) { | |
198 instance_->HandleOpenMultipleFilesImpl(title, default_path, filter, | |
199 on_success, on_failure); | |
200 } | |
201 } | |
202 | |
203 // static | |
204 void MetroViewerProcessHost::HandleSaveFile( | |
205 const base::string16& title, | |
206 const base::FilePath& default_path, | |
207 const base::string16& filter, | |
208 int filter_index, | |
209 const base::string16& default_extension, | |
210 const SaveFileCompletion& on_success, | |
211 const FileSelectionCanceled& on_failure) { | |
212 if (instance_) { | |
213 instance_->HandleSaveFileImpl(title, default_path, filter, filter_index, | |
214 default_extension, on_success, on_failure); | |
215 } | |
216 } | |
217 | |
218 // static | |
219 void MetroViewerProcessHost::HandleSelectFolder( | |
220 const base::string16& title, | |
221 const SelectFolderCompletion& on_success, | |
222 const FileSelectionCanceled& on_failure) { | |
223 if (instance_) | |
224 instance_->HandleSelectFolderImpl(title, on_success, on_failure); | |
225 } | |
226 | |
227 void MetroViewerProcessHost::HandleOpenFileImpl( | |
228 const base::string16& title, | |
229 const base::FilePath& default_path, | |
230 const base::string16& filter, | |
231 const OpenFileCompletion& on_success, | |
232 const FileSelectionCanceled& on_failure) { | |
233 // Can only have one of these operations in flight. | |
234 DCHECK(file_open_completion_callback_.is_null()); | |
235 DCHECK(failure_callback_.is_null()); | |
236 | |
237 file_open_completion_callback_ = on_success; | |
238 failure_callback_ = on_failure; | |
239 | |
240 Send(new MetroViewerHostMsg_DisplayFileOpen(title, filter, default_path, | |
241 false)); | |
242 } | |
243 | |
244 void MetroViewerProcessHost::HandleOpenMultipleFilesImpl( | |
245 const base::string16& title, | |
246 const base::FilePath& default_path, | |
247 const base::string16& filter, | |
248 const OpenMultipleFilesCompletion& on_success, | |
249 const FileSelectionCanceled& on_failure) { | |
250 // Can only have one of these operations in flight. | |
251 DCHECK(multi_file_open_completion_callback_.is_null()); | |
252 DCHECK(failure_callback_.is_null()); | |
253 multi_file_open_completion_callback_ = on_success; | |
254 failure_callback_ = on_failure; | |
255 | |
256 Send(new MetroViewerHostMsg_DisplayFileOpen(title, filter, default_path, | |
257 true)); | |
258 } | |
259 | |
260 void MetroViewerProcessHost::HandleSaveFileImpl( | |
261 const base::string16& title, | |
262 const base::FilePath& default_path, | |
263 const base::string16& filter, | |
264 int filter_index, | |
265 const base::string16& default_extension, | |
266 const SaveFileCompletion& on_success, | |
267 const FileSelectionCanceled& on_failure) { | |
268 MetroViewerHostMsg_SaveAsDialogParams params; | |
269 params.title = title; | |
270 params.default_extension = default_extension; | |
271 params.filter = filter; | |
272 params.filter_index = filter_index; | |
273 params.suggested_name = default_path; | |
274 | |
275 // Can only have one of these operations in flight. | |
276 DCHECK(file_saveas_completion_callback_.is_null()); | |
277 DCHECK(failure_callback_.is_null()); | |
278 file_saveas_completion_callback_ = on_success; | |
279 failure_callback_ = on_failure; | |
280 | |
281 Send(new MetroViewerHostMsg_DisplayFileSaveAs(params)); | |
282 } | |
283 | |
284 void MetroViewerProcessHost::HandleSelectFolderImpl( | |
285 const base::string16& title, | |
286 const SelectFolderCompletion& on_success, | |
287 const FileSelectionCanceled& on_failure) { | |
288 // Can only have one of these operations in flight. | |
289 DCHECK(select_folder_completion_callback_.is_null()); | |
290 DCHECK(failure_callback_.is_null()); | |
291 select_folder_completion_callback_ = on_success; | |
292 failure_callback_ = on_failure; | |
293 | |
294 Send(new MetroViewerHostMsg_DisplaySelectFolder(title)); | |
295 } | |
296 | |
297 void MetroViewerProcessHost::NotifyChannelConnected() { | |
298 if (channel_connected_event_) | |
299 channel_connected_event_->Signal(); | |
300 } | |
301 | |
302 void MetroViewerProcessHost::OnFileSaveAsDone(bool success, | |
303 const base::FilePath& filename, | |
304 int filter_index) { | |
305 if (success) | |
306 file_saveas_completion_callback_.Run(filename, filter_index, NULL); | |
307 else | |
308 failure_callback_.Run(NULL); | |
309 file_saveas_completion_callback_.Reset(); | |
310 failure_callback_.Reset(); | |
311 } | |
312 | |
313 | |
314 void MetroViewerProcessHost::OnFileOpenDone(bool success, | |
315 const base::FilePath& filename) { | |
316 if (success) | |
317 file_open_completion_callback_.Run(base::FilePath(filename), 0, NULL); | |
318 else | |
319 failure_callback_.Run(NULL); | |
320 file_open_completion_callback_.Reset(); | |
321 failure_callback_.Reset(); | |
322 } | |
323 | |
324 void MetroViewerProcessHost::OnMultiFileOpenDone( | |
325 bool success, | |
326 const std::vector<base::FilePath>& files) { | |
327 if (success) | |
328 multi_file_open_completion_callback_.Run(files, NULL); | |
329 else | |
330 failure_callback_.Run(NULL); | |
331 multi_file_open_completion_callback_.Reset(); | |
332 failure_callback_.Reset(); | |
333 } | |
334 | |
335 void MetroViewerProcessHost::OnSelectFolderDone( | |
336 bool success, | |
337 const base::FilePath& folder) { | |
338 if (success) | |
339 select_folder_completion_callback_.Run(base::FilePath(folder), 0, NULL); | |
340 else | |
341 failure_callback_.Run(NULL); | |
342 select_folder_completion_callback_.Reset(); | |
343 failure_callback_.Reset(); | |
344 } | |
345 | |
346 } // namespace win8 | |
OLD | NEW |