GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/Release/obj/gen/src/node/inspector/protocol/Protocol.h Lines: 105 187 56.1 %
Date: 2019-05-05 22:32:45 Branches: 25 68 36.8 %

Line Branch Exec Source
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 node_inspector_protocol_ErrorSupport_h
8
#define node_inspector_protocol_ErrorSupport_h
9
10
#include "src/node/inspector/protocol/Forward.h"
11
12
namespace node {
13
namespace inspector {
14
namespace protocol {
15
16
class  ErrorSupport {
17
public:
18
    ErrorSupport();
19
    ~ErrorSupport();
20
21
    void push();
22
    void setName(const char*);
23
    void setName(const String&);
24
    void pop();
25
    void addError(const char*);
26
    void addError(const String&);
27
    bool hasErrors();
28
    String errors();
29
30
private:
31
    std::vector<String> m_path;
32
    std::vector<String> m_errors;
33
};
34
35
} // namespace node
36
} // namespace inspector
37
} // namespace protocol
38
39
#endif // !defined(node_inspector_protocol_ErrorSupport_h)
40
41
42
// This file is generated by Values_h.template.
43
44
// Copyright 2016 The Chromium Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style license that can be
46
// found in the LICENSE file.
47
48
#ifndef node_inspector_protocol_Values_h
49
#define node_inspector_protocol_Values_h
50
51
//#include "Allocator.h"
52
//#include "Forward.h"
53
54
namespace node {
55
namespace inspector {
56
namespace protocol {
57
58
class ListValue;
59
class DictionaryValue;
60
class Value;
61
62
class  Value : public Serializable {
63
    PROTOCOL_DISALLOW_COPY(Value);
64
public:
65
57911
    virtual ~Value() override { }
66
67
1
    static std::unique_ptr<Value> null()
68
    {
69
1
        return std::unique_ptr<Value>(new Value());
70
    }
71
72
    static std::unique_ptr<Value> parseBinary(const uint8_t* data, size_t size);
73
74
    enum ValueType {
75
        TypeNull = 0,
76
        TypeBoolean,
77
        TypeInteger,
78
        TypeDouble,
79
        TypeString,
80
        TypeBinary,
81
        TypeObject,
82
        TypeArray,
83
        TypeSerialized,
84
        TypeImported
85
    };
86
87
43058
    ValueType type() const { return m_type; }
88
89
    bool isNull() const { return m_type == TypeNull; }
90
91
    virtual bool asBoolean(bool* output) const;
92
    virtual bool asDouble(double* output) const;
93
    virtual bool asInteger(int* output) const;
94
    virtual bool asString(String* output) const;
95
    virtual bool asBinary(Binary* output) const;
96
97
    virtual void writeJSON(StringBuilder* output) const;
98
    virtual void writeBinary(std::vector<uint8_t>* bytes) const;
99
    virtual std::unique_ptr<Value> clone() const;
100
    String toJSONString() const;
101
    String serializeToJSON() override;
102
    std::vector<uint8_t> serializeToBinary() override;
103
104
protected:
105
1
    Value() : m_type(TypeNull) { }
106
57909
    explicit Value(ValueType type) : m_type(type) { }
107
108
private:
109
    friend class DictionaryValue;
110
    friend class ListValue;
111
112
    ValueType m_type;
113
};
114
115
47508
class  FundamentalValue : public Value {
116
public:
117
9364
    static std::unique_ptr<FundamentalValue> create(bool value)
118
    {
119
9364
        return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
120
    }
121
122
14390
    static std::unique_ptr<FundamentalValue> create(int value)
123
    {
124
14390
        return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
125
    }
126
127
    static std::unique_ptr<FundamentalValue> create(double value)
128
    {
129
        return std::unique_ptr<FundamentalValue>(new FundamentalValue(value));
130
    }
131
132
    bool asBoolean(bool* output) const override;
133
    bool asDouble(double* output) const override;
134
    bool asInteger(int* output) const override;
135
    void writeJSON(StringBuilder* output) const override;
136
    void writeBinary(std::vector<uint8_t>* bytes) const override;
137
    std::unique_ptr<Value> clone() const override;
138
139
private:
140
9364
    explicit FundamentalValue(bool value) : Value(TypeBoolean), m_boolValue(value) { }
141
14390
    explicit FundamentalValue(int value) : Value(TypeInteger), m_integerValue(value) { }
142
    explicit FundamentalValue(double value) : Value(TypeDouble), m_doubleValue(value) { }
143
144
    union {
145
        bool m_boolValue;
146
        double m_doubleValue;
147
        int m_integerValue;
148
    };
149
};
150
151
29418
class  StringValue : public Value {
152
public:
153
14709
    static std::unique_ptr<StringValue> create(const String& value)
154
    {
155
14709
        return std::unique_ptr<StringValue>(new StringValue(value));
156
    }
157
158
    static std::unique_ptr<StringValue> create(const char* value)
159
    {
160
        return std::unique_ptr<StringValue>(new StringValue(value));
161
    }
162
163
    bool asString(String* output) const override;
164
    void writeJSON(StringBuilder* output) const override;
165
    void writeBinary(std::vector<uint8_t>* bytes) const override;
166
    std::unique_ptr<Value> clone() const override;
167
168
private:
169
14709
    explicit StringValue(const String& value) : Value(TypeString), m_stringValue(value) { }
170
    explicit StringValue(const char* value) : Value(TypeString), m_stringValue(value) { }
171
172
    String m_stringValue;
173
};
174
175
class  BinaryValue : public Value {
176
public:
177
    static std::unique_ptr<BinaryValue> create(const Binary& value)
178
    {
179
        return std::unique_ptr<BinaryValue>(new BinaryValue(value));
180
    }
181
182
    bool asBinary(Binary* output) const override;
183
    void writeJSON(StringBuilder* output) const override;
184
    void writeBinary(std::vector<uint8_t>* bytes) const override;
185
    std::unique_ptr<Value> clone() const override;
186
187
private:
188
    explicit BinaryValue(const Binary& value) : Value(TypeBinary), m_binaryValue(value) { }
189
190
    Binary m_binaryValue;
191
};
192
193
232
class  SerializedValue : public Value {
194
public:
195
116
    static std::unique_ptr<SerializedValue> fromJSON(const String& value)
196
    {
197
116
        return std::unique_ptr<SerializedValue>(new SerializedValue(value));
198
    }
199
200
    static std::unique_ptr<SerializedValue> fromBinary(std::vector<uint8_t> value)
201
    {
202
        return std::unique_ptr<SerializedValue>(new SerializedValue(std::move(value)));
203
    }
204
205
    void writeJSON(StringBuilder* output) const override;
206
    void writeBinary(std::vector<uint8_t>* bytes) const override;
207
    std::unique_ptr<Value> clone() const override;
208
209
private:
210
116
    explicit SerializedValue(const String& json) : Value(TypeSerialized), m_serializedJSON(json) { }
211
    explicit SerializedValue(std::vector<uint8_t> binary) : Value(TypeSerialized), m_serializedBinary(std::move(binary)) { }
212
    SerializedValue(const String& json, const std::vector<uint8_t>& binary)
213
        : Value(TypeSerialized), m_serializedJSON(json), m_serializedBinary(binary) { }
214
    String m_serializedJSON;
215
    std::vector<uint8_t> m_serializedBinary;
216
};
217
218
class  DictionaryValue : public Value {
219
public:
220
    using Entry = std::pair<String, Value*>;
221
19317
    static std::unique_ptr<DictionaryValue> create()
222
    {
223
19317
        return std::unique_ptr<DictionaryValue>(new DictionaryValue());
224
    }
225
226
28671
    static DictionaryValue* cast(Value* value)
227
    {
228

28671
        if (!value || value->type() != TypeObject)
229
            return nullptr;
230
28671
        return static_cast<DictionaryValue*>(value);
231
    }
232
233
14337
    static std::unique_ptr<DictionaryValue> cast(std::unique_ptr<Value> value)
234
    {
235
14337
        return std::unique_ptr<DictionaryValue>(DictionaryValue::cast(value.release()));
236
    }
237
238
    void writeJSON(StringBuilder* output) const override;
239
    void writeBinary(std::vector<uint8_t>* bytes) const override;
240
    std::unique_ptr<Value> clone() const override;
241
242
    size_t size() const { return m_data.size(); }
243
244
    void setBoolean(const String& name, bool);
245
    void setInteger(const String& name, int);
246
    void setDouble(const String& name, double);
247
    void setString(const String& name, const String&);
248
    void setValue(const String& name, std::unique_ptr<Value>);
249
    void setObject(const String& name, std::unique_ptr<DictionaryValue>);
250
    void setArray(const String& name, std::unique_ptr<ListValue>);
251
252
    bool getBoolean(const String& name, bool* output) const;
253
    bool getInteger(const String& name, int* output) const;
254
    bool getDouble(const String& name, double* output) const;
255
    bool getString(const String& name, String* output) const;
256
257
    DictionaryValue* getObject(const String& name) const;
258
    ListValue* getArray(const String& name) const;
259
    Value* get(const String& name) const;
260
    Entry at(size_t index) const;
261
262
    bool booleanProperty(const String& name, bool defaultValue) const;
263
    int integerProperty(const String& name, int defaultValue) const;
264
    double doubleProperty(const String& name, double defaultValue) const;
265
    void remove(const String& name);
266
267
    ~DictionaryValue() override;
268
269
private:
270
    DictionaryValue();
271
    template<typename T>
272
43343
    void set(const String& key, std::unique_ptr<T>& value)
273
    {
274
        DCHECK(value);
275
43343
        bool isNew = m_data.find(key) == m_data.end();
276
43343
        m_data[key] = std::move(value);
277

43343
        if (isNew)
278
43343
            m_order.push_back(key);
279
43342
    }
280
281
    using Dictionary = std::unordered_map<String, std::unique_ptr<Value>>;
282
    Dictionary m_data;
283
    std::vector<String> m_order;
284
};
285
286
class  ListValue : public Value {
287
public:
288
13
    static std::unique_ptr<ListValue> create()
289
    {
290
13
        return std::unique_ptr<ListValue>(new ListValue());
291
    }
292
293
3
    static ListValue* cast(Value* value)
294
    {
295

3
        if (!value || value->type() != TypeArray)
296
            return nullptr;
297
3
        return static_cast<ListValue*>(value);
298
    }
299
300
    static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value)
301
    {
302
        return std::unique_ptr<ListValue>(ListValue::cast(value.release()));
303
    }
304
305
    ~ListValue() override;
306
307
    void writeJSON(StringBuilder* output) const override;
308
    void writeBinary(std::vector<uint8_t>* bytes) const override;
309
    std::unique_ptr<Value> clone() const override;
310
311
    void pushValue(std::unique_ptr<Value>);
312
313
    Value* at(size_t index);
314
6
    size_t size() const { return m_data.size(); }
315
316
private:
317
    ListValue();
318
    std::vector<std::unique_ptr<Value>> m_data;
319
};
320
321
void escapeLatinStringForJSON(const uint8_t* str, unsigned len, StringBuilder* dst);
322
void escapeWideStringForJSON(const uint16_t* str, unsigned len, StringBuilder* dst);
323
324
} // namespace node
325
} // namespace inspector
326
} // namespace protocol
327
328
#endif // node_inspector_protocol_Values_h
329
330
331
// This file is generated by Object_h.template.
332
333
// Copyright 2016 The Chromium Authors. All rights reserved.
334
// Use of this source code is governed by a BSD-style license that can be
335
// found in the LICENSE file.
336
337
#ifndef node_inspector_protocol_Object_h
338
#define node_inspector_protocol_Object_h
339
340
//#include "ErrorSupport.h"
341
//#include "Forward.h"
342
//#include "Values.h"
343
344
namespace node {
345
namespace inspector {
346
namespace protocol {
347
348
class  Object {
349
public:
350
    static std::unique_ptr<Object> fromValue(protocol::Value*, ErrorSupport*);
351
    explicit Object(std::unique_ptr<protocol::DictionaryValue>);
352
    ~Object();
353
354
    std::unique_ptr<protocol::DictionaryValue> toValue() const;
355
    std::unique_ptr<Object> clone() const;
356
private:
357
    std::unique_ptr<protocol::DictionaryValue> m_object;
358
};
359
360
} // namespace node
361
} // namespace inspector
362
} // namespace protocol
363
364
#endif // !defined(node_inspector_protocol_Object_h)
365
366
367
// This file is generated by ValueConversions_h.template.
368
369
// Copyright 2016 The Chromium Authors. All rights reserved.
370
// Use of this source code is governed by a BSD-style license that can be
371
// found in the LICENSE file.
372
373
#ifndef node_inspector_protocol_ValueConversions_h
374
#define node_inspector_protocol_ValueConversions_h
375
376
//#include "ErrorSupport.h"
377
//#include "Forward.h"
378
//#include "Values.h"
379
380
namespace node {
381
namespace inspector {
382
namespace protocol {
383
384
template<typename T>
385
struct ValueConversions {
386
6
    static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors)
387
    {
388
6
        return T::fromValue(value, errors);
389
    }
390
391
5
    static std::unique_ptr<protocol::Value> toValue(T* value)
392
    {
393
5
        return value->toValue();
394
    }
395
396
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value)
397
    {
398
        return value->toValue();
399
    }
400
};
401
402
template<>
403
struct ValueConversions<bool> {
404
3
    static bool fromValue(protocol::Value* value, ErrorSupport* errors)
405
    {
406
3
        bool result = false;
407
3
        bool success = value ? value->asBoolean(&result) : false;
408
3
        if (!success)
409
            errors->addError("boolean value expected");
410
3
        return result;
411
    }
412
413
4
    static std::unique_ptr<protocol::Value> toValue(bool value)
414
    {
415
4
        return FundamentalValue::create(value);
416
    }
417
};
418
419
template<>
420
struct ValueConversions<int> {
421
    static int fromValue(protocol::Value* value, ErrorSupport* errors)
422
    {
423
        int result = 0;
424
        bool success = value ? value->asInteger(&result) : false;
425
        if (!success)
426
            errors->addError("integer value expected");
427
        return result;
428
    }
429
430
    static std::unique_ptr<protocol::Value> toValue(int value)
431
    {
432
        return FundamentalValue::create(value);
433
    }
434
};
435
436
template<>
437
struct ValueConversions<double> {
438
    static double fromValue(protocol::Value* value, ErrorSupport* errors)
439
    {
440
        double result = 0;
441
        bool success = value ? value->asDouble(&result) : false;
442
        if (!success)
443
            errors->addError("double value expected");
444
        return result;
445
    }
446
447
    static std::unique_ptr<protocol::Value> toValue(double value)
448
    {
449
        return FundamentalValue::create(value);
450
    }
451
};
452
453
template<>
454
struct ValueConversions<String> {
455
15
    static String fromValue(protocol::Value* value, ErrorSupport* errors)
456
    {
457
15
        String result;
458
15
        bool success = value ? value->asString(&result) : false;
459
15
        if (!success)
460
            errors->addError("string value expected");
461
15
        return result;
462
    }
463
464
212
    static std::unique_ptr<protocol::Value> toValue(const String& value)
465
    {
466
212
        return StringValue::create(value);
467
    }
468
};
469
470
template<>
471
struct ValueConversions<Binary> {
472
    static Binary fromValue(protocol::Value* value, ErrorSupport* errors)
473
    {
474
        if (!value ||
475
            (value->type() != Value::TypeBinary && value->type() != Value::TypeString)) {
476
            errors->addError("Either string base64 or binary value expected");
477
            return Binary();
478
        }
479
        Binary binary;
480
        if (value->asBinary(&binary))
481
            return binary;
482
        String result;
483
        value->asString(&result);
484
        bool success;
485
        Binary out = Binary::fromBase64(result, &success);
486
        if (!success)
487
          errors->addError("base64 decoding error");
488
        return out;
489
    }
490
491
    static std::unique_ptr<protocol::Value> toValue(const Binary& value)
492
    {
493
        return BinaryValue::create(value);
494
    }
495
};
496
497
template<>
498
struct ValueConversions<Value> {
499
    static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors)
500
    {
501
        bool success = !!value;
502
        if (!success) {
503
            errors->addError("value expected");
504
            return nullptr;
505
        }
506
        return value->clone();
507
    }
508
509
    static std::unique_ptr<protocol::Value> toValue(Value* value)
510
    {
511
        return value->clone();
512
    }
513
514
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value)
515
    {
516
        return value->clone();
517
    }
