Chromium Code Reviews| Index: content/renderer/java/gin_java_bridge_object.cc |
| diff --git a/content/renderer/java/gin_java_bridge_object.cc b/content/renderer/java/gin_java_bridge_object.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5d1fb5150c49a7d74d2d73d2baa77857113df613 |
| --- /dev/null |
| +++ b/content/renderer/java/gin_java_bridge_object.cc |
| @@ -0,0 +1,298 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/java/gin_java_bridge_object.h" |
| + |
| +#include "base/float_util.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/common/android/gin_java_bridge_value.h" |
| +#include "content/public/renderer/v8_value_converter.h" |
| +#include "gin/function_template.h" |
| +#include "third_party/WebKit/public/web/WebArrayBufferView.h" |
| +#include "third_party/WebKit/public/web/WebFrame.h" |
| +#include "third_party/WebKit/public/web/WebKit.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +const char kMethodInvocationErrorMessage[] = |
| + "Java bridge method invocation error"; |
| + |
| +} // namespace |
| + |
| + |
| +// static |
| +GinJavaBridgeObject* GinJavaBridgeObject::Inject( |
| + blink::WebFrame* frame, |
| + const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, |
| + const std::string& object_name, |
| + GinJavaBridgeDispatcher::ObjectID object_id) { |
| + GinJavaBridgeObject* result = new GinJavaBridgeObject( |
| + blink::mainThreadIsolate(), dispatcher, object_id); |
| + if (InjectExisting(frame, result, object_name)) { |
| + return result; |
| + } else { |
| + delete result; |
| + return NULL; |
| + } |
| +} |
| + |
| +// static |
| +GinJavaBridgeObject* GinJavaBridgeObject::InjectAnonymous( |
| + const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, |
| + GinJavaBridgeDispatcher::ObjectID object_id) { |
| + return new GinJavaBridgeObject( |
| + blink::mainThreadIsolate(), dispatcher, object_id); |
| +} |
| + |
| +// static |
| +bool GinJavaBridgeObject::InjectExisting(blink::WebFrame* frame, |
| + GinJavaBridgeObject* object, |
| + const std::string& object_name) { |
| + v8::Isolate* isolate = blink::mainThreadIsolate(); |
| + v8::HandleScope handle_scope(isolate); |
| + v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| + if (context.IsEmpty()) |
| + return false; |
| + |
| + v8::Context::Scope context_scope(context); |
| + v8::Handle<v8::Object> global = context->Global(); |
| + gin::Handle<GinJavaBridgeObject> controller = |
| + gin::CreateHandle(isolate, object); |
|
jochen (gone - plz use gerrit)
2014/04/29 13:50:16
return false here if controller.IsEmpty()
mnaganov (inactive)
2014/04/30 16:46:26
Done here and in line 164.
|
| + global->Set(gin::StringToV8(isolate, object_name), controller.ToV8()); |
| + return true; |
| +} |
| + |
| +GinJavaBridgeObject::GinJavaBridgeObject( |
| + v8::Isolate* isolate, |
| + const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, |
| + GinJavaBridgeDispatcher::ObjectID object_id) |
| + : gin::NamedPropertyInterceptor(isolate, this), |
| + dispatcher_(dispatcher), |
| + object_id_(object_id), |
| + converter_(V8ValueConverter::create()) { |
| + converter_->SetDateAllowed(false); |
| + converter_->SetRegExpAllowed(false); |
| + converter_->SetFunctionAllowed(true); |
| + converter_->SetStrategy(this); |
| +} |
| + |
| +GinJavaBridgeObject::~GinJavaBridgeObject() { |
| + if (dispatcher_) |
| + dispatcher_->OnGinJavaBridgeObjectDeleted(object_id_); |
| +} |
| + |
| +gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder( |
| + v8::Isolate* isolate) { |
| + return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate) |
| + .AddNamedPropertyInterceptor(); |
| +} |
| + |
| +v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty( |
| + v8::Isolate* isolate, |
| + const std::string& property) { |
| + if (dispatcher_ && dispatcher_->HasJavaMethod(object_id_, property)) { |
| + return gin::CreateFunctionTemplate( |
| + isolate, |
| + base::Bind(&GinJavaBridgeObject::InvokeMethod, |
| + base::Unretained(this), |
| + property))->GetFunction(); |
| + } else { |
| + return v8::Local<v8::Value>(); |
| + } |
| +} |
| + |
| +std::vector<std::string> GinJavaBridgeObject::EnumerateNamedProperties( |
| + v8::Isolate* isolate) { |
| + std::set<std::string> method_names; |
| + if (dispatcher_) { |
| + dispatcher_->GetJavaMethods(object_id_, &method_names); |
| + } |
| + return std::vector<std::string> (method_names.begin(), method_names.end()); |
| +} |
| + |
| +v8::Handle<v8::Value> GinJavaBridgeObject::InvokeMethod( |
| + const std::string& name, |
| + gin::Arguments* args) { |
| + if (!dispatcher_) { |
| + args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8( |
| + args->isolate(), kMethodInvocationErrorMessage))); |
| + return v8::Undefined(args->isolate()); |
| + } |
| + |
| + base::ListValue arguments; |
| + { |
| + v8::HandleScope handle_scope(args->isolate()); |
| + v8::Handle<v8::Context> context = args->isolate()->GetCurrentContext(); |
| + v8::Handle<v8::Value> val; |
| + while (args->GetNext(&val)) { |
| + scoped_ptr<base::Value> arg(converter_->FromV8Value(val, context)); |
| + if (arg.get()) { |
| + arguments.Append(arg.release()); |
| + } else { |
| + arguments.Append(base::Value::CreateNullValue()); |
| + } |
| + } |
| + } |
| + |
| + scoped_ptr<base::Value> result = |
| + dispatcher_->InvokeJavaMethod(object_id_, name, arguments); |
| + if (!result.get()) { |
| + args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8( |
| + args->isolate(), kMethodInvocationErrorMessage))); |
| + return v8::Undefined(args->isolate()); |
| + } |
| + if (!result->IsType(base::Value::TYPE_BINARY)) { |
| + return converter_->ToV8Value(result.get(), |
| + args->isolate()->GetCurrentContext()); |
| + } |
| + |
| + scoped_ptr<const GinJavaBridgeValue> gin_value = |
| + GinJavaBridgeValue::FromValue(result.get()); |
| + if (gin_value->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID)) { |
| + GinJavaBridgeObject* result = NULL; |
| + GinJavaBridgeDispatcher::ObjectID object_id; |
| + if (gin_value->GetAsObjectID(&object_id)) { |
| + result = dispatcher_->GetObject(object_id); |
| + } |
| + if (result) { |
| + gin::Handle<GinJavaBridgeObject> controller = |
| + gin::CreateHandle(args->isolate(), result); |
| + return controller.ToV8(); |
| + } |
| + } else if (gin_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE)) { |
| + float float_value; |
| + gin_value->GetAsNonFinite(&float_value); |
| + return v8::Number::New(args->isolate(), float_value); |
| + } |
| + return v8::Undefined(args->isolate()); |
| +} |
| + |
| +bool GinJavaBridgeObject::FromV8Object( |
| + v8::Handle<v8::Object> value, |
| + base::Value** out, |
| + v8::Isolate* isolate, |
| + const FromV8ValueCallback& callback) const { |
| + GinJavaBridgeObject* unwrapped; |
| + if (!gin::ConvertFromV8(isolate, value, &unwrapped)) { |
| + return false; |
| + } |
| + *out = |
| + GinJavaBridgeValue::CreateObjectIDValue(unwrapped->object_id_).release(); |
| + return true; |
| +} |
| + |
| +namespace { |
| + |
| +class TypedArraySerializer { |
| + public: |
| + virtual ~TypedArraySerializer() {} |
| + static scoped_ptr<TypedArraySerializer> Create( |
| + v8::Handle<v8::TypedArray> typed_array); |
| + virtual void serializeTo(char* data, |
| + size_t data_length, |
| + base::ListValue* out) = 0; |
| + protected: |
| + TypedArraySerializer() {} |
| +}; |
| + |
| +template <typename ElementType, typename ListType> |
| +class TypedArraySerializerImpl : public TypedArraySerializer { |
| + public: |
| + static scoped_ptr<TypedArraySerializer> Create( |
| + v8::Handle<v8::TypedArray> typed_array) { |
| + scoped_ptr<TypedArraySerializerImpl<ElementType, ListType> > result( |
| + new TypedArraySerializerImpl<ElementType, ListType>(typed_array)); |
| + return result.template PassAs<TypedArraySerializer>(); |
| + } |
| + |
| + virtual void serializeTo(char* data, |
| + size_t data_length, |
| + base::ListValue* out) OVERRIDE { |
| + DCHECK_EQ(data_length, typed_array_->Length() * sizeof(ElementType)); |
| + for (ElementType *element = reinterpret_cast<ElementType*>(data), |
| + *end = element + typed_array_->Length(); |
| + element != end; |
| + ++element) { |
| + const ListType list_value = *element; |
| + out->Append(new base::FundamentalValue(list_value)); |
| + } |
| + } |
| + |
| + private: |
| + explicit TypedArraySerializerImpl(v8::Handle<v8::TypedArray> typed_array) |
| + : typed_array_(typed_array) {} |
| + |
| + v8::Handle<v8::TypedArray> typed_array_; |
| +}; |
| + |
| +// static |
| +scoped_ptr<TypedArraySerializer> TypedArraySerializer::Create( |
| + v8::Handle<v8::TypedArray> typed_array) { |
| + if (typed_array->IsInt8Array() || |
| + typed_array->IsUint8Array() || |
| + typed_array->IsUint8ClampedArray()) { |
| + return TypedArraySerializerImpl<char, int>::Create(typed_array).Pass(); |
| + } else if (typed_array->IsInt16Array() || typed_array->IsUint16Array()) { |
| + return TypedArraySerializerImpl<int16_t, int>::Create(typed_array).Pass(); |
| + } else if (typed_array->IsInt32Array() || typed_array->IsUint32Array()) { |
| + return TypedArraySerializerImpl<int32_t, int>::Create(typed_array).Pass(); |
| + } else if (typed_array->IsFloat32Array()) { |
| + return TypedArraySerializerImpl<float, double>::Create(typed_array).Pass(); |
| + } else if (typed_array->IsFloat64Array()) { |
| + return TypedArraySerializerImpl<double, double>::Create(typed_array).Pass(); |
| + } |
| + NOTREACHED(); |
| + return scoped_ptr<TypedArraySerializer>(); |
| +} |
| + |
| +} // namespace |
| + |
| +bool GinJavaBridgeObject::FromV8ArrayBuffer(v8::Handle<v8::Object> value, |
| + base::Value** out) const { |
| + if (!value->IsTypedArray()) { |
| + *out = GinJavaBridgeValue::CreateUndefinedValue().release(); |
| + return true; |
| + } |
| + |
| + char* data = NULL; |
| + size_t data_length = 0; |
| + scoped_ptr<blink::WebArrayBufferView> view( |
| + blink::WebArrayBufferView::createFromV8Value(value)); |
| + if (view) { |
| + data = reinterpret_cast<char*>(view->baseAddress()) + view->byteOffset(); |
| + data_length = view->byteLength(); |
| + } |
| + if (!data) { |
| + *out = GinJavaBridgeValue::CreateUndefinedValue().release(); |
| + return true; |
| + } |
| + |
| + base::ListValue* result = new base::ListValue(); |
| + *out = result; |
| + scoped_ptr<TypedArraySerializer> serializer( |
| + TypedArraySerializer::Create(value.As<v8::TypedArray>())); |
| + serializer->serializeTo(data, data_length, result); |
| + return true; |
| +} |
| + |
| +bool GinJavaBridgeObject::FromV8Number(v8::Handle<v8::Number> value, |
| + base::Value** out) const { |
| + double double_value = value->Value(); |
| + if (base::IsFinite(double_value)) |
| + return false; |
| + *out = GinJavaBridgeValue::CreateNonFiniteValue(double_value).release(); |
| + return true; |
| +} |
| + |
| +bool GinJavaBridgeObject::FromV8Undefined(base::Value** out) const { |
| + *out = GinJavaBridgeValue::CreateUndefinedValue().release(); |
| + return true; |
| +} |
| + |
| +gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| + |
| +} // namespace content |