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

Side by Side Diff: content/renderer/java/gin_java_bridge_object.cc

Issue 259033002: [Android] Implement renderer side of Gin Java Bridge (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased, conflicts resolved Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "content/renderer/java/gin_java_bridge_object.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/common/android/gin_java_bridge_value.h"
9 #include "content/public/renderer/v8_value_converter.h"
10 #include "content/renderer/java/gin_java_bridge_value_converter.h"
11 #include "gin/function_template.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebKit.h"
14
15 namespace content {
16
17 namespace {
18
19 const char kMethodInvocationErrorMessage[] =
20 "Java bridge method invocation error";
21
22 } // namespace
23
24
25 // static
26 GinJavaBridgeObject* GinJavaBridgeObject::InjectNamed(
27 blink::WebFrame* frame,
28 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher,
29 const std::string& object_name,
30 GinJavaBridgeDispatcher::ObjectID object_id) {
31 v8::Isolate* isolate = blink::mainThreadIsolate();
32 v8::HandleScope handle_scope(isolate);
33 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
34 if (context.IsEmpty())
35 return NULL;
36
37 GinJavaBridgeObject* object =
38 new GinJavaBridgeObject(isolate, dispatcher, object_id);
39
40 v8::Context::Scope context_scope(context);
41 v8::Handle<v8::Object> global = context->Global();
42 gin::Handle<GinJavaBridgeObject> controller =
43 gin::CreateHandle(isolate, object);
44 // WrappableBase instance deletes itself in case of a wrapper
45 // creation failure, thus there is no need to delete |object|.
46 if (controller.IsEmpty())
47 return NULL;
48
49 global->Set(gin::StringToV8(isolate, object_name), controller.ToV8());
50 return object;
51 }
52
53 // static
54 GinJavaBridgeObject* GinJavaBridgeObject::InjectAnonymous(
55 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher,
56 GinJavaBridgeDispatcher::ObjectID object_id) {
57 return new GinJavaBridgeObject(
58 blink::mainThreadIsolate(), dispatcher, object_id);
59 }
60
61 GinJavaBridgeObject::GinJavaBridgeObject(
62 v8::Isolate* isolate,
63 const base::WeakPtr<GinJavaBridgeDispatcher>& dispatcher,
64 GinJavaBridgeDispatcher::ObjectID object_id)
65 : gin::NamedPropertyInterceptor(isolate, this),
66 dispatcher_(dispatcher),
67 object_id_(object_id),
68 converter_(new GinJavaBridgeValueConverter()) {
69 }
70
71 GinJavaBridgeObject::~GinJavaBridgeObject() {
72 if (dispatcher_)
73 dispatcher_->OnGinJavaBridgeObjectDeleted(object_id_);
74 }
75
76 gin::ObjectTemplateBuilder GinJavaBridgeObject::GetObjectTemplateBuilder(
77 v8::Isolate* isolate) {
78 return gin::Wrappable<GinJavaBridgeObject>::GetObjectTemplateBuilder(isolate)
79 .AddNamedPropertyInterceptor();
80 }
81
82 v8::Local<v8::Value> GinJavaBridgeObject::GetNamedProperty(
83 v8::Isolate* isolate,
84 const std::string& property) {
85 if (dispatcher_ && dispatcher_->HasJavaMethod(object_id_, property)) {
86 return gin::CreateFunctionTemplate(
87 isolate,
88 base::Bind(&GinJavaBridgeObject::InvokeMethod,
89 base::Unretained(this),
90 property))->GetFunction();
91 } else {
92 return v8::Local<v8::Value>();
93 }
94 }
95
96 std::vector<std::string> GinJavaBridgeObject::EnumerateNamedProperties(
97 v8::Isolate* isolate) {
98 std::set<std::string> method_names;
99 if (dispatcher_)
100 dispatcher_->GetJavaMethods(object_id_, &method_names);
101 return std::vector<std::string> (method_names.begin(), method_names.end());
102 }
103
104 v8::Handle<v8::Value> GinJavaBridgeObject::InvokeMethod(
105 const std::string& name,
106 gin::Arguments* args) {
107 if (!dispatcher_) {
108 args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
109 args->isolate(), kMethodInvocationErrorMessage)));
110 return v8::Undefined(args->isolate());
111 }
112
113 base::ListValue arguments;
114 {
115 v8::HandleScope handle_scope(args->isolate());
116 v8::Handle<v8::Context> context = args->isolate()->GetCurrentContext();
117 v8::Handle<v8::Value> val;
118 while (args->GetNext(&val)) {
119 scoped_ptr<base::Value> arg(converter_->FromV8Value(val, context));
120 if (arg.get()) {
121 arguments.Append(arg.release());
122 } else {
123 arguments.Append(base::Value::CreateNullValue());
124 }
125 }
126 }
127
128 scoped_ptr<base::Value> result =
129 dispatcher_->InvokeJavaMethod(object_id_, name, arguments);
130 if (!result.get()) {
131 args->isolate()->ThrowException(v8::Exception::Error(gin::StringToV8(
132 args->isolate(), kMethodInvocationErrorMessage)));
133 return v8::Undefined(args->isolate());
134 }
135 if (!result->IsType(base::Value::TYPE_BINARY)) {
136 return converter_->ToV8Value(result.get(),
137 args->isolate()->GetCurrentContext());
138 }
139
140 scoped_ptr<const GinJavaBridgeValue> gin_value =
141 GinJavaBridgeValue::FromValue(result.get());
142 if (gin_value->IsType(GinJavaBridgeValue::TYPE_OBJECT_ID)) {
143 GinJavaBridgeObject* result = NULL;
144 GinJavaBridgeDispatcher::ObjectID object_id;
145 if (gin_value->GetAsObjectID(&object_id)) {
146 result = dispatcher_->GetObject(object_id);
147 }
148 if (result) {
149 gin::Handle<GinJavaBridgeObject> controller =
150 gin::CreateHandle(args->isolate(), result);
151 if (controller.IsEmpty())
152 return v8::Undefined(args->isolate());
153 return controller.ToV8();
154 }
155 } else if (gin_value->IsType(GinJavaBridgeValue::TYPE_NONFINITE)) {
156 float float_value;
157 gin_value->GetAsNonFinite(&float_value);
158 return v8::Number::New(args->isolate(), float_value);
159 }
160 return v8::Undefined(args->isolate());
161 }
162
163 gin::WrapperInfo GinJavaBridgeObject::kWrapperInfo = {gin::kEmbedderNativeGin};
164
165 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/java/gin_java_bridge_object.h ('k') | content/renderer/java/gin_java_bridge_value_converter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698