[chromedriver] Drop support for M39 and add support for M42.
[chromium-blink-merge.git] / chromeos / dbus / nfc_adapter_client.cc
blob3cd366417c7c2c0c773e1d13afc94304dfaf649c
1 // Copyright 2013 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/nfc_adapter_client.h"
7 #include <map>
8 #include <utility>
10 #include "base/bind.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/strings/stringprintf.h"
14 #include "chromeos/dbus/nfc_manager_client.h"
15 #include "dbus/bus.h"
16 #include "dbus/message.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 NfcAdapterClient::Properties::Properties(
22 dbus::ObjectProxy* object_proxy,
23 const PropertyChangedCallback& callback)
24 : NfcPropertySet(object_proxy,
25 nfc_adapter::kNfcAdapterInterface,
26 callback) {
27 RegisterProperty(nfc_adapter::kModeProperty, &mode);
28 RegisterProperty(nfc_adapter::kPoweredProperty, &powered);
29 RegisterProperty(nfc_adapter::kPollingProperty, &polling);
30 RegisterProperty(nfc_adapter::kProtocolsProperty, &protocols);
31 RegisterProperty(nfc_adapter::kTagsProperty, &tags);
32 RegisterProperty(nfc_adapter::kDevicesProperty, &devices);
35 NfcAdapterClient::Properties::~Properties() {
38 // The NfcAdapterClient implementation used in production.
39 class NfcAdapterClientImpl
40 : public NfcAdapterClient,
41 public NfcManagerClient::Observer,
42 public nfc_client_helpers::DBusObjectMap::Delegate {
43 public:
44 explicit NfcAdapterClientImpl(NfcManagerClient* manager_client)
45 : bus_(NULL),
46 manager_client_(manager_client),
47 weak_ptr_factory_(this) {
48 DCHECK(manager_client);
51 ~NfcAdapterClientImpl() override { manager_client_->RemoveObserver(this); }
53 // NfcAdapterClient override.
54 void AddObserver(NfcAdapterClient::Observer* observer) override {
55 DCHECK(observer);
56 observers_.AddObserver(observer);
59 // NfcAdapterClient override.
60 void RemoveObserver(NfcAdapterClient::Observer* observer) override {
61 DCHECK(observer);
62 observers_.RemoveObserver(observer);
65 // NfcAdapterClient override.
66 std::vector<dbus::ObjectPath> GetAdapters() override {
67 return object_map_->GetObjectPaths();
70 // NfcAdapterClient override.
71 Properties* GetProperties(const dbus::ObjectPath& object_path) override {
72 return static_cast<Properties*>(
73 object_map_->GetObjectProperties(object_path));
76 // NfcAdapterClient override.
77 void StartPollLoop(
78 const dbus::ObjectPath& object_path,
79 const std::string& mode,
80 const base::Closure& callback,
81 const nfc_client_helpers::ErrorCallback& error_callback) override {
82 dbus::ObjectProxy* object_proxy = object_map_->GetObjectProxy(object_path);
83 if (!object_proxy) {
84 std::string error_message =
85 base::StringPrintf("NFC adapter with object path \"%s\" does not "
86 "exist.", object_path.value().c_str());
87 LOG(ERROR) << error_message;
88 error_callback.Run(nfc_client_helpers::kUnknownObjectError,
89 error_message);
90 return;
92 dbus::MethodCall method_call(nfc_adapter::kNfcAdapterInterface,
93 nfc_adapter::kStartPollLoop);
94 dbus::MessageWriter writer(&method_call);
95 writer.AppendString(mode);
96 object_proxy->CallMethodWithErrorCallback(
97 &method_call,
98 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
99 base::Bind(&nfc_client_helpers::OnSuccess, callback),
100 base::Bind(&nfc_client_helpers::OnError, error_callback));
103 // NfcAdapterClient override.
104 void StopPollLoop(
105 const dbus::ObjectPath& object_path,
106 const base::Closure& callback,
107 const nfc_client_helpers::ErrorCallback& error_callback) override {
108 dbus::ObjectProxy* object_proxy = object_map_->GetObjectProxy(object_path);
109 if (!object_proxy) {
110 std::string error_message =
111 base::StringPrintf("NFC adapter with object path \"%s\" does not "
112 "exist.", object_path.value().c_str());
113 LOG(ERROR) << error_message;
114 error_callback.Run(nfc_client_helpers::kUnknownObjectError,
115 error_message);
116 return;
118 dbus::MethodCall method_call(nfc_adapter::kNfcAdapterInterface,
119 nfc_adapter::kStopPollLoop);
120 object_proxy->CallMethodWithErrorCallback(
121 &method_call,
122 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
123 base::Bind(&nfc_client_helpers::OnSuccess, callback),
124 base::Bind(&nfc_client_helpers::OnError, error_callback));
127 protected:
128 // DBusClient override.
129 void Init(dbus::Bus* bus) override {
130 VLOG(1) << "Creating NfcAdapterClientImpl";
131 DCHECK(bus);
132 bus_ = bus;
133 object_map_.reset(new nfc_client_helpers::DBusObjectMap(
134 nfc_adapter::kNfcAdapterServiceName, this, bus));
135 DCHECK(manager_client_);
136 manager_client_->AddObserver(this);
139 private:
140 // NfcManagerClient::Observer override.
141 void ManagerPropertyChanged(const std::string& property_name) override {
142 // Update the adapter proxies.
143 DCHECK(manager_client_);
144 NfcManagerClient::Properties* manager_properties =
145 manager_client_->GetProperties();
147 // Ignore changes to properties other than "Adapters".
148 if (property_name != manager_properties->adapters.name())
149 return;
151 // Update the known adapters.
152 VLOG(1) << "NFC adapters changed.";
153 const std::vector<dbus::ObjectPath>& received_adapters =
154 manager_properties->adapters.value();
155 object_map_->UpdateObjects(received_adapters);
158 // nfc_client_helpers::DBusObjectMap::Delegate override.
159 NfcPropertySet* CreateProperties(dbus::ObjectProxy* object_proxy) override {
160 return new Properties(
161 object_proxy,
162 base::Bind(&NfcAdapterClientImpl::OnPropertyChanged,
163 weak_ptr_factory_.GetWeakPtr(),
164 object_proxy->object_path()));
167 // nfc_client_helpers::DBusObjectMap::Delegate override.
168 void ObjectAdded(const dbus::ObjectPath& object_path) override {
169 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
170 AdapterAdded(object_path));
173 // nfc_client_helpers::DBusObjectMap::Delegate override.
174 void ObjectRemoved(const dbus::ObjectPath& object_path) override {
175 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
176 AdapterRemoved(object_path));
179 // Called by NfcPropertySet when a property value is changed, either by
180 // result of a signal or response to a GetAll() or Get() call.
181 void OnPropertyChanged(const dbus::ObjectPath& object_path,
182 const std::string& property_name) {
183 VLOG(1) << "Adapter property changed; Path: " << object_path.value()
184 << " Property: " << property_name;
185 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_,
186 AdapterPropertyChanged(object_path, property_name));
189 // We maintain a pointer to the bus to be able to request proxies for
190 // new NFC adapters that appear.
191 dbus::Bus* bus_;
193 // List of observers interested in event notifications.
194 ObserverList<NfcAdapterClient::Observer> observers_;
196 // Mapping from object paths to object proxies and properties structures that
197 // were already created by us.
198 scoped_ptr<nfc_client_helpers::DBusObjectMap> object_map_;
200 // The manager client that we listen to events notifications from.
201 NfcManagerClient* manager_client_;
203 // Weak pointer factory for generating 'this' pointers that might live longer
204 // than we do.
205 // Note: This should remain the last member so it'll be destroyed and
206 // invalidate its weak pointers before any other members are destroyed.
207 base::WeakPtrFactory<NfcAdapterClientImpl> weak_ptr_factory_;
209 DISALLOW_COPY_AND_ASSIGN(NfcAdapterClientImpl);
212 NfcAdapterClient::NfcAdapterClient() {
215 NfcAdapterClient::~NfcAdapterClient() {
218 NfcAdapterClient* NfcAdapterClient::Create(NfcManagerClient* manager_client) {
219 return new NfcAdapterClientImpl(manager_client);
222 } // namespace chromeos