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 "content/browser/devtools/protocol_string.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/values.h" |
| 9 #include "content/browser/devtools/protocol/protocol.h" |
| 10 |
| 11 namespace content { |
| 12 namespace { |
| 13 |
| 14 std::unique_ptr<protocol::Value> toProtocolValue( |
| 15 const base::Value* value, int depth) { |
| 16 if (!value || !depth) |
| 17 return nullptr; |
| 18 if (value->IsType(base::Value::TYPE_NULL)) |
| 19 return protocol::Value::null(); |
| 20 if (value->IsType(base::Value::TYPE_BOOLEAN)) { |
| 21 bool inner; |
| 22 value->GetAsBoolean(&inner); |
| 23 return protocol::FundamentalValue::create(inner); |
| 24 } |
| 25 if (value->IsType(base::Value::TYPE_INTEGER)) { |
| 26 int inner; |
| 27 value->GetAsInteger(&inner); |
| 28 return protocol::FundamentalValue::create(inner); |
| 29 } |
| 30 if (value->IsType(base::Value::TYPE_DOUBLE)) { |
| 31 double inner; |
| 32 value->GetAsDouble(&inner); |
| 33 return protocol::FundamentalValue::create(inner); |
| 34 } |
| 35 if (value->IsType(base::Value::TYPE_STRING)) { |
| 36 std::string inner; |
| 37 value->GetAsString(&inner); |
| 38 return protocol::StringValue::create(inner); |
| 39 } |
| 40 if (value->IsType(base::Value::TYPE_LIST)) { |
| 41 const base::ListValue* list = nullptr; |
| 42 value->GetAsList(&list); |
| 43 std::unique_ptr<protocol::ListValue> result = protocol::ListValue::create(); |
| 44 for (size_t i = 0; i < list->GetSize(); i++) { |
| 45 const base::Value* item = nullptr; |
| 46 list->Get(i, &item); |
| 47 std::unique_ptr<protocol::Value> converted = |
| 48 toProtocolValue(item, depth - 1); |
| 49 if (converted) |
| 50 result->pushValue(std::move(converted)); |
| 51 } |
| 52 return std::move(result); |
| 53 } |
| 54 if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 55 const base::DictionaryValue* dictionary = nullptr; |
| 56 value->GetAsDictionary(&dictionary); |
| 57 std::unique_ptr<protocol::DictionaryValue> result = |
| 58 protocol::DictionaryValue::create(); |
| 59 for (base::DictionaryValue::Iterator it(*dictionary); |
| 60 !it.IsAtEnd(); it.Advance()) { |
| 61 std::unique_ptr<protocol::Value> converted = |
| 62 toProtocolValue(&it.value(), depth - 1); |
| 63 if (converted) |
| 64 result->setValue(it.key(), std::move(converted)); |
| 65 } |
| 66 return std::move(result); |
| 67 } |
| 68 return nullptr; |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 namespace protocol { |
| 74 |
| 75 // static |
| 76 std::unique_ptr<protocol::Value> StringUtil::parseJSON( |
| 77 const std::string& json) { |
| 78 std::unique_ptr<base::Value> value = base::JSONReader::Read(json); |
| 79 return toProtocolValue(value.get(), 1000); |
| 80 } |
| 81 |
| 82 StringBuilder::StringBuilder() {} |
| 83 |
| 84 StringBuilder::~StringBuilder() {} |
| 85 |
| 86 void StringBuilder::append(const std::string& s) { |
| 87 string_ += s; |
| 88 } |
| 89 |
| 90 void StringBuilder::append(char c) { |
| 91 string_ += c; |
| 92 } |
| 93 |
| 94 void StringBuilder::append(const char* characters, size_t length) { |
| 95 string_.append(characters, length); |
| 96 } |
| 97 |
| 98 std::string StringBuilder::toString() { |
| 99 return string_; |
| 100 } |
| 101 |
| 102 void StringBuilder::reserveCapacity(size_t capacity) { |
| 103 string_.reserve(capacity); |
| 104 } |
| 105 |
| 106 } // namespace protocol |
| 107 } // namespace content |
OLD | NEW |