| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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_le_advertising_manager_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/observer_list.h" | |
| 10 #include "dbus/bus.h" | |
| 11 #include "dbus/message.h" | |
| 12 #include "dbus/object_manager.h" | |
| 13 #include "dbus/object_proxy.h" | |
| 14 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 15 | |
| 16 namespace chromeos { | |
| 17 | |
| 18 const char BluetoothLEAdvertisingManagerClient::kNoResponseError[] = | |
| 19 "org.chromium.Error.NoResponse"; | |
| 20 | |
| 21 // The BluetoothAdvertisementManagerClient implementation used in production. | |
| 22 class BluetoothAdvertisementManagerClientImpl | |
| 23 : public BluetoothLEAdvertisingManagerClient, | |
| 24 public dbus::ObjectManager::Interface { | |
| 25 public: | |
| 26 BluetoothAdvertisementManagerClientImpl() | |
| 27 : object_manager_(NULL), weak_ptr_factory_(this) {} | |
| 28 | |
| 29 ~BluetoothAdvertisementManagerClientImpl() override { | |
| 30 if (object_manager_) { | |
| 31 object_manager_->UnregisterInterface( | |
| 32 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 // BluetoothAdapterClient override. | |
| 37 void AddObserver( | |
| 38 BluetoothLEAdvertisingManagerClient::Observer* observer) override { | |
| 39 DCHECK(observer); | |
| 40 observers_.AddObserver(observer); | |
| 41 } | |
| 42 | |
| 43 // BluetoothAdapterClient override. | |
| 44 void RemoveObserver( | |
| 45 BluetoothLEAdvertisingManagerClient::Observer* observer) override { | |
| 46 DCHECK(observer); | |
| 47 observers_.RemoveObserver(observer); | |
| 48 } | |
| 49 | |
| 50 // dbus::ObjectManager::Interface override. | |
| 51 dbus::PropertySet* CreateProperties( | |
| 52 dbus::ObjectProxy* object_proxy, | |
| 53 const dbus::ObjectPath& object_path, | |
| 54 const std::string& interface_name) override { | |
| 55 return new dbus::PropertySet(object_proxy, interface_name, | |
| 56 dbus::PropertySet::PropertyChangedCallback()); | |
| 57 } | |
| 58 | |
| 59 // BluetoothAdvertisementManagerClient override. | |
| 60 void RegisterAdvertisement(const dbus::ObjectPath& manager_object_path, | |
| 61 const dbus::ObjectPath& advertisement_object_path, | |
| 62 const base::Closure& callback, | |
| 63 const ErrorCallback& error_callback) override { | |
| 64 dbus::MethodCall method_call( | |
| 65 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface, | |
| 66 bluetooth_advertising_manager::kRegisterAdvertisement); | |
| 67 | |
| 68 dbus::MessageWriter writer(&method_call); | |
| 69 writer.AppendObjectPath(advertisement_object_path); | |
| 70 | |
| 71 // Empty dictionary for options. | |
| 72 dbus::MessageWriter array_writer(NULL); | |
| 73 writer.OpenArray("{sv}", &array_writer); | |
| 74 writer.CloseContainer(&array_writer); | |
| 75 | |
| 76 DCHECK(object_manager_); | |
| 77 dbus::ObjectProxy* object_proxy = | |
| 78 object_manager_->GetObjectProxy(manager_object_path); | |
| 79 object_proxy->CallMethodWithErrorCallback( | |
| 80 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 81 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, | |
| 82 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 83 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, | |
| 84 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 85 } | |
| 86 | |
| 87 // BluetoothAdvertisementManagerClient override. | |
| 88 void UnregisterAdvertisement( | |
| 89 const dbus::ObjectPath& manager_object_path, | |
| 90 const dbus::ObjectPath& advertisement_object_path, | |
| 91 const base::Closure& callback, | |
| 92 const ErrorCallback& error_callback) override { | |
| 93 dbus::MethodCall method_call( | |
| 94 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface, | |
| 95 bluetooth_advertising_manager::kUnregisterAdvertisement); | |
| 96 | |
| 97 dbus::MessageWriter writer(&method_call); | |
| 98 writer.AppendObjectPath(advertisement_object_path); | |
| 99 | |
| 100 DCHECK(object_manager_); | |
| 101 dbus::ObjectProxy* object_proxy = | |
| 102 object_manager_->GetObjectProxy(manager_object_path); | |
| 103 object_proxy->CallMethodWithErrorCallback( | |
| 104 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 105 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, | |
| 106 weak_ptr_factory_.GetWeakPtr(), callback), | |
| 107 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, | |
| 108 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
| 109 } | |
| 110 | |
| 111 protected: | |
| 112 void Init(dbus::Bus* bus) override { | |
| 113 DCHECK(bus); | |
| 114 object_manager_ = bus->GetObjectManager( | |
| 115 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | |
| 116 dbus::ObjectPath( | |
| 117 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | |
| 118 object_manager_->RegisterInterface( | |
| 119 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface, | |
| 120 this); | |
| 121 } | |
| 122 | |
| 123 private: | |
| 124 // Called by dbus::ObjectManager when an object with the advertising manager | |
| 125 // interface is created. Informs observers. | |
| 126 void ObjectAdded(const dbus::ObjectPath& object_path, | |
| 127 const std::string& interface_name) override { | |
| 128 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer, observers_, | |
| 129 AdvertisingManagerAdded(object_path)); | |
| 130 } | |
| 131 | |
| 132 // Called by dbus::ObjectManager when an object with the advertising manager | |
| 133 // interface is removed. Informs observers. | |
| 134 void ObjectRemoved(const dbus::ObjectPath& object_path, | |
| 135 const std::string& interface_name) override { | |
| 136 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer, observers_, | |
| 137 AdvertisingManagerRemoved(object_path)); | |
| 138 } | |
| 139 | |
| 140 // Called when a response for successful method call is received. | |
| 141 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | |
| 142 DCHECK(response); | |
| 143 callback.Run(); | |
| 144 } | |
| 145 | |
| 146 // Called when a response for a failed method call is received. | |
| 147 void OnError(const ErrorCallback& error_callback, | |
| 148 dbus::ErrorResponse* response) { | |
| 149 // Error response has optional error message argument. | |
| 150 std::string error_name; | |
| 151 std::string error_message; | |
| 152 if (response) { | |
| 153 dbus::MessageReader reader(response); | |
| 154 error_name = response->GetErrorName(); | |
| 155 reader.PopString(&error_message); | |
| 156 } else { | |
| 157 error_name = kNoResponseError; | |
| 158 error_message = ""; | |
| 159 } | |
| 160 error_callback.Run(error_name, error_message); | |
| 161 } | |
| 162 | |
| 163 dbus::ObjectManager* object_manager_; | |
| 164 | |
| 165 // List of observers interested in event notifications from us. | |
| 166 base::ObserverList<BluetoothLEAdvertisingManagerClient::Observer> observers_; | |
| 167 | |
| 168 // Weak pointer factory for generating 'this' pointers that might live longer | |
| 169 // than we do. | |
| 170 // Note: This should remain the last member so it'll be destroyed and | |
| 171 // invalidate its weak pointers before any other members are destroyed. | |
| 172 base::WeakPtrFactory<BluetoothAdvertisementManagerClientImpl> | |
| 173 weak_ptr_factory_; | |
| 174 | |
| 175 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl); | |
| 176 }; | |
| 177 | |
| 178 BluetoothLEAdvertisingManagerClient::BluetoothLEAdvertisingManagerClient() { | |
| 179 } | |
| 180 | |
| 181 BluetoothLEAdvertisingManagerClient::~BluetoothLEAdvertisingManagerClient() { | |
| 182 } | |
| 183 | |
| 184 BluetoothLEAdvertisingManagerClient* | |
| 185 BluetoothLEAdvertisingManagerClient::Create() { | |
| 186 return new BluetoothAdvertisementManagerClientImpl(); | |
| 187 } | |
| 188 | |
| 189 } // namespace chromeos | |
| OLD | NEW |