1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
content / public / renderer / v8_value_converter.h [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_
#define CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_
#include <memory>
#include "base/functional/callback.h"
#include "content/common/content_export.h"
#include "third_party/blink/public/platform/web_v8_value_converter.h"
#include "v8/include/v8-forward.h"
namespace base {
class Value;
class ValueView;
}
namespace content {
// Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's
// values (from base/values.h). Lists and dictionaries are converted
// recursively.
//
// The JSON types (null, boolean, string, number, array, and object) as well as
// binary values are supported. For binary values, we convert to WebKit
// ArrayBuffers, and support converting from an ArrayBuffer or any of the
// ArrayBufferView subclasses (Uint8Array, etc.).
class CONTENT_EXPORT V8ValueConverter : public blink::WebV8ValueConverter {
public:
// Extends the default behaviour of V8ValueConverter.
class CONTENT_EXPORT Strategy {
public:
virtual ~Strategy() {}
// If false is returned, V8ValueConverter proceeds with the default
// behavior.
// Use |callback| to convert any child values, as this will retain
// the ValueConverter's internal checks for depth and cycles.
virtual bool FromV8Object(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate);
// If false is returned, V8ValueConverter proceeds with the default
// behavior.
// Use |callback| to convert any child values, as this will retain
// the ValueConverter's internal checks for depth and cycles.
virtual bool FromV8Array(v8::Local<v8::Array> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate);
// If false is returned, V8ValueConverter proceeds with the default
// behavior. v8::Object is passed as ArrayBuffer and ArrayBufferView
// classes are siblings.
virtual bool FromV8ArrayBuffer(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate);
// If false is returned, V8ValueConverter proceeds with the default
// behavior. This allows to intercept "non-finite" values and do something
// with them.
virtual bool FromV8Number(v8::Local<v8::Number> value,
std::unique_ptr<base::Value>* out);
// If false is returned, V8ValueConverter proceeds with the default
// behavior.
virtual bool FromV8Undefined(std::unique_ptr<base::Value>* out);
};
static std::unique_ptr<V8ValueConverter> Create();
~V8ValueConverter() override = default;
// If true, Date objects are converted into DoubleValues with the number of
// seconds since Unix epoch.
//
// Otherwise they are converted into DictionaryValues with whatever additional
// properties has been set on them.
void SetDateAllowed(bool val) override = 0;
// If true, RegExp objects are converted into StringValues with the regular
// expression between / and /, for example "/ab?c/".
//
// Otherwise they are converted into DictionaryValues with whatever additional
// properties has been set on them.
void SetRegExpAllowed(bool val) override = 0;
// If true, Function objects are converted into DictionaryValues with whatever
// additional properties has been set on them.
//
// Otherwise they are treated as unsupported, see FromV8Value.
virtual void SetFunctionAllowed(bool val) = 0;
// If true, null values are stripped from objects. This is often useful when
// converting arguments to extension APIs.
virtual void SetStripNullFromObjects(bool val) = 0;
// If true, treats -0 as an integer. Otherwise, -0 is converted to a double.
virtual void SetConvertNegativeZeroToInt(bool val) = 0;
// Extend default behavior of V8ValueConverter.
virtual void SetStrategy(Strategy* strategy) = 0;
// Converts a base::Value to a v8::Value.
//
// Unsupported types are replaced with null. If an array or object throws
// while setting a value, that property or item is skipped, leaving a hole in
// the case of arrays.
v8::Local<v8::Value> ToV8Value(base::ValueView value,
v8::Local<v8::Context> context) override = 0;
// Converts a v8::Value to base::Value.
//
// Unsupported types (unless explicitly configured) are not converted, so
// this method may return NULL -- the exception is when converting arrays,
// where unsupported types are converted to Value(Type::NONE).
//
// Likewise, if an object throws while converting a property it will not be
// converted, whereas if an array throws while converting an item it will be
// converted to Value(Type::NONE).
std::unique_ptr<base::Value> FromV8Value(
v8::Local<v8::Value> value,
v8::Local<v8::Context> context) override = 0;
};
} // namespace content
#endif // CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_