Chromium Code Reviews| Index: content/browser/devtools/protocol_string.cc |
| diff --git a/content/browser/devtools/protocol_string.cc b/content/browser/devtools/protocol_string.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c566cd01df9fca9cfe559592879094b8fc2976a2 |
| --- /dev/null |
| +++ b/content/browser/devtools/protocol_string.cc |
| @@ -0,0 +1,111 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/devtools/protocol_string.h" |
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/values.h" |
| +#include "content/browser/devtools/protocol/protocol.h" |
| + |
| +namespace content { |
| +namespace { |
| + |
| +std::unique_ptr<protocol::Value> toProtocolValue( |
| + const base::Value* value, int depth) { |
| + if (!value || !depth) |
| + return nullptr; |
| + if (value->IsType(base::Value::TYPE_NULL)) |
| + return protocol::Value::null(); |
| + if (value->IsType(base::Value::TYPE_BOOLEAN)) { |
| + bool inner; |
| + value->GetAsBoolean(&inner); |
| + return protocol::FundamentalValue::create(inner); |
| + } |
| + if (value->IsType(base::Value::TYPE_INTEGER)) { |
| + int inner; |
| + value->GetAsInteger(&inner); |
| + return protocol::FundamentalValue::create(inner); |
| + } |
| + if (value->IsType(base::Value::TYPE_DOUBLE)) { |
| + double inner; |
| + value->GetAsDouble(&inner); |
| + return protocol::FundamentalValue::create(inner); |
| + } |
| + if (value->IsType(base::Value::TYPE_STRING)) { |
| + std::string inner; |
| + value->GetAsString(&inner); |
| + return protocol::StringValue::create(inner); |
| + } |
| + if (value->IsType(base::Value::TYPE_LIST)) { |
| + const base::ListValue* list = nullptr; |
| + value->GetAsList(&list); |
| + DCHECK(list); |
|
caseq
2016/11/12 02:17:04
drop it?
dgozman
2016/11/14 18:40:28
Done.
|
| + std::unique_ptr<protocol::ListValue> result = protocol::ListValue::create(); |
| + for (size_t i = 0; i < list->GetSize(); i++) { |
| + const base::Value* item = nullptr; |
| + list->Get(i, &item); |
| + DCHECK(item); |
|
caseq
2016/11/12 02:17:04
ditto.
dgozman
2016/11/14 18:40:28
Done.
|
| + std::unique_ptr<protocol::Value> converted = |
| + toProtocolValue(item, depth - 1); |
| + if (converted) |
| + result->pushValue(std::move(converted)); |
| + } |
| + return result; |
| + } |
| + if (value->IsType(base::Value::TYPE_DICTIONARY)) { |
| + const base::DictionaryValue* dictionary = nullptr; |
| + value->GetAsDictionary(&dictionary); |
| + DCHECK(dictionary); |
|
caseq
2016/11/12 02:17:04
ditto.
dgozman
2016/11/14 18:40:28
Done.
|
| + std::unique_ptr<protocol::DictionaryValue> result = |
| + protocol::DictionaryValue::create(); |
| + for (base::DictionaryValue::Iterator it(*dictionary); |
| + !it.IsAtEnd(); it.Advance()) { |
| + std::unique_ptr<protocol::Value> converted = |
| + toProtocolValue(&it.value(), depth - 1); |
| + if (converted) |
| + result->setValue(it.key(), std::move(converted)); |
| + } |
| + return result; |
| + } |
| + return nullptr; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace protocol { |
| + |
| +// static |
| +std::unique_ptr<protocol::Value> StringUtil::parseJSON( |
| + const std::string& json) { |
| + std::unique_ptr<base::Value> value = base::JSONReader::Read(json); |
| + return toProtocolValue(value.get(), 1000); |
| +} |
| + |
| +} // namespace protocol |
| + |
| +ProtocolStringBuilder::ProtocolStringBuilder() {} |
| + |
| +ProtocolStringBuilder::~ProtocolStringBuilder() {} |
| + |
| +void ProtocolStringBuilder::append(const std::string& s) { |
| + buffer_.insert(buffer_.end(), s.begin(), s.end()); |
| +} |
| + |
| +void ProtocolStringBuilder::append(char c) { |
| + buffer_.push_back(c); |
| +} |
| + |
| +void ProtocolStringBuilder::append(const char* characters, size_t length) { |
| + buffer_.insert(buffer_.end(), characters, characters + length); |
| +} |
| + |
| +std::string ProtocolStringBuilder::toString() { |
| + return std::string(buffer_.data(), buffer_.size()); |
| +} |
| + |
| +void ProtocolStringBuilder::reserveCapacity(size_t capacity) { |
| + buffer_.reserve(capacity); |
| +} |
| + |
| +} // namespace content |