Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/physical_web/data_source/fake_physical_web_data_source.h" | |
| 6 | |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "base/values.h" | |
| 9 #include "components/physical_web/data_source/physical_web_listener.h" | |
| 10 | |
| 11 using base::ListValue; | |
| 12 using base::DictionaryValue; | |
| 13 | |
| 14 namespace physical_web { | |
| 15 | |
| 16 std::unique_ptr<DictionaryValue> CreatePhysicalWebPage( | |
| 17 const std::string& resolved_url, | |
| 18 double distance_estimate, | |
| 19 int scan_timestamp, | |
| 20 const std::string& title, | |
| 21 const std::string& description, | |
| 22 const std::string& scanned_url) { | |
| 23 auto page = base::MakeUnique<DictionaryValue>(); | |
| 24 page->SetString(kScannedUrlKey, scanned_url); | |
| 25 page->SetDouble(kDistanceEstimateKey, distance_estimate); | |
| 26 // TODO(vitaliii): Do not set integer once crbug.com/667722 is resolved. | |
|
Olivier
2016/11/28 08:52:05
Can you use the format
// TODO(crbug.com/667722):
vitaliii
2016/11/28 08:58:23
Done.
I stopped on
// TODO(crbug.com/667722): Rem
| |
| 27 page->SetInteger(kScanTimestampKey, scan_timestamp); | |
| 28 page->SetString(kResolvedUrlKey, resolved_url); | |
| 29 page->SetString(kTitleKey, title); | |
| 30 page->SetString(kDescriptionKey, description); | |
| 31 return page; | |
| 32 } | |
| 33 | |
| 34 std::unique_ptr<DictionaryValue> CreateDummyPhysicalWebPage(int id, | |
| 35 double distance, | |
| 36 int timestamp) { | |
| 37 const std::string id_string = base::IntToString(id); | |
| 38 return CreatePhysicalWebPage("https://resolved_url.com/" + id_string, | |
| 39 distance, timestamp, "title " + id_string, | |
| 40 "description " + id_string, | |
| 41 "https://scanned_url.com/" + id_string); | |
| 42 } | |
| 43 | |
| 44 std::unique_ptr<ListValue> CreateDummyPhysicalWebPages( | |
| 45 const std::vector<int>& ids) { | |
| 46 int distance = 1; | |
| 47 int timestamp = ids.size(); | |
| 48 auto list = base::MakeUnique<ListValue>(); | |
| 49 for (int id : ids) { | |
| 50 list->Append(CreateDummyPhysicalWebPage(id, distance, timestamp)); | |
| 51 ++distance; | |
| 52 --timestamp; | |
| 53 } | |
| 54 return list; | |
| 55 } | |
| 56 | |
| 57 FakePhysicalWebDataSource::FakePhysicalWebDataSource() | |
| 58 : metadata_(base::MakeUnique<ListValue>()) {} | |
| 59 | |
| 60 FakePhysicalWebDataSource::~FakePhysicalWebDataSource() = default; | |
| 61 | |
| 62 void FakePhysicalWebDataSource::StartDiscovery(bool network_request_enabled) { | |
| 63 // Ignored. | |
| 64 } | |
| 65 | |
| 66 void FakePhysicalWebDataSource::StopDiscovery() { | |
| 67 // Ignored. | |
| 68 } | |
| 69 | |
| 70 std::unique_ptr<base::ListValue> FakePhysicalWebDataSource::GetMetadata() { | |
| 71 return metadata_->CreateDeepCopy(); | |
| 72 } | |
| 73 | |
| 74 bool FakePhysicalWebDataSource::HasUnresolvedDiscoveries() { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 void FakePhysicalWebDataSource::RegisterListener( | |
| 79 PhysicalWebListener* physical_web_listener) { | |
| 80 observer_list_.AddObserver(physical_web_listener); | |
| 81 } | |
| 82 | |
| 83 void FakePhysicalWebDataSource::UnregisterListener( | |
| 84 PhysicalWebListener* physical_web_listener) { | |
| 85 observer_list_.RemoveObserver(physical_web_listener); | |
| 86 } | |
| 87 | |
| 88 void FakePhysicalWebDataSource::SetMetadata( | |
| 89 std::unique_ptr<ListValue> metadata) { | |
| 90 metadata_ = std::move(metadata); | |
| 91 } | |
| 92 | |
| 93 void FakePhysicalWebDataSource::NotifyOnFound(const std::string& url) { | |
| 94 for (PhysicalWebListener& observer : observer_list_) | |
| 95 observer.OnFound(url); | |
| 96 } | |
| 97 | |
| 98 void FakePhysicalWebDataSource::NotifyOnLost(const std::string& url) { | |
| 99 for (PhysicalWebListener& observer : observer_list_) | |
| 100 observer.OnLost(url); | |
| 101 } | |
| 102 | |
| 103 void FakePhysicalWebDataSource::NotifyOnDistanceChanged( | |
| 104 const std::string& url, | |
| 105 double distance_estimate) { | |
| 106 for (PhysicalWebListener& observer : observer_list_) | |
| 107 observer.OnDistanceChanged(url, distance_estimate); | |
| 108 } | |
| 109 | |
| 110 } // namespace physical_web | |
| OLD | NEW |