Update To 11.40.268.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   void OnExtensionUnloaded(content::BrowserContext* browser_context,
55                            const Extension* extension,
56                            UnloadedExtensionInfo::Reason reason) override;
57
58  private:
59   friend class BrowserContextKeyedAPIFactory<PowerApiManager>;
60
61   explicit PowerApiManager(content::BrowserContext* context);
62   ~PowerApiManager() override;
63
64   // Updates |power_save_blocker_| and |current_level_| after iterating
65   // over |extension_levels_|.
66   void UpdatePowerSaveBlocker();
67
68   // BrowserContextKeyedAPI implementation.
69   static const char* service_name() { return "PowerApiManager"; }
70   static const bool kServiceRedirectedInIncognito = true;
71   static const bool kServiceIsCreatedWithBrowserContext = false;
72   void Shutdown() override;
73
74   content::BrowserContext* browser_context_;
75
76   // Function that should be called to create PowerSaveBlocker objects.
77   // Tests can change this to record what would've been done instead of
78   // actually changing the system power-saving settings.
79   CreateBlockerFunction create_blocker_function_;
80
81   scoped_ptr<content::PowerSaveBlocker> power_save_blocker_;
82
83   // Current level used by |power_save_blocker_|.  Meaningless if
84   // |power_save_blocker_| is NULL.
85   core_api::power::Level current_level_;
86
87   // Map from extension ID to the corresponding level for each extension
88   // that has an outstanding request.
89   typedef std::map<std::string, core_api::power::Level> ExtensionLevelMap;
90   ExtensionLevelMap extension_levels_;
91
92   DISALLOW_COPY_AND_ASSIGN(PowerApiManager);
93 };
94
95 }  // namespace extensions
96
97 #endif  // EXTENSIONS_BROWSER_API_POWER_POWER_API_MANAGER_H_