summaryrefslogtreecommitdiff
path: root/tools/inspector_protocol/lib/base_string_adapter_cc.template
blob: ed3316446f4a513deb3a41fef94c44f0b3d793f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// This file is generated by DispatcherBase_cpp.template.

// Copyright 2019 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 {{format_include(config.protocol.package, "base_string_adapter")}}
#include {{format_include(config.protocol.package, "Protocol")}}

#include <utility>
#include "base/base64.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string16.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"

{% for namespace in config.protocol.namespace %}
namespace {{namespace}} {
{% endfor %}

std::unique_ptr<protocol::Value> toProtocolValue(
    const base::Value* value, int depth) {
  if (!value || !depth)
    return nullptr;
  if (value->is_none())
    return protocol::Value::null();
  if (value->is_bool()) {
    bool inner;
    value->GetAsBoolean(&inner);
    return protocol::FundamentalValue::create(inner);
  }
  if (value->is_int()) {
    int inner;
    value->GetAsInteger(&inner);
    return protocol::FundamentalValue::create(inner);
  }
  if (value->is_double()) {
    double inner;
    value->GetAsDouble(&inner);
    return protocol::FundamentalValue::create(inner);
  }
  if (value->is_string()) {
    std::string inner;
    value->GetAsString(&inner);
    return protocol::StringValue::create(inner);
  }
  if (value->is_list()) {
    const base::ListValue* list = nullptr;
    value->GetAsList(&list);
    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);
      std::unique_ptr<protocol::Value> converted =
          toProtocolValue(item, depth - 1);
      if (converted)
        result->pushValue(std::move(converted));
    }
    return std::move(result);
  }
  if (value->is_dict()) {
    const base::DictionaryValue* dictionary = nullptr;
    value->GetAsDictionary(&dictionary);
    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 std::move(result);
  }
  return nullptr;
}

std::unique_ptr<base::Value> toBaseValue(Value* value, int depth) {
  if (!value || !depth)
    return nullptr;
  if (value->type() == Value::TypeNull)
    return std::make_unique<base::Value>();
  if (value->type() == Value::TypeBoolean) {
    bool inner;
    value->asBoolean(&inner);
    return base::WrapUnique(new base::Value(inner));
  }
  if (value->type() == Value::TypeInteger) {
    int inner;
    value->asInteger(&inner);
    return base::WrapUnique(new base::Value(inner));
  }
  if (value->type() == Value::TypeDouble) {
    double inner;
    value->asDouble(&inner);
    return base::WrapUnique(new base::Value(inner));
  }
  if (value->type() == Value::TypeString) {
    std::string inner;
    value->asString(&inner);
    return base::WrapUnique(new base::Value(inner));
  }
  if (value->type() == Value::TypeArray) {
    ListValue* list = ListValue::cast(value);
    std::unique_ptr<base::ListValue> result(new base::ListValue());
    for (size_t i = 0; i < list->size(); i++) {
      std::unique_ptr<base::Value> converted =
          toBaseValue(list->at(i), depth - 1);
      if (converted)
        result->Append(std::move(converted));
    }
    return std::move(result);
  }
  if (value->type() == Value::TypeObject) {
    DictionaryValue* dict = DictionaryValue::cast(value);
    std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
    for (size_t i = 0; i < dict->size(); i++) {
      DictionaryValue::Entry entry = dict->at(i);
      std::unique_ptr<base::Value> converted =
          toBaseValue(entry.second, depth - 1);
      if (converted)
        result->SetWithoutPathExpansion(entry.first, std::move(converted));
    }
    return std::move(result);
  }
  return nullptr;
}

// static
std::unique_ptr<Value> StringUtil::parseMessage(
    const std::string& message, bool binary) {
  if (binary) {
    return Value::parseBinary(
        reinterpret_cast<const uint8_t*>(message.data()),
        message.length());
  }
  std::unique_ptr<base::Value> value = base::JSONReader::Read(message);
  return toProtocolValue(value.get(), 1000);
}

// static
ProtocolMessage StringUtil::jsonToMessage(String message) {
  return message;
}

// static
ProtocolMessage StringUtil::binaryToMessage(std::vector<uint8_t> message) {
  // TODO(pfeldman): figure out what to do with this copy.
  return std::string(reinterpret_cast<const char*>(message.data()), message.size());
}

StringBuilder::StringBuilder() {}

StringBuilder::~StringBuilder() {}

void StringBuilder::append(const std::string& s) {
  string_ += s;
}

void StringBuilder::append(char c) {
  string_ += c;
}

void StringBuilder::append(const char* characters, size_t length) {
  string_.append(characters, length);
}

