| 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_tag_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "chromeos/dbus/nfc_adapter_client.h" | |
| 13 #include "dbus/bus.h" | |
| 14 #include "dbus/message.h" | |
| 15 #include "dbus/values_util.h" | |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 17 | |
| 18 using chromeos::nfc_client_helpers::DBusObjectMap; | |
| 19 using chromeos::nfc_client_helpers::ObjectProxyTree; | |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 NfcTagClient::Properties::Properties( | |
| 24 dbus::ObjectProxy* object_proxy, | |
| 25 const PropertyChangedCallback& callback) | |
| 26 : NfcPropertySet(object_proxy, | |
| 27 nfc_tag::kNfcTagInterface, | |
| 28 callback) { | |
| 29 RegisterProperty(nfc_tag::kTypeProperty, &type); | |
| 30 RegisterProperty(nfc_tag::kProtocolProperty, &protocol); | |
| 31 RegisterProperty(nfc_tag::kRecordsProperty, &records); | |
| 32 RegisterProperty(nfc_tag::kReadOnlyProperty, &read_only); | |
| 33 } | |
| 34 | |
| 35 NfcTagClient::Properties::~Properties() { | |
| 36 } | |
| 37 | |
| 38 // The NfcTagClient implementation used in production. | |
| 39 class NfcTagClientImpl : public NfcTagClient, | |
| 40 public NfcAdapterClient::Observer, | |
| 41 public DBusObjectMap::Delegate { | |
| 42 public: | |
| 43 explicit NfcTagClientImpl(NfcAdapterClient* adapter_client) | |
| 44 : bus_(NULL), | |
| 45 adapter_client_(adapter_client), | |
| 46 weak_ptr_factory_(this) { | |
| 47 DCHECK(adapter_client); | |
| 48 } | |
| 49 | |
| 50 ~NfcTagClientImpl() override { | |
| 51 DCHECK(adapter_client_); | |
| 52 adapter_client_->RemoveObserver(this); | |
| 53 } | |
| 54 | |
| 55 // NfcTagClient override. | |
| 56 void AddObserver(NfcTagClient::Observer* observer) override { | |
| 57 DCHECK(observer); | |
| 58 observers_.AddObserver(observer); | |
| 59 } | |
| 60 | |
| 61 // NfcTagClient override. | |
| 62 void RemoveObserver(NfcTagClient::Observer* observer) override { | |
| 63 DCHECK(observer); | |
| 64 observers_.RemoveObserver(observer); | |
| 65 } | |
| 66 | |
| 67 // NfcTagClient override. | |
| 68 std::vector<dbus::ObjectPath> GetTagsForAdapter( | |
| 69 const dbus::ObjectPath& adapter_path) override { | |
| 70 DBusObjectMap* object_map = | |
| 71 adapters_to_object_maps_.GetObjectMap(adapter_path); | |
| 72 if (!object_map) | |
| 73 return std::vector<dbus::ObjectPath>(); | |
| 74 return object_map->GetObjectPaths(); | |
| 75 } | |
| 76 | |
| 77 // NfcTagClient override. | |
| 78 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | |
| 79 return static_cast<Properties*>( | |
| 80 adapters_to_object_maps_.FindObjectProperties(object_path)); | |
| 81 } | |
| 82 | |
| 83 // NfcTagClient override. | |
| 84 void Write(const dbus::ObjectPath& object_path, | |
| 85 const base::DictionaryValue& attributes, | |
| 86 const base::Closure& callback, | |
| 87 const nfc_client_helpers::ErrorCallback& error_callback) override { | |
| 88 dbus::ObjectProxy* object_proxy = | |
| 89 adapters_to_object_maps_.FindObjectProxy(object_path); | |
| 90 if (!object_proxy) { | |
| 91 std::string error_message = | |
| 92 base::StringPrintf("NFC tag with object path \"%s\" does not exist.", | |
| 93 object_path.value().c_str()); | |
| 94 LOG(ERROR) << error_message; | |
| 95 error_callback.Run(nfc_client_helpers::kUnknownObjectError, | |
| 96 error_message); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 // |attributes| should not be empty. | |
| 101 if (attributes.empty()) { | |
| 102 std::string error_message = | |
| 103 "Cannot write data to tag with empty arguments."; | |
| 104 LOG(ERROR) << error_message; | |
| 105 error_callback.Run(nfc_error::kInvalidArguments, error_message); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 // Create the arguments. | |
| 110 dbus::MethodCall method_call(nfc_tag::kNfcTagInterface, nfc_tag::kWrite); | |
| 111 dbus::MessageWriter writer(&method_call); | |
| 112 dbus::AppendValueData(&writer, attributes); | |
| 113 | |
| 114 object_proxy->CallMethodWithErrorCallback( | |
| 115 &method_call, | |
| 116 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 117 base::Bind(&nfc_client_helpers::OnSuccess, callback), | |
| 118 base::Bind(&nfc_client_helpers::OnError, error_callback)); | |
| 119 } | |
| 120 | |
| 121 protected: | |
| 122 // DBusClient override. | |
| 123 void Init(dbus::Bus* bus) override { | |
| 124 VLOG(1) << "Creating NfcTagClientImpl"; | |
| 125 DCHECK(bus); | |
| 126 bus_ = bus; | |
| 127 DCHECK(adapter_client_); | |
| 128 adapter_client_->AddObserver(this); | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 // NfcAdapterClient::Observer override. | |
| 133 void AdapterAdded(const dbus::ObjectPath& object_path) override { | |
| 134 VLOG(1) << "Adapter added. Creating map for tag proxies belonging to " | |
| 135 << "adapter: " << object_path.value(); | |
| 136 adapters_to_object_maps_.CreateObjectMap( | |
| 137 object_path, nfc_tag::kNfcTagServiceName, this, bus_); | |
| 138 } | |
| 139 | |
| 140 // NfcAdapterClient::Observer override. | |
| 141 void AdapterRemoved(const dbus::ObjectPath& object_path) override { | |
| 142 // Neard doesn't send out property changed signals for the tags that | |
| 143 // are removed when the adapter they belong to is removed. Clean up the | |
| 144 // object proxies for devices that are managed by the removed adapter. | |
| 145 // Note: DBusObjectMap guarantees that the Properties structure for the | |
| 146 // removed adapter will be valid before this method returns. | |
| 147 VLOG(1) << "Adapter removed. Cleaning up tag proxies belonging to " | |
| 148 << "adapter: " << object_path.value(); | |
| 149 adapters_to_object_maps_.RemoveObjectMap(object_path); | |
| 150 } | |
| 151 | |
| 152 // NfcAdapterClient::Observer override. | |
| 153 void AdapterPropertyChanged(const dbus::ObjectPath& object_path, | |
| 154 const std::string& property_name) override { | |
| 155 // Update the tag proxies. | |
| 156 DCHECK(adapter_client_); | |
| 157 NfcAdapterClient::Properties *adapter_properties = | |
| 158 adapter_client_->GetProperties(object_path); | |
| 159 DCHECK(adapter_properties); | |
| 160 if (!adapter_properties) { | |
| 161 LOG(ERROR) << "No property structure found for adapter: " | |
| 162 << object_path.value(); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 // Ignore changes to properties other than "Tags". | |
| 167 if (property_name != adapter_properties->tags.name()) | |
| 168 return; | |
| 169 | |
| 170 // Update the known tags. | |
| 171 VLOG(1) << "NFC tags changed."; | |
| 172 const std::vector<dbus::ObjectPath>& received_tags = | |
| 173 adapter_properties->tags.value(); | |
| 174 DBusObjectMap* object_map = | |
| 175 adapters_to_object_maps_.GetObjectMap(object_path); | |
| 176 DCHECK(object_map); | |
| 177 object_map->UpdateObjects(received_tags); | |
| 178 } | |
| 179 | |
| 180 // nfc_client_helpers::DBusObjectMap::Delegate override. | |
| 181 NfcPropertySet* CreateProperties(dbus::ObjectProxy* object_proxy) override { | |
| 182 Properties* properties = new Properties( | |
| 183 object_proxy, | |
| 184 base::Bind(&NfcTagClientImpl::OnPropertyChanged, | |
| 185 weak_ptr_factory_.GetWeakPtr(), | |
| 186 object_proxy->object_path())); | |
| 187 properties->SetAllPropertiesReceivedCallback( | |
| 188 base::Bind(&NfcTagClientImpl::OnPropertiesReceived, | |
| 189 weak_ptr_factory_.GetWeakPtr(), | |
| 190 object_proxy->object_path())); | |
| 191 return properties; | |
| 192 } | |
| 193 | |
| 194 // nfc_client_helpers::DBusObjectMap::Delegate override. | |
| 195 void ObjectAdded(const dbus::ObjectPath& object_path) override { | |
| 196 FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_, | |
| 197 TagAdded(object_path)); | |
| 198 } | |
| 199 | |
| 200 void ObjectRemoved(const dbus::ObjectPath& object_path) override { | |
| 201 FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_, | |
| 202 TagRemoved(object_path)); | |
| 203 } | |
| 204 | |
| 205 // Called by NfcPropertySet when a property value is changed, either by | |
| 206 // result of a signal or response to a GetAll() or Get() call. | |
| 207 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
| 208 const std::string& property_name) { | |
| 209 VLOG(1) << "Tag property changed; Path: " << object_path.value() | |
| 210 << " Property: " << property_name; | |
| 211 FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_, | |
| 212 TagPropertyChanged(object_path, property_name)); | |
| 213 } | |
| 214 | |
| 215 // Called by NfcPropertySet when all properties have been processed as a | |
| 216 // result of a call to GetAll. | |
| 217 void OnPropertiesReceived(const dbus::ObjectPath& object_path) { | |
| 218 VLOG(1) << "All tag properties received; Path: " << object_path.value(); | |
| 219 FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_, | |
| 220 TagPropertiesReceived(object_path)); | |
| 221 } | |
| 222 | |
| 223 // We maintain a pointer to the bus to be able to request proxies for | |
| 224 // new NFC tags that appear. | |
| 225 dbus::Bus* bus_; | |
| 226 | |
| 227 // List of observers interested in event notifications. | |
| 228 base::ObserverList<NfcTagClient::Observer> observers_; | |
| 229 | |
| 230 // Mapping from object paths to object proxies and properties structures that | |
| 231 // were already created by us. This stucture stores a different DBusObjectMap | |
| 232 // for each known NFC adapter object path. | |
| 233 ObjectProxyTree adapters_to_object_maps_; | |
| 234 | |
| 235 // The adapter client that we listen to events notifications from. | |
| 236 NfcAdapterClient* adapter_client_; | |
| 237 | |
| 238 // Weak pointer factory for generating 'this' pointers that might live longer | |
| 239 // than we do. | |
| 240 // Note: This should remain the last member so it'll be destroyed and | |
| 241 // invalidate its weak pointers before any other members are destroyed. | |
| 242 base::WeakPtrFactory<NfcTagClientImpl> weak_ptr_factory_; | |
| 243 | |
| 244 DISALLOW_COPY_AND_ASSIGN(NfcTagClientImpl); | |
| 245 }; | |
| 246 | |
| 247 NfcTagClient::NfcTagClient() { | |
| 248 } | |
| 249 | |
| 250 NfcTagClient::~NfcTagClient() { | |
| 251 } | |
| 252 | |
| 253 NfcTagClient* NfcTagClient::Create(NfcAdapterClient* adapter_client) { | |
| 254 return new NfcTagClientImpl(adapter_client); | |
| 255 } | |
| 256 | |
| 257 } // namespace chromeos | |
| OLD | NEW |