Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / power / power_api.h
blob9648e4b7b913d20f1c7cee7e7c0adc20b97b3cbd
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.
5 #ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_H_
6 #define EXTENSIONS_BROWSER_API_POWER_POWER_API_H_
8 #include <map>
9 #include <string>
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_function.h"
16 #include "extensions/browser/extension_registry_observer.h"
17 #include "extensions/common/api/power.h"
19 namespace content {
20 class BrowserContext;
23 namespace extensions {
25 // Implementation of the chrome.power.requestKeepAwake API.
26 class PowerRequestKeepAwakeFunction : public SyncExtensionFunction {
27 public:
28 DECLARE_EXTENSION_FUNCTION("power.requestKeepAwake", POWER_REQUESTKEEPAWAKE)
30 protected:
31 ~PowerRequestKeepAwakeFunction() override {}
33 // ExtensionFunction:
34 bool RunSync() override;
37 // Implementation of the chrome.power.releaseKeepAwake API.
38 class PowerReleaseKeepAwakeFunction : public SyncExtensionFunction {
39 public:
40 DECLARE_EXTENSION_FUNCTION("power.releaseKeepAwake", POWER_RELEASEKEEPAWAKE)
42 protected:
43 ~PowerReleaseKeepAwakeFunction() override {}
45 // ExtensionFunction:
46 bool RunSync() override;
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;
60 static PowerAPI* Get(content::BrowserContext* context);
62 // BrowserContextKeyedAPI implementation.
63 static BrowserContextKeyedAPIFactory<PowerAPI>* GetFactoryInstance();
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, api::power::Level level);
69 // Removes an extension lock for an extension. Calling this for an
70 // extension id without a lock will do nothing.
71 void RemoveRequest(const std::string& extension_id);
73 // Replaces the function that will be called to create PowerSaveBlocker
74 // objects. Passing an empty callback will revert to the default.
75 void SetCreateBlockerFunctionForTesting(CreateBlockerFunction function);
77 // Overridden from extensions::ExtensionRegistryObserver.
78 void OnExtensionUnloaded(content::BrowserContext* browser_context,
79 const Extension* extension,
80 UnloadedExtensionInfo::Reason reason) override;
82 private:
83 friend class BrowserContextKeyedAPIFactory<PowerAPI>;
85 explicit PowerAPI(content::BrowserContext* context);
86 ~PowerAPI() override;
88 // Updates |power_save_blocker_| and |current_level_| after iterating
89 // over |extension_levels_|.
90 void UpdatePowerSaveBlocker();
92 // BrowserContextKeyedAPI implementation.
93 static const char* service_name() { return "PowerAPI"; }
94 static const bool kServiceRedirectedInIncognito = true;
95 static const bool kServiceIsCreatedWithBrowserContext = false;
96 void Shutdown() override;
98 content::BrowserContext* browser_context_;
100 // Function that should be called to create PowerSaveBlocker objects.
101 // Tests can change this to record what would've been done instead of
102 // actually changing the system power-saving settings.
103 CreateBlockerFunction create_blocker_function_;
105 scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
107 // Current level used by |power_save_blocker_|. Meaningless if
108 // |power_save_blocker_| is NULL.
109 api::power::Level current_level_;
111 // Map from extension ID to the corresponding level for each extension
112 // that has an outstanding request.
113 typedef std::map<std::string, api::power::Level> ExtensionLevelMap;
114 ExtensionLevelMap extension_levels_;
116 DISALLOW_COPY_AND_ASSIGN(PowerAPI);
119 } // namespace extensions
121 #endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_H_