• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This file is generated by DispatcherBase_cpp.template.
2
3// Copyright 2019 The Chromium Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6
7#include {{format_include(config.protocol.package, "base_string_adapter")}}
8#include {{format_include(config.protocol.package, "Protocol")}}
9
10#include <utility>
11#include "base/base64.h"
12#include "base/json/json_reader.h"
13#include "base/memory/ptr_util.h"
14#include "base/strings/string16.h"
15#include "base/strings/stringprintf.h"
16#include "base/strings/utf_string_conversions.h"
17#include "base/values.h"
18
19{% for namespace in config.protocol.namespace %}
20namespace {{namespace}} {
21{% endfor %}
22
23std::unique_ptr<protocol::Value> toProtocolValue(
24    const base::Value* value, int depth) {
25  if (!value || !depth)
26    return nullptr;
27  if (value->is_none())
28    return protocol::Value::null();
29  if (value->is_bool()) {
30    bool inner;
31    value->GetAsBoolean(&inner);
32    return protocol::FundamentalValue::create(inner);
33  }
34  if (value->is_int()) {
35    int inner;
36    value->GetAsInteger(&inner);
37    return protocol::FundamentalValue::create(inner);
38  }
39  if (value->is_double()) {
40    double inner;
41    value->GetAsDouble(&inner);
42    return protocol::FundamentalValue::create(inner);
43  }
44  if (value->is_string()) {
45    std::string inner;
46    value->GetAsString(&inner);
47    return protocol::StringValue::create(inner);
48  }
49  if (value->is_list()) {
50    const base::ListValue* list = nullptr;
51    value->GetAsList(&list);
52    std::unique_ptr<protocol::ListValue> result = protocol::ListValue::create();
53    for (size_t i = 0; i < list->GetSize(); i++) {
54      const base::Value* item = nullptr;
55      list->Get(i, &item);
56      std::unique_ptr<protocol::Value> converted =
57          toProtocolValue(item, depth - 1);
58      if (converted)
59        result->pushValue(std::move(converted));
60    }
61    return std::move(result);
62  }
63  if (value->is_dict()) {
64    const base::DictionaryValue* dictionary = nullptr;
65    value->GetAsDictionary(&dictionary);
66    std::unique_ptr<protocol::DictionaryValue> result =
67        protocol::DictionaryValue::create();
68    for (base::DictionaryValue::Iterator it(*dictionary);
69        !it.IsAtEnd(); it.Advance()) {
70      std::unique_ptr<protocol::Value> converted =
71          toProtocolValue(&it.value(), depth - 1);
72      if (converted)
73        result->setValue(it.key(), std::move(converted));
74    }
75    return std::move(result);
76  }
77  return nullptr;
78}
79
80std::unique_ptr<base::Value> toBaseValue(Value* value, int depth) {
81  if (!value || !depth)
82    return nullptr;
83  if (value->type() == Value::TypeNull)
84    return std::make_unique<base::Value>();
85  if (value->type() == Value::TypeBoolean) {
86    bool inner;
87    value->asBoolean(&inner);
88    return base::WrapUnique(new base::Value(inner));
89  }
90  if (value->type() == Value::TypeInteger) {
91    int inner;
92    value->asInteger(&inner);
93    return base::WrapUnique(new base::Value(inner));
94  }
95  if (value->type() == Value::TypeDouble) {
96    double inner;
97    value->asDouble(&inner);
98    return base::WrapUnique(new base::Value(inner));
99  }
100  if (value->type() == Value::TypeString) {
101    std::string inner;
102    value->asString(&inner);
103    return base::WrapUnique(new base::Value(inner));
104  }
105  if (value->type() == Value::TypeArray) {
106    ListValue* list = ListValue::cast(value);
107    std::unique_ptr<base::ListValue> result(new base::ListValue());
108    for (size_t i = 0; i < list->size(); i++) {
109      std::unique_ptr<base::Value> converted =
110          toBaseValue(list->at(i), depth - 1);
111      if (converted)
112        result->Append(std::move(converted));
113    }
114    return std::move(result);
115  }
116  if (value->type() == Value::TypeObject) {
117    DictionaryValue* dict = DictionaryValue::cast(value);
118    std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
119    for (size_t i = 0; i < dict->size(); i++) {
120      DictionaryValue::Entry entry = dict->at(i);
121      std::unique_ptr<base::Value> converted =
122          toBaseValue(entry.second, depth - 1);
123      if (converted)
124        result->SetWithoutPathExpansion(entry.first, std::move(converted));
125    }
126    return std::move(result);
127  }
128  return nullptr;
129}
130
131// static
132std::unique_ptr<Value> StringUtil::parseMessage(
133    const std::string& message, bool binary) {
134  if (binary) {
135    return Value::parseBinary(
136        reinterpret_cast<const uint8_t*>(message.data()),
137        message.length());
138  }
139  std::unique_ptr<base::Value> value = base::JSONReader::ReadDeprecated(message);
140  return toProtocolValue(value.get(), 1000);
141}
142
143// static
144ProtocolMessage StringUtil::jsonToMessage(String message) {
145  return message;
146}
147
148// static
149ProtocolMessage StringUtil::binaryToMessage(std::vector<uint8_t> message) {
150  // TODO(pfeldman): figure out what to do with this copy.
151  return std::string(reinterpret_cast<const char*>(message.data()), message.size());
152}
153
154StringBuilder::StringBuilder() {}
155
156StringBuilder::~StringBuilder() {}
157
158void StringBuilder::append(const std::string& s) {
159  string_ += s;
160}
161
162void StringBuilder::append(char c) {
163  string_ += c;
164}
165
166void StringBuilder::append(const char* characters, size_t length) {
167  string_.append(characters, length);
168}
169
170// static
171void StringUtil::builderAppendQuotedString(StringBuilder& builder,
172                                           const String& str) {
173  builder.append('"');
174  base::string16 str16 = base::UTF8ToUTF16(str);
175  escapeWideStringForJSON(reinterpret_cast<const uint16_t*>(&str16[0]),
176                          str16.length(), &builder);
177  builder.append('"');
178}
179
180std::string StringBuilder::toString() {
181  return string_;
182}
183
184void StringBuilder::reserveCapacity(size_t capacity) {
185  string_.reserve(capacity);
186}
187
188// static
189String StringUtil::fromUTF16(const uint16_t* data, size_t length) {
190  std::string utf8;
191  base::UTF16ToUTF8(reinterpret_cast<const base::char16*>(data), length, &utf8);
192  return utf8;
193}
194
195Binary::Binary() : bytes_(new base::RefCountedBytes) {}
196Binary::Binary(const Binary& binary) : bytes_(binary.bytes_) {}
197Binary::Binary(scoped_refptr<base::RefCountedMemory> bytes) : bytes_(bytes) {}
198Binary::~Binary() {}
199
200String Binary::toBase64() const {
201  std::string encoded;
202  base::Base64Encode(
203      base::StringPiece(reinterpret_cast<const char*>(bytes_->front()),
204                        bytes_->size()),
205      &encoded);
206  return encoded;
207}
208
209// static
210Binary Binary::fromBase64(const String& base64, bool* success) {
211  std::string decoded;
212  *success = base::Base64Decode(base::StringPiece(base64), &decoded);
213  if (*success) {
214    return Binary::fromString(std::move(decoded));
215  }
216  return Binary();
217}
218
219// static
220Binary Binary::fromRefCounted(scoped_refptr<base::RefCountedMemory> memory) {
221  return Binary(memory);
222}
223
224// static
225Binary Binary::fromVector(std::vector<uint8_t> data) {
226  return Binary(base::RefCountedBytes::TakeVector(&data));
227}
228
229// static
230Binary Binary::fromString(std::string data) {
231  return Binary(base::RefCountedString::TakeString(&data));
232}
233
234// static
235Binary Binary::fromSpan(const uint8_t* data, size_t size) {
236  return Binary(scoped_refptr<base::RefCountedBytes>(
237      new base::RefCountedBytes(data, size)));
238}
239
240{% for namespace in config.protocol.namespace %}
241} // namespace {{namespace}}
242{% endfor %}
243