1 // Copyright 2014 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_gatt_manager_client.h"
8 #include "base/memory/weak_ptr.h"
10 #include "dbus/message.h"
11 #include "dbus/object_proxy.h"
12 #include "third_party/cros_system_api/dbus/service_constants.h"
16 const char BluetoothGattManagerClient::kNoResponseError
[] =
17 "org.chromium.Error.NoResponse";
19 // The BluetoothGattManagerClient implementation used in production.
20 class BluetoothGattManagerClientImpl
: public BluetoothGattManagerClient
{
22 BluetoothGattManagerClientImpl()
23 : object_proxy_(NULL
),
24 weak_ptr_factory_(this) {
27 virtual ~BluetoothGattManagerClientImpl() {
30 // BluetoothGattManagerClient override.
31 virtual void RegisterService(const dbus::ObjectPath
& service_path
,
32 const Options
& options
,
33 const base::Closure
& callback
,
34 const ErrorCallback
& error_callback
) OVERRIDE
{
35 dbus::MethodCall
method_call(
36 bluetooth_gatt_manager::kBluetoothGattManagerInterface
,
37 bluetooth_gatt_manager::kRegisterService
);
39 dbus::MessageWriter
writer(&method_call
);
40 writer
.AppendObjectPath(service_path
);
42 // TODO(armansito): The parameters of the Options dictionary are undefined
43 // but the method signature still requires a value dictionary. Pass an
44 // empty dictionary and fill in the contents later once this is defined.
45 dbus::MessageWriter
array_writer(NULL
);
46 writer
.OpenArray("{sv}", &array_writer
);
47 writer
.CloseContainer(&array_writer
);
49 DCHECK(object_proxy_
);
50 object_proxy_
->CallMethodWithErrorCallback(
52 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
53 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess
,
54 weak_ptr_factory_
.GetWeakPtr(), callback
),
55 base::Bind(&BluetoothGattManagerClientImpl::OnError
,
56 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
59 // BluetoothGattManagerClient override.
60 virtual void UnregisterService(const dbus::ObjectPath
& service_path
,
61 const base::Closure
& callback
,
62 const ErrorCallback
& error_callback
) OVERRIDE
{
63 dbus::MethodCall
method_call(
64 bluetooth_gatt_manager::kBluetoothGattManagerInterface
,
65 bluetooth_gatt_manager::kUnregisterService
);
67 dbus::MessageWriter
writer(&method_call
);
68 writer
.AppendObjectPath(service_path
);
70 DCHECK(object_proxy_
);
71 object_proxy_
->CallMethodWithErrorCallback(
73 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
74 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess
,
75 weak_ptr_factory_
.GetWeakPtr(), callback
),
76 base::Bind(&BluetoothGattManagerClientImpl::OnError
,
77 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
81 // chromeos::DBusClient override.
82 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
84 object_proxy_
= bus
->GetObjectProxy(
85 bluetooth_gatt_manager::kBluetoothGattManagerServiceName
,
87 bluetooth_gatt_manager::kBluetoothGattManagerInterface
));
91 // Called when a response for a successful method call is received.
92 void OnSuccess(const base::Closure
& callback
, dbus::Response
* response
) {
97 // Called when a response for a failed method call is received.
98 void OnError(const ErrorCallback
& error_callback
,
99 dbus::ErrorResponse
* response
) {
100 // Error response has optional error message argument.
101 std::string error_name
;
102 std::string error_message
;
104 dbus::MessageReader
reader(response
);
105 error_name
= response
->GetErrorName();
106 reader
.PopString(&error_message
);
108 error_name
= kNoResponseError
;
110 error_callback
.Run(error_name
, error_message
);
113 // The proxy to the remote GATT manager object.
114 dbus::ObjectProxy
* object_proxy_
;
116 // Weak pointer factory for generating 'this' pointers that might live longer
118 // Note: This should remain the last member so it'll be destroyed and
119 // invalidate its weak pointers before any other members are destroyed.
120 base::WeakPtrFactory
<BluetoothGattManagerClientImpl
> weak_ptr_factory_
;
122 DISALLOW_COPY_AND_ASSIGN(BluetoothGattManagerClientImpl
);
125 BluetoothGattManagerClient::BluetoothGattManagerClient() {
128 BluetoothGattManagerClient::~BluetoothGattManagerClient() {
132 BluetoothGattManagerClient
* BluetoothGattManagerClient::Create() {
133 return new BluetoothGattManagerClientImpl();
136 } // namespace chromeos