| 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_object.h" | 5 #include "content/renderer/java/gin_java_bridge_object.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "content/renderer/java/gin_java_function_invocation_helper.h" |
| 8 #include "content/common/android/gin_java_bridge_errors.h" | |
| 9 #include "content/common/android/gin_java_bridge_value.h" | |
| 10 #include "content/public/child/v8_value_converter.h" | |
| 11 #include "content/renderer/java/gin_java_bridge_value_converter.h" | |
| 12 #include "gin/function_template.h" | 8 #include "gin/function_template.h" |
| 13 #include "third_party/WebKit/public/web/WebFrame.h" | 9 #include "third_party/WebKit/public/web/WebFrame.h" |
| 14 #include "third_party/WebKit/public/web/WebKit.h" | 10 #include "third_party/WebKit/public/web/WebKit.h" |
| 15 | 11 |
| 16 namespace content { | 12 namespace content { |
| 17 | 13 |
| 18 namespace { | |
| 19 | |
| 20 const char kMethodInvocationErrorMessage[] = | |
| 21 "Java bridge method invocation error"; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 | |
| 26 // static | 14 // static |
| 27 GinJavaBridgeObject* GinJavaBridgeObject::InjectNamed( | 15 GinJavaBridgeObject* GinJavaBridgeObject::InjectNamed( |
| 28 blink::WebFrame* frame, | 16 blink::WebFrame* frame, |
| 29 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, | 17 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, |
| 30 const std::string& object_name, | 18 const std::string& object_name, |
| 31 GinJavaBridgeDispatcher::ObjectID object_id) { | 19 GinJavaBridgeDispatcher::ObjectID object_id) { |
| 32 v8::Isolate* isolate = blink::mainThreadIsolate(); | 20 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 33 v8::HandleScope handle_scope(isolate); | 21 v8::HandleScope handle_scope(isolate); |
| 34 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 22 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 35 if (context.IsEmpty()) | 23 if (context.IsEmpty()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 59 blink::mainThreadIsolate(), dispatcher, object_id); | 47 blink::mainThreadIsolate(), dispatcher, object_id); |
| 60 } | 48 } |
| 61 | 49 |
| 62 GinJavaBridgeObject::GinJavaBridgeObject( | 50 GinJavaBridgeObject::GinJavaBridgeObject( |
| 63 v8::Isolate* isolate, | 51 v8::Isolate* isolate, |
| 64 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, | 52 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher, |
| 65 GinJavaBridgeDispatcher::ObjectID object_id) | 53 GinJavaBridgeDispatcher::ObjectID object_id) |
| 66 : gin::NamedPropertyInterceptor(isolate, this), | 54 : gin::NamedPropertyInterceptor(isolate, this), |
| 67 dispatcher_(dispatcher), | 55 dispatcher_(dispatcher), |
| 68 object_id_(object_id), | 56 object_id_(object_id), |
| 69 converter_(new GinJavaBridgeValueConverter()), | |
| 70 template_cache_(isolate) { | 57 template_cache_(isolate) { |
| 71 } | 58 } |
| 72 | 59 |
| 73 GinJavaBridgeObject::~GinJavaBridgeObject() { | 60 GinJavaBridgeObject::~GinJavaBridgeObject() { |
| 74 if (dispatcher_) | 61 if (dispatcher_) |
| 75 dispatcher_->OnGinJavaBridgeObjectDeleted(object_id_); | 62 dispatcher_->OnGinJavaBridgeObjectDeleted(this); |
| 76 } | 63 } |
| 77 | 64 |
| 78 gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder( | 65 gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder( |
| 79 v8::Isolate* isolate) { | 66 v8::Isolate* isolate) { |
| 80 return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate) | 67 return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate) |
| 81 .AddNamedPropertyInterceptor(); | 68 .AddNamedPropertyInterceptor(); |
| 82 } | 69 } |
| 83 | 70 |
| 84 v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty( | 71 v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty( |
| 85 v8::Isolate* isolate, | 72 v8::Isolate* isolate, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 106 return std::vector<std::string> (method_names.begin(), method_names.end()); | 93 return std::vector<std::string> (method_names.begin(), method_names.end()); |
| 107 } | 94 } |
| 108 | 95 |
| 109 v8::Local<v8::FunctionTemplate> GinJavaBridgeObject::GetFunctionTemplate( | 96 v8::Local<v8::FunctionTemplate> GinJavaBridgeObject::GetFunctionTemplate( |
| 110 v8::Isolate* isolate, | 97 v8::Isolate* isolate, |
| 111 const std::string& name) { | 98 const std::string& name) { |
| 112 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name); | 99 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name); |
| 113 if (!function_template.IsEmpty()) | 100 if (!function_template.IsEmpty()) |
| 114 return function_template; | 101 return function_template; |
| 115 function_template = gin::CreateFunctionTemplate( | 102 function_template = gin::CreateFunctionTemplate( |
| 116 isolate, base::Bind(&GinJavaBridgeObject::InvokeMethod, | 103 isolate, base::Bind(&GinJavaFunctionInvocationHelper::Invoke, |
| 117 base::Unretained(this), name)); | 104 base::Owned(new GinJavaFunctionInvocationHelper( |
| 105 name, dispatcher_)))); |
| 118 template_cache_.Set(name, function_template); | 106 template_cache_.Set(name, function_template); |
| 119 return function_template; | 107 return function_template; |
| 120 } | 108 } |
| 121 | 109 |
| 122 v8::Handle<v8::Value> GinJavaBridgeObject::InvokeMethod( | |
| 123 const std::string& name, | |
| 124 gin::Arguments* args) { | |
| 125 if (!dispatcher_) { | |
| 126 args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8( | |
| 127 args->isolate(), kMethodInvocationErrorMessage))); | |
| 128 return v8::Undefined(args->isolate()); | |
| 129 } | |
| 130 | |
| 131 base::ListValue arguments; | |
| 132 { | |
| 133 v8::HandleScope handle_scope(args->isolate()); | |
| 134 v8::Handle<v8::Context> context = args->isolate()->GetCurrentContext(); | |
| 135 v8::Handle<v8::Value> val; | |
| 136 while (args->GetNext(&val)) { | |
| 137 scoped_ptr<base::Value> arg(converter_->FromV8Value(val, context)); | |
| 138 if (arg.get()) { | |
| 139 arguments.Append(arg.release()); | |
| 140 } else { | |
| 141 arguments.Append(base::Value::CreateNullValue()); | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 GinJavaBridgeError error; | |
| 147 scoped_ptr<base::Value> result = dispatcher_->InvokeJavaMethod( | |
| 148 object_id_, name, arguments, &error); | |
| 149 if (!result.get()) { | |
| 150 args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8( | |
| 151 args->isolate(), GinJavaBridgeErrorToString(error)))); | |
| 152 return v8::Undefined(args->isolate()); | |
| 153 } | |
| 154 if (!result->IsType(base::Value::TYPE_BINARY)) { | |
| 155 return converter_->ToV8Value(result.get(), | |
| 156 args->isolate()->GetCurrentContext()); | |
| 157 } | |
| 158 | |
| 159 scoped_ptr<const GinJavaBridgeValue> gin_value = | |
| 160 GinJavaBridgeValue::FromValue(result.get()); | |
| 161 if (gin_value->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID)) { | |
| 162 GinJavaBridgeObject* result = NULL; | |
| 163 GinJavaBridgeDispatcher::ObjectID object_id; | |
| 164 if (gin_value->GetAsObjectID(&object_id)) { | |
| 165 result = dispatcher_->GetObject(object_id); | |
| 166 } | |
| 167 if (result) { | |
| 168 gin::Handle<GinJavaBridgeObject> controller = | |
| 169 gin::CreateHandle(args->isolate(), result); | |
| 170 if (controller.IsEmpty()) | |
| 171 return v8::Undefined(args->isolate()); | |
| 172 return controller.ToV8(); | |
| 173 } | |
| 174 } else if (gin_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE)) { | |
| 175 float float_value; | |
| 176 gin_value->GetAsNonFinite(&float_value); | |
| 177 return v8::Number::New(args->isolate(), float_value); | |
| 178 } | |
| 179 return v8::Undefined(args->isolate()); | |
| 180 } | |
| 181 | |
| 182 gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin}; | 110 gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin}; |
| 183 | 111 |
| 184 } // namespace content | 112 } // namespace content |
| OLD | NEW |