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