518
};
519
520
template<>
521
struct ValueConversions<DictionaryValue> {
522
    static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors)
523
    {
524
        bool success = value && value->type() == protocol::Value::TypeObject;
525
        if (!success)
526
            errors->addError("object expected");
527
        return DictionaryValue::cast(value->clone());
528
    }
529
530
    static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value)
531
    {
532
        return value->clone();
533
    }
534
535
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value)
536
    {
537
        return value->clone();
538
    }
539
};
540
541
template<>
542
struct ValueConversions<ListValue> {
543
    static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors)
544
    {
545
        bool success = value && value->type() == protocol::Value::TypeArray;
546
        if (!success)
547
            errors->addError("list expected");
548
        return ListValue::cast(value->clone());
549
    }
550
551
    static std::unique_ptr<protocol::Value> toValue(ListValue* value)
552
    {
553
        return value->clone();
554
    }
555
556
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value)
557
    {
558
        return value->clone();
559
    }
560
};
561
562
} // namespace node
563
} // namespace inspector
564
} // namespace protocol
565
566
#endif // !defined(node_inspector_protocol_ValueConversions_h)
567
568
569
// This file is generated by Maybe_h.template.
570
571
// Copyright 2016 The Chromium Authors. All rights reserved.
572
// Use of this source code is governed by a BSD-style license that can be
573
// found in the LICENSE file.
574
575
#ifndef node_inspector_protocol_Maybe_h
576
#define node_inspector_protocol_Maybe_h
577
578
// This macro allows to test for the version of the GNU C++ compiler.
579
// Note that this also applies to compilers that masquerade as GCC,
580
// for example clang and the Intel C++ compiler for Linux.
581
// Use like:
582
//  #if IP_GNUC_PREREQ(4, 3, 1)
583
//   ...
584
//  #endif
585
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
586
#define IP_GNUC_PREREQ(major, minor, patchlevel)                      \
587
  ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= \
