OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/renderer_host/java/java_bridge_dispatcher_host.h" | 5 #include "content/browser/renderer_host/java/java_bridge_dispatcher_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "content/browser/renderer_host/java/java_bridge_channel_host.h" | 10 #include "content/browser/renderer_host/java/java_bridge_channel_host.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 LAZY_INSTANCE_INITIALIZER; | 33 LAZY_INSTANCE_INITIALIZER; |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( | 36 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( |
37 RenderViewHost* render_view_host) | 37 RenderViewHost* render_view_host) |
38 : RenderViewHostObserver(render_view_host), | 38 : RenderViewHostObserver(render_view_host), |
39 is_renderer_initialized_(false) { | 39 is_renderer_initialized_(false) { |
40 } | 40 } |
41 | 41 |
42 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { | 42 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { |
43 g_background_thread.Get().message_loop()->PostTask( | |
44 FROM_HERE, | |
45 base::Bind(&JavaBridgeDispatcherHost::CleanUpStubs, stubs_)); | |
43 } | 46 } |
44 | 47 |
45 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, | 48 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, |
46 NPObject* object) { | 49 NPObject* object) { |
47 NPVariant_Param variant_param; | 50 NPVariant_Param variant_param; |
48 CreateNPVariantParam(object, &variant_param); | 51 CreateNPVariantParam(object, &variant_param); |
49 | 52 |
50 if (!is_renderer_initialized_) { | 53 if (!is_renderer_initialized_) { |
51 is_renderer_initialized_ = true; | 54 is_renderer_initialized_ = true; |
52 Send(new JavaBridgeMsg_Init(routing_id())); | 55 Send(new JavaBridgeMsg_Init(routing_id())); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 | 129 |
127 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, | 130 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, |
128 int route_id) { | 131 int route_id) { |
129 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); | 132 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); |
130 if (!channel_) { | 133 if (!channel_) { |
131 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( | 134 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( |
132 render_view_host()->GetProcess()->GetID(), | 135 render_view_host()->GetProcess()->GetID(), |
133 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 136 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
134 } | 137 } |
135 | 138 |
136 // NPObjectStub takes a ref to the NPObject. The lifetime of the NPObjectStub | 139 // In a typical scenario, the lifetime of each NPObjectStub is governed by |
137 // is governed by that of the NPObjectProxy in the renderer, via the channel. | 140 // that of the NPObjectProxy in the renderer, via the channel. However, |
141 // we cannot guaranteed that the renderer always terminates cleanly | |
142 // (crashes / sometimes just unavoidable). We keep a weak reference to | |
143 // it now and schedule a delete on it when this host is getting deleted. | |
144 | |
138 // Pass 0 for the containing window, as it's only used by plugins to pump the | 145 // Pass 0 for the containing window, as it's only used by plugins to pump the |
139 // window message queue when a method on a renderer-side object causes a | 146 // window message queue when a method on a renderer-side object causes a |
140 // dialog to be displayed, and the Java Bridge does not need this | 147 // dialog to be displayed, and the Java Bridge does not need this |
141 // functionality. The page URL is also not required. | 148 // functionality. The page URL is also not required. |
142 new NPObjectStub(object, channel_, route_id, 0, GURL()); | 149 new NPObjectStub(object, channel_, route_id, 0, GURL()); |
150 stubs_.push_back( | |
151 (new NPObjectStub(object, channel_, route_id, 0, GURL()))->AsWeakPtr()); | |
152 | |
143 // The NPObjectStub takes a reference to the NPObject. Release the ref added | 153 // The NPObjectStub takes a reference to the NPObject. Release the ref added |
144 // in CreateNPVariantParam(). | 154 // in CreateNPVariantParam(). |
145 WebKit::WebBindings::releaseObject(object); | 155 WebKit::WebBindings::releaseObject(object); |
146 } | 156 } |
147 | 157 |
158 void JavaBridgeDispatcherHost::CleanUpStubs( | |
159 std::vector<base::WeakPtr<NPObjectStub> > stubs) { | |
darin (slow to review)
2012/11/15 21:08:59
are you sure you want to pass by value here?
acleung
2012/11/15 22:39:40
I thought about that a bit. If I pass by reference
| |
160 for (size_t i = 0; i < stubs.size(); ++i) { | |
161 if (stubs[i]) { | |
darin (slow to review)
2012/11/15 21:08:59
nit: indentation
acleung
2012/11/15 22:39:40
Done.
| |
162 stubs[i]->DeleteSoon(); | |
163 } | |
164 } | |
165 } | |
166 | |
148 } // namespace content | 167 } // namespace content |
OLD | NEW |