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