Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(741)

Side by Side Diff: content/browser/renderer_host/java/java_bridge_dispatcher_host.cc

Issue 11359143: Make sure Java Bridge cleans up all stub object it creates. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/renderer_host/java/java_bridge_dispatcher_host.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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(std::vector<base::WeakPtr<NPObjectStub> > stubs) {
34 for (size_t i = 0; i < stubs.size(); ++i) {
35 if (stubs[i]) {
36 stubs[i]->DeleteSoon();
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_));
joth 2012/11/15 23:29:07 under the covers, Bind copy-constructs a copy of a
acleung 2012/11/16 02:07:18 Checked the docs. You are absolutely right.
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
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
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
OLDNEW
« no previous file with comments | « content/browser/renderer_host/java/java_bridge_dispatcher_host.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698