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 "dbus/bus.h" 10 #include "dbus/message.h" 11 #include "dbus/object_path.h" 12 #include "dbus/object_proxy.h" 13 #include "third_party/cros_system_api/dbus/service_constants.h" 14 15 namespace chromeos { 16 17 const char BluetoothAgentManagerClient::kNoResponseError[] = 18 "org.chromium.Error.NoResponse"; 19 20 // The BluetoothAgentManagerClient implementation used in production. 21 class BluetoothAgentManagerClientImpl 22 : public BluetoothAgentManagerClient { 23 public: 24 BluetoothAgentManagerClientImpl() : weak_ptr_factory_(this) {} 25 26 virtual ~BluetoothAgentManagerClientImpl() { 27 } 28 29 // BluetoothAgentManagerClient override. 30 virtual void RegisterAgent(const dbus::ObjectPath& agent_path, 31 const std::string& capability, 32 const base::Closure& callback, 33 const ErrorCallback& error_callback) OVERRIDE { 34 dbus::MethodCall method_call( 35 bluetooth_agent_manager::kBluetoothAgentManagerInterface, 36 bluetooth_agent_manager::kRegisterAgent); 37 38 dbus::MessageWriter writer(&method_call); 39 writer.AppendObjectPath(agent_path); 40 writer.AppendString(capability); 41 42 object_proxy_->CallMethodWithErrorCallback( 43 &method_call, 44 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 45 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, 46 weak_ptr_factory_.GetWeakPtr(), callback), 47 base::Bind(&BluetoothAgentManagerClientImpl::OnError, 48 weak_ptr_factory_.GetWeakPtr(), error_callback)); 49 } 50 51 // BluetoothAgentManagerClient override. 52 virtual void UnregisterAgent(const dbus::ObjectPath& agent_path, 53 const base::Closure& callback, 54 const ErrorCallback& error_callback) OVERRIDE { 55 dbus::MethodCall method_call( 56 bluetooth_agent_manager::kBluetoothAgentManagerInterface, 57 bluetooth_agent_manager::kUnregisterAgent); 58 59 dbus::MessageWriter writer(&method_call); 60 writer.AppendObjectPath(agent_path); 61 62 object_proxy_->CallMethodWithErrorCallback( 63 &method_call, 64 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 65 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, 66 weak_ptr_factory_.GetWeakPtr(), callback), 67 base::Bind(&BluetoothAgentManagerClientImpl::OnError, 68 weak_ptr_factory_.GetWeakPtr(), error_callback)); 69 } 70 71 72 // BluetoothAgentManagerClient override. 73 virtual void RequestDefaultAgent(const dbus::ObjectPath& agent_path, 74 const base::Closure& callback, 75 const ErrorCallback& error_callback) 76 OVERRIDE { 77 dbus::MethodCall method_call( 78 bluetooth_agent_manager::kBluetoothAgentManagerInterface, 79 bluetooth_agent_manager::kRequestDefaultAgent); 80 81 dbus::MessageWriter writer(&method_call); 82 writer.AppendObjectPath(agent_path); 83 84 object_proxy_->CallMethodWithErrorCallback( 85 &method_call, 86 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, 87 base::Bind(&BluetoothAgentManagerClientImpl::OnSuccess, 88 weak_ptr_factory_.GetWeakPtr(), callback), 89 base::Bind(&BluetoothAgentManagerClientImpl::OnError, 90 weak_ptr_factory_.GetWeakPtr(), error_callback)); 91 } 92 93 protected: 94 virtual void Init(dbus::Bus* bus) OVERRIDE { 95 DCHECK(bus); 96 object_proxy_ = bus->GetObjectProxy( 97 bluetooth_agent_manager::kBluetoothAgentManagerServiceName, 98 dbus::ObjectPath( 99 bluetooth_agent_manager::kBluetoothAgentManagerServicePath)); 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::ObjectProxy* object_proxy_; 128 129 // Weak pointer factory for generating 'this' pointers that might live longer 130 // than we do. 131 // Note: This should remain the last member so it'll be destroyed and 132 // invalidate its weak pointers before any other members are destroyed. 133 base::WeakPtrFactory<BluetoothAgentManagerClientImpl> 134 weak_ptr_factory_; 135 136 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentManagerClientImpl); 137 }; 138 139 BluetoothAgentManagerClient::BluetoothAgentManagerClient() { 140 } 141 142 BluetoothAgentManagerClient::~BluetoothAgentManagerClient() { 143 } 144 145 BluetoothAgentManagerClient* BluetoothAgentManagerClient::Create() { 146 return new BluetoothAgentManagerClientImpl(); 147 } 148 149 } // namespace chromeos 150