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

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

Powered by Google App Engine
This is Rietveld 408576698