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

Side by Side Diff: chrome/browser/chromeos/dbus/bluetooth_input_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_input_client.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/chromeos/chromeos_version.h"
11 #include "base/logging.h"
12 #include "base/stl_util.h"
13 #include "chrome/browser/chromeos/dbus/bluetooth_adapter_client.h"
14 #include "chrome/browser/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 chromeos {
22
23 BluetoothInputClient::Properties::Properties(dbus::ObjectProxy* object_proxy,
24 PropertyChangedCallback callback)
25 : BluetoothPropertySet(object_proxy,
26 bluetooth_input::kBluetoothInputInterface,
27 callback) {
28 RegisterProperty(bluetooth_input::kConnectedProperty, &connected);
29 }
30
31 BluetoothInputClient::Properties::~Properties() {
32 }
33
34
35 // The BluetoothInputClient implementation used in production.
36 class BluetoothInputClientImpl: public BluetoothInputClient,
37 private BluetoothAdapterClient::Observer {
38 public:
39 BluetoothInputClientImpl(dbus::Bus* bus,
40 BluetoothAdapterClient* adapter_client)
41 : weak_ptr_factory_(this),
42 bus_(bus) {
43 DVLOG(1) << "Creating BluetoothInputClientImpl";
44
45 DCHECK(adapter_client);
46 adapter_client->AddObserver(this);
47 }
48
49 virtual ~BluetoothInputClientImpl() {
50 // Clean up Properties structures
51 for (ObjectMap::iterator iter = object_map_.begin();
52 iter != object_map_.end(); ++iter) {
53 Object object = iter->second;
54 Properties* properties = object.second;
55 delete properties;
56 }
57 }
58
59 // BluetoothInputClient override.
60 virtual void AddObserver(BluetoothInputClient::Observer* observer)
61 OVERRIDE {
62 DCHECK(observer);
63 observers_.AddObserver(observer);
64 }
65
66 // BluetoothInputClient override.
67 virtual void RemoveObserver(BluetoothInputClient::Observer* observer)
68 OVERRIDE {
69 DCHECK(observer);
70 observers_.RemoveObserver(observer);
71 }
72
73 // BluetoothInputClient override.
74 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
75 OVERRIDE {
76 return GetObject(object_path).second;
77 }
78
79 // BluetoothInputClient override.
80 virtual void Connect(const dbus::ObjectPath& object_path,
81 const InputCallback& callback) OVERRIDE {
82 dbus::MethodCall method_call(
83 bluetooth_input::kBluetoothInputInterface,
84 bluetooth_input::kConnect);
85
86 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
87
88 object_proxy->CallMethod(
89 &method_call,
90 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
91 base::Bind(&BluetoothInputClientImpl::OnConnect,
92 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
93 }
94
95 // BluetoothInputClient override.
96 virtual void Disconnect(const dbus::ObjectPath& object_path,
97 const InputCallback& callback) OVERRIDE {
98 dbus::MethodCall method_call(
99 bluetooth_input::kBluetoothInputInterface,
100 bluetooth_input::kDisconnect);
101
102 dbus::ObjectProxy* object_proxy = GetObjectProxy(object_path);
103
104 object_proxy->CallMethod(
105 &method_call,
106 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
107 base::Bind(&BluetoothInputClientImpl::OnDisconnect,
108 weak_ptr_factory_.GetWeakPtr(), object_path, callback));
109 }
110
111 private:
112 // We maintain a collection of dbus object proxies and properties structures
113 // for each input device.
114 typedef std::pair<dbus::ObjectProxy*, Properties*> Object;
115 typedef std::map<const dbus::ObjectPath, Object> ObjectMap;
116 ObjectMap object_map_;
117
118 // BluetoothAdapterClient::Observer override.
119 virtual void DeviceCreated(const dbus::ObjectPath& adapter_path,
120 const dbus::ObjectPath& object_path) OVERRIDE {
121 }
122
123 // BluetoothAdapterClient::Observer override.
124 virtual void DeviceRemoved(const dbus::ObjectPath& adapter_path,
125 const dbus::ObjectPath& object_path) OVERRIDE {
126 RemoveObject(object_path);
127 }
128
129 // Ensures that we have an object proxy and properties structure for
130 // an input device with object path |object_path|, creating it if not and
131 // storing it in our |object_map_| map.
132 Object GetObject(const dbus::ObjectPath& object_path) {
133 ObjectMap::iterator iter = object_map_.find(object_path);
134 if (iter != object_map_.end())
135 return iter->second;
136
137 // Create the object proxy.
138 DCHECK(bus_);
139 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy(
140 bluetooth_input::kBluetoothInputServiceName, object_path);
141
142 // Create the properties structure.
143 Properties* properties = new Properties(
144 object_proxy,
145 base::Bind(&BluetoothInputClientImpl::OnPropertyChanged,
146 weak_ptr_factory_.GetWeakPtr(), object_path));
147
148 properties->ConnectSignals();
149 properties->GetAll();
150
151 Object object = std::make_pair(object_proxy, properties);
152 object_map_[object_path] = object;
153 return object;
154 }
155
156 // Removes the dbus object proxy and properties for the input device with
157 // dbus object path |object_path| from our |object_map_| map.
158 void RemoveObject(const dbus::ObjectPath& object_path) {
159 ObjectMap::iterator iter = object_map_.find(object_path);
160 if (iter != object_map_.end()) {
161 // Clean up the Properties structure.
162 Object object = iter->second;
163 Properties* properties = object.second;
164 delete properties;
165
166 object_map_.erase(iter);
167 }
168 }
169
170 // Returns a pointer to the object proxy for |object_path|, creating
171 // it if necessary.
172 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) {
173 return GetObject(object_path).first;
174 }
175
176 // Called by BluetoothPropertySet when a property value is changed,
177 // either by result of a signal or response to a GetAll() or Get()
178 // call. Informs observers.
179 void OnPropertyChanged(const dbus::ObjectPath& object_path,
180 const std::string& property_name) {
181 FOR_EACH_OBSERVER(BluetoothInputClient::Observer, observers_,
182 InputPropertyChanged(object_path, property_name));
183 }
184
185 // Called when a response for Connect() is received.
186 void OnConnect(const dbus::ObjectPath& object_path,
187 const InputCallback& callback,
188 dbus::Response* response) {
189 LOG_IF(WARNING, !response) << object_path.value()
190 << ": OnConnect: failed.";
191 callback.Run(object_path, response);
192 }
193
194 // Called when a response for Disconnect() is received.
195 void OnDisconnect(const dbus::ObjectPath& object_path,
196 const InputCallback& callback,
197 dbus::Response* response) {
198 LOG_IF(WARNING, !response) << object_path.value()
199 << ": OnDisconnect: failed.";
200 callback.Run(object_path, response);
201 }
202
203 // Weak pointer factory for generating 'this' pointers that might live longer
204 // than we do.
205 base::WeakPtrFactory<BluetoothInputClientImpl> weak_ptr_factory_;
206
207 dbus::Bus* bus_;
208
209 // List of observers interested in event notifications from us.
210 ObserverList<BluetoothInputClient::Observer> observers_;
211
212 DISALLOW_COPY_AND_ASSIGN(BluetoothInputClientImpl);
213 };
214
215 // The BluetoothInputClient implementation used on Linux desktop, which does
216 // nothing.
217 class BluetoothInputClientStubImpl : public BluetoothInputClient {
218 public:
219 // BluetoothInputClient override.
220 virtual void AddObserver(Observer* observer) OVERRIDE {
221 }
222
223 // BluetoothInputClient override.
224 virtual void RemoveObserver(Observer* observer) OVERRIDE {
225 }
226
227 // BluetoothInputClient override.
228 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
229 OVERRIDE {
230 VLOG(1) << "GetProperties: " << object_path.value();
231 return NULL;
232 }
233
234 // BluetoothInputClient override.
235 virtual void Connect(const dbus::ObjectPath& object_path,
236 const InputCallback& callback) OVERRIDE {
237 VLOG(1) << "Connect: " << object_path.value();
238 callback.Run(object_path, false);
239 }
240
241 // BluetoothInputClient override.
242 virtual void Disconnect(const dbus::ObjectPath& object_path,
243 const InputCallback& callback) OVERRIDE {
244 VLOG(1) << "Disconnect: " << object_path.value();
245 callback.Run(object_path, false);
246 }
247 };
248
249 BluetoothInputClient::BluetoothInputClient() {
250 }
251
252 BluetoothInputClient::~BluetoothInputClient() {
253 }
254
255 BluetoothInputClient* BluetoothInputClient::Create(
256 dbus::Bus* bus,
257 BluetoothAdapterClient* adapter_client) {
258 if (base::chromeos::IsRunningOnChromeOS()) {
259 return new BluetoothInputClientImpl(bus, adapter_client);
260 } else {
261 return new BluetoothInputClientStubImpl();
262 }
263 }
264
265 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698