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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 FromV8ValueState::Level state_level(state); | 243 FromV8ValueState::Level state_level(state); |
244 if (state->HasReachedMaxRecursionDepth()) | 244 if (state->HasReachedMaxRecursionDepth()) |
245 return NULL; | 245 return NULL; |
246 | 246 |
247 if (val->IsNull()) | 247 if (val->IsNull()) |
248 return base::Value::CreateNullValue(); | 248 return base::Value::CreateNullValue(); |
249 | 249 |
250 if (val->IsBoolean()) | 250 if (val->IsBoolean()) |
251 return new base::FundamentalValue(val->ToBoolean()->Value()); | 251 return new base::FundamentalValue(val->ToBoolean()->Value()); |
252 | 252 |
| 253 if (val->IsNumber() && strategy_) { |
| 254 base::Value* out = NULL; |
| 255 if (strategy_->FromV8Number(val->ToNumber(), &out)) |
| 256 return out; |
| 257 } |
| 258 |
253 if (val->IsInt32()) | 259 if (val->IsInt32()) |
254 return new base::FundamentalValue(val->ToInt32()->Value()); | 260 return new base::FundamentalValue(val->ToInt32()->Value()); |
255 | 261 |
256 if (val->IsNumber()) { | 262 if (val->IsNumber()) { |
257 double val_as_double = val->ToNumber()->Value(); | 263 double val_as_double = val->ToNumber()->Value(); |
258 if (!base::IsFinite(val_as_double)) | 264 if (!base::IsFinite(val_as_double)) |
259 return NULL; | 265 return NULL; |
260 return new base::FundamentalValue(val_as_double); | 266 return new base::FundamentalValue(val_as_double); |
261 } | 267 } |
262 | 268 |
263 if (val->IsString()) { | 269 if (val->IsString()) { |
264 v8::String::Utf8Value utf8(val->ToString()); | 270 v8::String::Utf8Value utf8(val->ToString()); |
265 return new base::StringValue(std::string(*utf8, utf8.length())); | 271 return new base::StringValue(std::string(*utf8, utf8.length())); |
266 } | 272 } |
267 | 273 |
268 if (val->IsUndefined()) | 274 if (val->IsUndefined()) { |
| 275 if (strategy_) { |
| 276 base::Value* out = NULL; |
| 277 if (strategy_->FromV8Undefined(&out)) |
| 278 return out; |
| 279 } |
269 // JSON.stringify ignores undefined. | 280 // JSON.stringify ignores undefined. |
270 return NULL; | 281 return NULL; |
| 282 } |
271 | 283 |
272 if (val->IsDate()) { | 284 if (val->IsDate()) { |
273 if (!date_allowed_) | 285 if (!date_allowed_) |
274 // JSON.stringify would convert this to a string, but an object is more | 286 // JSON.stringify would convert this to a string, but an object is more |
275 // consistent within this class. | 287 // consistent within this class. |
276 return FromV8Object(val->ToObject(), state, isolate); | 288 return FromV8Object(val->ToObject(), state, isolate); |
277 v8::Date* date = v8::Date::Cast(*val); | 289 v8::Date* date = v8::Date::Cast(*val); |
278 return new base::FundamentalValue(date->ValueOf() / 1000.0); | 290 return new base::FundamentalValue(date->ValueOf() / 1000.0); |
279 } | 291 } |
280 | 292 |
281 if (val->IsRegExp()) { | 293 if (val->IsRegExp()) { |
282 if (!reg_exp_allowed_) | 294 if (!reg_exp_allowed_) |
283 // JSON.stringify converts to an object. | 295 // JSON.stringify converts to an object. |
284 return FromV8Object(val->ToObject(), state, isolate); | 296 return FromV8Object(val->ToObject(), state, isolate); |
285 return new base::StringValue(*v8::String::Utf8Value(val->ToString())); | 297 return new base::StringValue(*v8::String::Utf8Value(val->ToString())); |
286 } | 298 } |
287 | 299 |
288 // v8::Value doesn't have a ToArray() method for some reason. | 300 // v8::Value doesn't have a ToArray() method for some reason. |
289 if (val->IsArray()) | 301 if (val->IsArray()) |
290 return FromV8Array(val.As<v8::Array>(), state, isolate); | 302 return FromV8Array(val.As<v8::Array>(), state, isolate); |
291 | 303 |
292 if (val->IsFunction()) { | 304 if (val->IsFunction()) { |
293 if (!function_allowed_) | 305 if (!function_allowed_) |
294 // JSON.stringify refuses to convert function(){}. | 306 // JSON.stringify refuses to convert function(){}. |
295 return NULL; | 307 return NULL; |
296 return FromV8Object(val->ToObject(), state, isolate); | 308 return FromV8Object(val->ToObject(), state, isolate); |
297 } | 309 } |
298 | 310 |
299 if (val->IsObject()) { | 311 if (val->IsArrayBuffer() || val->IsArrayBufferView()) |
300 base::BinaryValue* binary_value = FromV8Buffer(val); | 312 return FromV8ArrayBuffer(val->ToObject()); |
301 if (binary_value) { | 313 |
302 return binary_value; | 314 if (val->IsObject()) |
303 } else { | 315 return FromV8Object(val->ToObject(), state, isolate); |
304 return FromV8Object(val->ToObject(), state, isolate); | |
305 } | |
306 } | |
307 | 316 |
308 LOG(ERROR) << "Unexpected v8 value type encountered."; | 317 LOG(ERROR) << "Unexpected v8 value type encountered."; |
309 return NULL; | 318 return NULL; |
310 } | 319 } |
311 | 320 |
312 base::Value* V8ValueConverterImpl::FromV8Array( | 321 base::Value* V8ValueConverterImpl::FromV8Array( |
313 v8::Handle<v8::Array> val, | 322 v8::Handle<v8::Array> val, |
314 FromV8ValueState* state, | 323 FromV8ValueState* state, |
315 v8::Isolate* isolate) const { | 324 v8::Isolate* isolate) const { |
316 if (!state->UpdateAndCheckUniqueness(val)) | 325 if (!state->UpdateAndCheckUniqueness(val)) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 if (child) | 362 if (child) |
354 result->Append(child); | 363 result->Append(child); |
355 else | 364 else |
356 // JSON.stringify puts null in places where values don't serialize, for | 365 // JSON.stringify puts null in places where values don't serialize, for |
357 // example undefined and functions. Emulate that behavior. | 366 // example undefined and functions. Emulate that behavior. |
358 result->Append(base::Value::CreateNullValue()); | 367 result->Append(base::Value::CreateNullValue()); |
359 } | 368 } |
360 return result; | 369 return result; |
361 } | 370 } |
362 | 371 |
363 base::BinaryValue* V8ValueConverterImpl::FromV8Buffer( | 372 base::Value* V8ValueConverterImpl::FromV8ArrayBuffer( |
364 v8::Handle<v8::Value> val) const { | 373 v8::Handle<v8::Object> val) const { |
| 374 if (strategy_) { |
| 375 base::Value* out = NULL; |
| 376 if (strategy_->FromV8ArrayBuffer(val, &out)) |
| 377 return out; |
| 378 } |
| 379 |
365 char* data = NULL; | 380 char* data = NULL; |
366 size_t length = 0; | 381 size_t length = 0; |
367 | 382 |
368 scoped_ptr<blink::WebArrayBuffer> array_buffer( | 383 scoped_ptr<blink::WebArrayBuffer> array_buffer( |
369 blink::WebArrayBufferConverter::createFromV8Value(val)); | 384 blink::WebArrayBufferConverter::createFromV8Value(val)); |
370 scoped_ptr<blink::WebArrayBufferView> view; | 385 scoped_ptr<blink::WebArrayBufferView> view; |
371 if (array_buffer) { | 386 if (array_buffer) { |
372 data = reinterpret_cast<char*>(array_buffer->data()); | 387 data = reinterpret_cast<char*>(array_buffer->data()); |
373 length = array_buffer->byteLength(); | 388 length = array_buffer->byteLength(); |
374 } else { | 389 } else { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 continue; | 497 continue; |
483 | 498 |
484 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), | 499 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), |
485 child.release()); | 500 child.release()); |
486 } | 501 } |
487 | 502 |
488 return result.release(); | 503 return result.release(); |
489 } | 504 } |
490 | 505 |
491 } // namespace content | 506 } // namespace content |
OLD | NEW |