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