588
   ((major)*10000 + (minor)*100 + (patchlevel)))
589
#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
590
#define IP_GNUC_PREREQ(major, minor, patchlevel) \
591
  ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >=  \
592
   ((major)*10000 + (minor)*100 + (patchlevel)))
593
#else
594
#define IP_GNUC_PREREQ(major, minor, patchlevel) 0
595
#endif
596
597
#if defined(__mips64)
598
#define IP_TARGET_ARCH_MIPS64 1
599
#elif defined(__MIPSEB__) || defined(__MIPSEL__)
600
#define IP_TARGET_ARCH_MIPS 1
601
#endif
602
603
// Allowing the use of noexcept by removing the keyword on older compilers that
604
// do not support adding noexcept to default members.
605
#if ((IP_GNUC_PREREQ(4, 9, 0) && !defined(IP_TARGET_ARCH_MIPS) && \
606
      !defined(IP_TARGET_ARCH_MIPS64)) ||                         \
607
     (defined(__clang__) && __cplusplus > 201300L))
608
#define IP_NOEXCEPT noexcept
609
#else
610
#define IP_NOEXCEPT
611
#endif
612
613
//#include "Forward.h"
614
615
namespace node {
616
namespace inspector {
617
namespace protocol {
618
619
template<typename T>
620
class Maybe {
621
public:
622
    Maybe() : m_value() { }
623
    Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
624
    Maybe(Maybe&& other) IP_NOEXCEPT : m_value(std::move(other.m_value)) {}
625
    void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
626
    T* fromJust() const { DCHECK(m_value); return m_value.get(); }
627
    T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
628
    bool isJust() const { return !!m_value; }
629
    std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value); }
630
private:
631
    std::unique_ptr<T> m_value;
632
};
633
634
template<typename T>
635
3
class MaybeBase {
636
public:
637
3
    MaybeBase() : m_isJust(false) { }
638
    MaybeBase(T value) : m_isJust(true), m_value(value) { }
639
    MaybeBase(MaybeBase&& other) IP_NOEXCEPT
640
        : m_isJust(other.m_isJust),
641
          m_value(std::move(other.m_value)) {}
642
    void operator=(T value) { m_value = value; m_isJust = true; }
643
    T fromJust() const { DCHECK(m_isJust); return m_value; }
644
    T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
645
    bool isJust() const { return m_isJust; }
646
    T takeJust() { DCHECK(m_isJust); return m_value; }
647
648
protected:
649
    bool m_isJust;
650
    T m_value;
651
};
652
653
template<>
654
class Maybe<bool> : public MaybeBase<bool> {
655
public:
656
    Maybe() { m_value = false; }
657
    Maybe(bool value) : MaybeBase(value) { }
658
    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
659
    using MaybeBase::operator=;
660
};
661
662
template<>
663
class Maybe<int> : public MaybeBase<int> {
664
public:
665
    Maybe() { m_value = 0; }
666
    Maybe(int value) : MaybeBase(value) { }
667
    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
668
    using MaybeBase::operator=;
669
};
670
671
template<>
672
class Maybe<double> : public MaybeBase<double> {
673
public:
674
    Maybe() { m_value = 0; }
675
    Maybe(double value) : MaybeBase(value) { }
676
    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
677
    using MaybeBase::operator=;
678
};
679
680
template<>
681
3
class Maybe<String> : public MaybeBase<String> {
682
public:
683
3
    Maybe() { }
684
    Maybe(const String& value) : MaybeBase(value) { }
685
    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
686
    using MaybeBase::operator=;
687
};
688
689
template<>
690
class Maybe<Binary> : public MaybeBase<Binary> {
691
public:
692
    Maybe() { }
693
    Maybe(Binary value) : MaybeBase(value) { }
694
    Maybe(Maybe&& other) IP_NOEXCEPT : MaybeBase(std::move(other)) {}
695
    using MaybeBase::operator=;
696
};
697
698
} // namespace node
699
} // namespace inspector
700
} // namespace protocol
701
702
#undef IP_GNUC_PREREQ
703
#undef IP_TARGET_ARCH_MIPS64
704
#undef IP_TARGET_ARCH_MIPS
705
#undef IP_NOEXCEPT
706
707
#endif // !defined(node_inspector_protocol_Maybe_h)
708
709
710
// This file is generated by Array_h.template.
711
712
// Copyright 2016 The Chromium Authors. All rights reserved.
713
// Use of this source code is governed by a BSD-style license that can be
714
// found in the LICENSE file.
715
716
#ifndef node_inspector_protocol_Array_h
717
#define node_inspector_protocol_Array_h
718
719
//#include "ErrorSupport.h"
720
//#include "Forward.h"
721
//#include "ValueConversions.h"
722
//#include "Values.h"
723
724
namespace node {
725
namespace inspector {
726
namespace protocol {
727
728
template<typename T>
729
class Array {
730
public:
731
    static std::unique_ptr<Array<T>> create()
732
    {
733
        return std::unique_ptr<Array<T>>(new Array<T>());
734
    }
735
736
    static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors)
737
    {
738
        protocol::ListValue* array = ListValue::cast(value);
739
        if (!array) {
740
            errors->addError("array expected");
741
            return nullptr;
742
        }
743
        std::unique_ptr<Array<T>> result(new Array<T>());
744
        errors->push();
745
        for (size_t i = 0; i < array->size(); ++i) {
746
            errors->setName(StringUtil::fromInteger(i));
747
            std::unique_ptr<T> item = ValueConversions<T>::fromValue(array->at(i), errors);
748
            result->m_vector.push_back(std::move(item));
749
        }
750
        errors->pop();
751
        if (errors->hasErrors())
752
            return nullptr;
753
        return result;
754
    }
755
756
    void addItem(std::unique_ptr<T> value)
757
    {
758
        m_vector.push_back(std::move(value));
759
    }
760
761
    size_t length()
762
    {
763
        return m_vector.size();
764
    }
765
766
    T* get(size_t index)
767
    {
768
        return m_vector[index].get();
769
    }
770
771
    std::unique_ptr<protocol::ListValue> toValue()
772
    {
773
        std::unique_ptr<protocol::ListValue> result = ListValue::create();
774
        for (auto& item : m_vector)
775
            result->pushValue(ValueConversions<T>::toValue(item));
776
        return result;
777
    }
778
779
private:
780
    std::vector<std::unique_ptr<T>> m_vector;
781
};
782
783
template<typename T>
784
8
class ArrayBase {
785
public:
786
1
    static std::unique_ptr<Array<T>> create()
787
    {
788
1
        return std::unique_ptr<Array<T>>(new Array<T>());
789
    }
790
791
3
    static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors)
792
    {
793
3
        protocol::ListValue* array = ListValue::cast(value);
794
3
        if (!array) {
795
            errors->addError("array expected");
796
            return nullptr;
797
        }
798
3
        errors->push();
799
3
        std::unique_ptr<Array<T>> result(new Array<T>());
800
6
        for (size_t i = 0; i < array->size(); ++i) {
801
3
            errors->setName(StringUtil::fromInteger(i));
802
3
            T item = ValueConversions<T>::fromValue(array->at(i), errors);
803
3
            result->m_vector.push_back(item);
804
        }
805
3
        errors->pop();
806
3
        if (errors->hasErrors())
807
            return nullptr;
808
3
        return result;
809
    }
810
811
8
    void addItem(const T& value)
812
    {
813
8
        m_vector.push_back(value);
814
8
    }
815
816
4
    size_t length()
817
    {
818
4
        return m_vector.size();
819
    }
820
821
2
    T get(size_t index)
822
    {
823
2
        return m_vector[index];
824
    }
825
826
1
    std::unique_ptr<protocol::ListValue> toValue()
827
    {
828
1
        std::unique_ptr<protocol::ListValue> result = ListValue::create();
829
9
        for (auto& item : m_vector)
830
8
            result->pushValue(ValueConversions<T>::toValue(item));
831
1
        return result;
832
    }
833
834
private:
835
    std::vector<T> m_vector;
836
};
837
838
8
template<> class Array<String> : public ArrayBase<String> {};
839
template<> class Array<int> : public ArrayBase<int> {};
840
template<> class Array<double> : public ArrayBase<double> {};
841
template<> class Array<bool> : public ArrayBase<bool> {};
842
843
} // namespace node
844
} // namespace inspector
845
} // namespace protocol
846
847
#endif // !defined(node_inspector_protocol_Array_h)
848
849
850
// This file is generated by DispatcherBase_h.template.
851
852
// Copyright 2016 The Chromium Authors. All rights reserved.
853
// Use of this source code is governed by a BSD-style license that can be
854
// found in the LICENSE file.
855
856
#ifndef node_inspector_protocol_DispatcherBase_h
857
#define node_inspector_protocol_DispatcherBase_h
858
859
//#include "Forward.h"
860
//#include "ErrorSupport.h"
861
//#include "Values.h"
862
863
namespace node {
864
namespace inspector {
865
namespace protocol {
866
867
class WeakPtr;
868
869
17978
class  DispatchResponse {
870
public:
871
    enum Status {
872
        kSuccess = 0,
873
        kError = 1,
874
        kFallThrough = 2,
875
    };
876
877
    enum ErrorCode {
878
        kParseError = -32700,
879
        kInvalidRequest = -32600,
880
        kMethodNotFound = -32601,
881
        kInvalidParams = -32602,
882
        kInternalError = -32603,
883
        kServerError = -32000,
884
    };
885
886
35
    Status status() const { return m_status; }
887
1
    const String& errorMessage() const { return m_errorMessage; }
888
1
    ErrorCode errorCode() const { return m_errorCode; }
889
    bool isSuccess() const { return m_status == kSuccess; }
890
891
    static DispatchResponse OK();
892
    static DispatchResponse Error(const String&);
893
    static DispatchResponse InternalError();
894
    static DispatchResponse InvalidParams(const String&);
895
    static DispatchResponse FallThrough();
896
897
private:
898
    Status m_status;
899
    String m_errorMessage;
900
    ErrorCode m_errorCode;
901
};
902
903
class  DispatcherBase {
904
    PROTOCOL_DISALLOW_COPY(DispatcherBase);
905
public:
906
    static const char kInvalidParamsString[];
907
    class  WeakPtr {
908
    public:
909
        explicit WeakPtr(DispatcherBase*);
910
        ~WeakPtr();
911
34
        DispatcherBase* get() { return m_dispatcher; }
912
        void dispose() { m_dispatcher = nullptr; }
913
914
    private:
915
        DispatcherBase* m_dispatcher;
916
    };
917
918
    class  Callback {
919
    public:
920
        Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, const String& method, const ProtocolMessage& message);
921
        virtual ~Callback();
922
        void dispose();
923
924
    protected:
925
        void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response);
926
        void fallThroughIfActive();
927
928
    private:
929
        std::unique_ptr<WeakPtr> m_backendImpl;
930
        int m_callId;
931
        String m_method;
932
        ProtocolMessage m_message;
933
    };
934
935
    explicit DispatcherBase(FrontendChannel*);
936
    virtual ~DispatcherBase();
937
938
    virtual bool canDispatch(const String& method) = 0;
939
    virtual void dispatch(int callId, const String& method, const ProtocolMessage& rawMessage, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
940
    FrontendChannel* channel() { return m_frontendChannel; }
941
942
    void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
943
    void sendResponse(int callId, const DispatchResponse&);
944
945
    void reportProtocolError(int callId, DispatchResponse::ErrorCode, const String& errorMessage, ErrorSupport* errors);
946
    void clearFrontend();
947
948
    std::unique_ptr<WeakPtr> weakPtr();
949
950
private:
951
    FrontendChannel* m_frontendChannel;
952
    std::unordered_set<WeakPtr*> m_weakPtrs;
953
};
954
955
8972
class  UberDispatcher {
956
    PROTOCOL_DISALLOW_COPY(UberDispatcher);
957
public:
958
    explicit UberDispatcher(FrontendChannel*);
959
    void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
960
    void setupRedirects(const std::unordered_map<String, String>&);
961
    bool parseCommand(Value* message, int* callId, String* method);
962
    bool canDispatch(const String& method);
963
    void dispatch(int callId, const String& method, std::unique_ptr<Value> message, const ProtocolMessage& rawMessage);
964
19468
    FrontendChannel* channel() { return m_frontendChannel; }
965
    virtual ~UberDispatcher();
966
967
private:
968
    protocol::DispatcherBase* findDispatcher(const String& method);
969
    FrontendChannel* m_frontendChannel;
970
    std::unordered_map<String, String> m_redirects;
971
    std::unordered_map<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
972
};
973
974
class InternalResponse : public Serializable {
975
    PROTOCOL_DISALLOW_COPY(InternalResponse);
976
public:
977
    static std::unique_ptr<InternalResponse> createResponse(int callId, std::unique_ptr<Serializable> params);
978
    static std::unique_ptr<InternalResponse> createNotification(const String& notification, std::unique_ptr<Serializable> params = nullptr);
979
980
    String serializeToJSON() override;
981
    std::vector<uint8_t> serializeToBinary() override;
982
983
232
    ~InternalResponse() override {}
984
985
private:
986
    InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params);
987
988
    int m_callId;
989
    String m_notification;
990
    std::unique_ptr<Serializable> m_params;
991
};
992
993
class InternalRawNotification : public Serializable {
994
public:
995
4
    static std::unique_ptr<InternalRawNotification> fromJSON(String notification)
996
    {
997
4
        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
998
    }
999
1000
    static std::unique_ptr<InternalRawNotification> fromBinary(std::vector<uint8_t> notification)
1001
    {
1002
        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(std::move(notification)));
1003
    }
1004
1005
8
    ~InternalRawNotification() override {}
1006
1007
4
    String serializeToJSON() override
1008
    {
1009
4
        return std::move(m_jsonNotification);
1010
    }
1011
1012
    std::vector<uint8_t> serializeToBinary() override
1013
    {
1014
        return std::move(m_binaryNotification);
1015
    }
1016
1017
private:
1018
4
  explicit InternalRawNotification(String notification)
1019
4
    : m_jsonNotification(std::move(notification)) { }
1020
  explicit InternalRawNotification(std::vector<uint8_t> notification)
1021
    : m_binaryNotification(std::move(notification)) { }
1022
1023
  String m_jsonNotification;
1024
  std::vector<uint8_t> m_binaryNotification;
1025
};
1026
1027
} // namespace node
1028
} // namespace inspector
1029
} // namespace protocol
1030
1031
#endif // !defined(node_inspector_protocol_DispatcherBase_h)
1032
1033
1034
// This file is generated by Parser_h.template.
1035
1036
// Copyright 2016 The Chromium Authors. All rights reserved.
1037
// Use of this source code is governed by a BSD-style license that can be
1038
// found in the LICENSE file.
1039
1040
#ifndef node_inspector_protocol_Parser_h
1041
#define node_inspector_protocol_Parser_h
1042
1043
//#include "Forward.h"
1044
//#include "Values.h"
1045
1046
namespace node {
1047
namespace inspector {
1048
namespace protocol {
1049
1050
 std::unique_ptr<Value> parseJSONCharacters(const uint8_t*, unsigned);
1051
 std::unique_ptr<Value> parseJSONCharacters(const uint16_t*, unsigned);
1052
1053
} // namespace node
1054
} // namespace inspector
1055
} // namespace protocol
1056
1057
#endif // !defined(node_inspector_protocol_Parser_h)
1058
1059
1060
// Generated by lib/CBOR_h.template.
1061
1062
// Copyright 2019 The Chromium Authors. All rights reserved.
1063
// Use of this source code is governed by a BSD-style license that can be
1064
// found in the LICENSE file.
1065
1066
#ifndef node_inspector_protocol_CBOR_h
1067
#define node_inspector_protocol_CBOR_h
1068
1069
#include <cstddef>
1070
#include <cstdint>
1071
#include <memory>
1072
#include <vector>
1073
1074
namespace node {
1075
namespace inspector {
1076
namespace protocol {
1077
1078
// ===== encoding/status.h =====
1079
1080
// Error codes.
1081
enum class Error {
1082
  OK = 0,
1083
  // JSON parsing errors - json_parser.{h,cc}.
1084
  JSON_PARSER_UNPROCESSED_INPUT_REMAINS = 0x01,
1085
  JSON_PARSER_STACK_LIMIT_EXCEEDED = 0x02,
1086
  JSON_PARSER_NO_INPUT = 0x03,
1087
  JSON_PARSER_INVALID_TOKEN = 0x04,
1088
  JSON_PARSER_INVALID_NUMBER = 0x05,
1089
  JSON_PARSER_INVALID_STRING = 0x06,
1090
  JSON_PARSER_UNEXPECTED_ARRAY_END = 0x07,
1091
  JSON_PARSER_COMMA_OR_ARRAY_END_EXPECTED = 0x08,
1092
  JSON_PARSER_STRING_LITERAL_EXPECTED = 0x09,
1093
  JSON_PARSER_COLON_EXPECTED = 0x0a,
1094
  JSON_PARSER_UNEXPECTED_OBJECT_END = 0x0b,
1095
  JSON_PARSER_COMMA_OR_OBJECT_END_EXPECTED = 0x0c,
1096
  JSON_PARSER_VALUE_EXPECTED = 0x0d,
1097
1098
  CBOR_INVALID_INT32 = 0x0e,
1099
  CBOR_INVALID_DOUBLE = 0x0f,
1100
  CBOR_INVALID_ENVELOPE = 0x10,
1101
  CBOR_INVALID_STRING8 = 0x11,
1102
  CBOR_INVALID_STRING16 = 0x12,
1103
  CBOR_INVALID_BINARY = 0x13,
1104
  CBOR_UNSUPPORTED_VALUE = 0x14,
1105
  CBOR_NO_INPUT = 0x15,
1106
  CBOR_INVALID_START_BYTE = 0x16,
1107
  CBOR_UNEXPECTED_EOF_EXPECTED_VALUE = 0x17,
1108
  CBOR_UNEXPECTED_EOF_IN_ARRAY = 0x18,
1109
  CBOR_UNEXPECTED_EOF_IN_MAP = 0x19,
1110
  CBOR_INVALID_MAP_KEY = 0x1a,
1111
  CBOR_STACK_LIMIT_EXCEEDED = 0x1b,
1112
  CBOR_STRING8_MUST_BE_7BIT = 0x1c,
1113
  CBOR_TRAILING_JUNK = 0x1d,
1114
  CBOR_MAP_START_EXPECTED = 0x1e,
1115
};
1116
1117
// A status value with position that can be copied. The default status
1118
// is OK. Usually, error status values should come with a valid position.
1119
struct Status {
1120
  static constexpr std::ptrdiff_t npos() { return -1; }
1121
1122
  bool ok() const { return error == Error::OK; }
1123
1124
  Error error = Error::OK;
1125
  std::ptrdiff_t pos = npos();
1126
  Status(Error error, std::ptrdiff_t pos) : error(error), pos(pos) {}
1127
  Status() = default;
1128
};
1129
1130
// ===== encoding/span.h =====
1131
1132
// This template is similar to std::span, which will be included in C++20.  Like
1133
// std::span it uses ptrdiff_t, which is signed (and thus a bit annoying
1134
// sometimes when comparing with size_t), but other than this it's much simpler.
1135
template <typename T>
1136
class span {
1137
 public:
1138
  using index_type = std::ptrdiff_t;
1139
1140
  span() : data_(nullptr), size_(0) {}
1141
  span(const T* data, index_type size) : data_(data), size_(size) {}
1142
1143
  const T* data() const { return data_; }
1144
1145
  const T* begin() const { return data_; }
1146
  const T* end() const { return data_ + size_; }
1147
1148
  const T& operator[](index_type idx) const { return data_[idx]; }
1149
1150
  span<T> subspan(index_type offset, index_type count) const {
1151
    return span(data_ + offset, count);
1152
  }
1153
1154
  span<T> subspan(index_type offset) const {
1155
    return span(data_ + offset, size_ - offset);
1156
  }
1157
1158
  bool empty() const { return size_ == 0; }
1159
1160
  index_type size() const { return size_; }
1161
  index_type size_bytes() const { return size_ * sizeof(T); }
1162
1163
 private:
1164
  const T* data_;
1165
  index_type size_;
1166
};
1167
1168
// ===== encoding/json_parser_handler.h =====
1169
1170
// Handler interface for JSON parser events. See also json_parser.h.
1171
class JSONParserHandler {
1172
 public:
1173
  virtual ~JSONParserHandler() = default;
1174
  virtual void HandleObjectBegin() = 0;
1175
  virtual void HandleObjectEnd() = 0;
1176
  virtual void HandleArrayBegin() = 0;
1177
  virtual void HandleArrayEnd() = 0;
1178
  // TODO(johannes): Support utf8 (requires utf16->utf8 conversion
1179
  // internally, including handling mismatched surrogate pairs).
1180
  virtual void HandleString16(std::vector<uint16_t> chars) = 0;
1181
  virtual void HandleBinary(std::vector<uint8_t> bytes) = 0;
1182
  virtual void HandleDouble(double value) = 0;
1183
  virtual void HandleInt32(int32_t value) = 0;
1184
  virtual void HandleBool(bool value) = 0;
1185
  virtual void HandleNull() = 0;
1186
1187
  // The parser may send one error even after other events have already
1188
  // been received. Client code is reponsible to then discard the
1189
  // already processed events.
1190
  // |error| must be an eror, as in, |error.is_ok()| can't be true.
1191
  virtual void HandleError(Status error) = 0;
1192
};
1193
1194
// ===== encoding/cbor_internals.h =====
1195
1196
namespace cbor {
1197
enum class MajorType;
1198
}
1199
1200
namespace cbor_internals {
1201
1202
// Reads the start of a token with definitive size from |bytes|.
1203
// |type| is the major type as specified in RFC 7049 Section 2.1.
1204
// |value| is the payload (e.g. for MajorType::UNSIGNED) or is the size
1205
// (e.g. for BYTE_STRING).
1206
// If successful, returns the number of bytes read. Otherwise returns -1.
1207
int8_t ReadTokenStart(span<uint8_t> bytes, cbor::MajorType* type,
1208
                      uint64_t* value);
1209
1210
// Writes the start of a token with |type|. The |value| may indicate the size,
1211
// or it may be the payload if the value is an unsigned integer.
1212
void WriteTokenStart(cbor::MajorType type, uint64_t value,
1213
                     std::vector<uint8_t>* encoded);
1214
}  // namespace cbor_internals
1215
1216
// ===== encoding/cbor.h =====
1217
1218
1219
namespace cbor {
1220
1221
// The major types from RFC 7049 Section 2.1.
1222
enum class MajorType {
1223
  UNSIGNED = 0,
1224
  NEGATIVE = 1,
1225
  BYTE_STRING = 2,
1226
  STRING = 3,
1227
  ARRAY = 4,
1228
  MAP = 5,
1229
  TAG = 6,
1230
  SIMPLE_VALUE = 7
1231
};
1232
1233
// Indicates the number of bits the "initial byte" needs to be shifted to the
1234
// right after applying |kMajorTypeMask| to produce the major type in the
1235
// lowermost bits.
1236
static constexpr uint8_t kMajorTypeBitShift = 5u;
1237
// Mask selecting the low-order 5 bits of the "initial byte", which is where
1238
// the additional information is encoded.
1239
static constexpr uint8_t kAdditionalInformationMask = 0x1f;
1240
// Mask selecting the high-order 3 bits of the "initial byte", which indicates
1241
// the major type of the encoded value.
1242
static constexpr uint8_t kMajorTypeMask = 0xe0;
1243
// Indicates the integer is in the following byte.
1244
static constexpr uint8_t kAdditionalInformation1Byte = 24u;
1245
// Indicates the integer is in the next 2 bytes.
1246
static constexpr uint8_t kAdditionalInformation2Bytes = 25u;
1247
// Indicates the integer is in the next 4 bytes.
1248
static constexpr uint8_t kAdditionalInformation4Bytes = 26u;
1249
// Indicates the integer is in the next 8 bytes.
1250
static constexpr uint8_t kAdditionalInformation8Bytes = 27u;
1251
1252
// Encodes the initial byte, consisting of the |type| in the first 3 bits
1253
// followed by 5 bits of |additional_info|.
1254
constexpr uint8_t EncodeInitialByte(MajorType type, uint8_t additional_info) {
1255
  return (static_cast<uint8_t>(type) << kMajorTypeBitShift) |
1256
         (additional_info & kAdditionalInformationMask);
1257
}
1258
1259
// TAG 24 indicates that what follows is a byte string which is
1260
// encoded in CBOR format. We use this as a wrapper for
1261
// maps and arrays, allowing us to skip them, because the
1262
// byte string carries its size (byte length).
1263
// https://tools.ietf.org/html/rfc7049#section-2.4.4.1
1264
static constexpr uint8_t kInitialByteForEnvelope =
1265
    EncodeInitialByte(MajorType::TAG, 24);
1266
// The initial byte for a byte string with at most 2^32 bytes
1267
// of payload. This is used for envelope encoding, even if
1268
// the byte string is shorter.
1269
static constexpr uint8_t kInitialByteFor32BitLengthByteString =
1270
    EncodeInitialByte(MajorType::BYTE_STRING, 26);
1271
1272
// See RFC 7049 Section 2.2.1, indefinite length arrays / maps have additional
1273
// info = 31.
1274
static constexpr uint8_t kInitialByteIndefiniteLengthArray =
1275
    EncodeInitialByte(MajorType::ARRAY, 31);
1276
static constexpr uint8_t kInitialByteIndefiniteLengthMap =
1277
    EncodeInitialByte(MajorType::MAP, 31);
1278
// See RFC 7049 Section 2.3, Table 1; this is used for finishing indefinite
1279
// length maps / arrays.
1280
static constexpr uint8_t kStopByte =
1281
    EncodeInitialByte(MajorType::SIMPLE_VALUE, 31);
1282
1283
}  // namespace cbor
1284
1285
// The binary encoding for the inspector protocol follows the CBOR specification
1286
// (RFC 7049). Additional constraints:
1287
// - Only indefinite length maps and arrays are supported.
1288
// - Maps and arrays are wrapped with an envelope, that is, a
1289
//   CBOR tag with value 24 followed by a byte string specifying
1290
//   the byte length of the enclosed map / array. The byte string
1291
//   must use a 32 bit wide length.
1292
// - At the top level, a message must be an indefinite length map
1293
//   wrapped by an envelope.
1294
// - Maximal size for messages is 2^32 (4 GB).
1295
// - For scalars, we support only the int32_t range, encoded as
1296
//   UNSIGNED/NEGATIVE (major types 0 / 1).
1297
// - UTF16 strings, including with unbalanced surrogate pairs, are encoded
1298
//   as CBOR BYTE_STRING (major type 2). For such strings, the number of
1299
//   bytes encoded must be even.
1300
// - UTF8 strings (major type 3) may only have ASCII characters
1301
//   (7 bit US-ASCII).
1302
// - Arbitrary byte arrays, in the inspector protocol called 'binary',
1303
//   are encoded as BYTE_STRING (major type 2), prefixed with a byte
1304
//   indicating base64 when rendered as JSON.
1305
1306
// Encodes |value| as |UNSIGNED| (major type 0) iff >= 0, or |NEGATIVE|
1307
// (major type 1) iff < 0.
1308
void EncodeInt32(int32_t value, std::vector<uint8_t>* out);
1309
1310
// Encodes a UTF16 string as a BYTE_STRING (major type 2). Each utf16
1311
// character in |in| is emitted with most significant byte first,
1312
// appending to |out|.
1313
void EncodeString16(span<uint16_t> in, std::vector<uint8_t>* out);
1314
1315
// Encodes a UTF8 string |in| as STRING (major type 3).
1316
void EncodeString8(span<uint8_t> in, std::vector<uint8_t>* out);
1317
1318
// Encodes arbitrary binary data in |in| as a BYTE_STRING (major type 2) with
1319
// definitive length, prefixed with tag 22 indicating expected conversion to
1320
// base64 (see RFC 7049, Table 3 and Section 2.4.4.2).
1321
void EncodeBinary(span<uint8_t> in, std::vector<uint8_t>* out);
1322
1323
// Encodes / decodes a double as Major type 7 (SIMPLE_VALUE),
1324
// with additional info = 27, followed by 8 bytes in big endian.
1325
void EncodeDouble(double value, std::vector<uint8_t>* out);
1326
1327
// Some constants for CBOR tokens that only take a single byte on the wire.
1328
uint8_t EncodeTrue();
1329
uint8_t EncodeFalse();
1330
uint8_t EncodeNull();
1331
uint8_t EncodeIndefiniteLengthArrayStart();
1332
uint8_t EncodeIndefiniteLengthMapStart();
1333
uint8_t EncodeStop();
1334
1335
// An envelope indicates the byte length of a wrapped item.
1336
// We use this for maps and array, which allows the decoder
1337
// to skip such (nested) values whole sale.
1338
// It's implemented as a CBOR tag (major type 6) with additional
1339
// info = 24, followed by a byte string with a 32 bit length value;
1340
// so the maximal structure that we can wrap is 2^32 bits long.
1341
// See also: https://tools.ietf.org/html/rfc7049#section-2.4.4.1
1342
class EnvelopeEncoder {
1343
 public:
1344
  // Emits the envelope start bytes and records the position for the
1345
  // byte size in |byte_size_pos_|. Also emits empty bytes for the
1346
  // byte sisze so that encoding can continue.
1347
  void EncodeStart(std::vector<uint8_t>* out);
1348
  // This records the current size in |out| at position byte_size_pos_.
1349
  // Returns true iff successful.
1350
  bool EncodeStop(std::vector<uint8_t>* out);
1351
1352
 private:
1353
  std::size_t byte_size_pos_ = 0;
1354
};
1355
1356
// This can be used to convert from JSON to CBOR, by passing the
1357
// return value to the routines in json_parser.h.  The handler will encode into
1358
// |out|, and iff an error occurs it will set |status| to an error and clear
1359
// |out|. Otherwise, |status.ok()| will be |true|.
1360
std::unique_ptr<JSONParserHandler> NewJSONToCBOREncoder(
1361
    std::vector<uint8_t>* out, Status* status);
1362
1363
// Parses a CBOR encoded message from |bytes|, sending JSON events to
1364
// |json_out|. If an error occurs, sends |out->HandleError|, and parsing stops.
1365
// The client is responsible for discarding the already received information in
1366
// that case.
1367
void ParseCBOR(span<uint8_t> bytes, JSONParserHandler* json_out);
1368
1369
// Tags for the tokens within a CBOR message that CBORStream understands.
1370
// Note that this is not the same terminology as the CBOR spec (RFC 7049),
1371
// but rather, our adaptation. For instance, we lump unsigned and signed
1372
// major type into INT32 here (and disallow values outside the int32_t range).
1373
enum class CBORTokenTag {
1374
  // Encountered an error in the structure of the message. Consult
1375
  // status() for details.
1376
  ERROR_VALUE,
1377
  // Booleans and NULL.
1378
  TRUE_VALUE,
1379
  FALSE_VALUE,
1380
  NULL_VALUE,
1381
  // An int32_t (signed 32 bit integer).
1382
  INT32,
1383
  // A double (64 bit floating point).
1384
  DOUBLE,
1385
  // A UTF8 string.
1386
  STRING8,
1387
  // A UTF16 string.
1388
  STRING16,
1389
  // A binary string.
1390
  BINARY,
1391
  // Starts an indefinite length map; after the map start we expect
1392
  // alternating keys and values, followed by STOP.
1393
  MAP_START,
1394
  // Starts an indefinite length array; after the array start we
1395
  // expect values, followed by STOP.
1396
  ARRAY_START,
1397
  // Ends a map or an array.
1398
  STOP,
1399
  // An envelope indicator, wrapping a map or array.
1400
  // Internally this carries the byte length of the wrapped
1401
  // map or array. While CBORTokenizer::Next() will read / skip the entire
1402
  // envelope, CBORTokenizer::EnterEnvelope() reads the tokens
1403
  // inside of it.
1404
  ENVELOPE,
1405
  // We've reached the end there is nothing else to read.
1406
  DONE,
1407
};
1408
1409
// CBORTokenizer segments a CBOR message, presenting the tokens therein as
1410
// numbers, strings, etc. This is not a complete CBOR parser, but makes it much
1411
// easier to implement one (e.g. ParseCBOR, above). It can also be used to parse
1412
// messages partially.
1413
class CBORTokenizer {
1414
 public:
1415
  explicit CBORTokenizer(span<uint8_t> bytes);
1416
  ~CBORTokenizer();
1417
1418
  // Identifies the current token that we're looking at,
1419
  // or ERROR_VALUE (in which ase ::Status() has details)
1420
  // or DONE (if we're past the last token).
1421
  CBORTokenTag TokenTag() const;
1422
1423
  // Advances to the next token.
1424
  void Next();
1425
  // Can only be called if TokenTag() == CBORTokenTag::ENVELOPE.
1426
  // While Next() would skip past the entire envelope / what it's
1427
  // wrapping, EnterEnvelope positions the cursor inside of the envelope,
1428
  // letting the client explore the nested structure.
1429
  void EnterEnvelope();
1430
1431
  // If TokenTag() is CBORTokenTag::ERROR_VALUE, then Status().error describes
1432
  // the error more precisely; otherwise it'll be set to Error::OK.
1433
  // In either case, Status().pos is the current position.
1434
  struct Status Status() const;
1435
1436
  // The following methods retrieve the token values. They can only
1437
  // be called if TokenTag() matches.
1438
1439
  // To be called only if ::TokenTag() == CBORTokenTag::INT32.
1440
  int32_t GetInt32() const;
1441
1442
  // To be called only if ::TokenTag() == CBORTokenTag::DOUBLE.
1443
  double GetDouble() const;
1444
1445
  // To be called only if ::TokenTag() == CBORTokenTag::STRING8.
1446
  span<uint8_t> GetString8() const;
1447
1448
  // Wire representation for STRING16 is low byte first (little endian).
1449
  // To be called only if ::TokenTag() == CBORTokenTag::STRING16.
1450
  span<uint8_t> GetString16WireRep() const;
1451
1452
  // To be called only if ::TokenTag() == CBORTokenTag::BINARY.
1453
  span<uint8_t> GetBinary() const;
1454
1455
 private:
1456
  void ReadNextToken(bool enter_envelope);
1457
  void SetToken(CBORTokenTag token, std::ptrdiff_t token_byte_length);
1458
  void SetError(Error error);
1459
1460
  span<uint8_t> bytes_;
1461
  CBORTokenTag token_tag_;
1462
  struct Status status_;
1463
  std::ptrdiff_t token_byte_length_;
1464
  cbor::MajorType token_start_type_;
1465
  uint64_t token_start_internal_value_;
1466
};
1467
1468
void DumpCBOR(span<uint8_t> cbor);
1469
1470
1471
} // namespace node
1472
} // namespace inspector
1473
} // namespace protocol
1474
#endif // !defined(node_inspector_protocol_CBOR_h)