OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/v8_value_converter_impl.h" | 5 #include "content/renderer/v8_value_converter_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace content { | 23 namespace content { |
24 | 24 |
25 V8ValueConverter* V8ValueConverter::create() { | 25 V8ValueConverter* V8ValueConverter::create() { |
26 return new V8ValueConverterImpl(); | 26 return new V8ValueConverterImpl(); |
27 } | 27 } |
28 | 28 |
29 } | 29 } |
30 | 30 |
31 V8ValueConverterImpl::V8ValueConverterImpl() | 31 V8ValueConverterImpl::V8ValueConverterImpl() |
32 : allow_undefined_(false), | 32 : undefined_allowed_(false), |
33 allow_date_(false), | 33 date_allowed_(false), |
34 allow_regexp_(false) { | 34 regexp_allowed_(false) { |
| 35 } |
| 36 |
| 37 bool V8ValueConverterImpl::GetUndefinedAllowed() const { |
| 38 return undefined_allowed_; |
| 39 } |
| 40 |
| 41 void V8ValueConverterImpl::SetUndefinedAllowed(bool val) { |
| 42 undefined_allowed_ = val; |
| 43 } |
| 44 |
| 45 bool V8ValueConverterImpl::GetDateAllowed() const { |
| 46 return date_allowed_; |
| 47 } |
| 48 |
| 49 void V8ValueConverterImpl::SetDateAllowed(bool val) { |
| 50 date_allowed_ = val; |
| 51 } |
| 52 |
| 53 bool V8ValueConverterImpl::GetRegexpAllowed() const { |
| 54 return regexp_allowed_; |
| 55 } |
| 56 |
| 57 void V8ValueConverterImpl::SetRegexpAllowed(bool val) { |
| 58 regexp_allowed_ = val; |
35 } | 59 } |
36 | 60 |
37 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( | 61 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( |
38 const Value* value, v8::Handle<v8::Context> context) const { | 62 const Value* value, v8::Handle<v8::Context> context) const { |
39 v8::Context::Scope context_scope(context); | 63 v8::Context::Scope context_scope(context); |
40 v8::HandleScope handle_scope; | 64 v8::HandleScope handle_scope; |
41 return handle_scope.Close(ToV8ValueImpl(value)); | 65 return handle_scope.Close(ToV8ValueImpl(value)); |
42 } | 66 } |
43 | 67 |
44 Value* V8ValueConverterImpl::FromV8Value( | 68 Value* V8ValueConverterImpl::FromV8Value( |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 return Value::CreateIntegerValue(val->ToInt32()->Value()); | 184 return Value::CreateIntegerValue(val->ToInt32()->Value()); |
161 | 185 |
162 if (val->IsNumber()) | 186 if (val->IsNumber()) |
163 return Value::CreateDoubleValue(val->ToNumber()->Value()); | 187 return Value::CreateDoubleValue(val->ToNumber()->Value()); |
164 | 188 |
165 if (val->IsString()) { | 189 if (val->IsString()) { |
166 v8::String::Utf8Value utf8(val->ToString()); | 190 v8::String::Utf8Value utf8(val->ToString()); |
167 return Value::CreateStringValue(std::string(*utf8, utf8.length())); | 191 return Value::CreateStringValue(std::string(*utf8, utf8.length())); |
168 } | 192 } |
169 | 193 |
170 if (allow_undefined_ && val->IsUndefined()) | 194 if (undefined_allowed_ && val->IsUndefined()) |
171 return Value::CreateNullValue(); | 195 return Value::CreateNullValue(); |
172 | 196 |
173 if (allow_date_ && val->IsDate()) { | 197 if (date_allowed_ && val->IsDate()) { |
174 v8::Date* date = v8::Date::Cast(*val); | 198 v8::Date* date = v8::Date::Cast(*val); |
175 return Value::CreateDoubleValue(date->NumberValue() / 1000.0); | 199 return Value::CreateDoubleValue(date->NumberValue() / 1000.0); |
176 } | 200 } |
177 | 201 |
178 if (allow_regexp_ && val->IsRegExp()) { | 202 if (regexp_allowed_ && val->IsRegExp()) { |
179 return Value::CreateStringValue( | 203 return Value::CreateStringValue( |
180 *v8::String::Utf8Value(val->ToString())); | 204 *v8::String::Utf8Value(val->ToString())); |
181 } | 205 } |
182 | 206 |
183 // v8::Value doesn't have a ToArray() method for some reason. | 207 // v8::Value doesn't have a ToArray() method for some reason. |
184 if (val->IsArray()) | 208 if (val->IsArray()) |
185 return FromV8Array(val.As<v8::Array>()); | 209 return FromV8Array(val.As<v8::Array>()); |
186 | 210 |
187 if (val->IsObject()) { | 211 if (val->IsObject()) { |
188 BinaryValue* binary_value = FromV8Buffer(val); | 212 BinaryValue* binary_value = FromV8Buffer(val); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 } | 291 } |
268 | 292 |
269 Value* child = FromV8ValueImpl(child_v8); | 293 Value* child = FromV8ValueImpl(child_v8); |
270 CHECK(child); | 294 CHECK(child); |
271 | 295 |
272 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), | 296 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), |
273 child); | 297 child); |
274 } | 298 } |
275 return result; | 299 return result; |
276 } | 300 } |
OLD | NEW |