OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_POWER_SAVE_BLOCKER_POWER_SAVE_BLOCKER_H_ | 5 #ifndef DEVICE_POWER_SAVE_BLOCKER_POWER_SAVE_BLOCKER_H_ |
6 #define DEVICE_POWER_SAVE_BLOCKER_POWER_SAVE_BLOCKER_H_ | 6 #define DEVICE_POWER_SAVE_BLOCKER_POWER_SAVE_BLOCKER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
12 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "device/power_save_blocker/power_save_blocker_export.h" | 15 #include "device/power_save_blocker/power_save_blocker_export.h" |
15 | 16 |
| 17 #if defined(OS_ANDROID) |
| 18 #include "ui/android/view_android.h" |
| 19 #endif // OS_ANDROID |
| 20 |
16 namespace device { | 21 namespace device { |
17 | 22 |
18 // A RAII-style class to block the system from entering low-power (sleep) mode. | 23 // A RAII-style class to block the system from entering low-power (sleep) mode. |
19 // This class is thread-safe; it may be constructed and deleted on any thread. | 24 // This class is thread-safe; it may be constructed and deleted on any thread. |
20 class DEVICE_POWER_SAVE_BLOCKER_EXPORT PowerSaveBlocker { | 25 class DEVICE_POWER_SAVE_BLOCKER_EXPORT PowerSaveBlocker { |
21 public: | 26 public: |
22 enum PowerSaveBlockerType { | 27 enum PowerSaveBlockerType { |
23 // Prevent the application from being suspended. On some platforms, apps may | 28 // Prevent the application from being suspended. On some platforms, apps may |
24 // be suspended when they are not visible to the user. This type of block | 29 // be suspended when they are not visible to the user. This type of block |
25 // requests that the app continue to run in that case, and on all platforms | 30 // requests that the app continue to run in that case, and on all platforms |
(...skipping 11 matching lines...) Expand all Loading... |
37 // Reasons why power-saving features may be blocked. | 42 // Reasons why power-saving features may be blocked. |
38 enum Reason { | 43 enum Reason { |
39 // Audio is being played. | 44 // Audio is being played. |
40 kReasonAudioPlayback, | 45 kReasonAudioPlayback, |
41 // Video is being played. | 46 // Video is being played. |
42 kReasonVideoPlayback, | 47 kReasonVideoPlayback, |
43 // Power-saving is blocked for some other reason. | 48 // Power-saving is blocked for some other reason. |
44 kReasonOther, | 49 kReasonOther, |
45 }; | 50 }; |
46 | 51 |
47 virtual ~PowerSaveBlocker() = 0; | |
48 | |
49 // Pass in the type of power save blocking desired. If multiple types of | 52 // Pass in the type of power save blocking desired. If multiple types of |
50 // blocking are desired, instantiate one PowerSaveBlocker for each type. | 53 // blocking are desired, instantiate one PowerSaveBlocker for each type. |
51 // |reason| and |description| (a more-verbose, human-readable justification of | 54 // |reason| and |description| (a more-verbose, human-readable justification of |
52 // the blocking) may be provided to the underlying system APIs on some | 55 // the blocking) may be provided to the underlying system APIs on some |
53 // platforms. | 56 // platforms. |
54 static std::unique_ptr<PowerSaveBlocker> CreateWithTaskRunners( | 57 PowerSaveBlocker( |
55 PowerSaveBlockerType type, | 58 PowerSaveBlockerType type, |
56 Reason reason, | 59 Reason reason, |
57 const std::string& description, | 60 const std::string& description, |
58 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 61 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
59 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 62 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner); |
| 63 virtual ~PowerSaveBlocker(); |
| 64 |
| 65 #if defined(OS_ANDROID) |
| 66 // On Android, the kPowerSaveBlockPreventDisplaySleep type of |
| 67 // PowerSaveBlocker should associated with a View, so the blocker can be |
| 68 // removed by the platform. |
| 69 DEVICE_POWER_SAVE_BLOCKER_EXPORT void InitDisplaySleepBlocker( |
| 70 const base::WeakPtr<ui::ViewAndroid>& view_android); |
| 71 #endif |
| 72 |
| 73 private: |
| 74 class Delegate; |
| 75 |
| 76 // Implementations of this class may need a second object with different |
| 77 // lifetime than the RAII container, or additional storage. This member is |
| 78 // here for that purpose. If not used, just define the class as an empty |
| 79 // RefCounted (or RefCountedThreadSafe) like so to make it compile: |
| 80 // class PowerSaveBlocker::Delegate |
| 81 // : public base::RefCounted<PowerSaveBlocker::Delegate> { |
| 82 // private: |
| 83 // friend class base::RefCounted<Delegate>; |
| 84 // ~Delegate() {} |
| 85 // }; |
| 86 scoped_refptr<Delegate> delegate_; |
| 87 |
| 88 #if defined(USE_X11) |
| 89 // Since display sleep prevention also implies system suspend prevention, for |
| 90 // the Linux FreeDesktop API case, there needs to be a second delegate to |
| 91 // block system suspend when screen saver / display sleep is blocked. |
| 92 scoped_refptr<Delegate> freedesktop_suspend_delegate_; |
| 93 #endif |
| 94 |
| 95 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 96 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlocker); |
60 }; | 99 }; |
61 | 100 |
62 } // namespace device | 101 } // namespace device |
63 | 102 |
64 #endif // DEVICE_POWER_SAVE_BLOCKER_POWER_SAVE_BLOCKER_H_ | 103 #endif // DEVICE_POWER_SAVE_BLOCKER_POWER_SAVE_BLOCKER_H_ |
OLD | NEW |