Chromium Code Reviews| 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) { | |
|
jochen (gone - plz use gerrit)
2014/04/29 13:50:16
for world_id != 0, you should exit early.
mnaganov (inactive)
2014/04/30 16:46:26
Done.
| |
| 35 for (NamedObjectMap::const_iterator iter = named_objects_.begin(); | |
| 36 iter != named_objects_.end(); ++iter) { | |
| 37 // A wrapper can already be collected, as we don't hold persistent | |
| 38 // handles for them to avoid memory leaks. However, we would like to try | |
| 39 // to reuse an existing wrapper as it may have properties set on the JS | |
| 40 // side. | |
| 41 GinJavaBridgeObject* object = objects_.Lookup(iter->second); | |
| 42 bool wrapper_created = false; | |
| 43 if (object) { | |
| 44 wrapper_created = GinJavaBridgeObject::InjectExisting( | |
| 45 render_frame()->GetWebFrame(), | |
| 46 object, | |
| 47 iter->first); | |
| 48 } else { | |
| 49 object = GinJavaBridgeObject::Inject(render_frame()->GetWebFrame(), | |
| 50 AsWeakPtr(), | |
| 51 iter->first, | |
| 52 iter->second); | |
| 53 if (object) { | |
| 54 wrapper_created = true; | |
| 55 objects_.AddWithID(object, iter->second); | |
| 56 } | |
| 57 } | |
| 58 if (!wrapper_created) { | |
| 59 // Inform the host about wrapper creation failure. | |
| 60 render_frame()->Send(new GinJavaBridgeHostMsg_ObjectWrapperDeleted( | |
| 61 routing_id(), iter->second)); | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void GinJavaBridgeDispatcher::OnAddNamedObject( | |
| 67 const std::string& name, | |
| 68 ObjectID object_id) { | |
| 69 // Added objects only become available after page reload, so here they | |
| 70 // are only added into the internal map. | |
| 71 named_objects_.insert(std::make_pair(name, object_id)); | |
| 72 } | |
| 73 | |
| 74 void GinJavaBridgeDispatcher::OnRemoveNamedObject(const std::string& name) { | |
| 75 // Removal becomes in effect on next reload. We simply removing the entry | |
| 76 // from the map here. | |
| 77 NamedObjectMap::iterator iter = named_objects_.find(name); | |
| 78 DCHECK(iter != named_objects_.end()); | |
| 79 named_objects_.erase(iter); | |
| 80 } | |
| 81 | |
| 82 void GinJavaBridgeDispatcher::GetJavaMethods( | |
| 83 ObjectID object_id, | |
| 84 std::set<std::string>* methods) { | |
| 85 render_frame()->Send(new GinJavaBridgeHostMsg_GetMethods( | |
| 86 routing_id(), object_id, methods)); | |
| 87 } | |
| 88 | |
| 89 bool GinJavaBridgeDispatcher::HasJavaMethod(ObjectID object_id, | |
| 90 const std::string& method_name) { | |
| 91 bool result; | |
| 92 render_frame()->Send(new GinJavaBridgeHostMsg_HasMethod( | |
| 93 routing_id(), object_id, method_name, &result)); | |
| 94 return result; | |
| 95 } | |
| 96 | |
| 97 scoped_ptr<base::Value> GinJavaBridgeDispatcher::InvokeJavaMethod( | |
| 98 ObjectID object_id, | |
| 99 const std::string& method_name, | |
| 100 const base::ListValue& arguments) { | |
| 101 base::ListValue result_wrapper; | |
| 102 render_frame()->Send( | |
| 103 new GinJavaBridgeHostMsg_InvokeMethod(routing_id(), | |
| 104 object_id, | |
| 105 method_name, | |
| 106 arguments, | |
| 107 &result_wrapper)); | |
| 108 base::Value* result; | |
| 109 if (result_wrapper.Get(0, &result)) { | |
| 110 return scoped_ptr<base::Value>(result->DeepCopy()); | |
| 111 } else { | |
| 112 return scoped_ptr<base::Value>(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 GinJavaBridgeObject* GinJavaBridgeDispatcher::GetObject(ObjectID object_id) { | |
| 117 GinJavaBridgeObject* result = objects_.Lookup(object_id); | |
| 118 if (!result) { | |
| 119 result = GinJavaBridgeObject::InjectAnonymous(AsWeakPtr(), object_id); | |
| 120 if (result) | |
| 121 objects_.AddWithID(result, object_id); | |
| 122 } | |
| 123 return result; | |
| 124 } | |
| 125 | |
| 126 void GinJavaBridgeDispatcher::OnGinJavaBridgeObjectDeleted(ObjectID object_id) { | |
| 127 if (!objects_.Lookup(object_id)) | |
| 128 return; | |
| 129 objects_.Remove(object_id); | |
| 130 render_frame()->Send( | |
| 131 new GinJavaBridgeHostMsg_ObjectWrapperDeleted(routing_id(), object_id)); | |
| 132 } | |
| 133 | |
| 134 } // namespace content | |
| OLD | NEW |