| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/nfc_adapter_client.h" | 5 #include "chromeos/dbus/nfc_adapter_client.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 manager_client_(manager_client), | 46 manager_client_(manager_client), |
| 47 weak_ptr_factory_(this) { | 47 weak_ptr_factory_(this) { |
| 48 DCHECK(manager_client); | 48 DCHECK(manager_client); |
| 49 } | 49 } |
| 50 | 50 |
| 51 virtual ~NfcAdapterClientImpl() { | 51 virtual ~NfcAdapterClientImpl() { |
| 52 manager_client_->RemoveObserver(this); | 52 manager_client_->RemoveObserver(this); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // NfcAdapterClient override. | 55 // NfcAdapterClient override. |
| 56 virtual void AddObserver(NfcAdapterClient::Observer* observer) OVERRIDE { | 56 virtual void AddObserver(NfcAdapterClient::Observer* observer) override { |
| 57 DCHECK(observer); | 57 DCHECK(observer); |
| 58 observers_.AddObserver(observer); | 58 observers_.AddObserver(observer); |
| 59 } | 59 } |
| 60 | 60 |
| 61 // NfcAdapterClient override. | 61 // NfcAdapterClient override. |
| 62 virtual void RemoveObserver(NfcAdapterClient::Observer* observer) OVERRIDE { | 62 virtual void RemoveObserver(NfcAdapterClient::Observer* observer) override { |
| 63 DCHECK(observer); | 63 DCHECK(observer); |
| 64 observers_.RemoveObserver(observer); | 64 observers_.RemoveObserver(observer); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // NfcAdapterClient override. | 67 // NfcAdapterClient override. |
| 68 virtual std::vector<dbus::ObjectPath> GetAdapters() OVERRIDE { | 68 virtual std::vector<dbus::ObjectPath> GetAdapters() override { |
| 69 return object_map_->GetObjectPaths(); | 69 return object_map_->GetObjectPaths(); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // NfcAdapterClient override. | 72 // NfcAdapterClient override. |
| 73 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | 73 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) |
| 74 OVERRIDE { | 74 override { |
| 75 return static_cast<Properties*>( | 75 return static_cast<Properties*>( |
| 76 object_map_->GetObjectProperties(object_path)); | 76 object_map_->GetObjectProperties(object_path)); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // NfcAdapterClient override. | 79 // NfcAdapterClient override. |
| 80 virtual void StartPollLoop( | 80 virtual void StartPollLoop( |
| 81 const dbus::ObjectPath& object_path, | 81 const dbus::ObjectPath& object_path, |
| 82 const std::string& mode, | 82 const std::string& mode, |
| 83 const base::Closure& callback, | 83 const base::Closure& callback, |
| 84 const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE { | 84 const nfc_client_helpers::ErrorCallback& error_callback) override { |
| 85 dbus::ObjectProxy* object_proxy = object_map_->GetObjectProxy(object_path); | 85 dbus::ObjectProxy* object_proxy = object_map_->GetObjectProxy(object_path); |
| 86 if (!object_proxy) { | 86 if (!object_proxy) { |
| 87 std::string error_message = | 87 std::string error_message = |
| 88 base::StringPrintf("NFC adapter with object path \"%s\" does not " | 88 base::StringPrintf("NFC adapter with object path \"%s\" does not " |
| 89 "exist.", object_path.value().c_str()); | 89 "exist.", object_path.value().c_str()); |
| 90 LOG(ERROR) << error_message; | 90 LOG(ERROR) << error_message; |
| 91 error_callback.Run(nfc_client_helpers::kUnknownObjectError, | 91 error_callback.Run(nfc_client_helpers::kUnknownObjectError, |
| 92 error_message); | 92 error_message); |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 dbus::MethodCall method_call(nfc_adapter::kNfcAdapterInterface, | 95 dbus::MethodCall method_call(nfc_adapter::kNfcAdapterInterface, |
| 96 nfc_adapter::kStartPollLoop); | 96 nfc_adapter::kStartPollLoop); |
| 97 dbus::MessageWriter writer(&method_call); | 97 dbus::MessageWriter writer(&method_call); |
| 98 writer.AppendString(mode); | 98 writer.AppendString(mode); |
| 99 object_proxy->CallMethodWithErrorCallback( | 99 object_proxy->CallMethodWithErrorCallback( |
| 100 &method_call, | 100 &method_call, |
| 101 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 101 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 102 base::Bind(&nfc_client_helpers::OnSuccess, callback), | 102 base::Bind(&nfc_client_helpers::OnSuccess, callback), |
| 103 base::Bind(&nfc_client_helpers::OnError, error_callback)); | 103 base::Bind(&nfc_client_helpers::OnError, error_callback)); |
| 104 } | 104 } |
| 105 | 105 |
| 106 // NfcAdapterClient override. | 106 // NfcAdapterClient override. |
| 107 virtual void StopPollLoop( | 107 virtual void StopPollLoop( |
| 108 const dbus::ObjectPath& object_path, | 108 const dbus::ObjectPath& object_path, |
| 109 const base::Closure& callback, | 109 const base::Closure& callback, |
| 110 const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE { | 110 const nfc_client_helpers::ErrorCallback& error_callback) override { |
| 111 dbus::ObjectProxy* object_proxy = object_map_->GetObjectProxy(object_path); | 111 dbus::ObjectProxy* object_proxy = object_map_->GetObjectProxy(object_path); |
| 112 if (!object_proxy) { | 112 if (!object_proxy) { |
| 113 std::string error_message = | 113 std::string error_message = |
| 114 base::StringPrintf("NFC adapter with object path \"%s\" does not " | 114 base::StringPrintf("NFC adapter with object path \"%s\" does not " |
| 115 "exist.", object_path.value().c_str()); | 115 "exist.", object_path.value().c_str()); |
| 116 LOG(ERROR) << error_message; | 116 LOG(ERROR) << error_message; |
| 117 error_callback.Run(nfc_client_helpers::kUnknownObjectError, | 117 error_callback.Run(nfc_client_helpers::kUnknownObjectError, |
| 118 error_message); | 118 error_message); |
| 119 return; | 119 return; |
| 120 } | 120 } |
| 121 dbus::MethodCall method_call(nfc_adapter::kNfcAdapterInterface, | 121 dbus::MethodCall method_call(nfc_adapter::kNfcAdapterInterface, |
| 122 nfc_adapter::kStopPollLoop); | 122 nfc_adapter::kStopPollLoop); |
| 123 object_proxy->CallMethodWithErrorCallback( | 123 object_proxy->CallMethodWithErrorCallback( |
| 124 &method_call, | 124 &method_call, |
| 125 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 125 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 126 base::Bind(&nfc_client_helpers::OnSuccess, callback), | 126 base::Bind(&nfc_client_helpers::OnSuccess, callback), |
| 127 base::Bind(&nfc_client_helpers::OnError, error_callback)); | 127 base::Bind(&nfc_client_helpers::OnError, error_callback)); |
| 128 } | 128 } |
| 129 | 129 |
| 130 protected: | 130 protected: |
| 131 // DBusClient override. | 131 // DBusClient override. |
| 132 virtual void Init(dbus::Bus* bus) OVERRIDE { | 132 virtual void Init(dbus::Bus* bus) override { |
| 133 VLOG(1) << "Creating NfcAdapterClientImpl"; | 133 VLOG(1) << "Creating NfcAdapterClientImpl"; |
| 134 DCHECK(bus); | 134 DCHECK(bus); |
| 135 bus_ = bus; | 135 bus_ = bus; |
| 136 object_map_.reset(new nfc_client_helpers::DBusObjectMap( | 136 object_map_.reset(new nfc_client_helpers::DBusObjectMap( |
| 137 nfc_adapter::kNfcAdapterServiceName, this, bus)); | 137 nfc_adapter::kNfcAdapterServiceName, this, bus)); |
| 138 DCHECK(manager_client_); | 138 DCHECK(manager_client_); |
| 139 manager_client_->AddObserver(this); | 139 manager_client_->AddObserver(this); |
| 140 } | 140 } |
| 141 | 141 |
| 142 private: | 142 private: |
| 143 // NfcManagerClient::Observer override. | 143 // NfcManagerClient::Observer override. |
| 144 virtual void ManagerPropertyChanged( | 144 virtual void ManagerPropertyChanged( |
| 145 const std::string& property_name) OVERRIDE { | 145 const std::string& property_name) override { |
| 146 // Update the adapter proxies. | 146 // Update the adapter proxies. |
| 147 DCHECK(manager_client_); | 147 DCHECK(manager_client_); |
| 148 NfcManagerClient::Properties* manager_properties = | 148 NfcManagerClient::Properties* manager_properties = |
| 149 manager_client_->GetProperties(); | 149 manager_client_->GetProperties(); |
| 150 | 150 |
| 151 // Ignore changes to properties other than "Adapters". | 151 // Ignore changes to properties other than "Adapters". |
| 152 if (property_name != manager_properties->adapters.name()) | 152 if (property_name != manager_properties->adapters.name()) |
| 153 return; | 153 return; |
| 154 | 154 |
| 155 // Update the known adapters. | 155 // Update the known adapters. |
| 156 VLOG(1) << "NFC adapters changed."; | 156 VLOG(1) << "NFC adapters changed."; |
| 157 const std::vector<dbus::ObjectPath>& received_adapters = | 157 const std::vector<dbus::ObjectPath>& received_adapters = |
| 158 manager_properties->adapters.value(); | 158 manager_properties->adapters.value(); |
| 159 object_map_->UpdateObjects(received_adapters); | 159 object_map_->UpdateObjects(received_adapters); |
| 160 } | 160 } |
| 161 | 161 |
| 162 // nfc_client_helpers::DBusObjectMap::Delegate override. | 162 // nfc_client_helpers::DBusObjectMap::Delegate override. |
| 163 virtual NfcPropertySet* CreateProperties( | 163 virtual NfcPropertySet* CreateProperties( |
| 164 dbus::ObjectProxy* object_proxy) OVERRIDE { | 164 dbus::ObjectProxy* object_proxy) override { |
| 165 return new Properties( | 165 return new Properties( |
| 166 object_proxy, | 166 object_proxy, |
| 167 base::Bind(&NfcAdapterClientImpl::OnPropertyChanged, | 167 base::Bind(&NfcAdapterClientImpl::OnPropertyChanged, |
| 168 weak_ptr_factory_.GetWeakPtr(), | 168 weak_ptr_factory_.GetWeakPtr(), |
| 169 object_proxy->object_path())); | 169 object_proxy->object_path())); |
| 170 } | 170 } |
| 171 | 171 |
| 172 // nfc_client_helpers::DBusObjectMap::Delegate override. | 172 // nfc_client_helpers::DBusObjectMap::Delegate override. |
| 173 virtual void ObjectAdded(const dbus::ObjectPath& object_path) OVERRIDE { | 173 virtual void ObjectAdded(const dbus::ObjectPath& object_path) override { |
| 174 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_, | 174 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_, |
| 175 AdapterAdded(object_path)); | 175 AdapterAdded(object_path)); |
| 176 } | 176 } |
| 177 | 177 |
| 178 // nfc_client_helpers::DBusObjectMap::Delegate override. | 178 // nfc_client_helpers::DBusObjectMap::Delegate override. |
| 179 virtual void ObjectRemoved(const dbus::ObjectPath& object_path) OVERRIDE { | 179 virtual void ObjectRemoved(const dbus::ObjectPath& object_path) override { |
| 180 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_, | 180 FOR_EACH_OBSERVER(NfcAdapterClient::Observer, observers_, |
| 181 AdapterRemoved(object_path)); | 181 AdapterRemoved(object_path)); |
| 182 } | 182 } |
| 183 | 183 |
| 184 // Called by NfcPropertySet when a property value is changed, either by | 184 // Called by NfcPropertySet when a property value is changed, either by |
| 185 // result of a signal or response to a GetAll() or Get() call. | 185 // result of a signal or response to a GetAll() or Get() call. |
| 186 void OnPropertyChanged(const dbus::ObjectPath& object_path, | 186 void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 187 const std::string& property_name) { | 187 const std::string& property_name) { |
| 188 VLOG(1) << "Adapter property changed; Path: " << object_path.value() | 188 VLOG(1) << "Adapter property changed; Path: " << object_path.value() |
| 189 << " Property: " << property_name; | 189 << " Property: " << property_name; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 218 } | 218 } |
| 219 | 219 |
| 220 NfcAdapterClient::~NfcAdapterClient() { | 220 NfcAdapterClient::~NfcAdapterClient() { |
| 221 } | 221 } |
| 222 | 222 |
| 223 NfcAdapterClient* NfcAdapterClient::Create(NfcManagerClient* manager_client) { | 223 NfcAdapterClient* NfcAdapterClient::Create(NfcManagerClient* manager_client) { |
| 224 return new NfcAdapterClientImpl(manager_client); | 224 return new NfcAdapterClientImpl(manager_client); |
| 225 } | 225 } |
| 226 | 226 |
| 227 } // namespace chromeos | 227 } // namespace chromeos |
| OLD | NEW |