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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/renderer/argument_spec.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "content/public/child/v8_value_converter.h"
#include "extensions/renderer/api_type_reference_map.h"
#include "gin/converter.h"
#include "gin/dictionary.h"
namespace extensions {
namespace {
template <class T>
bool ParseFundamentalValueHelper(v8::Local<v8::Value> arg,
v8::Local<v8::Context> context,
const base::Optional<int>& minimum,
std::unique_ptr<base::Value>* out_value) {
T val;
if (!gin::Converter<T>::FromV8(context->GetIsolate(), arg, &val))
return false;
if (minimum && val < minimum.value())
return false;
if (out_value)
*out_value = base::MakeUnique<base::Value>(val);
return true;
}
} // namespace
ArgumentSpec::ArgumentSpec(const base::Value& value)
: type_(ArgumentType::INTEGER), optional_(false) {
const base::DictionaryValue* dict = nullptr;
CHECK(value.GetAsDictionary(&dict));
dict->GetBoolean("optional", &optional_);
dict->GetString("name", &name_);
InitializeType(dict);
}
void ArgumentSpec::InitializeType(const base::DictionaryValue* dict) {
std::string ref_string;
if (dict->GetString("$ref", &ref_string)) {
ref_ = std::move(ref_string);
type_ = ArgumentType::REF;
return;
}
{
const base::ListValue* choices = nullptr;
if (dict->GetList("choices", &choices)) {
DCHECK(!choices->empty());
type_ = ArgumentType::CHOICES;
choices_.reserve(choices->GetSize());
for (const auto& choice : *choices)
choices_.push_back(base::MakeUnique<ArgumentSpec>(*choice));
return;
}
}
std::string type_string;
CHECK(dict->GetString("type", &type_string));
if (type_string == "integer")
type_ = ArgumentType::INTEGER;
else if (type_string == "number")
type_ = ArgumentType::DOUBLE;
else if (type_string == "object")
type_ = ArgumentType::OBJECT;
else if (type_string == "array")
type_ = ArgumentType::LIST;
else if (type_string == "boolean")
type_ = ArgumentType::BOOLEAN;
else if (type_string == "string")
type_ = ArgumentType::STRING;
else if (type_string == "binary")
type_ = ArgumentType::BINARY;
else if (type_string == "any")
type_ = ArgumentType::ANY;
else if (type_string == "function")
type_ = ArgumentType::FUNCTION;
else
NOTREACHED();
int min = 0;
if (dict->GetInteger("minimum", &min))
minimum_ = min;
if (type_ == ArgumentType::OBJECT) {
const base::DictionaryValue* properties_value = nullptr;
if (dict->GetDictionary("properties", &properties_value)) {
for (base::DictionaryValue::Iterator iter(*properties_value);
!iter.IsAtEnd(); iter.Advance()) {
properties_[iter.key()] = base::MakeUnique<ArgumentSpec>(iter.value());
}
}
const base::DictionaryValue* additional_properties_value = nullptr;
if (dict->GetDictionary("additionalProperties",
&additional_properties_value)) {
additional_properties_ =
base::MakeUnique<ArgumentSpec>(*additional_properties_value);
}
} else if (type_ == ArgumentType::LIST) {
const base::DictionaryValue* item_value = nullptr;
CHECK(dict->GetDictionary("items", &item_value));
list_element_type_ = base::MakeUnique<ArgumentSpec>(*item_value);
} else if (type_ == ArgumentType::STRING) {
// Technically, there's no reason enums couldn't be other objects (e.g.
// numbers), but right now they seem to be exclusively strings. We could
// always update this if need be.
const base::ListValue* enums = nullptr;
if (dict->GetList("enum", &enums)) {
size_t size = enums->GetSize();
CHECK_GT(size, 0u);
for (size_t i = 0; i < size; ++i) {
std::string enum_value;
// Enum entries come in two versions: a list of possible strings, and
// a dictionary with a field 'name'.
if (!enums->GetString(i, &enum_value)) {
const base::DictionaryValue* enum_value_dictionary = nullptr;
CHECK(enums->GetDictionary(i, &enum_value_dictionary));
CHECK(enum_value_dictionary->GetString("name", &enum_value));
}
enum_values_.insert(std::move(enum_value));
}
}
}
}
ArgumentSpec::~ArgumentSpec() {}
bool ArgumentSpec::ParseArgument(v8::Local<v8::Context> context,
v8::Local<v8::Value> value,
const APITypeReferenceMap& refs,
std::unique_ptr<base::Value>* out_value,
std::string* error) const {
if (type_ == ArgumentType::FUNCTION) {
// We can't serialize functions. We shouldn't be asked to.
DCHECK(!out_value);
return value->IsFunction();
}
if (type_ == ArgumentType::REF) {
DCHECK(ref_);
const ArgumentSpec* reference = refs.GetSpec(ref_.value());
DCHECK(reference) << ref_.value();
return reference->ParseArgument(context, value, refs, out_value, error);
}
if (type_ == ArgumentType::CHOICES) {
for (const auto& choice : choices_) {
if (choice->ParseArgument(context, value, refs, out_value, error))
return true;
}
*error = "Did not match any of the choices";
return false;
}
if (IsFundamentalType())
return ParseArgumentToFundamental(context, value, out_value, error);
if (type_ == ArgumentType::OBJECT) {
// Don't allow functions or arrays (even though they are technically
// objects). This is to make it easier to match otherwise-ambiguous
// signatures. For instance, if an API method has an optional object
// parameter and then an optional callback, we wouldn't necessarily be able
// to match the arguments if we allowed functions as objects.
if (!value->IsObject() || value->IsFunction() || value->IsArray()) {
*error = "Wrong type";
return false;
}
v8::Local<v8::Object> object = value.As<v8::Object>();
return ParseArgumentToObject(context, object, refs, out_value, error);
}
if (type_ == ArgumentType::LIST) {
if (!value->IsArray()) {
*error = "Wrong type";
return false;
}
v8::Local<v8::Array> array = value.As<v8::Array>();
return ParseArgumentToArray(context, array, refs, out_value, error);
}
if (type_ == ArgumentType::BINARY) {
if (!value->IsArrayBuffer() && !value->IsArrayBufferView()) {
*error = "Wrong type";
return false;
}
return ParseArgumentToAny(context, value, out_value, error);
}
if (type_ == ArgumentType::ANY)
return ParseArgumentToAny(context, value, out_value, error);
NOTREACHED();
return false;
}
bool ArgumentSpec::IsFundamentalType() const {
return type_ == ArgumentType::INTEGER || type_ == ArgumentType::DOUBLE ||
type_ == ArgumentType::BOOLEAN || type_ == ArgumentType::STRING;
}
bool ArgumentSpec::ParseArgumentToFundamental(
v8::Local<v8::Context> context,
v8::Local<v8::Value> value,
std::unique_ptr<base::Value>* out_value,
std::string* error) const {
DCHECK(IsFundamentalType());
switch (type_) {
case ArgumentType::INTEGER:
return ParseFundamentalValueHelper<int32_t>(value, context, minimum_,
out_value);
case ArgumentType::DOUBLE:
return ParseFundamentalValueHelper<double>(value, context, minimum_,
out_value);
case ArgumentType::STRING: {
if (!value->IsString())
return false;
// If we don't need to match enum values and don't need to convert, we're
// done...
if (!out_value && enum_values_.empty())
return true;
// ...Otherwise, we need to convert to a std::string.
std::string s;
// We already checked that this is a string, so this should never fail.
CHECK(gin::Converter<std::string>::FromV8(context->GetIsolate(), value,
&s));
if (!enum_values_.empty() && enum_values_.count(s) == 0)
return false;
if (out_value) {
// TODO(devlin): If base::StringValue ever takes a std::string&&, we
// could use std::move to construct.
*out_value = base::MakeUnique<base::StringValue>(s);
}
return true;
}
case ArgumentType::BOOLEAN: {
if (!value->IsBoolean())
return false;
if (out_value) {
*out_value =
base::MakeUnique<base::Value>(value.As<v8::Boolean>()->Value());
}
return true;
}
default:
NOTREACHED();
}
return false;
}
bool ArgumentSpec::ParseArgumentToObject(
v8::Local<v8::Context> context,
v8::Local<v8::Object> object,
const APITypeReferenceMap& refs,
std::unique_ptr<base::Value>* out_value,
std::string* error) const {
DCHECK_EQ(ArgumentType::OBJECT, type_);
std::unique_ptr<base::DictionaryValue> result;
// Only construct the result if we have an |out_value| to populate.
if (out_value)
result = base::MakeUnique<base::DictionaryValue>();
gin::Dictionary dictionary(context->GetIsolate(), object);
for (const auto& kv : properties_) {
v8::Local<v8::Value> subvalue;
// See comment in ParseArgumentToArray() about passing in custom crazy
// values here.
// TODO(devlin): gin::Dictionary::Get() uses Isolate::GetCurrentContext() -
// is that always right here, or should we use the v8::Object APIs and
// pass in |context|?
// TODO(devlin): Hyper-optimization - Dictionary::Get() also creates a new
// v8::String for each call. Hypothetically, we could cache these, or at
// least use an internalized string.
if (!dictionary.Get(kv.first, &subvalue))
return false;
if (subvalue.IsEmpty() || subvalue->IsNull() || subvalue->IsUndefined()) {
if (!kv.second->optional_) {
*error = "Missing key: " + kv.first;
return false;
}
continue;
}
std::unique_ptr<base::Value> property;
if (!kv.second->ParseArgument(context, subvalue, refs,
out_value ? &property : nullptr, error)) {
return false;
}
if (result)
result->Set(kv.first, std::move(property));
}
// Check for additional properties.
if (additional_properties_) {
v8::Local<v8::Array> own_property_names;
if (!object->GetOwnPropertyNames(context).ToLocal(&own_property_names))
return false;
uint32_t length = own_property_names->Length();
for (uint32_t i = 0; i < length; ++i) {
v8::Local<v8::Value> key;
if (!own_property_names->Get(context, i).ToLocal(&key))
return false;
// In JS, all keys are strings or numbers (or symbols, but those are
// excluded by GetOwnPropertyNames()). If you try to set anything else
// (e.g. an object), it is converted to a string.
DCHECK(key->IsString() || key->IsNumber());
v8::String::Utf8Value utf8_key(key);
// If the key was one of the specified properties, we've already handled
// it. Continue.
if (properties_.find(*utf8_key) != properties_.end())
continue;
v8::Local<v8::Value> subvalue;
// Fun: It's possible that a previous getter has removed the property from
// the object. This isn't that big of a deal, since it would only manifest
// in the case of some reasonably-crazy script objects, and it's probably
// not worth optimizing for the uncommon case to the detriment of the
// common (and either should be totally safe). We can always add a
// HasOwnProperty() check here in the future, if we desire.
if (!object->Get(context, key).ToLocal(&subvalue))
return false;
// We don't serialize undefined values.
// TODO(devlin): This matches current behavior, but it is correct?
if (subvalue->IsUndefined())
continue;
std::unique_ptr<base::Value> property;
if (!additional_properties_->ParseArgument(
context, subvalue, refs, result ? &property : nullptr, error)) {
return false;
}
if (result)
result->SetWithoutPathExpansion(*utf8_key, std::move(property));
}
}
if (out_value)
*out_value = std::move(result);
return true;
}
bool ArgumentSpec::ParseArgumentToArray(v8::Local<v8::Context> context,
v8::Local<v8::Array> value,
const APITypeReferenceMap& refs,
std::unique_ptr<base::Value>* out_value,
std::string* error) const {
DCHECK_EQ(ArgumentType::LIST, type_);
std::unique_ptr<base::ListValue> result;
// Only construct the result if we have an |out_value| to populate.
if (out_value)
result = base::MakeUnique<base::ListValue>();
uint32_t length = value->Length();
for (uint32_t i = 0; i < length; ++i) {
v8::MaybeLocal<v8::Value> maybe_subvalue = value->Get(context, i);
v8::Local<v8::Value> subvalue;
// Note: This can fail in the case of a developer passing in the following:
// var a = [];
// Object.defineProperty(a, 0, { get: () => { throw new Error('foo'); } });
// Currently, this will cause the developer-specified error ('foo') to be
// thrown.
// TODO(devlin): This is probably fine, but it's worth contemplating
// catching the error and throwing our own.
if (!maybe_subvalue.ToLocal(&subvalue))
return false;
std::unique_ptr<base::Value> item;
if (!list_element_type_->ParseArgument(context, subvalue, refs,
result ? &item : nullptr, error)) {
return false;
}
if (result)
result->Append(std::move(item));
}
if (out_value)
*out_value = std::move(result);
return true;
}
bool ArgumentSpec::ParseArgumentToAny(v8::Local<v8::Context> context,
v8::Local<v8::Value> value,
std::unique_ptr<base::Value>* out_value,
std::string* error) const {
DCHECK(type_ == ArgumentType::ANY || type_ == ArgumentType::BINARY);
if (out_value) {
std::unique_ptr<content::V8ValueConverter> converter(
content::V8ValueConverter::create());
std::unique_ptr<base::Value> converted(
converter->FromV8Value(value, context));
if (!converted) {
*error = "Could not convert to 'any'.";
return false;
}
if (type_ == ArgumentType::BINARY)
DCHECK_EQ(base::Value::Type::BINARY, converted->GetType());
*out_value = std::move(converted);
}
return true;
}
} // namespace extensions
|