• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This file is generated by Parser_h.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#ifndef {{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H
8#define {{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H
9
10#include <memory>
11#include <string>
12#include <vector>
13
14#include "base/logging.h"
15#include "base/macros.h"
16#include "base/memory/ref_counted_memory.h"
17#include "base/strings/string_number_conversions.h"
18{% if config.lib.export_header %}
19#include "{{config.lib.export_header}}"
20{% endif %}
21
22namespace base {
23class Value;
24}
25
26{% for namespace in config.protocol.namespace %}
27namespace {{namespace}} {
28{% endfor %}
29
30class Value;
31
32using String = std::string;
33using ProtocolMessage = std::string;
34
35class {{config.lib.export_macro}} StringBuilder {
36 public:
37  StringBuilder();
38  ~StringBuilder();
39  void append(const String&);
40  void append(char);
41  void append(const char*, size_t);
42  String toString();
43  void reserveCapacity(size_t);
44
45 private:
46  std::string string_;
47};
48
49class {{config.lib.export_macro}} StringUtil {
50 public:
51  static String substring(const String& s, unsigned pos, unsigned len) {
52    return s.substr(pos, len);
53  }
54  static String fromInteger(int number) { return base::NumberToString(number); }
55  static String fromDouble(double number) {
56    String s = base::NumberToString(number);
57    if (!s.empty()) {  // .123 -> 0.123; -.123 -> -0.123 for valid JSON.
58      if (s[0] == '.')
59        s.insert(/*index=*/ 0, /*count=*/ 1, /*ch=*/ '0');
60      else if (s[0] == '-' && s.size() >= 2 && s[1] == '.')
61        s.insert(/*index=*/ 1, /*count=*/ 1, /*ch=*/ '0');
62    }
63    return s;
64  }
65  static double toDouble(const char* s, size_t len, bool* ok) {
66    double v = 0.0;
67    *ok = base::StringToDouble(std::string(s, len), &v);
68    return *ok ? v : 0.0;
69  }
70  static size_t find(const String& s, const char* needle) {
71    return s.find(needle);
72  }
73  static size_t find(const String& s, const String& needle) {
74    return s.find(needle);
75  }
76  static const size_t kNotFound = static_cast<size_t>(-1);
77  static void builderAppend(StringBuilder& builder, const String& s) {
78    builder.append(s);
79  }
80  static void builderAppend(StringBuilder& builder, char c) {
81    builder.append(c);
82  }
83  static void builderAppend(StringBuilder& builder, const char* s, size_t len) {
84    builder.append(s, len);
85  }
86  static void builderAppendQuotedString(StringBuilder& builder,
87                                        const String& str);
88  static void builderReserve(StringBuilder& builder, unsigned capacity) {
89    builder.reserveCapacity(capacity);
90  }
91  static String builderToString(StringBuilder& builder) {
92    return builder.toString();
93  }
94
95  static std::unique_ptr<Value> parseMessage(const std::string& message, bool binary);
96  static ProtocolMessage jsonToMessage(String message);
97  static ProtocolMessage binaryToMessage(std::vector<uint8_t> message);
98
99  static String fromUTF8(const uint8_t* data, size_t length) {
100    return std::string(reinterpret_cast<const char*>(data), length);
101  }
102
103  static String fromUTF16(const uint16_t* data, size_t length);
104
105  static const uint8_t* CharactersLatin1(const String& s) { return nullptr; }
106  static const uint8_t* CharactersUTF8(const String& s) {
107    return reinterpret_cast<const uint8_t*>(s.data());
108  }
109  static const uint16_t* CharactersUTF16(const String& s) { return nullptr; }
110  static size_t CharacterCount(const String& s) { return s.size(); }
111};
112
113// A read-only sequence of uninterpreted bytes with reference-counted storage.
114class {{config.lib.export_macro}} Binary {
115 public:
116  Binary(const Binary&);
117  Binary();
118  ~Binary();
119
120  const uint8_t* data() const { return bytes_->front(); }
121  size_t size() const { return bytes_->size(); }
122  scoped_refptr<base::RefCountedMemory> bytes() const { return bytes_; }
123
124  String toBase64() const;
125
126  static Binary fromBase64(const String& base64, bool* success);
127  static Binary fromRefCounted(scoped_refptr<base::RefCountedMemory> memory);
128  static Binary fromVector(std::vector<uint8_t> data);
129  static Binary fromString(std::string data);
130  static Binary fromSpan(const uint8_t* data, size_t size);
131
132 private:
133  explicit Binary(scoped_refptr<base::RefCountedMemory> bytes);
134  scoped_refptr<base::RefCountedMemory> bytes_;
135};
136
137std::unique_ptr<Value> toProtocolValue(const base::Value* value, int depth);
138std::unique_ptr<base::Value> toBaseValue(Value* value, int depth);
139{% for namespace in config.protocol.namespace %}
140} // namespace {{namespace}}
141{% endfor %}
142
143#endif // !defined({{"_".join(config.protocol.namespace)}}_BASE_STRING_ADAPTER_H)
144