fix: notify request context shutdown on IO before cleanup (#14092)

This commit is contained in:
trop[bot] 2018-08-22 08:24:56 -07:00 committed by Shelley Vohr
parent d1886c5d22
commit 4c7af6a429
22 changed files with 504 additions and 387 deletions

View file

@ -11,7 +11,6 @@
#include "brightray/browser/brightray_paths.h"
#include "brightray/browser/browser_client.h"
#include "brightray/browser/inspectable_web_contents_impl.h"
#include "brightray/browser/network_delegate.h"
#include "brightray/browser/special_storage_policy.h"
#include "brightray/browser/zoom_level_delegate.h"
#include "brightray/common/application_info.h"
@ -20,7 +19,6 @@
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_service_factory.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/escape.h"
@ -37,25 +35,10 @@ std::string MakePartitionName(const std::string& input) {
} // namespace
class BrowserContext::ResourceContext : public content::ResourceContext {
public:
ResourceContext() : getter_(nullptr) {}
void set_url_request_context_getter(URLRequestContextGetter* getter) {
getter_ = getter;
}
private:
net::HostResolver* GetHostResolver() override {
return getter_->host_resolver();
}
net::URLRequestContext* GetRequestContext() override {
return getter_->GetURLRequestContext();
}
URLRequestContextGetter* getter_;
};
// static
void BrowserContextDeleter::Destruct(const BrowserContext* browser_context) {
browser_context->OnDestruct();
}
// static
BrowserContext::BrowserContextMap BrowserContext::browser_context_map_;
@ -72,7 +55,6 @@ scoped_refptr<BrowserContext> BrowserContext::Get(const std::string& partition,
BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
: in_memory_(in_memory),
resource_context_(new ResourceContext),
storage_policy_(new SpecialStoragePolicy),
weak_factory_(this) {
if (!PathService::Get(DIR_USER_DATA, &path_)) {
@ -88,6 +70,8 @@ BrowserContext::BrowserContext(const std::string& partition, bool in_memory)
content::BrowserContext::Initialize(this, path_);
io_handle_ = new URLRequestContextGetter::Handle(GetWeakPtr());
browser_context_map_[PartitionKey(partition, in_memory)] = GetWeakPtr();
}
@ -95,13 +79,14 @@ BrowserContext::~BrowserContext() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
NotifyWillBeDestroyed(this);
ShutdownStoragePartitions();
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_)));
io_handle_->ShutdownOnUIThread();
}
void BrowserContext::OnDestruct() const {
if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
delete this;
} else {
BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this);
}
}
@ -135,17 +120,10 @@ URLRequestContextGetter* BrowserContext::GetRequestContext() {
net::URLRequestContextGetter* BrowserContext::CreateRequestContext(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector protocol_interceptors) {
DCHECK(!url_request_getter_.get());
url_request_getter_ = new URLRequestContextGetter(
this, static_cast<NetLog*>(BrowserClient::Get()->GetNetLog()), GetPath(),
in_memory_, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
protocol_handlers, std::move(protocol_interceptors));
resource_context_->set_url_request_context_getter(url_request_getter_.get());
return url_request_getter_.get();
}
std::unique_ptr<net::NetworkDelegate> BrowserContext::CreateNetworkDelegate() {
return std::make_unique<NetworkDelegate>();
return io_handle_
->CreateMainRequestContextGetter(protocol_handlers,
std::move(protocol_interceptors))
.get();
}
std::string BrowserContext::GetMediaDeviceIDSalt() {
@ -171,7 +149,7 @@ bool BrowserContext::IsOffTheRecord() const {
}
content::ResourceContext* BrowserContext::GetResourceContext() {
return resource_context_.get();
return io_handle_->GetResourceContext();
}
content::DownloadManagerDelegate* BrowserContext::GetDownloadManagerDelegate() {
@ -214,17 +192,19 @@ BrowserContext::CreateRequestContextForStoragePartition(
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
NOTREACHED();
return nullptr;
}
net::URLRequestContextGetter* BrowserContext::CreateMediaRequestContext() {
return url_request_getter_.get();
return io_handle_->GetMainRequestContextGetter().get();
}
net::URLRequestContextGetter*
BrowserContext::CreateMediaRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory) {
NOTREACHED();
return nullptr;
}