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

Side by Side Diff: chromeos/dbus/bluetooth_input_client.cc

Issue 15020009: Bluetooth: remove legacy backend (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « chromeos/dbus/bluetooth_input_client.h ('k') | chromeos/dbus/bluetooth_manager_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chromeos/dbus/bluetooth_input_client.h"
6
7 #include <map>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "chromeos/dbus/bluetooth_adapter_client.h"
14 #include "chromeos/dbus/bluetooth_property.h"
15 #include "dbus/bus.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "third_party/cros_system_api/dbus/service_constants.h"
20
21 namespace {
22
23 // The |Connect| DBus call needs a longer timeout than the default in order to
24 // give BlueZ enough time to return the timeout error response.
25 // See crosbug.com/37607
26 const int kConnectTimeoutMs = 50 * 1000;
27
28 } // namespace
29
30 namespace chromeos {
31
32 const char BluetoothInputClient::kNoResponseError[] =
33 "org.chromium.Error.NoResponse";
34
35 BluetoothInputClient::Properties::Properties(
36 dbus::ObjectProxy* object_proxy,
37 const PropertyChangedCallback& callback)
38 : BluetoothPropertySet(object_proxy,
39 bluetooth_input::kBluetoothInputInterface,
40 callback) {
41 RegisterProperty(bluetooth_input::kConnectedProperty, &connected);
42 }
43
44 BluetoothInputClient::Properties::~Properties() {
45 }
46
47
48 // The BluetoothInputClient implementation used in production.
49 class BluetoothInputClientImpl: public BluetoothInputClient,
50 private BluetoothAdapterClient::Observer {
51 public:
52 BluetoothInputClientImpl(dbus::Bus* bus,
53 BluetoothAdapterClient* adapter_client)
54 : bus_(bus),
55 weak_ptr_factory_(this) {
56 DCHECK(adapter_client);
57 adapter_client->AddObserver(this);
58 }
59
60 virtual ~BluetoothInputClientImpl() {
61 // Clean up Properties structures
62 for (ObjectMap::iterator iter = object_map_.begin();
63 iter != object_map_.end(); ++iter) {
64 Object object = iter->second;
65 Properties* properties = object.second;
66 delete properties;
67 }
68 }
69
70 // BluetoothInputClient override.
71 virtual void AddObserver(BluetoothInputClient::Observer* observer)
72 OVERRIDE {
73 DCHECK(observer);
74 observers_.AddObserver(observer);
75 }
76
77 // BluetoothInputClient override.
78 virtual void RemoveObserver(BluetoothInputClient::Observer* observer)
79 OVERRIDE {
80 DCHECK(observer);
81 observers_.RemoveObserver(observer);
82 }
83
84 // BluetoothInputClient override.
85 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
86 OVERRIDE {
87 return GetObject(object_path).second;
88 }
89
90 // BluetoothInputClient override.
91 virtual void Connect(const dbus::ObjectPath& object_path,
92 const ConnectCallback& callback,
93 const ConnectErrorCallback& error_callback) OVERRIDE {
94 dbus::MethodCall method_call(
95 bluetooth_input::kBluetoothInputInterface,
96 bluetooth_input::kConnect);
97
98 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
99
100 object_proxy->CallMethodWithErrorCallback(
101 &method_call,
102 kConnectTimeoutMs,
103 base::Bind(&BluetoothInputClientImpl::OnConnect,
104 weak_ptr_factory_.GetWeakPtr(), object_path,
105 callback),
106 base::Bind(&BluetoothInputClientImpl::OnConnectError,
107 weak_ptr_factory_.GetWeakPtr(), object_path,
108 error_callback));
109 }
110
111 // BluetoothInputClient override.
112 virtual void Disconnect(const dbus::ObjectPath& object_path,
113 const InputCallback& callback) OVERRIDE {
114 dbus::MethodCall method_call(
115 bluetooth_input::kBluetoothInputInterface,
116 bluetooth_input::kDisconnect);
117
118 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
119
120 object_proxy->CallMethod(
121 &method_call,
122 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
123 base::Bind(&BluetoothInputClientImpl::OnDisconnect,
124 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
125 }
126
127 private:
128 // We maintain a collection of dbus object proxies and properties structures
129 // for each input device.
130 typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
131 typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
132 ObjectMap object_map_;
133
134 // BluetoothAdapterClient::Observer override.
135 virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
136 const dbus::ObjectPath& object_path) OVERRIDE {
137 }
138
139 // BluetoothAdapterClient::Observer override.
140 virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
141 const dbus::ObjectPath& object_path) OVERRIDE {
142 RemoveObject(object_path);
143 }
144
145 // Ensures that we have an object proxy and properties structure for
146 // an input device with object path |object_path|, creating it if not and
147 // storing it in our |object_map_| map.
148 Object GetObject(const dbus::ObjectPath& object_path) {
149 ObjectMap::iterator iter = object_map_.find(object_path);
150 if (iter != object_map_.end())
151 return iter->second;
152
153 // Create the object proxy.
154 DCHECK(bus_);
155 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
156 bluetooth_input::kBluetoothInputServiceName, object_path);
157
158 // Create the properties structure.
159 Properties* properties = new Properties(
160 object_proxy,
161 base::Bind(&BluetoothInputClientImpl::OnPropertyChanged,
162 weak_ptr_factory_.GetWeakPtr(), object_path));
163
164 properties->ConnectSignals();
165 properties->GetAll();
166
167 Object object = std::make_pair(object_proxy, properties);
168 object_map_[object_path] = object;
169 return object;
170 }
171
172 // Removes the dbus object proxy and properties for the input device with
173 // dbus object path |object_path| from our |object_map_| map.
174 void RemoveObject(const dbus::ObjectPath& object_path) {
175 ObjectMap::iterator iter = object_map_.find(object_path);
176 if (iter != object_map_.end()) {
177 // Clean up the Properties structure.
178 Object object = iter->second;
179 Properties* properties = object.second;
180 delete properties;
181
182 object_map_.erase(iter);
183 }
184 }
185
186 // Returns a pointer to the object proxy for |object_path|, creating
187 // it if necessary.
188 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
189 return GetObject(object_path).first;
190 }
191
192 // Called by BluetoothPropertySet when a property value is changed,
193 // either by result of a signal or response to a GetAll() or Get()
194 // call. Informs observers.
195 void OnPropertyChanged(const dbus::ObjectPath& object_path,
196 const std::string& property_name) {
197 FOR_EACH_OBSERVER(BluetoothInputClient::Observer, observers_,
198 InputPropertyChanged(object_path, property_name));
199 }
200
201 // Called when a response for Connect() is received.
202 void OnConnect(const dbus::ObjectPath& object_path,
203 const ConnectCallback& callback,
204 dbus::Response* response) {
205 DCHECK(response);
206 callback.Run(object_path);
207 }
208
209 // Called when an error for Connect() is received.
210 void OnConnectError(const dbus::ObjectPath& object_path,
211 const ConnectErrorCallback& error_callback,
212 dbus::ErrorResponse* response) {
213 // Error response has optional error message argument.
214 std::string error_name;
215 std::string error_message;
216 if (response) {
217 dbus::MessageReader reader(response);
218 error_name = response->GetErrorName();
219 reader.PopString(&error_message);
220 } else {
221 error_name = kNoResponseError;
222 error_message = "";
223 }
224 error_callback.Run(object_path, error_name, error_message);
225 }
226
227 // Called when a response for Disconnect() is received.
228 void OnDisconnect(const dbus::ObjectPath& object_path,
229 const InputCallback& callback,
230 dbus::Response* response) {
231 LOG_IF(WARNING, !response) << object_path.value()
232 << ": OnDisconnect: failed.";
233 callback.Run(object_path, response);
234 }
235
236 dbus::Bus* bus_;
237
238 // List of observers interested in event notifications from us.
239 ObserverList<BluetoothInputClient::Observer> observers_;
240
241 // Weak pointer factory for generating 'this' pointers that might live longer
242 // than we do.
243 // Note: This should remain the last member so it'll be destroyed and
244 // invalidate its weak pointers before any other members are destroyed.
245 base::WeakPtrFactory<BluetoothInputClientImpl> weak_ptr_factory_;
246
247 DISALLOW_COPY_AND_ASSIGN(BluetoothInputClientImpl);
248 };
249
250 // The BluetoothInputClient implementation used on Linux desktop, which does
251 // nothing.
252 class BluetoothInputClientStubImpl : public BluetoothInputClient {
253 public:
254 // BluetoothInputClient override.
255 virtual void AddObserver(Observer* observer) OVERRIDE {
256 }
257
258 // BluetoothInputClient override.
259 virtual void RemoveObserver(Observer* observer) OVERRIDE {
260 }
261
262 // BluetoothInputClient override.
263 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
264 OVERRIDE {
265 VLOG(1) << "GetProperties: " << object_path.value();
266 return NULL;
267 }
268
269 // BluetoothInputClient override.
270 virtual void Connect(const dbus::ObjectPath& object_path,
271 const ConnectCallback& callback,
272 const ConnectErrorCallback& error_callback) OVERRIDE {
273 VLOG(1) << "Connect: " << object_path.value();
274 error_callback.Run(object_path, "", "");
275 }
276
277 // BluetoothInputClient override.
278 virtual void Disconnect(const dbus::ObjectPath& object_path,
279 const InputCallback& callback) OVERRIDE {
280 VLOG(1) << "Disconnect: " << object_path.value();
281 callback.Run(object_path, false);
282 }
283 };
284
285 BluetoothInputClient::BluetoothInputClient() {
286 }
287
288 BluetoothInputClient::~BluetoothInputClient() {
289 }
290
291 BluetoothInputClient* BluetoothInputClient::Create(
292 DBusClientImplementationType type,
293 dbus::Bus* bus,
294 BluetoothAdapterClient* adapter_client) {
295 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
296 return new BluetoothInputClientImpl(bus, adapter_client);
297 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
298 return new BluetoothInputClientStubImpl();
299 }
300
301 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/bluetooth_input_client.h ('k') | chromeos/dbus/bluetooth_manager_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698