Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/threading/platform_thread.h" | |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 15 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h" | |
| 16 #include "dbus/bus.h" | |
| 17 #include "dbus/exported_object.h" | |
| 18 #include "dbus/message.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // TODO(mcchou): Move these constants to dbus/service_constants.h. | |
| 23 // Bluetooth Media Endpoint service identifier. | |
| 24 const char kBluetoothMediaEndpointInterface[] = "org.bluez.MediaEndpoint1"; | |
| 25 | |
| 26 // Bluetooth Media Endpoint methods. | |
| 27 const char kSetConfiguration[] = "SetConfiguration"; | |
| 28 const char kSelectConfiguration[] = "SelectConfiguration"; | |
| 29 const char kClearConfiguration[] = "ClearConfiguration"; | |
| 30 const char kRelease[] = "Release"; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 namespace chromeos { | |
| 35 | |
| 36 // The BluetoothMediaEndopintServiceProvider implementation used in production. | |
| 37 class CHROMEOS_EXPORT BluetoothMediaEndpointServiceProviderImpl | |
| 38 : public BluetoothMediaEndpointServiceProvider { | |
| 39 public: | |
| 40 BluetoothMediaEndpointServiceProviderImpl(dbus::Bus* bus, | |
| 41 const dbus::ObjectPath& object_path, | |
| 42 Delegate* delegate) | |
| 43 : origin_thread_id_(base::PlatformThread::CurrentId()), | |
| 44 bus_(bus), | |
| 45 delegate_(delegate), | |
| 46 object_path_(object_path), | |
| 47 weak_ptr_factory_(this) { | |
| 48 VLOG(1) << "Createing Bluetooth Media Endpoint: " << object_path_.value(); | |
|
Ben Chan
2014/11/16 18:57:56
nit: perhaps adding CHECK()s for any assumption yo
Miao
2014/11/17 20:51:02
Done.
| |
| 49 | |
| 50 exported_object_ = bus_->GetExportedObject(object_path_); | |
| 51 | |
| 52 exported_object_->ExportMethod( | |
| 53 kBluetoothMediaEndpointInterface, | |
| 54 kSetConfiguration, | |
| 55 base::Bind( | |
| 56 &BluetoothMediaEndpointServiceProviderImpl::SetConfiguration, | |
| 57 weak_ptr_factory_.GetWeakPtr()), | |
| 58 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
| 59 weak_ptr_factory_.GetWeakPtr())); | |
| 60 | |
| 61 exported_object_->ExportMethod( | |
| 62 kBluetoothMediaEndpointInterface, | |
| 63 kSelectConfiguration, | |
| 64 base::Bind( | |
| 65 &BluetoothMediaEndpointServiceProviderImpl::SelectConfiguration, | |
| 66 weak_ptr_factory_.GetWeakPtr()), | |
| 67 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
| 68 weak_ptr_factory_.GetWeakPtr())); | |
| 69 | |
| 70 exported_object_->ExportMethod( | |
| 71 kBluetoothMediaEndpointInterface, | |
| 72 kClearConfiguration, | |
| 73 base::Bind( | |
| 74 &BluetoothMediaEndpointServiceProviderImpl::ClearConfiguration, | |
| 75 weak_ptr_factory_.GetWeakPtr()), | |
| 76 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
| 77 weak_ptr_factory_.GetWeakPtr())); | |
| 78 | |
| 79 exported_object_->ExportMethod( | |
| 80 kBluetoothMediaEndpointInterface, | |
| 81 kRelease, | |
| 82 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::Release, | |
| 83 weak_ptr_factory_.GetWeakPtr()), | |
| 84 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
| 85 weak_ptr_factory_.GetWeakPtr())); | |
| 86 } | |
| 87 | |
| 88 ~BluetoothMediaEndpointServiceProviderImpl() override { | |
| 89 VLOG(1) << "Clearing uip Bluetooth Media Endpoint: " | |
| 90 << object_path_.value(); | |
| 91 | |
| 92 bus_->UnregisterExportedObject(object_path_); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 // Returns true if the current thread is on the origin thread, false | |
| 97 // otherwise. | |
| 98 bool OnOriginThread() { | |
| 99 return base::PlatformThread::CurrentId() == origin_thread_id_; | |
| 100 } | |
| 101 | |
| 102 // Called by dbus:: when a method is exported. | |
| 103 void OnExported(const std::string& interface_name, | |
| 104 const std::string& method_name, | |
| 105 bool success) { | |
| 106 LOG_IF(WARNING, !success) << "Failed to export " | |
| 107 << interface_name << "." << method_name; | |
| 108 } | |
| 109 | |
| 110 // Called by dbus:: when the remote device connects to the Media Endpoint. | |
| 111 void SetConfiguration(dbus::MethodCall* method_call, | |
| 112 dbus::ExportedObject::ResponseSender response_sender) { | |
| 113 DCHECK(OnOriginThread()); | |
| 114 DCHECK(delegate_); | |
| 115 | |
| 116 dbus::MessageReader reader(method_call); | |
| 117 dbus::ObjectPath transport_path; | |
| 118 dbus::MessageReader properties(method_call); | |
| 119 if (!reader.PopObjectPath(&transport_path) || | |
| 120 !reader.PopArray(&properties)) { | |
| 121 LOG(WARNING) << "SetConfiguration called with incorrect parameters: " | |
| 122 << method_call->ToString(); | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 delegate_->SetConfiguration(transport_path, properties); | |
| 127 | |
| 128 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
| 129 } | |
| 130 | |
| 131 // Called by dbus:: when the remote received the configuration for media | |
| 132 // transport. | |
| 133 void SelectConfiguration( | |
| 134 dbus::MethodCall* method_call, | |
| 135 dbus::ExportedObject::ResponseSender response_sender) { | |
| 136 DCHECK(OnOriginThread()); | |
| 137 DCHECK(delegate_); | |
| 138 | |
| 139 dbus::MessageReader reader(method_call); | |
| 140 const uint8_t* capabilities = nullptr; | |
| 141 size_t length = 0; | |
| 142 if (!reader.PopArrayOfBytes(&capabilities, &length)) { | |
| 143 LOG(WARNING) << "SelectConfiguration called with incorrect parameters: " | |
| 144 << method_call->ToString(); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 // |delegate_| generates the response to |SelectConfiguration| and send it | |
| 149 // back via |callback|. | |
| 150 Delegate::SelectConfigurationCallback callback = base::Bind( | |
| 151 &BluetoothMediaEndpointServiceProviderImpl::OnConfiguration, | |
| 152 weak_ptr_factory_.GetWeakPtr(), | |
| 153 method_call, | |
| 154 response_sender); | |
| 155 | |
| 156 delegate_->SelectConfiguration(capabilities, length, callback); | |
| 157 } | |
| 158 | |
| 159 // Called by dbus:: when the remote device is about to close the connection. | |
| 160 void ClearConfiguration( | |
| 161 dbus::MethodCall* method_call, | |
| 162 dbus::ExportedObject::ResponseSender response_sender) { | |
| 163 DCHECK(OnOriginThread()); | |
| 164 DCHECK(delegate_); | |
| 165 | |
| 166 dbus::MessageReader reader(method_call); | |
| 167 dbus::ObjectPath transport_path; | |
| 168 if (!reader.PopObjectPath(&transport_path)) { | |
| 169 LOG(WARNING) << "ClearConfiguration called with incorrect parameters: " | |
| 170 << method_call->ToString(); | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 delegate_->ClearConfiguration(transport_path); | |
| 175 | |
| 176 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
| 177 } | |
| 178 | |
| 179 // Called by Bluetooth daemon to do the clean up after unregistering the Media | |
| 180 // Endpoint. | |
| 181 void Release(dbus::MethodCall* method_call, | |
| 182 dbus::ExportedObject::ResponseSender response_sender) { | |
| 183 DCHECK(OnOriginThread()); | |
| 184 DCHECK(delegate_); | |
| 185 | |
| 186 delegate_->Release(); | |
| 187 | |
| 188 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
| 189 } | |
| 190 | |
| 191 // Called by Delegate to response to a method requiring transport | |
| 192 // configuration. | |
| 193 void OnConfiguration(dbus::MethodCall* method_call, | |
| 194 dbus::ExportedObject::ResponseSender response_sender, | |
| 195 const uint8_t* configuration, | |
| 196 size_t length) { | |
| 197 DCHECK(OnOriginThread()); | |
| 198 | |
| 199 // Generates response to the method call. | |
| 200 scoped_ptr<dbus::Response> response( | |
| 201 dbus::Response::FromMethodCall(method_call)); | |
| 202 dbus::MessageWriter writer(response.get()); | |
| 203 writer.AppendArrayOfBytes(configuration, length); | |
| 204 response_sender.Run(response.Pass()); | |
| 205 } | |
| 206 | |
| 207 base::PlatformThreadId origin_thread_id_; | |
| 208 | |
| 209 dbus::Bus* bus_; | |
| 210 | |
| 211 Delegate* delegate_; | |
| 212 | |
| 213 dbus::ObjectPath object_path_; | |
| 214 | |
| 215 scoped_refptr<dbus::ExportedObject> exported_object_; | |
| 216 | |
| 217 base::WeakPtrFactory<BluetoothMediaEndpointServiceProviderImpl> | |
| 218 weak_ptr_factory_; | |
| 219 | |
| 220 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaEndpointServiceProviderImpl); | |
| 221 }; | |
| 222 | |
| 223 BluetoothMediaEndpointServiceProvider::BluetoothMediaEndpointServiceProvider() { | |
| 224 } | |
| 225 | |
| 226 BluetoothMediaEndpointServiceProvider::~BluetoothMediaEndpointServiceProvider() | |
| 227 {} | |
| 228 | |
| 229 BluetoothMediaEndpointServiceProvider* | |
| 230 BluetoothMediaEndpointServiceProvider::Create( | |
| 231 dbus::Bus* bus, | |
| 232 const dbus::ObjectPath& object_path, | |
| 233 Delegate* delegate) { | |
| 234 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { | |
| 235 return new BluetoothMediaEndpointServiceProviderImpl( | |
| 236 bus, object_path, delegate); | |
| 237 } else { | |
| 238 return new FakeBluetoothMediaEndpointServiceProvider(object_path, delegate); | |
| 239 } | |
| 240 } | |
| 241 | |
| 242 } // namespace chromeos | |
| OLD | NEW |