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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 } | 146 } |
147 | 147 |
148 void V8ValueConverterImpl::SetStrategy(Strategy* strategy) { | 148 void V8ValueConverterImpl::SetStrategy(Strategy* strategy) { |
149 strategy_ = strategy; | 149 strategy_ = strategy; |
150 } | 150 } |
151 | 151 |
152 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( | 152 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value( |
153 const base::Value* value, v8::Handle<v8::Context> context) const { | 153 const base::Value* value, v8::Handle<v8::Context> context) const { |
154 v8::Context::Scope context_scope(context); | 154 v8::Context::Scope context_scope(context); |
155 v8::EscapableHandleScope handle_scope(context->GetIsolate()); | 155 v8::EscapableHandleScope handle_scope(context->GetIsolate()); |
156 return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value)); | 156 return handle_scope.Escape( |
157 ToV8ValueImpl(context->GetIsolate(), context->Global(), value)); | |
157 } | 158 } |
158 | 159 |
159 base::Value* V8ValueConverterImpl::FromV8Value( | 160 base::Value* V8ValueConverterImpl::FromV8Value( |
160 v8::Handle<v8::Value> val, | 161 v8::Handle<v8::Value> val, |
161 v8::Handle<v8::Context> context) const { | 162 v8::Handle<v8::Context> context) const { |
162 v8::Context::Scope context_scope(context); | 163 v8::Context::Scope context_scope(context); |
163 v8::HandleScope handle_scope(context->GetIsolate()); | 164 v8::HandleScope handle_scope(context->GetIsolate()); |
164 FromV8ValueState state(avoid_identity_hash_for_testing_); | 165 FromV8ValueState state(avoid_identity_hash_for_testing_); |
165 return FromV8ValueImpl(&state, val, context->GetIsolate()); | 166 return FromV8ValueImpl(&state, val, context->GetIsolate()); |
166 } | 167 } |
167 | 168 |
168 v8::Local<v8::Value> V8ValueConverterImpl::ToV8ValueImpl( | 169 v8::Local<v8::Value> V8ValueConverterImpl::ToV8ValueImpl( |
169 v8::Isolate* isolate, | 170 v8::Isolate* isolate, |
171 v8::Handle<v8::Object> creationContext, | |
darin (slow to review)
2014/06/10 04:52:01
ditto... there are more. please fix-up code to mat
tasak
2014/06/10 05:09:15
Done.
| |
170 const base::Value* value) const { | 172 const base::Value* value) const { |
171 CHECK(value); | 173 CHECK(value); |
172 switch (value->GetType()) { | 174 switch (value->GetType()) { |
173 case base::Value::TYPE_NULL: | 175 case base::Value::TYPE_NULL: |
174 return v8::Null(isolate); | 176 return v8::Null(isolate); |
175 | 177 |
176 case base::Value::TYPE_BOOLEAN: { | 178 case base::Value::TYPE_BOOLEAN: { |
177 bool val = false; | 179 bool val = false; |
178 CHECK(value->GetAsBoolean(&val)); | 180 CHECK(value->GetAsBoolean(&val)); |
179 return v8::Boolean::New(isolate, val); | 181 return v8::Boolean::New(isolate, val); |
(...skipping 12 matching lines...) Expand all Loading... | |
192 } | 194 } |
193 | 195 |
194 case base::Value::TYPE_STRING: { | 196 case base::Value::TYPE_STRING: { |
195 std::string val; | 197 std::string val; |
196 CHECK(value->GetAsString(&val)); | 198 CHECK(value->GetAsString(&val)); |
197 return v8::String::NewFromUtf8( | 199 return v8::String::NewFromUtf8( |
198 isolate, val.c_str(), v8::String::kNormalString, val.length()); | 200 isolate, val.c_str(), v8::String::kNormalString, val.length()); |
199 } | 201 } |
200 | 202 |
201 case base::Value::TYPE_LIST: | 203 case base::Value::TYPE_LIST: |
202 return ToV8Array(isolate, static_cast<const base::ListValue*>(value)); | 204 return ToV8Array( |
205 isolate, creationContext, static_cast<const base::ListValue*>(value)); | |
203 | 206 |
204 case base::Value::TYPE_DICTIONARY: | 207 case base::Value::TYPE_DICTIONARY: |
205 return ToV8Object(isolate, | 208 return ToV8Object(isolate, |
209 creationContext, | |
206 static_cast<const base::DictionaryValue*>(value)); | 210 static_cast<const base::DictionaryValue*>(value)); |
207 | 211 |
208 case base::Value::TYPE_BINARY: | 212 case base::Value::TYPE_BINARY: |
209 return ToArrayBuffer(static_cast<const base::BinaryValue*>(value)); | 213 return ToArrayBuffer(isolate, |
214 creationContext, | |
215 static_cast<const base::BinaryValue*>(value)); | |
210 | 216 |
211 default: | 217 default: |
212 LOG(ERROR) << "Unexpected value type: " << value->GetType(); | 218 LOG(ERROR) << "Unexpected value type: " << value->GetType(); |
213 return v8::Null(isolate); | 219 return v8::Null(isolate); |
214 } | 220 } |
215 } | 221 } |
216 | 222 |
217 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array( | 223 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array( |
218 v8::Isolate* isolate, | 224 v8::Isolate* isolate, |
225 v8::Handle<v8::Object> creationContext, | |
219 const base::ListValue* val) const { | 226 const base::ListValue* val) const { |
220 v8::Handle<v8::Array> result(v8::Array::New(isolate, val->GetSize())); | 227 v8::Handle<v8::Array> result(v8::Array::New(isolate, val->GetSize())); |
221 | 228 |
222 for (size_t i = 0; i < val->GetSize(); ++i) { | 229 for (size_t i = 0; i < val->GetSize(); ++i) { |
223 const base::Value* child = NULL; | 230 const base::Value* child = NULL; |
224 CHECK(val->Get(i, &child)); | 231 CHECK(val->Get(i, &child)); |
225 | 232 |
226 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(isolate, child); | 233 v8::Handle<v8::Value> child_v8 = |
234 ToV8ValueImpl(isolate, creationContext, child); | |
227 CHECK(!child_v8.IsEmpty()); | 235 CHECK(!child_v8.IsEmpty()); |
228 | 236 |
229 v8::TryCatch try_catch; | 237 v8::TryCatch try_catch; |
230 result->Set(static_cast<uint32>(i), child_v8); | 238 result->Set(static_cast<uint32>(i), child_v8); |
231 if (try_catch.HasCaught()) | 239 if (try_catch.HasCaught()) |
232 LOG(ERROR) << "Setter for index " << i << " threw an exception."; | 240 LOG(ERROR) << "Setter for index " << i << " threw an exception."; |
233 } | 241 } |
234 | 242 |
235 return result; | 243 return result; |
236 } | 244 } |
237 | 245 |
238 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object( | 246 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object( |
239 v8::Isolate* isolate, | 247 v8::Isolate* isolate, |
248 v8::Handle<v8::Object> creationContext, | |
240 const base::DictionaryValue* val) const { | 249 const base::DictionaryValue* val) const { |
241 v8::Handle<v8::Object> result(v8::Object::New(isolate)); | 250 v8::Handle<v8::Object> result(v8::Object::New(isolate)); |
242 | 251 |
243 for (base::DictionaryValue::Iterator iter(*val); | 252 for (base::DictionaryValue::Iterator iter(*val); |
244 !iter.IsAtEnd(); iter.Advance()) { | 253 !iter.IsAtEnd(); iter.Advance()) { |
245 const std::string& key = iter.key(); | 254 const std::string& key = iter.key(); |
246 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value()); | 255 v8::Handle<v8::Value> child_v8 = |
256 ToV8ValueImpl(isolate, creationContext, &iter.value()); | |
247 CHECK(!child_v8.IsEmpty()); | 257 CHECK(!child_v8.IsEmpty()); |
248 | 258 |
249 v8::TryCatch try_catch; | 259 v8::TryCatch try_catch; |
250 result->Set( | 260 result->Set( |
251 v8::String::NewFromUtf8( | 261 v8::String::NewFromUtf8( |
252 isolate, key.c_str(), v8::String::kNormalString, key.length()), | 262 isolate, key.c_str(), v8::String::kNormalString, key.length()), |
253 child_v8); | 263 child_v8); |
254 if (try_catch.HasCaught()) { | 264 if (try_catch.HasCaught()) { |
255 LOG(ERROR) << "Setter for property " << key.c_str() << " threw an " | 265 LOG(ERROR) << "Setter for property " << key.c_str() << " threw an " |
256 << "exception."; | 266 << "exception."; |
257 } | 267 } |
258 } | 268 } |
259 | 269 |
260 return result; | 270 return result; |
261 } | 271 } |
262 | 272 |
263 v8::Handle<v8::Value> V8ValueConverterImpl::ToArrayBuffer( | 273 v8::Handle<v8::Value> V8ValueConverterImpl::ToArrayBuffer( |
274 v8::Isolate* isolate, | |
275 v8::Handle<v8::Object> creationContext, | |
264 const base::BinaryValue* value) const { | 276 const base::BinaryValue* value) const { |
265 blink::WebArrayBuffer buffer = | 277 blink::WebArrayBuffer buffer = |
266 blink::WebArrayBuffer::create(value->GetSize(), 1); | 278 blink::WebArrayBuffer::create(value->GetSize(), 1); |
267 memcpy(buffer.data(), value->GetBuffer(), value->GetSize()); | 279 memcpy(buffer.data(), value->GetBuffer(), value->GetSize()); |
268 return blink::WebArrayBufferConverter::toV8Value(&buffer); | 280 return blink::WebArrayBufferConverter::toV8Value( |
281 &buffer, creationContext, isolate); | |
269 } | 282 } |
270 | 283 |
271 base::Value* V8ValueConverterImpl::FromV8ValueImpl( | 284 base::Value* V8ValueConverterImpl::FromV8ValueImpl( |
272 FromV8ValueState* state, | 285 FromV8ValueState* state, |
273 v8::Handle<v8::Value> val, | 286 v8::Handle<v8::Value> val, |
274 v8::Isolate* isolate) const { | 287 v8::Isolate* isolate) const { |
275 CHECK(!val.IsEmpty()); | 288 CHECK(!val.IsEmpty()); |
276 | 289 |
277 FromV8ValueState::Level state_level(state); | 290 FromV8ValueState::Level state_level(state); |
278 if (state->HasReachedMaxRecursionDepth()) | 291 if (state->HasReachedMaxRecursionDepth()) |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 if (strategy_) { | 424 if (strategy_) { |
412 base::Value* out = NULL; | 425 base::Value* out = NULL; |
413 if (strategy_->FromV8ArrayBuffer(val, &out, isolate)) | 426 if (strategy_->FromV8ArrayBuffer(val, &out, isolate)) |
414 return out; | 427 return out; |
415 } | 428 } |
416 | 429 |
417 char* data = NULL; | 430 char* data = NULL; |
418 size_t length = 0; | 431 size_t length = 0; |
419 | 432 |
420 scoped_ptr<blink::WebArrayBuffer> array_buffer( | 433 scoped_ptr<blink::WebArrayBuffer> array_buffer( |
421 blink::WebArrayBufferConverter::createFromV8Value(val)); | 434 blink::WebArrayBufferConverter::createFromV8Value(val, isolate)); |
422 scoped_ptr<blink::WebArrayBufferView> view; | 435 scoped_ptr<blink::WebArrayBufferView> view; |
423 if (array_buffer) { | 436 if (array_buffer) { |
424 data = reinterpret_cast<char*>(array_buffer->data()); | 437 data = reinterpret_cast<char*>(array_buffer->data()); |
425 length = array_buffer->byteLength(); | 438 length = array_buffer->byteLength(); |
426 } else { | 439 } else { |
427 view.reset(blink::WebArrayBufferView::createFromV8Value(val)); | 440 view.reset(blink::WebArrayBufferView::createFromV8Value(val)); |
428 if (view) { | 441 if (view) { |
429 data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset(); | 442 data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset(); |
430 length = view->byteLength(); | 443 length = view->byteLength(); |
431 } | 444 } |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 continue; | 547 continue; |
535 | 548 |
536 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), | 549 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), |
537 child.release()); | 550 child.release()); |
538 } | 551 } |
539 | 552 |
540 return result.release(); | 553 return result.release(); |
541 } | 554 } |
542 | 555 |
543 } // namespace content | 556 } // namespace content |
OLD | NEW |