| 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 "extensions/browser/extension_registry.h" | 10 #include "extensions/browser/extension_registry.h" |
| 10 #include "extensions/common/api/power.h" | 11 #include "extensions/common/api/power.h" |
| 11 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
| 12 | 13 |
| 13 namespace extensions { | 14 namespace extensions { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 const char kPowerSaveBlockerDescription[] = "extension"; | 18 const char kPowerSaveBlockerDescription[] = "extension"; |
| 18 | 19 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 UpdatePowerSaveBlocker(); | 65 UpdatePowerSaveBlocker(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void PowerAPI::RemoveRequest(const std::string& extension_id) { | 68 void PowerAPI::RemoveRequest(const std::string& extension_id) { |
| 68 extension_levels_.erase(extension_id); | 69 extension_levels_.erase(extension_id); |
| 69 UpdatePowerSaveBlocker(); | 70 UpdatePowerSaveBlocker(); |
| 70 } | 71 } |
| 71 | 72 |
| 72 void PowerAPI::SetCreateBlockerFunctionForTesting( | 73 void PowerAPI::SetCreateBlockerFunctionForTesting( |
| 73 CreateBlockerFunction function) { | 74 CreateBlockerFunction function) { |
| 74 create_blocker_function_ = | 75 create_blocker_function_ = !function.is_null() |
| 75 !function.is_null() ? function | 76 ? function |
| 76 : base::Bind(&content::PowerSaveBlocker::Create); | 77 : base::Bind(&content::CreatePowerSaveBlocker); |
| 77 } | 78 } |
| 78 | 79 |
| 79 void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, | 80 void PowerAPI::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 80 const Extension* extension, | 81 const Extension* extension, |
| 81 UnloadedExtensionInfo::Reason reason) { | 82 UnloadedExtensionInfo::Reason reason) { |
| 82 RemoveRequest(extension->id()); | 83 RemoveRequest(extension->id()); |
| 83 UpdatePowerSaveBlocker(); | 84 UpdatePowerSaveBlocker(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 PowerAPI::PowerAPI(content::BrowserContext* context) | 87 PowerAPI::PowerAPI(content::BrowserContext* context) |
| 87 : browser_context_(context), | 88 : browser_context_(context), |
| 88 create_blocker_function_(base::Bind(&content::PowerSaveBlocker::Create)), | 89 create_blocker_function_(base::Bind(&content::CreatePowerSaveBlocker)), |
| 89 current_level_(api::power::LEVEL_SYSTEM) { | 90 current_level_(api::power::LEVEL_SYSTEM) { |
| 90 ExtensionRegistry::Get(browser_context_)->AddObserver(this); | 91 ExtensionRegistry::Get(browser_context_)->AddObserver(this); |
| 91 } | 92 } |
| 92 | 93 |
| 93 PowerAPI::~PowerAPI() { | 94 PowerAPI::~PowerAPI() { |
| 94 } | 95 } |
| 95 | 96 |
| 96 void PowerAPI::UpdatePowerSaveBlocker() { | 97 void PowerAPI::UpdatePowerSaveBlocker() { |
| 97 if (extension_levels_.empty()) { | 98 if (extension_levels_.empty()) { |
| 98 power_save_blocker_.reset(); | 99 power_save_blocker_.reset(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 122 } | 123 } |
| 123 | 124 |
| 124 void PowerAPI::Shutdown() { | 125 void PowerAPI::Shutdown() { |
| 125 // 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 |
| 126 // the already-deleted ExtensionRegistry. | 127 // the already-deleted ExtensionRegistry. |
| 127 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); | 128 ExtensionRegistry::Get(browser_context_)->RemoveObserver(this); |
| 128 power_save_blocker_.reset(); | 129 power_save_blocker_.reset(); |
| 129 } | 130 } |
| 130 | 131 |
| 131 } // namespace extensions | 132 } // namespace extensions |
| OLD | NEW |