OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/api/power/power_api.h" | 5 #include "extensions/browser/api/power/power_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/power_save_blocker_factory.h" |
10 #include "device/power_save_blocker/power_save_blocker.h" | |
11 #include "extensions/browser/extension_registry.h" | 10 #include "extensions/browser/extension_registry.h" |
12 #include "extensions/common/api/power.h" | 11 #include "extensions/common/api/power.h" |
13 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
14 | 13 |
15 namespace extensions { | 14 namespace extensions { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 const char kPowerSaveBlockerDescription[] = "extension"; | 18 const char kPowerSaveBlockerDescription[] = "extension"; |
20 | 19 |
21 device::PowerSaveBlocker::PowerSaveBlockerType LevelToPowerSaveBlockerType( | 20 content::PowerSaveBlocker::PowerSaveBlockerType LevelToPowerSaveBlockerType( |
22 api::power::Level level) { | 21 api::power::Level level) { |
23 switch (level) { | 22 switch (level) { |
24 case api::power::LEVEL_SYSTEM: | 23 case api::power::LEVEL_SYSTEM: |
25 return device::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension; | 24 return content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension; |
26 case api::power::LEVEL_DISPLAY: // fallthrough | 25 case api::power::LEVEL_DISPLAY: // fallthrough |
27 case api::power::LEVEL_NONE: | 26 case api::power::LEVEL_NONE: |
28 return device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; | 27 return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; |
29 } | 28 } |
30 NOTREACHED() << "Unhandled level " << level; | 29 NOTREACHED() << "Unhandled level " << level; |
31 return device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; | 30 return content::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; |
32 } | 31 } |
33 | 32 |
34 base::LazyInstance<BrowserContextKeyedAPIFactory<PowerAPI>> g_factory = | 33 base::LazyInstance<BrowserContextKeyedAPIFactory<PowerAPI>> g_factory = |
35 LAZY_INSTANCE_INITIALIZER; | 34 LAZY_INSTANCE_INITIALIZER; |
36 | 35 |
37 } // namespace | 36 } // namespace |
38 | 37 |
39 bool PowerRequestKeepAwakeFunction::RunSync() { | 38 bool PowerRequestKeepAwakeFunction::RunSync() { |
40 std::unique_ptr<api::power::RequestKeepAwake::Params> params( | 39 std::unique_ptr<api::power::RequestKeepAwake::Params> params( |
41 api::power::RequestKeepAwake::Params::Create(*args_)); | 40 api::power::RequestKeepAwake::Params::Create(*args_)); |
(...skipping 24 matching lines...) Expand all Loading... |
66 UpdatePowerSaveBlocker(); | 65 UpdatePowerSaveBlocker(); |
67 } | 66 } |
68 | 67 |
69 void PowerAPI::RemoveRequest(const std::string& extension_id) { | 68 void PowerAPI::RemoveRequest(const std::string& extension_id) { |
70 extension_levels_.erase(extension_id); | 69 extension_levels_.erase(extension_id); |
71 UpdatePowerSaveBlocker(); | 70 UpdatePowerSaveBlocker(); |
72 } | 71 } |
73 | 72 |
74 void PowerAPI::SetCreateBlockerFunctionForTesting( | 73 void PowerAPI::SetCreateBlockerFunctionForTesting( |
75 CreateBlockerFunction function) { | 74 CreateBlockerFunction function) { |
76 create_blocker_function_ = | 75 create_blocker_function_ = !function.is_null() |
77 !function.is_null() | 76 ? function |
78 ? function | 77 : base::Bind(&content::CreatePowerSaveBlocker); |
79 : base::Bind(&device::PowerSaveBlocker::CreateWithTaskRunners); | |
80 } | 78 } |
81 | 79 |
82 void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | 80 void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
83 const Extension* extension, | 81 const Extension* extension, |
84 UnloadedExtensionInfo::Reason reason) { | 82 UnloadedExtensionInfo::Reason reason) { |
85 RemoveRequest(extension->id()); | 83 RemoveRequest(extension->id()); |
86 UpdatePowerSaveBlocker(); | 84 UpdatePowerSaveBlocker(); |
87 } | 85 } |
88 | 86 |
89 PowerAPI::PowerAPI(content::BrowserContext* context) | 87 PowerAPI::PowerAPI(content::BrowserContext* context) |
90 : browser_context_(context), | 88 : browser_context_(context), |
91 create_blocker_function_( | 89 create_blocker_function_(base::Bind(&content::CreatePowerSaveBlocker)), |
92 base::Bind(&device::PowerSaveBlocker::CreateWithTaskRunners)), | |
93 current_level_(api::power::LEVEL_SYSTEM) { | 90 current_level_(api::power::LEVEL_SYSTEM) { |
94 ExtensionRegistry::Get(browser_context_)->AddObserver(this); | 91 ExtensionRegistry::Get(browser_context_)->AddObserver(this); |
95 } | 92 } |
96 | 93 |
97 PowerAPI::~PowerAPI() { | 94 PowerAPI::~PowerAPI() { |
98 } | 95 } |
99 | 96 |
100 void PowerAPI::UpdatePowerSaveBlocker() { | 97 void PowerAPI::UpdatePowerSaveBlocker() { |
101 if (extension_levels_.empty()) { | 98 if (extension_levels_.empty()) { |
102 power_save_blocker_.reset(); | 99 power_save_blocker_.reset(); |
103 return; | 100 return; |
104 } | 101 } |
105 | 102 |
106 api::power::Level new_level = api::power::LEVEL_SYSTEM; | 103 api::power::Level new_level = api::power::LEVEL_SYSTEM; |
107 for (ExtensionLevelMap::const_iterator it = extension_levels_.begin(); | 104 for (ExtensionLevelMap::const_iterator it = extension_levels_.begin(); |
108 it != extension_levels_.end(); ++it) { | 105 it != extension_levels_.end(); ++it) { |
109 if (it->second == api::power::LEVEL_DISPLAY) | 106 if (it->second == api::power::LEVEL_DISPLAY) |
110 new_level = it->second; | 107 new_level = it->second; |
111 } | 108 } |
112 | 109 |
113 // If the level changed and we need to create a new blocker, do a swap | 110 // If the level changed and we need to create a new blocker, do a swap |
114 // to ensure that there isn't a brief period where power management is | 111 // to ensure that there isn't a brief period where power management is |
115 // unblocked. | 112 // unblocked. |
116 if (!power_save_blocker_ || new_level != current_level_) { | 113 if (!power_save_blocker_ || new_level != current_level_) { |
117 device::PowerSaveBlocker::PowerSaveBlockerType type = | 114 content::PowerSaveBlocker::PowerSaveBlockerType type = |
118 LevelToPowerSaveBlockerType(new_level); | 115 LevelToPowerSaveBlockerType(new_level); |
119 std::unique_ptr<device::PowerSaveBlocker> new_blocker( | 116 std::unique_ptr<content::PowerSaveBlocker> new_blocker( |
120 create_blocker_function_.Run( | 117 create_blocker_function_.Run(type, |
121 type, device::PowerSaveBlocker::kReasonOther, | 118 content::PowerSaveBlocker::kReasonOther, |
122 kPowerSaveBlockerDescription, | 119 kPowerSaveBlockerDescription)); |
123 content::BrowserThread::GetMessageLoopProxyForThread( | |
124 content::BrowserThread::UI), | |
125 content::BrowserThread::GetMessageLoopProxyForThread( | |
126 content::BrowserThread::FILE))); | |
127 power_save_blocker_.swap(new_blocker); | 120 power_save_blocker_.swap(new_blocker); |
128 current_level_ = new_level; | 121 current_level_ = new_level; |
129 } | 122 } |
130 } | 123 } |
131 | 124 |
132 void PowerAPI::Shutdown() { | 125 void PowerAPI::Shutdown() { |
133 // Unregister here rather than in the d'tor; otherwise this call will recreate | 126 // Unregister here rather than in the d'tor; otherwise this call will recreate |
134 // the already-deleted ExtensionRegistry. | 127 // the already-deleted ExtensionRegistry. |
135 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); | 128 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); |
136 power_save_blocker_.reset(); | 129 power_save_blocker_.reset(); |
137 } | 130 } |
138 | 131 |
139 } // namespace extensions | 132 } // namespace extensions |
OLD | NEW |