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

Side by Side Diff: chromeos/dbus/bluetooth_manager_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_manager_client.h ('k') | chromeos/dbus/bluetooth_node_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_manager_client.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chromeos/dbus/bluetooth_property.h"
13 #include "chromeos/dbus/fake_old_bluetooth_manager_client.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 BluetoothManagerClient::Properties::Properties(
23 dbus::ObjectProxy* object_proxy,
24 const PropertyChangedCallback& callback)
25 : BluetoothPropertySet(object_proxy,
26 bluetooth_manager::kBluetoothManagerInterface,
27 callback) {
28 RegisterProperty(bluetooth_manager::kAdaptersProperty, &adapters);
29 }
30
31 BluetoothManagerClient::Properties::~Properties() {
32 }
33
34
35 // The BluetoothManagerClient implementation used in production.
36 class BluetoothManagerClientImpl : public BluetoothManagerClient {
37 public:
38 explicit BluetoothManagerClientImpl(dbus::Bus* bus)
39 : object_proxy_(NULL),
40 weak_ptr_factory_(this) {
41 // Create the object proxy.
42 DCHECK(bus);
43 object_proxy_ = bus->GetObjectProxy(
44 bluetooth_manager::kBluetoothManagerServiceName,
45 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath));
46
47 object_proxy_->ConnectToSignal(
48 bluetooth_manager::kBluetoothManagerInterface,
49 bluetooth_manager::kAdapterAddedSignal,
50 base::Bind(&BluetoothManagerClientImpl::AdapterAddedReceived,
51 weak_ptr_factory_.GetWeakPtr()),
52 base::Bind(&BluetoothManagerClientImpl::AdapterAddedConnected,
53 weak_ptr_factory_.GetWeakPtr()));
54
55 object_proxy_->ConnectToSignal(
56 bluetooth_manager::kBluetoothManagerInterface,
57 bluetooth_manager::kAdapterRemovedSignal,
58 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedReceived,
59 weak_ptr_factory_.GetWeakPtr()),
60 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedConnected,
61 weak_ptr_factory_.GetWeakPtr()));
62
63 object_proxy_->ConnectToSignal(
64 bluetooth_manager::kBluetoothManagerInterface,
65 bluetooth_manager::kDefaultAdapterChangedSignal,
66 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedReceived,
67 weak_ptr_factory_.GetWeakPtr()),
68 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedConnected,
69 weak_ptr_factory_.GetWeakPtr()));
70
71 // Create the properties structure.
72 properties_ = new Properties(
73 object_proxy_,
74 base::Bind(&BluetoothManagerClientImpl::OnPropertyChanged,
75 weak_ptr_factory_.GetWeakPtr()));
76
77 properties_->ConnectSignals();
78 properties_->GetAll();
79 }
80
81 virtual ~BluetoothManagerClientImpl() {
82 // Clean up the Properties structure.
83 delete properties_;
84 }
85
86 // BluetoothManagerClient override.
87 virtual void AddObserver(Observer* observer) OVERRIDE {
88 DCHECK(observer);
89 observers_.AddObserver(observer);
90 }
91
92 // BluetoothManagerClient override.
93 virtual void RemoveObserver(Observer* observer) OVERRIDE {
94 DCHECK(observer);
95 observers_.RemoveObserver(observer);
96 }
97
98 // BluetoothManagerClient override.
99 virtual Properties* GetProperties() OVERRIDE {
100 return properties_;
101 }
102
103 // BluetoothManagerClient override.
104 virtual void DefaultAdapter(const AdapterCallback& callback) OVERRIDE {
105 dbus::MethodCall method_call(
106 bluetooth_manager::kBluetoothManagerInterface,
107 bluetooth_manager::kDefaultAdapter);
108
109 DCHECK(object_proxy_);
110 object_proxy_->CallMethod(
111 &method_call,
112 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
113 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter,
114 weak_ptr_factory_.GetWeakPtr(), callback));
115 }
116
117 // BluetoothManagerClient override.
118 virtual void FindAdapter(const std::string& address,
119 const AdapterCallback& callback) OVERRIDE {
120 dbus::MethodCall method_call(
121 bluetooth_manager::kBluetoothManagerInterface,
122 bluetooth_manager::kFindAdapter);
123
124 dbus::MessageWriter writer(&method_call);
125 writer.AppendString(address);
126
127 DCHECK(object_proxy_);
128 object_proxy_->CallMethod(
129 &method_call,
130 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
131 base::Bind(&BluetoothManagerClientImpl::OnFindAdapter,
132 weak_ptr_factory_.GetWeakPtr(), callback));
133 }
134
135 private:
136 // Called by BluetoothPropertySet when a property value is changed,
137 // either by result of a signal or response to a GetAll() or Get()
138 // call. Informs observers.
139 void OnPropertyChanged(const std::string& property_name) {
140 FOR_EACH_OBSERVER(BluetoothManagerClient::Observer, observers_,
141 ManagerPropertyChanged(property_name));
142 }
143
144 // Called by dbus:: when an AdapterAdded signal is received.
145 void AdapterAddedReceived(dbus::Signal* signal) {
146 DCHECK(signal);
147 dbus::MessageReader reader(signal);
148 dbus::ObjectPath object_path;
149 if (!reader.PopObjectPath(&object_path)) {
150 LOG(WARNING) << "AdapterAdded signal has incorrect parameters: "
151 << signal->ToString();
152 return;
153 }
154
155 VLOG(1) << "Adapter added: " << object_path.value();
156 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path));
157 }
158
159 // Called by dbus:: when the AdapterAdded signal is initially connected.
160 void AdapterAddedConnected(const std::string& interface_name,
161 const std::string& signal_name,
162 bool success) {
163 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal.";
164 }
165
166 // Called by dbus:: when an AdapterRemoved signal is received.
167 void AdapterRemovedReceived(dbus::Signal* signal) {
168 DCHECK(signal);
169 dbus::MessageReader reader(signal);
170 dbus::ObjectPath object_path;
171 if (!reader.PopObjectPath(&object_path)) {
172 LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: "
173 << signal->ToString();
174 return;
175 }
176
177 VLOG(1) << "Adapter removed: " << object_path.value();
178 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path));
179 }
180
181 // Called by dbus:: when the AdapterRemoved signal is initially connected.
182 void AdapterRemovedConnected(const std::string& interface_name,
183 const std::string& signal_name,
184 bool success) {
185 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal.";
186 }
187
188 // Called by dbus:: when a DefaultAdapterChanged signal is received.
189 void DefaultAdapterChangedReceived(dbus::Signal* signal) {
190 DCHECK(signal);
191 dbus::MessageReader reader(signal);
192 dbus::ObjectPath object_path;
193 if (!reader.PopObjectPath(&object_path)) {
194 LOG(WARNING) << "DefaultAdapterChanged signal has incorrect parameters: "
195 << signal->ToString();
196 return;
197 }
198
199 VLOG(1) << "Default adapter changed: " << object_path.value();
200 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(object_path));
201 }
202
203 // Called by dbus:: when the DefaultAdapterChanged signal is initially
204 // connected.
205 void DefaultAdapterChangedConnected(const std::string& interface_name,
206 const std::string& signal_name,
207 bool success) {
208 LOG_IF(WARNING, !success)
209 << "Failed to connect to DefaultAdapterChanged signal.";
210 }
211
212 // Called when a response for DefaultAdapter() is received.
213 void OnDefaultAdapter(const AdapterCallback& callback,
214 dbus::Response* response) {
215 // Parse response.
216 bool success = false;
217 dbus::ObjectPath object_path;
218 if (response != NULL) {
219 dbus::MessageReader reader(response);
220 if (!reader.PopObjectPath(&object_path)) {
221 LOG(WARNING) << "DefaultAdapter response has incorrect parameters: "
222 << response->ToString();
223 } else {
224 success = true;
225 }
226 } else {
227 LOG(WARNING) << "Failed to get default adapter.";
228 }
229
230 // Notify client.
231 callback.Run(object_path, success);
232 }
233
234 // Called when a response for FindAdapter() is received.
235 void OnFindAdapter(const AdapterCallback& callback,
236 dbus::Response* response) {
237 // Parse response.
238 bool success = false;
239 dbus::ObjectPath object_path;
240 if (response != NULL) {
241 dbus::MessageReader reader(response);
242 if (!reader.PopObjectPath(&object_path)) {
243 LOG(WARNING) << "FindAdapter response has incorrect parameters: "
244 << response->ToString();
245 } else {
246 success = true;
247 }
248 } else {
249 LOG(WARNING) << "Failed to find adapter.";
250 }
251
252 // Notify client.
253 callback.Run(object_path, success);
254 }
255
256 // D-Bus proxy for BlueZ Manager interface.
257 dbus::ObjectProxy* object_proxy_;
258
259 // Properties for BlueZ Manager interface.
260 Properties* properties_;
261
262 // List of observers interested in event notifications from us.
263 ObserverList<Observer> observers_;
264
265 // Weak pointer factory for generating 'this' pointers that might live longer
266 // than we do.
267 // Note: This should remain the last member so it'll be destroyed and
268 // invalidate its weak pointers before any other members are destroyed.
269 base::WeakPtrFactory<BluetoothManagerClientImpl> weak_ptr_factory_;
270
271 DISALLOW_COPY_AND_ASSIGN(BluetoothManagerClientImpl);
272 };
273
274 BluetoothManagerClient::BluetoothManagerClient() {
275 }
276
277 BluetoothManagerClient::~BluetoothManagerClient() {
278 }
279
280 BluetoothManagerClient* BluetoothManagerClient::Create(
281 DBusClientImplementationType type,
282 dbus::Bus* bus) {
283 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
284 return new BluetoothManagerClientImpl(bus);
285 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
286 return new FakeOldBluetoothManagerClient();
287 }
288
289 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/dbus/bluetooth_manager_client.h ('k') | chromeos/dbus/bluetooth_node_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698