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