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 ~BluetoothGattManagerClientImpl() override
{}
29 // BluetoothGattManagerClient override.
30 void RegisterService(const dbus::ObjectPath
& service_path
,
31 const Options
& options
,
32 const base::Closure
& callback
,
33 const ErrorCallback
& error_callback
) override
{
34 dbus::MethodCall
method_call(
35 bluetooth_gatt_manager::kBluetoothGattManagerInterface
,
36 bluetooth_gatt_manager::kRegisterService
);
38 dbus::MessageWriter
writer(&method_call
);
39 writer
.AppendObjectPath(service_path
);
41 // TODO(armansito): The parameters of the Options dictionary are undefined
42 // but the method signature still requires a value dictionary. Pass an
43 // empty dictionary and fill in the contents later once this is defined.
44 dbus::MessageWriter
array_writer(NULL
);
45 writer
.OpenArray("{sv}", &array_writer
);
46 writer
.CloseContainer(&array_writer
);
48 DCHECK(object_proxy_
);
49 object_proxy_
->CallMethodWithErrorCallback(
51 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
52 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess
,
53 weak_ptr_factory_
.GetWeakPtr(), callback
),
54 base::Bind(&BluetoothGattManagerClientImpl::OnError
,
55 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
58 // BluetoothGattManagerClient override.
59 void UnregisterService(const dbus::ObjectPath
& service_path
,
60 const base::Closure
& callback
,
61 const ErrorCallback
& error_callback
) override
{
62 dbus::MethodCall
method_call(
63 bluetooth_gatt_manager::kBluetoothGattManagerInterface
,
64 bluetooth_gatt_manager::kUnregisterService
);
66 dbus::MessageWriter
writer(&method_call
);
67 writer
.AppendObjectPath(service_path
);
69 DCHECK(object_proxy_
);
70 object_proxy_
->CallMethodWithErrorCallback(
72 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
73 base::Bind(&BluetoothGattManagerClientImpl::OnSuccess
,
74 weak_ptr_factory_
.GetWeakPtr(), callback
),
75 base::Bind(&BluetoothGattManagerClientImpl::OnError
,
76 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
80 // chromeos::DBusClient override.
81 void Init(dbus::Bus
* bus
) override
{
83 object_proxy_
= bus
->GetObjectProxy(
84 bluetooth_gatt_manager::kBluetoothGattManagerServiceName
,
86 bluetooth_gatt_manager::kBluetoothGattManagerInterface
));
90 // Called when a response for a successful method call is received.
91 void OnSuccess(const base::Closure
& callback
, dbus::Response
* response
) {
96 // Called when a response for a failed method call is received.
97 void OnError(const ErrorCallback
& error_callback
,
98 dbus::ErrorResponse
* response
) {
99 // Error response has optional error message argument.
100 std::string error_name
;
101 std::string error_message
;
103 dbus::MessageReader
reader(response
);
104 error_name
= response
->GetErrorName();
105 reader
.PopString(&error_message
);
107 error_name
= kNoResponseError
;
109 error_callback
.Run(error_name
, error_message
);
112 // The proxy to the remote GATT manager object.
113 dbus::ObjectProxy
* object_proxy_
;
115 // Weak pointer factory for generating 'this' pointers that might live longer
117 // Note: This should remain the last member so it'll be destroyed and
118 // invalidate its weak pointers before any other members are destroyed.
119 base::WeakPtrFactory
<BluetoothGattManagerClientImpl
> weak_ptr_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(BluetoothGattManagerClientImpl
);
124 BluetoothGattManagerClient::BluetoothGattManagerClient() {
127 BluetoothGattManagerClient::~BluetoothGattManagerClient() {
131 BluetoothGattManagerClient
* BluetoothGattManagerClient::Create() {
132 return new BluetoothGattManagerClientImpl();
135 } // namespace chromeos