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/browser_thread.h" |
10 #include "device/power_save_blocker/power_save_blocker.h" | 10 #include "device/power_save_blocker/power_save_blocker.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 case api::power::LEVEL_NONE: | 27 case api::power::LEVEL_NONE: |
28 return device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; | 28 return device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; |
29 } | 29 } |
30 NOTREACHED() << "Unhandled level " << level; | 30 NOTREACHED() << "Unhandled level " << level; |
31 return device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; | 31 return device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep; |
32 } | 32 } |
33 | 33 |
34 base::LazyInstance<BrowserContextKeyedAPIFactory<PowerAPI>> g_factory = | 34 base::LazyInstance<BrowserContextKeyedAPIFactory<PowerAPI>> g_factory = |
35 LAZY_INSTANCE_INITIALIZER; | 35 LAZY_INSTANCE_INITIALIZER; |
36 | 36 |
| 37 std::unique_ptr<device::PowerSaveBlocker> CreatePowerSaveBlocker( |
| 38 device::PowerSaveBlocker::PowerSaveBlockerType type, |
| 39 device::PowerSaveBlocker::Reason reason, |
| 40 const std::string& description, |
| 41 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 42 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
| 43 return std::unique_ptr<device::PowerSaveBlocker>(new device::PowerSaveBlocker( |
| 44 type, reason, description, ui_task_runner, file_task_runner)); |
| 45 } |
| 46 |
37 } // namespace | 47 } // namespace |
38 | 48 |
39 bool PowerRequestKeepAwakeFunction::RunSync() { | 49 bool PowerRequestKeepAwakeFunction::RunSync() { |
40 std::unique_ptr<api::power::RequestKeepAwake::Params> params( | 50 std::unique_ptr<api::power::RequestKeepAwake::Params> params( |
41 api::power::RequestKeepAwake::Params::Create(*args_)); | 51 api::power::RequestKeepAwake::Params::Create(*args_)); |
42 EXTENSION_FUNCTION_VALIDATE(params); | 52 EXTENSION_FUNCTION_VALIDATE(params); |
43 EXTENSION_FUNCTION_VALIDATE(params->level != api::power::LEVEL_NONE); | 53 EXTENSION_FUNCTION_VALIDATE(params->level != api::power::LEVEL_NONE); |
44 PowerAPI::Get(browser_context())->AddRequest(extension_id(), params->level); | 54 PowerAPI::Get(browser_context())->AddRequest(extension_id(), params->level); |
45 return true; | 55 return true; |
46 } | 56 } |
(...skipping 20 matching lines...) Expand all Loading... |
67 } | 77 } |
68 | 78 |
69 void PowerAPI::RemoveRequest(const std::string& extension_id) { | 79 void PowerAPI::RemoveRequest(const std::string& extension_id) { |
70 extension_levels_.erase(extension_id); | 80 extension_levels_.erase(extension_id); |
71 UpdatePowerSaveBlocker(); | 81 UpdatePowerSaveBlocker(); |
72 } | 82 } |
73 | 83 |
74 void PowerAPI::SetCreateBlockerFunctionForTesting( | 84 void PowerAPI::SetCreateBlockerFunctionForTesting( |
75 CreateBlockerFunction function) { | 85 CreateBlockerFunction function) { |
76 create_blocker_function_ = | 86 create_blocker_function_ = |
77 !function.is_null() | 87 !function.is_null() ? function : base::Bind(&CreatePowerSaveBlocker); |
78 ? function | |
79 : base::Bind(&device::PowerSaveBlocker::CreateWithTaskRunners); | |
80 } | 88 } |
81 | 89 |
82 void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | 90 void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
83 const Extension* extension, | 91 const Extension* extension, |
84 UnloadedExtensionInfo::Reason reason) { | 92 UnloadedExtensionInfo::Reason reason) { |
85 RemoveRequest(extension->id()); | 93 RemoveRequest(extension->id()); |
86 UpdatePowerSaveBlocker(); | 94 UpdatePowerSaveBlocker(); |
87 } | 95 } |
88 | 96 |
89 PowerAPI::PowerAPI(content::BrowserContext* context) | 97 PowerAPI::PowerAPI(content::BrowserContext* context) |
90 : browser_context_(context), | 98 : browser_context_(context), |
91 create_blocker_function_( | 99 create_blocker_function_(base::Bind(&CreatePowerSaveBlocker)), |
92 base::Bind(&device::PowerSaveBlocker::CreateWithTaskRunners)), | |
93 current_level_(api::power::LEVEL_SYSTEM) { | 100 current_level_(api::power::LEVEL_SYSTEM) { |
94 ExtensionRegistry::Get(browser_context_)->AddObserver(this); | 101 ExtensionRegistry::Get(browser_context_)->AddObserver(this); |
95 } | 102 } |
96 | 103 |
97 PowerAPI::~PowerAPI() { | 104 PowerAPI::~PowerAPI() { |
98 } | 105 } |
99 | 106 |
100 void PowerAPI::UpdatePowerSaveBlocker() { | 107 void PowerAPI::UpdatePowerSaveBlocker() { |
101 if (extension_levels_.empty()) { | 108 if (extension_levels_.empty()) { |
102 power_save_blocker_.reset(); | 109 power_save_blocker_.reset(); |
(...skipping 27 matching lines...) Expand all Loading... |
130 } | 137 } |
131 | 138 |
132 void PowerAPI::Shutdown() { | 139 void PowerAPI::Shutdown() { |
133 // Unregister here rather than in the d'tor; otherwise this call will recreate | 140 // Unregister here rather than in the d'tor; otherwise this call will recreate |
134 // the already-deleted ExtensionRegistry. | 141 // the already-deleted ExtensionRegistry. |
135 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); | 142 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); |
136 power_save_blocker_.reset(); | 143 power_save_blocker_.reset(); |
137 } | 144 } |
138 | 145 |
139 } // namespace extensions | 146 } // namespace extensions |
OLD | NEW |