OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 V8ValueConverter* V8ValueConverter::create() { | 16 V8ValueConverter* V8ValueConverter::create() { |
17 return new V8ValueConverterImpl(); | 17 return new V8ValueConverterImpl(); |
18 } | 18 } |
19 | 19 |
20 } | 20 } |
21 | 21 |
22 V8ValueConverterImpl::V8ValueConverterImpl() | 22 V8ValueConverterImpl::V8ValueConverterImpl() |
23 : allow_undefined_(false), | 23 : allow_undefined_(false), |
24 allow_date_(false), | 24 allow_date_(false), |
25 allow_regexp_(false) { | 25 allow_regexp_(false) { |
26 } | 26 } |
27 | 27 |
28 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( | 28 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( |
29 Value* value, v8::Handle<v8::Context> context) const { | 29 const Value* value, v8::Handle<v8::Context> context) const { |
30 v8::Context::Scope context_scope(context); | 30 v8::Context::Scope context_scope(context); |
31 v8::HandleScope handle_scope; | 31 v8::HandleScope handle_scope; |
32 return handle_scope.Close(ToV8ValueImpl(value)); | 32 return handle_scope.Close(ToV8ValueImpl(value)); |
33 } | 33 } |
34 | 34 |
35 Value* V8ValueConverterImpl::FromV8Value( | 35 Value* V8ValueConverterImpl::FromV8Value( |
36 v8::Handle<v8::Value> val, | 36 v8::Handle<v8::Value> val, |
37 v8::Handle<v8::Context> context) const { | 37 v8::Handle<v8::Context> context) const { |
38 v8::Context::Scope context_scope(context); | 38 v8::Context::Scope context_scope(context); |
39 v8::HandleScope handle_scope; | 39 v8::HandleScope handle_scope; |
40 return FromV8ValueImpl(val); | 40 return FromV8ValueImpl(val); |
41 } | 41 } |
42 | 42 |
43 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8ValueImpl(Value* value) const { | 43 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8ValueImpl( |
| 44 const Value* value) const { |
44 CHECK(value); | 45 CHECK(value); |
45 switch (value->GetType()) { | 46 switch (value->GetType()) { |
46 case Value::TYPE_NULL: | 47 case Value::TYPE_NULL: |
47 return v8::Null(); | 48 return v8::Null(); |
48 | 49 |
49 case Value::TYPE_BOOLEAN: { | 50 case Value::TYPE_BOOLEAN: { |
50 bool val = false; | 51 bool val = false; |
51 CHECK(value->GetAsBoolean(&val)); | 52 CHECK(value->GetAsBoolean(&val)); |
52 return v8::Boolean::New(val); | 53 return v8::Boolean::New(val); |
53 } | 54 } |
(...skipping 10 matching lines...) Expand all Loading... |
64 return v8::Number::New(val); | 65 return v8::Number::New(val); |
65 } | 66 } |
66 | 67 |
67 case Value::TYPE_STRING: { | 68 case Value::TYPE_STRING: { |
68 std::string val; | 69 std::string val; |
69 CHECK(value->GetAsString(&val)); | 70 CHECK(value->GetAsString(&val)); |
70 return v8::String::New(val.c_str(), val.length()); | 71 return v8::String::New(val.c_str(), val.length()); |
71 } | 72 } |
72 | 73 |
73 case Value::TYPE_LIST: | 74 case Value::TYPE_LIST: |
74 return ToV8Array(static_cast<ListValue*>(value)); | 75 return ToV8Array(static_cast<const ListValue*>(value)); |
75 | 76 |
76 case Value::TYPE_DICTIONARY: | 77 case Value::TYPE_DICTIONARY: |
77 return ToV8Object(static_cast<DictionaryValue*>(value)); | 78 return ToV8Object(static_cast<const DictionaryValue*>(value)); |
78 | 79 |
79 default: | 80 default: |
80 LOG(ERROR) << "Unexpected value type: " << value->GetType(); | 81 LOG(ERROR) << "Unexpected value type: " << value->GetType(); |
81 return v8::Null(); | 82 return v8::Null(); |
82 } | 83 } |
83 } | 84 } |
84 | 85 |
85 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array(ListValue* val) const { | 86 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array( |
| 87 const ListValue* val) const { |
86 v8::Handle<v8::Array> result(v8::Array::New(val->GetSize())); | 88 v8::Handle<v8::Array> result(v8::Array::New(val->GetSize())); |
87 | 89 |
88 for (size_t i = 0; i < val->GetSize(); ++i) { | 90 for (size_t i = 0; i < val->GetSize(); ++i) { |
89 Value* child = NULL; | 91 Value* child = NULL; |
90 CHECK(val->Get(i, &child)); | 92 CHECK(val->Get(i, &child)); |
91 | 93 |
92 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(child); | 94 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(child); |
93 CHECK(!child_v8.IsEmpty()); | 95 CHECK(!child_v8.IsEmpty()); |
94 | 96 |
95 v8::TryCatch try_catch; | 97 v8::TryCatch try_catch; |
96 result->Set(static_cast<uint32>(i), child_v8); | 98 result->Set(static_cast<uint32>(i), child_v8); |
97 if (try_catch.HasCaught()) | 99 if (try_catch.HasCaught()) |
98 LOG(ERROR) << "Setter for index " << i << " threw an exception."; | 100 LOG(ERROR) << "Setter for index " << i << " threw an exception."; |
99 } | 101 } |
100 | 102 |
101 return result; | 103 return result; |
102 } | 104 } |
103 | 105 |
104 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object( | 106 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object( |
105 DictionaryValue* val) const { | 107 const DictionaryValue* val) const { |
106 v8::Handle<v8::Object> result(v8::Object::New()); | 108 v8::Handle<v8::Object> result(v8::Object::New()); |
107 | 109 |
108 for (DictionaryValue::key_iterator iter = val->begin_keys(); | 110 for (DictionaryValue::key_iterator iter = val->begin_keys(); |
109 iter != val->end_keys(); ++iter) { | 111 iter != val->end_keys(); ++iter) { |
110 Value* child = NULL; | 112 Value* child = NULL; |
111 CHECK(val->GetWithoutPathExpansion(*iter, &child)); | 113 CHECK(val->GetWithoutPathExpansion(*iter, &child)); |
112 | 114 |
113 const std::string& key = *iter; | 115 const std::string& key = *iter; |
114 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(child); | 116 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(child); |
115 CHECK(!child_v8.IsEmpty()); | 117 CHECK(!child_v8.IsEmpty()); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 } | 217 } |
216 | 218 |
217 Value* child = FromV8ValueImpl(child_v8); | 219 Value* child = FromV8ValueImpl(child_v8); |
218 CHECK(child); | 220 CHECK(child); |
219 | 221 |
220 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), | 222 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), |
221 child); | 223 child); |
222 } | 224 } |
223 return result; | 225 return result; |
224 } | 226 } |
OLD | NEW |