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 "chrome/browser/service_process/service_process_control.h" | 5 #include "chrome/browser/service_process/service_process_control.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 } | 313 } |
314 | 314 |
315 // static | 315 // static |
316 ServiceProcessControl* ServiceProcessControl::GetInstance() { | 316 ServiceProcessControl* ServiceProcessControl::GetInstance() { |
317 return Singleton<ServiceProcessControl>::get(); | 317 return Singleton<ServiceProcessControl>::get(); |
318 } | 318 } |
319 | 319 |
320 ServiceProcessControl::Launcher::Launcher( | 320 ServiceProcessControl::Launcher::Launcher( |
321 ServiceProcessControl* process, | 321 ServiceProcessControl* process, |
322 scoped_ptr<base::CommandLine> cmd_line) | 322 scoped_ptr<base::CommandLine> cmd_line) |
323 : process_(process), | 323 : process_control_(process), |
324 cmd_line_(cmd_line.Pass()), | 324 cmd_line_(cmd_line.Pass()), |
325 launched_(false), | 325 launched_(false), |
326 retry_count_(0), | 326 retry_count_(0) { |
327 process_handle_(base::kNullProcessHandle) { | |
328 } | 327 } |
329 | 328 |
330 // Execute the command line to start the process asynchronously. | 329 // Execute the command line to start the process asynchronously. |
331 // After the command is executed, |task| is called with the process handle on | 330 // After the command is executed, |task| is called with the process handle on |
332 // the UI thread. | 331 // the UI thread. |
333 void ServiceProcessControl::Launcher::Run(const base::Closure& task) { | 332 void ServiceProcessControl::Launcher::Run(const base::Closure& task) { |
334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
335 notify_task_ = task; | 334 notify_task_ = task; |
336 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 335 BrowserThread::PostTask(BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
337 base::Bind(&Launcher::DoRun, this)); | 336 base::Bind(&Launcher::DoRun, this)); |
338 } | 337 } |
339 | 338 |
340 ServiceProcessControl::Launcher::~Launcher() { | 339 ServiceProcessControl::Launcher::~Launcher() { |
341 CloseProcessHandle(); | |
342 } | 340 } |
343 | 341 |
344 | 342 |
345 void ServiceProcessControl::Launcher::Notify() { | 343 void ServiceProcessControl::Launcher::Notify() { |
346 DCHECK(!notify_task_.is_null()); | 344 DCHECK(!notify_task_.is_null()); |
347 notify_task_.Run(); | 345 notify_task_.Run(); |
348 notify_task_.Reset(); | 346 notify_task_.Reset(); |
349 } | 347 } |
350 | 348 |
351 void ServiceProcessControl::Launcher::CloseProcessHandle() { | |
352 if (process_handle_ != base::kNullProcessHandle) { | |
353 base::CloseProcessHandle(process_handle_); | |
354 process_handle_ = base::kNullProcessHandle; | |
355 } | |
356 } | |
357 | |
358 #if !defined(OS_MACOSX) | 349 #if !defined(OS_MACOSX) |
359 void ServiceProcessControl::Launcher::DoDetectLaunched() { | 350 void ServiceProcessControl::Launcher::DoDetectLaunched() { |
360 DCHECK(!notify_task_.is_null()); | 351 DCHECK(!notify_task_.is_null()); |
361 | 352 |
362 const uint32 kMaxLaunchDetectRetries = 10; | 353 const uint32 kMaxLaunchDetectRetries = 10; |
363 launched_ = CheckServiceProcessReady(); | 354 launched_ = CheckServiceProcessReady(); |
364 | 355 |
365 int exit_code = 0; | 356 int exit_code = 0; |
366 if (launched_ || (retry_count_ >= kMaxLaunchDetectRetries) || | 357 if (launched_ || (retry_count_ >= kMaxLaunchDetectRetries) || |
367 base::WaitForExitCodeWithTimeout(process_handle_, &exit_code, | 358 process_.WaitForExitWithTimeout(base::TimeDelta(), &exit_code)) { |
368 base::TimeDelta())) { | 359 process_.Close(); |
369 CloseProcessHandle(); | |
370 BrowserThread::PostTask( | 360 BrowserThread::PostTask( |
371 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this)); | 361 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this)); |
372 return; | 362 return; |
373 } | 363 } |
374 retry_count_++; | 364 retry_count_++; |
375 | 365 |
376 // If the service process is not launched yet then check again in 2 seconds. | 366 // If the service process is not launched yet then check again in 2 seconds. |
377 const base::TimeDelta kDetectLaunchRetry = base::TimeDelta::FromSeconds(2); | 367 const base::TimeDelta kDetectLaunchRetry = base::TimeDelta::FromSeconds(2); |
378 base::MessageLoop::current()->PostDelayedTask( | 368 base::MessageLoop::current()->PostDelayedTask( |
379 FROM_HERE, base::Bind(&Launcher::DoDetectLaunched, this), | 369 FROM_HERE, base::Bind(&Launcher::DoDetectLaunched, this), |
380 kDetectLaunchRetry); | 370 kDetectLaunchRetry); |
381 } | 371 } |
382 | 372 |
383 void ServiceProcessControl::Launcher::DoRun() { | 373 void ServiceProcessControl::Launcher::DoRun() { |
384 DCHECK(!notify_task_.is_null()); | 374 DCHECK(!notify_task_.is_null()); |
385 | 375 |
386 base::LaunchOptions options; | 376 base::LaunchOptions options; |
387 #if defined(OS_WIN) | 377 #if defined(OS_WIN) |
388 options.start_hidden = true; | 378 options.start_hidden = true; |
389 #endif | 379 #endif |
390 if (base::LaunchProcess(*cmd_line_, options, &process_handle_)) { | 380 process_ = base::LaunchProcess(*cmd_line_, options); |
| 381 if (process_.IsValid()) { |
391 BrowserThread::PostTask( | 382 BrowserThread::PostTask( |
392 BrowserThread::IO, FROM_HERE, | 383 BrowserThread::IO, FROM_HERE, |
393 base::Bind(&Launcher::DoDetectLaunched, this)); | 384 base::Bind(&Launcher::DoDetectLaunched, this)); |
394 } else { | 385 } else { |
395 BrowserThread::PostTask( | 386 BrowserThread::PostTask( |
396 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this)); | 387 BrowserThread::UI, FROM_HERE, base::Bind(&Launcher::Notify, this)); |
397 } | 388 } |
398 } | 389 } |
399 #endif // !OS_MACOSX | 390 #endif // !OS_MACOSX |
OLD | NEW |