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
|
// Copyright (c) 2011 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 "chrome/browser/chromeos/dbus/power_manager_client.h"
#include "base/bind.h"
#include "base/callback.h"
#include "chrome/browser/chromeos/system/runtime_environment.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
// The PowerManagerClient implementation used in production.
class PowerManagerClientImpl : public PowerManagerClient {
public:
explicit PowerManagerClientImpl(dbus::Bus* bus)
: power_manager_proxy_(NULL) {
power_manager_proxy_ = bus->GetObjectProxy(
power_manager::kPowerManagerServiceName,
power_manager::kPowerManagerServicePath);
// Monitor the D-Bus signal for brightness changes. Only the power
// manager knows the actual brightness level. We don't cache the
// brightness level in Chrome as it'll make things less reliable.
power_manager_proxy_->ConnectToSignal(
power_manager::kPowerManagerInterface,
power_manager::kBrightnessChangedSignal,
base::Bind(&PowerManagerClientImpl::BrightnessChangedReceived, this),
base::Bind(&PowerManagerClientImpl::BrightnessChangedConnected, this));
}
// PowerManagerClient override.
virtual void AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
// PowerManagerClient override.
virtual void RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
// PowerManagerClient override.
virtual void DecreaseScreenBrightness(bool allow_off) {
dbus::MethodCall method_call(
power_manager::kPowerManagerInterface,
power_manager::kDecreaseScreenBrightness);
dbus::MessageWriter writer(&method_call);
writer.AppendBool(allow_off);
power_manager_proxy_->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&PowerManagerClientImpl::OnDecreaseScreenBrightness,
this));
}
// PowerManagerClient override.
virtual void IncreaseScreenBrightness() {
dbus::MethodCall method_call(
power_manager::kPowerManagerInterface,
power_manager::kIncreaseScreenBrightness);
power_manager_proxy_->CallMethod(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&PowerManagerClientImpl::OnIncreaseScreenBrightness,
this));
}
private:
friend class base::RefCountedThreadSafe<PowerManagerClientImpl>;
virtual ~PowerManagerClientImpl() {
}
// Called when a brightness change signal is received.
void BrightnessChangedReceived(dbus::Signal* signal) {
dbus::MessageReader reader(signal);
int32 brightness_level = 0;
bool user_initiated = 0;
if (!(reader.PopInt32(&brightness_level) &&
reader.PopBool(&user_initiated))) {
LOG(ERROR) << "Brightness changed signal had incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << "Brightness changed to " << brightness_level
<< ": user initiated " << user_initiated;
FOR_EACH_OBSERVER(Observer, observers_,
BrightnessChanged(brightness_level, user_initiated));
}
// Called when the brightness change signal is initially connected.
void BrightnessChangedConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success)
<< "Failed to connect to brightness changed signal.";
}
// Called when a response for DecreaseScreenBrightness() is received.
void OnDecreaseScreenBrightness(dbus::Response* response) {
if (!response) {
LOG(ERROR) << "Failed to decrease screen brightness";
return;
}
VLOG(1) << "screen brightness increased: " << response->ToString();
}
// Called when a response for IncreaseScreenBrightness() is received.
void OnIncreaseScreenBrightness(dbus::Response* response) {
if (!response) {
LOG(ERROR) << "Failed to increase screen brightness";
return;
}
VLOG(1) << "screen brightness increased: " << response->ToString();
}
dbus::ObjectProxy* power_manager_proxy_;
ObserverList<Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(PowerManagerClientImpl);
};
// The PowerManagerClient implementation used on Linux desktop,
// which does nothing.
class PowerManagerClientStubImpl : public PowerManagerClient {
// PowerManagerClient override.
virtual void AddObserver(Observer* observer) {
}
// PowerManagerClient override.
virtual void RemoveObserver(Observer* observer) {
}
// PowerManagerClient override.
virtual void DecreaseScreenBrightness(bool allow_off) {
VLOG(1) << "Requested to descrease screen brightness";
}
// PowerManagerClient override.
virtual void IncreaseScreenBrightness() {
VLOG(1) << "Requested to increase screen brightness";
}
};
PowerManagerClient::PowerManagerClient() {
}
PowerManagerClient::~PowerManagerClient() {
}
PowerManagerClient* PowerManagerClient::Create(dbus::Bus* bus) {
if (system::runtime_environment::IsRunningOnChromeOS()) {
return new PowerManagerClientImpl(bus);
} else {
return new PowerManagerClientStubImpl();
}
}
} // namespace chromeos
|