1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/api/power/power_api_manager.h"
8 #include "base/lazy_instance.h"
9 #include "extensions/browser/extension_registry.h"
10 #include "extensions/common/extension.h"
12 namespace extensions
{
16 const char kPowerSaveBlockerReason
[] = "extension";
18 content::PowerSaveBlocker::PowerSaveBlockerType
19 LevelToPowerSaveBlockerType(core_api::power::Level level
) {
21 case core_api::power::LEVEL_SYSTEM
:
22 return content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension
;
23 case core_api::power::LEVEL_DISPLAY
: // fallthrough
24 case core_api::power::LEVEL_NONE
:
25 return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep
;
27 NOTREACHED() << "Unhandled level " << level
;
28 return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep
;
31 base::LazyInstance
<BrowserContextKeyedAPIFactory
<PowerApiManager
> > g_factory
=
32 LAZY_INSTANCE_INITIALIZER
;
37 PowerApiManager
* PowerApiManager::Get(content::BrowserContext
* context
) {
38 return BrowserContextKeyedAPIFactory
<PowerApiManager
>::Get(context
);
42 BrowserContextKeyedAPIFactory
<PowerApiManager
>*
43 PowerApiManager::GetFactoryInstance() {
44 return g_factory
.Pointer();
47 void PowerApiManager::AddRequest(const std::string
& extension_id
,
48 core_api::power::Level level
) {
49 extension_levels_
[extension_id
] = level
;
50 UpdatePowerSaveBlocker();
53 void PowerApiManager::RemoveRequest(const std::string
& extension_id
) {
54 extension_levels_
.erase(extension_id
);
55 UpdatePowerSaveBlocker();
58 void PowerApiManager::SetCreateBlockerFunctionForTesting(
59 CreateBlockerFunction function
) {
60 create_blocker_function_
= !function
.is_null() ? function
:
61 base::Bind(&content::PowerSaveBlocker::Create
);
64 void PowerApiManager::OnExtensionUnloaded(
65 content::BrowserContext
* browser_context
,
66 const Extension
* extension
,
67 UnloadedExtensionInfo::Reason reason
) {
68 RemoveRequest(extension
->id());
69 UpdatePowerSaveBlocker();
72 PowerApiManager::PowerApiManager(content::BrowserContext
* context
)
73 : browser_context_(context
),
74 create_blocker_function_(base::Bind(&content::PowerSaveBlocker::Create
)),
75 current_level_(core_api::power::LEVEL_SYSTEM
) {
76 ExtensionRegistry::Get(browser_context_
)->AddObserver(this);
79 PowerApiManager::~PowerApiManager() {}
81 void PowerApiManager::UpdatePowerSaveBlocker() {
82 if (extension_levels_
.empty()) {
83 power_save_blocker_
.reset();
87 core_api::power::Level new_level
= core_api::power::LEVEL_SYSTEM
;
88 for (ExtensionLevelMap::const_iterator it
= extension_levels_
.begin();
89 it
!= extension_levels_
.end(); ++it
) {
90 if (it
->second
== core_api::power::LEVEL_DISPLAY
)
91 new_level
= it
->second
;
94 // If the level changed and we need to create a new blocker, do a swap
95 // to ensure that there isn't a brief period where power management is
97 if (!power_save_blocker_
|| new_level
!= current_level_
) {
98 content::PowerSaveBlocker::PowerSaveBlockerType type
=
99 LevelToPowerSaveBlockerType(new_level
);
100 scoped_ptr
<content::PowerSaveBlocker
> new_blocker(
101 create_blocker_function_
.Run(type
, kPowerSaveBlockerReason
));
102 power_save_blocker_
.swap(new_blocker
);
103 current_level_
= new_level
;
107 void PowerApiManager::Shutdown() {
108 // Unregister here rather than in the d'tor; otherwise this call will recreate
109 // the already-deleted ExtensionRegistry.
110 ExtensionRegistry::Get(browser_context_
)->RemoveObserver(this);
111 power_save_blocker_
.reset();
114 } // namespace extensions