1 // Copyright (c) 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/experimental_bluetooth_agent_manager_client.h"
8 #include "base/logging.h"
9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.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"
18 const char ExperimentalBluetoothAgentManagerClient::kNoResponseError
[] =
19 "org.chromium.Error.NoResponse";
21 // The ExperimentalBluetoothAgentManagerClient implementation used in
23 class ExperimentalBluetoothAgentManagerClientImpl
24 : public ExperimentalBluetoothAgentManagerClient
{
26 explicit ExperimentalBluetoothAgentManagerClientImpl(dbus::Bus
* bus
)
28 weak_ptr_factory_(this) {
30 object_proxy_
= bus_
->GetObjectProxy(
31 bluetooth_agent_manager::kBluetoothAgentManagerServiceName
,
33 bluetooth_agent_manager::kBluetoothAgentManagerServicePath
));
36 virtual ~ExperimentalBluetoothAgentManagerClientImpl() {
39 // ExperimentalBluetoothAgentManagerClient override.
40 virtual void RegisterAgent(const dbus::ObjectPath
& agent_path
,
41 const std::string
& capability
,
42 const base::Closure
& callback
,
43 const ErrorCallback
& error_callback
) OVERRIDE
{
44 dbus::MethodCall
method_call(
45 bluetooth_agent_manager::kBluetoothAgentManagerInterface
,
46 bluetooth_agent_manager::kRegisterAgent
);
48 dbus::MessageWriter
writer(&method_call
);
49 writer
.AppendObjectPath(agent_path
);
50 writer
.AppendString(capability
);
52 object_proxy_
->CallMethodWithErrorCallback(
54 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
55 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnSuccess
,
56 weak_ptr_factory_
.GetWeakPtr(), callback
),
57 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnError
,
58 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
61 // ExperimentalBluetoothAgentManagerClient override.
62 virtual void UnregisterAgent(const dbus::ObjectPath
& agent_path
,
63 const base::Closure
& callback
,
64 const ErrorCallback
& error_callback
) OVERRIDE
{
65 dbus::MethodCall
method_call(
66 bluetooth_agent_manager::kBluetoothAgentManagerInterface
,
67 bluetooth_agent_manager::kUnregisterAgent
);
69 dbus::MessageWriter
writer(&method_call
);
70 writer
.AppendObjectPath(agent_path
);
72 object_proxy_
->CallMethodWithErrorCallback(
74 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
75 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnSuccess
,
76 weak_ptr_factory_
.GetWeakPtr(), callback
),
77 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnError
,
78 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
82 // ExperimentalBluetoothAgentManagerClient override.
83 virtual void RequestDefaultAgent(const dbus::ObjectPath
& agent_path
,
84 const base::Closure
& callback
,
85 const ErrorCallback
& error_callback
)
87 dbus::MethodCall
method_call(
88 bluetooth_agent_manager::kBluetoothAgentManagerInterface
,
89 bluetooth_agent_manager::kRequestDefaultAgent
);
91 dbus::MessageWriter
writer(&method_call
);
92 writer
.AppendObjectPath(agent_path
);
94 object_proxy_
->CallMethodWithErrorCallback(
96 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
97 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnSuccess
,
98 weak_ptr_factory_
.GetWeakPtr(), callback
),
99 base::Bind(&ExperimentalBluetoothAgentManagerClientImpl::OnError
,
100 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
104 // Called when a response for successful method call is received.
105 void OnSuccess(const base::Closure
& callback
,
106 dbus::Response
* response
) {
111 // Called when a response for a failed method call is received.
112 void OnError(const ErrorCallback
& error_callback
,
113 dbus::ErrorResponse
* response
) {
114 // Error response has optional error message argument.
115 std::string error_name
;
116 std::string error_message
;
118 dbus::MessageReader
reader(response
);
119 error_name
= response
->GetErrorName();
120 reader
.PopString(&error_message
);
122 error_name
= kNoResponseError
;
125 error_callback
.Run(error_name
, error_message
);
129 dbus::ObjectProxy
* object_proxy_
;
131 // Weak pointer factory for generating 'this' pointers that might live longer
133 // Note: This should remain the last member so it'll be destroyed and
134 // invalidate its weak pointers before any other members are destroyed.
135 base::WeakPtrFactory
<ExperimentalBluetoothAgentManagerClientImpl
>
138 DISALLOW_COPY_AND_ASSIGN(ExperimentalBluetoothAgentManagerClientImpl
);
141 ExperimentalBluetoothAgentManagerClient::
142 ExperimentalBluetoothAgentManagerClient() {
145 ExperimentalBluetoothAgentManagerClient::
146 ~ExperimentalBluetoothAgentManagerClient() {
149 ExperimentalBluetoothAgentManagerClient
*
150 ExperimentalBluetoothAgentManagerClient::Create(
151 DBusClientImplementationType type
,
153 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
154 return new ExperimentalBluetoothAgentManagerClientImpl(bus
);
155 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
156 return new FakeBluetoothAgentManagerClient();
159 } // namespace chromeos