OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/experimental_bluetooth_adapter_client.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "chromeos/dbus/bluetooth_property.h" |
| 12 #include "dbus/bus.h" |
| 13 #include "dbus/message.h" |
| 14 #include "dbus/object_manager.h" |
| 15 #include "dbus/object_path.h" |
| 16 #include "dbus/object_proxy.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 const char ExperimentalBluetoothAdapterClient::kNoResponseError[] = |
| 22 "org.chromium.Error.NoResponse"; |
| 23 const char ExperimentalBluetoothAdapterClient::kUnknownAdapterError[] = |
| 24 "org.chromium.Error.UnknownAdapter"; |
| 25 |
| 26 ExperimentalBluetoothAdapterClient::Properties::Properties( |
| 27 dbus::ObjectProxy* object_proxy, |
| 28 const std::string& interface_name, |
| 29 const PropertyChangedCallback& callback) |
| 30 : dbus::PropertySet(object_proxy, interface_name, callback) { |
| 31 RegisterProperty(bluetooth_adapter::kAddressProperty, &address); |
| 32 RegisterProperty(bluetooth_adapter::kNameProperty, &name); |
| 33 RegisterProperty(bluetooth_adapter::kAliasProperty, &alias); |
| 34 RegisterProperty(bluetooth_adapter::kClassProperty, &bluetooth_class); |
| 35 RegisterProperty(bluetooth_adapter::kPoweredProperty, &powered); |
| 36 RegisterProperty(bluetooth_adapter::kDiscoverableProperty, &discoverable); |
| 37 RegisterProperty(bluetooth_adapter::kPairableProperty, &pairable); |
| 38 RegisterProperty(bluetooth_adapter::kPairableTimeoutProperty, |
| 39 &pairable_timeout); |
| 40 RegisterProperty(bluetooth_adapter::kDiscoverableTimeoutProperty, |
| 41 &discoverable_timeout); |
| 42 RegisterProperty(bluetooth_adapter::kDiscoveringProperty, &discovering); |
| 43 RegisterProperty(bluetooth_adapter::kUUIDsProperty, &uuids); |
| 44 RegisterProperty(bluetooth_adapter::kModaliasProperty, &modalias); |
| 45 } |
| 46 |
| 47 ExperimentalBluetoothAdapterClient::Properties::~Properties() { |
| 48 } |
| 49 |
| 50 |
| 51 // The ExperimentalBluetoothAdapterClient implementation used in production. |
| 52 class ExperimentalBluetoothAdapterClientImpl |
| 53 : public ExperimentalBluetoothAdapterClient, |
| 54 public dbus::ObjectManager::Interface { |
| 55 public: |
| 56 explicit ExperimentalBluetoothAdapterClientImpl(dbus::Bus* bus) |
| 57 : bus_(bus), |
| 58 weak_ptr_factory_(this) { |
| 59 object_manager_ = bus_->GetObjectManager( |
| 60 bluetooth_manager::kBluetoothManagerServiceName, |
| 61 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath)); |
| 62 object_manager_->RegisterInterface( |
| 63 bluetooth_adapter::kExperimentalBluetoothAdapterInterface, this); |
| 64 } |
| 65 |
| 66 virtual ~ExperimentalBluetoothAdapterClientImpl() { |
| 67 object_manager_->UnregisterInterface( |
| 68 bluetooth_adapter::kExperimentalBluetoothAdapterInterface); |
| 69 } |
| 70 |
| 71 // ExperimentalBluetoothAdapterClient override. |
| 72 virtual void AddObserver( |
| 73 ExperimentalBluetoothAdapterClient::Observer* observer) OVERRIDE { |
| 74 DCHECK(observer); |
| 75 observers_.AddObserver(observer); |
| 76 } |
| 77 |
| 78 // ExperimentalBluetoothAdapterClient override. |
| 79 virtual void RemoveObserver( |
| 80 ExperimentalBluetoothAdapterClient::Observer* observer) OVERRIDE { |
| 81 DCHECK(observer); |
| 82 observers_.RemoveObserver(observer); |
| 83 } |
| 84 |
| 85 // Returns the list of adapter object paths known to the system. |
| 86 virtual std::vector<dbus::ObjectPath> GetAdapters() { |
| 87 return object_manager_->GetObjectsWithInterface( |
| 88 bluetooth_adapter::kExperimentalBluetoothAdapterInterface); |
| 89 } |
| 90 |
| 91 // dbus::ObjectManager::Interface override. |
| 92 virtual dbus::PropertySet* CreateProperties( |
| 93 dbus::ObjectProxy* object_proxy, |
| 94 const dbus::ObjectPath& object_path, |
| 95 const std::string& interface_name) { |
| 96 Properties* properties = new Properties( |
| 97 object_proxy, interface_name, |
| 98 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnPropertyChanged, |
| 99 weak_ptr_factory_.GetWeakPtr(), |
| 100 object_path)); |
| 101 return static_cast<dbus::PropertySet*>(properties); |
| 102 } |
| 103 |
| 104 // ExperimentalBluetoothAdapterClient override. |
| 105 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) |
| 106 OVERRIDE { |
| 107 return static_cast<Properties*>( |
| 108 object_manager_->GetProperties( |
| 109 object_path, |
| 110 bluetooth_adapter::kExperimentalBluetoothAdapterInterface)); |
| 111 } |
| 112 |
| 113 // ExperimentalBluetoothAdapterClient override. |
| 114 virtual void StartDiscovery(const dbus::ObjectPath& object_path, |
| 115 const base::Closure& callback, |
| 116 const ErrorCallback& error_callback) OVERRIDE { |
| 117 dbus::MethodCall method_call( |
| 118 bluetooth_adapter::kExperimentalBluetoothAdapterInterface, |
| 119 bluetooth_adapter::kStartDiscovery); |
| 120 |
| 121 dbus::ObjectProxy* object_proxy = |
| 122 object_manager_->GetObjectProxy(object_path); |
| 123 if (!object_proxy) { |
| 124 error_callback.Run(kUnknownAdapterError, ""); |
| 125 return; |
| 126 } |
| 127 |
| 128 object_proxy->CallMethodWithErrorCallback( |
| 129 &method_call, |
| 130 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 131 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnSuccess, |
| 132 weak_ptr_factory_.GetWeakPtr(), callback), |
| 133 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnError, |
| 134 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 135 } |
| 136 |
| 137 // ExperimentalBluetoothAdapterClient override. |
| 138 virtual void StopDiscovery(const dbus::ObjectPath& object_path, |
| 139 const base::Closure& callback, |
| 140 const ErrorCallback& error_callback) OVERRIDE { |
| 141 dbus::MethodCall method_call( |
| 142 bluetooth_adapter::kExperimentalBluetoothAdapterInterface, |
| 143 bluetooth_adapter::kStopDiscovery); |
| 144 |
| 145 dbus::ObjectProxy* object_proxy = |
| 146 object_manager_->GetObjectProxy(object_path); |
| 147 if (!object_proxy) { |
| 148 error_callback.Run(kUnknownAdapterError, ""); |
| 149 return; |
| 150 } |
| 151 |
| 152 object_proxy->CallMethodWithErrorCallback( |
| 153 &method_call, |
| 154 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 155 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnSuccess, |
| 156 weak_ptr_factory_.GetWeakPtr(), callback), |
| 157 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnError, |
| 158 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 159 } |
| 160 |
| 161 // ExperimentalBluetoothAdapterClient override. |
| 162 virtual void RemoveDevice(const dbus::ObjectPath& object_path, |
| 163 const dbus::ObjectPath& device_path, |
| 164 const base::Closure& callback, |
| 165 const ErrorCallback& error_callback) OVERRIDE { |
| 166 dbus::MethodCall method_call( |
| 167 bluetooth_adapter::kExperimentalBluetoothAdapterInterface, |
| 168 bluetooth_adapter::kRemoveDevice); |
| 169 |
| 170 dbus::MessageWriter writer(&method_call); |
| 171 writer.AppendObjectPath(device_path); |
| 172 |
| 173 dbus::ObjectProxy* object_proxy = |
| 174 object_manager_->GetObjectProxy(object_path); |
| 175 if (!object_proxy) { |
| 176 error_callback.Run(kUnknownAdapterError, ""); |
| 177 return; |
| 178 } |
| 179 |
| 180 object_proxy->CallMethodWithErrorCallback( |
| 181 &method_call, |
| 182 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 183 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnSuccess, |
| 184 weak_ptr_factory_.GetWeakPtr(), callback), |
| 185 base::Bind(&ExperimentalBluetoothAdapterClientImpl::OnError, |
| 186 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
| 187 } |
| 188 |
| 189 private: |
| 190 // Called by dbus::ObjectManager when an object with the adapter interface |
| 191 // is created. Informs observers. |
| 192 void ObjectAdded(const dbus::ObjectPath& object_path, |
| 193 const std::string& interface_name) OVERRIDE { |
| 194 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, |
| 195 AdapterAdded(object_path)); |
| 196 } |
| 197 |
| 198 // Called by dbus::ObjectManager when an object with the adapter interface |
| 199 // is removed. Informs observers. |
| 200 void ObjectRemoved(const dbus::ObjectPath& object_path, |
| 201 const std::string& interface_name) OVERRIDE { |
| 202 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, |
| 203 AdapterRemoved(object_path)); |
| 204 } |
| 205 |
| 206 // Called by dbus::PropertySet when a property value is changed, |
| 207 // either by result of a signal or response to a GetAll() or Get() |
| 208 // call. Informs observers. |
| 209 void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 210 const std::string& property_name) { |
| 211 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, |
| 212 AdapterPropertyChanged(object_path, property_name)); |
| 213 } |
| 214 |
| 215 // Called when a response for successful method call is received. |
| 216 void OnSuccess(const base::Closure& callback, |
| 217 dbus::Response* response) { |
| 218 DCHECK(response); |
| 219 callback.Run(); |
| 220 } |
| 221 |
| 222 // Called when a response for a failed method call is received. |
| 223 void OnError(const ErrorCallback& error_callback, |
| 224 dbus::ErrorResponse* response) { |
| 225 // Error response has optional error message argument. |
| 226 std::string error_name; |
| 227 std::string error_message; |
| 228 if (response) { |
| 229 dbus::MessageReader reader(response); |
| 230 error_name = response->GetErrorName(); |
| 231 reader.PopString(&error_message); |
| 232 } else { |
| 233 error_name = kNoResponseError; |
| 234 error_message = ""; |
| 235 } |
| 236 error_callback.Run(error_name, error_message); |
| 237 } |
| 238 |
| 239 dbus::Bus* bus_; |
| 240 dbus::ObjectManager* object_manager_; |
| 241 |
| 242 // List of observers interested in event notifications from us. |
| 243 ObserverList<ExperimentalBluetoothAdapterClient::Observer> observers_; |
| 244 |
| 245 // Weak pointer factory for generating 'this' pointers that might live longer |
| 246 // than we do. |
| 247 // Note: This should remain the last member so it'll be destroyed and |
| 248 // invalidate its weak pointers before any other members are destroyed. |
| 249 base::WeakPtrFactory<ExperimentalBluetoothAdapterClientImpl> |
| 250 weak_ptr_factory_; |
| 251 |
| 252 DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothAdapterClientImpl); |
| 253 }; |
| 254 |
| 255 // The ExperimentalBluetoothAdapterClient implementation used on Linux desktop, |
| 256 // which does nothing. |
| 257 class ExperimentalBluetoothAdapterClientStubImpl |
| 258 : public ExperimentalBluetoothAdapterClient { |
| 259 public: |
| 260 struct Properties : public ExperimentalBluetoothAdapterClient::Properties { |
| 261 explicit Properties(const PropertyChangedCallback& callback) |
| 262 : ExperimentalBluetoothAdapterClient::Properties( |
| 263 NULL, |
| 264 bluetooth_adapter::kExperimentalBluetoothAdapterInterface, |
| 265 callback) { |
| 266 } |
| 267 |
| 268 virtual ~Properties() { |
| 269 } |
| 270 |
| 271 virtual void Get(dbus::PropertyBase* property, |
| 272 dbus::PropertySet::GetCallback callback) OVERRIDE { |
| 273 VLOG(1) << "Get " << property->name(); |
| 274 callback.Run(false); |
| 275 } |
| 276 |
| 277 virtual void GetAll() OVERRIDE { |
| 278 VLOG(1) << "GetAll"; |
| 279 } |
| 280 |
| 281 virtual void Set(dbus::PropertyBase *property, |
| 282 dbus::PropertySet::SetCallback callback) OVERRIDE { |
| 283 VLOG(1) << "Set " << property->name(); |
| 284 if (property->name() == "Powered") { |
| 285 property->ReplaceValueWithSetValue(); |
| 286 NotifyPropertyChanged(property->name()); |
| 287 callback.Run(true); |
| 288 } else { |
| 289 callback.Run(false); |
| 290 } |
| 291 } |
| 292 }; |
| 293 |
| 294 ExperimentalBluetoothAdapterClientStubImpl() { |
| 295 properties_.reset(new Properties(base::Bind( |
| 296 &ExperimentalBluetoothAdapterClientStubImpl::OnPropertyChanged, |
| 297 base::Unretained(this)))); |
| 298 |
| 299 properties_->address.ReplaceValue("hci0"); |
| 300 properties_->name.ReplaceValue("Fake Adapter"); |
| 301 properties_->pairable.ReplaceValue(true); |
| 302 } |
| 303 |
| 304 // ExperimentalBluetoothAdapterClient override. |
| 305 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 306 observers_.AddObserver(observer); |
| 307 } |
| 308 |
| 309 // ExperimentalBluetoothAdapterClient override. |
| 310 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 311 observers_.RemoveObserver(observer); |
| 312 } |
| 313 |
| 314 // ExperimentalBluetoothAdapterClient override. |
| 315 virtual std::vector<dbus::ObjectPath> GetAdapters() OVERRIDE { |
| 316 std::vector<dbus::ObjectPath> object_paths; |
| 317 object_paths.push_back(dbus::ObjectPath("/fake/hci0")); |
| 318 return object_paths; |
| 319 } |
| 320 |
| 321 // ExperimentalBluetoothAdapterClient override. |
| 322 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) |
| 323 OVERRIDE { |
| 324 VLOG(1) << "GetProperties: " << object_path.value(); |
| 325 if (object_path.value() == "/fake/hci0") |
| 326 return properties_.get(); |
| 327 else |
| 328 return NULL; |
| 329 } |
| 330 |
| 331 // ExperimentalBluetoothAdapterClient override. |
| 332 virtual void StartDiscovery(const dbus::ObjectPath& object_path, |
| 333 const base::Closure& callback, |
| 334 const ErrorCallback& error_callback) OVERRIDE { |
| 335 VLOG(1) << "StartDiscovery: " << object_path.value(); |
| 336 error_callback.Run(kNoResponseError, ""); |
| 337 } |
| 338 |
| 339 // ExperimentalBluetoothAdapterClient override. |
| 340 virtual void StopDiscovery(const dbus::ObjectPath& object_path, |
| 341 const base::Closure& callback, |
| 342 const ErrorCallback& error_callback) OVERRIDE { |
| 343 VLOG(1) << "StopDiscovery: " << object_path.value(); |
| 344 error_callback.Run(kNoResponseError, ""); |
| 345 } |
| 346 |
| 347 // ExperimentalBluetoothAdapterClient override. |
| 348 virtual void RemoveDevice(const dbus::ObjectPath& object_path, |
| 349 const dbus::ObjectPath& device_path, |
| 350 const base::Closure& callback, |
| 351 const ErrorCallback& error_callback) OVERRIDE { |
| 352 VLOG(1) << "RemoveDevice: " << object_path.value() |
| 353 << " " << device_path.value(); |
| 354 error_callback.Run(kNoResponseError, ""); |
| 355 } |
| 356 |
| 357 private: |
| 358 void OnPropertyChanged(const std::string& property_name) { |
| 359 FOR_EACH_OBSERVER(ExperimentalBluetoothAdapterClient::Observer, observers_, |
| 360 AdapterPropertyChanged(dbus::ObjectPath("/fake/hci0"), |
| 361 property_name)); |
| 362 } |
| 363 |
| 364 // List of observers interested in event notifications from us. |
| 365 ObserverList<Observer> observers_; |
| 366 |
| 367 // Static properties we return. |
| 368 scoped_ptr<Properties> properties_; |
| 369 }; |
| 370 |
| 371 ExperimentalBluetoothAdapterClient::ExperimentalBluetoothAdapterClient() { |
| 372 } |
| 373 |
| 374 ExperimentalBluetoothAdapterClient::~ExperimentalBluetoothAdapterClient() { |
| 375 } |
| 376 |
| 377 ExperimentalBluetoothAdapterClient* ExperimentalBluetoothAdapterClient::Create( |
| 378 DBusClientImplementationType type, |
| 379 dbus::Bus* bus) { |
| 380 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 381 return new ExperimentalBluetoothAdapterClientImpl(bus); |
| 382 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 383 return new ExperimentalBluetoothAdapterClientStubImpl(); |
| 384 } |
| 385 |
| 386 } // namespace chromeos |
OLD | NEW |