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