// static
void StringUtil::builderAppendQuotedString(StringBuilder& builder,
                                           const String& str) {
  builder.append('"');
  base::string16 str16 = base::UTF8ToUTF16(str);
  escapeWideStringForJSON(reinterpret_cast<const uint16_t*>(&str16[0]),
                          str16.length(), &builder);
  builder.append('"');
}

std::string StringBuilder::toString() {
  return string_;
}

void StringBuilder::reserveCapacity(size_t capacity) {
  string_.reserve(capacity);
}

Binary::Binary() : bytes_(new base::RefCountedBytes) {}
Binary::Binary(const Binary& binary) : bytes_(binary.bytes_) {}
Binary::Binary(scoped_refptr<base::RefCountedMemory> bytes) : bytes_(bytes) {}
Binary::~Binary() {}

String Binary::toBase64() const {
  std::string encoded;
  base::Base64Encode(
      base::StringPiece(reinterpret_cast<const char*>(bytes_->front()),
                        bytes_->size()),
      &encoded);
  return encoded;
}

// static
Binary Binary::fromBase64(const String& base64, bool* success) {
  std::string decoded;
  *success = base::Base64Decode(base::StringPiece(base64), &decoded);
  if (*success) {
    return Binary::fromString(std::move(decoded));
  }
  return Binary();
}

// static
Binary Binary::fromRefCounted(scoped_refptr<base::RefCountedMemory> memory) {
  return Binary(memory);
}

// static
Binary Binary::fromVector(std::vector<uint8_t> data) {
  return Binary(base::RefCountedBytes::TakeVector(&data));
}

// static
Binary Binary::fromString(std::string data) {
  return Binary(base::RefCountedString::TakeString(&data));
}

// static
Binary Binary::fromSpan(const uint8_t* data, size_t size) {
  return Binary(scoped_refptr<base::RefCountedBytes>(
      new base::RefCountedBytes(data, size)));
}

namespace {
int32_t ReadEnvelopeSize(const uint8_t* in) {
  return (in[0] << 24) + (in[1] << 16) + (in[2] << 8) + in[3];
}

void WriteEnvelopeSize(uint32_t value, uint8_t* out) {
  *(out++) = (value >> 24) & 0xFF;
  *(out++) = (value >> 16) & 0xFF;
  *(out++) = (value >> 8) & 0xFF;
  *(out++) = (value) & 0xFF;
}

}

bool AppendStringValueToMapBinary(base::StringPiece in,
    base::StringPiece key, base::StringPiece value, std::string* out) {
  if (in.size() < 1 + 1 + 4 + 1 + 1)
    return false;
  const uint8_t* envelope = reinterpret_cast<const uint8_t*>(in.data());
  if (cbor::kInitialByteForEnvelope != envelope[0])
    return false;
  if (cbor::kInitialByteFor32BitLengthByteString != envelope[1])
    return false;
  if (cbor::kInitialByteIndefiniteLengthMap != envelope[6])
    return false;

  uint32_t envelope_size = ReadEnvelopeSize(envelope + 2);
  if (envelope_size + 2 + 4 != in.size())
    return false;
  if (cbor::kStopByte != static_cast<uint8_t>(*in.rbegin()))
    return false;

  std::vector<uint8_t> encoded_entry;
  encoded_entry.reserve(1 + 4 + key.size() + 1 + 4 + value.size());
  span<uint8_t> key_span(
      reinterpret_cast<const uint8_t*>(key.data()), key.size());
  EncodeString8(key_span, &encoded_entry);
  span<uint8_t> value_span(
      reinterpret_cast<const uint8_t*>(value.data()), value.size());
  EncodeString8(value_span, &encoded_entry);

  out->clear();
  out->reserve(in.size() + encoded_entry.size());
  out->append(in.begin(), in.end() - 1);
  out->append(reinterpret_cast<const char*>(encoded_entry.data()),
      encoded_entry.size());
  out->append(1, static_cast<char>(cbor::kStopByte));
  std::size_t new_size = envelope_size + out->size() - in.size();
  if (new_size > static_cast<std::size_t>(
      std::numeric_limits<uint32_t>::max())) {
    return false;
  }
  WriteEnvelopeSize(new_size, reinterpret_cast<uint8_t*>(&*out->begin() + 2));
  return true;
}

bool AppendStringValueToMapJSON(base::StringPiece in,
    base::StringPiece key, base::StringPiece value, std::string* out) {
  if (!in.length() || *in.rbegin() != '}')
    return false;
  std::string suffix =
      base::StringPrintf(", \"%s\": \"%s\"}", key.begin(), value.begin());
  out->clear();
  out->reserve(in.length() + suffix.length() - 1);
  out->append(in.data(), in.length() - 1);
  out->append(suffix);
  return true;
}

{% for namespace in config.protocol.namespace %}
} // namespace {{namespace}}
{% endfor %}