OLD | NEW |
---|---|
(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_dispatcher.h" | |
6 | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "content/common/gin_java_bridge_messages.h" | |
10 #include "content/public/renderer/render_frame.h" | |
11 #include "content/renderer/java/gin_java_bridge_object.h" | |
12 #include "third_party/WebKit/public/web/WebFrame.h" | |
13 #include "third_party/WebKit/public/web/WebView.h" | |
14 | |
15 namespace content { | |
16 | |
17 GinJavaBridgeDispatcher::GinJavaBridgeDispatcher(RenderFrame* render_frame) | |
18 : RenderFrameObserver(render_frame) { | |
19 } | |
20 | |
21 GinJavaBridgeDispatcher::~GinJavaBridgeDispatcher() { | |
22 } | |
23 | |
24 bool GinJavaBridgeDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
25 bool handled = true; | |
26 IPC_BEGIN_MESSAGE_MAP(GinJavaBridgeDispatcher, msg) | |
27 IPC_MESSAGE_HANDLER(GinJavaBridgeMsg_AddNamedObject, OnAddNamedObject) | |
28 IPC_MESSAGE_HANDLER(GinJavaBridgeMsg_RemoveNamedObject, OnRemoveNamedObject) | |
29 IPC_MESSAGE_UNHANDLED(handled = false) | |
30 IPC_END_MESSAGE_MAP() | |
31 return handled; | |
32 } | |
33 | |
34 void GinJavaBridgeDispatcher::DidClearWindowObject(int world_id) { | |
35 if (world_id != 0) | |
36 return; | |
37 | |
38 for (NamedObjectMap::const_iterator iter = named_objects_.begin(); | |
39 iter != named_objects_.end(); ++iter) { | |
40 // A wrapper can already be collected, as we don't hold persistent | |
41 // handles for them to avoid memory leaks. However, we would like to try | |
42 // to reuse an existing wrapper as it may have properties set on the JS | |
43 // side. | |
44 GinJavaBridgeObject* object = objects_.Lookup(iter->second); | |
45 bool wrapper_created = false; | |
46 if (object) { | |
jochen (gone - plz use gerrit)
2014/05/05 10:55:49
I'm not sure I understand this. InjectExisting wil
mnaganov (inactive)
2014/05/06 10:54:15
This is my understanding of how this works, please
| |
47 wrapper_created = GinJavaBridgeObject::InjectExisting( | |
48 render_frame()->GetWebFrame(), | |
49 object, | |
50 iter->first); | |
51 } else { | |
52 object = GinJavaBridgeObject::Inject(render_frame()->GetWebFrame(), | |
53 AsWeakPtr(), | |
54 iter->first, | |
55 iter->second); | |
56 if (object) { | |
57 wrapper_created = true; | |
58 objects_.AddWithID(object, iter->second); | |
59 } | |
60 } | |
61 if (!wrapper_created) { | |
62 // Inform the host about wrapper creation failure. | |
63 render_frame()->Send(new GinJavaBridgeHostMsg_ObjectWrapperDeleted( | |
64 routing_id(), iter->second)); | |
65 } | |
66 } | |
67 } | |
68 | |
69 void GinJavaBridgeDispatcher::OnAddNamedObject( | |
70 const std::string& name, | |
71 ObjectID object_id) { | |
72 // Added objects only become available after page reload, so here they | |
73 // are only added into the internal map. | |
74 named_objects_.insert(std::make_pair(name, object_id)); | |
75 } | |
76 | |
77 void GinJavaBridgeDispatcher::OnRemoveNamedObject(const std::string& name) { | |
78 // Removal becomes in effect on next reload. We simply removing the entry | |
79 // from the map here. | |
80 NamedObjectMap::iterator iter = named_objects_.find(name); | |
81 DCHECK(iter != named_objects_.end()); | |
82 named_objects_.erase(iter); | |
83 } | |
84 | |
85 void GinJavaBridgeDispatcher::GetJavaMethods( | |
86 ObjectID object_id, | |
87 std::set<std::string>* methods) { | |
88 render_frame()->Send(new GinJavaBridgeHostMsg_GetMethods( | |
89 routing_id(), object_id, methods)); | |
90 } | |
91 | |
92 bool GinJavaBridgeDispatcher::HasJavaMethod(ObjectID object_id, | |
93 const std::string& method_name) { | |
94 bool result; | |
95 render_frame()->Send(new GinJavaBridgeHostMsg_HasMethod( | |
96 routing_id(), object_id, method_name, &result)); | |
97 return result; | |
98 } | |
99 | |
100 scoped_ptr<base::Value> GinJavaBridgeDispatcher::InvokeJavaMethod( | |
101 ObjectID object_id, | |
102 const std::string& method_name, | |
103 const base::ListValue& arguments) { | |
104 base::ListValue result_wrapper; | |
105 render_frame()->Send( | |
106 new GinJavaBridgeHostMsg_InvokeMethod(routing_id(), | |
107 object_id, | |
108 method_name, | |
109 arguments, | |
110 &result_wrapper)); | |
111 base::Value* result; | |
112 if (result_wrapper.Get(0, &result)) { | |
113 return scoped_ptr<base::Value>(result->DeepCopy()); | |
114 } else { | |
115 return scoped_ptr<base::Value>(); | |
116 } | |
117 } | |
118 | |
119 GinJavaBridgeObject* GinJavaBridgeDispatcher::GetObject(ObjectID object_id) { | |
120 GinJavaBridgeObject* result = objects_.Lookup(object_id); | |
121 if (!result) { | |
122 result = GinJavaBridgeObject::InjectAnonymous(AsWeakPtr(), object_id); | |
123 if (result) | |
124 objects_.AddWithID(result, object_id); | |
125 } | |
126 return result; | |
127 } | |
128 | |
129 void GinJavaBridgeDispatcher::OnGinJavaBridgeObjectDeleted(ObjectID object_id) { | |
130 if (!objects_.Lookup(object_id)) | |
131 return; | |
132 objects_.Remove(object_id); | |
133 render_frame()->Send( | |
134 new GinJavaBridgeHostMsg_ObjectWrapperDeleted(routing_id(), object_id)); | |
135 } | |
136 | |
137 } // namespace content | |
OLD | NEW |