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
dict_entry_writer(NULL
);
73 writer
.OpenDictEntry(&dict_entry_writer
);
74 writer
.CloseContainer(&dict_entry_writer
);
76 writer
.AppendObjectPath(advertisement_object_path
);
78 DCHECK(object_manager_
);
79 dbus::ObjectProxy
* object_proxy
=
80 object_manager_
->GetObjectProxy(manager_object_path
);
81 object_proxy
->CallMethodWithErrorCallback(
82 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
83 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess
,
84 weak_ptr_factory_
.GetWeakPtr(), callback
),
85 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError
,
86 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
89 // BluetoothAdvertisementManagerClient override.
90 void UnregisterAdvertisement(
91 const dbus::ObjectPath
& manager_object_path
,
92 const dbus::ObjectPath
& advertisement_object_path
,
93 const base::Closure
& callback
,
94 const ErrorCallback
& error_callback
) override
{
95 dbus::MethodCall
method_call(
96 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
97 bluetooth_advertising_manager::kUnregisterAdvertisement
);
99 dbus::MessageWriter
writer(&method_call
);
100 writer
.AppendObjectPath(advertisement_object_path
);
102 DCHECK(object_manager_
);
103 dbus::ObjectProxy
* object_proxy
=
104 object_manager_
->GetObjectProxy(manager_object_path
);
105 object_proxy
->CallMethodWithErrorCallback(
106 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
107 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess
,
108 weak_ptr_factory_
.GetWeakPtr(), callback
),
109 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError
,
110 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
114 void Init(dbus::Bus
* bus
) override
{
116 object_manager_
= bus
->GetObjectManager(
117 bluetooth_object_manager::kBluetoothObjectManagerServiceName
,
119 bluetooth_object_manager::kBluetoothObjectManagerServicePath
));
120 object_manager_
->RegisterInterface(
121 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
126 // Called by dbus::ObjectManager when an object with the advertising manager
127 // interface is created. Informs observers.
128 void ObjectAdded(const dbus::ObjectPath
& object_path
,
129 const std::string
& interface_name
) override
{
130 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer
, observers_
,
131 AdvertisingManagerAdded(object_path
));
134 // Called by dbus::ObjectManager when an object with the advertising manager
135 // interface is removed. Informs observers.
136 void ObjectRemoved(const dbus::ObjectPath
& object_path
,
137 const std::string
& interface_name
) override
{
138 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer
, observers_
,
139 AdvertisingManagerRemoved(object_path
));
142 // Called when a response for successful method call is received.
143 void OnSuccess(const base::Closure
& callback
, dbus::Response
* response
) {
148 // Called when a response for a failed method call is received.
149 void OnError(const ErrorCallback
& error_callback
,
150 dbus::ErrorResponse
* response
) {
151 // Error response has optional error message argument.
152 std::string error_name
;
153 std::string error_message
;
155 dbus::MessageReader
reader(response
);
156 error_name
= response
->GetErrorName();
157 reader
.PopString(&error_message
);
159 error_name
= kNoResponseError
;
162 error_callback
.Run(error_name
, error_message
);
165 dbus::ObjectManager
* object_manager_
;
167 // List of observers interested in event notifications from us.
168 base::ObserverList
<BluetoothLEAdvertisingManagerClient::Observer
> observers_
;
170 // Weak pointer factory for generating 'this' pointers that might live longer
172 // Note: This should remain the last member so it'll be destroyed and
173 // invalidate its weak pointers before any other members are destroyed.
174 base::WeakPtrFactory
<BluetoothAdvertisementManagerClientImpl
>
177 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl
);
180 BluetoothLEAdvertisingManagerClient::BluetoothLEAdvertisingManagerClient() {
183 BluetoothLEAdvertisingManagerClient::~BluetoothLEAdvertisingManagerClient() {
186 BluetoothLEAdvertisingManagerClient
*
187 BluetoothLEAdvertisingManagerClient::Create() {
188 return new BluetoothAdvertisementManagerClientImpl();
191 } // namespace chromeos