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.h"
8 #include "base/lazy_instance.h"
9 #include "extensions/browser/extension_registry.h"
10 #include "extensions/common/api/power.h"
11 #include "extensions/common/extension.h"
13 namespace extensions
{
17 const char kPowerSaveBlockerDescription
[] = "extension";
19 content::PowerSaveBlocker::PowerSaveBlockerType
LevelToPowerSaveBlockerType(
20 api::power::Level level
) {
22 case api::power::LEVEL_SYSTEM
:
23 return content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension
;
24 case api::power::LEVEL_DISPLAY
: // fallthrough
25 case api::power::LEVEL_NONE
:
26 return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep
;
28 NOTREACHED() << "Unhandled level " << level
;
29 return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep
;
32 base::LazyInstance
<BrowserContextKeyedAPIFactory
<PowerAPI
>> g_factory
=
33 LAZY_INSTANCE_INITIALIZER
;
37 bool PowerRequestKeepAwakeFunction::RunSync() {
38 scoped_ptr
<api::power::RequestKeepAwake::Params
> params(
39 api::power::RequestKeepAwake::Params::Create(*args_
));
40 EXTENSION_FUNCTION_VALIDATE(params
);
41 EXTENSION_FUNCTION_VALIDATE(params
->level
!= api::power::LEVEL_NONE
);
42 PowerAPI::Get(browser_context())->AddRequest(extension_id(), params
->level
);
46 bool PowerReleaseKeepAwakeFunction::RunSync() {
47 PowerAPI::Get(browser_context())->RemoveRequest(extension_id());
52 PowerAPI
* PowerAPI::Get(content::BrowserContext
* context
) {
53 return BrowserContextKeyedAPIFactory
<PowerAPI
>::Get(context
);
57 BrowserContextKeyedAPIFactory
<PowerAPI
>* PowerAPI::GetFactoryInstance() {
58 return g_factory
.Pointer();
61 void PowerAPI::AddRequest(const std::string
& extension_id
,
62 api::power::Level level
) {
63 extension_levels_
[extension_id
] = level
;
64 UpdatePowerSaveBlocker();
67 void PowerAPI::RemoveRequest(const std::string
& extension_id
) {
68 extension_levels_
.erase(extension_id
);
69 UpdatePowerSaveBlocker();
72 void PowerAPI::SetCreateBlockerFunctionForTesting(
73 CreateBlockerFunction function
) {
74 create_blocker_function_
=
75 !function
.is_null() ? function
76 : base::Bind(&content::PowerSaveBlocker::Create
);
79 void PowerAPI::OnExtensionUnloaded(content::BrowserContext
* browser_context
,
80 const Extension
* extension
,
81 UnloadedExtensionInfo::Reason reason
) {
82 RemoveRequest(extension
->id());
83 UpdatePowerSaveBlocker();
86 PowerAPI::PowerAPI(content::BrowserContext
* context
)
87 : browser_context_(context
),
88 create_blocker_function_(base::Bind(&content::PowerSaveBlocker::Create
)),
89 current_level_(api::power::LEVEL_SYSTEM
) {
90 ExtensionRegistry::Get(browser_context_
)->AddObserver(this);
93 PowerAPI::~PowerAPI() {
96 void PowerAPI::UpdatePowerSaveBlocker() {
97 if (extension_levels_
.empty()) {
98 power_save_blocker_
.reset();
102 api::power::Level new_level
= api::power::LEVEL_SYSTEM
;
103 for (ExtensionLevelMap::const_iterator it
= extension_levels_
.begin();
104 it
!= extension_levels_
.end(); ++it
) {
105 if (it
->second
== api::power::LEVEL_DISPLAY
)
106 new_level
= it
->second
;
109 // If the level changed and we need to create a new blocker, do a swap
110 // to ensure that there isn't a brief period where power management is
112 if (!power_save_blocker_
|| new_level
!= current_level_
) {
113 content::PowerSaveBlocker::PowerSaveBlockerType type
=
114 LevelToPowerSaveBlockerType(new_level
);
115 scoped_ptr
<content::PowerSaveBlocker
> new_blocker(
116 create_blocker_function_
.Run(type
,
117 content::PowerSaveBlocker::kReasonOther
,
118 kPowerSaveBlockerDescription
));
119 power_save_blocker_
.swap(new_blocker
);
120 current_level_
= new_level
;
124 void PowerAPI::Shutdown() {
125 // Unregister here rather than in the d'tor; otherwise this call will recreate
126 // the already-deleted ExtensionRegistry.
127 ExtensionRegistry::Get(browser_context_
)->RemoveObserver(this);
128 power_save_blocker_
.reset();
131 } // namespace extensions