Line data Source code
1 : // This file is generated by ErrorSupport_h.template.
2 :
3 : // Copyright 2016 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 v8_inspector_protocol_ErrorSupport_h
8 : #define v8_inspector_protocol_ErrorSupport_h
9 :
10 : #include "src/inspector/protocol/Forward.h"
11 :
12 : namespace v8_inspector {
13 : namespace protocol {
14 :
15 : class ErrorSupport {
16 : public:
17 : ErrorSupport();
18 : ~ErrorSupport();
19 :
20 : void push();
21 : void setName(const char*);
22 : void setName(const String&);
23 : void pop();
24 : void addError(const char*);
25 : void addError(const String&);
26 : bool hasErrors();
27 : String errors();
28 :
29 : private:
30 : std::vector<String> m_path;
31 : std::vector<String> m_errors;
32 : };
33 :
34 : } // namespace v8_inspector
35 : } // namespace protocol
36 :
37 : #endif // !defined(v8_inspector_protocol_ErrorSupport_h)
38 :
39 :
40 : // This file is generated by Values_h.template.
41 :
42 : // Copyright 2016 The Chromium Authors. All rights reserved.
43 : // Use of this source code is governed by a BSD-style license that can be
44 : // found in the LICENSE file.
45 :
46 : #ifndef v8_inspector_protocol_Values_h
47 : #define v8_inspector_protocol_Values_h
48 :
49 : //#include "Allocator.h"
50 : //#include "Forward.h"
51 :
52 : namespace v8_inspector {
53 : namespace protocol {
54 :
55 : class ListValue;
56 : class DictionaryValue;
57 : class Value;
58 :
59 : class Value : public Serializable {
60 : PROTOCOL_DISALLOW_COPY(Value);
61 : public:
62 76753426 : virtual ~Value() override { }
63 :
64 : static std::unique_ptr<Value> null()
65 : {
66 5344 : return std::unique_ptr<Value>(new Value());
67 : }
68 :
69 : static std::unique_ptr<Value> parseBinary(const uint8_t* data, size_t size);
70 :
71 : enum ValueType {
72 : TypeNull = 0,
73 : TypeBoolean,
74 : TypeInteger,
75 : TypeDouble,
76 : TypeString,
77 : TypeBinary,
78 : TypeObject,
79 : TypeArray,
80 : TypeSerialized,
81 : TypeImported
82 : };
83 :
84 : ValueType type() const { return m_type; }
85 :
86 : bool isNull() const { return m_type == TypeNull; }
87 :
88 : virtual bool asBoolean(bool* output) const;
89 : virtual bool asDouble(double* output) const;
90 : virtual bool asInteger(int* output) const;
91 : virtual bool asString(String* output) const;
92 : virtual bool asBinary(Binary* output) const;
93 :
94 : virtual void writeJSON(StringBuilder* output) const;
95 : virtual void writeBinary(std::vector<uint8_t>* bytes) const;
96 : virtual std::unique_ptr<Value> clone() const;
97 : String toJSONString() const;
98 : String serializeToJSON() override;
99 : std::vector<uint8_t> serializeToBinary() override;
100 :
101 : protected:
102 5344 : Value() : m_type(TypeNull) { }
103 38371369 : explicit Value(ValueType type) : m_type(type) { }
104 :
105 : private:
106 : friend class DictionaryValue;
107 : friend class ListValue;
108 :
109 : ValueType m_type;
110 : };
111 :
112 25737718 : class FundamentalValue : public Value {
113 : public:
114 : static std::unique_ptr<FundamentalValue> create(bool value)
115 : {
116 10358908 : return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
117 : }
118 :
119 : static std::unique_ptr<FundamentalValue> create(int value)
120 : {
121 2213082 : return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
122 : }
123 :
124 : static std::unique_ptr<FundamentalValue> create(double value)
125 : {
126 296869 : return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
127 : }
128 :
129 : bool asBoolean(bool* output) const override;
130 : bool asDouble(double* output) const override;
131 : bool asInteger(int* output) const override;
132 : void writeJSON(StringBuilder* output) const override;
133 : void writeBinary(std::vector<uint8_t>* bytes) const override;
134 : std::unique_ptr<Value> clone() const override;
135 :
136 : private:
137 10358908 : explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { }
138 2213082 : explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { }
139 296869 : explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { }
140 :
141 : union {
142 : bool m_boolValue;
143 : double m_doubleValue;
144 : int m_integerValue;
145 : };
146 : };
147 :
148 33631430 : class StringValue : public Value {
149 : public:
150 : static std::unique_ptr<StringValue> create(const String& value)
151 : {
152 16668560 : return std::unique_ptr<StringValue>(new StringValue(value));
153 : }
154 :
155 : static std::unique_ptr<StringValue> create(const char* value)
156 : {
157 : return std::unique_ptr<StringValue>(new StringValue(value));
158 : }
159 :
160 : bool asString(String* output) const override;
161 : void writeJSON(StringBuilder* output) const override;
162 : void writeBinary(std::vector<uint8_t>* bytes) const override;
163 : std::unique_ptr<Value> clone() const override;
164 :
165 : private:
166 50447145 : explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { }
167 : explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { }
168 :
169 : String m_stringValue;
170 : };
171 :
172 0 : class BinaryValue : public Value {
173 : public:
174 : static std::unique_ptr<BinaryValue> create(const Binary& value)
175 : {
176 0 : return std::unique_ptr<BinaryValue>(new BinaryValue(value));
177 : }
178 :
179 : bool asBinary(Binary* output) const override;
180 : void writeJSON(StringBuilder* output) const override;
181 : void writeBinary(std::vector<uint8_t>* bytes) const override;
182 : std::unique_ptr<Value> clone() const override;
183 :
184 : private:
185 0 : explicit BinaryValue(const Binary& value) : Value(TypeBinary), m_binaryValue(value) { }
186 :
187 : Binary m_binaryValue;
188 : };
189 :
190 987102 : class SerializedValue : public Value {
191 : public:
192 : static std::unique_ptr<SerializedValue> fromJSON(const String& value)
193 : {
194 329034 : return std::unique_ptr<SerializedValue>(new SerializedValue(value));
195 : }
196 :
197 0 : static std::unique_ptr<SerializedValue> fromBinary(std::vector<uint8_t> value)
198 : {
199 0 : return std::unique_ptr<SerializedValue>(new SerializedValue(std::move(value)));
200 : }
201 :
202 : void writeJSON(StringBuilder* output) const override;
203 : void writeBinary(std::vector<uint8_t>* bytes) const override;
204 : std::unique_ptr<Value> clone() const override;
205 :
206 : private:
207 987102 : explicit SerializedValue(const String& json) : Value(TypeSerialized), m_serializedJSON(json) { }
208 0 : explicit SerializedValue(std::vector<uint8_t> binary) : Value(TypeSerialized), m_serializedBinary(std::move(binary)) { }
209 0 : SerializedValue(const String& json, const std::vector<uint8_t>& binary)
210 0 : : Value(TypeSerialized), m_serializedJSON(json), m_serializedBinary(binary) { }
211 : String m_serializedJSON;
212 : std::vector<uint8_t> m_serializedBinary;
213 : };
214 :
215 : class DictionaryValue : public Value {
216 : public:
217 : using Entry = std::pair<String, Value*>;
218 811729 : static std::unique_ptr<DictionaryValue> create()
219 : {
220 8763919 : return std::unique_ptr<DictionaryValue>(new DictionaryValue());
221 : }
222 :
223 : static DictionaryValue* cast(Value* value)
224 : {
225 777580 : if (!value || value->type() != TypeObject)
226 : return nullptr;
227 : return static_cast<DictionaryValue*>(value);
228 : }
229 :
230 : static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value)
231 : {
232 : return std::unique_ptr<DictionaryValue>(DictionaryValue::cast(value.release()));
233 : }
234 :
235 : void writeJSON(StringBuilder* output) const override;
236 : void writeBinary(std::vector<uint8_t>* bytes) const override;
237 : std::unique_ptr<Value> clone() const override;
238 :
239 : size_t size() const { return m_data.size(); }
240 :
241 : void setBoolean(const String& name, bool);
242 : void setInteger(const String& name, int);
243 : void setDouble(const String& name, double);
244 : void setString(const String& name, const String&);
245 : void setValue(const String& name, std::unique_ptr<Value>);
246 : void setObject(const String& name, std::unique_ptr<DictionaryValue>);
247 : void setArray(const String& name, std::unique_ptr<ListValue>);
248 :
249 : bool getBoolean(const String& name, bool* output) const;
250 : bool getInteger(const String& name, int* output) const;
251 : bool getDouble(const String& name, double* output) const;
252 : bool getString(const String& name, String* output) const;
253 :
254 : DictionaryValue* getObject(const String& name) const;
255 : ListValue* getArray(const String& name) const;
256 : Value* get(const String& name) const;
257 : Entry at(size_t index) const;
258 :
259 : bool booleanProperty(const String& name, bool defaultValue) const;
260 : int integerProperty(const String& name, int defaultValue) const;
261 : double doubleProperty(const String& name, double defaultValue) const;
262 : void remove(const String& name);
263 :
264 : ~DictionaryValue() override;
265 :
266 : private:
267 : DictionaryValue();
268 : template<typename T>
269 34089836 : void set(const String& key, std::unique_ptr<T>& value)
270 : {
271 : DCHECK(value);
272 : bool isNew = m_data.find(key) == m_data.end();
273 : m_data[key] = std::move(value);
274 34089836 : if (isNew)
275 34078458 : m_order.push_back(key);
276 34089836 : }
277 :
278 : using Dictionary = std::unordered_map<String, std::unique_ptr<Value>>;
279 : Dictionary m_data;
280 : std::vector<String> m_order;
281 : };
282 :
283 : class ListValue : public Value {
284 : public:
285 940 : static std::unique_ptr<ListValue> create()
286 : {
287 406511 : return std::unique_ptr<ListValue>(new ListValue());
288 : }
289 :
290 : static ListValue* cast(Value* value)
291 : {
292 285 : if (!value || value->type() != TypeArray)
293 : return nullptr;
294 : return static_cast<ListValue*>(value);
295 : }
296 :
297 : static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value)
298 : {
299 : return std::unique_ptr<ListValue>(ListValue::cast(value.release()));
300 : }
301 :
302 : ~ListValue() override;
303 :
304 : void writeJSON(StringBuilder* output) const override;
305 : void writeBinary(std::vector<uint8_t>* bytes) const override;
306 : std::unique_ptr<Value> clone() const override;
307 :
308 : void pushValue(std::unique_ptr<Value>);
309 :
310 : Value* at(size_t index);
311 : size_t size() const { return m_data.size(); }
312 :
313 : private:
314 : ListValue();
315 : std::vector<std::unique_ptr<Value>> m_data;
316 : };
317 :
318 : void escapeLatinStringForJSON(const uint8_t* str, unsigned len, StringBuilder* dst);
319 : void escapeWideStringForJSON(const uint16_t* str, unsigned len, StringBuilder* dst);
320 :
321 : } // namespace v8_inspector
322 : } // namespace protocol
323 :
324 : #endif // v8_inspector_protocol_Values_h
325 :
326 :
327 : // This file is generated by Object_h.template.
328 :
329 : // Copyright 2016 The Chromium Authors. All rights reserved.
330 : // Use of this source code is governed by a BSD-style license that can be
331 : // found in the LICENSE file.
332 :
333 : #ifndef v8_inspector_protocol_Object_h
334 : #define v8_inspector_protocol_Object_h
335 :
336 : //#include "ErrorSupport.h"
337 : //#include "Forward.h"
338 : //#include "Values.h"
339 :
340 : namespace v8_inspector {
341 : namespace protocol {
342 :
343 : class Object {
344 : public:
345 : static std::unique_ptr<Object> fromValue(protocol::Value*, ErrorSupport*);
346 : explicit Object(std::unique_ptr<protocol::DictionaryValue>);
347 : ~Object();
348 :
349 : std::unique_ptr<protocol::DictionaryValue> toValue() const;
350 : std::unique_ptr<Object> clone() const;
351 : private:
352 : std::unique_ptr<protocol::DictionaryValue> m_object;
353 : };
354 :
355 : } // namespace v8_inspector
356 : } // namespace protocol
357 :
358 : #endif // !defined(v8_inspector_protocol_Object_h)
359 :
360 :
361 : // This file is generated by ValueConversions_h.template.
362 :
363 : // Copyright 2016 The Chromium Authors. All rights reserved.
364 : // Use of this source code is governed by a BSD-style license that can be
365 : // found in the LICENSE file.
366 :
367 : #ifndef v8_inspector_protocol_ValueConversions_h
368 : #define v8_inspector_protocol_ValueConversions_h
369 :
370 : //#include "ErrorSupport.h"
371 : //#include "Forward.h"
372 : //#include "Values.h"
373 :
374 : namespace v8_inspector {
375 : namespace protocol {
376 :
377 : template<typename T>
378 : struct ValueConversions {
379 0 : static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors)
380 : {
381 3282 : return T::fromValue(value, errors);
382 : }
383 :
384 11435 : static std::unique_ptr<protocol::Value> toValue(T* value)
385 : {
386 8317650 : return value->toValue();
387 : }
388 :
389 : static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value)
390 : {
391 6266592 : return value->toValue();
392 : }
393 : };
394 :
395 : template<>
396 : struct ValueConversions<bool> {
397 82147 : static bool fromValue(protocol::Value* value, ErrorSupport* errors)
398 : {
399 82147 : bool result = false;
400 82147 : bool success = value ? value->asBoolean(&result) : false;
401 82147 : if (!success)
402 0 : errors->addError("boolean value expected");
403 82147 : return result;
404 : }
405 :
406 : static std::unique_ptr<protocol::Value> toValue(bool value)
407 : {
408 : return FundamentalValue::create(value);
409 : }
410 : };
411 :
412 : template<>
413 : struct ValueConversions<int> {
414 5675 : static int fromValue(protocol::Value* value, ErrorSupport* errors)
415 : {
416 5675 : int result = 0;
417 5675 : bool success = value ? value->asInteger(&result) : false;
418 5675 : if (!success)
419 5 : errors->addError("integer value expected");
420 5675 : return result;
421 : }
422 :
423 : static std::unique_ptr<protocol::Value> toValue(int value)
424 : {
425 : return FundamentalValue::create(value);
426 : }
427 : };
428 :
429 : template<>
430 : struct ValueConversions<double> {
431 35 : static double fromValue(protocol::Value* value, ErrorSupport* errors)
432 : {
433 35 : double result = 0;
434 35 : bool success = value ? value->asDouble(&result) : false;
435 35 : if (!success)
436 0 : errors->addError("double value expected");
437 35 : return result;
438 : }
439 :
440 : static std::unique_ptr<protocol::Value> toValue(double value)
441 : {
442 : return FundamentalValue::create(value);
443 : }
444 : };
445 :
446 : template<>
447 : struct ValueConversions<String> {
448 113861 : static String fromValue(protocol::Value* value, ErrorSupport* errors)
449 : {
450 : String result;
451 113861 : bool success = value ? value->asString(&result) : false;
452 113861 : if (!success)
453 58 : errors->addError("string value expected");
454 113861 : return result;
455 : }
456 :
457 : static std::unique_ptr<protocol::Value> toValue(const String& value)
458 : {
459 : return StringValue::create(value);
460 : }
461 : };
462 :
463 : template<>
464 : struct ValueConversions<Binary> {
465 : static Binary fromValue(protocol::Value* value, ErrorSupport* errors)
466 : {
467 : if (!value ||
468 : (value->type() != Value::TypeBinary && value->type() != Value::TypeString)) {
469 : errors->addError("Either string base64 or binary value expected");
470 : return Binary();
471 : }
472 : Binary binary;
473 : if (value->asBinary(&binary))
474 : return binary;
475 : String result;
476 : value->asString(&result);
477 : bool success;
478 : Binary out = Binary::fromBase64(result, &success);
479 : if (!success)
480 : errors->addError("base64 decoding error");
481 : return out;
482 : }
483 :
484 : static std::unique_ptr<protocol::Value> toValue(const Binary& value)
485 : {
486 : return BinaryValue::create(value);
487 : }
488 : };
489 :
490 : template<>
491 : struct ValueConversions<Value> {
492 291 : static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors)
493 : {
494 : bool success = !!value;
495 291 : if (!success) {
496 0 : errors->addError("value expected");
497 : return nullptr;
498 : }
499 291 : return value->clone();
500 : }
501 :
502 : static std::unique_ptr<protocol::Value> toValue(Value* value)
503 : {
504 228398 : return value->clone();
505 : }
506 :
507 : static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value)
508 : {
509 : return value->clone();
510 : }
511 : };
512 :
513 : template<>
514 : struct ValueConversions<DictionaryValue> {
515 0 : static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors)
516 : {
517 0 : bool success = value && value->type() == protocol::Value::TypeObject;
518 0 : if (!success)
519 0 : errors->addError("object expected");
520 0 : return DictionaryValue::cast(value->clone());
521 : }
522 :
523 : static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value)
524 : {
525 2029 : return value->clone();
526 : }
527 :
528 : static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value)
529 : {
530 : return value->clone();
531 : }
532 : };
533 :
534 : template<>
535 : struct ValueConversions<ListValue> {
536 : static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors)
537 : {
538 : bool success = value && value->type() == protocol::Value::TypeArray;
539 : if (!success)
540 : errors->addError("list expected");
541 : return ListValue::cast(value->clone());
542 : }
543 :
544 : static std::unique_ptr<protocol::Value> toValue(ListValue* value)
545 : {
546 : return value->clone();
547 : }
548 :
549 : static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value)
550 : {
551 : return value->clone();
552 : }
553 : };
554 :
555 : } // namespace v8_inspector
556 : } // namespace protocol
557 :
558 : #endif // !defined(v8_inspector_protocol_ValueConversions_h)
559 :
560 :
561 : // This file is generated by Maybe_h.template.
562 :
563 : // Copyright 2016 The Chromium Authors. All rights reserved.
564 : // Use of this source code is governed by a BSD-style license that can be
565 : // found in the LICENSE file.
566 :
567 : #ifndef v8_inspector_protocol_Maybe_h
568 : #define v8_inspector_protocol_Maybe_h
569 :
570 : // This macro allows to test for the version of the GNU C++ compiler.
571 : // Note that this also applies to compilers that masquerade as GCC,
572 : // for example clang and the Intel C++ compiler for Linux.
573 : // Use like:
574 : // #if IP_GNUC_PREREQ(4, 3, 1)
575 : // ...
576 : // #endif
577 : #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
578 : #define IP_GNUC_PREREQ(major, minor, patchlevel) \
579 : ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= \
580 : ((major)*10000 + (minor)*100 + (patchlevel)))
581 : #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
582 : #define IP_GNUC_PREREQ(major, minor, patchlevel) \
583 : ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= \
584 : ((major)*10000 + (minor)*100 + (patchlevel)))
585 : #else
586 : #define IP_GNUC_PREREQ(major, minor, patchlevel) 0
587 : #endif
588 :
589 : #if defined(__mips64)
590 : #define IP_TARGET_ARCH_MIPS64 1
591 : #elif defined(__MIPSEB__) || defined(__MIPSEL__)
592 : #define IP_TARGET_ARCH_MIPS 1
593 : #endif
594 :
595 : // Allowing the use of noexcept by removing the keyword on older compilers that
596 : // do not support adding noexcept to default members.
597 : #if ((IP_GNUC_PREREQ(4, 9, 0) && !defined(IP_TARGET_ARCH_MIPS) && \
598 : !defined(IP_TARGET_ARCH_MIPS64)) || \
599 : (defined(__clang__) && __cplusplus > 201300L))
600 : #define IP_NOEXCEPT noexcept
601 : #else
602 : #define IP_NOEXCEPT
603 : #endif
604 :
605 : //#include "Forward.h"
606 :
607 : namespace v8_inspector {
608 : namespace protocol {
609 :
610 : template<typename T>
611 21327863 : class Maybe {
612 : public:
613 : Maybe() : m_value() { }
614 : Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
615 : Maybe(Maybe&& other) IP_NOEXCEPT : m_value(std::move(other.m_value)) {}
616 56083 : void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
617 : T* fromJust() const { DCHECK(m_value); return m_value.get(); }
618 : T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
619 : bool isJust() const { return !!m_value; }
620 : std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value); }
621 : private:
622 : std::unique_ptr<T> m_value;
623 : };
624 :
625 : template<typename T>
626 16189728 : class MaybeBase {
627 : public:
628 24406460 : MaybeBase() : m_isJust(false) { }
629 280552 : MaybeBase(T value) : m_isJust(true), m_value(value) { }
630 : MaybeBase(MaybeBase&& other) IP_NOEXCEPT
631 : : m_isJust(other.m_isJust),
632 207119 : m_value(std::move(other.m_value)) {}
633 14660272 : void operator=(T value) { m_value = value; m_isJust = true; }
634 : T fromJust() const { DCHECK(m_isJust); return m_value; }
635 460109 : T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
636 : bool isJust() const { return m_isJust; }
637 : T takeJust() { DCHECK(m_isJust); return m_value; }
638 :
639 : protected:
640 : bool m_isJust;
641 : T m_value;
642 : };
643 :
644 : template<>
645 : class Maybe<bool> : public MaybeBase<bool> {
646 : public:
647 7689403 : Maybe() { m_value = false; }
648 : Maybe(bool value) : MaybeBase(value) { }
649 : Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
650 : using MaybeBase::operator=;
651 : };
652 :
653 : template<>
654 : class Maybe<int> : public MaybeBase<int> {
655 : public:
656 671534 : Maybe() { m_value = 0; }
657 : Maybe(int value) : MaybeBase(value) { }
658 : Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
659 : using MaybeBase::operator=;
660 : };
661 :
662 : template<>
663 : class Maybe<double> : public MaybeBase<double> {
664 : public:
665 : Maybe() { m_value = 0; }
666 : Maybe(double value) : MaybeBase(value) { }
667 : Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
668 : using MaybeBase::operator=;
669 : };
670 :
671 : template<>
672 16189728 : class Maybe<String> : public MaybeBase<String> {
673 : public:
674 : Maybe() { }
675 112714 : Maybe(const String& value) : MaybeBase(value) { }
676 : Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
677 : using MaybeBase::operator=;
678 : };
679 :
680 : template<>
681 : class Maybe<Binary> : public MaybeBase<Binary> {
682 : public:
683 : Maybe() { }
684 : Maybe(Binary value) : MaybeBase(value) { }
685 : Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
686 : using MaybeBase::operator=;
687 : };
688 :
689 : } // namespace v8_inspector
690 : } // namespace protocol
691 :
692 : #undef IP_GNUC_PREREQ
693 : #undef IP_TARGET_ARCH_MIPS64
694 : #undef IP_TARGET_ARCH_MIPS
695 : #undef IP_NOEXCEPT
696 :
697 : #endif // !defined(v8_inspector_protocol_Maybe_h)
698 :
699 :
700 : // This file is generated by Array_h.template.
701 :
702 : // Copyright 2016 The Chromium Authors. All rights reserved.
703 : // Use of this source code is governed by a BSD-style license that can be
704 : // found in the LICENSE file.
705 :
706 : #ifndef v8_inspector_protocol_Array_h
707 : #define v8_inspector_protocol_Array_h
708 :
709 : //#include "ErrorSupport.h"
710 : //#include "Forward.h"
711 : //#include "ValueConversions.h"
712 : //#include "Values.h"
713 :
714 : namespace v8_inspector {
715 : namespace protocol {
716 :
717 : template<typename T>
718 485073 : class Array {
719 : public:
720 : static std::unique_ptr<Array<T>> create()
721 : {
722 484893 : return std::unique_ptr<Array<T>>(new Array<T>());
723 : }
724 :
725 180 : static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors)
726 : {
727 : protocol::ListValue* array = ListValue::cast(value);
728 180 : if (!array) {
729 0 : errors->addError("array expected");
730 : return nullptr;
731 : }
732 360 : std::unique_ptr<Array<T>> result(new Array<T>());
733 180 : errors->push();
734 650 : for (size_t i = 0; i < array->size(); ++i) {
735 470 : errors->setName(StringUtil::fromInteger(i));
736 235 : std::unique_ptr<T> item = ValueConversions<T>::fromValue(array->at(i), errors);
737 235 : result->m_vector.push_back(std::move(item));
738 : }
739 180 : errors->pop();
740 180 : if (errors->hasErrors())
741 : return nullptr;
742 : return result;
743 : }
744 :
745 : void addItem(std::unique_ptr<T> value)
746 : {
747 3133366 : m_vector.push_back(std::move(value));
748 : }
749 :
750 : size_t length()
751 : {
752 : return m_vector.size();
753 : }
754 :
755 : T* get(size_t index)
756 : {
757 : return m_vector[index].get();
758 : }
759 :
760 348414 : std::unique_ptr<protocol::ListValue> toValue()
761 : {
762 : std::unique_ptr<protocol::ListValue> result = ListValue::create();
763 3481710 : for (auto& item : m_vector)
764 6266592 : result->pushValue(ValueConversions<T>::toValue(item));
765 348414 : return result;
766 : }
767 :
768 : private:
769 : std::vector<std::unique_ptr<T>> m_vector;
770 : };
771 :
772 : template<typename T>
773 56057 : class ArrayBase {
774 : public:
775 : static std::unique_ptr<Array<T>> create()
776 : {
777 55952 : return std::unique_ptr<Array<T>>(new Array<T>());
778 : }
779 :
780 105 : static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors)
781 : {
782 : protocol::ListValue* array = ListValue::cast(value);
783 105 : if (!array) {
784 0 : errors->addError("array expected");
785 : return nullptr;
786 : }
787 105 : errors->push();
788 210 : std::unique_ptr<Array<T>> result(new Array<T>());
789 325 : for (size_t i = 0; i < array->size(); ++i) {
790 220 : errors->setName(StringUtil::fromInteger(i));
791 110 : T item = ValueConversions<T>::fromValue(array->at(i), errors);
792 110 : result->m_vector.push_back(item);
793 : }
794 105 : errors->pop();
795 105 : if (errors->hasErrors())
796 : return nullptr;
797 : return result;
798 : }
799 :
800 : void addItem(const T& value)
801 : {
802 3170 : m_vector.push_back(value);
803 : }
804 :
805 : size_t length()
806 : {
807 : return m_vector.size();
808 : }
809 :
810 : T get(size_t index)
811 : {
812 : return m_vector[index];
813 : }
814 :
815 55952 : std::unique_ptr<protocol::ListValue> toValue()
816 : {
817 : std::unique_ptr<protocol::ListValue> result = ListValue::create();
818 59122 : for (auto& item : m_vector)
819 6929 : result->pushValue(ValueConversions<T>::toValue(item));
820 55952 : return result;
821 : }
822 :
823 : private:
824 : std::vector<T> m_vector;
825 : };
826 :
827 55598 : template<> class Array<String> : public ArrayBase<String> {};
828 459 : template<> class Array<int> : public ArrayBase<int> {};
829 : template<> class Array<double> : public ArrayBase<double> {};
830 : template<> class Array<bool> : public ArrayBase<bool> {};
831 :
832 : } // namespace v8_inspector
833 : } // namespace protocol
834 :
835 : #endif // !defined(v8_inspector_protocol_Array_h)
836 :
837 :
838 : // This file is generated by DispatcherBase_h.template.
839 :
840 : // Copyright 2016 The Chromium Authors. All rights reserved.
841 : // Use of this source code is governed by a BSD-style license that can be
842 : // found in the LICENSE file.
843 :
844 : #ifndef v8_inspector_protocol_DispatcherBase_h
845 : #define v8_inspector_protocol_DispatcherBase_h
846 :
847 : //#include "Forward.h"
848 : //#include "ErrorSupport.h"
849 : //#include "Values.h"
850 :
851 : namespace v8_inspector {
852 : namespace protocol {
853 :
854 : class WeakPtr;
855 :
856 25190744 : class DispatchResponse {
857 : public:
858 : enum Status {
859 : kSuccess = 0,
860 : kError = 1,
861 : kFallThrough = 2,
862 : };
863 :
864 : enum ErrorCode {
865 : kParseError = -32700,
866 : kInvalidRequest = -32600,
867 : kMethodNotFound = -32601,
868 : kInvalidParams = -32602,
869 : kInternalError = -32603,
870 : kServerError = -32000,
871 : };
872 :
873 : Status status() const { return m_status; }
874 848 : const String& errorMessage() const { return m_errorMessage; }
875 : ErrorCode errorCode() const { return m_errorCode; }
876 : bool isSuccess() const { return m_status == kSuccess; }
877 :
878 : static DispatchResponse OK();
879 : static DispatchResponse Error(const String&);
880 : static DispatchResponse InternalError();
881 : static DispatchResponse InvalidParams(const String&);
882 : static DispatchResponse FallThrough();
883 :
884 : private:
885 : Status m_status;
886 : String m_errorMessage;
887 : ErrorCode m_errorCode;
888 : };
889 :
890 : class DispatcherBase {
891 : PROTOCOL_DISALLOW_COPY(DispatcherBase);
892 : public:
893 : static const char kInvalidParamsString[];
894 : class WeakPtr {
895 : public:
896 : explicit WeakPtr(DispatcherBase*);
897 : ~WeakPtr();
898 252591 : DispatcherBase* get() { return m_dispatcher; }
899 10 : void dispose() { m_dispatcher = nullptr; }
900 :
901 : private:
902 : DispatcherBase* m_dispatcher;
903 : };
904 :
905 19954 : class Callback {
906 : public:
907 : Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message);
908 : virtual ~Callback();
909 : void dispose();
910 :
911 : protected:
912 : void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response);
913 : void fallThroughIfActive();
914 :
915 : private:
916 : std::unique_ptr<WeakPtr> m_backendImpl;
917 : int m_callId;
918 : String m_method;
919 : ProtocolMessage m_message;
920 : };
921 :
922 : explicit DispatcherBase(FrontendChannel*);
923 : virtual ~DispatcherBase();
924 :
925 : virtual bool canDispatch(const String& method) = 0;
926 : virtual void dispatch(int callId, const String& method, const ProtocolMessage& rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
927 : FrontendChannel* channel() { return m_frontendChannel; }
928 :
929 : void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
930 : void sendResponse(int callId, const DispatchResponse&);
931 :
932 : void reportProtocolError(int callId, DispatchResponse::ErrorCode, const String& errorMessage, ErrorSupport* errors);
933 : void clearFrontend();
934 :
935 : std::unique_ptr<WeakPtr> weakPtr();
936 :
937 : private:
938 : FrontendChannel* m_frontendChannel;
939 : std::unordered_set<WeakPtr*> m_weakPtrs;
940 : };
941 :
942 7758 : class UberDispatcher {
943 : PROTOCOL_DISALLOW_COPY(UberDispatcher);
944 : public:
945 : explicit UberDispatcher(FrontendChannel*);
946 : void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
947 : void setupRedirects(const std::unordered_map<String, String>&);
948 : bool parseCommand(Value* message, int* callId, String* method);
949 : bool canDispatch(const String& method);
950 : void dispatch(int callId, const String& method, std::unique_ptr<Value> message, const ProtocolMessage& rawMessage);
951 : FrontendChannel* channel() { return m_frontendChannel; }
952 : virtual ~UberDispatcher();
953 :
954 : private:
955 : protocol::DispatcherBase* findDispatcher(const String& method);
956 : FrontendChannel* m_frontendChannel;
957 : std::unordered_map<String, String> m_redirects;
958 : std::unordered_map<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
959 : };
960 :
961 : class InternalResponse : public Serializable {
962 : PROTOCOL_DISALLOW_COPY(InternalResponse);
963 : public:
964 : static std::unique_ptr<InternalResponse> createResponse(int callId, std::unique_ptr<Serializable> params);
965 : static std::unique_ptr<InternalResponse> createNotification(const String& notification, std::unique_ptr<Serializable> params = nullptr);
966 :
967 : String serializeToJSON() override;
968 : std::vector<uint8_t> serializeToBinary() override;
969 :
970 987102 : ~InternalResponse() override {}
971 :
972 : private:
973 : InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params);
974 :
975 : int m_callId;
976 : String m_notification;
977 : std::unique_ptr<Serializable> m_params;
978 : };
979 :
980 : class InternalRawNotification : public Serializable {
981 : public:
982 0 : static std::unique_ptr<InternalRawNotification> fromJSON(String notification)
983 : {
984 0 : return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
985 : }
986 :
987 0 : static std::unique_ptr<InternalRawNotification> fromBinary(std::vector<uint8_t> notification)
988 : {
989 0 : return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
990 : }
991 :
992 0 : ~InternalRawNotification() override {}
993 :
994 0 : String serializeToJSON() override
995 : {
996 0 : return std::move(m_jsonNotification);
997 : }
998 :
999 0 : std::vector<uint8_t> serializeToBinary() override
1000 : {
1001 0 : return std::move(m_binaryNotification);
1002 : }
1003 :
1004 : private:
1005 : explicit InternalRawNotification(String notification)
1006 0 : : m_jsonNotification(std::move(notification)) { }
1007 : explicit InternalRawNotification(std::vector<uint8_t> notification)
1008 0 : : m_binaryNotification(std::move(notification)) { }
1009 :
1010 : String m_jsonNotification;
1011 : std::vector<uint8_t> m_binaryNotification;
1012 : };
1013 :
1014 : } // namespace v8_inspector
1015 : } // namespace protocol
1016 :
1017 : #endif // !defined(v8_inspector_protocol_DispatcherBase_h)
1018 :
1019 :
1020 : // This file is generated by Parser_h.template.
1021 :
1022 : // Copyright 2016 The Chromium Authors. All rights reserved.
1023 : // Use of this source code is governed by a BSD-style license that can be
1024 : // found in the LICENSE file.
1025 :
1026 : #ifndef v8_inspector_protocol_Parser_h
1027 : #define v8_inspector_protocol_Parser_h
1028 :
1029 : //#include "Forward.h"
1030 : //#include "Values.h"
1031 :
1032 : namespace v8_inspector {
1033 : namespace protocol {
1034 :
1035 : std::unique_ptr<Value> parseJSONCharacters(const uint8_t*, unsigned);
1036 : std::unique_ptr<Value> parseJSONCharacters(const uint16_t*, unsigned);
1037 :
1038 : } // namespace v8_inspector
1039 : } // namespace protocol
1040 :
1041 : #endif // !defined(v8_inspector_protocol_Parser_h)
1042 :
1043 :
1044 : // Generated by lib/encoding_h.template.
1045 :
1046 : // Copyright 2019 The Chromium Authors. All rights reserved.
1047 : // Use of this source code is governed by a BSD-style license that can be
1048 : // found in the LICENSE file.
1049 :
1050 : #ifndef v8_inspector_protocol_encoding_h
1051 : #define v8_inspector_protocol_encoding_h
1052 :
1053 : #include <cstddef>
1054 : #include <cstdint>
1055 : #include <memory>
1056 : #include <string>
1057 : #include <vector>
1058 :
1059 : namespace v8_inspector {
1060 : namespace protocol {
1061 :
1062 : // ===== encoding/encoding.h =====
1063 :
1064 :
1065 : // =============================================================================
1066 : // span - sequence of bytes
1067 : // =============================================================================
1068 :
1069 : // This template is similar to std::span, which will be included in C++20. Like
1070 : // std::span it uses ptrdiff_t, which is signed (and thus a bit annoying
1071 : // sometimes when comparing with size_t), but other than this it's much simpler.
1072 : template <typename T>
1073 : class span {
1074 : public:
1075 : using index_type = std::ptrdiff_t;
1076 :
1077 : span() : data_(nullptr), size_(0) {}
1078 0 : span(const T* data, index_type size) : data_(data), size_(size) {}
1079 :
1080 : const T* data() const { return data_; }
1081 :
1082 : const T* begin() const { return data_; }
1083 0 : const T* end() const { return data_ + size_; }
1084 :
1085 0 : const T& operator[](index_type idx) const { return data_[idx]; }
1086 :
1087 : span<T> subspan(index_type offset, index_type count) const {
1088 0 : return span(data_ + offset, count);
1089 : }
1090 :
1091 : span<T> subspan(index_type offset) const {
1092 0 : return span(data_ + offset, size_ - offset);
1093 : }
1094 :
1095 : bool empty() const { return size_ == 0; }
1096 :
1097 : index_type size() const { return size_; }
1098 0 : index_type size_bytes() const { return size_ * sizeof(T); }
1099 :
1100 : private:
1101 : const T* data_;
1102 : index_type size_;
1103 : };
1104 :
1105 : template <typename T>
1106 : span<T> SpanFromVector(const std::vector<T>& v) {
1107 : return span<T>(v.data(), v.size());
1108 : }
1109 :
1110 : inline span<uint8_t> SpanFromStdString(const std::string& v) {
1111 : return span<uint8_t>(reinterpret_cast<const uint8_t*>(v.data()), v.size());
1112 : }
1113 :
1114 : // Error codes.
1115 : enum class Error {
1116 : OK = 0,
1117 : // JSON parsing errors - json_parser.{h,cc}.
1118 : JSON_PARSER_UNPROCESSED_INPUT_REMAINS = 0x01,
1119 : JSON_PARSER_STACK_LIMIT_EXCEEDED = 0x02,
1120 : JSON_PARSER_NO_INPUT = 0x03,
1121 : JSON_PARSER_INVALID_TOKEN = 0x04,
1122 : JSON_PARSER_INVALID_NUMBER = 0x05,
1123 : JSON_PARSER_INVALID_STRING = 0x06,
1124 : JSON_PARSER_UNEXPECTED_ARRAY_END = 0x07,
1125 : JSON_PARSER_COMMA_OR_ARRAY_END_EXPECTED = 0x08,
1126 : JSON_PARSER_STRING_LITERAL_EXPECTED = 0x09,
1127 : JSON_PARSER_COLON_EXPECTED = 0x0a,
1128 : JSON_PARSER_UNEXPECTED_MAP_END = 0x0b,
1129 : JSON_PARSER_COMMA_OR_MAP_END_EXPECTED = 0x0c,
1130 : JSON_PARSER_VALUE_EXPECTED = 0x0d,
1131 :
1132 : CBOR_INVALID_INT32 = 0x0e,
1133 : CBOR_INVALID_DOUBLE = 0x0f,
1134 : CBOR_INVALID_ENVELOPE = 0x10,
1135 : CBOR_INVALID_STRING8 = 0x11,
1136 : CBOR_INVALID_STRING16 = 0x12,
1137 : CBOR_INVALID_BINARY = 0x13,
1138 : CBOR_UNSUPPORTED_VALUE = 0x14,
1139 : CBOR_NO_INPUT = 0x15,
1140 : CBOR_INVALID_START_BYTE = 0x16,
1141 : CBOR_UNEXPECTED_EOF_EXPECTED_VALUE = 0x17,
1142 : CBOR_UNEXPECTED_EOF_IN_ARRAY = 0x18,
1143 : CBOR_UNEXPECTED_EOF_IN_MAP = 0x19,
1144 : CBOR_INVALID_MAP_KEY = 0x1a,
1145 : CBOR_STACK_LIMIT_EXCEEDED = 0x1b,
1146 : CBOR_STRING8_MUST_BE_7BIT = 0x1c,
1147 : CBOR_TRAILING_JUNK = 0x1d,
1148 : CBOR_MAP_START_EXPECTED = 0x1e,
1149 : CBOR_MAP_STOP_EXPECTED = 0x1f,
1150 : CBOR_ENVELOPE_SIZE_LIMIT_EXCEEDED = 0x20,
1151 : };
1152 :
1153 : // A status value with position that can be copied. The default status
1154 : // is OK. Usually, error status values should come with a valid position.
1155 : struct Status {
1156 : static constexpr std::ptrdiff_t npos() { return -1; }
1157 :
1158 : bool ok() const { return error == Error::OK; }
1159 :
1160 : Error error = Error::OK;
1161 : std::ptrdiff_t pos = npos();
1162 0 : Status(Error error, std::ptrdiff_t pos) : error(error), pos(pos) {}
1163 0 : Status() = default;
1164 : };
1165 :
1166 : // Handler interface for parser events emitted by a streaming parser.
1167 : // See cbor::NewCBOREncoder, cbor::ParseCBOR, json::NewJSONEncoder,
1168 : // json::ParseJSON.
1169 0 : class StreamingParserHandler {
1170 : public:
1171 0 : virtual ~StreamingParserHandler() = default;
1172 : virtual void HandleMapBegin() = 0;
1173 : virtual void HandleMapEnd() = 0;
1174 : virtual void HandleArrayBegin() = 0;
1175 : virtual void HandleArrayEnd() = 0;
1176 : virtual void HandleString8(span<uint8_t> chars) = 0;
1177 : virtual void HandleString16(span<uint16_t> chars) = 0;
1178 : virtual void HandleBinary(span<uint8_t> bytes) = 0;
1179 : virtual void HandleDouble(double value) = 0;
1180 : virtual void HandleInt32(int32_t value) = 0;
1181 : virtual void HandleBool(bool value) = 0;
1182 : virtual void HandleNull() = 0;
1183 :
1184 : // The parser may send one error even after other events have already
1185 : // been received. Client code is reponsible to then discard the
1186 : // already processed events.
1187 : // |error| must be an eror, as in, |error.is_ok()| can't be true.
1188 : virtual void HandleError(Status error) = 0;
1189 : };
1190 :
1191 : namespace cbor {
1192 : // The binary encoding for the inspector protocol follows the CBOR specification
1193 : // (RFC 7049). Additional constraints:
1194 : // - Only indefinite length maps and arrays are supported.
1195 : // - Maps and arrays are wrapped with an envelope, that is, a
1196 : // CBOR tag with value 24 followed by a byte string specifying
1197 : // the byte length of the enclosed map / array. The byte string
1198 : // must use a 32 bit wide length.
1199 : // - At the top level, a message must be an indefinite length map
1200 : // wrapped by an envelope.
1201 : // - Maximal size for messages is 2^32 (4 GB).
1202 : // - For scalars, we support only the int32_t range, encoded as
1203 : // UNSIGNED/NEGATIVE (major types 0 / 1).
1204 : // - UTF16 strings, including with unbalanced surrogate pairs, are encoded
1205 : // as CBOR BYTE_STRING (major type 2). For such strings, the number of
1206 : // bytes encoded must be even.
1207 : // - UTF8 strings (major type 3) are supported.
1208 : // - 7 bit US-ASCII strings must always be encoded as UTF8 strings, never
1209 : // as UTF16 strings.
1210 : // - Arbitrary byte arrays, in the inspector protocol called 'binary',
1211 : // are encoded as BYTE_STRING (major type 2), prefixed with a byte
1212 : // indicating base64 when rendered as JSON.
1213 :
1214 : // =============================================================================
1215 : // Detecting CBOR content
1216 : // =============================================================================
1217 :
1218 : // The first byte for an envelope, which we use for wrapping dictionaries
1219 : // and arrays; and the byte that indicates a byte string with 32 bit length.
1220 : // These two bytes start an envelope, and thereby also any CBOR message
1221 : // produced or consumed by this protocol. See also |EnvelopeEncoder| below.
1222 : uint8_t InitialByteForEnvelope();
1223 : uint8_t InitialByteFor32BitLengthByteString();
1224 :
1225 : // Checks whether |msg| is a cbor message.
1226 : bool IsCBORMessage(span<uint8_t> msg);
1227 :
1228 : // =============================================================================
1229 : // Encoding individual CBOR items
1230 : // =============================================================================
1231 :
1232 : // Some constants for CBOR tokens that only take a single byte on the wire.
1233 : uint8_t EncodeTrue();
1234 : uint8_t EncodeFalse();
1235 : uint8_t EncodeNull();
1236 : uint8_t EncodeIndefiniteLengthArrayStart();
1237 : uint8_t EncodeIndefiniteLengthMapStart();
1238 : uint8_t EncodeStop();
1239 :
1240 : // Encodes |value| as |UNSIGNED| (major type 0) iff >= 0, or |NEGATIVE|
1241 : // (major type 1) iff < 0.
1242 : void EncodeInt32(int32_t value, std::vector<uint8_t>* out);
1243 : void EncodeInt32(int32_t value, std::string* out);
1244 :
1245 : // Encodes a UTF16 string as a BYTE_STRING (major type 2). Each utf16
1246 : // character in |in| is emitted with most significant byte first,
1247 : // appending to |out|.
1248 : void EncodeString16(span<uint16_t> in, std::vector<uint8_t>* out);
1249 : void EncodeString16(span<uint16_t> in, std::string* out);
1250 :
1251 : // Encodes a UTF8 string |in| as STRING (major type 3).
1252 : void EncodeString8(span<uint8_t> in, std::vector<uint8_t>* out);
1253 : void EncodeString8(span<uint8_t> in, std::string* out);
1254 :
1255 : // Encodes the given |latin1| string as STRING8.
1256 : // If any non-ASCII character is present, it will be represented
1257 : // as a 2 byte UTF8 sequence.
1258 : void EncodeFromLatin1(span<uint8_t> latin1, std::vector<uint8_t>* out);
1259 : void EncodeFromLatin1(span<uint8_t> latin1, std::string* out);
1260 :
1261 : // Encodes the given |utf16| string as STRING8 if it's entirely US-ASCII.
1262 : // Otherwise, encodes as STRING16.
1263 : void EncodeFromUTF16(span<uint16_t> utf16, std::vector<uint8_t>* out);
1264 : void EncodeFromUTF16(span<uint16_t> utf16, std::string* out);
1265 :
1266 : // Encodes arbitrary binary data in |in| as a BYTE_STRING (major type 2) with
1267 : // definitive length, prefixed with tag 22 indicating expected conversion to
1268 : // base64 (see RFC 7049, Table 3 and Section 2.4.4.2).
1269 : void EncodeBinary(span<uint8_t> in, std::vector<uint8_t>* out);
1270 : void EncodeBinary(span<uint8_t> in, std::string* out);
1271 :
1272 : // Encodes / decodes a double as Major type 7 (SIMPLE_VALUE),
1273 : // with additional info = 27, followed by 8 bytes in big endian.
1274 : void EncodeDouble(double value, std::vector<uint8_t>* out);
1275 : void EncodeDouble(double value, std::string* out);
1276 :
1277 : // =============================================================================
1278 : // cbor::EnvelopeEncoder - for wrapping submessages
1279 : // =============================================================================
1280 :
1281 : // An envelope indicates the byte length of a wrapped item.
1282 : // We use this for maps and array, which allows the decoder
1283 : // to skip such (nested) values whole sale.
1284 : // It's implemented as a CBOR tag (major type 6) with additional
1285 : // info = 24, followed by a byte string with a 32 bit length value;
1286 : // so the maximal structure that we can wrap is 2^32 bits long.
1287 : // See also: https://tools.ietf.org/html/rfc7049#section-2.4.4.1
1288 : class EnvelopeEncoder {
1289 : public:
1290 : // Emits the envelope start bytes and records the position for the
1291 : // byte size in |byte_size_pos_|. Also emits empty bytes for the
1292 : // byte sisze so that encoding can continue.
1293 : void EncodeStart(std::vector<uint8_t>* out);
1294 : void EncodeStart(std::string* out);
1295 : // This records the current size in |out| at position byte_size_pos_.
1296 : // Returns true iff successful.
1297 : bool EncodeStop(std::vector<uint8_t>* out);
1298 : bool EncodeStop(std::string* out);
1299 :
1300 : private:
1301 : std::size_t byte_size_pos_ = 0;
1302 : };
1303 :
1304 : // =============================================================================
1305 : // cbor::NewCBOREncoder - for encoding from a streaming parser
1306 : // =============================================================================
1307 :
1308 : // This can be used to convert to CBOR, by passing the return value to a parser
1309 : // that drives it. The handler will encode into |out|, and iff an error occurs
1310 : // it will set |status| to an error and clear |out|. Otherwise, |status.ok()|
1311 : // will be |true|.
1312 : std::unique_ptr<StreamingParserHandler> NewCBOREncoder(
1313 : std::vector<uint8_t>* out,
1314 : Status* status);
1315 : std::unique_ptr<StreamingParserHandler> NewCBOREncoder(std::string* out,
1316 : Status* status);
1317 :
1318 : // =============================================================================
1319 : // cbor::CBORTokenizer - for parsing individual CBOR items
1320 : // =============================================================================
1321 :
1322 : // Tags for the tokens within a CBOR message that CBORTokenizer understands.
1323 : // Note that this is not the same terminology as the CBOR spec (RFC 7049),
1324 : // but rather, our adaptation. For instance, we lump unsigned and signed
1325 : // major type into INT32 here (and disallow values outside the int32_t range).
1326 : enum class CBORTokenTag {
1327 : // Encountered an error in the structure of the message. Consult
1328 : // status() for details.
1329 : ERROR_VALUE,
1330 : // Booleans and NULL.
1331 : TRUE_VALUE,
1332 : FALSE_VALUE,
1333 : NULL_VALUE,
1334 : // An int32_t (signed 32 bit integer).
1335 : INT32,
1336 : // A double (64 bit floating point).
1337 : DOUBLE,
1338 : // A UTF8 string.
1339 : STRING8,
1340 : // A UTF16 string.
1341 : STRING16,
1342 : // A binary string.
1343 : BINARY,
1344 : // Starts an indefinite length map; after the map start we expect
1345 : // alternating keys and values, followed by STOP.
1346 : MAP_START,
1347 : // Starts an indefinite length array; after the array start we
1348 : // expect values, followed by STOP.
1349 : ARRAY_START,
1350 : // Ends a map or an array.
1351 : STOP,
1352 : // An envelope indicator, wrapping a map or array.
1353 : // Internally this carries the byte length of the wrapped
1354 : // map or array. While CBORTokenizer::Next() will read / skip the entire
1355 : // envelope, CBORTokenizer::EnterEnvelope() reads the tokens
1356 : // inside of it.
1357 : ENVELOPE,
1358 : // We've reached the end there is nothing else to read.
1359 : DONE,
1360 : };
1361 :
1362 : // The major types from RFC 7049 Section 2.1.
1363 : enum class MajorType {
1364 : UNSIGNED = 0,
1365 : NEGATIVE = 1,
1366 : BYTE_STRING = 2,
1367 : STRING = 3,
1368 : ARRAY = 4,
1369 : MAP = 5,
1370 : TAG = 6,
1371 : SIMPLE_VALUE = 7
1372 : };
1373 :
1374 : // CBORTokenizer segments a CBOR message, presenting the tokens therein as
1375 : // numbers, strings, etc. This is not a complete CBOR parser, but makes it much
1376 : // easier to implement one (e.g. ParseCBOR, above). It can also be used to parse
1377 : // messages partially.
1378 : class CBORTokenizer {
1379 : public:
1380 : explicit CBORTokenizer(span<uint8_t> bytes);
1381 : ~CBORTokenizer();
1382 :
1383 : // Identifies the current token that we're looking at,
1384 : // or ERROR_VALUE (in which ase ::Status() has details)
1385 : // or DONE (if we're past the last token).
1386 : CBORTokenTag TokenTag() const;
1387 :
1388 : // Advances to the next token.
1389 : void Next();
1390 : // Can only be called if TokenTag() == CBORTokenTag::ENVELOPE.
1391 : // While Next() would skip past the entire envelope / what it's
1392 : // wrapping, EnterEnvelope positions the cursor inside of the envelope,
1393 : // letting the client explore the nested structure.
1394 : void EnterEnvelope();
1395 :
1396 : // If TokenTag() is CBORTokenTag::ERROR_VALUE, then Status().error describes
1397 : // the error more precisely; otherwise it'll be set to Error::OK.
1398 : // In either case, Status().pos is the current position.
1399 : struct Status Status() const;
1400 :
1401 : // The following methods retrieve the token values. They can only
1402 : // be called if TokenTag() matches.
1403 :
1404 : // To be called only if ::TokenTag() == CBORTokenTag::INT32.
1405 : int32_t GetInt32() const;
1406 :
1407 : // To be called only if ::TokenTag() == CBORTokenTag::DOUBLE.
1408 : double GetDouble() const;
1409 :
1410 : // To be called only if ::TokenTag() == CBORTokenTag::STRING8.
1411 : span<uint8_t> GetString8() const;
1412 :
1413 : // Wire representation for STRING16 is low byte first (little endian).
1414 : // To be called only if ::TokenTag() == CBORTokenTag::STRING16.
1415 : span<uint8_t> GetString16WireRep() const;
1416 :
1417 : // To be called only if ::TokenTag() == CBORTokenTag::BINARY.
1418 : span<uint8_t> GetBinary() const;
1419 :
1420 : // To be called only if ::TokenTag() == CBORTokenTag::ENVELOPE.
1421 : span<uint8_t> GetEnvelopeContents() const;
1422 :
1423 : private:
1424 : void ReadNextToken(bool enter_envelope);
1425 : void SetToken(CBORTokenTag token, std::ptrdiff_t token_byte_length);
1426 : void SetError(Error error);
1427 :
1428 : span<uint8_t> bytes_;
1429 : CBORTokenTag token_tag_;
1430 : struct Status status_;
1431 : std::ptrdiff_t token_byte_length_;
1432 : MajorType token_start_type_;
1433 : uint64_t token_start_internal_value_;
1434 : };
1435 :
1436 : // =============================================================================
1437 : // cbor::ParseCBOR - for receiving streaming parser events for CBOR messages
1438 : // =============================================================================
1439 :
1440 : // Parses a CBOR encoded message from |bytes|, sending events to
1441 : // |out|. If an error occurs, sends |out->HandleError|, and parsing stops.
1442 : // The client is responsible for discarding the already received information in
1443 : // that case.
1444 : void ParseCBOR(span<uint8_t> bytes, StreamingParserHandler* out);
1445 :
1446 : // =============================================================================
1447 : // cbor::AppendString8EntryToMap - for limited in-place editing of messages
1448 : // =============================================================================
1449 :
1450 : // Modifies the |cbor| message by appending a new key/value entry at the end
1451 : // of the map. Patches up the envelope size; Status.ok() iff successful.
1452 : // If not successful, |cbor| may be corrupted after this call.
1453 : Status AppendString8EntryToCBORMap(span<uint8_t> string8_key,
1454 : span<uint8_t> string8_value,
1455 : std::vector<uint8_t>* cbor);
1456 : Status AppendString8EntryToCBORMap(span<uint8_t> string8_key,
1457 : span<uint8_t> string8_value,
1458 : std::string* cbor);
1459 :
1460 : namespace internals { // Exposed only for writing tests.
1461 : int8_t ReadTokenStart(span<uint8_t> bytes,
1462 : cbor::MajorType* type,
1463 : uint64_t* value);
1464 :
1465 : void WriteTokenStart(cbor::MajorType type,
1466 : uint64_t value,
1467 : std::vector<uint8_t>* encoded);
1468 : void WriteTokenStart(cbor::MajorType type,
1469 : uint64_t value,
1470 : std::string* encoded);
1471 : } // namespace internals
1472 : } // namespace cbor
1473 :
1474 : namespace json {
1475 : // Client code must provide an instance. Implementation should delegate
1476 : // to whatever is appropriate.
1477 : class Platform {
1478 : public:
1479 : virtual ~Platform() = default;
1480 : // Parses |str| into |result|. Returns false iff there are
1481 : // leftover characters or parsing errors.
1482 : virtual bool StrToD(const char* str, double* result) const = 0;
1483 :
1484 : // Prints |value| in a format suitable for JSON.
1485 : virtual std::unique_ptr<char[]> DToStr(double value) const = 0;
1486 : };
1487 :
1488 : // =============================================================================
1489 : // json::NewJSONEncoder - for encoding streaming parser events as JSON
1490 : // =============================================================================
1491 :
1492 : // Returns a handler object which will write ascii characters to |out|.
1493 : // |status->ok()| will be false iff the handler routine HandleError() is called.
1494 : // In that case, we'll stop emitting output.
1495 : // Except for calling the HandleError routine at any time, the client
1496 : // code must call the Handle* methods in an order in which they'd occur
1497 : // in valid JSON; otherwise we may crash (the code uses assert).
1498 : std::unique_ptr<StreamingParserHandler> NewJSONEncoder(
1499 : const Platform* platform,
1500 : std::vector<uint8_t>* out,
1501 : Status* status);
1502 : std::unique_ptr<StreamingParserHandler> NewJSONEncoder(const Platform* platform,
1503 : std::string* out,
1504 : Status* status);
1505 :
1506 : // =============================================================================
1507 : // json::ParseJSON - for receiving streaming parser events for JSON
1508 : // =============================================================================
1509 :
1510 : void ParseJSON(const Platform& platform,
1511 : span<uint8_t> chars,
1512 : StreamingParserHandler* handler);
1513 : void ParseJSON(const Platform& platform,
1514 : span<uint16_t> chars,
1515 : StreamingParserHandler* handler);
1516 :
1517 : // =============================================================================
1518 : // json::ConvertCBORToJSON, json::ConvertJSONToCBOR - for transcoding
1519 : // =============================================================================
1520 : Status ConvertCBORToJSON(const Platform& platform,
1521 : span<uint8_t> cbor,
1522 : std::string* json);
1523 : Status ConvertCBORToJSON(const Platform& platform,
1524 : span<uint8_t> cbor,
1525 : std::vector<uint8_t>* json);
1526 : Status ConvertJSONToCBOR(const Platform& platform,
1527 : span<uint8_t> json,
1528 : std::vector<uint8_t>* cbor);
1529 : Status ConvertJSONToCBOR(const Platform& platform,
1530 : span<uint8_t> json,
1531 : std::string* cbor);
1532 : } // namespace json
1533 :
1534 : } // namespace v8_inspector
1535 : } // namespace protocol
1536 : #endif // !defined(v8_inspector_protocol_encoding_h)
|