2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-15 16:25:08 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
2013-04-15 16:25:08 +00:00
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
2015-06-22 11:43:49 +00:00
|
|
|
#include "atom/browser/atom_download_manager_delegate.h"
|
2015-07-14 18:40:07 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2015-11-17 11:03:09 +00:00
|
|
|
#include "atom/browser/net/atom_cert_verifier.h"
|
2015-12-01 04:52:22 +00:00
|
|
|
#include "atom/browser/net/atom_network_delegate.h"
|
2015-11-17 11:05:38 +00:00
|
|
|
#include "atom/browser/net/atom_ssl_config_service.h"
|
2014-08-13 09:40:31 +00:00
|
|
|
#include "atom/browser/net/atom_url_request_job_factory.h"
|
2014-09-23 04:13:46 +00:00
|
|
|
#include "atom/browser/net/asar/asar_protocol_handler.h"
|
2015-05-06 20:15:56 +00:00
|
|
|
#include "atom/browser/net/http_protocol_handler.h"
|
2016-01-30 11:19:18 +00:00
|
|
|
#include "atom/browser/atom_permission_manager.h"
|
2015-02-04 23:17:28 +00:00
|
|
|
#include "atom/browser/web_view_manager.h"
|
2015-07-14 18:40:07 +00:00
|
|
|
#include "atom/common/atom_version.h"
|
|
|
|
#include "atom/common/chrome_version.h"
|
2015-01-05 21:40:38 +00:00
|
|
|
#include "atom/common/options_switches.h"
|
|
|
|
#include "base/command_line.h"
|
2015-07-26 08:17:55 +00:00
|
|
|
#include "base/files/file_path.h"
|
2016-01-21 08:21:37 +00:00
|
|
|
#include "base/path_service.h"
|
2015-07-26 08:17:55 +00:00
|
|
|
#include "base/prefs/pref_registry_simple.h"
|
2015-07-14 18:40:07 +00:00
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "base/strings/stringprintf.h"
|
2014-08-13 09:40:31 +00:00
|
|
|
#include "base/threading/sequenced_worker_pool.h"
|
|
|
|
#include "base/threading/worker_pool.h"
|
2016-01-21 08:21:37 +00:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
2015-07-26 08:17:55 +00:00
|
|
|
#include "chrome/common/pref_names.h"
|
2013-09-20 08:47:47 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2014-08-13 09:40:31 +00:00
|
|
|
#include "content/public/common/url_constants.h"
|
2015-07-14 18:40:07 +00:00
|
|
|
#include "content/public/common/user_agent.h"
|
2015-05-14 15:02:40 +00:00
|
|
|
#include "net/ftp/ftp_network_layer.h"
|
2014-08-13 09:40:31 +00:00
|
|
|
#include "net/url_request/data_protocol_handler.h"
|
2015-05-14 15:02:40 +00:00
|
|
|
#include "net/url_request/ftp_protocol_handler.h"
|
2014-09-01 07:36:37 +00:00
|
|
|
#include "net/url_request/url_request_intercepting_job_factory.h"
|
2015-05-14 15:02:40 +00:00
|
|
|
#include "net/url_request/url_request_context.h"
|
2014-09-01 07:36:37 +00:00
|
|
|
#include "url/url_constants.h"
|
2013-04-15 16:25:08 +00:00
|
|
|
|
2013-09-20 08:47:47 +00:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2014-08-13 09:40:31 +00:00
|
|
|
namespace atom {
|
2013-09-20 08:47:47 +00:00
|
|
|
|
2014-09-23 04:13:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-01-05 21:40:38 +00:00
|
|
|
class NoCacheBackend : public net::HttpCache::BackendFactory {
|
|
|
|
int CreateBackend(net::NetLog* net_log,
|
|
|
|
scoped_ptr<disk_cache::Backend>* backend,
|
|
|
|
const net::CompletionCallback& callback) override {
|
|
|
|
return net::ERR_FAILED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-14 18:40:07 +00:00
|
|
|
std::string RemoveWhitespace(const std::string& str) {
|
|
|
|
std::string trimmed;
|
|
|
|
if (base::RemoveChars(str, " ", &trimmed))
|
|
|
|
return trimmed;
|
|
|
|
else
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2014-09-23 04:13:46 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-09-05 12:54:36 +00:00
|
|
|
AtomBrowserContext::AtomBrowserContext(const std::string& partition,
|
|
|
|
bool in_memory)
|
|
|
|
: brightray::BrowserContext(partition, in_memory),
|
2015-12-07 11:56:23 +00:00
|
|
|
cert_verifier_(nullptr),
|
2015-10-19 20:03:20 +00:00
|
|
|
job_factory_(new AtomURLRequestJobFactory),
|
2015-12-01 04:52:22 +00:00
|
|
|
network_delegate_(new AtomNetworkDelegate),
|
2015-10-19 20:12:17 +00:00
|
|
|
allow_ntlm_everywhere_(false) {
|
2015-10-19 20:07:14 +00:00
|
|
|
}
|
2013-04-15 16:25:08 +00:00
|
|
|
|
|
|
|
AtomBrowserContext::~AtomBrowserContext() {
|
|
|
|
}
|
|
|
|
|
2015-12-01 04:52:22 +00:00
|
|
|
net::NetworkDelegate* AtomBrowserContext::CreateNetworkDelegate() {
|
|
|
|
return network_delegate_;
|
|
|
|
}
|
|
|
|
|
2015-07-14 18:40:07 +00:00
|
|
|
std::string AtomBrowserContext::GetUserAgent() {
|
|
|
|
Browser* browser = Browser::Get();
|
2015-07-14 19:12:55 +00:00
|
|
|
std::string name = RemoveWhitespace(browser->GetName());
|
|
|
|
std::string user_agent;
|
|
|
|
if (name == ATOM_PRODUCT_NAME) {
|
|
|
|
user_agent = "Chrome/" CHROME_VERSION_STRING " "
|
|
|
|
ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING;
|
|
|
|
} else {
|
|
|
|
user_agent = base::StringPrintf(
|
|
|
|
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
|
|
|
|
name.c_str(),
|
|
|
|
browser->GetVersion().c_str(),
|
|
|
|
CHROME_VERSION_STRING);
|
|
|
|
}
|
2015-07-14 18:40:07 +00:00
|
|
|
return content::BuildUserAgentFromProduct(user_agent);
|
|
|
|
}
|
|
|
|
|
2015-12-07 14:44:35 +00:00
|
|
|
scoped_ptr<net::URLRequestJobFactory>
|
|
|
|
AtomBrowserContext::CreateURLRequestJobFactory(
|
2014-08-13 09:40:31 +00:00
|
|
|
content::ProtocolHandlerMap* handlers,
|
2014-09-01 07:36:37 +00:00
|
|
|
content::URLRequestInterceptorScopedVector* interceptors) {
|
2014-08-13 09:40:31 +00:00
|
|
|
scoped_ptr<AtomURLRequestJobFactory> job_factory(job_factory_);
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
for (auto& it : *handlers) {
|
|
|
|
job_factory->SetProtocolHandler(it.first,
|
|
|
|
make_scoped_ptr(it.second.release()));
|
|
|
|
}
|
2014-08-13 09:40:31 +00:00
|
|
|
handlers->clear();
|
|
|
|
|
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kDataScheme, make_scoped_ptr(new net::DataProtocolHandler));
|
2014-08-13 09:40:31 +00:00
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kFileScheme, make_scoped_ptr(new asar::AsarProtocolHandler(
|
2014-08-13 09:40:31 +00:00
|
|
|
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
|
2015-08-12 05:30:19 +00:00
|
|
|
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
|
2015-05-06 20:15:56 +00:00
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kHttpScheme,
|
|
|
|
make_scoped_ptr(new HttpProtocolHandler(url::kHttpScheme)));
|
2015-05-11 07:26:36 +00:00
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kHttpsScheme,
|
|
|
|
make_scoped_ptr(new HttpProtocolHandler(url::kHttpsScheme)));
|
2015-05-11 07:26:36 +00:00
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kWsScheme,
|
|
|
|
make_scoped_ptr(new HttpProtocolHandler(url::kWsScheme)));
|
2015-05-11 07:26:36 +00:00
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kWssScheme,
|
|
|
|
make_scoped_ptr(new HttpProtocolHandler(url::kWssScheme)));
|
2014-08-13 09:40:31 +00:00
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
auto host_resolver =
|
|
|
|
url_request_context_getter()->GetURLRequestContext()->host_resolver();
|
2015-05-21 05:37:25 +00:00
|
|
|
job_factory->SetProtocolHandler(
|
2015-08-12 05:30:19 +00:00
|
|
|
url::kFtpScheme,
|
|
|
|
make_scoped_ptr(new net::FtpProtocolHandler(
|
|
|
|
new net::FtpNetworkLayer(host_resolver))));
|
2015-05-21 05:37:25 +00:00
|
|
|
|
2014-08-13 09:40:31 +00:00
|
|
|
// Set up interceptors in the reverse order.
|
2016-03-08 14:28:53 +00:00
|
|
|
scoped_ptr<net::URLRequestJobFactory> top_job_factory =
|
|
|
|
std::move(job_factory);
|
2014-09-01 07:36:37 +00:00
|
|
|
content::URLRequestInterceptorScopedVector::reverse_iterator it;
|
2014-08-13 09:40:31 +00:00
|
|
|
for (it = interceptors->rbegin(); it != interceptors->rend(); ++it)
|
2014-09-01 07:36:37 +00:00
|
|
|
top_job_factory.reset(new net::URLRequestInterceptingJobFactory(
|
2016-03-08 14:28:53 +00:00
|
|
|
std::move(top_job_factory), make_scoped_ptr(*it)));
|
2014-08-13 09:40:31 +00:00
|
|
|
interceptors->weak_clear();
|
|
|
|
|
2016-03-08 14:28:53 +00:00
|
|
|
return top_job_factory;
|
2013-09-20 08:47:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 21:40:38 +00:00
|
|
|
net::HttpCache::BackendFactory*
|
|
|
|
AtomBrowserContext::CreateHttpCacheBackendFactory(
|
|
|
|
const base::FilePath& base_path) {
|
|
|
|
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
if (command_line->HasSwitch(switches::kDisableHttpCache))
|
|
|
|
return new NoCacheBackend;
|
|
|
|
else
|
|
|
|
return brightray::BrowserContext::CreateHttpCacheBackendFactory(base_path);
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:43:49 +00:00
|
|
|
content::DownloadManagerDelegate*
|
|
|
|
AtomBrowserContext::GetDownloadManagerDelegate() {
|
|
|
|
if (!download_manager_delegate_.get()) {
|
|
|
|
auto download_manager = content::BrowserContext::GetDownloadManager(this);
|
|
|
|
download_manager_delegate_.reset(
|
|
|
|
new AtomDownloadManagerDelegate(download_manager));
|
|
|
|
}
|
|
|
|
return download_manager_delegate_.get();
|
|
|
|
}
|
|
|
|
|
2014-10-22 14:55:13 +00:00
|
|
|
content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
|
|
|
|
if (!guest_manager_)
|
2015-09-05 02:43:30 +00:00
|
|
|
guest_manager_.reset(new WebViewManager);
|
2014-10-22 14:55:13 +00:00
|
|
|
return guest_manager_.get();
|
|
|
|
}
|
|
|
|
|
2016-01-30 11:19:18 +00:00
|
|
|
content::PermissionManager* AtomBrowserContext::GetPermissionManager() {
|
2016-01-31 19:13:29 +00:00
|
|
|
if (!permission_manager_.get())
|
|
|
|
permission_manager_.reset(new AtomPermissionManager);
|
2016-01-30 11:19:18 +00:00
|
|
|
return permission_manager_.get();
|
|
|
|
}
|
|
|
|
|
2015-12-07 11:56:23 +00:00
|
|
|
scoped_ptr<net::CertVerifier> AtomBrowserContext::CreateCertVerifier() {
|
|
|
|
DCHECK(!cert_verifier_);
|
|
|
|
cert_verifier_ = new AtomCertVerifier;
|
|
|
|
return make_scoped_ptr(cert_verifier_);
|
2015-11-05 14:06:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-21 16:43:32 +00:00
|
|
|
net::SSLConfigService* AtomBrowserContext::CreateSSLConfigService() {
|
|
|
|
return new AtomSSLConfigService;
|
|
|
|
}
|
|
|
|
|
2015-07-26 08:17:55 +00:00
|
|
|
void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) {
|
|
|
|
pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
|
|
|
|
base::FilePath());
|
2016-01-21 08:21:37 +00:00
|
|
|
base::FilePath download_dir;
|
|
|
|
PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir);
|
2015-07-26 08:30:02 +00:00
|
|
|
pref_registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
|
2016-01-21 08:21:37 +00:00
|
|
|
download_dir);
|
2016-03-15 02:21:36 +00:00
|
|
|
pref_registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
|
2015-07-26 08:17:55 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 05:52:39 +00:00
|
|
|
bool AtomBrowserContext::AllowNTLMCredentialsForDomain(const GURL& origin) {
|
|
|
|
if (allow_ntlm_everywhere_)
|
|
|
|
return true;
|
|
|
|
return Delegate::AllowNTLMCredentialsForDomain(origin);
|
2015-10-19 20:03:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 20:07:14 +00:00
|
|
|
void AtomBrowserContext::AllowNTLMCredentialsForAllDomains(bool should_allow) {
|
|
|
|
allow_ntlm_everywhere_ = should_allow;
|
|
|
|
}
|
|
|
|
|
2013-04-15 16:25:08 +00:00
|
|
|
} // namespace atom
|
2015-09-05 12:54:36 +00:00
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
// static
|
2015-09-05 14:39:48 +00:00
|
|
|
scoped_refptr<BrowserContext> BrowserContext::Create(
|
|
|
|
const std::string& partition, bool in_memory) {
|
|
|
|
return make_scoped_refptr(new atom::AtomBrowserContext(partition, in_memory));
|
2015-09-05 12:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|