Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / renderer / java / gin_java_bridge_value_converter.cc
blob2fdf658bcfc7f9f112f1248d4e2101c4c0aaf5d2
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/java/gin_java_bridge_value_converter.h"
7 #include <cmath>
9 #include "base/values.h"
10 #include "content/common/android/gin_java_bridge_value.h"
11 #include "content/renderer/java/gin_java_bridge_object.h"
12 #include "gin/array_buffer.h"
14 namespace content {
16 GinJavaBridgeValueConverter::GinJavaBridgeValueConverter()
17 : converter_(V8ValueConverter::create()) {
18 converter_->SetDateAllowed(false);
19 converter_->SetRegExpAllowed(false);
20 converter_->SetFunctionAllowed(true);
21 converter_->SetStrategy(this);
24 GinJavaBridgeValueConverter::~GinJavaBridgeValueConverter() {
27 v8::Local<v8::Value> GinJavaBridgeValueConverter::ToV8Value(
28 const base::Value* value,
29 v8::Local<v8::Context> context) const {
30 return converter_->ToV8Value(value, context);
33 scoped_ptr<base::Value> GinJavaBridgeValueConverter::FromV8Value(
34 v8::Local<v8::Value> value,
35 v8::Local<v8::Context> context) const {
36 return make_scoped_ptr(converter_->FromV8Value(value, context));
39 bool GinJavaBridgeValueConverter::FromV8Object(
40 v8::Local<v8::Object> value,
41 base::Value** out,
42 v8::Isolate* isolate,
43 const FromV8ValueCallback& callback) const {
44 GinJavaBridgeObject* unwrapped;
45 if (!gin::ConvertFromV8(isolate, value, &unwrapped)) {
46 return false;
48 *out =
49 GinJavaBridgeValue::CreateObjectIDValue(unwrapped->object_id()).release();
50 return true;
53 namespace {
55 class TypedArraySerializer {
56 public:
57 virtual ~TypedArraySerializer() {}
58 static scoped_ptr<TypedArraySerializer> Create(
59 v8::Local<v8::TypedArray> typed_array);
60 virtual void serializeTo(char* data,
61 size_t data_length,
62 base::ListValue* out) = 0;
63 protected:
64 TypedArraySerializer() {}
67 template <typename ElementType, typename ListType>
68 class TypedArraySerializerImpl : public TypedArraySerializer {
69 public:
70 static scoped_ptr<TypedArraySerializer> Create(
71 v8::Local<v8::TypedArray> typed_array) {
72 return make_scoped_ptr(
73 new TypedArraySerializerImpl<ElementType, ListType>(typed_array));
76 void serializeTo(char* data,
77 size_t data_length,
78 base::ListValue* out) override {
79 DCHECK_EQ(data_length, typed_array_->Length() * sizeof(ElementType));
80 for (ElementType *element = reinterpret_cast<ElementType*>(data),
81 *end = element + typed_array_->Length();
82 element != end;
83 ++element) {
84 const ListType list_value = *element;
85 out->Append(new base::FundamentalValue(list_value));
89 private:
90 explicit TypedArraySerializerImpl(v8::Local<v8::TypedArray> typed_array)
91 : typed_array_(typed_array) {}
93 v8::Local<v8::TypedArray> typed_array_;
95 DISALLOW_COPY_AND_ASSIGN(TypedArraySerializerImpl);
98 // static
99 scoped_ptr<TypedArraySerializer> TypedArraySerializer::Create(
100 v8::Local<v8::TypedArray> typed_array) {
101 if (typed_array->IsInt8Array() ||
102 typed_array->IsUint8Array() ||
103 typed_array->IsUint8ClampedArray()) {
104 return TypedArraySerializerImpl<char, int>::Create(typed_array).Pass();
105 } else if (typed_array->IsInt16Array() || typed_array->IsUint16Array()) {
106 return TypedArraySerializerImpl<int16_t, int>::Create(typed_array).Pass();
107 } else if (typed_array->IsInt32Array() || typed_array->IsUint32Array()) {
108 return TypedArraySerializerImpl<int32_t, int>::Create(typed_array).Pass();
109 } else if (typed_array->IsFloat32Array()) {
110 return TypedArraySerializerImpl<float, double>::Create(typed_array).Pass();
111 } else if (typed_array->IsFloat64Array()) {
112 return TypedArraySerializerImpl<double, double>::Create(typed_array).Pass();
114 NOTREACHED();
115 return scoped_ptr<TypedArraySerializer>();
118 } // namespace
120 bool GinJavaBridgeValueConverter::FromV8ArrayBuffer(
121 v8::Local<v8::Object> value,
122 base::Value** out,
123 v8::Isolate* isolate) const {
124 if (!value->IsTypedArray()) {
125 *out = GinJavaBridgeValue::CreateUndefinedValue().release();
126 return true;
129 char* data = NULL;
130 size_t data_length = 0;
131 gin::ArrayBufferView view;
132 if (ConvertFromV8(isolate, value.As<v8::ArrayBufferView>(), &view)) {
133 data = reinterpret_cast<char*>(view.bytes());
134 data_length = view.num_bytes();
136 if (!data) {
137 *out = GinJavaBridgeValue::CreateUndefinedValue().release();
138 return true;
141 base::ListValue* result = new base::ListValue();
142 *out = result;
143 scoped_ptr<TypedArraySerializer> serializer(
144 TypedArraySerializer::Create(value.As<v8::TypedArray>()));
145 serializer->serializeTo(data, data_length, result);
146 return true;
149 bool GinJavaBridgeValueConverter::FromV8Number(v8::Local<v8::Number> value,
150 base::Value** out) const {
151 double double_value = value->Value();
152 if (std::isfinite(double_value))
153 return false;
154 *out = GinJavaBridgeValue::CreateNonFiniteValue(double_value).release();
155 return true;
158 bool GinJavaBridgeValueConverter::FromV8Undefined(base::Value** out) const {
159 *out = GinJavaBridgeValue::CreateUndefinedValue().release();
160 return true;
163 } // namespace content