OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/java/gin_java_bridge_value_converter.h" | 5 #include "content/renderer/java/gin_java_bridge_value_converter.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "content/common/android/gin_java_bridge_value.h" | 10 #include "content/common/android/gin_java_bridge_value.h" |
11 #include "content/renderer/java/gin_java_bridge_object.h" | 11 #include "content/renderer/java/gin_java_bridge_object.h" |
12 #include "gin/array_buffer.h" | 12 #include "gin/array_buffer.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 GinJavaBridgeValueConverter::GinJavaBridgeValueConverter() | 16 GinJavaBridgeValueConverter::GinJavaBridgeValueConverter() |
17 : converter_(V8ValueConverter::create()) { | 17 : converter_(V8ValueConverter::create()) { |
18 converter_->SetDateAllowed(false); | 18 converter_->SetDateAllowed(false); |
19 converter_->SetRegExpAllowed(false); | 19 converter_->SetRegExpAllowed(false); |
20 converter_->SetFunctionAllowed(true); | 20 converter_->SetFunctionAllowed(true); |
21 converter_->SetStrategy(this); | 21 converter_->SetStrategy(this); |
22 } | 22 } |
23 | 23 |
24 GinJavaBridgeValueConverter::~GinJavaBridgeValueConverter() { | 24 GinJavaBridgeValueConverter::~GinJavaBridgeValueConverter() { |
25 } | 25 } |
26 | 26 |
27 v8::Handle<v8::Value> GinJavaBridgeValueConverter::ToV8Value( | 27 v8::Local<v8::Value> GinJavaBridgeValueConverter::ToV8Value( |
28 const base::Value* value, | 28 const base::Value* value, |
29 v8::Handle<v8::Context> context) const { | 29 v8::Local<v8::Context> context) const { |
30 return converter_->ToV8Value(value, context); | 30 return converter_->ToV8Value(value, context); |
31 } | 31 } |
32 | 32 |
33 scoped_ptr<base::Value> GinJavaBridgeValueConverter::FromV8Value( | 33 scoped_ptr<base::Value> GinJavaBridgeValueConverter::FromV8Value( |
34 v8::Handle<v8::Value> value, | 34 v8::Local<v8::Value> value, |
35 v8::Handle<v8::Context> context) const { | 35 v8::Local<v8::Context> context) const { |
36 return make_scoped_ptr(converter_->FromV8Value(value, context)); | 36 return make_scoped_ptr(converter_->FromV8Value(value, context)); |
37 } | 37 } |
38 | 38 |
39 bool GinJavaBridgeValueConverter::FromV8Object( | 39 bool GinJavaBridgeValueConverter::FromV8Object( |
40 v8::Handle<v8::Object> value, | 40 v8::Local<v8::Object> value, |
41 base::Value** out, | 41 base::Value** out, |
42 v8::Isolate* isolate, | 42 v8::Isolate* isolate, |
43 const FromV8ValueCallback& callback) const { | 43 const FromV8ValueCallback& callback) const { |
44 GinJavaBridgeObject* unwrapped; | 44 GinJavaBridgeObject* unwrapped; |
45 if (!gin::ConvertFromV8(isolate, value, &unwrapped)) { | 45 if (!gin::ConvertFromV8(isolate, value, &unwrapped)) { |
46 return false; | 46 return false; |
47 } | 47 } |
48 *out = | 48 *out = |
49 GinJavaBridgeValue::CreateObjectIDValue(unwrapped->object_id()).release(); | 49 GinJavaBridgeValue::CreateObjectIDValue(unwrapped->object_id()).release(); |
50 return true; | 50 return true; |
51 } | 51 } |
52 | 52 |
53 namespace { | 53 namespace { |
54 | 54 |
55 class TypedArraySerializer { | 55 class TypedArraySerializer { |
56 public: | 56 public: |
57 virtual ~TypedArraySerializer() {} | 57 virtual ~TypedArraySerializer() {} |
58 static scoped_ptr<TypedArraySerializer> Create( | 58 static scoped_ptr<TypedArraySerializer> Create( |
59 v8::Handle<v8::TypedArray> typed_array); | 59 v8::Local<v8::TypedArray> typed_array); |
60 virtual void serializeTo(char* data, | 60 virtual void serializeTo(char* data, |
61 size_t data_length, | 61 size_t data_length, |
62 base::ListValue* out) = 0; | 62 base::ListValue* out) = 0; |
63 protected: | 63 protected: |
64 TypedArraySerializer() {} | 64 TypedArraySerializer() {} |
65 }; | 65 }; |
66 | 66 |
67 template <typename ElementType, typename ListType> | 67 template <typename ElementType, typename ListType> |
68 class TypedArraySerializerImpl : public TypedArraySerializer { | 68 class TypedArraySerializerImpl : public TypedArraySerializer { |
69 public: | 69 public: |
70 static scoped_ptr<TypedArraySerializer> Create( | 70 static scoped_ptr<TypedArraySerializer> Create( |
71 v8::Handle<v8::TypedArray> typed_array) { | 71 v8::Local<v8::TypedArray> typed_array) { |
72 return make_scoped_ptr( | 72 return make_scoped_ptr( |
73 new TypedArraySerializerImpl<ElementType, ListType>(typed_array)); | 73 new TypedArraySerializerImpl<ElementType, ListType>(typed_array)); |
74 } | 74 } |
75 | 75 |
76 void serializeTo(char* data, | 76 void serializeTo(char* data, |
77 size_t data_length, | 77 size_t data_length, |
78 base::ListValue* out) override { | 78 base::ListValue* out) override { |
79 DCHECK_EQ(data_length, typed_array_->Length() * sizeof(ElementType)); | 79 DCHECK_EQ(data_length, typed_array_->Length() * sizeof(ElementType)); |
80 for (ElementType *element = reinterpret_cast<ElementType*>(data), | 80 for (ElementType *element = reinterpret_cast<ElementType*>(data), |
81 *end = element + typed_array_->Length(); | 81 *end = element + typed_array_->Length(); |
82 element != end; | 82 element != end; |
83 ++element) { | 83 ++element) { |
84 const ListType list_value = *element; | 84 const ListType list_value = *element; |
85 out->Append(new base::FundamentalValue(list_value)); | 85 out->Append(new base::FundamentalValue(list_value)); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 private: | 89 private: |
90 explicit TypedArraySerializerImpl(v8::Handle<v8::TypedArray> typed_array) | 90 explicit TypedArraySerializerImpl(v8::Local<v8::TypedArray> typed_array) |
91 : typed_array_(typed_array) {} | 91 : typed_array_(typed_array) {} |
92 | 92 |
93 v8::Handle<v8::TypedArray> typed_array_; | 93 v8::Local<v8::TypedArray> typed_array_; |
94 | 94 |
95 DISALLOW_COPY_AND_ASSIGN(TypedArraySerializerImpl); | 95 DISALLOW_COPY_AND_ASSIGN(TypedArraySerializerImpl); |
96 }; | 96 }; |
97 | 97 |
98 // static | 98 // static |
99 scoped_ptr<TypedArraySerializer> TypedArraySerializer::Create( | 99 scoped_ptr<TypedArraySerializer> TypedArraySerializer::Create( |
100 v8::Handle<v8::TypedArray> typed_array) { | 100 v8::Local<v8::TypedArray> typed_array) { |
101 if (typed_array->IsInt8Array() || | 101 if (typed_array->IsInt8Array() || |
102 typed_array->IsUint8Array() || | 102 typed_array->IsUint8Array() || |
103 typed_array->IsUint8ClampedArray()) { | 103 typed_array->IsUint8ClampedArray()) { |
104 return TypedArraySerializerImpl<char, int>::Create(typed_array).Pass(); | 104 return TypedArraySerializerImpl<char, int>::Create(typed_array).Pass(); |
105 } else if (typed_array->IsInt16Array() || typed_array->IsUint16Array()) { | 105 } else if (typed_array->IsInt16Array() || typed_array->IsUint16Array()) { |
106 return TypedArraySerializerImpl<int16_t, int>::Create(typed_array).Pass(); | 106 return TypedArraySerializerImpl<int16_t, int>::Create(typed_array).Pass(); |
107 } else if (typed_array->IsInt32Array() || typed_array->IsUint32Array()) { | 107 } else if (typed_array->IsInt32Array() || typed_array->IsUint32Array()) { |
108 return TypedArraySerializerImpl<int32_t, int>::Create(typed_array).Pass(); | 108 return TypedArraySerializerImpl<int32_t, int>::Create(typed_array).Pass(); |
109 } else if (typed_array->IsFloat32Array()) { | 109 } else if (typed_array->IsFloat32Array()) { |
110 return TypedArraySerializerImpl<float, double>::Create(typed_array).Pass(); | 110 return TypedArraySerializerImpl<float, double>::Create(typed_array).Pass(); |
111 } else if (typed_array->IsFloat64Array()) { | 111 } else if (typed_array->IsFloat64Array()) { |
112 return TypedArraySerializerImpl<double, double>::Create(typed_array).Pass(); | 112 return TypedArraySerializerImpl<double, double>::Create(typed_array).Pass(); |
113 } | 113 } |
114 NOTREACHED(); | 114 NOTREACHED(); |
115 return scoped_ptr<TypedArraySerializer>(); | 115 return scoped_ptr<TypedArraySerializer>(); |
116 } | 116 } |
117 | 117 |
118 } // namespace | 118 } // namespace |
119 | 119 |
120 bool GinJavaBridgeValueConverter::FromV8ArrayBuffer( | 120 bool GinJavaBridgeValueConverter::FromV8ArrayBuffer( |
121 v8::Handle<v8::Object> value, | 121 v8::Local<v8::Object> value, |
122 base::Value** out, | 122 base::Value** out, |
123 v8::Isolate* isolate) const { | 123 v8::Isolate* isolate) const { |
124 if (!value->IsTypedArray()) { | 124 if (!value->IsTypedArray()) { |
125 *out = GinJavaBridgeValue::CreateUndefinedValue().release(); | 125 *out = GinJavaBridgeValue::CreateUndefinedValue().release(); |
126 return true; | 126 return true; |
127 } | 127 } |
128 | 128 |
129 char* data = NULL; | 129 char* data = NULL; |
130 size_t data_length = 0; | 130 size_t data_length = 0; |
131 gin::ArrayBufferView view; | 131 gin::ArrayBufferView view; |
132 if (ConvertFromV8(isolate, value.As<v8::ArrayBufferView>(), &view)) { | 132 if (ConvertFromV8(isolate, value.As<v8::ArrayBufferView>(), &view)) { |
133 data = reinterpret_cast<char*>(view.bytes()); | 133 data = reinterpret_cast<char*>(view.bytes()); |
134 data_length = view.num_bytes(); | 134 data_length = view.num_bytes(); |
135 } | 135 } |
136 if (!data) { | 136 if (!data) { |
137 *out = GinJavaBridgeValue::CreateUndefinedValue().release(); | 137 *out = GinJavaBridgeValue::CreateUndefinedValue().release(); |
138 return true; | 138 return true; |
139 } | 139 } |
140 | 140 |
141 base::ListValue* result = new base::ListValue(); | 141 base::ListValue* result = new base::ListValue(); |
142 *out = result; | 142 *out = result; |
143 scoped_ptr<TypedArraySerializer> serializer( | 143 scoped_ptr<TypedArraySerializer> serializer( |
144 TypedArraySerializer::Create(value.As<v8::TypedArray>())); | 144 TypedArraySerializer::Create(value.As<v8::TypedArray>())); |
145 serializer->serializeTo(data, data_length, result); | 145 serializer->serializeTo(data, data_length, result); |
146 return true; | 146 return true; |
147 } | 147 } |
148 | 148 |
149 bool GinJavaBridgeValueConverter::FromV8Number(v8::Handle<v8::Number> value, | 149 bool GinJavaBridgeValueConverter::FromV8Number(v8::Local<v8::Number> value, |
150 base::Value** out) const { | 150 base::Value** out) const { |
151 double double_value = value->Value(); | 151 double double_value = value->Value(); |
152 if (std::isfinite(double_value)) | 152 if (std::isfinite(double_value)) |
153 return false; | 153 return false; |
154 *out = GinJavaBridgeValue::CreateNonFiniteValue(double_value).release(); | 154 *out = GinJavaBridgeValue::CreateNonFiniteValue(double_value).release(); |
155 return true; | 155 return true; |
156 } | 156 } |
157 | 157 |
158 bool GinJavaBridgeValueConverter::FromV8Undefined(base::Value** out) const { | 158 bool GinJavaBridgeValueConverter::FromV8Undefined(base::Value** out) const { |
159 *out = GinJavaBridgeValue::CreateUndefinedValue().release(); | 159 *out = GinJavaBridgeValue::CreateUndefinedValue().release(); |
160 return true; | 160 return true; |
161 } | 161 } |
162 | 162 |
163 } // namespace content | 163 } // namespace content |
OLD | NEW |