| 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/fake_nfc_record_client.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 11 #include "chromeos/dbus/fake_nfc_device_client.h" | |
| 12 #include "chromeos/dbus/fake_nfc_tag_client.h" | |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 14 | |
| 15 namespace chromeos { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Gets and returns the value for |key| in |dictionary| as a string. If |key| is | |
| 20 // not found, returns an empty string. | |
| 21 std::string GetStringValue(const base::DictionaryValue& dictionary, | |
| 22 const std::string& key) { | |
| 23 std::string value; | |
| 24 bool result = dictionary.GetString(key, &value); | |
| 25 | |
| 26 // Simply return |value|. |value| will remain untouched if | |
| 27 // base::DictionaryValue::GetString returns false. | |
| 28 DCHECK(result || value.empty()); | |
| 29 return value; | |
| 30 } | |
| 31 | |
| 32 // Gets and returns the value for |key| in |dictionary| as a double. If |key| is | |
| 33 // not found, returns 0. | |
| 34 double GetDoubleValue(const base::DictionaryValue& dictionary, | |
| 35 const std::string& key) { | |
| 36 double value = 0; | |
| 37 bool result = dictionary.GetDouble(key, &value); | |
| 38 | |
| 39 // Simply return |value|. |value| will remain untouched if | |
| 40 // base::DictionaryValue::GetString returns false. | |
| 41 DCHECK(result || !value); | |
| 42 return value; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 const char FakeNfcRecordClient::kDeviceSmartPosterRecordPath[] = | |
| 48 "/fake/device/record0"; | |
| 49 const char FakeNfcRecordClient::kDeviceTextRecordPath[] = | |
| 50 "/fake/device/record1"; | |
| 51 const char FakeNfcRecordClient::kDeviceUriRecordPath[] = "/fake/device/record2"; | |
| 52 const char FakeNfcRecordClient::kTagRecordPath[] = "/fake/tag/record0"; | |
| 53 | |
| 54 FakeNfcRecordClient::Properties::Properties( | |
| 55 const PropertyChangedCallback& callback) | |
| 56 : NfcRecordClient::Properties(NULL, callback) { | |
| 57 } | |
| 58 | |
| 59 FakeNfcRecordClient::Properties::~Properties() { | |
| 60 } | |
| 61 | |
| 62 void FakeNfcRecordClient::Properties::Get( | |
| 63 dbus::PropertyBase* property, | |
| 64 dbus::PropertySet::GetCallback callback) { | |
| 65 VLOG(1) << "Get " << property->name(); | |
| 66 callback.Run(false); | |
| 67 } | |
| 68 | |
| 69 void FakeNfcRecordClient::Properties::GetAll() { | |
| 70 VLOG(1) << "GetAll"; | |
| 71 if (!on_get_all_callback().is_null()) | |
| 72 on_get_all_callback().Run(); | |
| 73 } | |
| 74 | |
| 75 void FakeNfcRecordClient::Properties::Set( | |
| 76 dbus::PropertyBase* property, | |
| 77 dbus::PropertySet::SetCallback callback) { | |
| 78 VLOG(1) << "Set " << property->name(); | |
| 79 callback.Run(false); | |
| 80 } | |
| 81 | |
| 82 FakeNfcRecordClient::FakeNfcRecordClient() | |
| 83 : device_records_visible_(false), | |
| 84 tag_records_visible_(false) { | |
| 85 VLOG(1) << "Creating FakeNfcRecordClient"; | |
| 86 | |
| 87 device_smart_poster_record_properties_.reset(new Properties( | |
| 88 base::Bind(&FakeNfcRecordClient::OnPropertyChanged, | |
| 89 base::Unretained(this), | |
| 90 dbus::ObjectPath(kDeviceSmartPosterRecordPath)))); | |
| 91 device_smart_poster_record_properties_->SetAllPropertiesReceivedCallback( | |
| 92 base::Bind(&FakeNfcRecordClient::OnPropertiesReceived, | |
| 93 base::Unretained(this), | |
| 94 dbus::ObjectPath(kDeviceSmartPosterRecordPath))); | |
| 95 | |
| 96 device_text_record_properties_.reset(new Properties( | |
| 97 base::Bind(&FakeNfcRecordClient::OnPropertyChanged, | |
| 98 base::Unretained(this), | |
| 99 dbus::ObjectPath(kDeviceTextRecordPath)))); | |
| 100 device_text_record_properties_->SetAllPropertiesReceivedCallback( | |
| 101 base::Bind(&FakeNfcRecordClient::OnPropertiesReceived, | |
| 102 base::Unretained(this), | |
| 103 dbus::ObjectPath(kDeviceTextRecordPath))); | |
| 104 | |
| 105 device_uri_record_properties_.reset(new Properties( | |
| 106 base::Bind(&FakeNfcRecordClient::OnPropertyChanged, | |
| 107 base::Unretained(this), | |
| 108 dbus::ObjectPath(kDeviceUriRecordPath)))); | |
| 109 device_uri_record_properties_->SetAllPropertiesReceivedCallback( | |
| 110 base::Bind(&FakeNfcRecordClient::OnPropertiesReceived, | |
| 111 base::Unretained(this), | |
| 112 dbus::ObjectPath(kDeviceUriRecordPath))); | |
| 113 | |
| 114 tag_record_properties_.reset(new Properties( | |
| 115 base::Bind(&FakeNfcRecordClient::OnPropertyChanged, | |
| 116 base::Unretained(this), | |
| 117 dbus::ObjectPath(kTagRecordPath)))); | |
| 118 } | |
| 119 | |
| 120 FakeNfcRecordClient::~FakeNfcRecordClient() { | |
| 121 } | |
| 122 | |
| 123 void FakeNfcRecordClient::Init(dbus::Bus* bus) { | |
| 124 } | |
| 125 | |
| 126 void FakeNfcRecordClient::AddObserver(Observer* observer) { | |
| 127 observers_.AddObserver(observer); | |
| 128 } | |
| 129 | |
| 130 void FakeNfcRecordClient::RemoveObserver(Observer* observer) { | |
| 131 observers_.RemoveObserver(observer); | |
| 132 } | |
| 133 | |
| 134 std::vector<dbus::ObjectPath> FakeNfcRecordClient::GetRecordsForDevice( | |
| 135 const dbus::ObjectPath& device_path) { | |
| 136 std::vector<dbus::ObjectPath> record_paths; | |
| 137 if (device_records_visible_ && | |
| 138 device_path == dbus::ObjectPath(FakeNfcDeviceClient::kDevicePath)) { | |
| 139 record_paths.push_back(dbus::ObjectPath(kDeviceSmartPosterRecordPath)); | |
| 140 record_paths.push_back(dbus::ObjectPath(kDeviceTextRecordPath)); | |
| 141 record_paths.push_back(dbus::ObjectPath(kDeviceUriRecordPath)); | |
| 142 } | |
| 143 return record_paths; | |
| 144 } | |
| 145 | |
| 146 std::vector<dbus::ObjectPath> FakeNfcRecordClient::GetRecordsForTag( | |
| 147 const dbus::ObjectPath& tag_path) { | |
| 148 std::vector<dbus::ObjectPath> record_paths; | |
| 149 if (tag_records_visible_ && tag_path.value() == FakeNfcTagClient::kTagPath) | |
| 150 record_paths.push_back(dbus::ObjectPath(kTagRecordPath)); | |
| 151 return record_paths; | |
| 152 } | |
| 153 | |
| 154 FakeNfcRecordClient::Properties* | |
| 155 FakeNfcRecordClient::GetProperties(const dbus::ObjectPath& object_path) { | |
| 156 if (device_records_visible_) { | |
| 157 if (object_path.value() == kDeviceSmartPosterRecordPath) | |
| 158 return device_smart_poster_record_properties_.get(); | |
| 159 if (object_path.value() == kDeviceTextRecordPath) | |
| 160 return device_text_record_properties_.get(); | |
| 161 if (object_path.value() == kDeviceUriRecordPath) | |
| 162 return device_uri_record_properties_.get(); | |
| 163 return NULL; | |
| 164 } | |
| 165 if (tag_records_visible_ && object_path.value() == kTagRecordPath) | |
| 166 return tag_record_properties_.get(); | |
| 167 return NULL; | |
| 168 } | |
| 169 | |
| 170 void FakeNfcRecordClient::SetDeviceRecordsVisible(bool visible) { | |
| 171 if (device_records_visible_ == visible) { | |
| 172 VLOG(1) << "Record visibility is already: " << visible; | |
| 173 return; | |
| 174 } | |
| 175 FakeNfcDeviceClient* device_client = static_cast<FakeNfcDeviceClient*>( | |
| 176 DBusThreadManager::Get()->GetNfcDeviceClient()); | |
| 177 if (!device_client->device_visible()) { | |
| 178 VLOG(1) << "Cannot set records when device is not visible."; | |
| 179 return; | |
| 180 } | |
| 181 if (!visible) { | |
| 182 device_client->ClearRecords(); | |
| 183 FOR_EACH_OBSERVER( | |
| 184 NfcRecordClient::Observer, observers_, | |
| 185 RecordRemoved(dbus::ObjectPath(kDeviceSmartPosterRecordPath))); | |
| 186 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 187 RecordRemoved(dbus::ObjectPath(kDeviceTextRecordPath))); | |
| 188 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 189 RecordRemoved(dbus::ObjectPath(kDeviceUriRecordPath))); | |
| 190 device_records_visible_ = visible; | |
| 191 return; | |
| 192 } | |
| 193 device_records_visible_ = visible; | |
| 194 std::vector<dbus::ObjectPath> record_paths = | |
| 195 GetRecordsForDevice( | |
| 196 dbus::ObjectPath(FakeNfcDeviceClient::kDevicePath)); | |
| 197 device_client->SetRecords(record_paths); | |
| 198 | |
| 199 // Reassign each property and send signals. | |
| 200 FOR_EACH_OBSERVER( | |
| 201 NfcRecordClient::Observer, observers_, | |
| 202 RecordAdded(dbus::ObjectPath(kDeviceSmartPosterRecordPath))); | |
| 203 device_smart_poster_record_properties_->type.ReplaceValue( | |
| 204 nfc_record::kTypeSmartPoster); | |
| 205 device_smart_poster_record_properties_->uri.ReplaceValue( | |
| 206 "http://fake.uri0.fake"); | |
| 207 device_smart_poster_record_properties_->mime_type.ReplaceValue("text/fake"); | |
| 208 device_smart_poster_record_properties_->size.ReplaceValue(128); | |
| 209 device_smart_poster_record_properties_-> | |
| 210 representation.ReplaceValue("Fake Title"); | |
| 211 device_smart_poster_record_properties_->encoding.ReplaceValue( | |
| 212 nfc_record::kEncodingUtf16); | |
| 213 device_smart_poster_record_properties_->language.ReplaceValue("en"); | |
| 214 OnPropertiesReceived(dbus::ObjectPath(kDeviceSmartPosterRecordPath)); | |
| 215 | |
| 216 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 217 RecordAdded(dbus::ObjectPath(kDeviceTextRecordPath))); | |
| 218 device_text_record_properties_->type.ReplaceValue(nfc_record::kTypeText); | |
| 219 device_text_record_properties_->representation.ReplaceValue( | |
| 220 "Kablosuz \xC4\xb0leti\xC5\x9fim"); | |
| 221 device_text_record_properties_->encoding.ReplaceValue( | |
| 222 nfc_record::kEncodingUtf8); | |
| 223 device_text_record_properties_->language.ReplaceValue("tr"); | |
| 224 OnPropertiesReceived(dbus::ObjectPath(kDeviceTextRecordPath)); | |
| 225 | |
| 226 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 227 RecordAdded(dbus::ObjectPath(kDeviceUriRecordPath))); | |
| 228 device_uri_record_properties_->type.ReplaceValue(nfc_record::kTypeUri); | |
| 229 device_uri_record_properties_->uri.ReplaceValue("file://some/fake/path"); | |
| 230 device_uri_record_properties_->mime_type.ReplaceValue("text/fake"); | |
| 231 device_uri_record_properties_->size.ReplaceValue(512); | |
| 232 OnPropertiesReceived(dbus::ObjectPath(kDeviceUriRecordPath)); | |
| 233 } | |
| 234 | |
| 235 void FakeNfcRecordClient::SetTagRecordsVisible(bool visible) { | |
| 236 if (tag_records_visible_ == visible) { | |
| 237 VLOG(1) << "Record visibility is already: " << visible; | |
| 238 return; | |
| 239 } | |
| 240 FakeNfcTagClient* tag_client = static_cast<FakeNfcTagClient*>( | |
| 241 DBusThreadManager::Get()->GetNfcTagClient()); | |
| 242 if (!tag_client->tag_visible()) { | |
| 243 VLOG(1) << "Cannot set records when tag is not visible."; | |
| 244 return; | |
| 245 } | |
| 246 if (!visible) { | |
| 247 tag_client->ClearRecords(); | |
| 248 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 249 RecordRemoved(dbus::ObjectPath(kTagRecordPath))); | |
| 250 tag_records_visible_ = visible; | |
| 251 return; | |
| 252 } | |
| 253 tag_records_visible_ = visible; | |
| 254 std::vector<dbus::ObjectPath> record_paths = | |
| 255 GetRecordsForTag(dbus::ObjectPath(FakeNfcTagClient::kTagPath)); | |
| 256 tag_client->SetRecords(record_paths); | |
| 257 | |
| 258 // Reassign each property to its current value to trigger a property change | |
| 259 // signal. | |
| 260 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 261 RecordAdded(dbus::ObjectPath(kTagRecordPath))); | |
| 262 tag_record_properties_->type.ReplaceValue( | |
| 263 tag_record_properties_->type.value()); | |
| 264 tag_record_properties_->representation.ReplaceValue( | |
| 265 tag_record_properties_->representation.value()); | |
| 266 tag_record_properties_->encoding.ReplaceValue( | |
| 267 tag_record_properties_->encoding.value()); | |
| 268 tag_record_properties_->language.ReplaceValue( | |
| 269 tag_record_properties_->language.value()); | |
| 270 tag_record_properties_->uri.ReplaceValue( | |
| 271 tag_record_properties_->uri.value()); | |
| 272 tag_record_properties_->mime_type.ReplaceValue( | |
| 273 tag_record_properties_->mime_type.value()); | |
| 274 tag_record_properties_->size.ReplaceValue( | |
| 275 tag_record_properties_->size.value()); | |
| 276 tag_record_properties_->action.ReplaceValue( | |
| 277 tag_record_properties_->action.value()); | |
| 278 OnPropertiesReceived(dbus::ObjectPath(kTagRecordPath)); | |
| 279 } | |
| 280 | |
| 281 bool FakeNfcRecordClient::WriteTagRecord( | |
| 282 const base::DictionaryValue& attributes) { | |
| 283 if (attributes.empty()) | |
| 284 return false; | |
| 285 | |
| 286 tag_record_properties_->type.ReplaceValue( | |
| 287 GetStringValue(attributes, nfc_record::kTypeProperty)); | |
| 288 tag_record_properties_->encoding.ReplaceValue( | |
| 289 GetStringValue(attributes, nfc_record::kEncodingProperty)); | |
| 290 tag_record_properties_->language.ReplaceValue( | |
| 291 GetStringValue(attributes, nfc_record::kLanguageProperty)); | |
| 292 tag_record_properties_->representation.ReplaceValue( | |
| 293 GetStringValue(attributes, nfc_record::kRepresentationProperty)); | |
| 294 tag_record_properties_->uri.ReplaceValue( | |
| 295 GetStringValue(attributes, nfc_record::kUriProperty)); | |
| 296 tag_record_properties_->mime_type.ReplaceValue( | |
| 297 GetStringValue(attributes, nfc_record::kMimeTypeProperty)); | |
| 298 tag_record_properties_->action.ReplaceValue( | |
| 299 GetStringValue(attributes, nfc_record::kActionProperty)); | |
| 300 tag_record_properties_->size.ReplaceValue(static_cast<uint32_t>( | |
| 301 GetDoubleValue(attributes, nfc_record::kSizeProperty))); | |
| 302 | |
| 303 SetTagRecordsVisible(false); | |
| 304 SetTagRecordsVisible(true); | |
| 305 | |
| 306 return true; | |
| 307 } | |
| 308 | |
| 309 void FakeNfcRecordClient::OnPropertyChanged( | |
| 310 const dbus::ObjectPath& object_path, | |
| 311 const std::string& property_name) { | |
| 312 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 313 RecordPropertyChanged(object_path, property_name)); | |
| 314 } | |
| 315 | |
| 316 void FakeNfcRecordClient::OnPropertiesReceived( | |
| 317 const dbus::ObjectPath& object_path) { | |
| 318 FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_, | |
| 319 RecordPropertiesReceived(object_path)); | |
| 320 } | |
| 321 | |
| 322 } // namespace chromeos | |
| OLD | NEW |