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() { |
| 35 for (NamedObjectMap::const_iterator iter = named_objects_.begin(); |
| 36 iter != named_objects_.end(); ++iter) { |
| 37 // Always create a new GinJavaBridgeObject, so we don't pull any of the V8 |
| 38 // wrapper's custom properties into the context of the page we have |
| 39 // navigated to. The old GinJavaBridgeObject will be automatically |
| 40 // deleted after its wrapper will be collected. |
| 41 // On the browser side, we ignore wrapper deletion events for named objects, |
| 42 // as they are only removed upon embedder's request (RemoveNamedObject). |
| 43 if (objects_.Lookup(iter->second)) |
| 44 objects_.Remove(iter->second); |
| 45 GinJavaBridgeObject* object = GinJavaBridgeObject::InjectNamed( |
| 46 render_frame()->GetWebFrame(), AsWeakPtr(), iter->first, iter->second); |
| 47 if (object) { |
| 48 objects_.AddWithID(object, iter->second); |
| 49 } else { |
| 50 // Inform the host about wrapper creation failure. |
| 51 render_frame()->Send(new GinJavaBridgeHostMsg_ObjectWrapperDeleted( |
| 52 routing_id(), iter->second)); |
| 53 } |
| 54 } |
| 55 } |
| 56 |
| 57 void GinJavaBridgeDispatcher::OnAddNamedObject( |
| 58 const std::string& name, |
| 59 ObjectID object_id) { |
| 60 // Added objects only become available after page reload, so here they |
| 61 // are only added into the internal map. |
| 62 named_objects_.insert(std::make_pair(name, object_id)); |
| 63 } |
| 64 |
| 65 void GinJavaBridgeDispatcher::OnRemoveNamedObject(const std::string& name) { |
| 66 // Removal becomes in effect on next reload. We simply removing the entry |
| 67 // from the map here. |
| 68 NamedObjectMap::iterator iter = named_objects_.find(name); |
| 69 DCHECK(iter != named_objects_.end()); |
| 70 named_objects_.erase(iter); |
| 71 } |
| 72 |
| 73 void GinJavaBridgeDispatcher::GetJavaMethods( |
| 74 ObjectID object_id, |
| 75 std::set<std::string>* methods) { |
| 76 render_frame()->Send(new GinJavaBridgeHostMsg_GetMethods( |
| 77 routing_id(), object_id, methods)); |
| 78 } |
| 79 |
| 80 bool GinJavaBridgeDispatcher::HasJavaMethod(ObjectID object_id, |
| 81 const std::string& method_name) { |
| 82 bool result; |
| 83 render_frame()->Send(new GinJavaBridgeHostMsg_HasMethod( |
| 84 routing_id(), object_id, method_name, &result)); |
| 85 return result; |
| 86 } |
| 87 |
| 88 scoped_ptr<base::Value> GinJavaBridgeDispatcher::InvokeJavaMethod( |
| 89 ObjectID object_id, |
| 90 const std::string& method_name, |
| 91 const base::ListValue& arguments) { |
| 92 base::ListValue result_wrapper; |
| 93 render_frame()->Send( |
| 94 new GinJavaBridgeHostMsg_InvokeMethod(routing_id(), |
| 95 object_id, |
| 96 method_name, |
| 97 arguments, |
| 98 &result_wrapper)); |
| 99 base::Value* result; |
| 100 if (result_wrapper.Get(0, &result)) { |
| 101 return scoped_ptr<base::Value>(result->DeepCopy()); |
| 102 } else { |
| 103 return scoped_ptr<base::Value>(); |
| 104 } |
| 105 } |
| 106 |
| 107 GinJavaBridgeObject* GinJavaBridgeDispatcher::GetObject(ObjectID object_id) { |
| 108 GinJavaBridgeObject* result = objects_.Lookup(object_id); |
| 109 if (!result) { |
| 110 result = GinJavaBridgeObject::InjectAnonymous(AsWeakPtr(), object_id); |
| 111 if (result) |
| 112 objects_.AddWithID(result, object_id); |
| 113 } |
| 114 return result; |
| 115 } |
| 116 |
| 117 void GinJavaBridgeDispatcher::OnGinJavaBridgeObjectDeleted(ObjectID object_id) { |
| 118 if (!objects_.Lookup(object_id)) |
| 119 return; |
| 120 objects_.Remove(object_id); |
| 121 render_frame()->Send( |
| 122 new GinJavaBridgeHostMsg_ObjectWrapperDeleted(routing_id(), object_id)); |
| 123 } |
| 124 |
| 125 } // namespace content |
OLD | NEW |