OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "bluetooth_le_advertising_manager_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/observer_list.h" | |
10 #include "dbus/bus.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" | |
15 | |
16 namespace chromeos { | |
17 | |
18 const char BluetoothLEAdvertisingManagerClient::kNoResponseError[] = | |
19 "org.chromium.Error.NoResponse"; | |
20 | |
21 // The BluetoothAdvertisementManagerClient implementation used in production. | |
22 class BluetoothAdvertisementManagerClientImpl | |
23 : public BluetoothLEAdvertisingManagerClient, | |
24 public dbus::ObjectManager::Interface { | |
25 public: | |
26 BluetoothAdvertisementManagerClientImpl() | |
27 : object_manager_(NULL), weak_ptr_factory_(this) {} | |
28 | |
29 ~BluetoothAdvertisementManagerClientImpl() override { | |
30 object_manager_->UnregisterInterface( | |
31 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface); | |
32 } | |
33 | |
34 // BluetoothAdapterClient override. | |
35 void AddObserver( | |
36 BluetoothLEAdvertisingManagerClient::Observer* observer) override { | |
37 DCHECK(observer); | |
38 observers_.AddObserver(observer); | |
39 } | |
40 | |
41 // BluetoothAdapterClient override. | |
42 void RemoveObserver( | |
43 BluetoothLEAdvertisingManagerClient::Observer* observer) override { | |
44 DCHECK(observer); | |
45 observers_.RemoveObserver(observer); | |
46 } | |
47 | |
48 // dbus::ObjectManager::Interface override. | |
49 dbus::PropertySet* CreateProperties( | |
50 dbus::ObjectProxy* object_proxy, | |
51 const dbus::ObjectPath& object_path, | |
52 const std::string& interface_name) override { | |
53 return new dbus::PropertySet(object_proxy, interface_name, | |
54 dbus::PropertySet::PropertyChangedCallback()); | |
55 } | |
56 | |
57 // BluetoothAdvertisementManagerClient override. | |
58 void RegisterAdvertisement(const dbus::ObjectPath& manager_object_path, | |
59 const dbus::ObjectPath& advertisement_object_path, | |
60 const base::Closure& callback, | |
61 const ErrorCallback& error_callback) override { | |
62 dbus::MethodCall method_call( | |
63 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface, | |
64 bluetooth_advertising_manager::kRegisterAdvertisement); | |
65 | |
66 dbus::MessageWriter writer(&method_call); | |
67 writer.AppendObjectPath(advertisement_object_path); | |
68 | |
69 // Empty dictionary for options. | |
70 dbus::MessageWriter dict_entry_writer(NULL); | |
71 writer.OpenDictEntry(&dict_entry_writer); | |
72 writer.CloseContainer(&dict_entry_writer); | |
73 | |
74 writer.AppendObjectPath(advertisement_object_path); | |
75 | |
76 dbus::ObjectProxy* object_proxy = | |
77 object_manager_->GetObjectProxy(manager_object_path); | |
78 object_proxy->CallMethodWithErrorCallback( | |
79 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
80 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, | |
81 weak_ptr_factory_.GetWeakPtr(), callback), | |
82 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, | |
83 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
84 } | |
85 | |
86 // BluetoothAdvertisementManagerClient override. | |
87 void UnregisterAdvertisement( | |
88 const dbus::ObjectPath& manager_object_path, | |
89 const dbus::ObjectPath& advertisement_object_path, | |
90 const base::Closure& callback, | |
91 const ErrorCallback& error_callback) override { | |
92 dbus::MethodCall method_call( | |
93 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface, | |
94 bluetooth_advertising_manager::kUnregisterAdvertisement); | |
95 | |
96 dbus::MessageWriter writer(&method_call); | |
97 writer.AppendObjectPath(advertisement_object_path); | |
98 | |
99 dbus::ObjectProxy* object_proxy = | |
100 object_manager_->GetObjectProxy(manager_object_path); | |
101 object_proxy->CallMethodWithErrorCallback( | |
102 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
103 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, | |
104 weak_ptr_factory_.GetWeakPtr(), callback), | |
105 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, | |
106 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
107 } | |
108 | |
109 protected: | |
110 void Init(dbus::Bus* bus) override { | |
111 DCHECK(bus); | |
112 object_manager_ = bus->GetObjectManager( | |
113 bluetooth_object_manager::kBluetoothObjectManagerServiceName, | |
114 dbus::ObjectPath( | |
115 bluetooth_object_manager::kBluetoothObjectManagerServicePath)); | |
116 object_manager_->RegisterInterface( | |
117 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface, | |
118 this); | |
119 } | |
120 | |
121 private: | |
122 // Called by dbus::ObjectManager when an object with the adapter interface | |
Marie Janssen
2015/04/14 16:47:24
advertising manager interface
rkc
2015/04/14 17:54:32
Done.
| |
123 // is created. Informs observers. | |
124 void ObjectAdded(const dbus::ObjectPath& object_path, | |
125 const std::string& interface_name) override { | |
126 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer, observers_, | |
127 AdvertisingManagerAdded(object_path)); | |
128 } | |
129 | |
130 // Called by dbus::ObjectManager when an object with the adapter interface | |
Marie Janssen
2015/04/14 16:47:24
ditto
rkc
2015/04/14 17:54:32
Done.
| |
131 // is removed. Informs observers. | |
132 void ObjectRemoved(const dbus::ObjectPath& object_path, | |
133 const std::string& interface_name) override { | |
134 FOR_EACH_OBSERVER(BluetoothLEAdvertisingManagerClient::Observer, observers_, | |
135 AdvertisingManagerRemoved(object_path)); | |
136 } | |
137 | |
138 // Called when a response for successful method call is received. | |
139 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | |
140 DCHECK(response); | |
141 callback.Run(); | |
142 } | |
143 | |
144 // Called when a response for a failed method call is received. | |
145 void OnError(const ErrorCallback& error_callback, | |
146 dbus::ErrorResponse* response) { | |
147 // Error response has optional error message argument. | |
148 std::string error_name; | |
149 std::string error_message; | |
150 if (response) { | |
151 dbus::MessageReader reader(response); | |
152 error_name = response->GetErrorName(); | |
153 reader.PopString(&error_message); | |
154 } else { | |
155 error_name = kNoResponseError; | |
156 error_message = ""; | |
157 } | |
158 error_callback.Run(error_name, error_message); | |
159 } | |
160 | |
161 dbus::ObjectManager* object_manager_; | |
162 | |
163 // List of observers interested in event notifications from us. | |
164 ObserverList<BluetoothLEAdvertisingManagerClient::Observer> observers_; | |
165 | |
166 // Weak pointer factory for generating 'this' pointers that might live longer | |
167 // than we do. | |
168 // Note: This should remain the last member so it'll be destroyed and | |
169 // invalidate its weak pointers before any other members are destroyed. | |
170 base::WeakPtrFactory<BluetoothAdvertisementManagerClientImpl> | |
171 weak_ptr_factory_; | |
172 | |
173 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl); | |
174 }; | |
175 | |
176 BluetoothLEAdvertisingManagerClient::BluetoothLEAdvertisingManagerClient() { | |
177 } | |
178 | |
179 BluetoothLEAdvertisingManagerClient::~BluetoothLEAdvertisingManagerClient() { | |
180 } | |
181 | |
182 BluetoothLEAdvertisingManagerClient* | |
183 BluetoothLEAdvertisingManagerClient::Create() { | |
184 return new BluetoothAdvertisementManagerClientImpl(); | |
185 } | |
186 | |
187 } // namespace chromeos | |
OLD | NEW |