| 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 "chrome/browser/media/webrtc/test_stats_dictionary.h" | |
| 6 | |
| 7 #include "base/json/json_writer.h" | |
| 8 | |
| 9 namespace content { | |
| 10 | |
| 11 TestStatsReportDictionary::TestStatsReportDictionary( | |
| 12 std::unique_ptr<base::DictionaryValue> report) | |
| 13 : report_(std::move(report)) { | |
| 14 DCHECK(report_); | |
| 15 } | |
| 16 | |
| 17 TestStatsReportDictionary::~TestStatsReportDictionary() { | |
| 18 } | |
| 19 | |
| 20 void TestStatsReportDictionary::ForEach( | |
| 21 std::function<void(const TestStatsDictionary&)> iteration) { | |
| 22 for (base::DictionaryValue::Iterator it(*report_); !it.IsAtEnd(); | |
| 23 it.Advance()) { | |
| 24 const base::DictionaryValue* it_value; | |
| 25 DCHECK(it.value().GetAsDictionary(&it_value)); | |
| 26 iteration(TestStatsDictionary(this, it_value)); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 std::vector<TestStatsDictionary> TestStatsReportDictionary::Filter( | |
| 31 std::function<bool(const TestStatsDictionary&)> filter) { | |
| 32 std::vector<TestStatsDictionary> result; | |
| 33 ForEach([&result, &filter](const TestStatsDictionary& stats) { | |
| 34 if (filter(stats)) | |
| 35 result.push_back(stats); | |
| 36 }); | |
| 37 return result; | |
| 38 } | |
| 39 | |
| 40 std::unique_ptr<TestStatsDictionary> TestStatsReportDictionary::Get( | |
| 41 const std::string& id) { | |
| 42 const base::DictionaryValue* dictionary; | |
| 43 if (!report_->GetDictionary(id, &dictionary)) | |
| 44 return nullptr; | |
| 45 return std::unique_ptr<TestStatsDictionary>( | |
| 46 new TestStatsDictionary(this, dictionary)); | |
| 47 } | |
| 48 | |
| 49 std::vector<TestStatsDictionary> TestStatsReportDictionary::GetAll() { | |
| 50 return Filter([](const TestStatsDictionary&) { return true; }); | |
| 51 } | |
| 52 | |
| 53 std::vector<TestStatsDictionary> TestStatsReportDictionary::GetByType( | |
| 54 const std::string& type) { | |
| 55 return Filter([&type](const TestStatsDictionary& stats) { | |
| 56 return stats.GetString("type") == type; | |
| 57 }); | |
| 58 } | |
| 59 | |
| 60 TestStatsDictionary::TestStatsDictionary( | |
| 61 TestStatsReportDictionary* report, const base::DictionaryValue* stats) | |
| 62 : report_(report), stats_(stats) { | |
| 63 DCHECK(report_); | |
| 64 DCHECK(stats_); | |
| 65 } | |
| 66 | |
| 67 TestStatsDictionary::TestStatsDictionary( | |
| 68 const TestStatsDictionary& other) = default; | |
| 69 | |
| 70 TestStatsDictionary::~TestStatsDictionary() { | |
| 71 } | |
| 72 | |
| 73 bool TestStatsDictionary::IsBoolean(const std::string& key) const { | |
| 74 bool value; | |
| 75 return GetBoolean(key, &value); | |
| 76 } | |
| 77 | |
| 78 bool TestStatsDictionary::GetBoolean(const std::string& key) const { | |
| 79 bool value; | |
| 80 DCHECK(GetBoolean(key, &value)); | |
| 81 return value; | |
| 82 } | |
| 83 | |
| 84 bool TestStatsDictionary::IsNumber(const std::string& key) const { | |
| 85 double value; | |
| 86 return GetNumber(key, &value); | |
| 87 } | |
| 88 | |
| 89 double TestStatsDictionary::GetNumber(const std::string& key) const { | |
| 90 double value; | |
| 91 DCHECK(GetNumber(key, &value)); | |
| 92 return value; | |
| 93 } | |
| 94 | |
| 95 bool TestStatsDictionary::IsString(const std::string& key) const { | |
| 96 std::string value; | |
| 97 return GetString(key, &value); | |
| 98 } | |
| 99 | |
| 100 std::string TestStatsDictionary::GetString(const std::string& key) const { | |
| 101 std::string value; | |
| 102 DCHECK(GetString(key, &value)); | |
| 103 return value; | |
| 104 } | |
| 105 | |
| 106 bool TestStatsDictionary::IsSequenceBoolean(const std::string& key) const { | |
| 107 std::vector<bool> value; | |
| 108 return GetSequenceBoolean(key, &value); | |
| 109 } | |
| 110 | |
| 111 std::vector<bool> TestStatsDictionary::GetSequenceBoolean( | |
| 112 const std::string& key) const { | |
| 113 std::vector<bool> value; | |
| 114 DCHECK(GetSequenceBoolean(key, &value)); | |
| 115 return value; | |
| 116 } | |
| 117 | |
| 118 bool TestStatsDictionary::IsSequenceNumber(const std::string& key) const { | |
| 119 std::vector<double> value; | |
| 120 return GetSequenceNumber(key, &value); | |
| 121 } | |
| 122 | |
| 123 std::vector<double> TestStatsDictionary::GetSequenceNumber( | |
| 124 const std::string& key) const { | |
| 125 std::vector<double> value; | |
| 126 DCHECK(GetSequenceNumber(key, &value)); | |
| 127 return value; | |
| 128 } | |
| 129 | |
| 130 bool TestStatsDictionary::IsSequenceString(const std::string& key) const { | |
| 131 std::vector<std::string> value; | |
| 132 return GetSequenceString(key, &value); | |
| 133 } | |
| 134 | |
| 135 std::vector<std::string> TestStatsDictionary::GetSequenceString( | |
| 136 const std::string& key) const { | |
| 137 std::vector<std::string> value; | |
| 138 DCHECK(GetSequenceString(key, &value)); | |
| 139 return value; | |
| 140 } | |
| 141 | |
| 142 bool TestStatsDictionary::GetBoolean( | |
| 143 const std::string& key, bool* out) const { | |
| 144 return stats_->GetBoolean(key, out); | |
| 145 } | |
| 146 | |
| 147 bool TestStatsDictionary::GetNumber( | |
| 148 const std::string& key, double* out) const { | |
| 149 return stats_->GetDouble(key, out); | |
| 150 } | |
| 151 | |
| 152 bool TestStatsDictionary::GetString( | |
| 153 const std::string& key, std::string* out) const { | |
| 154 return stats_->GetString(key, out); | |
| 155 } | |
| 156 | |
| 157 bool TestStatsDictionary::GetSequenceBoolean( | |
| 158 const std::string& key, | |
| 159 std::vector<bool>* out) const { | |
| 160 const base::ListValue* list; | |
| 161 if (!stats_->GetList(key, &list)) | |
| 162 return false; | |
| 163 std::vector<bool> sequence; | |
| 164 bool element; | |
| 165 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 166 if (!list->GetBoolean(i, &element)) | |
| 167 return false; | |
| 168 sequence.push_back(element); | |
| 169 } | |
| 170 *out = std::move(sequence); | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 bool TestStatsDictionary::GetSequenceNumber( | |
| 175 const std::string& key, | |
| 176 std::vector<double>* out) const { | |
| 177 const base::ListValue* list; | |
| 178 if (!stats_->GetList(key, &list)) | |
| 179 return false; | |
| 180 std::vector<double> sequence; | |
| 181 double element; | |
| 182 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 183 if (!list->GetDouble(i, &element)) | |
| 184 return false; | |
| 185 sequence.push_back(element); | |
| 186 } | |
| 187 *out = std::move(sequence); | |
| 188 return true; | |
| 189 } | |
| 190 | |
| 191 bool TestStatsDictionary::GetSequenceString( | |
| 192 const std::string& key, | |
| 193 std::vector<std::string>* out) const { | |
| 194 const base::ListValue* list; | |
| 195 if (!stats_->GetList(key, &list)) | |
| 196 return false; | |
| 197 std::vector<std::string> sequence; | |
| 198 std::string element; | |
| 199 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 200 if (!list->GetString(i, &element)) | |
| 201 return false; | |
| 202 sequence.push_back(element); | |
| 203 } | |
| 204 *out = std::move(sequence); | |
| 205 return true; | |
| 206 } | |
| 207 | |
| 208 std::string TestStatsDictionary::ToString() const { | |
| 209 std::string str; | |
| 210 CHECK(base::JSONWriter::WriteWithOptions( | |
| 211 *stats_, base::JSONWriter::OPTIONS_PRETTY_PRINT, &str)); | |
| 212 return str; | |
| 213 } | |
| 214 | |
| 215 } // namespace content | |
| OLD | NEW |