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 "content/browser/devtools/protocol_string.h" | 5 #include "content/browser/devtools/protocol_string.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "content/browser/devtools/protocol/protocol.h" | 10 #include "content/browser/devtools/protocol/protocol.h" |
10 | 11 |
11 namespace content { | 12 namespace content { |
12 namespace { | 13 namespace protocol { |
13 | 14 |
14 std::unique_ptr<protocol::Value> toProtocolValue( | 15 std::unique_ptr<protocol::Value> toProtocolValue( |
15 const base::Value* value, int depth) { | 16 const base::Value* value, int depth) { |
16 if (!value || !depth) | 17 if (!value || !depth) |
17 return nullptr; | 18 return nullptr; |
18 if (value->IsType(base::Value::TYPE_NULL)) | 19 if (value->IsType(base::Value::TYPE_NULL)) |
19 return protocol::Value::null(); | 20 return protocol::Value::null(); |
20 if (value->IsType(base::Value::TYPE_BOOLEAN)) { | 21 if (value->IsType(base::Value::TYPE_BOOLEAN)) { |
21 bool inner; | 22 bool inner; |
22 value->GetAsBoolean(&inner); | 23 value->GetAsBoolean(&inner); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 std::unique_ptr<protocol::Value> converted = | 62 std::unique_ptr<protocol::Value> converted = |
62 toProtocolValue(&it.value(), depth - 1); | 63 toProtocolValue(&it.value(), depth - 1); |
63 if (converted) | 64 if (converted) |
64 result->setValue(it.key(), std::move(converted)); | 65 result->setValue(it.key(), std::move(converted)); |
65 } | 66 } |
66 return std::move(result); | 67 return std::move(result); |
67 } | 68 } |
68 return nullptr; | 69 return nullptr; |
69 } | 70 } |
70 | 71 |
71 } // namespace | 72 std::unique_ptr<base::Value> toBaseValue( |
72 | 73 protocol::Value* value, int depth) { |
73 namespace protocol { | 74 if (!value || !depth) |
| 75 return nullptr; |
| 76 if (value->type() == protocol::Value::TypeNull) |
| 77 return base::Value::CreateNullValue(); |
| 78 if (value->type() == protocol::Value::TypeBoolean) { |
| 79 bool inner; |
| 80 value->asBoolean(&inner); |
| 81 return base::WrapUnique(new base::FundamentalValue(inner)); |
| 82 } |
| 83 if (value->type() == protocol::Value::TypeInteger) { |
| 84 int inner; |
| 85 value->asInteger(&inner); |
| 86 return base::WrapUnique(new base::FundamentalValue(inner)); |
| 87 } |
| 88 if (value->type() == protocol::Value::TypeDouble) { |
| 89 double inner; |
| 90 value->asDouble(&inner); |
| 91 return base::WrapUnique(new base::FundamentalValue(inner)); |
| 92 } |
| 93 if (value->type() == protocol::Value::TypeString) { |
| 94 std::string inner; |
| 95 value->asString(&inner); |
| 96 return base::WrapUnique(new base::StringValue(inner)); |
| 97 } |
| 98 if (value->type() == protocol::Value::TypeArray) { |
| 99 protocol::ListValue* list = protocol::ListValue::cast(value); |
| 100 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 101 for (size_t i = 0; i < list->size(); i++) { |
| 102 std::unique_ptr<base::Value> converted = |
| 103 toBaseValue(list->at(i), depth - 1); |
| 104 if (converted) |
| 105 result->Append(std::move(converted)); |
| 106 } |
| 107 return std::move(result); |
| 108 } |
| 109 if (value->type() == protocol::Value::TypeObject) { |
| 110 protocol::DictionaryValue* dict = protocol::DictionaryValue::cast(value); |
| 111 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 112 for (size_t i = 0; i < dict->size(); i++) { |
| 113 protocol::DictionaryValue::Entry entry = dict->at(i); |
| 114 std::unique_ptr<base::Value> converted = |
| 115 toBaseValue(entry.second, depth - 1); |
| 116 if (converted) |
| 117 result->SetWithoutPathExpansion(entry.first, std::move(converted)); |
| 118 } |
| 119 return std::move(result); |
| 120 } |
| 121 return nullptr; |
| 122 } |
74 | 123 |
75 // static | 124 // static |
76 std::unique_ptr<protocol::Value> StringUtil::parseJSON( | 125 std::unique_ptr<protocol::Value> StringUtil::parseJSON( |
77 const std::string& json) { | 126 const std::string& json) { |
78 std::unique_ptr<base::Value> value = base::JSONReader::Read(json); | 127 std::unique_ptr<base::Value> value = base::JSONReader::Read(json); |
79 return toProtocolValue(value.get(), 1000); | 128 return toProtocolValue(value.get(), 1000); |
80 } | 129 } |
81 | 130 |
82 StringBuilder::StringBuilder() {} | 131 StringBuilder::StringBuilder() {} |
83 | 132 |
(...skipping 14 matching lines...) Expand all Loading... |
98 std::string StringBuilder::toString() { | 147 std::string StringBuilder::toString() { |
99 return string_; | 148 return string_; |
100 } | 149 } |
101 | 150 |
102 void StringBuilder::reserveCapacity(size_t capacity) { | 151 void StringBuilder::reserveCapacity(size_t capacity) { |
103 string_.reserve(capacity); | 152 string_.reserve(capacity); |
104 } | 153 } |
105 | 154 |
106 } // namespace protocol | 155 } // namespace protocol |
107 } // namespace content | 156 } // namespace content |
OLD | NEW |