| 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> creation_context, |
| 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(isolate, |
| 205 creation_context, |
| 206 static_cast<const base::ListValue*>(value)); |
| 203 | 207 |
| 204 case base::Value::TYPE_DICTIONARY: | 208 case base::Value::TYPE_DICTIONARY: |
| 205 return ToV8Object(isolate, | 209 return ToV8Object(isolate, |
| 210 creation_context, |
| 206 static_cast<const base::DictionaryValue*>(value)); | 211 static_cast<const base::DictionaryValue*>(value)); |
| 207 | 212 |
| 208 case base::Value::TYPE_BINARY: | 213 case base::Value::TYPE_BINARY: |
| 209 return ToArrayBuffer(static_cast<const base::BinaryValue*>(value)); | 214 return ToArrayBuffer(isolate, |
| 215 creation_context, |
| 216 static_cast<const base::BinaryValue*>(value)); |
| 210 | 217 |
| 211 default: | 218 default: |
| 212 LOG(ERROR) << "Unexpected value type: " << value->GetType(); | 219 LOG(ERROR) << "Unexpected value type: " << value->GetType(); |
| 213 return v8::Null(isolate); | 220 return v8::Null(isolate); |
| 214 } | 221 } |
| 215 } | 222 } |
| 216 | 223 |
| 217 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array( | 224 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array( |
| 218 v8::Isolate* isolate, | 225 v8::Isolate* isolate, |
| 226 v8::Handle<v8::Object> creation_context, |
| 219 const base::ListValue* val) const { | 227 const base::ListValue* val) const { |
| 220 v8::Handle<v8::Array> result(v8::Array::New(isolate, val->GetSize())); | 228 v8::Handle<v8::Array> result(v8::Array::New(isolate, val->GetSize())); |
| 221 | 229 |
| 222 for (size_t i = 0; i < val->GetSize(); ++i) { | 230 for (size_t i = 0; i < val->GetSize(); ++i) { |
| 223 const base::Value* child = NULL; | 231 const base::Value* child = NULL; |
| 224 CHECK(val->Get(i, &child)); | 232 CHECK(val->Get(i, &child)); |
| 225 | 233 |
| 226 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(isolate, child); | 234 v8::Handle<v8::Value> child_v8 = |
| 235 ToV8ValueImpl(isolate, creation_context, child); |
| 227 CHECK(!child_v8.IsEmpty()); | 236 CHECK(!child_v8.IsEmpty()); |
| 228 | 237 |
| 229 v8::TryCatch try_catch; | 238 v8::TryCatch try_catch; |
| 230 result->Set(static_cast<uint32>(i), child_v8); | 239 result->Set(static_cast<uint32>(i), child_v8); |
| 231 if (try_catch.HasCaught()) | 240 if (try_catch.HasCaught()) |
| 232 LOG(ERROR) << "Setter for index " << i << " threw an exception."; | 241 LOG(ERROR) << "Setter for index " << i << " threw an exception."; |
| 233 } | 242 } |
| 234 | 243 |
| 235 return result; | 244 return result; |
| 236 } | 245 } |
| 237 | 246 |
| 238 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object( | 247 v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object( |
| 239 v8::Isolate* isolate, | 248 v8::Isolate* isolate, |
| 249 v8::Handle<v8::Object> creation_context, |
| 240 const base::DictionaryValue* val) const { | 250 const base::DictionaryValue* val) const { |
| 241 v8::Handle<v8::Object> result(v8::Object::New(isolate)); | 251 v8::Handle<v8::Object> result(v8::Object::New(isolate)); |
| 242 | 252 |
| 243 for (base::DictionaryValue::Iterator iter(*val); | 253 for (base::DictionaryValue::Iterator iter(*val); |
| 244 !iter.IsAtEnd(); iter.Advance()) { | 254 !iter.IsAtEnd(); iter.Advance()) { |
| 245 const std::string& key = iter.key(); | 255 const std::string& key = iter.key(); |
| 246 v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value()); | 256 v8::Handle<v8::Value> child_v8 = |
| 257 ToV8ValueImpl(isolate, creation_context, &iter.value()); |
| 247 CHECK(!child_v8.IsEmpty()); | 258 CHECK(!child_v8.IsEmpty()); |
| 248 | 259 |
| 249 v8::TryCatch try_catch; | 260 v8::TryCatch try_catch; |
| 250 result->Set( | 261 result->Set( |
| 251 v8::String::NewFromUtf8( | 262 v8::String::NewFromUtf8( |
| 252 isolate, key.c_str(), v8::String::kNormalString, key.length()), | 263 isolate, key.c_str(), v8::String::kNormalString, key.length()), |
| 253 child_v8); | 264 child_v8); |
| 254 if (try_catch.HasCaught()) { | 265 if (try_catch.HasCaught()) { |
| 255 LOG(ERROR) << "Setter for property " << key.c_str() << " threw an " | 266 LOG(ERROR) << "Setter for property " << key.c_str() << " threw an " |
| 256 << "exception."; | 267 << "exception."; |
| 257 } | 268 } |
| 258 } | 269 } |
| 259 | 270 |
| 260 return result; | 271 return result; |
| 261 } | 272 } |
| 262 | 273 |
| 263 v8::Handle<v8::Value> V8ValueConverterImpl::ToArrayBuffer( | 274 v8::Handle<v8::Value> V8ValueConverterImpl::ToArrayBuffer( |
| 275 v8::Isolate* isolate, |
| 276 v8::Handle<v8::Object> creation_context, |
| 264 const base::BinaryValue* value) const { | 277 const base::BinaryValue* value) const { |
| 265 blink::WebArrayBuffer buffer = | 278 blink::WebArrayBuffer buffer = |
| 266 blink::WebArrayBuffer::create(value->GetSize(), 1); | 279 blink::WebArrayBuffer::create(value->GetSize(), 1); |
| 267 memcpy(buffer.data(), value->GetBuffer(), value->GetSize()); | 280 memcpy(buffer.data(), value->GetBuffer(), value->GetSize()); |
| 268 return blink::WebArrayBufferConverter::toV8Value(&buffer); | 281 return blink::WebArrayBufferConverter::toV8Value( |
| 282 &buffer, creation_context, isolate); |
| 269 } | 283 } |
| 270 | 284 |
| 271 base::Value* V8ValueConverterImpl::FromV8ValueImpl( | 285 base::Value* V8ValueConverterImpl::FromV8ValueImpl( |
| 272 FromV8ValueState* state, | 286 FromV8ValueState* state, |
| 273 v8::Handle<v8::Value> val, | 287 v8::Handle<v8::Value> val, |
| 274 v8::Isolate* isolate) const { | 288 v8::Isolate* isolate) const { |
| 275 CHECK(!val.IsEmpty()); | 289 CHECK(!val.IsEmpty()); |
| 276 | 290 |
| 277 FromV8ValueState::Level state_level(state); | 291 FromV8ValueState::Level state_level(state); |
| 278 if (state->HasReachedMaxRecursionDepth()) | 292 if (state->HasReachedMaxRecursionDepth()) |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 if (strategy_) { | 425 if (strategy_) { |
| 412 base::Value* out = NULL; | 426 base::Value* out = NULL; |
| 413 if (strategy_->FromV8ArrayBuffer(val, &out, isolate)) | 427 if (strategy_->FromV8ArrayBuffer(val, &out, isolate)) |
| 414 return out; | 428 return out; |
| 415 } | 429 } |
| 416 | 430 |
| 417 char* data = NULL; | 431 char* data = NULL; |
| 418 size_t length = 0; | 432 size_t length = 0; |
| 419 | 433 |
| 420 scoped_ptr<blink::WebArrayBuffer> array_buffer( | 434 scoped_ptr<blink::WebArrayBuffer> array_buffer( |
| 421 blink::WebArrayBufferConverter::createFromV8Value(val)); | 435 blink::WebArrayBufferConverter::createFromV8Value(val, isolate)); |
| 422 scoped_ptr<blink::WebArrayBufferView> view; | 436 scoped_ptr<blink::WebArrayBufferView> view; |
| 423 if (array_buffer) { | 437 if (array_buffer) { |
| 424 data = reinterpret_cast<char*>(array_buffer->data()); | 438 data = reinterpret_cast<char*>(array_buffer->data()); |
| 425 length = array_buffer->byteLength(); | 439 length = array_buffer->byteLength(); |
| 426 } else { | 440 } else { |
| 427 view.reset(blink::WebArrayBufferView::createFromV8Value(val)); | 441 view.reset(blink::WebArrayBufferView::createFromV8Value(val)); |
| 428 if (view) { | 442 if (view) { |
| 429 data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset(); | 443 data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset(); |
| 430 length = view->byteLength(); | 444 length = view->byteLength(); |
| 431 } | 445 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 continue; | 548 continue; |
| 535 | 549 |
| 536 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), | 550 result->SetWithoutPathExpansion(std::string(*name_utf8, name_utf8.length()), |
| 537 child.release()); | 551 child.release()); |
| 538 } | 552 } |
| 539 | 553 |
| 540 return result.release(); | 554 return result.release(); |
| 541 } | 555 } |
| 542 | 556 |
| 543 } // namespace content | 557 } // namespace content |
| OLD | NEW |