Cleanup destruction of URLRequestContextGetter (#12305)

- Add Leak detector
 - Indicate shutdown of request context from Browser Context
 - Change stored references to URLRequestContextGetter to use BrowserContext
 - Destroy session properties explicitly
This commit is contained in:
Robo 2018-03-30 18:54:55 +05:30 committed by Charles Kerr
parent fc00a2ba32
commit 171230e45d
14 changed files with 144 additions and 96 deletions

View file

@ -92,11 +92,17 @@ BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
}
BrowserContext::~BrowserContext() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
NotifyWillBeDestroyed(this);
ShutdownStoragePartitions();
BrowserThread::DeleteSoon(BrowserThread::IO,
FROM_HERE,
resource_context_.release());
if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
resource_context_.release());
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&URLRequestContextGetter::NotifyContextShutdownOnIO,
base::RetainedRef(url_request_getter_)));
}
}
void BrowserContext::InitPrefs() {

View file

@ -130,7 +130,8 @@ URLRequestContextGetter::URLRequestContextGetter(
in_memory_(in_memory),
io_task_runner_(io_task_runner),
protocol_interceptors_(std::move(protocol_interceptors)),
job_factory_(nullptr) {
job_factory_(nullptr),
context_shutting_down_(false) {
// Must first be created on the UI thread.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
@ -150,12 +151,24 @@ URLRequestContextGetter::URLRequestContextGetter(
URLRequestContextGetter::~URLRequestContextGetter() {
}
void URLRequestContextGetter::NotifyContextShutdownOnIO() {
context_shutting_down_ = true;
cookie_change_sub_.reset();
http_network_session_.reset();
http_auth_preferences_.reset();
host_mapping_rules_.reset();
url_request_context_.reset();
storage_.reset();
ct_delegate_.reset();
net::URLRequestContextGetter::NotifyContextShuttingDown();
}
void URLRequestContextGetter::OnCookieChanged(
const net::CanonicalCookie& cookie,
net::CookieStore::ChangeCause cause) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!delegate_)
if (!delegate_ || context_shutting_down_)
return;
content::BrowserThread::PostTask(
@ -172,6 +185,9 @@ net::HostResolver* URLRequestContextGetter::host_resolver() {
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (context_shutting_down_)
return nullptr;
if (!url_request_context_.get()) {
ct_delegate_.reset(new RequireCTDelegate);
auto& command_line = *base::CommandLine::ForCurrentProcess();
@ -342,14 +358,14 @@ net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
// Set up interceptors in the reverse order.
std::unique_ptr<net::URLRequestJobFactory> top_job_factory =
std::move(job_factory);
content::URLRequestInterceptorScopedVector::reverse_iterator it;
for (it = protocol_interceptors_.rbegin();
it != protocol_interceptors_.rend();
++it) {
top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
std::move(top_job_factory), std::move(*it)));
if (!protocol_interceptors_.empty()) {
for (auto it = protocol_interceptors_.rbegin();
it != protocol_interceptors_.rend(); ++it) {
top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
std::move(top_job_factory), std::move(*it)));
}
protocol_interceptors_.clear();
}
protocol_interceptors_.clear();
storage_->set_job_factory(std::move(top_job_factory));
}

View file

@ -18,6 +18,10 @@
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#if DCHECK_IS_ON()
#include "base/debug/leak_tracker.h"
#endif
namespace base {
class MessageLoop;
}
@ -83,6 +87,8 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
net::HostResolver* host_resolver();
net::URLRequestJobFactory* job_factory() const { return job_factory_; }
void NotifyContextShutdownOnIO();
private:
Delegate* delegate_;
@ -93,6 +99,10 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
std::string user_agent_;
#if DCHECK_IS_ON()
base::debug::LeakTracker<URLRequestContextGetter> leak_tracker_;
#endif
std::unique_ptr<RequireCTDelegate> ct_delegate_;
std::unique_ptr<net::ProxyConfigService> proxy_config_service_;
std::unique_ptr<net::URLRequestContextStorage> storage_;
@ -107,6 +117,8 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
net::URLRequestJobFactory* job_factory_; // weak ref
bool context_shutting_down_;
DISALLOW_COPY_AND_ASSIGN(URLRequestContextGetter);
};