1 // Copyright 2015 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_le_advertising_manager_client.h"
8 #include "base/logging.h"
9 #include "base/observer_list.h"
11 #include "dbus/message.h"
12 #include "dbus/object_manager.h"
13 #include "dbus/object_proxy.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
18 const char BluetoothLEAdvertisingManagerClient::kNoResponseError
[] =
19 "org.chromium.Error.NoResponse";
21 // The BluetoothAdvertisementManagerClient implementation used in production.
22 class BluetoothAdvertisementManagerClientImpl
23 : public BluetoothLEAdvertisingManagerClient
,
24 public dbus::ObjectManager::Interface
{
26 BluetoothAdvertisementManagerClientImpl()
27 : object_manager_(NULL
), weak_ptr_factory_(this) {}
29 ~BluetoothAdvertisementManagerClientImpl() override
{
30 if (object_manager_
) {
31 object_manager_
->UnregisterInterface(
32 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
);
36 // BluetoothAdapterClient override.
38 BluetoothLEAdvertisingManagerClient::Observer
* observer
) override
{
40 observers_
.AddObserver(observer
);
43 // BluetoothAdapterClient override.
45 BluetoothLEAdvertisingManagerClient::Observer
* observer
) override
{
47 observers_
.RemoveObserver(observer
);
50 // dbus::ObjectManager::Interface override.
51 dbus::PropertySet
* CreateProperties(
52 dbus::ObjectProxy
* object_proxy
,
53 const dbus::ObjectPath
& object_path
,
54 const std::string
& interface_name
) override
{
55 return new dbus::PropertySet(object_proxy
, interface_name
,
56 dbus::PropertySet::PropertyChangedCallback());
59 // BluetoothAdvertisementManagerClient override.
60 void RegisterAdvertisement(const dbus::ObjectPath
& manager_object_path
,
61 const dbus::ObjectPath
& advertisement_object_path
,
62 const base::Closure
& callback
,
63 const ErrorCallback
& error_callback
) override
{
64 dbus::MethodCall
method_call(
65 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
66 bluetooth_advertising_manager::kRegisterAdvertisement
);
68 dbus::MessageWriter
writer(&method_call
);
69 writer
.AppendObjectPath(advertisement_object_path
);
71 // Empty dictionary for options.
72 dbus::MessageWriter
array_writer(NULL
);
73 writer
.OpenArray("{sv}", &array_writer
);
74 writer
.CloseContainer(&array_writer
);
76 DCHECK(object_manager_
);
77 dbus::ObjectProxy
* object_proxy
=
78 object_manager_
->GetObjectProxy(manager_object_path
);
79 object_proxy
->CallMethodWithErrorCallback(
80 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
81 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess
,
82 weak_ptr_factory_
.GetWeakPtr(), callback
),
83 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError
,
84 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
87 // BluetoothAdvertisementManagerClient override.
88 void UnregisterAdvertisement(
89 const dbus::ObjectPath
& manager_object_path
,
90 const dbus::ObjectPath
& advertisement_object_path
,
91 const base::Closure
& callback
,
92 const ErrorCallback
& error_callback
) override
{
93 dbus::MethodCall
method_call(
94 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
95 bluetooth_advertising_manager::kUnregisterAdvertisement
);
97 dbus::MessageWriter
writer(&method_call
);
98 writer
.AppendObjectPath(advertisement_object_path
);
100 DCHECK(object_manager_
);
101 dbus::ObjectProxy
* object_proxy
=
102 object_manager_
->GetObjectProxy(manager_object_path
);
103 object_proxy
->CallMethodWithErrorCallback(
104 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
105 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess
,
106 weak_ptr_factory_
.GetWeakPtr(), callback
),
107 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError
,
108 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
112 void Init(dbus::Bus
* bus
) override
{
114 object_manager_
= bus
->GetObjectManager(
115 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
117 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
118 object_manager_
->RegisterInterface(
119 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
124 // Called by dbus::ObjectManager when an object with the advertising manager
125 // interface is created. Informs observers.
126 void ObjectAdded(const dbus::ObjectPath
& object_path
,
127 const std::string
& interface_name
) override
{
128 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer
, observers_
,
129 AdvertisingManagerAdded(object_path
));
132 // Called by dbus::ObjectManager when an object with the advertising manager
133 // interface is removed. Informs observers.
134 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
135 const std::string
& interface_name
) override
{
136 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer
, observers_
,
137 AdvertisingManagerRemoved(object_path
));
140 // Called when a response for successful method call is received.
141 void OnSuccess(const base::Closure
& callback
, dbus::Response
* response
) {
146 // Called when a response for a failed method call is received.
147 void OnError(const ErrorCallback
& error_callback
,
148 dbus::ErrorResponse
* response
) {
149 // Error response has optional error message argument.
150 std::string error_name
;
151 std::string error_message
;
153 dbus::MessageReader
reader(response
);
154 error_name
= response
->GetErrorName();
155 reader
.PopString(&error_message
);
157 error_name
= kNoResponseError
;
160 error_callback
.Run(error_name
, error_message
);
163 dbus::ObjectManager
* object_manager_
;
165 // List of observers interested in event notifications from us.
166 base::ObserverList
<BluetoothLEAdvertisingManagerClient::Observer
> observers_
;
168 // Weak pointer factory for generating 'this' pointers that might live longer
170 // Note: This should remain the last member so it'll be destroyed and
171 // invalidate its weak pointers before any other members are destroyed.
172 base::WeakPtrFactory
<BluetoothAdvertisementManagerClientImpl
>
175 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl
);
178 BluetoothLEAdvertisingManagerClient::BluetoothLEAdvertisingManagerClient() {
181 BluetoothLEAdvertisingManagerClient::~BluetoothLEAdvertisingManagerClient() {
184 BluetoothLEAdvertisingManagerClient
*
185 BluetoothLEAdvertisingManagerClient::Create() {
186 return new BluetoothAdvertisementManagerClientImpl();
189 } // namespace chromeos