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