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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 } | 264 } |
265 } | 265 } |
266 if (stop_event_.TimedWait(time_out)) | 266 if (stop_event_.TimedWait(time_out)) |
267 break; | 267 break; |
268 } | 268 } |
269 } | 269 } |
270 | 270 |
271 std::string ChromeLauncher::CreateServiceStateFile( | 271 std::string ChromeLauncher::CreateServiceStateFile( |
272 const std::string& proxy_id, | 272 const std::string& proxy_id, |
273 const std::vector<std::string>& printers) { | 273 const std::vector<std::string>& printers) { |
274 std::string result; | |
275 | |
276 base::ScopedTempDir temp_user_data; | 274 base::ScopedTempDir temp_user_data; |
277 if (!temp_user_data.CreateUniqueTempDir()) { | 275 if (!temp_user_data.CreateUniqueTempDir()) { |
278 LOG(ERROR) << "Can't create temp dir."; | 276 LOG(ERROR) << "Can't create temp dir."; |
279 return result; | 277 return std::string(); |
280 } | 278 } |
281 | 279 |
282 base::FilePath chrome_path = chrome_launcher_support::GetAnyChromePath(); | 280 base::FilePath chrome_path = chrome_launcher_support::GetAnyChromePath(); |
283 | |
284 if (chrome_path.empty()) { | 281 if (chrome_path.empty()) { |
285 LOG(ERROR) << "Can't find Chrome."; | 282 LOG(ERROR) << "Can't find Chrome."; |
286 return result; | 283 return std::string(); |
287 } | 284 } |
288 | 285 |
289 base::FilePath printers_file = temp_user_data.path().Append(L"printers.json"); | 286 base::FilePath printers_file = temp_user_data.path().Append(L"printers.json"); |
290 | 287 |
291 base::ListValue printer_list; | 288 base::ListValue printer_list; |
292 printer_list.AppendStrings(printers); | 289 printer_list.AppendStrings(printers); |
293 std::string printers_json; | 290 std::string printers_json; |
294 base::JSONWriter::Write(&printer_list, &printers_json); | 291 base::JSONWriter::Write(&printer_list, &printers_json); |
295 size_t written = base::WriteFile(printers_file, | 292 size_t written = base::WriteFile(printers_file, |
296 printers_json.c_str(), | 293 printers_json.c_str(), |
297 printers_json.size()); | 294 printers_json.size()); |
298 if (written != printers_json.size()) { | 295 if (written != printers_json.size()) { |
299 LOG(ERROR) << "Can't write file."; | 296 LOG(ERROR) << "Can't write file."; |
300 return result; | 297 return std::string(); |
301 } | 298 } |
302 | 299 |
303 CommandLine cmd(chrome_path); | 300 CommandLine cmd(chrome_path); |
304 CopyChromeSwitchesFromCurrentProcess(&cmd); | 301 CopyChromeSwitchesFromCurrentProcess(&cmd); |
305 cmd.AppendSwitchPath(switches::kUserDataDir, temp_user_data.path()); | 302 cmd.AppendSwitchPath(switches::kUserDataDir, temp_user_data.path()); |
306 cmd.AppendSwitchPath(switches::kCloudPrintSetupProxy, printers_file); | 303 cmd.AppendSwitchPath(switches::kCloudPrintSetupProxy, printers_file); |
307 cmd.AppendSwitch(switches::kNoServiceAutorun); | 304 cmd.AppendSwitch(switches::kNoServiceAutorun); |
308 | 305 |
309 // Optional. | 306 // Optional. |
310 cmd.AppendSwitch(switches::kDisableBackgroundMode); | 307 cmd.AppendSwitch(switches::kDisableBackgroundMode); |
311 cmd.AppendSwitch(switches::kDisableDefaultApps); | 308 cmd.AppendSwitch(switches::kDisableDefaultApps); |
312 cmd.AppendSwitch(switches::kDisableExtensions); | 309 cmd.AppendSwitch(switches::kDisableExtensions); |
313 cmd.AppendSwitch(switches::kDisableSync); | 310 cmd.AppendSwitch(switches::kDisableSync); |
314 cmd.AppendSwitch(switches::kNoDefaultBrowserCheck); | 311 cmd.AppendSwitch(switches::kNoDefaultBrowserCheck); |
315 cmd.AppendSwitch(switches::kNoFirstRun); | 312 cmd.AppendSwitch(switches::kNoFirstRun); |
316 | 313 |
317 cmd.AppendArg(GetCloudPrintServiceEnableURLWithSignin(proxy_id).spec()); | 314 cmd.AppendArg(GetCloudPrintServiceEnableURLWithSignin(proxy_id).spec()); |
318 | 315 |
319 base::win::ScopedHandle chrome_handle; | 316 base::win::ScopedHandle chrome_handle; |
320 DWORD thread_id = 0; | 317 DWORD thread_id = 0; |
321 if (!LaunchProcess(cmd, &chrome_handle, &thread_id)) { | 318 if (!LaunchProcess(cmd, &chrome_handle, &thread_id)) { |
322 LOG(ERROR) << "Unable to launch Chrome."; | 319 LOG(ERROR) << "Unable to launch Chrome."; |
323 return result; | 320 return std::string(); |
324 } | 321 } |
325 | 322 |
326 for (;;) { | 323 for (;;) { |
327 DWORD wait_result = ::WaitForSingleObject(chrome_handle, 500); | 324 DWORD wait_result = ::WaitForSingleObject(chrome_handle, 500); |
328 std::string json = ReadAndUpdateServiceState(temp_user_data.path(), | 325 std::string json = ReadAndUpdateServiceState(temp_user_data.path(), |
329 proxy_id); | 326 proxy_id); |
330 if (wait_result == WAIT_OBJECT_0) { | 327 if (wait_result == WAIT_OBJECT_0) { |
331 // Return what we have because browser is closed. | 328 // Return what we have because browser is closed. |
332 return json; | 329 return json; |
333 } else if (wait_result == WAIT_TIMEOUT) { | 330 } |
334 if (!json.empty()) { | 331 if (wait_result != WAIT_TIMEOUT) { |
335 // Close chrome because Service State is ready. | |
336 CloseChrome(chrome_handle, thread_id); | |
337 return json; | |
338 } | |
339 } else { | |
340 LOG(ERROR) << "Chrome launch failed."; | 332 LOG(ERROR) << "Chrome launch failed."; |
341 return result; | 333 return std::string(); |
| 334 } |
| 335 if (!json.empty()) { |
| 336 // Close chrome because Service State is ready. |
| 337 CloseChrome(chrome_handle, thread_id); |
| 338 return json; |
342 } | 339 } |
343 } | 340 } |
344 NOTREACHED(); | |
345 return std::string(); | |
346 } | 341 } |
347 | 342 |
OLD | NEW |