electron/brightray/browser/url_request_context_getter.cc

160 lines
4.7 KiB
C++
Raw Normal View History

2013-03-13 19:12:05 +00:00
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "brightray/browser/url_request_context_getter.h"
2013-03-13 19:12:05 +00:00
#include <vector>
2017-12-18 00:46:57 +00:00
#include "base/task_scheduler/post_task.h"
#include "brightray/browser/browser_context.h"
2013-03-13 19:12:05 +00:00
#include "content/public/browser/browser_thread.h"
2014-08-13 07:09:26 +00:00
using content::BrowserThread;
2013-03-13 19:12:05 +00:00
namespace brightray {
class ResourceContext : public content::ResourceContext {
public:
ResourceContext() = default;
~ResourceContext() override = default;
net::HostResolver* GetHostResolver() override {
if (request_context_)
return request_context_->host_resolver();
return nullptr;
}
net::URLRequestContext* GetRequestContext() override {
return request_context_;
}
private:
friend class URLRequestContextGetter;
2015-01-05 21:29:16 +00:00
net::URLRequestContext* request_context_;
DISALLOW_COPY_AND_ASSIGN(ResourceContext);
};
namespace {
// For safe shutdown, must be called before
// URLRequestContextGetter::Handle is destroyed.
void NotifyContextGettersOfShutdownOnIO(
std::unique_ptr<std::vector<scoped_refptr<URLRequestContextGetter>>>
getters) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
for (auto& context_getter : *getters)
context_getter->NotifyContextShuttingDown();
}
} // namespace
2013-03-13 19:12:05 +00:00
URLRequestContextGetter::Handle::Handle(
base::WeakPtr<BrowserContext> browser_context)
: resource_context_(new ResourceContext),
browser_context_(browser_context),
initialized_(false) {}
2013-03-13 19:12:05 +00:00
URLRequestContextGetter::Handle::~Handle() {}
2016-06-22 06:52:04 +00:00
content::ResourceContext* URLRequestContextGetter::Handle::GetResourceContext()
const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
LazyInitialize();
return resource_context_.get();
2013-03-13 19:12:05 +00:00
}
scoped_refptr<URLRequestContextGetter>
URLRequestContextGetter::Handle::CreateMainRequestContextGetter(
URLRequestContextGetterFactory* factory) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!main_request_context_getter_.get());
main_request_context_getter_ =
new URLRequestContextGetter(factory, resource_context_.get());
return main_request_context_getter_;
}
scoped_refptr<URLRequestContextGetter>
URLRequestContextGetter::Handle::GetMediaRequestContextGetter() const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return main_request_context_getter_;
}
void URLRequestContextGetter::Handle::LazyInitialize() const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (initialized_)
return;
initialized_ = true;
content::BrowserContext::EnsureResourceContextInitialized(
browser_context_.get());
2013-03-13 19:12:05 +00:00
}
void URLRequestContextGetter::Handle::ShutdownOnUIThread() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto context_getters =
std::make_unique<std::vector<scoped_refptr<URLRequestContextGetter>>>();
if (media_request_context_getter_.get())
context_getters->push_back(media_request_context_getter_);
if (main_request_context_getter_.get())
context_getters->push_back(main_request_context_getter_);
if (!context_getters->empty()) {
if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) {
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&NotifyContextGettersOfShutdownOnIO,
std::move(context_getters)));
2015-07-10 08:22:02 +00:00
}
}
if (!BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this))
delete this;
}
URLRequestContextGetter::URLRequestContextGetter(
URLRequestContextGetterFactory* factory,
ResourceContext* resource_context)
: factory_(factory),
resource_context_(resource_context),
url_request_context_(nullptr),
context_shutting_down_(false) {
// Must first be created on the UI thread.
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
URLRequestContextGetter::~URLRequestContextGetter() {
DCHECK(!factory_.get());
DCHECK(!url_request_context_);
}
2014-08-15 04:30:50 +00:00
void URLRequestContextGetter::NotifyContextShuttingDown() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
2013-03-13 19:12:05 +00:00
context_shutting_down_ = true;
url_request_context_ = nullptr;
net::URLRequestContextGetter::NotifyContextShuttingDown();
factory_.reset();
}
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
2016-03-10 05:39:07 +00:00
if (factory_.get() && !url_request_context_ && !context_shutting_down_) {
url_request_context_ = factory_->Create();
if (resource_context_) {
resource_context_->request_context_ = url_request_context_;
}
2013-03-13 19:12:05 +00:00
}
return url_request_context_;
2013-03-13 19:12:05 +00:00
}
scoped_refptr<base::SingleThreadTaskRunner>
URLRequestContextGetter::GetNetworkTaskRunner() const {
return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
2013-03-13 19:12:05 +00:00
}
} // namespace brightray