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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_POWER_POWER_API_MANAGER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_POWER_POWER_API_MANAGER_H_
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "chrome/common/extensions/api/power.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/power_save_blocker.h"
19 namespace extensions
{
21 class PowerApiManager
: public content::NotificationObserver
{
23 typedef base::Callback
<scoped_ptr
<content::PowerSaveBlocker
>(
24 content::PowerSaveBlocker::PowerSaveBlockerType
,
25 const std::string
&)> CreateBlockerFunction
;
27 static PowerApiManager
* GetInstance();
29 // Adds an extension lock at |level| for |extension_id|, replacing the
30 // extension's existing lock, if any.
31 void AddRequest(const std::string
& extension_id
, api::power::Level level
);
33 // Removes an extension lock for an extension. Calling this for an
34 // extension id without a lock will do nothing.
35 void RemoveRequest(const std::string
& extension_id
);
37 // Replaces the function that will be called to create PowerSaveBlocker
38 // objects. Passing an empty callback will revert to the default.
39 void SetCreateBlockerFunctionForTesting(CreateBlockerFunction function
);
41 // Overridden from content::NotificationObserver.
42 virtual void Observe(int type
,
43 const content::NotificationSource
& source
,
44 const content::NotificationDetails
& details
) OVERRIDE
;
47 friend struct DefaultSingletonTraits
<PowerApiManager
>;
50 virtual ~PowerApiManager();
52 // Updates |power_save_blocker_| and |current_level_| after iterating
53 // over |extension_levels_|.
54 void UpdatePowerSaveBlocker();
56 content::NotificationRegistrar registrar_
;
58 // Function that should be called to create PowerSaveBlocker objects.
59 // Tests can change this to record what would've been done instead of
60 // actually changing the system power-saving settings.
61 CreateBlockerFunction create_blocker_function_
;
63 scoped_ptr
<content::PowerSaveBlocker
> power_save_blocker_
;
65 // Current level used by |power_save_blocker_|. Meaningless if
66 // |power_save_blocker_| is NULL.
67 api::power::Level current_level_
;
69 // Map from extension ID to the corresponding level for each extension
70 // that has an outstanding request.
71 typedef std::map
<std::string
, api::power::Level
> ExtensionLevelMap
;
72 ExtensionLevelMap extension_levels_
;
74 DISALLOW_COPY_AND_ASSIGN(PowerApiManager
);
77 } // namespace extensions
79 #endif // CHROME_BROWSER_EXTENSIONS_API_POWER_POWER_API_MANAGER_H_