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/float_util.h" | 9 #include "base/float_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 79 |
80 V8ValueConverter* V8ValueConverter::create() { | 80 V8ValueConverter* V8ValueConverter::create() { |
81 return new V8ValueConverterImpl(); | 81 return new V8ValueConverterImpl(); |
82 } | 82 } |
83 | 83 |
84 V8ValueConverterImpl::V8ValueConverterImpl() | 84 V8ValueConverterImpl::V8ValueConverterImpl() |
85 : date_allowed_(false), | 85 : date_allowed_(false), |
86 reg_exp_allowed_(false), | 86 reg_exp_allowed_(false), |
87 function_allowed_(false), | 87 function_allowed_(false), |
88 strip_null_from_objects_(false), | 88 strip_null_from_objects_(false), |
89 avoid_identity_hash_for_testing_(false) {} | 89 avoid_identity_hash_for_testing_(false), |
| 90 strategy_(NULL) {} |
90 | 91 |
91 void V8ValueConverterImpl::SetDateAllowed(bool val) { | 92 void V8ValueConverterImpl::SetDateAllowed(bool val) { |
92 date_allowed_ = val; | 93 date_allowed_ = val; |
93 } | 94 } |
94 | 95 |
95 void V8ValueConverterImpl::SetRegExpAllowed(bool val) { | 96 void V8ValueConverterImpl::SetRegExpAllowed(bool val) { |
96 reg_exp_allowed_ = val; | 97 reg_exp_allowed_ = val; |
97 } | 98 } |
98 | 99 |
99 void V8ValueConverterImpl::SetFunctionAllowed(bool val) { | 100 void V8ValueConverterImpl::SetFunctionAllowed(bool val) { |
100 function_allowed_ = val; | 101 function_allowed_ = val; |
101 } | 102 } |
102 | 103 |
103 void V8ValueConverterImpl::SetStripNullFromObjects(bool val) { | 104 void V8ValueConverterImpl::SetStripNullFromObjects(bool val) { |
104 strip_null_from_objects_ = val; | 105 strip_null_from_objects_ = val; |
105 } | 106 } |
106 | 107 |
| 108 void V8ValueConverterImpl::SetStrategy(Strategy* strategy) { |
| 109 strategy_ = strategy; |
| 110 } |
| 111 |
107 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( | 112 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( |
108 const base::Value* value, v8::Handle<v8::Context> context) const { | 113 const base::Value* value, v8::Handle<v8::Context> context) const { |
109 v8::Context::Scope context_scope(context); | 114 v8::Context::Scope context_scope(context); |
110 v8::HandleScope handle_scope; | 115 v8::HandleScope handle_scope; |
111 return handle_scope.Close(ToV8ValueImpl(value)); | 116 return handle_scope.Close(ToV8ValueImpl(value)); |
112 } | 117 } |
113 | 118 |
114 Value* V8ValueConverterImpl::FromV8Value( | 119 Value* V8ValueConverterImpl::FromV8Value( |
115 v8::Handle<v8::Value> val, | 120 v8::Handle<v8::Value> val, |
116 v8::Handle<v8::Context> context) const { | 121 v8::Handle<v8::Context> context) const { |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 if (!state->UpdateAndCheckUniqueness(val)) | 300 if (!state->UpdateAndCheckUniqueness(val)) |
296 return base::Value::CreateNullValue(); | 301 return base::Value::CreateNullValue(); |
297 | 302 |
298 scoped_ptr<v8::Context::Scope> scope; | 303 scoped_ptr<v8::Context::Scope> scope; |
299 // If val was created in a different context than our current one, change to | 304 // If val was created in a different context than our current one, change to |
300 // that context, but change back after val is converted. | 305 // that context, but change back after val is converted. |
301 if (!val->CreationContext().IsEmpty() && | 306 if (!val->CreationContext().IsEmpty() && |
302 val->CreationContext() != v8::Context::GetCurrent()) | 307 val->CreationContext() != v8::Context::GetCurrent()) |
303 scope.reset(new v8::Context::Scope(val->CreationContext())); | 308 scope.reset(new v8::Context::Scope(val->CreationContext())); |
304 | 309 |
| 310 if (strategy_) { |
| 311 Value* out = NULL; |
| 312 if (strategy_->FromV8Array(val, &out)) |
| 313 return out; |
| 314 } |
| 315 |
305 base::ListValue* result = new base::ListValue(); | 316 base::ListValue* result = new base::ListValue(); |
306 | 317 |
307 // Only fields with integer keys are carried over to the ListValue. | 318 // Only fields with integer keys are carried over to the ListValue. |
308 for (uint32 i = 0; i < val->Length(); ++i) { | 319 for (uint32 i = 0; i < val->Length(); ++i) { |
309 v8::TryCatch try_catch; | 320 v8::TryCatch try_catch; |
310 v8::Handle<v8::Value> child_v8 = val->Get(i); | 321 v8::Handle<v8::Value> child_v8 = val->Get(i); |
311 if (try_catch.HasCaught()) { | 322 if (try_catch.HasCaught()) { |
312 LOG(ERROR) << "Getter for index " << i << " threw an exception."; | 323 LOG(ERROR) << "Getter for index " << i << " threw an exception."; |
313 child_v8 = v8::Null(); | 324 child_v8 = v8::Null(); |
314 } | 325 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 return base::BinaryValue::CreateWithCopiedBuffer(data, length); | 361 return base::BinaryValue::CreateWithCopiedBuffer(data, length); |
351 else | 362 else |
352 return NULL; | 363 return NULL; |
353 } | 364 } |
354 | 365 |
355 base::Value* V8ValueConverterImpl::FromV8Object( | 366 base::Value* V8ValueConverterImpl::FromV8Object( |
356 v8::Handle<v8::Object> val, | 367 v8::Handle<v8::Object> val, |
357 FromV8ValueState* state) const { | 368 FromV8ValueState* state) const { |
358 if (!state->UpdateAndCheckUniqueness(val)) | 369 if (!state->UpdateAndCheckUniqueness(val)) |
359 return base::Value::CreateNullValue(); | 370 return base::Value::CreateNullValue(); |
| 371 |
360 scoped_ptr<v8::Context::Scope> scope; | 372 scoped_ptr<v8::Context::Scope> scope; |
361 // If val was created in a different context than our current one, change to | 373 // If val was created in a different context than our current one, change to |
362 // that context, but change back after val is converted. | 374 // that context, but change back after val is converted. |
363 if (!val->CreationContext().IsEmpty() && | 375 if (!val->CreationContext().IsEmpty() && |
364 val->CreationContext() != v8::Context::GetCurrent()) | 376 val->CreationContext() != v8::Context::GetCurrent()) |
365 scope.reset(new v8::Context::Scope(val->CreationContext())); | 377 scope.reset(new v8::Context::Scope(val->CreationContext())); |
366 | 378 |
| 379 if (strategy_) { |
| 380 Value* out = NULL; |
| 381 if (strategy_->FromV8Object(val, &out)) |
| 382 return out; |
| 383 } |
| 384 |
367 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 385 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
368 v8::Handle<v8::Array> property_names(val->GetOwnPropertyNames()); | 386 v8::Handle<v8::Array> property_names(val->GetOwnPropertyNames()); |
369 | 387 |
370 for (uint32 i = 0; i < property_names->Length(); ++i) { | 388 for (uint32 i = 0; i < property_names->Length(); ++i) { |
371 v8::Handle<v8::Value> key(property_names->Get(i)); | 389 v8::Handle<v8::Value> key(property_names->Get(i)); |
372 | 390 |
373 // Extend this test to cover more types as necessary and if sensible. | 391 // Extend this test to cover more types as necessary and if sensible. |
374 if (!key->IsString() && | 392 if (!key->IsString() && |
375 !key->IsNumber()) { | 393 !key->IsNumber()) { |
376 NOTREACHED() << "Key \"" << *v8::String::AsciiValue(key) << "\" " | 394 NOTREACHED() << "Key \"" << *v8::String::AsciiValue(key) << "\" " |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 continue; | 437 continue; |
420 | 438 |
421 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), | 439 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), |
422 child.release()); | 440 child.release()); |
423 } | 441 } |
424 | 442 |
425 return result.release(); | 443 return result.release(); |
426 } | 444 } |
427 | 445 |
428 } // namespace content | 446 } // namespace content |
OLD | NEW |