Index: chrome/browser/extensions/api/power/power_api_manager.h |
diff --git a/chrome/browser/extensions/api/power/power_api_manager.h b/chrome/browser/extensions/api/power/power_api_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..268acb71bc995e0329b7a688196944d82bdd538c |
--- /dev/null |
+++ b/chrome/browser/extensions/api/power/power_api_manager.h |
@@ -0,0 +1,92 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_EXTENSIONS_API_POWER_POWER_API_MANAGER_H_ |
+#define CHROME_BROWSER_EXTENSIONS_API_POWER_POWER_API_MANAGER_H_ |
+ |
+#include <map> |
+#include <string> |
+ |
+#include "base/callback.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/singleton.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
+#include "content/public/browser/power_save_blocker.h" |
+ |
+namespace extensions { |
+ |
+class PowerApiManager : public content::NotificationObserver { |
+ public: |
+ // Different levels of power-management-overriding. Keep in ascending |
+ // order so that higher-valued levels' behavior subsumes lower-valued |
+ // ones' behavior. |
+ enum Level { |
+ // Prevents the system from sleeping due to user inactivity. |
+ LEVEL_SYSTEM = 0, |
+ |
+ // Prevents the system from dimming the display, turning the display |
+ // off, or sleeping due to user inactivity. |
+ LEVEL_DISPLAY = 1, |
not at google - send to devlin
2013/03/19 21:16:39
if it were an enum, you wouldn't need this definit
Daniel Erat
2013/03/19 23:39:38
Done.
|
+ }; |
+ |
+ typedef base::Callback<scoped_ptr<content::PowerSaveBlocker>( |
+ content::PowerSaveBlocker::PowerSaveBlockerType, |
+ const std::string&)> CreateBlockerFunction; |
+ |
+ static PowerApiManager* GetInstance(); |
+ |
+ void set_create_blocker_function_for_testing( |
+ scoped_ptr<CreateBlockerFunction> function) { |
+ create_blocker_function_.swap(function); |
+ } |
+ |
+ // Adds an extension lock at |level| for |extension_id|, replacing the |
+ // extension's existing lock, if any. |
+ void AddRequest(const std::string& extension_id, Level level); |
+ |
+ // Removes an extension lock for an extension. Calling this for an |
+ // extension id without a lock will do nothing. |
+ void RemoveRequest(const std::string& extension_id); |
+ |
+ // Overridden from content::NotificationObserver. |
+ virtual void Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) OVERRIDE; |
+ |
+ private: |
+ friend struct DefaultSingletonTraits<PowerApiManager>; |
+ |
+ PowerApiManager(); |
+ virtual ~PowerApiManager(); |
+ |
+ // Updates |power_save_blocker_| and |current_level_| after iterating |
+ // over |extension_levels_|. |
+ void UpdatePowerSaveBlocker(); |
+ |
+ content::NotificationRegistrar registrar_; |
+ |
+ // Alternate function that should be called to create PowerSaveBlocker |
+ // objects. Tests can use this to override what would've been done |
+ // instead of actually changing the system power-saving settings. If |
+ // NULL, content::PowerSaveBlocker::Create() is used. |
+ scoped_ptr<CreateBlockerFunction> create_blocker_function_; |
+ |
+ scoped_ptr<content::PowerSaveBlocker> power_save_blocker_; |
+ |
+ // Current level used by |power_save_blocker_|. Meaningless if |
+ // |power_save_blocker_| is NULL. |
+ Level current_level_; |
+ |
+ // Make from extension ID to the corresponding level for each extension |
+ // that has an outstanding request. |
+ typedef std::map<std::string, Level> ExtensionLevelMap; |
+ ExtensionLevelMap extension_levels_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(PowerApiManager); |
+}; |
+ |
+} // namespace extensions |
+ |
+#endif // CHROME_BROWSER_EXTENSIONS_API_POWER_POWER_API_MANAGER_H_ |