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_manager_client.h"
8 #include "base/logging.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"
17 const char BluetoothAgentManagerClient::kNoResponseError
[] =
18 "org.chromium.Error.NoResponse";
20 // The BluetoothAgentManagerClient implementation used in production.
21 class BluetoothAgentManagerClientImpl
22 : public BluetoothAgentManagerClient
{
24 BluetoothAgentManagerClientImpl() : weak_ptr_factory_(this) {}
26 virtual ~BluetoothAgentManagerClientImpl() {
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
);
38 dbus::MessageWriter
writer(&method_call
);
39 writer
.AppendObjectPath(agent_path
);
40 writer
.AppendString(capability
);
42 object_proxy_
->CallMethodWithErrorCallback(
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
));
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
);
59 dbus::MessageWriter
writer(&method_call
);
60 writer
.AppendObjectPath(agent_path
);
62 object_proxy_
->CallMethodWithErrorCallback(
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
));
72 // BluetoothAgentManagerClient override.
73 virtual void RequestDefaultAgent(const dbus::ObjectPath
& agent_path
,
74 const base::Closure
& callback
,
75 const ErrorCallback
& error_callback
)
77 dbus::MethodCall
method_call(
78 bluetooth_agent_manager::kBluetoothAgentManagerInterface
,
79 bluetooth_agent_manager::kRequestDefaultAgent
);
81 dbus::MessageWriter
writer(&method_call
);
82 writer
.AppendObjectPath(agent_path
);
84 object_proxy_
->CallMethodWithErrorCallback(
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
));
94 virtual void Init(dbus::Bus
* bus
) override
{
96 object_proxy_
= bus
->GetObjectProxy(
97 bluetooth_agent_manager::kBluetoothAgentManagerServiceName
,
99 bluetooth_agent_manager::kBluetoothAgentManagerServicePath
));
103 // Called when a response for successful method call is received.
104 void OnSuccess(const base::Closure
& callback
,
105 dbus::Response
* response
) {
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
;
117 dbus::MessageReader
reader(response
);
118 error_name
= response
->GetErrorName();
119 reader
.PopString(&error_message
);
121 error_name
= kNoResponseError
;
124 error_callback
.Run(error_name
, error_message
);
127 dbus::ObjectProxy
* object_proxy_
;
129 // Weak pointer factory for generating 'this' pointers that might live longer
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
>
136 DISALLOW_COPY_AND_ASSIGN(BluetoothAgentManagerClientImpl
);
139 BluetoothAgentManagerClient::BluetoothAgentManagerClient() {
142 BluetoothAgentManagerClient::~BluetoothAgentManagerClient() {
145 BluetoothAgentManagerClient
* BluetoothAgentManagerClient::Create() {
146 return new BluetoothAgentManagerClientImpl();
149 } // namespace chromeos