Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_media_transport_client.cc
blob7672ea38aeb385dc4841e708168247b147434272
1 // Copyright 2014 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.
5 #include "chromeos/dbus/bluetooth_media_transport_client.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/observer_list.h"
11 #include "dbus/bus.h"
12 #include "dbus/message.h"
13 #include "dbus/object_manager.h"
14 #include "dbus/object_proxy.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 namespace {
19 // TODO(mcchou): Add these service constants into dbus/service_constants.h
20 // later.
21 const char kBluetoothMediaTransportInterface[] = "org.bluez.MediaTransport1";
23 // Constants used to indicate exceptional error conditions.
24 const char kNoResponseError[] = "org.chromium.Error.NoResponse";
25 const char kUnexpectedResponse[] = "org.chromium.Error.UnexpectedResponse";
27 // Method names of Media Transport interface.
28 const char kAcquire[] = "Acquire";
29 const char kTryAcquire[] = "TryAcquire";
30 const char kRelease[] = "Release";
32 } // namespace
34 namespace chromeos {
36 // static
37 const char BluetoothMediaTransportClient::kDeviceProperty[] = "Device";
38 const char BluetoothMediaTransportClient::kUUIDProperty[] = "UUID";
39 const char BluetoothMediaTransportClient::kCodecProperty[] = "Codec";
40 const char BluetoothMediaTransportClient::kConfigurationProperty[] =
41 "Configuration";
42 const char BluetoothMediaTransportClient::kStateProperty[] = "State";
43 const char BluetoothMediaTransportClient::kDelayProperty[] = "Delay";
44 const char BluetoothMediaTransportClient::kVolumeProperty[] = "Volume";
46 // static
47 const char BluetoothMediaTransportClient::kStateIdle[] = "idle";
48 const char BluetoothMediaTransportClient::kStatePending[] = "pending";
49 const char BluetoothMediaTransportClient::kStateActive[] = "active";
51 BluetoothMediaTransportClient::Properties::Properties(
52 dbus::ObjectProxy* object_proxy,
53 const std::string& interface_name,
54 const PropertyChangedCallback& callback)
55 : dbus::PropertySet(object_proxy, interface_name, callback) {
56 RegisterProperty(kDeviceProperty, &device);
57 RegisterProperty(kUUIDProperty, &uuid);
58 RegisterProperty(kCodecProperty, &codec);
59 RegisterProperty(kConfigurationProperty, &configuration);
60 RegisterProperty(kStateProperty, &state);
61 RegisterProperty(kDelayProperty, &delay);
62 RegisterProperty(kVolumeProperty, &volume);
65 BluetoothMediaTransportClient::Properties::~Properties() {
68 class BluetoothMediaTransportClientImpl
69 : public BluetoothMediaTransportClient,
70 public dbus::ObjectManager::Interface {
71 public:
72 BluetoothMediaTransportClientImpl()
73 : object_manager_(nullptr), weak_ptr_factory_(this) {}
75 ~BluetoothMediaTransportClientImpl() override {
76 object_manager_->UnregisterInterface(kBluetoothMediaTransportInterface);
79 // dbus::ObjectManager::Interface overrides.
81 dbus::PropertySet* CreateProperties(
82 dbus::ObjectProxy* object_proxy,
83 const dbus::ObjectPath& object_path,
84 const std::string& interface_name) override {
85 Properties* properties = new Properties(
86 object_proxy,
87 interface_name,
88 base::Bind(&BluetoothMediaTransportClientImpl::OnPropertyChanged,
89 weak_ptr_factory_.GetWeakPtr(), object_path));
90 return properties;
93 void ObjectAdded(const dbus::ObjectPath& object_path,
94 const std::string& interface_name) override {
95 VLOG(1) << "Remote Media Transport added: " << object_path.value();
96 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer,
97 observers_,
98 MediaTransportAdded(object_path));
101 void ObjectRemoved(const dbus::ObjectPath& object_path,
102 const std::string& interface_name) override {
103 VLOG(1) << "Remote Media Transport removed: " << object_path.value();
104 FOR_EACH_OBSERVER(BluetoothMediaTransportClient::Observer,
105 observers_,
106 MediaTransportRemoved(object_path));
109 // BluetoothMediaTransportClient overrides.
111 void AddObserver(BluetoothMediaTransportClient::Observer* observer) override {
112 DCHECK(observer);
113 observers_.AddObserver(observer);
116 void RemoveObserver(
117 BluetoothMediaTransportClient::Observer* observer) override {
118 DCHECK(observer);
119 observers_.RemoveObserver(observer);
122 Properties* GetProperties(const dbus::ObjectPath& object_path) override {
123 DCHECK(object_manager_);
124 return static_cast<Properties*>(
125 object_manager_->GetProperties(object_path,
126 kBluetoothMediaTransportInterface));
129 void Acquire(const dbus::ObjectPath& object_path,
130 const AcquireCallback& callback,
131 const ErrorCallback& error_callback) override {
132 DCHECK(object_manager_);
134 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, kAcquire);
136 // Get object proxy.
137 scoped_refptr<dbus::ObjectProxy> object_proxy(
138 object_manager_->GetObjectProxy(object_path));
140 // Call Acquire method of Media Transport interface.
141 object_proxy->CallMethodWithErrorCallback(
142 &method_call,
143 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
144 base::Bind(&BluetoothMediaTransportClientImpl::OnAcquireSuccess,
145 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
146 base::Bind(&BluetoothMediaTransportClientImpl::OnError,
147 weak_ptr_factory_.GetWeakPtr(), error_callback));
150 void TryAcquire(const dbus::ObjectPath& object_path,
151 const AcquireCallback& callback,
152 const ErrorCallback& error_callback) override {
153 DCHECK(object_manager_);
155 dbus::MethodCall method_call(kBluetoothMediaTransportInterface,
156 kTryAcquire);
158 // Get object proxy.
159 scoped_refptr<dbus::ObjectProxy> object_proxy(
160 object_manager_->GetObjectProxy(object_path));
162 // Call TryAcquire method of Media Transport interface.
163 object_proxy->CallMethodWithErrorCallback(
164 &method_call,
165 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
166 base::Bind(&BluetoothMediaTransportClientImpl::OnAcquireSuccess,
167 weak_ptr_factory_.GetWeakPtr(), callback, error_callback),
168 base::Bind(&BluetoothMediaTransportClientImpl::OnError,
169 weak_ptr_factory_.GetWeakPtr(), error_callback));
172 void Release(const dbus::ObjectPath& object_path,
173 const base::Closure& callback,
174 const ErrorCallback& error_callback) override {
175 DCHECK(object_manager_);
177 dbus::MethodCall method_call(kBluetoothMediaTransportInterface, kRelease);
179 // Get object proxy.
180 scoped_refptr<dbus::ObjectProxy> object_proxy(
181 object_manager_->GetObjectProxy(object_path));
183 // Call TryAcquire method of Media Transport interface.
184 object_proxy->CallMethodWithErrorCallback(
185 &method_call,
186 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
187 base::Bind(&BluetoothMediaTransportClientImpl::OnSuccess,
188 weak_ptr_factory_.GetWeakPtr(), callback),
189 base::Bind(&BluetoothMediaTransportClientImpl::OnError,
190 weak_ptr_factory_.GetWeakPtr(), error_callback));
193 protected:
194 void Init(dbus::Bus* bus) override {
195 DCHECK(bus);
196 object_manager_ = bus->GetObjectManager(
197 bluetooth_object_manager::kBluetoothObjectManagerServiceName,
198 dbus::ObjectPath(
199 bluetooth_object_manager::kBluetoothObjectManagerServicePath));
200 object_manager_->RegisterInterface(kBluetoothMediaTransportInterface,
201 this);
204 private:
205 // Called by dbus::PropertySet when a property value is changed.
206 void OnPropertyChanged(const dbus::ObjectPath& object_path,
207 const std::string& property_name) {
208 VLOG(1) << "Name of the changed property: " << property_name;
210 // Dispatches the change to the corresponding property-changed handler.
211 FOR_EACH_OBSERVER(
212 BluetoothMediaTransportClient::Observer, observers_,
213 MediaTransportPropertyChanged(object_path, property_name));
216 // Called when a response for successful method call is received.
217 void OnSuccess(const base::Closure& callback,
218 dbus::Response* response) {
219 DCHECK(response);
220 callback.Run();
223 // Called when a response for |Acquire|/|TryAcquire| method call is received.
224 void OnAcquireSuccess(const AcquireCallback& callback,
225 const ErrorCallback& error_callback,
226 dbus::Response* response) {
227 DCHECK(response);
229 dbus::FileDescriptor fd;
230 uint16_t read_mtu;
231 uint16_t write_mtu;
233 // Parse the response.
234 dbus::MessageReader reader(response);
235 if (reader.PopFileDescriptor(&fd) &&
236 reader.PopUint16(&read_mtu) &&
237 reader.PopUint16(&write_mtu)) {
238 callback.Run(fd, read_mtu, write_mtu);
239 return;
242 error_callback.Run(
243 kUnexpectedResponse,
244 "Failed to retrieve file descriptor, read MTU and write MTU.");
247 // Called when a response for a failed method call is received.
248 void OnError(const ErrorCallback& error_callback,
249 dbus::ErrorResponse* response) {
250 DCHECK(response);
252 // Error response has optional error message argument.
253 std::string error_name;
254 std::string error_message;
255 if (response) {
256 dbus::MessageReader reader(response);
257 error_name = response->GetErrorName();
258 reader.PopString(&error_message);
259 } else {
260 error_name = kNoResponseError;
262 error_callback.Run(error_name, error_message);
265 dbus::ObjectManager* object_manager_;
267 // List of observers interested in event notifications from us.
268 ObserverList<BluetoothMediaTransportClient::Observer> observers_;
270 base::WeakPtrFactory<BluetoothMediaTransportClientImpl> weak_ptr_factory_;
272 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaTransportClientImpl);
275 BluetoothMediaTransportClient::BluetoothMediaTransportClient() {
278 BluetoothMediaTransportClient::~BluetoothMediaTransportClient() {
281 BluetoothMediaTransportClient* BluetoothMediaTransportClient::Create() {
282 return new BluetoothMediaTransportClientImpl();
285 } // namespace chromeos