forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_power_manager_client.cc
212 lines (166 loc) · 6.62 KB
/
fake_power_manager_client.cc
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/dbus/fake_power_manager_client.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
namespace chromeos {
namespace {
// Minimum power for a USB power source to be classified as AC.
constexpr double kUsbMinAcWatts = 24;
}
FakePowerManagerClient::FakePowerManagerClient() : weak_ptr_factory_(this) {}
FakePowerManagerClient::~FakePowerManagerClient() = default;
void FakePowerManagerClient::Init(dbus::Bus* bus) {
props_.set_battery_percent(50);
props_.set_is_calculating_battery_time(false);
props_.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
props_.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
props_.set_battery_time_to_full_sec(0);
props_.set_battery_time_to_empty_sec(18000);
}
void FakePowerManagerClient::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void FakePowerManagerClient::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
bool FakePowerManagerClient::HasObserver(const Observer* observer) const {
return observers_.HasObserver(observer);
}
void FakePowerManagerClient::SetRenderProcessManagerDelegate(
base::WeakPtr<RenderProcessManagerDelegate> delegate) {
render_process_manager_delegate_ = delegate;
}
void FakePowerManagerClient::DecreaseScreenBrightness(bool allow_off) {}
void FakePowerManagerClient::IncreaseScreenBrightness() {}
void FakePowerManagerClient::SetScreenBrightnessPercent(double percent,
bool gradual) {}
void FakePowerManagerClient::GetScreenBrightnessPercent(
const GetScreenBrightnessPercentCallback& callback) {}
void FakePowerManagerClient::DecreaseKeyboardBrightness() {}
void FakePowerManagerClient::IncreaseKeyboardBrightness() {}
void FakePowerManagerClient::RequestStatusUpdate() {
// RequestStatusUpdate() calls and notifies the observers
// asynchronously on a real device. On the fake implementation, we call
// observers in a posted task to emulate the same behavior.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&FakePowerManagerClient::NotifyObservers,
weak_ptr_factory_.GetWeakPtr()));
}
void FakePowerManagerClient::RequestSuspend() {}
void FakePowerManagerClient::RequestRestart() {
++num_request_restart_calls_;
}
void FakePowerManagerClient::RequestShutdown() {
++num_request_shutdown_calls_;
}
void FakePowerManagerClient::NotifyUserActivity(
power_manager::UserActivityType type) {}
void FakePowerManagerClient::NotifyVideoActivity(bool is_fullscreen) {
video_activity_reports_.push_back(is_fullscreen);
}
void FakePowerManagerClient::SetPolicy(
const power_manager::PowerManagementPolicy& policy) {
policy_ = policy;
++num_set_policy_calls_;
}
void FakePowerManagerClient::SetIsProjecting(bool is_projecting) {
++num_set_is_projecting_calls_;
is_projecting_ = is_projecting;
}
void FakePowerManagerClient::SetPowerSource(const std::string& id) {
props_.set_external_power_source_id(id);
props_.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
for (const auto& source : props_.available_external_power_source()) {
if (source.id() == id) {
props_.set_external_power(
!source.active_by_default() || source.max_power() < kUsbMinAcWatts
? power_manager::PowerSupplyProperties_ExternalPower_USB
: power_manager::PowerSupplyProperties_ExternalPower_AC);
break;
}
}
NotifyObservers();
}
void FakePowerManagerClient::SetBacklightsForcedOff(bool forced_off) {
backlights_forced_off_ = forced_off;
++num_set_backlights_forced_off_calls_;
}
void FakePowerManagerClient::GetBacklightsForcedOff(
const GetBacklightsForcedOffCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, backlights_forced_off_));
}
void FakePowerManagerClient::GetSwitchStates(
const GetSwitchStatesCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, lid_state_, tablet_mode_));
}
base::Closure FakePowerManagerClient::GetSuspendReadinessCallback() {
++num_pending_suspend_readiness_callbacks_;
return base::Bind(&FakePowerManagerClient::HandleSuspendReadiness,
base::Unretained(this));
}
int FakePowerManagerClient::GetNumPendingSuspendReadinessCallbacks() {
return num_pending_suspend_readiness_callbacks_;
}
bool FakePowerManagerClient::PopVideoActivityReport() {
CHECK(!video_activity_reports_.empty());
bool fullscreen = video_activity_reports_.front();
video_activity_reports_.pop_front();
return fullscreen;
}
void FakePowerManagerClient::SendSuspendImminent() {
for (auto& observer : observers_)
observer.SuspendImminent();
if (render_process_manager_delegate_)
render_process_manager_delegate_->SuspendImminent();
}
void FakePowerManagerClient::SendSuspendDone() {
if (render_process_manager_delegate_)
render_process_manager_delegate_->SuspendDone();
for (auto& observer : observers_)
observer.SuspendDone(base::TimeDelta());
}
void FakePowerManagerClient::SendDarkSuspendImminent() {
for (auto& observer : observers_)
observer.DarkSuspendImminent();
}
void FakePowerManagerClient::SendBrightnessChanged(int level,
bool user_initiated) {
for (auto& observer : observers_)
observer.BrightnessChanged(level, user_initiated);
}
void FakePowerManagerClient::SendPowerButtonEvent(
bool down,
const base::TimeTicks& timestamp) {
for (auto& observer : observers_)
observer.PowerButtonEventReceived(down, timestamp);
}
void FakePowerManagerClient::SetLidState(LidState state,
const base::TimeTicks& timestamp) {
lid_state_ = state;
for (auto& observer : observers_)
observer.LidEventReceived(state, timestamp);
}
void FakePowerManagerClient::UpdatePowerProperties(
const power_manager::PowerSupplyProperties& power_props) {
props_ = power_props;
NotifyObservers();
}
void FakePowerManagerClient::NotifyObservers() {
for (auto& observer : observers_)
observer.PowerChanged(props_);
}
void FakePowerManagerClient::HandleSuspendReadiness() {
CHECK(num_pending_suspend_readiness_callbacks_ > 0);
--num_pending_suspend_readiness_callbacks_;
}
} // namespace chromeos