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 #include "cloud_print/service/win/chrome_launcher.h" | 5 #include "cloud_print/service/win/chrome_launcher.h" |
6 | 6 |
7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/process/kill.h" | 13 #include "base/process/process.h" |
14 #include "base/process/process.h" | 14 #include "base/process/process.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "base/win/registry.h" | 16 #include "base/win/registry.h" |
17 #include "base/win/scoped_handle.h" | 17 #include "base/win/scoped_handle.h" |
18 #include "base/win/scoped_process_information.h" | 18 #include "base/win/scoped_process_information.h" |
19 #include "chrome/common/chrome_constants.h" | 19 #include "chrome/common/chrome_constants.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
22 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | 22 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
23 #include "cloud_print/common/win/cloud_print_utils.h" | 23 #include "cloud_print/common/win/cloud_print_utils.h" |
24 #include "cloud_print/service/service_constants.h" | 24 #include "cloud_print/service/service_constants.h" |
25 #include "cloud_print/service/win/service_utils.h" | 25 #include "cloud_print/service/win/service_utils.h" |
26 #include "components/cloud_devices/common/cloud_devices_urls.h" | 26 #include "components/cloud_devices/common/cloud_devices_urls.h" |
27 #include "google_apis/gaia/gaia_urls.h" | 27 #include "google_apis/gaia/gaia_urls.h" |
28 #include "net/base/url_util.h" | 28 #include "net/base/url_util.h" |
29 #include "url/gurl.h" | 29 #include "url/gurl.h" |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 const int kShutdownTimeoutMs = 30 * 1000; | 33 const int kShutdownTimeoutMs = 30 * 1000; |
34 const int kUsageUpdateTimeoutMs = 6 * 3600 * 1000; // 6 hours. | 34 const int kUsageUpdateTimeoutMs = 6 * 3600 * 1000; // 6 hours. |
35 | 35 |
36 static const base::char16 kAutoRunKeyPath[] = | 36 static const base::char16 kAutoRunKeyPath[] = |
37 L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; | 37 L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; |
38 | 38 |
39 // Terminates any process. | 39 // Terminates any process. |
40 void ShutdownChrome(HANDLE process, DWORD thread_id) { | 40 void ShutdownChrome(base::Process process, DWORD thread_id) { |
41 if (::PostThreadMessage(thread_id, WM_QUIT, 0, 0) && | 41 if (::PostThreadMessage(thread_id, WM_QUIT, 0, 0) && |
42 WAIT_OBJECT_0 == ::WaitForSingleObject(process, kShutdownTimeoutMs)) { | 42 WAIT_OBJECT_0 == ::WaitForSingleObject(process.Handle(), |
| 43 kShutdownTimeoutMs)) { |
43 return; | 44 return; |
44 } | 45 } |
45 LOG(ERROR) << "Failed to shutdown process."; | 46 LOG(ERROR) << "Failed to shutdown process."; |
46 base::KillProcess(process, 0, true); | 47 process.Terminate(0, true); |
47 } | 48 } |
48 | 49 |
49 BOOL CALLBACK CloseIfPidEqual(HWND wnd, LPARAM lparam) { | 50 BOOL CALLBACK CloseIfPidEqual(HWND wnd, LPARAM lparam) { |
50 DWORD pid = 0; | 51 DWORD pid = 0; |
51 ::GetWindowThreadProcessId(wnd, &pid); | 52 ::GetWindowThreadProcessId(wnd, &pid); |
52 if (pid == static_cast<DWORD>(lparam)) | 53 if (pid == static_cast<DWORD>(lparam)) |
53 ::PostMessage(wnd, WM_CLOSE, 0, 0); | 54 ::PostMessage(wnd, WM_CLOSE, 0, 0); |
54 return TRUE; | 55 return TRUE; |
55 } | 56 } |
56 | 57 |
57 void CloseAllProcessWindows(HANDLE process) { | 58 void CloseAllProcessWindows(HANDLE process) { |
58 ::EnumWindows(&CloseIfPidEqual, GetProcessId(process)); | 59 ::EnumWindows(&CloseIfPidEqual, GetProcessId(process)); |
59 } | 60 } |
60 | 61 |
61 // Close Chrome browser window. | 62 // Close Chrome browser window. |
62 void CloseChrome(HANDLE process, DWORD thread_id) { | 63 void CloseChrome(base::Process process, DWORD thread_id) { |
63 CloseAllProcessWindows(process); | 64 CloseAllProcessWindows(process.Handle()); |
64 if (WAIT_OBJECT_0 == ::WaitForSingleObject(process, kShutdownTimeoutMs)) { | 65 if (WAIT_OBJECT_0 == |
| 66 ::WaitForSingleObject(process.Handle(), kShutdownTimeoutMs)) { |
65 return; | 67 return; |
66 } | 68 } |
67 ShutdownChrome(process, thread_id); | 69 ShutdownChrome(process.Pass(), thread_id); |
68 } | 70 } |
69 | 71 |
70 bool LaunchProcess(const base::CommandLine& cmdline, | 72 bool LaunchProcess(const base::CommandLine& cmdline, |
71 base::win::ScopedHandle* process_handle, | 73 base::win::ScopedHandle* process_handle, |
72 DWORD* thread_id) { | 74 DWORD* thread_id) { |
73 STARTUPINFO startup_info = {}; | 75 STARTUPINFO startup_info = {}; |
74 startup_info.cb = sizeof(startup_info); | 76 startup_info.cb = sizeof(startup_info); |
75 startup_info.dwFlags = STARTF_USESHOWWINDOW; | 77 startup_info.dwFlags = STARTF_USESHOWWINDOW; |
76 startup_info.wShowWindow = SW_SHOW; | 78 startup_info.wShowWindow = SW_SHOW; |
77 | 79 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 cmd.AppendSwitch(switches::kDisableGpu); | 219 cmd.AppendSwitch(switches::kDisableGpu); |
218 cmd.AppendSwitch(switches::kDisableSoftwareRasterizer); | 220 cmd.AppendSwitch(switches::kDisableSoftwareRasterizer); |
219 cmd.AppendSwitch(switches::kDisableSync); | 221 cmd.AppendSwitch(switches::kDisableSync); |
220 cmd.AppendSwitch(switches::kNoFirstRun); | 222 cmd.AppendSwitch(switches::kNoFirstRun); |
221 cmd.AppendSwitch(switches::kNoStartupWindow); | 223 cmd.AppendSwitch(switches::kNoStartupWindow); |
222 | 224 |
223 base::win::ScopedHandle chrome_handle; | 225 base::win::ScopedHandle chrome_handle; |
224 base::Time started = base::Time::Now(); | 226 base::Time started = base::Time::Now(); |
225 DWORD thread_id = 0; | 227 DWORD thread_id = 0; |
226 LaunchProcess(cmd, &chrome_handle, &thread_id); | 228 LaunchProcess(cmd, &chrome_handle, &thread_id); |
| 229 base::Process chrome_process; |
| 230 if (chrome_handle.IsValid()) |
| 231 chrome_process = base::Process(chrome_handle.Take()); |
227 | 232 |
228 HANDLE handles[] = { stop_event_.handle(), chrome_handle.Get() }; | 233 HANDLE handles[] = { stop_event_.handle(), chrome_process.Handle() }; |
229 DWORD wait_result = WAIT_TIMEOUT; | 234 DWORD wait_result = WAIT_TIMEOUT; |
230 while (wait_result == WAIT_TIMEOUT) { | 235 while (wait_result == WAIT_TIMEOUT) { |
231 cloud_print::SetGoogleUpdateUsage(kGoogleUpdateId); | 236 cloud_print::SetGoogleUpdateUsage(kGoogleUpdateId); |
232 wait_result = ::WaitForMultipleObjects(arraysize(handles), handles, | 237 wait_result = ::WaitForMultipleObjects(arraysize(handles), handles, |
233 FALSE, kUsageUpdateTimeoutMs); | 238 FALSE, kUsageUpdateTimeoutMs); |
234 } | 239 } |
235 if (wait_result == WAIT_OBJECT_0) { | 240 if (wait_result == WAIT_OBJECT_0) { |
236 ShutdownChrome(chrome_handle.Get(), thread_id); | 241 ShutdownChrome(chrome_process.Pass(), thread_id); |
237 break; | 242 break; |
238 } else if (wait_result == WAIT_OBJECT_0 + 1) { | 243 } else if (wait_result == WAIT_OBJECT_0 + 1) { |
239 LOG(ERROR) << "Chrome process exited."; | 244 LOG(ERROR) << "Chrome process exited."; |
240 } else { | 245 } else { |
241 LOG(ERROR) << "Error waiting Chrome (" << ::GetLastError() << ")."; | 246 LOG(ERROR) << "Error waiting Chrome (" << ::GetLastError() << ")."; |
242 } | 247 } |
243 if (base::Time::Now() - started > base::TimeDelta::FromHours(1)) { | 248 if (base::Time::Now() - started > base::TimeDelta::FromHours(1)) { |
244 // Reset timeout because process worked long enough. | 249 // Reset timeout because process worked long enough. |
245 time_out = default_time_out; | 250 time_out = default_time_out; |
246 } | 251 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 | 300 |
296 cmd.AppendArg( | 301 cmd.AppendArg( |
297 cloud_devices::GetCloudPrintEnableWithSigninURL(proxy_id).spec()); | 302 cloud_devices::GetCloudPrintEnableWithSigninURL(proxy_id).spec()); |
298 | 303 |
299 base::win::ScopedHandle chrome_handle; | 304 base::win::ScopedHandle chrome_handle; |
300 DWORD thread_id = 0; | 305 DWORD thread_id = 0; |
301 if (!LaunchProcess(cmd, &chrome_handle, &thread_id)) { | 306 if (!LaunchProcess(cmd, &chrome_handle, &thread_id)) { |
302 LOG(ERROR) << "Unable to launch Chrome."; | 307 LOG(ERROR) << "Unable to launch Chrome."; |
303 return std::string(); | 308 return std::string(); |
304 } | 309 } |
| 310 base::Process chrome_process(chrome_handle.Take()); |
305 | 311 |
306 for (;;) { | 312 for (;;) { |
307 DWORD wait_result = ::WaitForSingleObject(chrome_handle.Get(), 500); | 313 DWORD wait_result = ::WaitForSingleObject(chrome_process.Handle(), 500); |
308 std::string json = ReadAndUpdateServiceState(temp_user_data.path(), | 314 std::string json = ReadAndUpdateServiceState(temp_user_data.path(), |
309 proxy_id); | 315 proxy_id); |
310 if (wait_result == WAIT_OBJECT_0) { | 316 if (wait_result == WAIT_OBJECT_0) { |
311 // Return what we have because browser is closed. | 317 // Return what we have because browser is closed. |
312 return json; | 318 return json; |
313 } | 319 } |
314 if (wait_result != WAIT_TIMEOUT) { | 320 if (wait_result != WAIT_TIMEOUT) { |
315 LOG(ERROR) << "Chrome launch failed."; | 321 LOG(ERROR) << "Chrome launch failed."; |
316 return std::string(); | 322 return std::string(); |
317 } | 323 } |
318 if (!json.empty()) { | 324 if (!json.empty()) { |
319 // Close chrome because Service State is ready. | 325 // Close chrome because Service State is ready. |
320 CloseChrome(chrome_handle.Get(), thread_id); | 326 CloseChrome(chrome_process.Pass(), thread_id); |
321 return json; | 327 return json; |
322 } | 328 } |
323 } | 329 } |
324 } | 330 } |
OLD | NEW |