| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_ | |
| 6 #define EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "content/public/browser/power_save_blocker.h" | |
| 14 #include "extensions/browser/browser_context_keyed_api_factory.h" | |
| 15 #include "extensions/browser/extension_registry_observer.h" | |
| 16 #include "extensions/common/api/power.h" | |
| 17 | |
| 18 namespace content { | |
| 19 class BrowserContext; | |
| 20 } | |
| 21 | |
| 22 namespace extensions { | |
| 23 | |
| 24 // Handles calls made via the chrome.power API. There is a separate instance of | |
| 25 // this class for each profile, as requests are tracked by extension ID, but a | |
| 26 // regular and incognito profile will share the same instance. | |
| 27 // TODO(derat): Move this to power_api.h and rename it to PowerApi. | |
| 28 class PowerApiManager : public BrowserContextKeyedAPI, | |
| 29 public extensions::ExtensionRegistryObserver { | |
| 30 public: | |
| 31 typedef base::Callback<scoped_ptr<content::PowerSaveBlocker>( | |
| 32 content::PowerSaveBlocker::PowerSaveBlockerType, | |
| 33 content::PowerSaveBlocker::Reason, | |
| 34 const std::string&)> CreateBlockerFunction; | |
| 35 | |
| 36 static PowerApiManager* Get(content::BrowserContext* context); | |
| 37 | |
| 38 // BrowserContextKeyedAPI implementation. | |
| 39 static BrowserContextKeyedAPIFactory<PowerApiManager>* GetFactoryInstance(); | |
| 40 | |
| 41 // Adds an extension lock at |level| for |extension_id|, replacing the | |
| 42 // extension's existing lock, if any. | |
| 43 void AddRequest(const std::string& extension_id, | |
| 44 core_api::power::Level level); | |
| 45 | |
| 46 // Removes an extension lock for an extension. Calling this for an | |
| 47 // extension id without a lock will do nothing. | |
| 48 void RemoveRequest(const std::string& extension_id); | |
| 49 | |
| 50 // Replaces the function that will be called to create PowerSaveBlocker | |
| 51 // objects. Passing an empty callback will revert to the default. | |
| 52 void SetCreateBlockerFunctionForTesting(CreateBlockerFunction function); | |
| 53 | |
| 54 // Overridden from extensions::ExtensionRegistryObserver. | |
| 55 void OnExtensionUnloaded(content::BrowserContext* browser_context, | |
| 56 const Extension* extension, | |
| 57 UnloadedExtensionInfo::Reason reason) override; | |
| 58 | |
| 59 private: | |
| 60 friend class BrowserContextKeyedAPIFactory<PowerApiManager>; | |
| 61 | |
| 62 explicit PowerApiManager(content::BrowserContext* context); | |
| 63 ~PowerApiManager() override; | |
| 64 | |
| 65 // Updates |power_save_blocker_| and |current_level_| after iterating | |
| 66 // over |extension_levels_|. | |
| 67 void UpdatePowerSaveBlocker(); | |
| 68 | |
| 69 // BrowserContextKeyedAPI implementation. | |
| 70 static const char* service_name() { return "PowerApiManager"; } | |
| 71 static const bool kServiceRedirectedInIncognito = true; | |
| 72 static const bool kServiceIsCreatedWithBrowserContext = false; | |
| 73 void Shutdown() override; | |
| 74 | |
| 75 content::BrowserContext* browser_context_; | |
| 76 | |
| 77 // Function that should be called to create PowerSaveBlocker objects. | |
| 78 // Tests can change this to record what would've been done instead of | |
| 79 // actually changing the system power-saving settings. | |
| 80 CreateBlockerFunction create_blocker_function_; | |
| 81 | |
| 82 scoped_ptr<content::PowerSaveBlocker> power_save_blocker_; | |
| 83 | |
| 84 // Current level used by |power_save_blocker_|. Meaningless if | |
| 85 // |power_save_blocker_| is NULL. | |
| 86 core_api::power::Level current_level_; | |
| 87 | |
| 88 // Map from extension ID to the corresponding level for each extension | |
| 89 // that has an outstanding request. | |
| 90 typedef std::map<std::string, core_api::power::Level> ExtensionLevelMap; | |
| 91 ExtensionLevelMap extension_levels_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(PowerApiManager); | |
| 94 }; | |
| 95 | |
| 96 } // namespace extensions | |
| 97 | |
| 98 #endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_ | |
| OLD | NEW |