OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/argument_spec.h" | 5 #include "extensions/renderer/argument_spec.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "content/public/child/v8_value_converter.h" | 10 #include "content/public/child/v8_value_converter.h" |
11 #include "extensions/renderer/api_type_reference_map.h" | 11 #include "extensions/renderer/api_type_reference_map.h" |
12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
13 #include "gin/dictionary.h" | 13 #include "gin/dictionary.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 template <class T> | 19 template <class T> |
20 bool ParseFundamentalValueHelper(v8::Local<v8::Value> arg, | 20 bool ParseFundamentalValueHelper(v8::Local<v8::Value> arg, |
21 v8::Local<v8::Context> context, | 21 v8::Local<v8::Context> context, |
22 const base::Optional<int>& minimum, | 22 const base::Optional<int>& minimum, |
| 23 const base::Optional<int>& maximum, |
23 std::unique_ptr<base::Value>* out_value) { | 24 std::unique_ptr<base::Value>* out_value) { |
24 T val; | 25 T val; |
25 if (!gin::Converter<T>::FromV8(context->GetIsolate(), arg, &val)) | 26 if (!gin::Converter<T>::FromV8(context->GetIsolate(), arg, &val)) |
26 return false; | 27 return false; |
27 if (minimum && val < minimum.value()) | 28 if (minimum && val < *minimum) |
| 29 return false; |
| 30 if (maximum && val > *maximum) |
28 return false; | 31 return false; |
29 if (out_value) | 32 if (out_value) |
30 *out_value = base::MakeUnique<base::Value>(val); | 33 *out_value = base::MakeUnique<base::Value>(val); |
31 return true; | 34 return true; |
32 } | 35 } |
33 | 36 |
34 } // namespace | 37 } // namespace |
35 | 38 |
36 ArgumentSpec::ArgumentSpec(const base::Value& value) | 39 ArgumentSpec::ArgumentSpec(const base::Value& value) |
37 : type_(ArgumentType::INTEGER), optional_(false) { | 40 : type_(ArgumentType::INTEGER), optional_(false) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 type_ = ArgumentType::ANY; | 88 type_ = ArgumentType::ANY; |
86 else if (type_string == "function") | 89 else if (type_string == "function") |
87 type_ = ArgumentType::FUNCTION; | 90 type_ = ArgumentType::FUNCTION; |
88 else | 91 else |
89 NOTREACHED(); | 92 NOTREACHED(); |
90 | 93 |
91 int min = 0; | 94 int min = 0; |
92 if (dict->GetInteger("minimum", &min)) | 95 if (dict->GetInteger("minimum", &min)) |
93 minimum_ = min; | 96 minimum_ = min; |
94 | 97 |
| 98 int max = 0; |
| 99 if (dict->GetInteger("maximum", &max)) |
| 100 maximum_ = max; |
| 101 |
95 int min_length = 0; | 102 int min_length = 0; |
96 if (dict->GetInteger("minLength", &min_length) || | 103 if (dict->GetInteger("minLength", &min_length) || |
97 dict->GetInteger("minItems", &min_length)) { | 104 dict->GetInteger("minItems", &min_length)) { |
98 DCHECK_GE(min_length, 0); | 105 DCHECK_GE(min_length, 0); |
99 min_length_ = min_length; | 106 min_length_ = min_length; |
100 } | 107 } |
101 | 108 |
102 int max_length = 0; | 109 int max_length = 0; |
103 if (dict->GetInteger("maxLength", &max_length) || | 110 if (dict->GetInteger("maxLength", &max_length) || |
104 dict->GetInteger("maxItems", &max_length)) { | 111 dict->GetInteger("maxItems", &max_length)) { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 241 |
235 bool ArgumentSpec::ParseArgumentToFundamental( | 242 bool ArgumentSpec::ParseArgumentToFundamental( |
236 v8::Local<v8::Context> context, | 243 v8::Local<v8::Context> context, |
237 v8::Local<v8::Value> value, | 244 v8::Local<v8::Value> value, |
238 std::unique_ptr<base::Value>* out_value, | 245 std::unique_ptr<base::Value>* out_value, |
239 std::string* error) const { | 246 std::string* error) const { |
240 DCHECK(IsFundamentalType()); | 247 DCHECK(IsFundamentalType()); |
241 switch (type_) { | 248 switch (type_) { |
242 case ArgumentType::INTEGER: | 249 case ArgumentType::INTEGER: |
243 return ParseFundamentalValueHelper<int32_t>(value, context, minimum_, | 250 return ParseFundamentalValueHelper<int32_t>(value, context, minimum_, |
244 out_value); | 251 maximum_, out_value); |
245 case ArgumentType::DOUBLE: | 252 case ArgumentType::DOUBLE: |
246 return ParseFundamentalValueHelper<double>(value, context, minimum_, | 253 return ParseFundamentalValueHelper<double>(value, context, minimum_, |
247 out_value); | 254 maximum_, out_value); |
248 case ArgumentType::STRING: { | 255 case ArgumentType::STRING: { |
249 if (!value->IsString()) | 256 if (!value->IsString()) |
250 return false; | 257 return false; |
251 | 258 |
252 v8::Local<v8::String> v8_string = value.As<v8::String>(); | 259 v8::Local<v8::String> v8_string = value.As<v8::String>(); |
253 size_t length = static_cast<size_t>(v8_string->Length()); | 260 size_t length = static_cast<size_t>(v8_string->Length()); |
254 if (min_length_ && length < *min_length_) { | 261 if (min_length_ && length < *min_length_) { |
255 *error = "Less than min length"; | 262 *error = "Less than min length"; |
256 return false; | 263 return false; |
257 } | 264 } |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 return false; | 483 return false; |
477 } | 484 } |
478 if (type_ == ArgumentType::BINARY) | 485 if (type_ == ArgumentType::BINARY) |
479 DCHECK_EQ(base::Value::Type::BINARY, converted->GetType()); | 486 DCHECK_EQ(base::Value::Type::BINARY, converted->GetType()); |
480 *out_value = std::move(converted); | 487 *out_value = std::move(converted); |
481 } | 488 } |
482 return true; | 489 return true; |
483 } | 490 } |
484 | 491 |
485 } // namespace extensions | 492 } // namespace extensions |
OLD | NEW |