| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 69 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 70 #include "chrome/browser/profiles/profile_manager.h" | 70 #include "chrome/browser/profiles/profile_manager.h" |
| 71 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 71 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 72 #include "chrome/browser/shell_integration.h" | 72 #include "chrome/browser/shell_integration.h" |
| 73 #include "chrome/browser/status_icons/status_tray.h" | 73 #include "chrome/browser/status_icons/status_tray.h" |
| 74 #include "chrome/browser/ui/browser_dialogs.h" | 74 #include "chrome/browser/ui/browser_dialogs.h" |
| 75 #include "chrome/browser/ui/browser_finder.h" | 75 #include "chrome/browser/ui/browser_finder.h" |
| 76 #include "chrome/browser/update_client/chrome_update_query_params_delegate.h" | 76 #include "chrome/browser/update_client/chrome_update_query_params_delegate.h" |
| 77 #include "chrome/common/channel_info.h" | 77 #include "chrome/common/channel_info.h" |
| 78 #include "chrome/common/chrome_constants.h" | 78 #include "chrome/common/chrome_constants.h" |
| 79 #include "chrome/common/chrome_features.h" |
| 79 #include "chrome/common/chrome_paths.h" | 80 #include "chrome/common/chrome_paths.h" |
| 80 #include "chrome/common/chrome_switches.h" | 81 #include "chrome/common/chrome_switches.h" |
| 81 #include "chrome/common/crash_keys.h" | 82 #include "chrome/common/crash_keys.h" |
| 82 #include "chrome/common/extensions/chrome_extensions_client.h" | 83 #include "chrome/common/extensions/chrome_extensions_client.h" |
| 83 #include "chrome/common/extensions/extension_process_policy.h" | 84 #include "chrome/common/extensions/extension_process_policy.h" |
| 84 #include "chrome/common/features.h" | 85 #include "chrome/common/features.h" |
| 85 #include "chrome/common/pref_names.h" | 86 #include "chrome/common/pref_names.h" |
| 86 #include "chrome/common/switch_utils.h" | 87 #include "chrome/common/switch_utils.h" |
| 87 #include "chrome/common/url_constants.h" | 88 #include "chrome/common/url_constants.h" |
| 88 #include "chrome/installer/util/google_update_constants.h" | 89 #include "chrome/installer/util/google_update_constants.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 // How often to check if the persistent instance of Chrome needs to restart | 188 // How often to check if the persistent instance of Chrome needs to restart |
| 188 // to install an update. | 189 // to install an update. |
| 189 static const int kUpdateCheckIntervalHours = 6; | 190 static const int kUpdateCheckIntervalHours = 6; |
| 190 #endif | 191 #endif |
| 191 | 192 |
| 192 #if defined(USE_X11) || defined(OS_WIN) || defined(USE_OZONE) | 193 #if defined(USE_X11) || defined(OS_WIN) || defined(USE_OZONE) |
| 193 // How long to wait for the File thread to complete during EndSession, on Linux | 194 // How long to wait for the File thread to complete during EndSession, on Linux |
| 194 // and Windows. We have a timeout here because we're unable to run the UI | 195 // and Windows. We have a timeout here because we're unable to run the UI |
| 195 // messageloop and there's some deadlock risk. Our only option is to exit | 196 // messageloop and there's some deadlock risk. Our only option is to exit |
| 196 // anyway. | 197 // anyway. |
| 197 static const int kEndSessionTimeoutSeconds = 10; | 198 static constexpr base::TimeDelta kEndSessionTimeout = |
| 199 base::TimeDelta::FromSeconds(10); |
| 198 #endif | 200 #endif |
| 199 | 201 |
| 200 using content::BrowserThread; | 202 using content::BrowserThread; |
| 201 using content::ChildProcessSecurityPolicy; | 203 using content::ChildProcessSecurityPolicy; |
| 202 using content::PluginService; | 204 using content::PluginService; |
| 203 using content::ResourceDispatcherHost; | 205 using content::ResourceDispatcherHost; |
| 204 | 206 |
| 205 rappor::RapporService* GetBrowserRapporService() { | 207 rappor::RapporService* GetBrowserRapporService() { |
| 206 if (g_browser_process != nullptr) | 208 if (g_browser_process != nullptr) |
| 207 return g_browser_process->rappor_service(); | 209 return g_browser_process->rappor_service(); |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 // continuing. | 405 // continuing. |
| 404 class RundownTaskCounter : | 406 class RundownTaskCounter : |
| 405 public base::RefCountedThreadSafe<RundownTaskCounter> { | 407 public base::RefCountedThreadSafe<RundownTaskCounter> { |
| 406 public: | 408 public: |
| 407 RundownTaskCounter(); | 409 RundownTaskCounter(); |
| 408 | 410 |
| 409 // Posts a rundown task to |task_runner|, can be invoked an arbitrary number | 411 // Posts a rundown task to |task_runner|, can be invoked an arbitrary number |
| 410 // of times before calling TimedWait. | 412 // of times before calling TimedWait. |
| 411 void Post(base::SequencedTaskRunner* task_runner); | 413 void Post(base::SequencedTaskRunner* task_runner); |
| 412 | 414 |
| 413 // Waits until the count is zero or |max_time| has passed. | 415 // Waits until the count is zero or |end_time| is reached. |
| 414 // This can only be called once per instance. | 416 // This can only be called once per instance. Returns true if a count of zero |
| 415 bool TimedWait(const base::TimeDelta& max_time); | 417 // is reached or false if the |end_time| is reached. It is valid to pass an |
| 418 // |end_time| in the past. |
| 419 bool TimedWaitUntil(const base::TimeTicks& end_time); |
| 416 | 420 |
| 417 private: | 421 private: |
| 418 friend class base::RefCountedThreadSafe<RundownTaskCounter>; | 422 friend class base::RefCountedThreadSafe<RundownTaskCounter>; |
| 419 ~RundownTaskCounter() {} | 423 ~RundownTaskCounter() {} |
| 420 | 424 |
| 421 // Decrements the counter and releases the waitable event on transition to | 425 // Decrements the counter and releases the waitable event on transition to |
| 422 // zero. | 426 // zero. |
| 423 void Decrement(); | 427 void Decrement(); |
| 424 | 428 |
| 425 // The count starts at one to defer the possibility of one->zero transitions | 429 // The count starts at one to defer the possibility of one->zero transitions |
| (...skipping 20 matching lines...) Expand all Loading... |
| 446 // currently scheduled on |task_runner| have completed. | 450 // currently scheduled on |task_runner| have completed. |
| 447 task_runner->PostNonNestableTask(FROM_HERE, | 451 task_runner->PostNonNestableTask(FROM_HERE, |
| 448 base::Bind(&RundownTaskCounter::Decrement, this)); | 452 base::Bind(&RundownTaskCounter::Decrement, this)); |
| 449 } | 453 } |
| 450 | 454 |
| 451 void RundownTaskCounter::Decrement() { | 455 void RundownTaskCounter::Decrement() { |
| 452 if (!base::AtomicRefCountDec(&count_)) | 456 if (!base::AtomicRefCountDec(&count_)) |
| 453 waitable_event_.Signal(); | 457 waitable_event_.Signal(); |
| 454 } | 458 } |
| 455 | 459 |
| 456 bool RundownTaskCounter::TimedWait(const base::TimeDelta& max_time) { | 460 bool RundownTaskCounter::TimedWaitUntil(const base::TimeTicks& end_time) { |
| 457 // Decrement the excess count from the constructor. | 461 // Decrement the excess count from the constructor. |
| 458 Decrement(); | 462 Decrement(); |
| 459 | 463 |
| 460 return waitable_event_.TimedWait(max_time); | 464 return waitable_event_.TimedWaitUntil(end_time); |
| 461 } | 465 } |
| 462 | 466 |
| 463 } // namespace | 467 } // namespace |
| 464 | 468 |
| 465 void BrowserProcessImpl::EndSession() { | 469 void BrowserProcessImpl::EndSession() { |
| 466 // Mark all the profiles as clean. | 470 // Mark all the profiles as clean. |
| 467 ProfileManager* pm = profile_manager(); | 471 ProfileManager* pm = profile_manager(); |
| 468 std::vector<Profile*> profiles(pm->GetLoadedProfiles()); | 472 std::vector<Profile*> profiles(pm->GetLoadedProfiles()); |
| 469 scoped_refptr<RundownTaskCounter> rundown_counter(new RundownTaskCounter()); | 473 scoped_refptr<RundownTaskCounter> rundown_counter(new RundownTaskCounter()); |
| 474 const bool pref_service_enabled = |
| 475 base::FeatureList::IsEnabled(features::kPrefService); |
| 476 std::vector<scoped_refptr<base::SequencedTaskRunner>> profile_writer_runners; |
| 470 for (size_t i = 0; i < profiles.size(); ++i) { | 477 for (size_t i = 0; i < profiles.size(); ++i) { |
| 471 Profile* profile = profiles[i]; | 478 Profile* profile = profiles[i]; |
| 472 profile->SetExitType(Profile::EXIT_SESSION_ENDED); | 479 profile->SetExitType(Profile::EXIT_SESSION_ENDED); |
| 473 if (profile->GetPrefs()) { | 480 if (profile->GetPrefs()) { |
| 474 profile->GetPrefs()->CommitPendingWrite(); | 481 profile->GetPrefs()->CommitPendingWrite(); |
| 475 rundown_counter->Post(profile->GetIOTaskRunner().get()); | 482 if (pref_service_enabled) { |
| 483 rundown_counter->Post(content::BrowserThread::GetTaskRunnerForThread( |
| 484 content::BrowserThread::IO) |
| 485 .get()); |
| 486 profile_writer_runners.push_back(profile->GetIOTaskRunner()); |
| 487 } else { |
| 488 rundown_counter->Post(profile->GetIOTaskRunner().get()); |
| 489 } |
| 476 } | 490 } |
| 477 } | 491 } |
| 478 | 492 |
| 479 // Tell the metrics service it was cleanly shutdown. | 493 // Tell the metrics service it was cleanly shutdown. |
| 480 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | 494 metrics::MetricsService* metrics = g_browser_process->metrics_service(); |
| 481 if (metrics && local_state()) { | 495 if (metrics && local_state()) { |
| 482 metrics->RecordStartOfSessionEnd(); | 496 metrics->RecordStartOfSessionEnd(); |
| 483 #if !defined(OS_CHROMEOS) | 497 #if !defined(OS_CHROMEOS) |
| 484 // MetricsService lazily writes to prefs, force it to write now. | 498 // MetricsService lazily writes to prefs, force it to write now. |
| 485 // On ChromeOS, chrome gets killed when hangs, so no need to | 499 // On ChromeOS, chrome gets killed when hangs, so no need to |
| (...skipping 20 matching lines...) Expand all Loading... |
| 506 // | 520 // |
| 507 // On Windows, we previously posted a message to FILE and then ran a nested | 521 // On Windows, we previously posted a message to FILE and then ran a nested |
| 508 // message loop, waiting for that message to be processed until quitting. | 522 // message loop, waiting for that message to be processed until quitting. |
| 509 // However, doing so means that other messages will also be processed. In | 523 // However, doing so means that other messages will also be processed. In |
| 510 // particular, if the GPU process host notices that the GPU has been killed | 524 // particular, if the GPU process host notices that the GPU has been killed |
| 511 // during shutdown, it races exiting the nested loop with the process host | 525 // during shutdown, it races exiting the nested loop with the process host |
| 512 // blocking the message loop attempting to re-establish a connection to the | 526 // blocking the message loop attempting to re-establish a connection to the |
| 513 // GPU process synchronously. Because the system may not be allowing | 527 // GPU process synchronously. Because the system may not be allowing |
| 514 // processes to launch, this can result in a hang. See | 528 // processes to launch, this can result in a hang. See |
| 515 // http://crbug.com/318527. | 529 // http://crbug.com/318527. |
| 516 rundown_counter->TimedWait( | 530 const base::TimeTicks end_time = base::TimeTicks::Now() + kEndSessionTimeout; |
| 517 base::TimeDelta::FromSeconds(kEndSessionTimeoutSeconds)); | 531 const bool timed_out = !rundown_counter->TimedWaitUntil(end_time); |
| 532 if (timed_out || !pref_service_enabled) |
| 533 return; |
| 534 |
| 535 scoped_refptr<RundownTaskCounter> profile_write_rundown_counter( |
| 536 new RundownTaskCounter()); |
| 537 for (auto& profile_writer_runner : profile_writer_runners) |
| 538 profile_write_rundown_counter->Post(profile_writer_runner.get()); |
| 539 profile_write_rundown_counter->TimedWaitUntil(end_time); |
| 518 #else | 540 #else |
| 519 NOTIMPLEMENTED(); | 541 NOTIMPLEMENTED(); |
| 520 #endif | 542 #endif |
| 521 } | 543 } |
| 522 | 544 |
| 523 metrics_services_manager::MetricsServicesManager* | 545 metrics_services_manager::MetricsServicesManager* |
| 524 BrowserProcessImpl::GetMetricsServicesManager() { | 546 BrowserProcessImpl::GetMetricsServicesManager() { |
| 525 DCHECK(CalledOnValidThread()); | 547 DCHECK(CalledOnValidThread()); |
| 526 if (!metrics_services_manager_) { | 548 if (!metrics_services_manager_) { |
| 527 metrics_services_manager_.reset( | 549 metrics_services_manager_.reset( |
| (...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1413 } | 1435 } |
| 1414 | 1436 |
| 1415 void BrowserProcessImpl::OnAutoupdateTimer() { | 1437 void BrowserProcessImpl::OnAutoupdateTimer() { |
| 1416 if (CanAutorestartForUpdate()) { | 1438 if (CanAutorestartForUpdate()) { |
| 1417 DLOG(WARNING) << "Detected update. Restarting browser."; | 1439 DLOG(WARNING) << "Detected update. Restarting browser."; |
| 1418 RestartBackgroundInstance(); | 1440 RestartBackgroundInstance(); |
| 1419 } | 1441 } |
| 1420 } | 1442 } |
| 1421 | 1443 |
| 1422 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) | 1444 #endif // (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS) |
| OLD | NEW |