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_dispatcher.h"
7 #include "base/auto_reset.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/common/gin_java_bridge_messages.h"
11 #include "content/public/renderer/render_frame.h"
12 #include "content/renderer/java/gin_java_bridge_object.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebView.h"
18 GinJavaBridgeDispatcher::GinJavaBridgeDispatcher(RenderFrame
* render_frame
)
19 : RenderFrameObserver(render_frame
),
20 inside_did_clear_window_object_(false) {
23 GinJavaBridgeDispatcher::~GinJavaBridgeDispatcher() {
26 bool GinJavaBridgeDispatcher::OnMessageReceived(const IPC::Message
& msg
) {
28 IPC_BEGIN_MESSAGE_MAP(GinJavaBridgeDispatcher
, msg
)
29 IPC_MESSAGE_HANDLER(GinJavaBridgeMsg_AddNamedObject
, OnAddNamedObject
)
30 IPC_MESSAGE_HANDLER(GinJavaBridgeMsg_RemoveNamedObject
, OnRemoveNamedObject
)
31 IPC_MESSAGE_UNHANDLED(handled
= false)
36 void GinJavaBridgeDispatcher::DidClearWindowObject() {
37 // Accessing window object when adding properties to it may trigger
38 // a nested call to DidClearWindowObject.
39 if (inside_did_clear_window_object_
)
41 base::AutoReset
<bool> flag_entry(&inside_did_clear_window_object_
, true);
42 for (NamedObjectMap::const_iterator iter
= named_objects_
.begin();
43 iter
!= named_objects_
.end(); ++iter
) {
44 // Always create a new GinJavaBridgeObject, so we don't pull any of the V8
45 // wrapper's custom properties into the context of the page we have
46 // navigated to. The old GinJavaBridgeObject will be automatically
47 // deleted after its wrapper will be collected.
48 // On the browser side, we ignore wrapper deletion events for named objects,
49 // as they are only removed upon embedder's request (RemoveNamedObject).
50 if (objects_
.Lookup(iter
->second
))
51 objects_
.Remove(iter
->second
);
52 GinJavaBridgeObject
* object
= GinJavaBridgeObject::InjectNamed(
53 render_frame()->GetWebFrame(), AsWeakPtr(), iter
->first
, iter
->second
);
55 objects_
.AddWithID(object
, iter
->second
);
57 // Inform the host about wrapper creation failure.
58 render_frame()->Send(new GinJavaBridgeHostMsg_ObjectWrapperDeleted(
59 routing_id(), iter
->second
));
64 void GinJavaBridgeDispatcher::OnAddNamedObject(
65 const std::string
& name
,
67 // Added objects only become available after page reload, so here they
68 // are only added into the internal map.
69 named_objects_
.insert(std::make_pair(name
, object_id
));
72 void GinJavaBridgeDispatcher::OnRemoveNamedObject(const std::string
& name
) {
73 // Removal becomes in effect on next reload. We simply removing the entry
75 NamedObjectMap::iterator iter
= named_objects_
.find(name
);
76 DCHECK(iter
!= named_objects_
.end());
77 named_objects_
.erase(iter
);
80 void GinJavaBridgeDispatcher::GetJavaMethods(
82 std::set
<std::string
>* methods
) {
83 render_frame()->Send(new GinJavaBridgeHostMsg_GetMethods(
84 routing_id(), object_id
, methods
));
87 bool GinJavaBridgeDispatcher::HasJavaMethod(ObjectID object_id
,
88 const std::string
& method_name
) {
90 render_frame()->Send(new GinJavaBridgeHostMsg_HasMethod(
91 routing_id(), object_id
, method_name
, &result
));
95 scoped_ptr
<base::Value
> GinJavaBridgeDispatcher::InvokeJavaMethod(
97 const std::string
& method_name
,
98 const base::ListValue
& arguments
,
99 GinJavaBridgeError
* error
) {
100 base::ListValue result_wrapper
;
101 render_frame()->Send(
102 new GinJavaBridgeHostMsg_InvokeMethod(routing_id(),
109 if (result_wrapper
.Get(0, &result
)) {
110 return scoped_ptr
<base::Value
>(result
->DeepCopy());
112 return scoped_ptr
<base::Value
>();
116 GinJavaBridgeObject
* GinJavaBridgeDispatcher::GetObject(ObjectID object_id
) {
117 GinJavaBridgeObject
* result
= objects_
.Lookup(object_id
);
119 result
= GinJavaBridgeObject::InjectAnonymous(AsWeakPtr(), object_id
);
121 objects_
.AddWithID(result
, object_id
);
126 void GinJavaBridgeDispatcher::OnGinJavaBridgeObjectDeleted(
127 GinJavaBridgeObject
* object
) {
128 int object_id
= object
->object_id();
129 // Ignore cleaning up of old object wrappers.
130 if (objects_
.Lookup(object_id
) != object
) return;
131 objects_
.Remove(object_id
);
132 render_frame()->Send(
133 new GinJavaBridgeHostMsg_ObjectWrapperDeleted(routing_id(), object_id
));
136 } // namespace content