Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / power / power_api.h
blob7a4a752d2b71951896d09d6b25143933336b94e4
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,
68 core_api::power::Level level);
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);
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);
78 // Overridden from extensions::ExtensionRegistryObserver.
79 void OnExtensionUnloaded(content::BrowserContext* browser_context,
80 const Extension* extension,
81 UnloadedExtensionInfo::Reason reason) override;
83 private:
84 friend class BrowserContextKeyedAPIFactory<PowerAPI>;
86 explicit PowerAPI(content::BrowserContext* context);
87 ~PowerAPI() override;
89 // Updates |power_save_blocker_| and |current_level_| after iterating
90 // over |extension_levels_|.
91 void UpdatePowerSaveBlocker();
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;
99 content::BrowserContext* browser_context_;
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_;
106 scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
108 // Current level used by |power_save_blocker_|. Meaningless if
109 // |power_save_blocker_| is NULL.
110 core_api::power::Level current_level_;
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_;
117 DISALLOW_COPY_AND_ASSIGN(PowerAPI);
120 } // namespace extensions
122 #endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_H_