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