Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/chromeos/dbus/bluetooth_manager_client.cc

Issue 9838085: Move files inside chrome/browser/chromeos/dbus to chromeos/dbus (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: _ Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/dbus/bluetooth_manager_client.h"
6
7 #include "base/bind.h"
8 #include "base/chromeos/chromeos_version.h"
9 #include "base/logging.h"
10 #include "chrome/browser/chromeos/dbus/bluetooth_property.h"
11 #include "dbus/bus.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
16
17 namespace chromeos {
18
19 BluetoothManagerClient::Properties::Properties(dbus::ObjectProxy* object_proxy,
20 PropertyChangedCallback callback)
21 : BluetoothPropertySet(object_proxy,
22 bluetooth_manager::kBluetoothManagerInterface,
23 callback) {
24 RegisterProperty(bluetooth_manager::kAdaptersProperty, &adapters);
25 }
26
27 BluetoothManagerClient::Properties::~Properties() {
28 }
29
30
31 // The BluetoothManagerClient implementation used in production.
32 class BluetoothManagerClientImpl : public BluetoothManagerClient {
33 public:
34 explicit BluetoothManagerClientImpl(dbus::Bus* bus)
35 : weak_ptr_factory_(this),
36 object_proxy_(NULL) {
37 DVLOG(1) << "Creating BluetoothManagerClientImpl";
38
39 // Create the object proxy.
40 DCHECK(bus);
41 object_proxy_ = bus->GetObjectProxy(
42 bluetooth_manager::kBluetoothManagerServiceName,
43 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath));
44
45 object_proxy_->ConnectToSignal(
46 bluetooth_manager::kBluetoothManagerInterface,
47 bluetooth_manager::kAdapterAddedSignal,
48 base::Bind(&BluetoothManagerClientImpl::AdapterAddedReceived,
49 weak_ptr_factory_.GetWeakPtr()),
50 base::Bind(&BluetoothManagerClientImpl::AdapterAddedConnected,
51 weak_ptr_factory_.GetWeakPtr()));
52
53 object_proxy_->ConnectToSignal(
54 bluetooth_manager::kBluetoothManagerInterface,
55 bluetooth_manager::kAdapterRemovedSignal,
56 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedReceived,
57 weak_ptr_factory_.GetWeakPtr()),
58 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedConnected,
59 weak_ptr_factory_.GetWeakPtr()));
60
61 object_proxy_->ConnectToSignal(
62 bluetooth_manager::kBluetoothManagerInterface,
63 bluetooth_manager::kDefaultAdapterChangedSignal,
64 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedReceived,
65 weak_ptr_factory_.GetWeakPtr()),
66 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedConnected,
67 weak_ptr_factory_.GetWeakPtr()));
68
69 // Create the properties structure.
70 properties_ = new Properties(
71 object_proxy_,
72 base::Bind(&BluetoothManagerClientImpl::OnPropertyChanged,
73 weak_ptr_factory_.GetWeakPtr()));
74
75 properties_->ConnectSignals();
76 properties_->GetAll();
77 }
78
79 virtual ~BluetoothManagerClientImpl() {
80 // Clean up the Properties structure.
81 delete properties_;
82 }
83
84 // BluetoothManagerClient override.
85 virtual void AddObserver(Observer* observer) OVERRIDE {
86 DCHECK(observer);
87 observers_.AddObserver(observer);
88 }
89
90 // BluetoothManagerClient override.
91 virtual void RemoveObserver(Observer* observer) OVERRIDE {
92 DCHECK(observer);
93 observers_.RemoveObserver(observer);
94 }
95
96 // BluetoothManagerClient override.
97 virtual Properties* GetProperties() OVERRIDE {
98 return properties_;
99 }
100
101 // BluetoothManagerClient override.
102 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE {
103 dbus::MethodCall method_call(
104 bluetooth_manager::kBluetoothManagerInterface,
105 bluetooth_manager::kDefaultAdapter);
106
107 DCHECK(object_proxy_);
108 object_proxy_->CallMethod(
109 &method_call,
110 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
111 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter,
112 weak_ptr_factory_.GetWeakPtr(), callback));
113 }
114
115 // BluetoothManagerClient override.
116 virtual void FindAdapter(const std::string& address,
117 const AdapterCallback& callback) {
118 dbus::MethodCall method_call(
119 bluetooth_manager::kBluetoothManagerInterface,
120 bluetooth_manager::kFindAdapter);
121
122 dbus::MessageWriter writer(&method_call);
123 writer.AppendString(address);
124
125 DCHECK(object_proxy_);
126 object_proxy_->CallMethod(
127 &method_call,
128 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
129 base::Bind(&BluetoothManagerClientImpl::OnFindAdapter,
130 weak_ptr_factory_.GetWeakPtr(), callback));
131 }
132
133 private:
134 // Called by BluetoothPropertySet when a property value is changed,
135 // either by result of a signal or response to a GetAll() or Get()
136 // call. Informs observers.
137 void OnPropertyChanged(const std::string& property_name) {
138 FOR_EACH_OBSERVER(BluetoothManagerClient::Observer, observers_,
139 ManagerPropertyChanged(property_name));
140 }
141
142 // Called by dbus:: when an AdapterAdded signal is received.
143 void AdapterAddedReceived(dbus::Signal* signal) {
144 DCHECK(signal);
145 dbus::MessageReader reader(signal);
146 dbus::ObjectPath object_path;
147 if (!reader.PopObjectPath(&object_path)) {
148 LOG(WARNING) << "AdapterAdded signal has incorrect parameters: "
149 << signal->ToString();
150 return;
151 }
152
153 DVLOG(1) << "Adapter added: " << object_path.value();
154 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path));
155 }
156
157 // Called by dbus:: when the AdapterAdded signal is initially connected.
158 void AdapterAddedConnected(const std::string& interface_name,
159 const std::string& signal_name,
160 bool success) {
161 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal.";
162 }
163
164 // Called by dbus:: when an AdapterRemoved signal is received.
165 void AdapterRemovedReceived(dbus::Signal* signal) {
166 DCHECK(signal);
167 dbus::MessageReader reader(signal);
168 dbus::ObjectPath object_path;
169 if (!reader.PopObjectPath(&object_path)) {
170 LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: "
171 << signal->ToString();
172 return;
173 }
174
175 DVLOG(1) << "Adapter removed: " << object_path.value();
176 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path));
177 }
178
179 // Called by dbus:: when the AdapterRemoved signal is initially connected.
180 void AdapterRemovedConnected(const std::string& interface_name,
181 const std::string& signal_name,
182 bool success) {
183 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal.";
184 }
185
186 // Called by dbus:: when a DefaultAdapterChanged signal is received.
187 void DefaultAdapterChangedReceived(dbus::Signal* signal) {
188 DCHECK(signal);
189 dbus::MessageReader reader(signal);
190 dbus::ObjectPath object_path;
191 if (!reader.PopObjectPath(&object_path)) {
192 LOG(WARNING) << "DefaultAdapterChanged signal has incorrect parameters: "
193 << signal->ToString();
194 return;
195 }
196
197 DVLOG(1) << "Default adapter changed: " << object_path.value();
198 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(object_path));
199 }
200
201 // Called by dbus:: when the DefaultAdapterChanged signal is initially
202 // connected.
203 void DefaultAdapterChangedConnected(const std::string& interface_name,
204 const std::string& signal_name,
205 bool success) {
206 LOG_IF(WARNING, !success)
207 << "Failed to connect to DefaultAdapterChanged signal.";
208 }
209
210 // Called when a response for DefaultAdapter() is received.
211 void OnDefaultAdapter(const AdapterCallback& callback,
212 dbus::Response* response) {
213 // Parse response.
214 bool success = false;
215 dbus::ObjectPath object_path;
216 if (response != NULL) {
217 dbus::MessageReader reader(response);
218 if (!reader.PopObjectPath(&object_path)) {
219 LOG(WARNING) << "DefaultAdapter response has incorrect parameters: "
220 << response->ToString();
221 } else {
222 success = true;
223 }
224 } else {
225 LOG(WARNING) << "Failed to get default adapter.";
226 }
227
228 // Notify client.
229 callback.Run(object_path, success);
230 }
231
232 // Called when a response for FindAdapter() is received.
233 void OnFindAdapter(const AdapterCallback& callback,
234 dbus::Response* response) {
235 // Parse response.
236 bool success = false;
237 dbus::ObjectPath object_path;
238 if (response != NULL) {
239 dbus::MessageReader reader(response);
240 if (!reader.PopObjectPath(&object_path)) {
241 LOG(WARNING) << "FindAdapter response has incorrect parameters: "
242 << response->ToString();
243 } else {
244 success = true;
245 }
246 } else {
247 LOG(WARNING) << "Failed to find adapter.";
248 }
249
250 // Notify client.
251 callback.Run(object_path, success);
252 }
253
254 // Weak pointer factory for generating 'this' pointers that might live longer
255 // than we do.
256 base::WeakPtrFactory<BluetoothManagerClientImpl> weak_ptr_factory_;
257
258 // D-Bus proxy for BlueZ Manager interface.
259 dbus::ObjectProxy* object_proxy_;
260
261 // Properties for BlueZ Manager interface.
262 Properties* properties_;
263
264 // List of observers interested in event notifications from us.
265 ObserverList<Observer> observers_;
266
267 DISALLOW_COPY_AND_ASSIGN(BluetoothManagerClientImpl);
268 };
269
270 // The BluetoothManagerClient implementation used on Linux desktop, which does
271 // nothing.
272 class BluetoothManagerClientStubImpl : public BluetoothManagerClient {
273 public:
274 // BluetoothManagerClient override.
275 virtual void AddObserver(Observer* observer) OVERRIDE {
276 }
277
278 // BluetoothManagerClient override.
279 virtual void RemoveObserver(Observer* observer) OVERRIDE {
280 }
281
282 // BluetoothManagerClient override.
283 virtual Properties* GetProperties() OVERRIDE {
284 VLOG(1) << "GetProperties";
285 return NULL;
286 }
287
288 // BluetoothManagerClient override.
289 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE {
290 VLOG(1) << "DefaultAdapter.";
291 callback.Run(dbus::ObjectPath(), false);
292 }
293
294 // BluetoothManagerClient override.
295 virtual void FindAdapter(const std::string& address,
296 const AdapterCallback& callback) {
297 VLOG(1) << "FindAdapter: " << address;
298 callback.Run(dbus::ObjectPath(), false);
299 }
300 };
301
302 BluetoothManagerClient::BluetoothManagerClient() {
303 }
304
305 BluetoothManagerClient::~BluetoothManagerClient() {
306 }
307
308 BluetoothManagerClient* BluetoothManagerClient::Create(dbus::Bus* bus) {
309 if (base::chromeos::IsRunningOnChromeOS()) {
310 return new BluetoothManagerClientImpl(bus);
311 } else {
312 return new BluetoothManagerClientStubImpl();
313 }
314 }
315
316 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698