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