Chromium Code Reviews| 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 DCHECK(list); | |
|
caseq
2016/11/12 02:17:04
drop it?
dgozman
2016/11/14 18:40:28
Done.
| |
| 44 std::unique_ptr<protocol::ListValue> result = protocol::ListValue::create(); | |
| 45 for (size_t i = 0; i < list->GetSize(); i++) { | |
| 46 const base::Value* item = nullptr; | |
| 47 list->Get(i, &item); | |
| 48 DCHECK(item); | |
|
caseq
2016/11/12 02:17:04
ditto.
dgozman
2016/11/14 18:40:28
Done.
| |
| 49 std::unique_ptr<protocol::Value> converted = | |
| 50 toProtocolValue(item, depth - 1); | |
| 51 if (converted) | |
| 52 result->pushValue(std::move(converted)); | |
| 53 } | |
| 54 return result; | |
| 55 } | |
| 56 if (value->IsType(base::Value::TYPE_DICTIONARY)) { | |
| 57 const base::DictionaryValue* dictionary = nullptr; | |
| 58 value->GetAsDictionary(&dictionary); | |
| 59 DCHECK(dictionary); | |
|
caseq
2016/11/12 02:17:04
ditto.
dgozman
2016/11/14 18:40:28
Done.
| |
| 60 std::unique_ptr<protocol::DictionaryValue> result = | |
| 61 protocol::DictionaryValue::create(); | |
| 62 for (base::DictionaryValue::Iterator it(*dictionary); | |
| 63 !it.IsAtEnd(); it.Advance()) { | |
| 64 std::unique_ptr<protocol::Value> converted = | |
| 65 toProtocolValue(&it.value(), depth - 1); | |
| 66 if (converted) | |
| 67 result->setValue(it.key(), std::move(converted)); | |
| 68 } | |
| 69 return result; | |
| 70 } | |
| 71 return nullptr; | |
| 72 } | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 namespace protocol { | |
| 77 | |
| 78 // static | |
| 79 std::unique_ptr<protocol::Value> StringUtil::parseJSON( | |
| 80 const std::string& json) { | |
| 81 std::unique_ptr<base::Value> value = base::JSONReader::Read(json); | |
| 82 return toProtocolValue(value.get(), 1000); | |
| 83 } | |
| 84 | |
| 85 } // namespace protocol | |
| 86 | |
| 87 ProtocolStringBuilder::ProtocolStringBuilder() {} | |
| 88 | |
| 89 ProtocolStringBuilder::~ProtocolStringBuilder() {} | |
| 90 | |
| 91 void ProtocolStringBuilder::append(const std::string& s) { | |
| 92 buffer_.insert(buffer_.end(), s.begin(), s.end()); | |
| 93 } | |
| 94 | |
| 95 void ProtocolStringBuilder::append(char c) { | |
| 96 buffer_.push_back(c); | |
| 97 } | |
| 98 | |
| 99 void ProtocolStringBuilder::append(const char* characters, size_t length) { | |
| 100 buffer_.insert(buffer_.end(), characters, characters + length); | |
| 101 } | |
| 102 | |
| 103 std::string ProtocolStringBuilder::toString() { | |
| 104 return std::string(buffer_.data(), buffer_.size()); | |
| 105 } | |
| 106 | |
| 107 void ProtocolStringBuilder::reserveCapacity(size_t capacity) { | |
| 108 buffer_.reserve(capacity); | |
| 109 } | |
| 110 | |
| 111 } // namespace content | |
| OLD | NEW |