#include <string>
+#include "base/bind.h"
+#include "base/bind_helpers.h"
#include "base/float_util.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "third_party/WebKit/public/platform/WebArrayBuffer.h"
+#include "third_party/WebKit/public/web/WebArrayBufferConverter.h"
#include "third_party/WebKit/public/web/WebArrayBufferView.h"
#include "v8/include/v8.h"
namespace content {
+// Default implementation of V8ValueConverter::Strategy
+
+bool V8ValueConverter::Strategy::FromV8Object(
+ v8::Handle<v8::Object> value,
+ base::Value** out,
+ v8::Isolate* isolate,
+ const FromV8ValueCallback& callback) const {
+ return false;
+}
+
+bool V8ValueConverter::Strategy::FromV8Array(
+ v8::Handle<v8::Array> value,
+ base::Value** out,
+ v8::Isolate* isolate,
+ const FromV8ValueCallback& callback) const {
+ return false;
+}
+
+bool V8ValueConverter::Strategy::FromV8ArrayBuffer(v8::Handle<v8::Object> value,
+ base::Value** out) const {
+ return false;
+}
+
+bool V8ValueConverter::Strategy::FromV8Number(v8::Handle<v8::Number> value,
+ base::Value** out) const {
+ return false;
+}
+
+bool V8ValueConverter::Strategy::FromV8Undefined(base::Value** out) const {
+ return false;
+}
+
+
namespace {
// For the sake of the storage API, make this quite large.
v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Value(
const base::Value* value, v8::Handle<v8::Context> context) const {
v8::Context::Scope context_scope(context);
- v8::HandleScope handle_scope(context->GetIsolate());
- return handle_scope.Close(ToV8ValueImpl(value));
+ v8::EscapableHandleScope handle_scope(context->GetIsolate());
+ return handle_scope.Escape(ToV8ValueImpl(context->GetIsolate(), value));
}
-Value* V8ValueConverterImpl::FromV8Value(
+base::Value* V8ValueConverterImpl::FromV8Value(
v8::Handle<v8::Value> val,
v8::Handle<v8::Context> context) const {
v8::Context::Scope context_scope(context);
v8::HandleScope handle_scope(context->GetIsolate());
FromV8ValueState state(avoid_identity_hash_for_testing_);
- return FromV8ValueImpl(val, &state);
+ return FromV8ValueImpl(&state, val, context->GetIsolate());
}
-v8::Handle<v8::Value> V8ValueConverterImpl::ToV8ValueImpl(
- const base::Value* value) const {
+v8::Local<v8::Value> V8ValueConverterImpl::ToV8ValueImpl(
+ v8::Isolate* isolate,
+ const base::Value* value) const {
CHECK(value);
switch (value->GetType()) {
case base::Value::TYPE_NULL:
- return v8::Null();
+ return v8::Null(isolate);
case base::Value::TYPE_BOOLEAN: {
bool val = false;
CHECK(value->GetAsBoolean(&val));
- return v8::Boolean::New(val);
+ return v8::Boolean::New(isolate, val);
}
case base::Value::TYPE_INTEGER: {
int val = 0;
CHECK(value->GetAsInteger(&val));
- return v8::Integer::New(val);
+ return v8::Integer::New(isolate, val);
}
case base::Value::TYPE_DOUBLE: {
double val = 0.0;
CHECK(value->GetAsDouble(&val));
- return v8::Number::New(val);
+ return v8::Number::New(isolate, val);
}
case base::Value::TYPE_STRING: {
std::string val;
CHECK(value->GetAsString(&val));
- return v8::String::New(val.c_str(), val.length());
+ return v8::String::NewFromUtf8(
+ isolate, val.c_str(), v8::String::kNormalString, val.length());
}
case base::Value::TYPE_LIST:
- return ToV8Array(static_cast<const base::ListValue*>(value));
+ return ToV8Array(isolate, static_cast<const base::ListValue*>(value));
case base::Value::TYPE_DICTIONARY:
- return ToV8Object(static_cast<const base::DictionaryValue*>(value));
+ return ToV8Object(isolate,
+ static_cast<const base::DictionaryValue*>(value));
case base::Value::TYPE_BINARY:
return ToArrayBuffer(static_cast<const base::BinaryValue*>(value));
default:
LOG(ERROR) << "Unexpected value type: " << value->GetType();
- return v8::Null();
+ return v8::Null(isolate);
}
}
v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Array(
+ v8::Isolate* isolate,
const base::ListValue* val) const {
- v8::Handle<v8::Array> result(v8::Array::New(val->GetSize()));
+ v8::Handle<v8::Array> result(v8::Array::New(isolate, val->GetSize()));
for (size_t i = 0; i < val->GetSize(); ++i) {
const base::Value* child = NULL;
CHECK(val->Get(i, &child));
- v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(child);
+ v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(isolate, child);
CHECK(!child_v8.IsEmpty());
v8::TryCatch try_catch;
}
v8::Handle<v8::Value> V8ValueConverterImpl::ToV8Object(
+ v8::Isolate* isolate,
const base::DictionaryValue* val) const {
- v8::Handle<v8::Object> result(v8::Object::New());
+ v8::Handle<v8::Object> result(v8::Object::New(isolate));
for (base::DictionaryValue::Iterator iter(*val);
!iter.IsAtEnd(); iter.Advance()) {
const std::string& key = iter.key();
- v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(&iter.value());
+ v8::Handle<v8::Value> child_v8 = ToV8ValueImpl(isolate, &iter.value());
CHECK(!child_v8.IsEmpty());
v8::TryCatch try_catch;
- result->Set(v8::String::New(key.c_str(), key.length()), child_v8);
+ result->Set(
+ v8::String::NewFromUtf8(
+ isolate, key.c_str(), v8::String::kNormalString, key.length()),
+ child_v8);
if (try_catch.HasCaught()) {
LOG(ERROR) << "Setter for property " << key.c_str() << " threw an "
<< "exception.";
v8::Handle<v8::Value> V8ValueConverterImpl::ToArrayBuffer(
const base::BinaryValue* value) const {
- WebKit::WebArrayBuffer buffer =
- WebKit::WebArrayBuffer::create(value->GetSize(), 1);
+ blink::WebArrayBuffer buffer =
+ blink::WebArrayBuffer::create(value->GetSize(), 1);
memcpy(buffer.data(), value->GetBuffer(), value->GetSize());
- return buffer.toV8Value();
+ return blink::WebArrayBufferConverter::toV8Value(&buffer);
}
-Value* V8ValueConverterImpl::FromV8ValueImpl(
+base::Value* V8ValueConverterImpl::FromV8ValueImpl(
+ FromV8ValueState* state,
v8::Handle<v8::Value> val,
- FromV8ValueState* state) const {
+ v8::Isolate* isolate) const {
CHECK(!val.IsEmpty());
FromV8ValueState::Level state_level(state);
if (val->IsBoolean())
return new base::FundamentalValue(val->ToBoolean()->Value());
+ if (val->IsNumber() && strategy_) {
+ base::Value* out = NULL;
+ if (strategy_->FromV8Number(val->ToNumber(), &out))
+ return out;
+ }
+
if (val->IsInt32())
return new base::FundamentalValue(val->ToInt32()->Value());
return new base::StringValue(std::string(*utf8, utf8.length()));
}
- if (val->IsUndefined())
+ if (val->IsUndefined()) {
+ if (strategy_) {
+ base::Value* out = NULL;
+ if (strategy_->FromV8Undefined(&out))
+ return out;
+ }
// JSON.stringify ignores undefined.
return NULL;
+ }
if (val->IsDate()) {
if (!date_allowed_)
// JSON.stringify would convert this to a string, but an object is more
// consistent within this class.
- return FromV8Object(val->ToObject(), state);
+ return FromV8Object(val->ToObject(), state, isolate);
v8::Date* date = v8::Date::Cast(*val);
- return new base::FundamentalValue(date->NumberValue() / 1000.0);
+ return new base::FundamentalValue(date->ValueOf() / 1000.0);
}
if (val->IsRegExp()) {
if (!reg_exp_allowed_)
// JSON.stringify converts to an object.
- return FromV8Object(val->ToObject(), state);
+ return FromV8Object(val->ToObject(), state, isolate);
return new base::StringValue(*v8::String::Utf8Value(val->ToString()));
}
// v8::Value doesn't have a ToArray() method for some reason.
if (val->IsArray())
- return FromV8Array(val.As<v8::Array>(), state);
+ return FromV8Array(val.As<v8::Array>(), state, isolate);
if (val->IsFunction()) {
if (!function_allowed_)
// JSON.stringify refuses to convert function(){}.
return NULL;
- return FromV8Object(val->ToObject(), state);
+ return FromV8Object(val->ToObject(), state, isolate);
}
- if (val->IsObject()) {
- base::BinaryValue* binary_value = FromV8Buffer(val);
- if (binary_value) {
- return binary_value;
- } else {
- return FromV8Object(val->ToObject(), state);
- }
- }
+ if (val->IsArrayBuffer() || val->IsArrayBufferView())
+ return FromV8ArrayBuffer(val->ToObject());
+
+ if (val->IsObject())
+ return FromV8Object(val->ToObject(), state, isolate);
LOG(ERROR) << "Unexpected v8 value type encountered.";
return NULL;
base::Value* V8ValueConverterImpl::FromV8Array(
v8::Handle<v8::Array> val,
- FromV8ValueState* state) const {
+ FromV8ValueState* state,
+ v8::Isolate* isolate) const {
if (!state->UpdateAndCheckUniqueness(val))
return base::Value::CreateNullValue();
// If val was created in a different context than our current one, change to
// that context, but change back after val is converted.
if (!val->CreationContext().IsEmpty() &&
- val->CreationContext() != v8::Context::GetCurrent())
+ val->CreationContext() != isolate->GetCurrentContext())
scope.reset(new v8::Context::Scope(val->CreationContext()));
if (strategy_) {
- Value* out = NULL;
- if (strategy_->FromV8Array(val, &out))
+ // These base::Unretained's are safe, because Strategy::FromV8Value should
+ // be synchronous, so this object can't be out of scope.
+ V8ValueConverter::Strategy::FromV8ValueCallback callback =
+ base::Bind(&V8ValueConverterImpl::FromV8ValueImpl,
+ base::Unretained(this),
+ base::Unretained(state));
+ base::Value* out = NULL;
+ if (strategy_->FromV8Array(val, &out, isolate, callback))
return out;
}
v8::Handle<v8::Value> child_v8 = val->Get(i);
if (try_catch.HasCaught()) {
LOG(ERROR) << "Getter for index " << i << " threw an exception.";
- child_v8 = v8::Null();
+ child_v8 = v8::Null(isolate);
}
- if (!val->HasRealIndexedProperty(i))
+ if (!val->HasRealIndexedProperty(i)) {
+ result->Append(base::Value::CreateNullValue());
continue;
+ }
- base::Value* child = FromV8ValueImpl(child_v8, state);
+ base::Value* child = FromV8ValueImpl(state, child_v8, isolate);
if (child)
result->Append(child);
else
return result;
}
-base::BinaryValue* V8ValueConverterImpl::FromV8Buffer(
- v8::Handle<v8::Value> val) const {
+base::Value* V8ValueConverterImpl::FromV8ArrayBuffer(
+ v8::Handle<v8::Object> val) const {
+ if (strategy_) {
+ base::Value* out = NULL;
+ if (strategy_->FromV8ArrayBuffer(val, &out))
+ return out;
+ }
+
char* data = NULL;
size_t length = 0;
- scoped_ptr<WebKit::WebArrayBuffer> array_buffer(
- WebKit::WebArrayBuffer::createFromV8Value(val));
- scoped_ptr<WebKit::WebArrayBufferView> view;
+ scoped_ptr<blink::WebArrayBuffer> array_buffer(
+ blink::WebArrayBufferConverter::createFromV8Value(val));
+ scoped_ptr<blink::WebArrayBufferView> view;
if (array_buffer) {
data = reinterpret_cast<char*>(array_buffer->data());
length = array_buffer->byteLength();
} else {
- view.reset(WebKit::WebArrayBufferView::createFromV8Value(val));
+ view.reset(blink::WebArrayBufferView::createFromV8Value(val));
if (view) {
data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset();
length = view->byteLength();
base::Value* V8ValueConverterImpl::FromV8Object(
v8::Handle<v8::Object> val,
- FromV8ValueState* state) const {
+ FromV8ValueState* state,
+ v8::Isolate* isolate) const {
if (!state->UpdateAndCheckUniqueness(val))
return base::Value::CreateNullValue();
// If val was created in a different context than our current one, change to
// that context, but change back after val is converted.
if (!val->CreationContext().IsEmpty() &&
- val->CreationContext() != v8::Context::GetCurrent())
+ val->CreationContext() != isolate->GetCurrentContext())
scope.reset(new v8::Context::Scope(val->CreationContext()));
if (strategy_) {
- Value* out = NULL;
- if (strategy_->FromV8Object(val, &out))
+ // These base::Unretained's are safe, because Strategy::FromV8Value should
+ // be synchronous, so this object can't be out of scope.
+ V8ValueConverter::Strategy::FromV8ValueCallback callback =
+ base::Bind(&V8ValueConverterImpl::FromV8ValueImpl,
+ base::Unretained(this),
+ base::Unretained(state));
+ base::Value* out = NULL;
+ if (strategy_->FromV8Object(val, &out, isolate, callback))
return out;
}
//
// NOTE: check this after |strategy_| so that callers have a chance to
// do something else, such as convert to the node's name rather than NULL.
+ //
+ // ANOTHER NOTE: returning an empty dictionary here to minimise surprise.
+ // See also http://crbug.com/330559.
if (val->InternalFieldCount())
- return NULL;
+ return new base::DictionaryValue();
scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
v8::Handle<v8::Array> property_names(val->GetOwnPropertyNames());
// Extend this test to cover more types as necessary and if sensible.
if (!key->IsString() &&
!key->IsNumber()) {
- NOTREACHED() << "Key \"" << *v8::String::AsciiValue(key) << "\" "
+ NOTREACHED() << "Key \"" << *v8::String::Utf8Value(key) << "\" "
"is neither a string nor a number";
continue;
}
if (try_catch.HasCaught()) {
LOG(WARNING) << "Getter for property " << *name_utf8
<< " threw an exception.";
- child_v8 = v8::Null();
+ child_v8 = v8::Null(isolate);
}
- scoped_ptr<base::Value> child(FromV8ValueImpl(child_v8, state));
+ scoped_ptr<base::Value> child(FromV8ValueImpl(state, child_v8, isolate));
if (!child)
// JSON.stringify skips properties whose values don't serialize, for
// example undefined and functions. Emulate that behavior.