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/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // Close Chrome browser window. | 60 // Close Chrome browser window. |
61 void CloseChrome(HANDLE process, DWORD thread_id) { | 61 void CloseChrome(HANDLE process, DWORD thread_id) { |
62 CloseAllProcessWindows(process); | 62 CloseAllProcessWindows(process); |
63 if (WAIT_OBJECT_0 == ::WaitForSingleObject(process, kShutdownTimeoutMs)) { | 63 if (WAIT_OBJECT_0 == ::WaitForSingleObject(process, kShutdownTimeoutMs)) { |
64 return; | 64 return; |
65 } | 65 } |
66 ShutdownChrome(process, thread_id); | 66 ShutdownChrome(process, thread_id); |
67 } | 67 } |
68 | 68 |
69 bool LaunchProcess(const CommandLine& cmdline, | 69 bool LaunchProcess(const CommandLine& cmdline, |
70 base::win::ScopedHandle* process_handle, | 70 base::ProcessHandle* process_handle, |
71 DWORD* thread_id) { | 71 DWORD* thread_id) { |
72 STARTUPINFO startup_info = {}; | 72 STARTUPINFO startup_info = {}; |
73 startup_info.cb = sizeof(startup_info); | 73 startup_info.cb = sizeof(startup_info); |
74 startup_info.dwFlags = STARTF_USESHOWWINDOW; | 74 startup_info.dwFlags = STARTF_USESHOWWINDOW; |
75 startup_info.wShowWindow = SW_SHOW; | 75 startup_info.wShowWindow = SW_SHOW; |
76 | 76 |
77 PROCESS_INFORMATION temp_process_info = {}; | 77 base::win::ScopedProcessInformation process_info; |
78 if (!CreateProcess(NULL, | 78 if (!CreateProcess(NULL, |
79 const_cast<wchar_t*>(cmdline.GetCommandLineString().c_str()), NULL, NULL, | 79 const_cast<wchar_t*>(cmdline.GetCommandLineString().c_str()), NULL, NULL, |
80 FALSE, 0, NULL, NULL, &startup_info, &temp_process_info)) { | 80 FALSE, 0, NULL, NULL, &startup_info, process_info.Receive())) { |
81 return false; | 81 return false; |
82 } | 82 } |
83 base::win::ScopedProcessInformation process_info(temp_process_info); | |
84 | 83 |
85 if (process_handle) | 84 if (process_handle) |
86 process_handle->Set(process_info.TakeProcessHandle()); | 85 *process_handle = process_info.TakeProcessHandle(); |
87 | 86 |
88 if (thread_id) | 87 if (thread_id) |
89 *thread_id = process_info.thread_id(); | 88 *thread_id = process_info.thread_id(); |
90 | 89 |
91 return true; | 90 return true; |
92 } | 91 } |
93 | 92 |
94 GURL GetCloudPrintServiceEnableURL(const std::string& proxy_id) { | 93 GURL GetCloudPrintServiceEnableURL(const std::string& proxy_id) { |
95 GURL url( | 94 GURL url( |
96 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 95 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 cmd.AppendSwitch(switches::kDisableExtensions); | 233 cmd.AppendSwitch(switches::kDisableExtensions); |
235 cmd.AppendSwitch(switches::kDisableGpu); | 234 cmd.AppendSwitch(switches::kDisableGpu); |
236 cmd.AppendSwitch(switches::kDisableSoftwareRasterizer); | 235 cmd.AppendSwitch(switches::kDisableSoftwareRasterizer); |
237 cmd.AppendSwitch(switches::kDisableSync); | 236 cmd.AppendSwitch(switches::kDisableSync); |
238 cmd.AppendSwitch(switches::kNoFirstRun); | 237 cmd.AppendSwitch(switches::kNoFirstRun); |
239 cmd.AppendSwitch(switches::kNoStartupWindow); | 238 cmd.AppendSwitch(switches::kNoStartupWindow); |
240 | 239 |
241 base::win::ScopedHandle chrome_handle; | 240 base::win::ScopedHandle chrome_handle; |
242 base::Time started = base::Time::Now(); | 241 base::Time started = base::Time::Now(); |
243 DWORD thread_id = 0; | 242 DWORD thread_id = 0; |
244 LaunchProcess(cmd, &chrome_handle, &thread_id); | 243 LaunchProcess(cmd, chrome_handle.Receive(), &thread_id); |
245 | 244 |
246 HANDLE handles[] = {stop_event_.handle(), chrome_handle}; | 245 HANDLE handles[] = {stop_event_.handle(), chrome_handle}; |
247 DWORD wait_result = WAIT_TIMEOUT; | 246 DWORD wait_result = WAIT_TIMEOUT; |
248 while (wait_result == WAIT_TIMEOUT) { | 247 while (wait_result == WAIT_TIMEOUT) { |
249 cloud_print::SetGoogleUpdateUsage(kGoogleUpdateId); | 248 cloud_print::SetGoogleUpdateUsage(kGoogleUpdateId); |
250 wait_result = ::WaitForMultipleObjects(arraysize(handles), handles, | 249 wait_result = ::WaitForMultipleObjects(arraysize(handles), handles, |
251 FALSE, kUsageUpdateTimeoutMs); | 250 FALSE, kUsageUpdateTimeoutMs); |
252 } | 251 } |
253 if (wait_result == WAIT_OBJECT_0) { | 252 if (wait_result == WAIT_OBJECT_0) { |
254 ShutdownChrome(chrome_handle, thread_id); | 253 ShutdownChrome(chrome_handle, thread_id); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 cmd.AppendSwitch(switches::kDisableDefaultApps); | 310 cmd.AppendSwitch(switches::kDisableDefaultApps); |
312 cmd.AppendSwitch(switches::kDisableExtensions); | 311 cmd.AppendSwitch(switches::kDisableExtensions); |
313 cmd.AppendSwitch(switches::kDisableSync); | 312 cmd.AppendSwitch(switches::kDisableSync); |
314 cmd.AppendSwitch(switches::kNoDefaultBrowserCheck); | 313 cmd.AppendSwitch(switches::kNoDefaultBrowserCheck); |
315 cmd.AppendSwitch(switches::kNoFirstRun); | 314 cmd.AppendSwitch(switches::kNoFirstRun); |
316 | 315 |
317 cmd.AppendArg(GetCloudPrintServiceEnableURLWithSignin(proxy_id).spec()); | 316 cmd.AppendArg(GetCloudPrintServiceEnableURLWithSignin(proxy_id).spec()); |
318 | 317 |
319 base::win::ScopedHandle chrome_handle; | 318 base::win::ScopedHandle chrome_handle; |
320 DWORD thread_id = 0; | 319 DWORD thread_id = 0; |
321 if (!LaunchProcess(cmd, &chrome_handle, &thread_id)) { | 320 if (!LaunchProcess(cmd, chrome_handle.Receive(), &thread_id)) { |
322 LOG(ERROR) << "Unable to launch Chrome."; | 321 LOG(ERROR) << "Unable to launch Chrome."; |
323 return result; | 322 return result; |
324 } | 323 } |
325 | 324 |
326 for (;;) { | 325 for (;;) { |
327 DWORD wait_result = ::WaitForSingleObject(chrome_handle, 500); | 326 DWORD wait_result = ::WaitForSingleObject(chrome_handle, 500); |
328 std::string json = ReadAndUpdateServiceState(temp_user_data.path(), | 327 std::string json = ReadAndUpdateServiceState(temp_user_data.path(), |
329 proxy_id); | 328 proxy_id); |
330 if (wait_result == WAIT_OBJECT_0) { | 329 if (wait_result == WAIT_OBJECT_0) { |
331 // Return what we have because browser is closed. | 330 // Return what we have because browser is closed. |
332 return json; | 331 return json; |
333 } else if (wait_result == WAIT_TIMEOUT) { | 332 } else if (wait_result == WAIT_TIMEOUT) { |
334 if (!json.empty()) { | 333 if (!json.empty()) { |
335 // Close chrome because Service State is ready. | 334 // Close chrome because Service State is ready. |
336 CloseChrome(chrome_handle, thread_id); | 335 CloseChrome(chrome_handle, thread_id); |
337 return json; | 336 return json; |
338 } | 337 } |
339 } else { | 338 } else { |
340 LOG(ERROR) << "Chrome launch failed."; | 339 LOG(ERROR) << "Chrome launch failed."; |
341 return result; | 340 return result; |
342 } | 341 } |
343 } | 342 } |
344 NOTREACHED(); | 343 NOTREACHED(); |
345 return std::string(); | 344 return std::string(); |
346 } | 345 } |
347 | 346 |
OLD | NEW |