Don't preload rarely seen large images
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_agent_service_provider.cc
blob97eb273c42e57b1b8083f3b4826d9d76835a3ae6
1 // Copyright 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.
5 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
8 #include "base/bind.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/threading/platform_thread.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/fake_bluetooth_agent_service_provider.h"
14 #include "dbus/exported_object.h"
15 #include "dbus/message.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
18 namespace chromeos {
20 // The BluetoothAgentServiceProvider implementation used in production.
21 class BluetoothAgentServiceProviderImpl
22 : public BluetoothAgentServiceProvider {
23 public:
24 BluetoothAgentServiceProviderImpl(dbus::Bus* bus,
25 const dbus::ObjectPath& object_path,
26 Delegate* delegate)
27 : origin_thread_id_(base::PlatformThread::CurrentId()),
28 bus_(bus),
29 delegate_(delegate),
30 object_path_(object_path),
31 weak_ptr_factory_(this) {
32 VLOG(1) << "Creating Bluetooth Agent: " << object_path_.value();
34 exported_object_ = bus_->GetExportedObject(object_path_);
36 exported_object_->ExportMethod(
37 bluetooth_agent::kBluetoothAgentInterface,
38 bluetooth_agent::kRelease,
39 base::Bind(&BluetoothAgentServiceProviderImpl::Release,
40 weak_ptr_factory_.GetWeakPtr()),
41 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
42 weak_ptr_factory_.GetWeakPtr()));
44 exported_object_->ExportMethod(
45 bluetooth_agent::kBluetoothAgentInterface,
46 bluetooth_agent::kRequestPinCode,
47 base::Bind(&BluetoothAgentServiceProviderImpl::RequestPinCode,
48 weak_ptr_factory_.GetWeakPtr()),
49 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
50 weak_ptr_factory_.GetWeakPtr()));
52 exported_object_->ExportMethod(
53 bluetooth_agent::kBluetoothAgentInterface,
54 bluetooth_agent::kDisplayPinCode,
55 base::Bind(&BluetoothAgentServiceProviderImpl::DisplayPinCode,
56 weak_ptr_factory_.GetWeakPtr()),
57 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
58 weak_ptr_factory_.GetWeakPtr()));
60 exported_object_->ExportMethod(
61 bluetooth_agent::kBluetoothAgentInterface,
62 bluetooth_agent::kRequestPasskey,
63 base::Bind(&BluetoothAgentServiceProviderImpl::RequestPasskey,
64 weak_ptr_factory_.GetWeakPtr()),
65 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
66 weak_ptr_factory_.GetWeakPtr()));
68 exported_object_->ExportMethod(
69 bluetooth_agent::kBluetoothAgentInterface,
70 bluetooth_agent::kDisplayPasskey,
71 base::Bind(&BluetoothAgentServiceProviderImpl::DisplayPasskey,
72 weak_ptr_factory_.GetWeakPtr()),
73 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
74 weak_ptr_factory_.GetWeakPtr()));
76 exported_object_->ExportMethod(
77 bluetooth_agent::kBluetoothAgentInterface,
78 bluetooth_agent::kRequestConfirmation,
79 base::Bind(&BluetoothAgentServiceProviderImpl::RequestConfirmation,
80 weak_ptr_factory_.GetWeakPtr()),
81 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
82 weak_ptr_factory_.GetWeakPtr()));
84 exported_object_->ExportMethod(
85 bluetooth_agent::kBluetoothAgentInterface,
86 bluetooth_agent::kRequestAuthorization,
87 base::Bind(&BluetoothAgentServiceProviderImpl::RequestAuthorization,
88 weak_ptr_factory_.GetWeakPtr()),
89 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
90 weak_ptr_factory_.GetWeakPtr()));
92 exported_object_->ExportMethod(
93 bluetooth_agent::kBluetoothAgentInterface,
94 bluetooth_agent::kAuthorizeService,
95 base::Bind(&BluetoothAgentServiceProviderImpl::AuthorizeService,
96 weak_ptr_factory_.GetWeakPtr()),
97 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
98 weak_ptr_factory_.GetWeakPtr()));
100 exported_object_->ExportMethod(
101 bluetooth_agent::kBluetoothAgentInterface,
102 bluetooth_agent::kCancel,
103 base::Bind(&BluetoothAgentServiceProviderImpl::Cancel,
104 weak_ptr_factory_.GetWeakPtr()),
105 base::Bind(&BluetoothAgentServiceProviderImpl::OnExported,
106 weak_ptr_factory_.GetWeakPtr()));
109 ~BluetoothAgentServiceProviderImpl() override {
110 VLOG(1) << "Cleaning up Bluetooth Agent: " << object_path_.value();
112 // Unregister the object path so we can reuse with a new agent.
113 bus_->UnregisterExportedObject(object_path_);
116 private:
117 // Returns true if the current thread is on the origin thread.
118 bool OnOriginThread() {
119 return base::PlatformThread::CurrentId() == origin_thread_id_;
122 // Called by dbus:: when the agent is unregistered from the Bluetooth
123 // daemon, generally at the end of a pairing request.
124 void Release(dbus::MethodCall* method_call,
125 dbus::ExportedObject::ResponseSender response_sender) {
126 DCHECK(OnOriginThread());
127 DCHECK(delegate_);
129 delegate_->Released();
131 response_sender.Run(dbus::Response::FromMethodCall(method_call));
134 // Called by dbus:: when the Bluetooth daemon requires a PIN Code for
135 // device authentication.
136 void RequestPinCode(dbus::MethodCall* method_call,
137 dbus::ExportedObject::ResponseSender response_sender) {
138 DCHECK(OnOriginThread());
139 DCHECK(delegate_);
141 dbus::MessageReader reader(method_call);
142 dbus::ObjectPath device_path;
143 if (!reader.PopObjectPath(&device_path)) {
144 LOG(WARNING) << "RequestPinCode called with incorrect paramters: "
145 << method_call->ToString();
146 return;
149 Delegate::PinCodeCallback callback = base::Bind(
150 &BluetoothAgentServiceProviderImpl::OnPinCode,
151 weak_ptr_factory_.GetWeakPtr(),
152 method_call,
153 response_sender);
155 delegate_->RequestPinCode(device_path, callback);
158 // Called by dbus:: when the Bluetooth daemon requires that the user
159 // enter a PIN Code into the remote device so that it may be
160 // authenticated.
161 void DisplayPinCode(dbus::MethodCall* method_call,
162 dbus::ExportedObject::ResponseSender response_sender) {
163 DCHECK(OnOriginThread());
164 DCHECK(delegate_);
166 dbus::MessageReader reader(method_call);
167 dbus::ObjectPath device_path;
168 std::string pincode;
169 if (!reader.PopObjectPath(&device_path) ||
170 !reader.PopString(&pincode)) {
171 LOG(WARNING) << "DisplayPinCode called with incorrect paramters: "
172 << method_call->ToString();
173 return;
176 delegate_->DisplayPinCode(device_path, pincode);
178 response_sender.Run(dbus::Response::FromMethodCall(method_call));
181 // Called by dbus:: when the Bluetooth daemon requires a Passkey for
182 // device authentication.
183 void RequestPasskey(dbus::MethodCall* method_call,
184 dbus::ExportedObject::ResponseSender response_sender) {
185 DCHECK(OnOriginThread());
186 DCHECK(delegate_);
188 dbus::MessageReader reader(method_call);
189 dbus::ObjectPath device_path;
190 if (!reader.PopObjectPath(&device_path)) {
191 LOG(WARNING) << "RequestPasskey called with incorrect paramters: "
192 << method_call->ToString();
193 return;
196 Delegate::PasskeyCallback callback = base::Bind(
197 &BluetoothAgentServiceProviderImpl::OnPasskey,
198 weak_ptr_factory_.GetWeakPtr(),
199 method_call,
200 response_sender);
202 delegate_->RequestPasskey(device_path, callback);
205 // Called by dbus:: when the Bluetooth daemon requires that the user
206 // enter a Passkey into the remote device so that it may be
207 // authenticated.
208 void DisplayPasskey(dbus::MethodCall* method_call,
209 dbus::ExportedObject::ResponseSender response_sender) {
210 DCHECK(OnOriginThread());
211 DCHECK(delegate_);
213 dbus::MessageReader reader(method_call);
214 dbus::ObjectPath device_path;
215 uint32 passkey;
216 uint16 entered;
217 if (!reader.PopObjectPath(&device_path) ||
218 !reader.PopUint32(&passkey) ||
219 !reader.PopUint16(&entered)) {
220 LOG(WARNING) << "DisplayPasskey called with incorrect paramters: "
221 << method_call->ToString();
222 return;
225 delegate_->DisplayPasskey(device_path, passkey, entered);
227 response_sender.Run(dbus::Response::FromMethodCall(method_call));
230 // Called by dbus:: when the Bluetooth daemon requires that the user
231 // confirm that a Passkey is displayed on the screen of the remote
232 // device so that it may be authenticated.
233 void RequestConfirmation(
234 dbus::MethodCall* method_call,
235 dbus::ExportedObject::ResponseSender response_sender) {
236 DCHECK(OnOriginThread());
237 DCHECK(delegate_);
239 dbus::MessageReader reader(method_call);
240 dbus::ObjectPath device_path;
241 uint32 passkey;
242 if (!reader.PopObjectPath(&device_path) ||
243 !reader.PopUint32(&passkey)) {
244 LOG(WARNING) << "RequestConfirmation called with incorrect paramters: "
245 << method_call->ToString();
246 return;
249 Delegate::ConfirmationCallback callback = base::Bind(
250 &BluetoothAgentServiceProviderImpl::OnConfirmation,
251 weak_ptr_factory_.GetWeakPtr(),
252 method_call,
253 response_sender);
255 delegate_->RequestConfirmation(device_path, passkey, callback);
258 // Called by dbus:: when the Bluetooth daemon requires that the user
259 // confirm an incoming just-works pairing.
260 void RequestAuthorization(
261 dbus::MethodCall* method_call,
262 dbus::ExportedObject::ResponseSender response_sender) {
263 DCHECK(OnOriginThread());
264 DCHECK(delegate_);
266 dbus::MessageReader reader(method_call);
267 dbus::ObjectPath device_path;
268 if (!reader.PopObjectPath(&device_path)) {
269 LOG(WARNING) << "RequestAuthorization called with incorrect paramters: "
270 << method_call->ToString();
271 return;
274 Delegate::ConfirmationCallback callback = base::Bind(
275 &BluetoothAgentServiceProviderImpl::OnConfirmation,
276 weak_ptr_factory_.GetWeakPtr(),
277 method_call,
278 response_sender);
280 delegate_->RequestAuthorization(device_path, callback);
283 // Called by dbus:: when the Bluetooth daemon requires that the user
284 // confirm that that a remote device is authorized to connect to a service
285 // UUID.
286 void AuthorizeService(dbus::MethodCall* method_call,
287 dbus::ExportedObject::ResponseSender response_sender) {
288 DCHECK(OnOriginThread());
289 DCHECK(delegate_);
291 dbus::MessageReader reader(method_call);
292 dbus::ObjectPath device_path;
293 std::string uuid;
294 if (!reader.PopObjectPath(&device_path) ||
295 !reader.PopString(&uuid)) {
296 LOG(WARNING) << "AuthorizeService called with incorrect paramters: "
297 << method_call->ToString();
298 return;
301 Delegate::ConfirmationCallback callback = base::Bind(
302 &BluetoothAgentServiceProviderImpl::OnConfirmation,
303 weak_ptr_factory_.GetWeakPtr(),
304 method_call,
305 response_sender);
307 delegate_->AuthorizeService(device_path, uuid, callback);
310 // Called by dbus:: when the request failed before a reply was returned
311 // from the device.
312 void Cancel(dbus::MethodCall* method_call,
313 dbus::ExportedObject::ResponseSender response_sender) {
314 DCHECK(OnOriginThread());
315 DCHECK(delegate_);
317 delegate_->Cancel();
319 response_sender.Run(dbus::Response::FromMethodCall(method_call));
322 // Called by dbus:: when a method is exported.
323 void OnExported(const std::string& interface_name,
324 const std::string& method_name,
325 bool success) {
326 LOG_IF(WARNING, !success) << "Failed to export "
327 << interface_name << "." << method_name;
330 // Called by the Delegate to response to a method requesting a PIN code.
331 void OnPinCode(dbus::MethodCall* method_call,
332 dbus::ExportedObject::ResponseSender response_sender,
333 Delegate::Status status,
334 const std::string& pincode) {
335 DCHECK(OnOriginThread());
337 switch (status) {
338 case Delegate::SUCCESS: {
339 scoped_ptr<dbus::Response> response(
340 dbus::Response::FromMethodCall(method_call));
341 dbus::MessageWriter writer(response.get());
342 writer.AppendString(pincode);
343 response_sender.Run(response.Pass());
344 break;
346 case Delegate::REJECTED: {
347 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
348 method_call, bluetooth_agent::kErrorRejected, "rejected"));
349 break;
351 case Delegate::CANCELLED: {
352 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
353 method_call, bluetooth_agent::kErrorCanceled, "canceled"));
354 break;
356 default:
357 NOTREACHED() << "Unexpected status code from delegate: " << status;
361 // Called by the Delegate to response to a method requesting a Passkey.
362 void OnPasskey(dbus::MethodCall* method_call,
363 dbus::ExportedObject::ResponseSender response_sender,
364 Delegate::Status status,
365 uint32 passkey) {
366 DCHECK(OnOriginThread());
368 switch (status) {
369 case Delegate::SUCCESS: {
370 scoped_ptr<dbus::Response> response(
371 dbus::Response::FromMethodCall(method_call));
372 dbus::MessageWriter writer(response.get());
373 writer.AppendUint32(passkey);
374 response_sender.Run(response.Pass());
375 break;
377 case Delegate::REJECTED: {
378 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
379 method_call, bluetooth_agent::kErrorRejected, "rejected"));
380 break;
382 case Delegate::CANCELLED: {
383 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
384 method_call, bluetooth_agent::kErrorCanceled, "canceled"));
385 break;
387 default:
388 NOTREACHED() << "Unexpected status code from delegate: " << status;
392 // Called by the Delegate in response to a method requiring confirmation.
393 void OnConfirmation(dbus::MethodCall* method_call,
394 dbus::ExportedObject::ResponseSender response_sender,
395 Delegate::Status status) {
396 DCHECK(OnOriginThread());
398 switch (status) {
399 case Delegate::SUCCESS: {
400 response_sender.Run(dbus::Response::FromMethodCall(method_call));
401 break;
403 case Delegate::REJECTED: {
404 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
405 method_call, bluetooth_agent::kErrorRejected, "rejected"));
406 break;
408 case Delegate::CANCELLED: {
409 response_sender.Run(dbus::ErrorResponse::FromMethodCall(
410 method_call, bluetooth_agent::kErrorCanceled, "canceled"));
411 break;
413 default:
414 NOTREACHED() << "Unexpected status code from delegate: " << status;
418 // Origin thread (i.e. the UI thread in production).
419 base::PlatformThreadId origin_thread_id_;
421 // D-Bus bus object is exported on, not owned by this object and must
422 // outlive it.
423 dbus::Bus* bus_;
425 // All incoming method calls are passed on to the Delegate and a callback
426 // passed to generate the reply. |delegate_| is generally the object that
427 // owns this one, and must outlive it.
428 Delegate* delegate_;
430 // D-Bus object path of object we are exporting, kept so we can unregister
431 // again in our destructor.
432 dbus::ObjectPath object_path_;
434 // D-Bus object we are exporting, owned by this object.
435 scoped_refptr<dbus::ExportedObject> exported_object_;
437 // Weak pointer factory for generating 'this' pointers that might live longer
438 // than we do.
439 // Note: This should remain the last member so it'll be destroyed and
440 // invalidate its weak pointers before any other members are destroyed.
441 base::WeakPtrFactory<BluetoothAgentServiceProviderImpl> weak_ptr_factory_;
443 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentServiceProviderImpl);
446 BluetoothAgentServiceProvider::BluetoothAgentServiceProvider() {
449 BluetoothAgentServiceProvider::~BluetoothAgentServiceProvider() {
452 // static
453 BluetoothAgentServiceProvider* BluetoothAgentServiceProvider::Create(
454 dbus::Bus* bus,
455 const dbus::ObjectPath& object_path,
456 Delegate* delegate) {
457 if (!DBusThreadManager::Get()->IsUsingStub(DBusClientBundle::BLUETOOTH)) {
458 return new BluetoothAgentServiceProviderImpl(bus, object_path, delegate);
459 } else {
460 return new FakeBluetoothAgentServiceProvider(object_path, delegate);
464 } // namespace chromeos