1 // Copyright (c) 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 #include "content/shell/common/power_monitor_test_impl.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "mojo/public/cpp/bindings/pending_receiver.h"
11 #include "mojo/public/cpp/bindings/self_owned_receiver.h"
12 
13 namespace content {
14 
15 // static
MakeSelfOwnedReceiver(mojo::PendingReceiver<mojom::PowerMonitorTest> receiver)16 void PowerMonitorTestImpl::MakeSelfOwnedReceiver(
17     mojo::PendingReceiver<mojom::PowerMonitorTest> receiver) {
18   mojo::MakeSelfOwnedReceiver(std::make_unique<PowerMonitorTestImpl>(),
19                               std::move(receiver));
20 }
21 
PowerMonitorTestImpl()22 PowerMonitorTestImpl::PowerMonitorTestImpl() {
23   base::PowerMonitor::AddObserver(this);
24 }
25 
~PowerMonitorTestImpl()26 PowerMonitorTestImpl::~PowerMonitorTestImpl() {
27   base::PowerMonitor::RemoveObserver(this);
28 }
29 
QueryNextState(QueryNextStateCallback callback)30 void PowerMonitorTestImpl::QueryNextState(QueryNextStateCallback callback) {
31   // Do not allow overlapping call.
32   DCHECK(callback_.is_null());
33   callback_ = std::move(callback);
34 
35   if (need_to_report_)
36     ReportState();
37 }
38 
OnPowerStateChange(bool on_battery_power)39 void PowerMonitorTestImpl::OnPowerStateChange(bool on_battery_power) {
40   on_battery_power_ = on_battery_power;
41   need_to_report_ = true;
42 
43   if (!callback_.is_null())
44     ReportState();
45 }
46 
ReportState()47 void PowerMonitorTestImpl::ReportState() {
48   std::move(callback_).Run(on_battery_power_);
49   need_to_report_ = false;
50 }
51 
52 }  // namespace content
53