bluetooth_agent_manager_client.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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. 4 5#include "chromeos/dbus/bluetooth_agent_manager_client.h" 6 7#include "base/bind.h" 8#include "base/logging.h" 9#include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" 10#include "dbus/bus.h" 11#include "dbus/message.h" 12#include "dbus/object_path.h" 13#include "dbus/object_proxy.h" 14#include "third_party/cros_system_api/dbus/service_constants.h" 15 16namespace chromeos { 17 18const char BluetoothAgentManagerClient::kNoResponseError[] = 19 "org.chromium.Error.NoResponse"; 20 21// The BluetoothAgentManagerClient implementation used in production. 22class BluetoothAgentManagerClientImpl 23 : public BluetoothAgentManagerClient { 24 public: 25 explicit BluetoothAgentManagerClientImpl(dbus::Bus* bus) 26 : bus_(bus), 27 weak_ptr_factory_(this) { 28 DCHECK(bus_); 29 object_proxy_ = bus_->GetObjectProxy( 30 bluetooth_agent_manager::kBluetoothAgentManagerServiceName, 31 dbus::ObjectPath( 32 bluetooth_agent_manager::kBluetoothAgentManagerServicePath)); 33 } 34 35 virtual ~BluetoothAgentManagerClientImpl() { 36 } 37 38 // BluetoothAgentManagerClient override. 39 virtual void RegisterAgent(const dbus::ObjectPath& agent_path, 40 const std::string& capability, 41 const base::Closure& callback, 42 const ErrorCallback& error_callback) OVERRIDE { 43 dbus::MethodCall method_call( 44 bluetooth_agent_manager::kBluetoothAgentManagerInterface, 45 bluetooth_agent_manager::kRegisterAgent); 46 47 dbus::MessageWriter writer(&method_call); 48 writer.AppendObjectPath(agent_path); 49 writer.AppendString(capability); 50 51 object_proxy_->CallMethodWithErrorCallback( 52 &method_call, 53 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 54 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, 55 weak_ptr_factory_.GetWeakPtr(), callback), 56 base::Bind(&BluetoothAgentManagerClientImpl::OnError, 57 weak_ptr_factory_.GetWeakPtr(), error_callback)); 58 } 59 60 // BluetoothAgentManagerClient override. 61 virtual void UnregisterAgent(const dbus::ObjectPath& agent_path, 62 const base::Closure& callback, 63 const ErrorCallback& error_callback) OVERRIDE { 64 dbus::MethodCall method_call( 65 bluetooth_agent_manager::kBluetoothAgentManagerInterface, 66 bluetooth_agent_manager::kUnregisterAgent); 67 68 dbus::MessageWriter writer(&method_call); 69 writer.AppendObjectPath(agent_path); 70 71 object_proxy_->CallMethodWithErrorCallback( 72 &method_call, 73 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 74 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, 75 weak_ptr_factory_.GetWeakPtr(), callback), 76 base::Bind(&BluetoothAgentManagerClientImpl::OnError, 77 weak_ptr_factory_.GetWeakPtr(), error_callback)); 78 } 79 80 81 // BluetoothAgentManagerClient override. 82 virtual void RequestDefaultAgent(const dbus::ObjectPath& agent_path, 83 const base::Closure& callback, 84 const ErrorCallback& error_callback) 85 OVERRIDE { 86 dbus::MethodCall method_call( 87 bluetooth_agent_manager::kBluetoothAgentManagerInterface, 88 bluetooth_agent_manager::kRequestDefaultAgent); 89 90 dbus::MessageWriter writer(&method_call); 91 writer.AppendObjectPath(agent_path); 92 93 object_proxy_->CallMethodWithErrorCallback( 94 &method_call, 95 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 96 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, 97 weak_ptr_factory_.GetWeakPtr(), callback), 98 base::Bind(&BluetoothAgentManagerClientImpl::OnError, 99 weak_ptr_factory_.GetWeakPtr(), error_callback)); 100 } 101 102 private: 103 // Called when a response for successful method call is received. 104 void OnSuccess(const base::Closure& callback, 105 dbus::Response* response) { 106 DCHECK(response); 107 callback.Run(); 108 } 109 110 // Called when a response for a failed method call is received. 111 void OnError(const ErrorCallback& error_callback, 112 dbus::ErrorResponse* response) { 113 // Error response has optional error message argument. 114 std::string error_name; 115 std::string error_message; 116 if (response) { 117 dbus::MessageReader reader(response); 118 error_name = response->GetErrorName(); 119 reader.PopString(&error_message); 120 } else { 121 error_name = kNoResponseError; 122 error_message = ""; 123 } 124 error_callback.Run(error_name, error_message); 125 } 126 127 dbus::Bus* bus_; 128 dbus::ObjectProxy* object_proxy_; 129 130 // Weak pointer factory for generating 'this' pointers that might live longer 131 // than we do. 132 // Note: This should remain the last member so it'll be destroyed and 133 // invalidate its weak pointers before any other members are destroyed. 134 base::WeakPtrFactory<BluetoothAgentManagerClientImpl> 135 weak_ptr_factory_; 136 137 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentManagerClientImpl); 138}; 139 140BluetoothAgentManagerClient::BluetoothAgentManagerClient() { 141} 142 143BluetoothAgentManagerClient::~BluetoothAgentManagerClient() { 144} 145 146BluetoothAgentManagerClient* BluetoothAgentManagerClient::Create( 147 DBusClientImplementationType type, 148 dbus::Bus* bus) { 149 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) 150 return new BluetoothAgentManagerClientImpl(bus); 151 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); 152 return new FakeBluetoothAgentManagerClient(); 153} 154 155} // namespace chromeos 156