1 // Copyright 2017 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 MEDIA_AUDIO_POWER_OBSERVER_HELPER_H_ 6 #define MEDIA_AUDIO_POWER_OBSERVER_HELPER_H_ 7 8 #include "base/callback_forward.h" 9 #include "base/gtest_prod_util.h" 10 #include "base/macros.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/power_monitor/power_observer.h" 13 #include "base/sequenced_task_runner.h" 14 #include "media/base/media_export.h" 15 16 namespace media { 17 18 // Helper class that implements PowerObserver and handles threading. A task 19 // runner is given, on which suspend and resume notification callbacks are run. 20 // It also provides a function to check if we are suspending on the task runner. 21 // Note that on Linux suspend/resume information is not supported. 22 class MEDIA_EXPORT PowerObserverHelper : public base::PowerObserver { 23 public: 24 PowerObserverHelper(scoped_refptr<base::SequencedTaskRunner> task_runner, 25 base::RepeatingClosure suspend_callback, 26 base::RepeatingClosure resume_callback); 27 28 ~PowerObserverHelper() override; 29 30 // Must be called on |task_runner|. 31 virtual bool IsSuspending() const; 32 33 protected: TaskRunnerForTesting()34 base::SequencedTaskRunner* TaskRunnerForTesting() const { 35 return task_runner_.get(); 36 } 37 SuspendCallbackForTesting()38 base::RepeatingClosure* SuspendCallbackForTesting() { 39 return &suspend_callback_; 40 } 41 ResumeCallbackForTesting()42 base::RepeatingClosure* ResumeCallbackForTesting() { 43 return &resume_callback_; 44 } 45 46 private: 47 FRIEND_TEST_ALL_PREFIXES(PowerObserverHelperTest, 48 SuspendAndResumeNotificationsTwice); 49 FRIEND_TEST_ALL_PREFIXES(PowerObserverHelperTest, 50 TwoSuspendAndTwoResumeNotifications); 51 52 // The task runner on which |is_suspending_| should live and the callbacks 53 // should be run on. 54 scoped_refptr<base::SequencedTaskRunner> task_runner_; 55 56 // Suspend and resume callbacks. Run on |task_runner_|. 57 base::RepeatingClosure suspend_callback_; 58 base::RepeatingClosure resume_callback_; 59 60 // base::PowerObserver implementation. 61 void OnSuspend() override; 62 void OnResume() override; 63 64 // Flag if we are suspending. 65 bool is_suspending_ = false; 66 67 base::WeakPtrFactory<PowerObserverHelper> weak_factory_{this}; 68 69 DISALLOW_COPY_AND_ASSIGN(PowerObserverHelper); 70 }; 71 72 } // namespace media 73 74 #endif // MEDIA_AUDIO_POWER_OBSERVER_HELPER_H_ 75