| 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/browser_process_impl.h" | 5 #include "chrome/browser/browser_process_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/atomic_ref_count.h" | |
| 12 #include "base/bind.h" | 11 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 14 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 15 #include "base/debug/alias.h" | 14 #include "base/debug/alias.h" |
| 16 #include "base/debug/leak_annotations.h" | 15 #include "base/debug/leak_annotations.h" |
| 17 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 18 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 19 #include "base/prefs/json_pref_store.h" | 18 #include "base/prefs/json_pref_store.h" |
| 20 #include "base/prefs/pref_registry_simple.h" | 19 #include "base/prefs/pref_registry_simple.h" |
| 21 #include "base/prefs/pref_service.h" | 20 #include "base/prefs/pref_service.h" |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 // Reset associated state right after actual thread is stopped, | 308 // Reset associated state right after actual thread is stopped, |
| 310 // as io_thread_.global_ cleanup happens in CleanUp on the IO | 309 // as io_thread_.global_ cleanup happens in CleanUp on the IO |
| 311 // thread, i.e. as the thread exits its message loop. | 310 // thread, i.e. as the thread exits its message loop. |
| 312 // | 311 // |
| 313 // This is important also because in various places, the | 312 // This is important also because in various places, the |
| 314 // IOThread object being NULL is considered synonymous with the | 313 // IOThread object being NULL is considered synonymous with the |
| 315 // IO thread having stopped. | 314 // IO thread having stopped. |
| 316 io_thread_.reset(); | 315 io_thread_.reset(); |
| 317 } | 316 } |
| 318 | 317 |
| 318 #if defined(USE_X11) || defined(OS_WIN) |
| 319 static void Signal(base::WaitableEvent* event) { |
| 320 event->Signal(); |
| 321 } |
| 322 #endif |
| 323 |
| 319 unsigned int BrowserProcessImpl::AddRefModule() { | 324 unsigned int BrowserProcessImpl::AddRefModule() { |
| 320 DCHECK(CalledOnValidThread()); | 325 DCHECK(CalledOnValidThread()); |
| 321 | 326 |
| 322 // CHECK(!IsShuttingDown()); | 327 // CHECK(!IsShuttingDown()); |
| 323 if (IsShuttingDown()) { | 328 if (IsShuttingDown()) { |
| 324 // Copy the stacktrace which released the final reference onto our stack so | 329 // Copy the stacktrace which released the final reference onto our stack so |
| 325 // it will be available in the crash report for inspection. | 330 // it will be available in the crash report for inspection. |
| 326 base::debug::StackTrace callstack = release_last_reference_callstack_; | 331 base::debug::StackTrace callstack = release_last_reference_callstack_; |
| 327 base::debug::Alias(&callstack); | 332 base::debug::Alias(&callstack); |
| 328 CHECK(false); | 333 CHECK(false); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 #if defined(OS_MACOSX) | 377 #if defined(OS_MACOSX) |
| 373 base::MessageLoop::current()->PostTask( | 378 base::MessageLoop::current()->PostTask( |
| 374 FROM_HERE, | 379 FROM_HERE, |
| 375 base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop)); | 380 base::Bind(ChromeBrowserMainPartsMac::DidEndMainMessageLoop)); |
| 376 #endif | 381 #endif |
| 377 base::MessageLoop::current()->Quit(); | 382 base::MessageLoop::current()->Quit(); |
| 378 } | 383 } |
| 379 return module_ref_count_; | 384 return module_ref_count_; |
| 380 } | 385 } |
| 381 | 386 |
| 382 namespace { | |
| 383 | |
| 384 // Used at the end of session to block the UI thread for completion of sentinel | |
| 385 // tasks on the set of threads used to persist profile data and local state. | |
| 386 // This is done to ensure that the data has been persisted to disk before | |
| 387 // continuing. | |
| 388 class RundownTaskCounter : | |
| 389 public base::RefCountedThreadSafe<RundownTaskCounter> { | |
| 390 public: | |
| 391 RundownTaskCounter(); | |
| 392 | |
| 393 // Posts a rundown task to |task_runner|, can be invoked an arbitrary number | |
| 394 // of times before calling TimedWait. | |
| 395 void Post(base::SequencedTaskRunner* task_runner); | |
| 396 | |
| 397 // Waits until the count is zero or |max_time| has passed. | |
| 398 // This can only be called once per instance. | |
| 399 bool TimedWait(const base::TimeDelta& max_time); | |
| 400 | |
| 401 private: | |
| 402 friend class base::RefCountedThreadSafe<RundownTaskCounter>; | |
| 403 ~RundownTaskCounter() {} | |
| 404 | |
| 405 // Decrements the counter and releases the waitable event on transition to | |
| 406 // zero. | |
| 407 void Decrement(); | |
| 408 | |
| 409 // The count starts at one to defer the possibility of one->zero transitions | |
| 410 // until TimedWait is called. | |
| 411 base::AtomicRefCount count_; | |
| 412 base::WaitableEvent waitable_event_; | |
| 413 | |
| 414 DISALLOW_COPY_AND_ASSIGN(RundownTaskCounter); | |
| 415 }; | |
| 416 | |
| 417 RundownTaskCounter::RundownTaskCounter() | |
| 418 : count_(1), waitable_event_(true, false) { | |
| 419 } | |
| 420 | |
| 421 void RundownTaskCounter::Post(base::SequencedTaskRunner* task_runner) { | |
| 422 // As the count starts off at one, it should never get to zero unless | |
| 423 // TimedWait has been called. | |
| 424 DCHECK(!base::AtomicRefCountIsZero(&count_)); | |
| 425 | |
| 426 base::AtomicRefCountInc(&count_); | |
| 427 | |
| 428 task_runner->PostTask(FROM_HERE, | |
| 429 base::Bind(&RundownTaskCounter::Decrement, this)); | |
| 430 } | |
| 431 | |
| 432 void RundownTaskCounter::Decrement() { | |
| 433 if (!base::AtomicRefCountDec(&count_)) | |
| 434 waitable_event_.Signal(); | |
| 435 } | |
| 436 | |
| 437 bool RundownTaskCounter::TimedWait(const base::TimeDelta& max_time) { | |
| 438 // Decrement the excess count from the constructor. | |
| 439 Decrement(); | |
| 440 | |
| 441 return waitable_event_.TimedWait(max_time); | |
| 442 } | |
| 443 | |
| 444 } // namespace | |
| 445 | |
| 446 void BrowserProcessImpl::EndSession() { | 387 void BrowserProcessImpl::EndSession() { |
| 447 // Mark all the profiles as clean. | 388 // Mark all the profiles as clean. |
| 448 ProfileManager* pm = profile_manager(); | 389 ProfileManager* pm = profile_manager(); |
| 449 std::vector<Profile*> profiles(pm->GetLoadedProfiles()); | 390 std::vector<Profile*> profiles(pm->GetLoadedProfiles()); |
| 450 scoped_refptr<RundownTaskCounter> rundown_counter(new RundownTaskCounter()); | 391 for (size_t i = 0; i < profiles.size(); ++i) |
| 451 for (size_t i = 0; i < profiles.size(); ++i) { | 392 profiles[i]->SetExitType(Profile::EXIT_SESSION_ENDED); |
| 452 Profile* profile = profiles[i]; | |
| 453 profile->SetExitType(Profile::EXIT_SESSION_ENDED); | |
| 454 | |
| 455 rundown_counter->Post(profile->GetIOTaskRunner()); | |
| 456 } | |
| 457 | 393 |
| 458 // Tell the metrics service it was cleanly shutdown. | 394 // Tell the metrics service it was cleanly shutdown. |
| 459 MetricsService* metrics = g_browser_process->metrics_service(); | 395 MetricsService* metrics = g_browser_process->metrics_service(); |
| 460 if (metrics && local_state()) { | 396 if (metrics && local_state()) { |
| 461 metrics->RecordStartOfSessionEnd(); | 397 metrics->RecordStartOfSessionEnd(); |
| 462 #if !defined(OS_CHROMEOS) | 398 #if !defined(OS_CHROMEOS) |
| 463 // MetricsService lazily writes to prefs, force it to write now. | 399 // MetricsService lazily writes to prefs, force it to write now. |
| 464 // On ChromeOS, chrome gets killed when hangs, so no need to | 400 // On ChromeOS, chrome gets killed when hangs, so no need to |
| 465 // commit metrics::prefs::kStabilitySessionEndCompleted change immediately. | 401 // commit metrics::prefs::kStabilitySessionEndCompleted change immediately. |
| 466 local_state()->CommitPendingWrite(); | 402 local_state()->CommitPendingWrite(); |
| 467 | |
| 468 rundown_counter->Post(local_state_task_runner_); | |
| 469 #endif | 403 #endif |
| 470 } | 404 } |
| 471 | 405 |
| 472 // http://crbug.com/125207 | 406 // http://crbug.com/125207 |
| 473 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 407 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 474 | 408 |
| 475 // We must write that the profile and metrics service shutdown cleanly, | 409 // We must write that the profile and metrics service shutdown cleanly, |
| 476 // otherwise on startup we'll think we crashed. So we block until done and | 410 // otherwise on startup we'll think we crashed. So we block until done and |
| 477 // then proceed with normal shutdown. | 411 // then proceed with normal shutdown. |
| 478 #if defined(USE_X11) || defined(OS_WIN) | 412 #if defined(USE_X11) || defined(OS_WIN) |
| 479 // Do a best-effort wait on the successful countdown of rundown tasks. Note | 413 // Create a waitable event to block on file writing being complete. |
| 480 // that if we don't complete "quickly enough", Windows will terminate our | |
| 481 // process. | |
| 482 // | 414 // |
| 483 // On Windows, we previously posted a message to FILE and then ran a nested | 415 // On Windows, we previously posted a message to FILE and then ran a nested |
| 484 // message loop, waiting for that message to be processed until quitting. | 416 // message loop, waiting for that message to be processed until quitting. |
| 485 // However, doing so means that other messages will also be processed. In | 417 // However, doing so means that other messages will also be processed. In |
| 486 // particular, if the GPU process host notices that the GPU has been killed | 418 // particular, if the GPU process host notices that the GPU has been killed |
| 487 // during shutdown, it races exiting the nested loop with the process host | 419 // during shutdown, it races exiting the nested loop with the process host |
| 488 // blocking the message loop attempting to re-establish a connection to the | 420 // blocking the message loop attempting to re-establish a connection to the |
| 489 // GPU process synchronously. Because the system may not be allowing | 421 // GPU process synchronously. Because the system may not be allowing |
| 490 // processes to launch, this can result in a hang. See | 422 // processes to launch, this can result in a hang. See |
| 491 // http://crbug.com/318527. | 423 // http://crbug.com/318527. |
| 492 rundown_counter->TimedWait( | 424 scoped_ptr<base::WaitableEvent> done_writing( |
| 493 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds)); | 425 new base::WaitableEvent(false, false)); |
| 426 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 427 base::Bind(Signal, done_writing.get())); |
| 428 // If all file writes haven't cleared in the timeout, leak the WaitableEvent |
| 429 // so that there's no race to reference it in Signal(). |
| 430 if (!done_writing->TimedWait( |
| 431 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds))) { |
| 432 ignore_result(done_writing.release()); |
| 433 } |
| 494 #else | 434 #else |
| 495 NOTIMPLEMENTED(); | 435 NOTIMPLEMENTED(); |
| 496 #endif | 436 #endif |
| 497 } | 437 } |
| 498 | 438 |
| 499 MetricsServicesManager* BrowserProcessImpl::GetMetricsServicesManager() { | 439 MetricsServicesManager* BrowserProcessImpl::GetMetricsServicesManager() { |
| 500 DCHECK(CalledOnValidThread()); | 440 DCHECK(CalledOnValidThread()); |
| 501 if (!metrics_services_manager_) | 441 if (!metrics_services_manager_) |
| 502 metrics_services_manager_.reset(new MetricsServicesManager(local_state())); | 442 metrics_services_manager_.reset(new MetricsServicesManager(local_state())); |
| 503 return metrics_services_manager_.get(); | 443 return metrics_services_manager_.get(); |
| (...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1168 } | 1108 } |
| 1169 | 1109 |
| 1170 void BrowserProcessImpl::OnAutoupdateTimer() { | 1110 void BrowserProcessImpl::OnAutoupdateTimer() { |
| 1171 if (CanAutorestartForUpdate()) { | 1111 if (CanAutorestartForUpdate()) { |
| 1172 DLOG(WARNING) << "Detected update. Restarting browser."; | 1112 DLOG(WARNING) << "Detected update. Restarting browser."; |
| 1173 RestartBackgroundInstance(); | 1113 RestartBackgroundInstance(); |
| 1174 } | 1114 } |
| 1175 } | 1115 } |
| 1176 | 1116 |
| 1177 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) | 1117 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) |
| OLD | NEW |