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