Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/java/java_bridge_dispatcher.h" | 5 #include "content/renderer/java/java_bridge_dispatcher.h" |
| 6 | 6 |
| 7 #include "content/common/child_process.h" | 7 #include "content/common/child_process.h" |
| 8 #include "content/common/java_bridge_messages.h" | 8 #include "content/common/java_bridge_messages.h" |
| 9 #include "content/common/npobject_util.h" // For CreateNPVariant() | 9 #include "content/common/npobject_util.h" // For CreateNPVariant() |
| 10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
| 11 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
| 12 #include "content/renderer/java/java_bridge_channel.h" | 12 #include "content/renderer/java/java_bridge_channel.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 17 | 17 |
| 18 JavaBridgeDispatcher::JavaBridgeDispatcher( | 18 JavaBridgeDispatcher::JavaBridgeDispatcher( |
| 19 content::RenderView* render_view, | 19 content::RenderView* render_view) |
| 20 const IPC::ChannelHandle& channel_handle) | |
| 21 : RenderViewObserver(render_view) { | 20 : RenderViewObserver(render_view) { |
| 21 } | |
| 22 | |
| 23 void JavaBridgeDispatcher::EnsureChannelIsSetUp() { | |
| 24 if (channel_.get()) { | |
| 25 return; | |
| 26 } | |
| 27 | |
| 28 IPC::ChannelHandle channel_handle; | |
| 29 render_view()->Send(new JavaBridgeMsg_GetChannelHandle( | |
|
jam
2011/12/07 22:07:03
nit: "render_view()->" isn't needed, and render_vi
Steve Block
2011/12/08 11:17:56
Done.
| |
| 30 render_view()->GetRoutingId(), | |
| 31 &channel_handle)); | |
| 32 | |
| 22 channel_.reset(JavaBridgeChannel::GetJavaBridgeChannel( | 33 channel_.reset(JavaBridgeChannel::GetJavaBridgeChannel( |
| 23 channel_handle, ChildProcess::current()->io_message_loop_proxy())); | 34 channel_handle, ChildProcess::current()->io_message_loop_proxy())); |
| 24 } | 35 } |
| 25 | 36 |
| 26 JavaBridgeDispatcher::~JavaBridgeDispatcher() { | 37 JavaBridgeDispatcher::~JavaBridgeDispatcher() { |
| 27 for (ObjectMap::const_iterator iter = objects_.begin(); | 38 for (ObjectMap::const_iterator iter = objects_.begin(); |
| 28 iter != objects_.end(); ++iter) { | 39 iter != objects_.end(); ++iter) { |
| 29 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); | 40 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); |
| 30 } | 41 } |
| 31 } | 42 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 55 // deleted at any time after OnRemoveNamedObject() is called. | 66 // deleted at any time after OnRemoveNamedObject() is called. |
| 56 web_frame->bindToWindowObject(iter->first, | 67 web_frame->bindToWindowObject(iter->first, |
| 57 NPVARIANT_TO_OBJECT(iter->second)); | 68 NPVARIANT_TO_OBJECT(iter->second)); |
| 58 } | 69 } |
| 59 } | 70 } |
| 60 | 71 |
| 61 void JavaBridgeDispatcher::OnAddNamedObject( | 72 void JavaBridgeDispatcher::OnAddNamedObject( |
| 62 const string16& name, | 73 const string16& name, |
| 63 const NPVariant_Param& variant_param) { | 74 const NPVariant_Param& variant_param) { |
| 64 DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID); | 75 DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID); |
| 76 | |
| 77 EnsureChannelIsSetUp(); | |
| 78 | |
| 65 // This creates an NPObject, wrapped as an NPVariant. We don't need the | 79 // This creates an NPObject, wrapped as an NPVariant. We don't need the |
| 66 // containing window or the page URL, as we don't do re-entrant sync IPC. | 80 // containing window or the page URL, as we don't do re-entrant sync IPC. |
| 67 NPVariant variant; | 81 NPVariant variant; |
| 68 bool created = | 82 bool created = |
| 69 CreateNPVariant(variant_param, channel_.get(), &variant, 0, GURL()); | 83 CreateNPVariant(variant_param, channel_.get(), &variant, 0, GURL()); |
| 70 DCHECK(created); | 84 DCHECK(created); |
| 71 DCHECK_EQ(variant.type, NPVariantType_Object); | 85 DCHECK_EQ(variant.type, NPVariantType_Object); |
| 72 | 86 |
| 73 // The NPObject is created with a ref count of one, which we remove when | 87 // The NPObject is created with a ref count of one, which we remove when |
| 74 // OnRemoveNamedObject() is called for that object. | 88 // OnRemoveNamedObject() is called for that object. |
| 75 ObjectMap::iterator iter = objects_.find(name); | 89 ObjectMap::iterator iter = objects_.find(name); |
| 76 if (iter != objects_.end()) { | 90 if (iter != objects_.end()) { |
| 77 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); | 91 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); |
| 78 } | 92 } |
| 79 objects_[name] = variant; | 93 objects_[name] = variant; |
| 80 } | 94 } |
| 81 | 95 |
| 82 void JavaBridgeDispatcher::OnRemoveNamedObject(const string16& name) { | 96 void JavaBridgeDispatcher::OnRemoveNamedObject(const string16& name) { |
| 83 // Removing an object does not unbind it from JavaScript until the window | 97 // Removing an object does not unbind it from JavaScript until the window |
| 84 // object is next cleared. Note that the browser checks that the named object | 98 // object is next cleared. Note that the browser checks that the named object |
| 85 // is present. | 99 // is present. |
| 86 ObjectMap::iterator iter = objects_.find(name); | 100 ObjectMap::iterator iter = objects_.find(name); |
| 87 DCHECK(iter != objects_.end()); | 101 DCHECK(iter != objects_.end()); |
| 88 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); | 102 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); |
| 89 objects_.erase(iter); | 103 objects_.erase(iter); |
| 90 } | 104 } |
| OLD | NEW |