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