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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 // there are background apps) or exit if there are none. | 312 // there are background apps) or exit if there are none. |
312 if (command_line.HasSwitch(switches::kNoStartupWindow)) { | 313 if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
313 keep_alive_for_startup_.reset( | 314 keep_alive_for_startup_.reset( |
314 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP, | 315 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER_STARTUP, |
315 KeepAliveRestartOption::DISABLED)); | 316 KeepAliveRestartOption::DISABLED)); |
316 } else { | 317 } else { |
317 // Otherwise, start with background mode suspended in case we're launching | 318 // 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 | 319 // in a mode that doesn't open a browser window. It will be resumed when the |
319 // first browser window is opened. | 320 // first browser window is opened. |
320 SuspendBackgroundMode(); | 321 SuspendBackgroundMode(); |
| 322 optimizer_ = BackgroundModeOptimizer::Create(); |
321 } | 323 } |
322 | 324 |
323 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 325 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
324 // in the background until the user explicitly terminates it. | 326 // in the background until the user explicitly terminates it. |
325 if (command_line.HasSwitch(switches::kKeepAliveForTest)) | 327 if (command_line.HasSwitch(switches::kKeepAliveForTest)) |
326 keep_alive_for_test_ = true; | 328 keep_alive_for_test_ = true; |
327 | 329 |
328 if (ShouldBeInBackgroundMode()) | 330 if (ShouldBeInBackgroundMode()) |
329 StartBackgroundMode(); | 331 StartBackgroundMode(); |
330 | 332 |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 } | 671 } |
670 break; | 672 break; |
671 } | 673 } |
672 } | 674 } |
673 | 675 |
674 | 676 |
675 /////////////////////////////////////////////////////////////////////////////// | 677 /////////////////////////////////////////////////////////////////////////////// |
676 // BackgroundModeManager, private | 678 // BackgroundModeManager, private |
677 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { | 679 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { |
678 keep_alive_for_startup_.reset(); | 680 keep_alive_for_startup_.reset(); |
| 681 optimizer_ = BackgroundModeOptimizer::Create(); |
679 } | 682 } |
680 | 683 |
681 void BackgroundModeManager::ReleaseStartupKeepAlive() { | 684 void BackgroundModeManager::ReleaseStartupKeepAlive() { |
682 if (keep_alive_for_startup_) { | 685 if (keep_alive_for_startup_) { |
683 // We call this via the message queue to make sure we don't try to end | 686 // We call this via the message queue to make sure we don't try to end |
684 // keep-alive (which can shutdown Chrome) before the message loop has | 687 // keep-alive (which can shutdown Chrome) before the message loop has |
685 // started. This object reference is safe because it's going to be kept | 688 // started. This object reference is safe because it's going to be kept |
686 // alive by the browser process until after the callback is called. | 689 // alive by the browser process until after the callback is called. |
687 base::ThreadTaskRunnerHandle::Get()->PostTask( | 690 base::ThreadTaskRunnerHandle::Get()->PostTask( |
688 FROM_HERE, | 691 FROM_HERE, |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1029 } | 1032 } |
1030 } | 1033 } |
1031 return profile_it; | 1034 return profile_it; |
1032 } | 1035 } |
1033 | 1036 |
1034 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1037 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
1035 PrefService* service = g_browser_process->local_state(); | 1038 PrefService* service = g_browser_process->local_state(); |
1036 DCHECK(service); | 1039 DCHECK(service); |
1037 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1040 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
1038 } | 1041 } |
OLD | NEW |