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