OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/java_bridge_dispatcher.h" | |
6 | |
7 #include "content/common/child_process.h" | |
8 #include "content/common/java_bridge_messages.h" | |
9 #include "content/common/npobject_util.h" // For CreateNPVariant() | |
10 #include "content/public/renderer/render_thread.h" | |
11 #include "content/public/renderer/render_view.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
16 | |
17 JavaBridgeDispatcher::JavaBridgeDispatcher( | |
18 content::RenderView* render_view, | |
19 const IPC::ChannelHandle& channel_handle) | |
20 : RenderViewObserver(render_view) { | |
21 channel_.reset(JavaBridgeChannel::GetJavaBridgeChannel( | |
22 channel_handle, ChildProcess::current()->io_message_loop_proxy())); | |
23 } | |
24 | |
25 JavaBridgeDispatcher::~JavaBridgeDispatcher() { | |
26 for (ObjectMap::const_iterator iter = objects_.begin(); | |
27 iter != objects_.end(); ++iter) { | |
28 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); | |
29 } | |
30 } | |
31 | |
32 bool JavaBridgeDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
33 bool handled = true; | |
34 IPC_BEGIN_MESSAGE_MAP(JavaBridgeDispatcher, msg) | |
35 IPC_MESSAGE_HANDLER(JavaBridgeMsg_AddNamedObject, OnAddNamedObject) | |
36 IPC_MESSAGE_HANDLER(JavaBridgeMsg_RemoveNamedObject, | |
37 OnRemoveNamedObject) | |
38 IPC_MESSAGE_UNHANDLED(handled = false) | |
39 IPC_END_MESSAGE_MAP() | |
40 return handled; | |
41 } | |
42 | |
43 void JavaBridgeDispatcher::DidClearWindowObject(WebKit::WebFrame* web_frame) { | |
44 DCHECK_EQ(content::RenderThread::Get()->GetMessageLoop(), | |
jam
2011/10/19 23:46:19
(and below)
you don't need to test that RVO inter
Steve Block
2011/10/20 09:43:08
Done.
| |
45 MessageLoop::current()); | |
46 // We only inject objects into the main frame. | |
47 if (web_frame != render_view()->GetWebView()->mainFrame()) | |
48 return; | |
49 | |
50 // Note that we have to (re)bind all objects, as they will have been unbound | |
51 // when the window object was cleared. | |
52 for (ObjectMap::const_iterator iter = objects_.begin(); | |
53 iter != objects_.end(); ++iter) { | |
54 // This refs the NPObject. This reference is dropped when either the window | |
55 // object is later cleared, or the object is GC'ed. So the object may be | |
56 // deleted at any time after OnRemoveNamedObject() is called. | |
57 web_frame->bindToWindowObject(iter->first, | |
58 NPVARIANT_TO_OBJECT(iter->second)); | |
59 } | |
60 } | |
61 | |
62 void JavaBridgeDispatcher::OnAddNamedObject( | |
63 const string16& name, | |
64 const NPVariant_Param& variant_param) { | |
65 DCHECK_EQ(content::RenderThread::Get()->GetMessageLoop(), | |
66 MessageLoop::current()); | |
67 DCHECK_EQ(variant_param.type, NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID); | |
68 // This creates an NPObject, wrapped as an NPVariant. We don't need the | |
69 // containing window or the page URL, as we don't do re-entrant sync IPC. | |
70 NPVariant variant; | |
71 bool created = | |
72 CreateNPVariant(variant_param, channel_.get(), &variant, NULL, GURL()); | |
73 DCHECK(created); | |
74 DCHECK_EQ(variant.type, NPVariantType_Object); | |
75 | |
76 // The NPObject is created with a ref count of one, which we remove when | |
77 // OnRemoveNamedObject() is called for that object. | |
78 ObjectMap::iterator iter = objects_.find(name); | |
79 if (iter != objects_.end()) { | |
80 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); | |
81 } | |
82 objects_[name] = variant; | |
83 } | |
84 | |
85 void JavaBridgeDispatcher::OnRemoveNamedObject(const string16& name) { | |
86 DCHECK_EQ(content::RenderThread::Get()->GetMessageLoop(), | |
jam
2011/10/19 23:46:19
ditto: this isn't needed
Steve Block
2011/10/20 09:43:08
Done.
| |
87 MessageLoop::current()); | |
88 // Removing an object does not unbind it from JavaScript until the window | |
89 // object is next cleared. Note that the browser checks that the named object | |
90 // is present. | |
91 ObjectMap::iterator iter = objects_.find(name); | |
92 DCHECK(iter != objects_.end()); | |
93 WebKit::WebBindings::releaseObject(NPVARIANT_TO_OBJECT(iter->second)); | |
94 objects_.erase(iter); | |
95 } | |
OLD | NEW |