Commit d68b9a12 authored by acleung@google.com's avatar acleung@google.com

Make sure Java Bridge cleans up all stub object it creates.

I have been trying to see why the JavaBridgeChannelHost, which is reference counted, sticks around forever.

Here is what I found:

When talk back is enabled. Each JavaBridgeChannelHost typically have 5 references to it. 3 of them should be cleaned up by this patch. (see comments in the .cc file)

BUG=157699


Review URL: https://chromiumcodereview.appspot.com/11359143

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170694 0039d316-1c4b-4281-b951-d872f2087c98
parent 7670ccaa
...@@ -29,6 +29,15 @@ class JavaBridgeThread : public base::Thread { ...@@ -29,6 +29,15 @@ class JavaBridgeThread : public base::Thread {
Stop(); Stop();
} }
}; };
void CleanUpStubs(const std::vector<base::WeakPtr<NPObjectStub> > & stubs) {
for (size_t i = 0; i < stubs.size(); ++i) {
if (stubs[i]) {
stubs[i]->DeleteSoon();
}
}
}
base::LazyInstance<JavaBridgeThread> g_background_thread = base::LazyInstance<JavaBridgeThread> g_background_thread =
LAZY_INSTANCE_INITIALIZER; LAZY_INSTANCE_INITIALIZER;
} // namespace } // namespace
...@@ -40,6 +49,9 @@ JavaBridgeDispatcherHost::JavaBridgeDispatcherHost( ...@@ -40,6 +49,9 @@ JavaBridgeDispatcherHost::JavaBridgeDispatcherHost(
} }
JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() { JavaBridgeDispatcherHost::~JavaBridgeDispatcherHost() {
g_background_thread.Get().message_loop()->PostTask(
FROM_HERE,
base::Bind(&CleanUpStubs, stubs_));
} }
void JavaBridgeDispatcherHost::AddNamedObject(const string16& name, void JavaBridgeDispatcherHost::AddNamedObject(const string16& name,
...@@ -133,13 +145,19 @@ void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object, ...@@ -133,13 +145,19 @@ void JavaBridgeDispatcherHost::CreateObjectStub(NPObject* object,
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
} }
// NPObjectStub takes a ref to the NPObject. The lifetime of the NPObjectStub // In a typical scenario, the lifetime of each NPObjectStub is governed by
// is governed by that of the NPObjectProxy in the renderer, via the channel. // that of the NPObjectProxy in the renderer, via the channel. However,
// we cannot guaranteed that the renderer always terminates cleanly
// (crashes / sometimes just unavoidable). We keep a weak reference to
// it now and schedule a delete on it when this host is getting deleted.
// Pass 0 for the containing window, as it's only used by plugins to pump the // Pass 0 for the containing window, as it's only used by plugins to pump the
// window message queue when a method on a renderer-side object causes a // window message queue when a method on a renderer-side object causes a
// dialog to be displayed, and the Java Bridge does not need this // dialog to be displayed, and the Java Bridge does not need this
// functionality. The page URL is also not required. // functionality. The page URL is also not required.
new NPObjectStub(object, channel_, route_id, 0, GURL()); stubs_.push_back(
(new NPObjectStub(object, channel_, route_id, 0, GURL()))->AsWeakPtr());
// The NPObjectStub takes a reference to the NPObject. Release the ref added // The NPObjectStub takes a reference to the NPObject. Release the ref added
// in CreateNPVariantParam(). // in CreateNPVariantParam().
WebKit::WebBindings::releaseObject(object); WebKit::WebBindings::releaseObject(object);
......
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
#ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_H_ #ifndef CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_H_
#define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_H_ #define CONTENT_BROWSER_RENDERER_HOST_JAVA_JAVA_BRIDGE_DISPATCHER_HOST_H_
#include <vector>
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/string16.h" #include "base/string16.h"
#include "content/common/npobject_stub.h"
#include "content/public/browser/render_view_host_observer.h" #include "content/public/browser/render_view_host_observer.h"
class RouteIDGenerator; class RouteIDGenerator;
...@@ -62,6 +65,7 @@ class JavaBridgeDispatcherHost ...@@ -62,6 +65,7 @@ class JavaBridgeDispatcherHost
scoped_refptr<NPChannelBase> channel_; scoped_refptr<NPChannelBase> channel_;
bool is_renderer_initialized_; bool is_renderer_initialized_;
std::vector<base::WeakPtr<NPObjectStub> > stubs_;
DISALLOW_COPY_AND_ASSIGN(JavaBridgeDispatcherHost); DISALLOW_COPY_AND_ASSIGN(JavaBridgeDispatcherHost);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment