Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / power / power_api_manager.h
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.
4
5 #ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_
6 #define EXTENSIONS_BROWSER_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 "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"
17
18 namespace content {
19 class BrowserContext;
20 }
21
22 namespace extensions {
23
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 {
30  public:
31   typedef base::Callback<scoped_ptr<content::PowerSaveBlocker>(
32       content::PowerSaveBlocker::PowerSaveBlockerType,
33       const std::string&)> CreateBlockerFunction;
34
35   static PowerApiManager* Get(content::BrowserContext* context);
36
37   // BrowserContextKeyedAPI implementation.
38   static BrowserContextKeyedAPIFactory<PowerApiManager>* GetFactoryInstance();
39
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);
44
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);
48
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);
52
53   // Overridden from extensions::ExtensionRegistryObserver.
54   virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
55                                    const Extension* extension,
56                                    UnloadedExtensionInfo::Reason reason)
57       OVERRIDE;
58
59  private:
60   friend class BrowserContextKeyedAPIFactory<PowerApiManager>;
61
62   explicit PowerApiManager(content::BrowserContext* context);
63   virtual ~PowerApiManager();
64
65   // Updates |power_save_blocker_| and |current_level_| after iterating
66   // over |extension_levels_|.
67   void UpdatePowerSaveBlocker();
68
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;
74
75   content::BrowserContext* browser_context_;
76
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_;
81
82   scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
83
84   // Current level used by |power_save_blocker_|.  Meaningless if
85   // |power_save_blocker_| is NULL.
86   core_api::power::Level current_level_;
87
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_;
92
93   DISALLOW_COPY_AND_ASSIGN(PowerApiManager);
94 };
95
96 }  // namespace extensions
97
98 #endif  // EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_