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(); | |
armansito
2014/11/17 21:17:13
nit: s/Createing/Creating/
Miao
2014/11/18 19:04:33
Done.
| |
49 CHECK(bus_); | |
50 CHECK(delegate_); | |
51 CHECK(object_path_.IsValid()); | |
armansito
2014/11/17 21:17:14
Use DCHECKs here please.
Miao
2014/11/18 19:04:33
Done.
| |
52 | |
53 exported_object_ = bus_->GetExportedObject(object_path_); | |
54 | |
55 exported_object_->ExportMethod( | |
56 kBluetoothMediaEndpointInterface, | |
57 kSetConfiguration, | |
58 base::Bind( | |
59 &BluetoothMediaEndpointServiceProviderImpl::SetConfiguration, | |
60 weak_ptr_factory_.GetWeakPtr()), | |
61 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
62 weak_ptr_factory_.GetWeakPtr())); | |
63 | |
64 exported_object_->ExportMethod( | |
65 kBluetoothMediaEndpointInterface, | |
66 kSelectConfiguration, | |
67 base::Bind( | |
68 &BluetoothMediaEndpointServiceProviderImpl::SelectConfiguration, | |
69 weak_ptr_factory_.GetWeakPtr()), | |
70 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
71 weak_ptr_factory_.GetWeakPtr())); | |
72 | |
73 exported_object_->ExportMethod( | |
74 kBluetoothMediaEndpointInterface, | |
75 kClearConfiguration, | |
76 base::Bind( | |
77 &BluetoothMediaEndpointServiceProviderImpl::ClearConfiguration, | |
78 weak_ptr_factory_.GetWeakPtr()), | |
79 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
80 weak_ptr_factory_.GetWeakPtr())); | |
81 | |
82 exported_object_->ExportMethod( | |
83 kBluetoothMediaEndpointInterface, | |
84 kRelease, | |
85 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::Release, | |
86 weak_ptr_factory_.GetWeakPtr()), | |
87 base::Bind(&BluetoothMediaEndpointServiceProviderImpl::OnExported, | |
88 weak_ptr_factory_.GetWeakPtr())); | |
89 } | |
90 | |
91 ~BluetoothMediaEndpointServiceProviderImpl() override { | |
92 VLOG(1) << "Clearing uip Bluetooth Media Endpoint: " | |
Miao
2014/11/18 19:04:33
s/Clearing uip/Cleaning up/
| |
93 << object_path_.value(); | |
94 | |
95 bus_->UnregisterExportedObject(object_path_); | |
96 } | |
97 | |
98 private: | |
99 // Returns true if the current thread is on the origin thread, false | |
100 // otherwise. | |
101 bool OnOriginThread() { | |
102 return base::PlatformThread::CurrentId() == origin_thread_id_; | |
103 } | |
104 | |
105 // Called by dbus:: when a method is exported. | |
106 void OnExported(const std::string& interface_name, | |
107 const std::string& method_name, | |
108 bool success) { | |
109 LOG_IF(WARNING, !success) << "Failed to export " | |
110 << interface_name << "." << method_name; | |
111 } | |
112 | |
113 // Called by dbus:: when the remote device connects to the Media Endpoint. | |
114 void SetConfiguration(dbus::MethodCall* method_call, | |
115 dbus::ExportedObject::ResponseSender response_sender) { | |
116 DCHECK(OnOriginThread()); | |
117 DCHECK(delegate_); | |
118 | |
119 dbus::MessageReader reader(method_call); | |
120 dbus::ObjectPath transport_path; | |
121 dbus::MessageReader properties(method_call); | |
122 if (!reader.PopObjectPath(&transport_path) || | |
123 !reader.PopArray(&properties)) { | |
124 LOG(WARNING) << "SetConfiguration called with incorrect parameters: " | |
125 << method_call->ToString(); | |
126 return; | |
127 } | |
128 | |
129 delegate_->SetConfiguration(transport_path, properties); | |
130 | |
131 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
132 } | |
133 | |
134 // Called by dbus:: when the remote device receives the configuration for | |
135 // media transport. | |
136 void SelectConfiguration( | |
137 dbus::MethodCall* method_call, | |
138 dbus::ExportedObject::ResponseSender response_sender) { | |
139 DCHECK(OnOriginThread()); | |
140 DCHECK(delegate_); | |
141 | |
142 dbus::MessageReader reader(method_call); | |
143 const uint8_t* capabilities = nullptr; | |
144 size_t length = 0; | |
145 if (!reader.PopArrayOfBytes(&capabilities, &length)) { | |
146 LOG(WARNING) << "SelectConfiguration called with incorrect parameters: " | |
147 << method_call->ToString(); | |
148 return; | |
149 } | |
150 | |
151 // |delegate_| generates the response to |SelectConfiguration| and sends it | |
152 // back via |callback|. | |
153 Delegate::SelectConfigurationCallback callback = base::Bind( | |
154 &BluetoothMediaEndpointServiceProviderImpl::OnConfiguration, | |
155 weak_ptr_factory_.GetWeakPtr(), | |
156 method_call, | |
157 response_sender); | |
158 | |
159 delegate_->SelectConfiguration(capabilities, length, callback); | |
160 } | |
161 | |
162 // Called by dbus:: when the remote device is about to close the connection. | |
163 void ClearConfiguration( | |
164 dbus::MethodCall* method_call, | |
165 dbus::ExportedObject::ResponseSender response_sender) { | |
166 DCHECK(OnOriginThread()); | |
167 DCHECK(delegate_); | |
168 | |
169 dbus::MessageReader reader(method_call); | |
170 dbus::ObjectPath transport_path; | |
171 if (!reader.PopObjectPath(&transport_path)) { | |
172 LOG(WARNING) << "ClearConfiguration called with incorrect parameters: " | |
173 << method_call->ToString(); | |
174 return; | |
175 } | |
176 | |
177 delegate_->ClearConfiguration(transport_path); | |
178 | |
179 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
180 } | |
181 | |
182 // Called by Bluetooth daemon to do the clean up after unregistering the Media | |
183 // Endpoint. | |
184 void Release(dbus::MethodCall* method_call, | |
185 dbus::ExportedObject::ResponseSender response_sender) { | |
186 DCHECK(OnOriginThread()); | |
187 DCHECK(delegate_); | |
188 | |
189 delegate_->Release(); | |
190 | |
191 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
192 } | |
193 | |
194 // Called by Delegate to response to a method requiring transport | |
195 // configuration. | |
196 void OnConfiguration(dbus::MethodCall* method_call, | |
197 dbus::ExportedObject::ResponseSender response_sender, | |
198 const uint8_t* configuration, | |
199 size_t length) { | |
200 DCHECK(OnOriginThread()); | |
201 | |
202 // Generates the response to the method call. | |
203 scoped_ptr<dbus::Response> response( | |
204 dbus::Response::FromMethodCall(method_call)); | |
205 dbus::MessageWriter writer(response.get()); | |
206 writer.AppendArrayOfBytes(configuration, length); | |
armansito
2014/11/17 21:17:14
Make sure that this doesn't cause a crash if the d
Miao
2014/11/18 19:04:33
Done.
| |
207 response_sender.Run(response.Pass()); | |
208 } | |
209 | |
210 // Origin thread (i.e. the UI thread in production). | |
211 base::PlatformThreadId origin_thread_id_; | |
212 | |
213 // D-Bus Bus object is exported on. | |
214 dbus::Bus* bus_; | |
215 | |
216 // All incoming method calls are passed on to |delegate_|. |callback| passed | |
217 // to |delegate+| will generate the response for those methods whose returns | |
218 // are non-void. | |
219 Delegate* delegate_; | |
220 | |
221 // D-Bus object path of the object we are exporting, kept so we can unregister | |
222 // again in you destructor. | |
223 dbus::ObjectPath object_path_; | |
224 | |
225 // D-Bus object we are exporting, owned by this object. | |
226 scoped_refptr<dbus::ExportedObject> exported_object_; | |
227 | |
228 // Weak pointer factory for generating 'this' printers that might live longer | |
229 // than we do. | |
230 // Note This should remain the last member so it'll be destroyed and | |
231 // invalidate it's weak pointers before any other members are destroyed. | |
232 base::WeakPtrFactory<BluetoothMediaEndpointServiceProviderImpl> | |
233 weak_ptr_factory_; | |
234 | |
235 DISALLOW_COPY_AND_ASSIGN(BluetoothMediaEndpointServiceProviderImpl); | |
236 }; | |
237 | |
238 BluetoothMediaEndpointServiceProvider::BluetoothMediaEndpointServiceProvider() { | |
239 } | |
240 | |
241 BluetoothMediaEndpointServiceProvider::~BluetoothMediaEndpointServiceProvider() | |
242 {} | |
243 | |
244 BluetoothMediaEndpointServiceProvider* | |
245 BluetoothMediaEndpointServiceProvider::Create( | |
246 dbus::Bus* bus, | |
247 const dbus::ObjectPath& object_path, | |
248 Delegate* delegate) { | |
249 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) { | |
250 return new BluetoothMediaEndpointServiceProviderImpl( | |
251 bus, object_path, delegate); | |
252 } else { | |
armansito
2014/11/17 21:17:13
You don't need the else as you're returning in eac
Miao
2014/11/18 19:04:33
Done.
| |
253 return new FakeBluetoothMediaEndpointServiceProvider(object_path, delegate); | |
254 } | |
255 } | |
256 | |
257 } // namespace chromeos | |
OLD | NEW |