OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "platform/inspector_protocol/Values.h" | 5 #include "platform/inspector_protocol/Values.h" |
6 | 6 |
7 #include "platform/inspector_protocol/Parser.h" | 7 #include "platform/inspector_protocol/Parser.h" |
8 #include "platform/inspector_protocol/String16.h" | 8 #include "platform/inspector_protocol/String16.h" |
| 9 |
| 10 #include <algorithm> |
9 #include <cmath> | 11 #include <cmath> |
10 | 12 |
11 namespace blink { | 13 namespace blink { |
12 namespace protocol { | 14 namespace protocol { |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 const char* const nullString = "null"; | 18 const char* const nullString = "null"; |
17 const char* const trueString = "true"; | 19 const char* const trueString = "true"; |
18 const char* const falseString = "false"; | 20 const char* const falseString = "false"; |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 setValue(name, FundamentalValue::create(value)); | 188 setValue(name, FundamentalValue::create(value)); |
187 } | 189 } |
188 | 190 |
189 void DictionaryValue::setString(const String16& name, const String16& value) | 191 void DictionaryValue::setString(const String16& name, const String16& value) |
190 { | 192 { |
191 setValue(name, StringValue::create(value)); | 193 setValue(name, StringValue::create(value)); |
192 } | 194 } |
193 | 195 |
194 void DictionaryValue::setValue(const String16& name, std::unique_ptr<Value> valu
e) | 196 void DictionaryValue::setValue(const String16& name, std::unique_ptr<Value> valu
e) |
195 { | 197 { |
196 DCHECK(value); | 198 set(name, value); |
197 if (m_data.set(name, std::move(value))) | |
198 m_order.append(name); | |
199 } | 199 } |
200 | 200 |
201 void DictionaryValue::setObject(const String16& name, std::unique_ptr<Dictionary
Value> value) | 201 void DictionaryValue::setObject(const String16& name, std::unique_ptr<Dictionary
Value> value) |
202 { | 202 { |
203 DCHECK(value); | 203 set(name, value); |
204 if (m_data.set(name, std::move(value))) | |
205 m_order.append(name); | |
206 } | 204 } |
207 | 205 |
208 void DictionaryValue::setArray(const String16& name, std::unique_ptr<ListValue>
value) | 206 void DictionaryValue::setArray(const String16& name, std::unique_ptr<ListValue>
value) |
209 { | 207 { |
210 DCHECK(value); | 208 set(name, value); |
211 if (m_data.set(name, std::move(value))) | |
212 m_order.append(name); | |
213 } | 209 } |
214 | 210 |
215 bool DictionaryValue::getBoolean(const String16& name, bool* output) const | 211 bool DictionaryValue::getBoolean(const String16& name, bool* output) const |
216 { | 212 { |
217 protocol::Value* value = get(name); | 213 protocol::Value* value = get(name); |
218 if (!value) | 214 if (!value) |
219 return false; | 215 return false; |
220 return value->asBoolean(output); | 216 return value->asBoolean(output); |
221 } | 217 } |
222 | 218 |
(...skipping 13 matching lines...) Expand all Loading... |
236 protocol::ListValue* DictionaryValue::getArray(const String16& name) const | 232 protocol::ListValue* DictionaryValue::getArray(const String16& name) const |
237 { | 233 { |
238 return ListValue::cast(get(name)); | 234 return ListValue::cast(get(name)); |
239 } | 235 } |
240 | 236 |
241 protocol::Value* DictionaryValue::get(const String16& name) const | 237 protocol::Value* DictionaryValue::get(const String16& name) const |
242 { | 238 { |
243 Dictionary::const_iterator it = m_data.find(name); | 239 Dictionary::const_iterator it = m_data.find(name); |
244 if (it == m_data.end()) | 240 if (it == m_data.end()) |
245 return nullptr; | 241 return nullptr; |
246 return it->second; | 242 return it->second.get(); |
247 } | 243 } |
248 | 244 |
249 DictionaryValue::Entry DictionaryValue::at(size_t index) const | 245 DictionaryValue::Entry DictionaryValue::at(size_t index) const |
250 { | 246 { |
251 String16 key = m_order[index]; | 247 const String16 key = m_order[index]; |
252 return std::make_pair(key, m_data.get(key)); | 248 return std::make_pair(key, m_data.find(key)->second.get()); |
253 } | 249 } |
254 | 250 |
255 bool DictionaryValue::booleanProperty(const String16& name, bool defaultValue) c
onst | 251 bool DictionaryValue::booleanProperty(const String16& name, bool defaultValue) c
onst |
256 { | 252 { |
257 bool result = defaultValue; | 253 bool result = defaultValue; |
258 getBoolean(name, &result); | 254 getBoolean(name, &result); |
259 return result; | 255 return result; |
260 } | 256 } |
261 | 257 |
262 double DictionaryValue::numberProperty(const String16& name, double defaultValue
) const | 258 double DictionaryValue::numberProperty(const String16& name, double defaultValue
) const |
263 { | 259 { |
264 double result = defaultValue; | 260 double result = defaultValue; |
265 getNumber(name, &result); | 261 getNumber(name, &result); |
266 return result; | 262 return result; |
267 } | 263 } |
268 | 264 |
269 void DictionaryValue::remove(const String16& name) | 265 void DictionaryValue::remove(const String16& name) |
270 { | 266 { |
271 m_data.remove(name); | 267 m_data.erase(name); |
272 for (size_t i = 0; i < m_order.size(); ++i) { | 268 m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end
()); |
273 if (m_order[i] == name) { | |
274 m_order.remove(i); | |
275 break; | |
276 } | |
277 } | |
278 } | 269 } |
279 | 270 |
280 void DictionaryValue::writeJSON(String16Builder* output) const | 271 void DictionaryValue::writeJSON(String16Builder* output) const |
281 { | 272 { |
282 output->append('{'); | 273 output->append('{'); |
283 for (size_t i = 0; i < m_order.size(); ++i) { | 274 for (size_t i = 0; i < m_order.size(); ++i) { |
284 Dictionary::const_iterator it = m_data.find(m_order[i]); | 275 Dictionary::const_iterator it = m_data.find(m_order[i]); |
285 CHECK(it != m_data.end()); | 276 CHECK(it != m_data.end()); |
286 if (i) | 277 if (i) |
287 output->append(','); | 278 output->append(','); |
288 doubleQuoteStringForJSON(it->first, output); | 279 doubleQuoteStringForJSON(it->first, output); |
289 output->append(':'); | 280 output->append(':'); |
290 it->second->writeJSON(output); | 281 it->second->writeJSON(output); |
291 } | 282 } |
292 output->append('}'); | 283 output->append('}'); |
293 } | 284 } |
294 | 285 |
295 std::unique_ptr<Value> DictionaryValue::clone() const | 286 std::unique_ptr<Value> DictionaryValue::clone() const |
296 { | 287 { |
297 std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); | 288 std::unique_ptr<DictionaryValue> result = DictionaryValue::create(); |
298 for (size_t i = 0; i < m_order.size(); ++i) { | 289 for (size_t i = 0; i < m_order.size(); ++i) { |
299 String16 key = m_order[i]; | 290 String16 key = m_order[i]; |
300 Value* value = m_data.get(key); | 291 Dictionary::const_iterator value = m_data.find(key); |
301 DCHECK(value); | 292 DCHECK(value != m_data.cend() && value->second); |
302 result->setValue(key, value->clone()); | 293 result->setValue(key, value->second->clone()); |
303 } | 294 } |
304 return std::move(result); | 295 return std::move(result); |
305 } | 296 } |
306 | 297 |
307 DictionaryValue::DictionaryValue() | 298 DictionaryValue::DictionaryValue() |
308 : Value(TypeObject) | 299 : Value(TypeObject) |
309 { | 300 { |
310 } | 301 } |
311 | 302 |
312 ListValue::~ListValue() | 303 ListValue::~ListValue() |
313 { | 304 { |
314 } | 305 } |
315 | 306 |
316 void ListValue::writeJSON(String16Builder* output) const | 307 void ListValue::writeJSON(String16Builder* output) const |
317 { | 308 { |
318 output->append('['); | 309 output->append('['); |
319 for (Vector<std::unique_ptr<protocol::Value>>::const_iterator it = m_data.be
gin(); it != m_data.end(); ++it) { | 310 bool first = true; |
320 if (it != m_data.begin()) | 311 for (const std::unique_ptr<protocol::Value>& value : m_data) { |
| 312 if (!first) |
321 output->append(','); | 313 output->append(','); |
322 (*it)->writeJSON(output); | 314 value->writeJSON(output); |
| 315 first = false; |
323 } | 316 } |
324 output->append(']'); | 317 output->append(']'); |
325 } | 318 } |
326 | 319 |
327 std::unique_ptr<Value> ListValue::clone() const | 320 std::unique_ptr<Value> ListValue::clone() const |
328 { | 321 { |
329 std::unique_ptr<ListValue> result = ListValue::create(); | 322 std::unique_ptr<ListValue> result = ListValue::create(); |
330 for (Vector<std::unique_ptr<protocol::Value>>::const_iterator it = m_data.be
gin(); it != m_data.end(); ++it) | 323 for (const std::unique_ptr<protocol::Value>& value : m_data) |
331 result->pushValue((*it)->clone()); | 324 result->pushValue(value->clone()); |
332 return std::move(result); | 325 return std::move(result); |
333 } | 326 } |
334 | 327 |
335 ListValue::ListValue() | 328 ListValue::ListValue() |
336 : Value(TypeArray) | 329 : Value(TypeArray) |
337 { | 330 { |
338 } | 331 } |
339 | 332 |
340 void ListValue::pushValue(std::unique_ptr<protocol::Value> value) | 333 void ListValue::pushValue(std::unique_ptr<protocol::Value> value) |
341 { | 334 { |
342 DCHECK(value); | 335 DCHECK(value); |
343 m_data.append(std::move(value)); | 336 m_data.push_back(std::move(value)); |
344 } | 337 } |
345 | 338 |
346 protocol::Value* ListValue::at(size_t index) | 339 protocol::Value* ListValue::at(size_t index) |
347 { | 340 { |
348 DCHECK_LT(index, m_data.size()); | 341 DCHECK_LT(index, m_data.size()); |
349 return m_data[index]; | 342 return m_data[index].get(); |
350 } | 343 } |
351 | 344 |
352 } // namespace protocol | 345 } // namespace protocol |
353 } // namespace blink | 346 } // namespace blink |
OLD | NEW |