| 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/child/v8_value_converter_impl.h" | 5 #include "content/child/v8_value_converter_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 15 #include "base/test/values_test_util.h" | 16 #include "base/test/values_test_util.h" |
| 16 #include "base/values.h" | 17 #include "base/values.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 // To improve the performance of | 23 // To improve the performance of |
| 23 // V8ValueConverterImpl::UpdateAndCheckUniqueness, identity hashes of objects | 24 // V8ValueConverterImpl::UpdateAndCheckUniqueness, identity hashes of objects |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 ASSERT_TRUE(raw.get()); | 174 ASSERT_TRUE(raw.get()); |
| 174 EXPECT_TRUE(expected_value->Equals(raw.get())); | 175 EXPECT_TRUE(expected_value->Equals(raw.get())); |
| 175 EXPECT_EQ(expected_type, raw->GetType()); | 176 EXPECT_EQ(expected_type, raw->GetType()); |
| 176 } else { | 177 } else { |
| 177 EXPECT_FALSE(raw.get()); | 178 EXPECT_FALSE(raw.get()); |
| 178 } | 179 } |
| 179 | 180 |
| 180 v8::Local<v8::Object> object(v8::Object::New(isolate_)); | 181 v8::Local<v8::Object> object(v8::Object::New(isolate_)); |
| 181 object->Set(v8::String::NewFromUtf8(isolate_, "test"), val); | 182 object->Set(v8::String::NewFromUtf8(isolate_, "test"), val); |
| 182 std::unique_ptr<base::DictionaryValue> dictionary( | 183 std::unique_ptr<base::DictionaryValue> dictionary( |
| 183 static_cast<base::DictionaryValue*>( | 184 base::DictionaryValue::From(converter.FromV8Value(object, context))); |
| 184 converter.FromV8Value(object, context))); | |
| 185 ASSERT_TRUE(dictionary.get()); | 185 ASSERT_TRUE(dictionary.get()); |
| 186 | 186 |
| 187 if (expected_value) { | 187 if (expected_value) { |
| 188 base::Value* temp = NULL; | 188 base::Value* temp = NULL; |
| 189 ASSERT_TRUE(dictionary->Get("test", &temp)); | 189 ASSERT_TRUE(dictionary->Get("test", &temp)); |
| 190 EXPECT_EQ(expected_type, temp->GetType()); | 190 EXPECT_EQ(expected_type, temp->GetType()); |
| 191 EXPECT_TRUE(expected_value->Equals(temp)); | 191 EXPECT_TRUE(expected_value->Equals(temp)); |
| 192 } else { | 192 } else { |
| 193 EXPECT_FALSE(dictionary->HasKey("test")); | 193 EXPECT_FALSE(dictionary->HasKey("test")); |
| 194 } | 194 } |
| 195 | 195 |
| 196 v8::Local<v8::Array> array(v8::Array::New(isolate_)); | 196 v8::Local<v8::Array> array(v8::Array::New(isolate_)); |
| 197 array->Set(0, val); | 197 array->Set(0, val); |
| 198 std::unique_ptr<base::ListValue> list( | 198 std::unique_ptr<base::ListValue> list( |
| 199 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 199 base::ListValue::From(converter.FromV8Value(array, context))); |
| 200 ASSERT_TRUE(list.get()); | 200 ASSERT_TRUE(list.get()); |
| 201 if (expected_value) { | 201 if (expected_value) { |
| 202 base::Value* temp = NULL; | 202 base::Value* temp = NULL; |
| 203 ASSERT_TRUE(list->Get(0, &temp)); | 203 ASSERT_TRUE(list->Get(0, &temp)); |
| 204 EXPECT_EQ(expected_type, temp->GetType()); | 204 EXPECT_EQ(expected_type, temp->GetType()); |
| 205 EXPECT_TRUE(expected_value->Equals(temp)); | 205 EXPECT_TRUE(expected_value->Equals(temp)); |
| 206 } else { | 206 } else { |
| 207 // Arrays should preserve their length, and convert unconvertible | 207 // Arrays should preserve their length, and convert unconvertible |
| 208 // types into null. | 208 // types into null. |
| 209 base::Value* temp = NULL; | 209 base::Value* temp = NULL; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 322 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 323 script->Run(); | 323 script->Run(); |
| 324 | 324 |
| 325 v8::Local<v8::Object> object(v8::Object::New(isolate_)); | 325 v8::Local<v8::Object> object(v8::Object::New(isolate_)); |
| 326 object->Set(v8::String::NewFromUtf8(isolate_, "bar"), | 326 object->Set(v8::String::NewFromUtf8(isolate_, "bar"), |
| 327 v8::String::NewFromUtf8(isolate_, "bar")); | 327 v8::String::NewFromUtf8(isolate_, "bar")); |
| 328 | 328 |
| 329 // Converting from v8 value should replace the foo property with null. | 329 // Converting from v8 value should replace the foo property with null. |
| 330 V8ValueConverterImpl converter; | 330 V8ValueConverterImpl converter; |
| 331 std::unique_ptr<base::DictionaryValue> converted( | 331 std::unique_ptr<base::DictionaryValue> converted( |
| 332 static_cast<base::DictionaryValue*>( | 332 base::DictionaryValue::From(converter.FromV8Value(object, context))); |
| 333 converter.FromV8Value(object, context))); | |
| 334 EXPECT_TRUE(converted.get()); | 333 EXPECT_TRUE(converted.get()); |
| 335 // http://code.google.com/p/v8/issues/detail?id=1342 | 334 // http://code.google.com/p/v8/issues/detail?id=1342 |
| 336 // EXPECT_EQ(2u, converted->size()); | 335 // EXPECT_EQ(2u, converted->size()); |
| 337 // EXPECT_TRUE(IsNull(converted.get(), "foo")); | 336 // EXPECT_TRUE(IsNull(converted.get(), "foo")); |
| 338 EXPECT_EQ(1u, converted->size()); | 337 EXPECT_EQ(1u, converted->size()); |
| 339 EXPECT_EQ("bar", GetString(converted.get(), "bar")); | 338 EXPECT_EQ("bar", GetString(converted.get(), "bar")); |
| 340 | 339 |
| 341 // Converting to v8 value should not trigger the setter. | 340 // Converting to v8 value should not trigger the setter. |
| 342 converted->SetString("foo", "foo"); | 341 converted->SetString("foo", "foo"); |
| 343 v8::Local<v8::Object> copy = | 342 v8::Local<v8::Object> copy = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 367 "})();"; | 366 "})();"; |
| 368 | 367 |
| 369 v8::Local<v8::Script> script( | 368 v8::Local<v8::Script> script( |
| 370 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 369 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 371 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); | 370 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
| 372 ASSERT_FALSE(array.IsEmpty()); | 371 ASSERT_FALSE(array.IsEmpty()); |
| 373 | 372 |
| 374 // Converting from v8 value should replace the first item with null. | 373 // Converting from v8 value should replace the first item with null. |
| 375 V8ValueConverterImpl converter; | 374 V8ValueConverterImpl converter; |
| 376 std::unique_ptr<base::ListValue> converted( | 375 std::unique_ptr<base::ListValue> converted( |
| 377 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 376 base::ListValue::From(converter.FromV8Value(array, context))); |
| 378 ASSERT_TRUE(converted.get()); | 377 ASSERT_TRUE(converted.get()); |
| 379 // http://code.google.com/p/v8/issues/detail?id=1342 | 378 // http://code.google.com/p/v8/issues/detail?id=1342 |
| 380 EXPECT_EQ(2u, converted->GetSize()); | 379 EXPECT_EQ(2u, converted->GetSize()); |
| 381 EXPECT_TRUE(IsNull(converted.get(), 0)); | 380 EXPECT_TRUE(IsNull(converted.get(), 0)); |
| 382 | 381 |
| 383 // Converting to v8 value should not be affected by the getter/setter | 382 // Converting to v8 value should not be affected by the getter/setter |
| 384 // because the setters/getters are defined on the array instance, not | 383 // because the setters/getters are defined on the array instance, not |
| 385 // on the Array's prototype. | 384 // on the Array's prototype. |
| 386 converted.reset(static_cast<base::ListValue*>( | 385 converted.reset(static_cast<base::ListValue*>( |
| 387 base::test::ParseJson("[ \"foo\", \"bar\" ]").release())); | 386 base::test::ParseJson("[ \"foo\", \"bar\" ]").release())); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 "return {};" | 434 "return {};" |
| 436 "})();"; | 435 "})();"; |
| 437 | 436 |
| 438 v8::Local<v8::Script> script( | 437 v8::Local<v8::Script> script( |
| 439 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 438 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 440 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); | 439 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 441 ASSERT_FALSE(object.IsEmpty()); | 440 ASSERT_FALSE(object.IsEmpty()); |
| 442 | 441 |
| 443 V8ValueConverterImpl converter; | 442 V8ValueConverterImpl converter; |
| 444 std::unique_ptr<base::DictionaryValue> result( | 443 std::unique_ptr<base::DictionaryValue> result( |
| 445 static_cast<base::DictionaryValue*>( | 444 base::DictionaryValue::From(converter.FromV8Value(object, context))); |
| 446 converter.FromV8Value(object, context))); | |
| 447 ASSERT_TRUE(result.get()); | 445 ASSERT_TRUE(result.get()); |
| 448 EXPECT_EQ(0u, result->size()); | 446 EXPECT_EQ(0u, result->size()); |
| 449 } | 447 } |
| 450 | 448 |
| 451 TEST_F(V8ValueConverterImplTest, ObjectPrototypeSetter) { | 449 TEST_F(V8ValueConverterImplTest, ObjectPrototypeSetter) { |
| 452 std::unique_ptr<base::Value> original = | 450 std::unique_ptr<base::Value> original = |
| 453 base::test::ParseJson("{ \"foo\": \"good value\" }"); | 451 base::test::ParseJson("{ \"foo\": \"good value\" }"); |
| 454 | 452 |
| 455 v8::HandleScope handle_scope(isolate_); | 453 v8::HandleScope handle_scope(isolate_); |
| 456 v8::Local<v8::Context> context = | 454 v8::Local<v8::Context> context = |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 | 600 |
| 603 v8::Local<v8::Script> script( | 601 v8::Local<v8::Script> script( |
| 604 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 602 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 605 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); | 603 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 606 ASSERT_FALSE(object.IsEmpty()); | 604 ASSERT_FALSE(object.IsEmpty()); |
| 607 | 605 |
| 608 V8ValueConverterImpl converter; | 606 V8ValueConverterImpl converter; |
| 609 converter.SetStripNullFromObjects(true); | 607 converter.SetStripNullFromObjects(true); |
| 610 | 608 |
| 611 std::unique_ptr<base::DictionaryValue> result( | 609 std::unique_ptr<base::DictionaryValue> result( |
| 612 static_cast<base::DictionaryValue*>( | 610 base::DictionaryValue::From(converter.FromV8Value(object, context))); |
| 613 converter.FromV8Value(object, context))); | |
| 614 ASSERT_TRUE(result.get()); | 611 ASSERT_TRUE(result.get()); |
| 615 EXPECT_EQ(0u, result->size()); | 612 EXPECT_EQ(0u, result->size()); |
| 616 } | 613 } |
| 617 | 614 |
| 618 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { | 615 TEST_F(V8ValueConverterImplTest, RecursiveObjects) { |
| 619 v8::HandleScope handle_scope(isolate_); | 616 v8::HandleScope handle_scope(isolate_); |
| 620 v8::Local<v8::Context> context = | 617 v8::Local<v8::Context> context = |
| 621 v8::Local<v8::Context>::New(isolate_, context_); | 618 v8::Local<v8::Context>::New(isolate_, context_); |
| 622 v8::Context::Scope context_scope(context); | 619 v8::Context::Scope context_scope(context); |
| 623 | 620 |
| 624 V8ValueConverterImpl converter; | 621 V8ValueConverterImpl converter; |
| 625 | 622 |
| 626 v8::Local<v8::Object> object = v8::Object::New(isolate_).As<v8::Object>(); | 623 v8::Local<v8::Object> object = v8::Object::New(isolate_).As<v8::Object>(); |
| 627 ASSERT_FALSE(object.IsEmpty()); | 624 ASSERT_FALSE(object.IsEmpty()); |
| 628 object->Set(v8::String::NewFromUtf8(isolate_, "foo"), | 625 object->Set(v8::String::NewFromUtf8(isolate_, "foo"), |
| 629 v8::String::NewFromUtf8(isolate_, "bar")); | 626 v8::String::NewFromUtf8(isolate_, "bar")); |
| 630 object->Set(v8::String::NewFromUtf8(isolate_, "obj"), object); | 627 object->Set(v8::String::NewFromUtf8(isolate_, "obj"), object); |
| 631 | 628 |
| 632 std::unique_ptr<base::DictionaryValue> object_result( | 629 std::unique_ptr<base::DictionaryValue> object_result( |
| 633 static_cast<base::DictionaryValue*>( | 630 base::DictionaryValue::From(converter.FromV8Value(object, context))); |
| 634 converter.FromV8Value(object, context))); | |
| 635 ASSERT_TRUE(object_result.get()); | 631 ASSERT_TRUE(object_result.get()); |
| 636 EXPECT_EQ(2u, object_result->size()); | 632 EXPECT_EQ(2u, object_result->size()); |
| 637 EXPECT_TRUE(IsNull(object_result.get(), "obj")); | 633 EXPECT_TRUE(IsNull(object_result.get(), "obj")); |
| 638 | 634 |
| 639 v8::Local<v8::Array> array = v8::Array::New(isolate_).As<v8::Array>(); | 635 v8::Local<v8::Array> array = v8::Array::New(isolate_).As<v8::Array>(); |
| 640 ASSERT_FALSE(array.IsEmpty()); | 636 ASSERT_FALSE(array.IsEmpty()); |
| 641 array->Set(0, v8::String::NewFromUtf8(isolate_, "1")); | 637 array->Set(0, v8::String::NewFromUtf8(isolate_, "1")); |
| 642 array->Set(1, array); | 638 array->Set(1, array); |
| 643 | 639 |
| 644 std::unique_ptr<base::ListValue> list_result( | 640 std::unique_ptr<base::ListValue> list_result( |
| 645 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 641 base::ListValue::From(converter.FromV8Value(array, context))); |
| 646 ASSERT_TRUE(list_result.get()); | 642 ASSERT_TRUE(list_result.get()); |
| 647 EXPECT_EQ(2u, list_result->GetSize()); | 643 EXPECT_EQ(2u, list_result->GetSize()); |
| 648 EXPECT_TRUE(IsNull(list_result.get(), 1)); | 644 EXPECT_TRUE(IsNull(list_result.get(), 1)); |
| 649 } | 645 } |
| 650 | 646 |
| 651 TEST_F(V8ValueConverterImplTest, WeirdProperties) { | 647 TEST_F(V8ValueConverterImplTest, WeirdProperties) { |
| 652 v8::HandleScope handle_scope(isolate_); | 648 v8::HandleScope handle_scope(isolate_); |
| 653 v8::Local<v8::Context> context = | 649 v8::Local<v8::Context> context = |
| 654 v8::Local<v8::Context>::New(isolate_, context_); | 650 v8::Local<v8::Context>::New(isolate_, context_); |
| 655 v8::Context::Scope context_scope(context); | 651 v8::Context::Scope context_scope(context); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 "return a;" | 698 "return a;" |
| 703 "})();"; | 699 "})();"; |
| 704 | 700 |
| 705 v8::Local<v8::Script> script( | 701 v8::Local<v8::Script> script( |
| 706 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 702 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 707 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); | 703 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
| 708 ASSERT_FALSE(array.IsEmpty()); | 704 ASSERT_FALSE(array.IsEmpty()); |
| 709 | 705 |
| 710 V8ValueConverterImpl converter; | 706 V8ValueConverterImpl converter; |
| 711 std::unique_ptr<base::ListValue> result( | 707 std::unique_ptr<base::ListValue> result( |
| 712 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 708 base::ListValue::From(converter.FromV8Value(array, context))); |
| 713 ASSERT_TRUE(result.get()); | 709 ASSERT_TRUE(result.get()); |
| 714 EXPECT_EQ(2u, result->GetSize()); | 710 EXPECT_EQ(2u, result->GetSize()); |
| 715 } | 711 } |
| 716 | 712 |
| 717 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { | 713 TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) { |
| 718 v8::HandleScope handle_scope(isolate_); | 714 v8::HandleScope handle_scope(isolate_); |
| 719 v8::Local<v8::Context> context = | 715 v8::Local<v8::Context> context = |
| 720 v8::Local<v8::Context>::New(isolate_, context_); | 716 v8::Local<v8::Context>::New(isolate_, context_); |
| 721 v8::Context::Scope context_scope(context); | 717 v8::Context::Scope context_scope(context); |
| 722 v8::MicrotasksScope microtasks( | 718 v8::MicrotasksScope microtasks( |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 866 "var obj = {one: objA, two: objA};" | 862 "var obj = {one: objA, two: objA};" |
| 867 "return obj;" | 863 "return obj;" |
| 868 "})();"; | 864 "})();"; |
| 869 v8::Local<v8::Script> script( | 865 v8::Local<v8::Script> script( |
| 870 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 866 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 871 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); | 867 v8::Local<v8::Object> object = script->Run().As<v8::Object>(); |
| 872 ASSERT_FALSE(object.IsEmpty()); | 868 ASSERT_FALSE(object.IsEmpty()); |
| 873 | 869 |
| 874 // The actual result. | 870 // The actual result. |
| 875 std::unique_ptr<base::DictionaryValue> result( | 871 std::unique_ptr<base::DictionaryValue> result( |
| 876 static_cast<base::DictionaryValue*>( | 872 base::DictionaryValue::From(converter.FromV8Value(object, context))); |
| 877 converter.FromV8Value(object, context))); | |
| 878 ASSERT_TRUE(result.get()); | 873 ASSERT_TRUE(result.get()); |
| 879 EXPECT_EQ(2u, result->size()); | 874 EXPECT_EQ(2u, result->size()); |
| 880 | 875 |
| 881 { | 876 { |
| 882 base::DictionaryValue* one_dict = nullptr; | 877 base::DictionaryValue* one_dict = nullptr; |
| 883 const char* key1 = "one"; | 878 const char* key1 = "one"; |
| 884 ASSERT_TRUE(result->GetDictionary(key1, &one_dict)); | 879 ASSERT_TRUE(result->GetDictionary(key1, &one_dict)); |
| 885 EXPECT_EQ("another same value", GetString(one_dict, "key")); | 880 EXPECT_EQ("another same value", GetString(one_dict, "key")); |
| 886 } | 881 } |
| 887 { | 882 { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 899 "var arr = [objA, objA];" | 894 "var arr = [objA, objA];" |
| 900 "return arr;" | 895 "return arr;" |
| 901 "})();"; | 896 "})();"; |
| 902 v8::Local<v8::Script> script( | 897 v8::Local<v8::Script> script( |
| 903 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); | 898 v8::Script::Compile(v8::String::NewFromUtf8(isolate_, source))); |
| 904 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); | 899 v8::Local<v8::Array> array = script->Run().As<v8::Array>(); |
| 905 ASSERT_FALSE(array.IsEmpty()); | 900 ASSERT_FALSE(array.IsEmpty()); |
| 906 | 901 |
| 907 // The actual result. | 902 // The actual result. |
| 908 std::unique_ptr<base::ListValue> list_result( | 903 std::unique_ptr<base::ListValue> list_result( |
| 909 static_cast<base::ListValue*>(converter.FromV8Value(array, context))); | 904 base::ListValue::From(converter.FromV8Value(array, context))); |
| 910 ASSERT_TRUE(list_result.get()); | 905 ASSERT_TRUE(list_result.get()); |
| 911 ASSERT_EQ(2u, list_result->GetSize()); | 906 ASSERT_EQ(2u, list_result->GetSize()); |
| 912 for (size_t i = 0; i < list_result->GetSize(); ++i) { | 907 for (size_t i = 0; i < list_result->GetSize(); ++i) { |
| 913 ASSERT_FALSE(IsNull(list_result.get(), i)); | 908 ASSERT_FALSE(IsNull(list_result.get(), i)); |
| 914 base::DictionaryValue* dict_value = nullptr; | 909 base::DictionaryValue* dict_value = nullptr; |
| 915 ASSERT_TRUE(list_result->GetDictionary(0u, &dict_value)); | 910 ASSERT_TRUE(list_result->GetDictionary(0u, &dict_value)); |
| 916 EXPECT_EQ("same value", GetString(dict_value, "key")); | 911 EXPECT_EQ("same value", GetString(dict_value, "key")); |
| 917 } | 912 } |
| 918 } | 913 } |
| 919 } | 914 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 956 base::DictionaryValue empty; | 951 base::DictionaryValue empty; |
| 957 EXPECT_TRUE(base::Value::Equals(&empty, current)) << *current; | 952 EXPECT_TRUE(base::Value::Equals(&empty, current)) << *current; |
| 958 } | 953 } |
| 959 | 954 |
| 960 class V8ValueConverterOverridingStrategyForTesting | 955 class V8ValueConverterOverridingStrategyForTesting |
| 961 : public V8ValueConverter::Strategy { | 956 : public V8ValueConverter::Strategy { |
| 962 public: | 957 public: |
| 963 V8ValueConverterOverridingStrategyForTesting() | 958 V8ValueConverterOverridingStrategyForTesting() |
| 964 : reference_value_(NewReferenceValue()) {} | 959 : reference_value_(NewReferenceValue()) {} |
| 965 bool FromV8Object(v8::Local<v8::Object> value, | 960 bool FromV8Object(v8::Local<v8::Object> value, |
| 966 base::Value** out, | 961 std::unique_ptr<base::Value>* out, |
| 967 v8::Isolate* isolate, | 962 v8::Isolate* isolate, |
| 968 const FromV8ValueCallback& callback) const override { | 963 const FromV8ValueCallback& callback) const override { |
| 969 *out = NewReferenceValue(); | 964 *out = NewReferenceValue(); |
| 970 return true; | 965 return true; |
| 971 } | 966 } |
| 972 bool FromV8Array(v8::Local<v8::Array> value, | 967 bool FromV8Array(v8::Local<v8::Array> value, |
| 973 base::Value** out, | 968 std::unique_ptr<base::Value>* out, |
| 974 v8::Isolate* isolate, | 969 v8::Isolate* isolate, |
| 975 const FromV8ValueCallback& callback) const override { | 970 const FromV8ValueCallback& callback) const override { |
| 976 *out = NewReferenceValue(); | 971 *out = NewReferenceValue(); |
| 977 return true; | 972 return true; |
| 978 } | 973 } |
| 979 bool FromV8ArrayBuffer(v8::Local<v8::Object> value, | 974 bool FromV8ArrayBuffer(v8::Local<v8::Object> value, |
| 980 base::Value** out, | 975 std::unique_ptr<base::Value>* out, |
| 981 v8::Isolate* isolate) const override { | 976 v8::Isolate* isolate) const override { |
| 982 *out = NewReferenceValue(); | 977 *out = NewReferenceValue(); |
| 983 return true; | 978 return true; |
| 984 } | 979 } |
| 985 bool FromV8Number(v8::Local<v8::Number> value, | 980 bool FromV8Number(v8::Local<v8::Number> value, |
| 986 base::Value** out) const override { | 981 std::unique_ptr<base::Value>* out) const override { |
| 987 *out = NewReferenceValue(); | 982 *out = NewReferenceValue(); |
| 988 return true; | 983 return true; |
| 989 } | 984 } |
| 990 bool FromV8Undefined(base::Value** out) const override { | 985 bool FromV8Undefined(std::unique_ptr<base::Value>* out) const override { |
| 991 *out = NewReferenceValue(); | 986 *out = NewReferenceValue(); |
| 992 return true; | 987 return true; |
| 993 } | 988 } |
| 994 base::Value* reference_value() const { return reference_value_.get(); } | 989 base::Value* reference_value() const { return reference_value_.get(); } |
| 995 | 990 |
| 996 private: | 991 private: |
| 997 static base::Value* NewReferenceValue() { | 992 static std::unique_ptr<base::Value> NewReferenceValue() { |
| 998 return new base::StringValue("strategy"); | 993 return base::MakeUnique<base::StringValue>("strategy"); |
| 999 } | 994 } |
| 1000 std::unique_ptr<base::Value> reference_value_; | 995 std::unique_ptr<base::Value> reference_value_; |
| 1001 }; | 996 }; |
| 1002 | 997 |
| 1003 TEST_F(V8ValueConverterImplTest, StrategyOverrides) { | 998 TEST_F(V8ValueConverterImplTest, StrategyOverrides) { |
| 1004 v8::HandleScope handle_scope(isolate_); | 999 v8::HandleScope handle_scope(isolate_); |
| 1005 v8::Local<v8::Context> context = | 1000 v8::Local<v8::Context> context = |
| 1006 v8::Local<v8::Context>::New(isolate_, context_); | 1001 v8::Local<v8::Context>::New(isolate_, context_); |
| 1007 v8::Context::Scope context_scope(context); | 1002 v8::Context::Scope context_scope(context); |
| 1008 | 1003 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1051 converter.FromV8Value(undefined, context)); | 1046 converter.FromV8Value(undefined, context)); |
| 1052 ASSERT_TRUE(undefined_value); | 1047 ASSERT_TRUE(undefined_value); |
| 1053 EXPECT_TRUE( | 1048 EXPECT_TRUE( |
| 1054 base::Value::Equals(strategy.reference_value(), undefined_value.get())); | 1049 base::Value::Equals(strategy.reference_value(), undefined_value.get())); |
| 1055 } | 1050 } |
| 1056 | 1051 |
| 1057 class V8ValueConverterBypassStrategyForTesting | 1052 class V8ValueConverterBypassStrategyForTesting |
| 1058 : public V8ValueConverter::Strategy { | 1053 : public V8ValueConverter::Strategy { |
| 1059 public: | 1054 public: |
| 1060 bool FromV8Object(v8::Local<v8::Object> value, | 1055 bool FromV8Object(v8::Local<v8::Object> value, |
| 1061 base::Value** out, | 1056 std::unique_ptr<base::Value>* out, |
| 1062 v8::Isolate* isolate, | 1057 v8::Isolate* isolate, |
| 1063 const FromV8ValueCallback& callback) const override { | 1058 const FromV8ValueCallback& callback) const override { |
| 1064 return false; | 1059 return false; |
| 1065 } | 1060 } |
| 1066 bool FromV8Array(v8::Local<v8::Array> value, | 1061 bool FromV8Array(v8::Local<v8::Array> value, |
| 1067 base::Value** out, | 1062 std::unique_ptr<base::Value>* out, |
| 1068 v8::Isolate* isolate, | 1063 v8::Isolate* isolate, |
| 1069 const FromV8ValueCallback& callback) const override { | 1064 const FromV8ValueCallback& callback) const override { |
| 1070 return false; | 1065 return false; |
| 1071 } | 1066 } |
| 1072 bool FromV8ArrayBuffer(v8::Local<v8::Object> value, | 1067 bool FromV8ArrayBuffer(v8::Local<v8::Object> value, |
| 1073 base::Value** out, | 1068 std::unique_ptr<base::Value>* out, |
| 1074 v8::Isolate* isolate) const override { | 1069 v8::Isolate* isolate) const override { |
| 1075 return false; | 1070 return false; |
| 1076 } | 1071 } |
| 1077 bool FromV8Number(v8::Local<v8::Number> value, | 1072 bool FromV8Number(v8::Local<v8::Number> value, |
| 1078 base::Value** out) const override { | 1073 std::unique_ptr<base::Value>* out) const override { |
| 1079 return false; | 1074 return false; |
| 1080 } | 1075 } |
| 1081 bool FromV8Undefined(base::Value** out) const override { return false; } | 1076 bool FromV8Undefined(std::unique_ptr<base::Value>* out) const override { |
| 1077 return false; |
| 1078 } |
| 1082 }; | 1079 }; |
| 1083 | 1080 |
| 1084 // Verify that having a strategy that fallbacks to default behaviour | 1081 // Verify that having a strategy that fallbacks to default behaviour |
| 1085 // actually preserves it. | 1082 // actually preserves it. |
| 1086 TEST_F(V8ValueConverterImplTest, StrategyBypass) { | 1083 TEST_F(V8ValueConverterImplTest, StrategyBypass) { |
| 1087 v8::HandleScope handle_scope(isolate_); | 1084 v8::HandleScope handle_scope(isolate_); |
| 1088 v8::Local<v8::Context> context = | 1085 v8::Local<v8::Context> context = |
| 1089 v8::Local<v8::Context>::New(isolate_, context_); | 1086 v8::Local<v8::Context>::New(isolate_, context_); |
| 1090 v8::Context::Scope context_scope(context); | 1087 v8::Context::Scope context_scope(context); |
| 1091 | 1088 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1123 EXPECT_TRUE( | 1120 EXPECT_TRUE( |
| 1124 base::Value::Equals(reference_number_value.get(), number_value.get())); | 1121 base::Value::Equals(reference_number_value.get(), number_value.get())); |
| 1125 | 1122 |
| 1126 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); | 1123 v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_)); |
| 1127 std::unique_ptr<base::Value> undefined_value( | 1124 std::unique_ptr<base::Value> undefined_value( |
| 1128 converter.FromV8Value(undefined, context)); | 1125 converter.FromV8Value(undefined, context)); |
| 1129 EXPECT_FALSE(undefined_value); | 1126 EXPECT_FALSE(undefined_value); |
| 1130 } | 1127 } |
| 1131 | 1128 |
| 1132 } // namespace content | 1129 } // namespace content |
| OLD | NEW |