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 11 matching lines...) Expand all Loading... | |
22 namespace { | 22 namespace { |
23 class JavaBridgeThread : public base::Thread { | 23 class JavaBridgeThread : public base::Thread { |
24 public: | 24 public: |
25 JavaBridgeThread() : base::Thread("JavaBridge") { | 25 JavaBridgeThread() : base::Thread("JavaBridge") { |
26 Start(); | 26 Start(); |
27 } | 27 } |
28 virtual ~JavaBridgeThread() { | 28 virtual ~JavaBridgeThread() { |
29 Stop(); | 29 Stop(); |
30 } | 30 } |
31 }; | 31 }; |
32 | |
33 void CleanUpStubs(const std::vector<base::WeakPtr<NPObjectStub> > & stubs) { | |
34 for (size_t i = 0; i < stubs.size(); ++i) { | |
35 if (stubs[i]) { | |
36 stubs[i]->DeleteSoon(); | |
Steve Block
2012/11/27 05:04:33
Is there any possibility that this deletion will r
acleung
2012/11/27 07:48:33
Hmm. The dispatcher gets deleted when the renderer
| |
37 } | |
38 } | |
39 } | |
40 | |
32 base::LazyInstance<JavaBridgeThread> g_background_thread = | 41 base::LazyInstance<JavaBridgeThread> g_background_thread = |
33 LAZY_INSTANCE_INITIALIZER; | 42 LAZY_INSTANCE_INITIALIZER; |
34 } // namespace | 43 } // namespace |
35 | 44 |
36 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( | 45 JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( |
37 RenderViewHost* render_view_host) | 46 RenderViewHost* render_view_host) |
38 : RenderViewHostObserver(render_view_host), | 47 : RenderViewHostObserver(render_view_host), |
39 is_renderer_initialized_(false) { | 48 is_renderer_initialized_(false) { |
40 } | 49 } |
41 | 50 |
42 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { | 51 JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { |
52 g_background_thread.Get().message_loop()->PostTask( | |
53 FROM_HERE, | |
54 base::Bind(&CleanUpStubs, stubs_)); | |
43 } | 55 } |
44 | 56 |
45 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, | 57 void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, |
46 NPObject* object) { | 58 NPObject* object) { |
47 NPVariant_Param variant_param; | 59 NPVariant_Param variant_param; |
48 CreateNPVariantParam(object, &variant_param); | 60 CreateNPVariantParam(object, &variant_param); |
49 | 61 |
50 if (!is_renderer_initialized_) { | 62 if (!is_renderer_initialized_) { |
51 is_renderer_initialized_ = true; | 63 is_renderer_initialized_ = true; |
52 Send(new JavaBridgeMsg_Init(routing_id())); | 64 Send(new JavaBridgeMsg_Init(routing_id())); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 | 138 |
127 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, | 139 void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, |
128 int route_id) { | 140 int route_id) { |
129 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); | 141 DCHECK_EQ(g_background_thread.Get().message_loop(), MessageLoop::current()); |
130 if (!channel_) { | 142 if (!channel_) { |
131 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( | 143 channel_ = JavaBridgeChannelHost::GetJavaBridgeChannelHost( |
132 render_view_host()->GetProcess()->GetID(), | 144 render_view_host()->GetProcess()->GetID(), |
133 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | 145 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
134 } | 146 } |
135 | 147 |
136 // NPObjectStub takes a ref to the NPObject. The lifetime of the NPObjectStub | 148 // 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. | 149 // that of the NPObjectProxy in the renderer, via the channel. However, |
150 // we cannot guaranteed that the renderer always terminates cleanly | |
Steve Block
2012/11/27 05:04:33
s/guaranteed/guarantee
acleung
2012/11/27 07:48:33
Done.
| |
151 // (crashes / sometimes just unavoidable). We keep a weak reference to | |
152 // it now and schedule a delete on it when this host is getting deleted. | |
153 | |
138 // Pass 0 for the containing window, as it's only used by plugins to pump the | 154 // 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 | 155 // 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 | 156 // dialog to be displayed, and the Java Bridge does not need this |
141 // functionality. The page URL is also not required. | 157 // functionality. The page URL is also not required. |
142 new NPObjectStub(object, channel_, route_id, 0, GURL()); | 158 new NPObjectStub(object, channel_, route_id, 0, GURL()); |
159 stubs_.push_back( | |
160 (new NPObjectStub(object, channel_, route_id, 0, GURL()))->AsWeakPtr()); | |
161 | |
143 // The NPObjectStub takes a reference to the NPObject. Release the ref added | 162 // The NPObjectStub takes a reference to the NPObject. Release the ref added |
144 // in CreateNPVariantParam(). | 163 // in CreateNPVariantParam(). |
145 WebKit::WebBindings::releaseObject(object); | 164 WebKit::WebBindings::releaseObject(object); |
146 } | 165 } |
147 | 166 |
148 } // namespace content | 167 } // namespace content |
OLD | NEW |