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 // static |
| 97 bool BackgroundModeManager::should_restart_in_background_ = false; |
| 98 |
95 BackgroundModeManager::BackgroundModeData::BackgroundModeData( | 99 BackgroundModeManager::BackgroundModeData::BackgroundModeData( |
96 Profile* profile, | 100 Profile* profile, |
97 CommandIdHandlerVector* command_id_handler_vector) | 101 CommandIdHandlerVector* command_id_handler_vector) |
98 : applications_(new BackgroundApplicationListModel(profile)), | 102 : applications_(new BackgroundApplicationListModel(profile)), |
99 profile_(profile), | 103 profile_(profile), |
100 command_id_handler_vector_(command_id_handler_vector) { | 104 command_id_handler_vector_(command_id_handler_vector) { |
101 } | 105 } |
102 | 106 |
103 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { | 107 BackgroundModeManager::BackgroundModeData::~BackgroundModeData() { |
104 } | 108 } |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 // there are background apps) or exit if there are none. | 315 // there are background apps) or exit if there are none. |
312 if (command_line.HasSwitch(switches::kNoStartupWindow)) { | 316 if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
313 keep_alive_for_startup_.reset( | 317 keep_alive_for_startup_.reset( |
314 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP, | 318 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP, |
315 KeepAliveRestartOption::DISABLED)); | 319 KeepAliveRestartOption::DISABLED)); |
316 } else { | 320 } else { |
317 // Otherwise, start with background mode suspended in case we're launching | 321 // 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 | 322 // in a mode that doesn't open a browser window. It will be resumed when the |
319 // first browser window is opened. | 323 // first browser window is opened. |
320 SuspendBackgroundMode(); | 324 SuspendBackgroundMode(); |
| 325 optimizer_ = BackgroundModeOptimizer::Create(); |
321 } | 326 } |
322 | 327 |
323 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 328 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
324 // in the background until the user explicitly terminates it. | 329 // in the background until the user explicitly terminates it. |
325 if (command_line.HasSwitch(switches::kKeepAliveForTest)) | 330 if (command_line.HasSwitch(switches::kKeepAliveForTest)) |
326 keep_alive_for_test_ = true; | 331 keep_alive_for_test_ = true; |
327 | 332 |
328 if (ShouldBeInBackgroundMode()) | 333 if (ShouldBeInBackgroundMode()) |
329 StartBackgroundMode(); | 334 StartBackgroundMode(); |
330 | 335 |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 } | 669 } |
665 break; | 670 break; |
666 } | 671 } |
667 } | 672 } |
668 | 673 |
669 | 674 |
670 /////////////////////////////////////////////////////////////////////////////// | 675 /////////////////////////////////////////////////////////////////////////////// |
671 // BackgroundModeManager, private | 676 // BackgroundModeManager, private |
672 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { | 677 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { |
673 keep_alive_for_startup_.reset(); | 678 keep_alive_for_startup_.reset(); |
| 679 optimizer_ = BackgroundModeOptimizer::Create(); |
674 } | 680 } |
675 | 681 |
676 void BackgroundModeManager::ReleaseStartupKeepAlive() { | 682 void BackgroundModeManager::ReleaseStartupKeepAlive() { |
677 if (keep_alive_for_startup_) { | 683 if (keep_alive_for_startup_) { |
678 // We call this via the message queue to make sure we don't try to end | 684 // 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 | 685 // 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 | 686 // 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. | 687 // alive by the browser process until after the callback is called. |
682 base::ThreadTaskRunnerHandle::Get()->PostTask( | 688 base::ThreadTaskRunnerHandle::Get()->PostTask( |
683 FROM_HERE, | 689 FROM_HERE, |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 } | 1030 } |
1025 } | 1031 } |
1026 return profile_it; | 1032 return profile_it; |
1027 } | 1033 } |
1028 | 1034 |
1029 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1035 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
1030 PrefService* service = g_browser_process->local_state(); | 1036 PrefService* service = g_browser_process->local_state(); |
1031 DCHECK(service); | 1037 DCHECK(service); |
1032 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1038 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
1033 } | 1039 } |
OLD | NEW |