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_MANAGER_H_
6 #define EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_
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"
22 namespace extensions {
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 {
31 typedef base::Callback<scoped_ptr<content::PowerSaveBlocker>(
32 content::PowerSaveBlocker::PowerSaveBlockerType,
33 const std::string&)> CreateBlockerFunction;
35 static PowerApiManager* Get(content::BrowserContext* context);
37 // BrowserContextKeyedAPI implementation.
38 static BrowserContextKeyedAPIFactory<PowerApiManager>* GetFactoryInstance();
40 // Adds an extension lock at |level| for |extension_id|, replacing the
41 // extension's existing lock, if any.
42 void AddRequest(const std::string& extension_id,
43 core_api::power::Level level);
45 // Removes an extension lock for an extension. Calling this for an
46 // extension id without a lock will do nothing.
47 void RemoveRequest(const std::string& extension_id);
49 // Replaces the function that will be called to create PowerSaveBlocker
50 // objects. Passing an empty callback will revert to the default.
51 void SetCreateBlockerFunctionForTesting(CreateBlockerFunction function);
53 // Overridden from extensions::ExtensionRegistryObserver.
54 virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
55 const Extension* extension,
56 UnloadedExtensionInfo::Reason reason)
60 friend class BrowserContextKeyedAPIFactory<PowerApiManager>;
62 explicit PowerApiManager(content::BrowserContext* context);
63 virtual ~PowerApiManager();
65 // Updates |power_save_blocker_| and |current_level_| after iterating
66 // over |extension_levels_|.
67 void UpdatePowerSaveBlocker();
69 // BrowserContextKeyedAPI implementation.
70 static const char* service_name() { return "PowerApiManager"; }
71 static const bool kServiceRedirectedInIncognito = true;
72 static const bool kServiceIsCreatedWithBrowserContext = false;
73 virtual void Shutdown() OVERRIDE;
75 content::BrowserContext* browser_context_;
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_;
82 scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
84 // Current level used by |power_save_blocker_|. Meaningless if
85 // |power_save_blocker_| is NULL.
86 core_api::power::Level current_level_;
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_;
93 DISALLOW_COPY_AND_ASSIGN(PowerApiManager);
96 } // namespace extensions
98 #endif // EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_