Chromium Code Reviews| 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/background/background_mode_manager.h" | 5 #include "chrome/browser/background/background_mode_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "chrome/app/chrome_command_ids.h" | 24 #include "chrome/app/chrome_command_ids.h" |
| 25 #include "chrome/browser/background/background_application_list_model.h" | 25 #include "chrome/browser/background/background_application_list_model.h" |
| 26 #include "chrome/browser/background/background_mode_optimizer.h" | |
| 26 #include "chrome/browser/background/background_trigger.h" | 27 #include "chrome/browser/background/background_trigger.h" |
| 27 #include "chrome/browser/browser_process.h" | 28 #include "chrome/browser/browser_process.h" |
| 28 #include "chrome/browser/browser_shutdown.h" | 29 #include "chrome/browser/browser_shutdown.h" |
| 29 #include "chrome/browser/chrome_notification_types.h" | 30 #include "chrome/browser/chrome_notification_types.h" |
| 30 #include "chrome/browser/extensions/extension_service.h" | 31 #include "chrome/browser/extensions/extension_service.h" |
| 31 #include "chrome/browser/lifetime/application_lifetime.h" | 32 #include "chrome/browser/lifetime/application_lifetime.h" |
| 32 #include "chrome/browser/lifetime/keep_alive_registry.h" | 33 #include "chrome/browser/lifetime/keep_alive_registry.h" |
| 33 #include "chrome/browser/lifetime/keep_alive_types.h" | 34 #include "chrome/browser/lifetime/keep_alive_types.h" |
| 34 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
| 35 #include "chrome/browser/profiles/profile_attributes_entry.h" | 36 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 MENU_ITEM_EXIT = 4, | 86 MENU_ITEM_EXIT = 4, |
| 86 MENU_ITEM_NUM_STATES | 87 MENU_ITEM_NUM_STATES |
| 87 }; | 88 }; |
| 88 | 89 |
| 89 void RecordMenuItemClick(MenuItem item) { | 90 void RecordMenuItemClick(MenuItem item) { |
| 90 UMA_HISTOGRAM_ENUMERATION("BackgroundMode.MenuItemClick", item, | 91 UMA_HISTOGRAM_ENUMERATION("BackgroundMode.MenuItemClick", item, |
| 91 MENU_ITEM_NUM_STATES); | 92 MENU_ITEM_NUM_STATES); |
| 92 } | 93 } |
| 93 } // namespace | 94 } // namespace |
| 94 | 95 |
| 96 bool BackgroundModeManager::should_restart_in_background_ = false; | |
|
sky
2016/07/22 16:57:52
in general we prefix statics like this with
// sta
dgn
2016/07/25 17:32:28
Ah! on variables too, okay. Done.
| |
| 97 | |
| 95 BackgroundModeManager::BackgroundModeData::BackgroundModeData( | 98 BackgroundModeManager::BackgroundModeData::BackgroundModeData( |
| 96 Profile* profile, | 99 Profile* profile, |
| 97 CommandIdHandlerVector* command_id_handler_vector) | 100 CommandIdHandlerVector* command_id_handler_vector) |
| 98 : applications_(new BackgroundApplicationListModel(profile)), | 101 : applications_(new BackgroundApplicationListModel(profile)), |
| 99 profile_(profile), | 102 profile_(profile), |
| 100 command_id_handler_vector_(command_id_handler_vector) { | 103 command_id_handler_vector_(command_id_handler_vector) { |
| 101 } | 104 } |
| 102 | 105 |
| 103 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { | 106 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { |
| 104 } | 107 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 context_menu_(NULL), | 283 context_menu_(NULL), |
| 281 in_background_mode_(false), | 284 in_background_mode_(false), |
| 282 keep_alive_for_test_(false), | 285 keep_alive_for_test_(false), |
| 283 background_mode_suspended_(false), | 286 background_mode_suspended_(false), |
| 284 weak_factory_(this) { | 287 weak_factory_(this) { |
| 285 // We should never start up if there is no browser process or if we are | 288 // We should never start up if there is no browser process or if we are |
| 286 // currently quitting. | 289 // currently quitting. |
| 287 CHECK(g_browser_process != NULL); | 290 CHECK(g_browser_process != NULL); |
| 288 CHECK(!browser_shutdown::IsTryingToQuit()); | 291 CHECK(!browser_shutdown::IsTryingToQuit()); |
| 289 | 292 |
| 293 BackgroundModeManager::should_restart_in_background_ = false; | |
|
sky
2016/07/22 16:57:52
It isn't obvious why you need this here. Please ad
dgn
2016/07/25 17:32:28
Not needed. Removed.
| |
| 294 | |
| 290 // Add self as an observer for the ProfileAttributesStorage so we know when | 295 // Add self as an observer for the ProfileAttributesStorage so we know when |
| 291 // profiles are deleted and their names change. | 296 // profiles are deleted and their names change. |
| 292 profile_storage_->AddObserver(this); | 297 profile_storage_->AddObserver(this); |
| 293 | 298 |
| 294 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", | 299 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", |
| 295 command_line.HasSwitch(switches::kNoStartupWindow)); | 300 command_line.HasSwitch(switches::kNoStartupWindow)); |
| 296 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", | 301 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", |
| 297 IsBackgroundModePrefEnabled()); | 302 IsBackgroundModePrefEnabled()); |
| 298 | 303 |
| 299 // Listen for the background mode preference changing. | 304 // Listen for the background mode preference changing. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 311 // there are background apps) or exit if there are none. | 316 // there are background apps) or exit if there are none. |
| 312 if (command_line.HasSwitch(switches::kNoStartupWindow)) { | 317 if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
| 313 keep_alive_for_startup_.reset( | 318 keep_alive_for_startup_.reset( |
| 314 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP, | 319 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP, |
| 315 KeepAliveRestartOption::DISABLED)); | 320 KeepAliveRestartOption::DISABLED)); |
| 316 } else { | 321 } else { |
| 317 // Otherwise, start with background mode suspended in case we're launching | 322 // Otherwise, start with background mode suspended in case we're launching |
| 318 // in a mode that doesn't open a browser window. It will be resumed when the | 323 // in a mode that doesn't open a browser window. It will be resumed when the |
| 319 // first browser window is opened. | 324 // first browser window is opened. |
| 320 SuspendBackgroundMode(); | 325 SuspendBackgroundMode(); |
| 326 optimizer_ = BackgroundModeOptimizer::Create(); | |
| 321 } | 327 } |
| 322 | 328 |
| 323 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 329 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
| 324 // in the background until the user explicitly terminates it. | 330 // in the background until the user explicitly terminates it. |
| 325 if (command_line.HasSwitch(switches::kKeepAliveForTest)) | 331 if (command_line.HasSwitch(switches::kKeepAliveForTest)) |
| 326 keep_alive_for_test_ = true; | 332 keep_alive_for_test_ = true; |
| 327 | 333 |
| 328 if (ShouldBeInBackgroundMode()) | 334 if (ShouldBeInBackgroundMode()) |
| 329 StartBackgroundMode(); | 335 StartBackgroundMode(); |
| 330 | 336 |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 664 } | 670 } |
| 665 break; | 671 break; |
| 666 } | 672 } |
| 667 } | 673 } |
| 668 | 674 |
| 669 | 675 |
| 670 /////////////////////////////////////////////////////////////////////////////// | 676 /////////////////////////////////////////////////////////////////////////////// |
| 671 // BackgroundModeManager, private | 677 // BackgroundModeManager, private |
| 672 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { | 678 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { |
| 673 keep_alive_for_startup_.reset(); | 679 keep_alive_for_startup_.reset(); |
| 680 optimizer_ = BackgroundModeOptimizer::Create(); | |
| 674 } | 681 } |
| 675 | 682 |
| 676 void BackgroundModeManager::ReleaseStartupKeepAlive() { | 683 void BackgroundModeManager::ReleaseStartupKeepAlive() { |
| 677 if (keep_alive_for_startup_) { | 684 if (keep_alive_for_startup_) { |
| 678 // We call this via the message queue to make sure we don't try to end | 685 // We call this via the message queue to make sure we don't try to end |
| 679 // keep-alive (which can shutdown Chrome) before the message loop has | 686 // keep-alive (which can shutdown Chrome) before the message loop has |
| 680 // started. This object reference is safe because it's going to be kept | 687 // started. This object reference is safe because it's going to be kept |
| 681 // alive by the browser process until after the callback is called. | 688 // alive by the browser process until after the callback is called. |
| 682 base::ThreadTaskRunnerHandle::Get()->PostTask( | 689 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 683 FROM_HERE, | 690 FROM_HERE, |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1024 } | 1031 } |
| 1025 } | 1032 } |
| 1026 return profile_it; | 1033 return profile_it; |
| 1027 } | 1034 } |
| 1028 | 1035 |
| 1029 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1036 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
| 1030 PrefService* service = g_browser_process->local_state(); | 1037 PrefService* service = g_browser_process->local_state(); |
| 1031 DCHECK(service); | 1038 DCHECK(service); |
| 1032 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1039 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
| 1033 } | 1040 } |
| OLD | NEW |