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.
|
|
|
|
|
2017-05-18 22:58:12 +00:00
|
|
|
#include "brightray/browser/url_request_context_getter.h"
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
#include <vector>
|
2013-11-17 23:11:47 +00:00
|
|
|
|
2017-12-18 00:46:57 +00:00
|
|
|
#include "base/task_scheduler/post_task.h"
|
2018-08-13 22:22:45 +00:00
|
|
|
#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 {
|
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
class ResourceContext : public content::ResourceContext {
|
|
|
|
public:
|
|
|
|
ResourceContext() = default;
|
|
|
|
~ResourceContext() override = default;
|
2018-04-17 23:47:47 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
net::HostResolver* GetHostResolver() override {
|
|
|
|
if (request_context_)
|
|
|
|
return request_context_->host_resolver();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-20 06:48:02 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
net::URLRequestContext* GetRequestContext() override {
|
|
|
|
return request_context_;
|
2016-06-15 11:30:26 +00:00
|
|
|
}
|
2014-08-20 06:48:02 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
private:
|
|
|
|
friend class URLRequestContextGetter;
|
2015-01-05 21:29:16 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
net::URLRequestContext* request_context_;
|
2015-11-05 15:26:46 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(ResourceContext);
|
|
|
|
};
|
2015-09-21 16:44:32 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
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();
|
2016-08-05 07:23:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
} // namespace
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2018-08-13 22:22:45 +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
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
URLRequestContextGetter::Handle::~Handle() {}
|
2016-06-22 06:52:04 +00:00
|
|
|
|
2018-08-13 22:22:45 +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
|
|
|
}
|
|
|
|
|
2018-08-13 22:22:45 +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_;
|
2018-03-30 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
scoped_refptr<URLRequestContextGetter>
|
|
|
|
URLRequestContextGetter::Handle::GetMediaRequestContextGetter() const {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
return main_request_context_getter_;
|
|
|
|
}
|
2017-11-27 03:27:14 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
void URLRequestContextGetter::Handle::LazyInitialize() const {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
if (initialized_)
|
2017-11-28 07:23:42 +00:00
|
|
|
return;
|
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
initialized_ = true;
|
|
|
|
content::BrowserContext::EnsureResourceContextInitialized(
|
|
|
|
browser_context_.get());
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:22:45 +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
|
|
|
}
|
2018-08-13 22:22:45 +00:00
|
|
|
}
|
2015-05-04 22:35:25 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
if (!BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this))
|
|
|
|
delete this;
|
|
|
|
}
|
2016-05-21 18:10:02 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-05-21 18:10:02 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
URLRequestContextGetter::~URLRequestContextGetter() {
|
|
|
|
DCHECK(!factory_.get());
|
|
|
|
DCHECK(!url_request_context_);
|
|
|
|
}
|
2014-08-15 04:30:50 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
void URLRequestContextGetter::NotifyContextShuttingDown() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2013-03-13 19:12:05 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
context_shutting_down_ = true;
|
|
|
|
url_request_context_ = nullptr;
|
|
|
|
net::URLRequestContextGetter::NotifyContextShuttingDown();
|
|
|
|
factory_.reset();
|
|
|
|
}
|
2017-11-02 14:05:30 +00:00
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
net::URLRequestContext* URLRequestContextGetter::GetURLRequestContext() {
|
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
2016-03-10 05:39:07 +00:00
|
|
|
|
2018-08-13 22:22:45 +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_;
|
2016-06-15 11:30:26 +00:00
|
|
|
}
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 22:22:45 +00:00
|
|
|
return url_request_context_;
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 22:47:30 +00:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner>
|
|
|
|
URLRequestContextGetter::GetNetworkTaskRunner() const {
|
2017-01-13 08:52:45 +00:00
|
|
|
return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO);
|
2013-03-13 19:12:05 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 23:11:47 +00:00
|
|
|
} // namespace brightray
|