| 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 |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 | 400 |
| 401 namespace { | 401 namespace { |
| 402 | 402 |
| 403 // Used at the end of session to block the UI thread for completion of sentinel | 403 // Used at the end of session to block the UI thread for completion of sentinel |
| 404 // tasks on the set of threads used to persist profile data and local state. | 404 // tasks on the set of threads used to persist profile data and local state. |
| 405 // This is done to ensure that the data has been persisted to disk before | 405 // This is done to ensure that the data has been persisted to disk before |
| 406 // continuing. | 406 // continuing. |
| 407 class RundownTaskCounter : | 407 class RundownTaskCounter : |
| 408 public base::RefCountedThreadSafe<RundownTaskCounter> { | 408 public base::RefCountedThreadSafe<RundownTaskCounter> { |
| 409 public: | 409 public: |
| 410 using TaskRunnerVector = |
| 411 std::vector<scoped_refptr<base::SequencedTaskRunner>>; |
| 412 |
| 410 RundownTaskCounter(); | 413 RundownTaskCounter(); |
| 411 | 414 |
| 412 // Posts a rundown task to |task_runner|, can be invoked an arbitrary number | 415 // Posts a rundown task to |task_runner|, can be invoked an arbitrary number |
| 413 // of times before calling TimedWait. | 416 // of times before calling TimedWait. |
| 414 void Post(base::SequencedTaskRunner* task_runner); | 417 void Post(base::SequencedTaskRunner* task_runner); |
| 415 | 418 |
| 419 // Posts a rundown task to each runner in |runners|, where the rundown task |
| 420 // for runner[n+1] is posted during rundown task execution for runner[n]. |
| 421 void PostSerially(const TaskRunnerVector &runners); |
| 422 |
| 416 // Waits until the count is zero or |max_time| has passed. | 423 // Waits until the count is zero or |max_time| has passed. |
| 417 // This can only be called once per instance. | 424 // This can only be called once per instance. |
| 418 bool TimedWait(const base::TimeDelta& max_time); | 425 bool TimedWait(const base::TimeDelta& max_time); |
| 419 | 426 |
| 420 private: | 427 private: |
| 421 friend class base::RefCountedThreadSafe<RundownTaskCounter>; | 428 friend class base::RefCountedThreadSafe<RundownTaskCounter>; |
| 422 ~RundownTaskCounter() {} | 429 ~RundownTaskCounter() {} |
| 423 | 430 |
| 431 void RundownNextSerially(TaskRunnerVector::const_iterator it, |
| 432 scoped_ptr<TaskRunnerVector> runners_ptr); |
| 433 |
| 424 // Decrements the counter and releases the waitable event on transition to | 434 // Decrements the counter and releases the waitable event on transition to |
| 425 // zero. | 435 // zero. |
| 426 void Decrement(); | 436 void Decrement(); |
| 427 | 437 |
| 428 // The count starts at one to defer the possibility of one->zero transitions | 438 // The count starts at one to defer the possibility of one->zero transitions |
| 429 // until TimedWait is called. | 439 // until TimedWait is called. |
| 430 base::AtomicRefCount count_; | 440 base::AtomicRefCount count_; |
| 431 base::WaitableEvent waitable_event_; | 441 base::WaitableEvent waitable_event_; |
| 432 | 442 |
| 433 DISALLOW_COPY_AND_ASSIGN(RundownTaskCounter); | 443 DISALLOW_COPY_AND_ASSIGN(RundownTaskCounter); |
| 434 }; | 444 }; |
| 435 | 445 |
| 436 RundownTaskCounter::RundownTaskCounter() | 446 RundownTaskCounter::RundownTaskCounter() |
| 437 : count_(1), waitable_event_(true, false) { | 447 : count_(1), waitable_event_(true, false) { |
| 438 } | 448 } |
| 439 | 449 |
| 440 void RundownTaskCounter::Post(base::SequencedTaskRunner* task_runner) { | 450 void RundownTaskCounter::Post(base::SequencedTaskRunner* task_runner) { |
| 441 // As the count starts off at one, it should never get to zero unless | 451 // As the count starts off at one, it should never get to zero unless |
| 442 // TimedWait has been called. | 452 // TimedWait has been called. |
| 443 DCHECK(!base::AtomicRefCountIsZero(&count_)); | 453 DCHECK(!base::AtomicRefCountIsZero(&count_)); |
| 444 | 454 |
| 445 base::AtomicRefCountInc(&count_); | 455 base::AtomicRefCountInc(&count_); |
| 446 | 456 |
| 447 // The task must be non-nestable to guarantee that it runs after all tasks | 457 // The task must be non-nestable to guarantee that it runs after all tasks |
| 448 // currently scheduled on |task_runner| have completed. | 458 // currently scheduled on |task_runner| have completed. |
| 449 task_runner->PostNonNestableTask(FROM_HERE, | 459 task_runner->PostNonNestableTask(FROM_HERE, |
| 450 base::Bind(&RundownTaskCounter::Decrement, this)); | 460 base::Bind(&RundownTaskCounter::Decrement, this)); |
| 451 } | 461 } |
| 452 | 462 |
| 453 void RundownTaskCounter::Decrement() { | 463 void RundownTaskCounter::PostSerially(const TaskRunnerVector& runners) { |
| 454 if (!base::AtomicRefCountDec(&count_)) | 464 DCHECK(!runners.empty()); |
| 455 waitable_event_.Signal(); | 465 base::AtomicRefCountInc(&count_); |
| 466 scoped_ptr<TaskRunnerVector> runners_ptr(new TaskRunnerVector(runners)); |
| 467 TaskRunnerVector::const_iterator it = runners_ptr->begin(); |
| 468 (*it)->PostNonNestableTask(FROM_HERE, |
| 469 base::Bind(&RundownTaskCounter::RundownNextSerially, |
| 470 this, it, base::Passed(runners_ptr.Pass()))); |
| 456 } | 471 } |
| 457 | 472 |
| 458 bool RundownTaskCounter::TimedWait(const base::TimeDelta& max_time) { | 473 bool RundownTaskCounter::TimedWait(const base::TimeDelta& max_time) { |
| 459 // Decrement the excess count from the constructor. | 474 // Decrement the excess count from the constructor. |
| 460 Decrement(); | 475 Decrement(); |
| 461 | 476 |
| 462 return waitable_event_.TimedWait(max_time); | 477 return waitable_event_.TimedWait(max_time); |
| 463 } | 478 } |
| 464 | 479 |
| 480 void RundownTaskCounter::Decrement() { |
| 481 if (!base::AtomicRefCountDec(&count_)) |
| 482 waitable_event_.Signal(); |
| 483 } |
| 484 |
| 485 void RundownTaskCounter::RundownNextSerially( |
| 486 TaskRunnerVector::const_iterator it, |
| 487 scoped_ptr<TaskRunnerVector> runners_ptr) { |
| 488 TaskRunnerVector::const_iterator next = ++it; |
| 489 if (next == runners_ptr->end()) { |
| 490 Decrement(); |
| 491 return; |
| 492 } |
| 493 (*next)->PostNonNestableTask(FROM_HERE, |
| 494 base::Bind(&RundownTaskCounter::RundownNextSerially, |
| 495 this, next, base::Passed(runners_ptr.Pass()))); |
| 496 } |
| 497 |
| 498 void FlushStoragePartition( |
| 499 RundownTaskCounter* rundown_counter, |
| 500 content::StoragePartition* partition) { |
| 501 partition->Flush(); |
| 502 //rundown_counter->PostSerially(partition->GetFlushTaskRunners()); |
| 503 } |
| 504 |
| 465 } // namespace | 505 } // namespace |
| 466 | 506 |
| 467 void BrowserProcessImpl::EndSession() { | 507 void BrowserProcessImpl::EndSession() { |
| 468 // Mark all the profiles as clean. | 508 // Mark all the profiles as clean. |
| 469 ProfileManager* pm = profile_manager(); | 509 ProfileManager* pm = profile_manager(); |
| 470 std::vector<Profile*> profiles(pm->GetLoadedProfiles()); | 510 std::vector<Profile*> profiles(pm->GetLoadedProfiles()); |
| 471 scoped_refptr<RundownTaskCounter> rundown_counter(new RundownTaskCounter()); | 511 scoped_refptr<RundownTaskCounter> rundown_counter(new RundownTaskCounter()); |
| 472 for (size_t i = 0; i < profiles.size(); ++i) { | 512 for (size_t i = 0; i < profiles.size(); ++i) { |
| 473 Profile* profile = profiles[i]; | 513 Profile* profile = profiles[i]; |
| 474 profile->SetExitType(Profile::EXIT_SESSION_ENDED); | 514 profile->SetExitType(Profile::EXIT_SESSION_ENDED); |
| 475 if (profile->GetPrefs()) { | 515 if (profile->GetPrefs()) { |
| 476 profile->GetPrefs()->CommitPendingWrite(); | 516 profile->GetPrefs()->CommitPendingWrite(); |
| 477 rundown_counter->Post(profile->GetIOTaskRunner().get()); | 517 rundown_counter->Post(profile->GetIOTaskRunner().get()); |
| 478 } | 518 } |
| 519 content::BrowserContext::ForEachStoragePartition( |
| 520 profile, base::Bind(FlushStoragePartition, rundown_counter)); |
| 479 } | 521 } |
| 480 | 522 |
| 481 // Tell the metrics service it was cleanly shutdown. | 523 // Tell the metrics service it was cleanly shutdown. |
| 482 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | 524 metrics::MetricsService* metrics = g_browser_process->metrics_service(); |
| 483 if (metrics && local_state()) { | 525 if (metrics && local_state()) { |
| 484 metrics->RecordStartOfSessionEnd(); | 526 metrics->RecordStartOfSessionEnd(); |
| 485 #if !defined(OS_CHROMEOS) | 527 #if !defined(OS_CHROMEOS) |
| 486 // MetricsService lazily writes to prefs, force it to write now. | 528 // MetricsService lazily writes to prefs, force it to write now. |
| 487 // On ChromeOS, chrome gets killed when hangs, so no need to | 529 // On ChromeOS, chrome gets killed when hangs, so no need to |
| 488 // commit metrics::prefs::kStabilitySessionEndCompleted change immediately. | 530 // commit metrics::prefs::kStabilitySessionEndCompleted change immediately. |
| (...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1229 } | 1271 } |
| 1230 | 1272 |
| 1231 void BrowserProcessImpl::OnAutoupdateTimer() { | 1273 void BrowserProcessImpl::OnAutoupdateTimer() { |
| 1232 if (CanAutorestartForUpdate()) { | 1274 if (CanAutorestartForUpdate()) { |
| 1233 DLOG(WARNING) << "Detected update. Restarting browser."; | 1275 DLOG(WARNING) << "Detected update. Restarting browser."; |
| 1234 RestartBackgroundInstance(); | 1276 RestartBackgroundInstance(); |
| 1235 } | 1277 } |
| 1236 } | 1278 } |
| 1237 | 1279 |
| 1238 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) | 1280 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) |
| OLD | NEW |