Add a test for DevTools screenshot recording
[chromium-blink-merge.git] / chromeos / dbus / bluetooth_le_advertising_manager_client.cc
blobc7981757b7b4b12776c1470a69acfa9e131e9b75
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"
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"
16 namespace chromeos {
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 {
25 public:
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.
37 void AddObserver(
38 BluetoothLEAdvertisingManagerClient::Observer* observer) override {
39 DCHECK(observer);
40 observers_.AddObserver(observer);
43 // BluetoothAdapterClient override.
44 void RemoveObserver(
45 BluetoothLEAdvertisingManagerClient::Observer* observer) override {
46 DCHECK(observer);
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));
113 protected:
114 void Init(dbus::Bus* bus) override {
115 DCHECK(bus);
116 object_manager_ = bus->GetObjectManager(
117 bluetooth_object_manager::kBluetoothObjectManagerServiceName,
118 dbus::ObjectPath(
119 bluetooth_object_manager::kBluetoothObjectManagerServicePath));
120 object_manager_->RegisterInterface(
121 bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface,
122 this);
125 private:
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) {
144 DCHECK(response);
145 callback.Run();
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;
154 if (response) {
155 dbus::MessageReader reader(response);
156 error_name = response->GetErrorName();
157 reader.PopString(&error_message);
158 } else {
159 error_name = kNoResponseError;
160 error_message = "";
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
171 // than we do.
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>
175 weak_ptr_factory_;
177 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl);
180 BluetoothLEAdvertisingManagerClient::BluetoothLEAdvertisingManagerClient() {
183 BluetoothLEAdvertisingManagerClient::~BluetoothLEAdvertisingManagerClient() {
186 BluetoothLEAdvertisingManagerClient*
187 BluetoothLEAdvertisingManagerClient::Create() {
188 return new BluetoothAdvertisementManagerClientImpl();
191 } // namespace chromeos