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.
5 #include "content/renderer/java/gin_java_bridge_object.h"
7 #include "content/common/gin_java_bridge_messages.h"
8 #include "content/public/renderer/render_thread.h"
9 #include "content/renderer/java/gin_java_function_invocation_helper.h"
10 #include "gin/function_template.h"
11 #include "third_party/WebKit/public/web/WebFrame.h"
12 #include "third_party/WebKit/public/web/WebKit.h"
17 GinJavaBridgeObject
* GinJavaBridgeObject::InjectNamed(
18 blink::WebFrame
* frame
,
19 const base::WeakPtr
<GinJavaBridgeDispatcher
>& dispatcher
,
20 const std::string
& object_name
,
21 GinJavaBridgeDispatcher::ObjectID object_id
) {
22 v8::Isolate
* isolate
= blink::mainThreadIsolate();
23 v8::HandleScope
handle_scope(isolate
);
24 v8::Local
<v8::Context
> context
= frame
->mainWorldScriptContext();
25 if (context
.IsEmpty())
28 GinJavaBridgeObject
* object
=
29 new GinJavaBridgeObject(isolate
, dispatcher
, object_id
);
31 v8::Context::Scope
context_scope(context
);
32 v8::Local
<v8::Object
> global
= context
->Global();
33 gin::Handle
<GinJavaBridgeObject
> controller
=
34 gin::CreateHandle(isolate
, object
);
35 // WrappableBase instance deletes itself in case of a wrapper
36 // creation failure, thus there is no need to delete |object|.
37 if (controller
.IsEmpty())
40 global
->Set(gin::StringToV8(isolate
, object_name
), controller
.ToV8());
45 GinJavaBridgeObject
* GinJavaBridgeObject::InjectAnonymous(
46 const base::WeakPtr
<GinJavaBridgeDispatcher
>& dispatcher
,
47 GinJavaBridgeDispatcher::ObjectID object_id
) {
48 return new GinJavaBridgeObject(
49 blink::mainThreadIsolate(), dispatcher
, object_id
);
52 GinJavaBridgeObject::GinJavaBridgeObject(
54 const base::WeakPtr
<GinJavaBridgeDispatcher
>& dispatcher
,
55 GinJavaBridgeDispatcher::ObjectID object_id
)
56 : gin::NamedPropertyInterceptor(isolate
, this),
57 dispatcher_(dispatcher
),
58 object_id_(object_id
),
59 frame_routing_id_(dispatcher_
->routing_id()),
60 template_cache_(isolate
) {
63 GinJavaBridgeObject::~GinJavaBridgeObject() {
65 dispatcher_
->OnGinJavaBridgeObjectDeleted(this);
67 // A wrapper can outlive a render frame, and thus the dispatcher.
68 // Note that we intercept GinJavaBridgeHostMsg messages in a browser filter
69 // thus it's OK to send the message with a routing id of a ceased frame.
70 RenderThread::Get()->Send(new GinJavaBridgeHostMsg_ObjectWrapperDeleted(
71 frame_routing_id_
, object_id_
));
75 gin::ObjectTemplateBuilder
GinJavaBridgeObject::GetObjectTemplateBuilder(
76 v8::Isolate
* isolate
) {
77 return gin::Wrappable
<GinJavaBridgeObject
>::GetObjectTemplateBuilder(isolate
)
78 .AddNamedPropertyInterceptor();
81 v8::Local
<v8::Value
> GinJavaBridgeObject::GetNamedProperty(
83 const std::string
& property
) {
84 std::map
<std::string
, bool>::iterator method_pos
=
85 known_methods_
.find(property
);
86 if (method_pos
== known_methods_
.end()) {
88 return v8::Local
<v8::Value
>();
90 known_methods_
[property
] = dispatcher_
->HasJavaMethod(object_id_
, property
);
92 if (known_methods_
[property
])
93 return GetFunctionTemplate(isolate
, property
)->GetFunction();
95 return v8::Local
<v8::Value
>();
98 std::vector
<std::string
> GinJavaBridgeObject::EnumerateNamedProperties(
99 v8::Isolate
* isolate
) {
100 std::set
<std::string
> method_names
;
102 dispatcher_
->GetJavaMethods(object_id_
, &method_names
);
103 return std::vector
<std::string
> (method_names
.begin(), method_names
.end());
106 v8::Local
<v8::FunctionTemplate
> GinJavaBridgeObject::GetFunctionTemplate(
107 v8::Isolate
* isolate
,
108 const std::string
& name
) {
109 v8::Local
<v8::FunctionTemplate
> function_template
= template_cache_
.Get(name
);
110 if (!function_template
.IsEmpty())
111 return function_template
;
112 function_template
= gin::CreateFunctionTemplate(
113 isolate
, base::Bind(&GinJavaFunctionInvocationHelper::Invoke
,
114 base::Owned(new GinJavaFunctionInvocationHelper(
115 name
, dispatcher_
))));
116 template_cache_
.Set(name
, function_template
);
117 return function_template
;
120 gin::WrapperInfo
GinJavaBridgeObject::kWrapperInfo
= {gin::kEmbedderNativeGin
};
122 } // namespace content