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 // TODO(rkc): object_manager_ should not be NULL, just a hot fix till
31 // http://crbug.com/479430 is properly fixed.
32 if (object_manager_
) {
33 object_manager_
->UnregisterInterface(
34 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
);
38 // BluetoothAdapterClient override.
40 BluetoothLEAdvertisingManagerClient::Observer
* observer
) override
{
42 observers_
.AddObserver(observer
);
45 // BluetoothAdapterClient override.
47 BluetoothLEAdvertisingManagerClient::Observer
* observer
) override
{
49 observers_
.RemoveObserver(observer
);
52 // dbus::ObjectManager::Interface override.
53 dbus::PropertySet
* CreateProperties(
54 dbus::ObjectProxy
* object_proxy
,
55 const dbus::ObjectPath
& object_path
,
56 const std::string
& interface_name
) override
{
57 return new dbus::PropertySet(object_proxy
, interface_name
,
58 dbus::PropertySet::PropertyChangedCallback());
61 // BluetoothAdvertisementManagerClient override.
62 void RegisterAdvertisement(const dbus::ObjectPath
& manager_object_path
,
63 const dbus::ObjectPath
& advertisement_object_path
,
64 const base::Closure
& callback
,
65 const ErrorCallback
& error_callback
) override
{
66 dbus::MethodCall
method_call(
67 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
68 bluetooth_advertising_manager::kRegisterAdvertisement
);
70 dbus::MessageWriter
writer(&method_call
);
71 writer
.AppendObjectPath(advertisement_object_path
);
73 // Empty dictionary for options.
74 dbus::MessageWriter
dict_entry_writer(NULL
);
75 writer
.OpenDictEntry(&dict_entry_writer
);
76 writer
.CloseContainer(&dict_entry_writer
);
78 writer
.AppendObjectPath(advertisement_object_path
);
80 dbus::ObjectProxy
* object_proxy
=
81 object_manager_
->GetObjectProxy(manager_object_path
);
82 object_proxy
->CallMethodWithErrorCallback(
83 &method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
84 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess
,
85 weak_ptr_factory_
.GetWeakPtr(), callback
),
86 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError
,
87 weak_ptr_factory_
.GetWeakPtr(), error_callback
));
90 // BluetoothAdvertisementManagerClient override.
91 void UnregisterAdvertisement(
92 const dbus::ObjectPath
& manager_object_path
,
93 const dbus::ObjectPath
& advertisement_object_path
,
94 const base::Closure
& callback
,
95 const ErrorCallback
& error_callback
) override
{
96 dbus::MethodCall
method_call(
97 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface
,
98 bluetooth_advertising_manager::kUnregisterAdvertisement
);
100 dbus::MessageWriter
writer(&method_call
);
101 writer
.AppendObjectPath(advertisement_object_path
);
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 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