File: power_observer_helper.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (74 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_POWER_OBSERVER_HELPER_H_
#define MEDIA_AUDIO_POWER_OBSERVER_HELPER_H_

#include "base/functional/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "base/power_monitor/power_observer.h"
#include "base/task/sequenced_task_runner.h"
#include "media/base/media_export.h"

namespace media {

// Helper class that implements PowerSuspendObserver and handles threading. A
// task runner is given, on which suspend and resume notification callbacks are
// run. It also provides a function to check if we are suspending on the task
// runner.
class MEDIA_EXPORT PowerObserverHelper : public base::PowerSuspendObserver {
 public:
  PowerObserverHelper(scoped_refptr<base::SequencedTaskRunner> task_runner,
                      base::RepeatingClosure suspend_callback,
                      base::RepeatingClosure resume_callback);

  PowerObserverHelper(const PowerObserverHelper&) = delete;
  PowerObserverHelper& operator=(const PowerObserverHelper&) = delete;

  ~PowerObserverHelper() override;

  // Must be called on |task_runner|.
  virtual bool IsSuspending() const;

 protected:
  base::SequencedTaskRunner* TaskRunnerForTesting() const {
    return task_runner_.get();
  }

  base::RepeatingClosure* SuspendCallbackForTesting() {
    return &suspend_callback_;
  }

  base::RepeatingClosure* ResumeCallbackForTesting() {
    return &resume_callback_;
  }

 private:
  FRIEND_TEST_ALL_PREFIXES(PowerObserverHelperTest,
                           SuspendAndResumeNotificationsTwice);
  FRIEND_TEST_ALL_PREFIXES(PowerObserverHelperTest,
                           TwoSuspendAndTwoResumeNotifications);

  // The task runner on which |is_suspending_| should live and the callbacks
  // should be run on.
  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  // Suspend and resume callbacks. Run on |task_runner_|.
  base::RepeatingClosure suspend_callback_;
  base::RepeatingClosure resume_callback_;

  // base::PowerSuspendObserver implementation.
  void OnSuspend() override;
  void OnResume() override;

  // Flag if we are suspending.
  bool is_suspending_ = false;

  base::WeakPtrFactory<PowerObserverHelper> weak_factory_{this};
};

}  // namespace media

#endif  // MEDIA_AUDIO_POWER_OBSERVER_HELPER_H_