Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1453)

Unified Diff: content/renderer/java/gin_java_function_invocation_helper.cc

Issue 1018413002: [Android] Fix method invocation and wrappers cleanup handling in Java Bridge (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added a dedicated test for attempting to call a non-existing method via `call` Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/java/gin_java_function_invocation_helper.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/java/gin_java_function_invocation_helper.cc
diff --git a/content/renderer/java/gin_java_function_invocation_helper.cc b/content/renderer/java/gin_java_function_invocation_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a113bad4fbfa36f725717a780a6a3aa86e7a3a22
--- /dev/null
+++ b/content/renderer/java/gin_java_function_invocation_helper.cc
@@ -0,0 +1,109 @@
+// Copyright 2015 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_function_invocation_helper.h"
+
+#include "content/common/android/gin_java_bridge_errors.h"
+#include "content/common/android/gin_java_bridge_value.h"
+#include "content/public/child/v8_value_converter.h"
+#include "content/renderer/java/gin_java_bridge_object.h"
+#include "content/renderer/java/gin_java_bridge_value_converter.h"
+
+namespace content {
+
+namespace {
+
+const char kMethodInvocationAsConstructorDisallowed[] =
+ "Java bridge method can't be invoked as a constructor";
+const char kMethodInvocationOnNonInjectedObjectDisallowed[] =
+ "Java bridge method can't be invoked on a non-injected object";
+const char kMethodInvocationErrorMessage[] =
+ "Java bridge method invocation error";
+
+} // namespace
+
+GinJavaFunctionInvocationHelper::GinJavaFunctionInvocationHelper(
+ const std::string& method_name,
+ const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher)
+ : method_name_(method_name),
+ dispatcher_(dispatcher),
+ converter_(new GinJavaBridgeValueConverter()) {
+}
+
+GinJavaFunctionInvocationHelper::~GinJavaFunctionInvocationHelper() {
+}
+
+v8::Handle<v8::Value> GinJavaFunctionInvocationHelper::Invoke(
+ gin::Arguments* args) {
+ if (!dispatcher_) {
+ args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
+ args->isolate(), kMethodInvocationErrorMessage)));
+ return v8::Undefined(args->isolate());
+ }
+
+ if (args->IsConstructCall()) {
+ args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
+ args->isolate(), kMethodInvocationAsConstructorDisallowed)));
+ return v8::Undefined(args->isolate());
+ }
+
+ content::GinJavaBridgeObject* object = NULL;
+ if (!args->GetHolder(&object) || !object) {
+ args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
+ args->isolate(), kMethodInvocationOnNonInjectedObjectDisallowed)));
+ 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());
+ }
+ }
+ }
+
+ GinJavaBridgeError error;
+ scoped_ptr<base::Value> result = dispatcher_->InvokeJavaMethod(
+ object->object_id(), method_name_, arguments, &error);
+ if (!result.get()) {
+ args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
+ args->isolate(), GinJavaBridgeErrorToString(error))));
+ 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);
+ if (controller.IsEmpty())
+ return v8::Undefined(args->isolate());
+ 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());
+}
+
+} // namespace content
« no previous file with comments | « content/renderer/java/gin_java_function_invocation_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698