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

media / audio / power_observer_helper.h [blame]

// 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_