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: 29 148 19.6 %
Date: 2019-02-01 22:03:38 Branches: 8 64 12.5 %

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

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

1467
        if (isNew)
284
1467
            m_order.push_back(key);
285
1467
    }
286
287
    using Dictionary = protocol::HashMap<String, std::unique_ptr<Value>>;
288
    Dictionary m_data;
289
    std::vector<String> m_order;
290
};
291
292
class  ListValue : public Value {
293
public:
294
    static std::unique_ptr<ListValue> create()
295
    {
296
        return std::unique_ptr<ListValue>(new ListValue());
297
    }
298
299
    static ListValue* cast(Value* value)
300
    {
301
        if (!value || value->type() != TypeArray)
302
            return nullptr;
303
        return static_cast<ListValue*>(value);
304
    }
305
306
    static std::unique_ptr<ListValue> cast(std::unique_ptr<Value> value)
307
    {
308
        return std::unique_ptr<ListValue>(ListValue::cast(value.release()));
309
    }
310
311
    ~ListValue() override;
312
313
    void writeJSON(StringBuilder* output) const override;
314
    std::unique_ptr<Value> clone() const override;
315
316
    void pushValue(std::unique_ptr<Value>);
317
318
    Value* at(size_t index);
319
    size_t size() const { return m_data.size(); }
320
321
private:
322
    ListValue();
323
    std::vector<std::unique_ptr<Value>> m_data;
324
};
325
326
void escapeLatinStringForJSON(const uint8_t* str, unsigned len, StringBuilder* dst);
327
void escapeWideStringForJSON(const uint16_t* str, unsigned len, StringBuilder* dst);
328
329
} // namespace node
330
} // namespace inspector
331
} // namespace protocol
332
333
#endif // node_inspector_protocol_Values_h
334
335
336
// Copyright 2016 The Chromium Authors. All rights reserved.
337
// Use of this source code is governed by a BSD-style license that can be
338
// found in the LICENSE file.
339
340
#ifndef node_inspector_protocol_Object_h
341
#define node_inspector_protocol_Object_h
342
343
//#include "ErrorSupport.h"
344
//#include "Forward.h"
345
//#include "Values.h"
346
347
namespace node {
348
namespace inspector {
349
namespace protocol {
350
351
class  Object {
352
public:
353
    static std::unique_ptr<Object> fromValue(protocol::Value*, ErrorSupport*);
354
    ~Object();
355
356
    std::unique_ptr<protocol::DictionaryValue> toValue() const;
357
    std::unique_ptr<Object> clone() const;
358
private:
359
    explicit Object(std::unique_ptr<protocol::DictionaryValue>);
360
    std::unique_ptr<protocol::DictionaryValue> m_object;
361
};
362
363
} // namespace node
364
} // namespace inspector
365
} // namespace protocol
366
367
#endif // !defined(node_inspector_protocol_Object_h)
368
369
370
// Copyright 2016 The Chromium Authors. All rights reserved.
371
// Use of this source code is governed by a BSD-style license that can be
372
// found in the LICENSE file.
373
374
#ifndef node_inspector_protocol_ValueConversions_h
375
#define node_inspector_protocol_ValueConversions_h
376
377
//#include "ErrorSupport.h"
378
//#include "Forward.h"
379
//#include "Values.h"
380
381
namespace node {
382
namespace inspector {
383
namespace protocol {
384
385
template<typename T>
386
struct ValueConversions {
387
    static std::unique_ptr<T> fromValue(protocol::Value* value, ErrorSupport* errors)
388
    {
389
        return T::fromValue(value, errors);
390
    }
391
392
    static std::unique_ptr<protocol::Value> toValue(T* value)
393
    {
394
        return value->toValue();
395
    }
396
397
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<T>& value)
398
    {
399
        return value->toValue();
400
    }
401
};
402
403
template<>
404
struct ValueConversions<bool> {
405
    static bool fromValue(protocol::Value* value, ErrorSupport* errors)
406
    {
407
        bool result = false;
408
        bool success = value ? value->asBoolean(&result) : false;
409
        if (!success)
410
            errors->addError("boolean value expected");
411
        return result;
412
    }
413
414
    static std::unique_ptr<protocol::Value> toValue(bool value)
415
    {
416
        return FundamentalValue::create(value);
417
    }
418
};
419
420
template<>
421
struct ValueConversions<int> {
422
    static int fromValue(protocol::Value* value, ErrorSupport* errors)
423
    {
424
        int result = 0;
425
        bool success = value ? value->asInteger(&result) : false;
426
        if (!success)
427
            errors->addError("integer value expected");
428
        return result;
429
    }
430
431
    static std::unique_ptr<protocol::Value> toValue(int value)
432
    {
433
        return FundamentalValue::create(value);
434
    }
435
};
436
437
template<>
438
struct ValueConversions<double> {
439
    static double fromValue(protocol::Value* value, ErrorSupport* errors)
440
    {
441
        double result = 0;
442
        bool success = value ? value->asDouble(&result) : false;
443
        if (!success)
444
            errors->addError("double value expected");
445
        return result;
446
    }
447
448
    static std::unique_ptr<protocol::Value> toValue(double value)
449
    {
450
        return FundamentalValue::create(value);
451
    }
452
};
453
454
template<>
455
struct ValueConversions<String> {
456
    static String fromValue(protocol::Value* value, ErrorSupport* errors)
457
    {
458
        String result;
459
        bool success = value ? value->asString(&result) : false;
460
        if (!success)
461
            errors->addError("string value expected");
462
        return result;
463
    }
464
465
    static std::unique_ptr<protocol::Value> toValue(const String& value)
466
    {
467
        return StringValue::create(value);
468
    }
469
};
470
471
template<>
472
struct ValueConversions<Value> {
473
    static std::unique_ptr<Value> fromValue(protocol::Value* value, ErrorSupport* errors)
474
    {
475
        bool success = !!value;
476
        if (!success) {
477
            errors->addError("value expected");
478
            return nullptr;
479
        }
480
        return value->clone();
481
    }
482
483
    static std::unique_ptr<protocol::Value> toValue(Value* value)
484
    {
485
        return value->clone();
486
    }
487
488
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<Value>& value)
489
    {
490
        return value->clone();
491
    }
492
};
493
494
template<>
495
struct ValueConversions<DictionaryValue> {
496
    static std::unique_ptr<DictionaryValue> fromValue(protocol::Value* value, ErrorSupport* errors)
497
    {
498
        bool success = value && value->type() == protocol::Value::TypeObject;
499
        if (!success)
500
            errors->addError("object expected");
501
        return DictionaryValue::cast(value->clone());
502
    }
503
504
    static std::unique_ptr<protocol::Value> toValue(DictionaryValue* value)
505
    {
506
        return value->clone();
507
    }
508
509
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<DictionaryValue>& value)
510
    {
511
        return value->clone();
512
    }
513
};
514
515
template<>
516
struct ValueConversions<ListValue> {
517
    static std::unique_ptr<ListValue> fromValue(protocol::Value* value, ErrorSupport* errors)
518
    {
519
        bool success = value && value->type() == protocol::Value::TypeArray;
520
        if (!success)
521
            errors->addError("list expected");
522
        return ListValue::cast(value->clone());
523
    }
524
525
    static std::unique_ptr<protocol::Value> toValue(ListValue* value)
526
    {
527
        return value->clone();
528
    }
529
530
    static std::unique_ptr<protocol::Value> toValue(const std::unique_ptr<ListValue>& value)
531
    {
532
        return value->clone();
533
    }
534
};
535
536
} // namespace node
537
} // namespace inspector
538
} // namespace protocol
539
540
#endif // !defined(node_inspector_protocol_ValueConversions_h)
541
542
543
// Copyright 2016 The Chromium Authors. All rights reserved.
544
// Use of this source code is governed by a BSD-style license that can be
545
// found in the LICENSE file.
546
547
#ifndef node_inspector_protocol_Maybe_h
548
#define node_inspector_protocol_Maybe_h
549
550
//#include "Forward.h"
551
552
namespace node {
553
namespace inspector {
554
namespace protocol {
555
556
template<typename T>
557
class Maybe {
558
public:
559
    Maybe() : m_value() { }
560
    Maybe(std::unique_ptr<T> value) : m_value(std::move(value)) { }
561
    Maybe(Maybe&& other) : m_value(std::move(other.m_value)) { }
562
    void operator=(std::unique_ptr<T> value) { m_value = std::move(value); }
563
    T* fromJust() const { DCHECK(m_value); return m_value.get(); }
564
    T* fromMaybe(T* defaultValue) const { return m_value ? m_value.get() : defaultValue; }
565
    bool isJust() const { return !!m_value; }
566
    std::unique_ptr<T> takeJust() { DCHECK(m_value); return std::move(m_value); }
567
private:
568
    std::unique_ptr<T> m_value;
569
};
570
571
template<typename T>
572
class MaybeBase {
573
public:
574
    MaybeBase() : m_isJust(false) { }
575
    MaybeBase(T value) : m_isJust(true), m_value(value) { }
576
    MaybeBase(MaybeBase&& other) : m_isJust(other.m_isJust), m_value(std::move(other.m_value)) { }
577
    void operator=(T value) { m_value = value; m_isJust = true; }
578
    T fromJust() const { DCHECK(m_isJust); return m_value; }
579
    T fromMaybe(const T& defaultValue) const { return m_isJust ? m_value : defaultValue; }
580
    bool isJust() const { return m_isJust; }
581
    T takeJust() { DCHECK(m_isJust); return m_value; }
582
583
protected:
584
    bool m_isJust;
585
    T m_value;
586
};
587
588
template<>
589
class Maybe<bool> : public MaybeBase<bool> {
590
public:
591
    Maybe() { }
592
    Maybe(bool value) : MaybeBase(value) { }
593
    Maybe(Maybe&& other) : MaybeBase(std::move(other)) { }
594
    using MaybeBase::operator=;
595
};
596
597
template<>
598
class Maybe<int> : public MaybeBase<int> {
599
public:
600
    Maybe() { }
601
    Maybe(int value) : MaybeBase(value) { }
602
    Maybe(Maybe&& other) : MaybeBase(std::move(other)) { }
603
    using MaybeBase::operator=;
604
};
605
606
template<>
607
class Maybe<double> : public MaybeBase<double> {
608
public:
609
    Maybe() { }
610
    Maybe(double value) : MaybeBase(value) { }
611
    Maybe(Maybe&& other) : MaybeBase(std::move(other)) { }
612
    using MaybeBase::operator=;
613
};
614
615
template<>
616
class Maybe<String> : public MaybeBase<String> {
617
public:
618
    Maybe() { }
619
    Maybe(const String& value) : MaybeBase(value) { }
620
    Maybe(Maybe&& other) : MaybeBase(std::move(other)) { }
621
    using MaybeBase::operator=;
622
};
623
624
} // namespace node
625
} // namespace inspector
626
} // namespace protocol
627
628
#endif // !defined(node_inspector_protocol_Maybe_h)
629
630
631
// Copyright 2016 The Chromium Authors. All rights reserved.
632
// Use of this source code is governed by a BSD-style license that can be
633
// found in the LICENSE file.
634
635
#ifndef node_inspector_protocol_Array_h
636
#define node_inspector_protocol_Array_h
637
638
//#include "ErrorSupport.h"
639
//#include "Forward.h"
640
//#include "ValueConversions.h"
641
//#include "Values.h"
642
643
namespace node {
644
namespace inspector {
645
namespace protocol {
646
647
template<typename T>
648
class Array {
649
public:
650
    static std::unique_ptr<Array<T>> create()
651
    {
652
        return std::unique_ptr<Array<T>>(new Array<T>());
653
    }
654
655
    static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors)
656
    {
657
        protocol::ListValue* array = ListValue::cast(value);
658
        if (!array) {
659
            errors->addError("array expected");
660
            return nullptr;
661
        }
662
        std::unique_ptr<Array<T>> result(new Array<T>());
663
        errors->push();
664
        for (size_t i = 0; i < array->size(); ++i) {
665
            errors->setName(StringUtil::fromInteger(i));
666
            std::unique_ptr<T> item = ValueConversions<T>::fromValue(array->at(i), errors);
667
            result->m_vector.push_back(std::move(item));
668
        }
669
        errors->pop();
670
        if (errors->hasErrors())
671
            return nullptr;
672
        return result;
673
    }
674
675
    void addItem(std::unique_ptr<T> value)
676
    {
677
        m_vector.push_back(std::move(value));
678
    }
679
680
    size_t length()
681
    {
682
        return m_vector.size();
683
    }
684
685
    T* get(size_t index)
686
    {
687
        return m_vector[index].get();
688
    }
689
690
    std::unique_ptr<protocol::ListValue> toValue()
691
    {
692
        std::unique_ptr<protocol::ListValue> result = ListValue::create();
693
        for (auto& item : m_vector)
694
            result->pushValue(ValueConversions<T>::toValue(item));
695
        return result;
696
    }
697
698
private:
699
    std::vector<std::unique_ptr<T>> m_vector;
700
};
701
702
template<typename T>
703
class ArrayBase {
704
public:
705
    static std::unique_ptr<Array<T>> create()
706
    {
707
        return std::unique_ptr<Array<T>>(new Array<T>());
708
    }
709
710
    static std::unique_ptr<Array<T>> fromValue(protocol::Value* value, ErrorSupport* errors)
711
    {
712
        protocol::ListValue* array = ListValue::cast(value);
713
        if (!array) {
714
            errors->addError("array expected");
715
            return nullptr;
716
        }
717
        errors->push();
718
        std::unique_ptr<Array<T>> result(new Array<T>());
719
        for (size_t i = 0; i < array->size(); ++i) {
720
            errors->setName(StringUtil::fromInteger(i));
721
            T item = ValueConversions<T>::fromValue(array->at(i), errors);
722
            result->m_vector.push_back(item);
723
        }
724
        errors->pop();
725
        if (errors->hasErrors())
726
            return nullptr;
727
        return result;
728
    }
729
730
    void addItem(const T& value)
731
    {
732
        m_vector.push_back(value);
733
    }
734
735
    size_t length()
736
    {
737
        return m_vector.size();
738
    }
739
740
    T get(size_t index)
741
    {
742
        return m_vector[index];
743
    }
744
745
    std::unique_ptr<protocol::ListValue> toValue()
746
    {
747
        std::unique_ptr<protocol::ListValue> result = ListValue::create();
748
        for (auto& item : m_vector)
749
            result->pushValue(ValueConversions<T>::toValue(item));
750
        return result;
751
    }
752
753
private:
754
    std::vector<T> m_vector;
755
};
756
757
template<> class Array<String> : public ArrayBase<String> {};
758
template<> class Array<int> : public ArrayBase<int> {};
759
template<> class Array<double> : public ArrayBase<double> {};
760
template<> class Array<bool> : public ArrayBase<bool> {};
761
762
} // namespace node
763
} // namespace inspector
764
} // namespace protocol
765
766
#endif // !defined(node_inspector_protocol_Array_h)
767
768
769
// Copyright 2016 The Chromium Authors. All rights reserved.
770
// Use of this source code is governed by a BSD-style license that can be
771
// found in the LICENSE file.
772
773
#ifndef node_inspector_protocol_DispatcherBase_h
774
#define node_inspector_protocol_DispatcherBase_h
775
776
//#include "Collections.h"
777
//#include "ErrorSupport.h"
778
//#include "Forward.h"
779
//#include "Values.h"
780
781
namespace node {
782
namespace inspector {
783
namespace protocol {
784
785
class WeakPtr;
786
787
652
class  DispatchResponse {
788
public:
789
    enum Status {
790
        kSuccess = 0,
791
        kError = 1,
792
        kFallThrough = 2,
793
        kAsync = 3
794
    };
795
796
    enum ErrorCode {
797
        kParseError = -32700,
798
        kInvalidRequest = -32600,
799
        kMethodNotFound = -32601,
800
        kInvalidParams = -32602,
801
        kInternalError = -32603,
802
        kServerError = -32000,
803
    };
804
805
    Status status() const { return m_status; }
806
    const String& errorMessage() const { return m_errorMessage; }
807
    ErrorCode errorCode() const { return m_errorCode; }
808
    bool isSuccess() const { return m_status == kSuccess; }
809
810
    static DispatchResponse OK();
811
    static DispatchResponse Error(const String&);
812
    static DispatchResponse InternalError();
813
    static DispatchResponse InvalidParams(const String&);
814
    static DispatchResponse FallThrough();
815
816
private:
817
    Status m_status;
818
    String m_errorMessage;
819
    ErrorCode m_errorCode;
820
};
821
822
class  DispatcherBase {
823
    PROTOCOL_DISALLOW_COPY(DispatcherBase);
824
public:
825
    static const char kInvalidParamsString[];
826
    class  WeakPtr {
827
    public:
828
        explicit WeakPtr(DispatcherBase*);
829
        ~WeakPtr();
830
        DispatcherBase* get() { return m_dispatcher; }
831
        void dispose() { m_dispatcher = nullptr; }
832
833
    private:
834
        DispatcherBase* m_dispatcher;
835
    };
836
837
    class  Callback {
838
    public:
839
        Callback(std::unique_ptr<WeakPtr> backendImpl, int callId, int callbackId);
840
        virtual ~Callback();
841
        void dispose();
842
843
    protected:
844
        void sendIfActive(std::unique_ptr<protocol::DictionaryValue> partialMessage, const DispatchResponse& response);
845
        void fallThroughIfActive();
846
847
    private:
848
        std::unique_ptr<WeakPtr> m_backendImpl;
849
        int m_callId;
850
        int m_callbackId;
851
    };
852
853
    explicit DispatcherBase(FrontendChannel*);
854
    virtual ~DispatcherBase();
855
856
    virtual DispatchResponse::Status dispatch(int callId, const String& method, std::unique_ptr<protocol::DictionaryValue> messageObject) = 0;
857
858
    void sendResponse(int callId, const DispatchResponse&, std::unique_ptr<protocol::DictionaryValue> result);
859
    void sendResponse(int callId, const DispatchResponse&);
860
861
    void reportProtocolError(int callId, DispatchResponse::ErrorCode, const String& errorMessage, ErrorSupport* errors);
862
    void clearFrontend();
863
864
    std::unique_ptr<WeakPtr> weakPtr();
865
866
    int nextCallbackId();
867
    void markFallThrough(int callbackId);
868
    bool lastCallbackFallThrough() { return m_lastCallbackFallThrough; }
869
870
private:
871
    FrontendChannel* m_frontendChannel;
872
    protocol::HashSet<WeakPtr*> m_weakPtrs;
873
    int m_lastCallbackId;
874
    bool m_lastCallbackFallThrough;
875
};
876
877
326
class  UberDispatcher {
878
    PROTOCOL_DISALLOW_COPY(UberDispatcher);
879
public:
880
    explicit UberDispatcher(FrontendChannel*);
881
    void registerBackend(const String& name, std::unique_ptr<protocol::DispatcherBase>);
882
    void setupRedirects(const HashMap<String, String>&);
883
    DispatchResponse::Status dispatch(std::unique_ptr<Value> message, int* callId = nullptr, String* method = nullptr);
884
652
    FrontendChannel* channel() { return m_frontendChannel; }
885
326
    bool fallThroughForNotFound() { return m_fallThroughForNotFound; }
886
    void setFallThroughForNotFound(bool);
887
    bool getCommandName(const String& message, String* method, std::unique_ptr<protocol::DictionaryValue>* parsedMessage);
888
    virtual ~UberDispatcher();
889
890
private:
891
    FrontendChannel* m_frontendChannel;
892
    bool m_fallThroughForNotFound;
893
    HashMap<String, String> m_redirects;
894
    protocol::HashMap<String, std::unique_ptr<protocol::DispatcherBase>> m_dispatchers;
895
};
896
897
class InternalResponse : public Serializable {
898
    PROTOCOL_DISALLOW_COPY(InternalResponse);
899
public:
900
    static std::unique_ptr<InternalResponse> createResponse(int callId, std::unique_ptr<Serializable> params);
901
    static std::unique_ptr<InternalResponse> createNotification(const String& notification, std::unique_ptr<Serializable> params = nullptr);
902
903
    String serialize() override;
904
905
    ~InternalResponse() override {}
906
907
private:
908
    InternalResponse(int callId, const String& notification, std::unique_ptr<Serializable> params);
909
910
    int m_callId;
911
    String m_notification;
912
    std::unique_ptr<Serializable> m_params;
913
};
914
915
class InternalRawNotification : public Serializable {
916
public:
917
    static std::unique_ptr<InternalRawNotification> create(const String& notification)
918
    {
919
        return std::unique_ptr<InternalRawNotification>(new InternalRawNotification(notification));
920
    }
921
    ~InternalRawNotification() override {}
922
923
    String serialize() override
924
    {
925
        return m_notification;
926
    }
927
928
private:
929
  explicit InternalRawNotification(const String& notification)
930
    : m_notification(notification)
931
  {
932
  }
933
934
  String m_notification;
935
};
936
937
} // namespace node
938
} // namespace inspector
939
} // namespace protocol
940
941
#endif // !defined(node_inspector_protocol_DispatcherBase_h)
942
943
944
// Copyright 2016 The Chromium Authors. All rights reserved.
945
// Use of this source code is governed by a BSD-style license that can be
946
// found in the LICENSE file.
947
948
#ifndef node_inspector_protocol_Parser_h
949
#define node_inspector_protocol_Parser_h
950
951
//#include "Forward.h"
952
//#include "Values.h"
953
954
namespace node {
955
namespace inspector {
956
namespace protocol {
957
958
 std::unique_ptr<Value> parseJSONCharacters(const uint8_t*, unsigned);
959
 std::unique_ptr<Value> parseJSONCharacters(const uint16_t*, unsigned);
960
961
} // namespace node
962
} // namespace inspector
963
} // namespace protocol
964
965
#endif // !defined(node_inspector_protocol_Parser_h)