electron/atom/browser/atom_browser_context.cc

250 lines
8.8 KiB
C++
Raw Normal View History

// 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
// found in the LICENSE file.
2014-03-16 00:30:26 +00:00
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/api/atom_api_protocol.h"
2016-08-31 00:08:32 +00:00
#include "atom/browser/atom_blob_reader.h"
2014-03-16 00:30:26 +00:00
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_download_manager_delegate.h"
2016-08-26 22:30:02 +00:00
#include "atom/browser/atom_permission_manager.h"
#include "atom/browser/browser.h"
2016-08-26 22:30:02 +00:00
#include "atom/browser/net/asar/asar_protocol_handler.h"
#include "atom/browser/net/atom_cert_verifier.h"
#include "atom/browser/net/atom_ct_delegate.h"
2015-12-01 04:52:22 +00:00
#include "atom/browser/net/atom_network_delegate.h"
#include "atom/browser/net/atom_ssl_config_service.h"
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "atom/browser/net/http_protocol_handler.h"
2015-02-04 23:17:28 +00:00
#include "atom/browser/web_view_manager.h"
#include "atom/common/atom_version.h"
#include "atom/common/chrome_version.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/worker_pool.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
2016-08-31 00:08:32 +00:00
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/public/browser/browser_thread.h"
2016-08-31 00:08:32 +00:00
#include "content/public/browser/storage_partition.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/user_agent.h"
2015-05-14 15:02:40 +00:00
#include "net/ftp/ftp_network_layer.h"
#include "net/url_request/data_protocol_handler.h"
2015-05-14 15:02:40 +00:00
#include "net/url_request/ftp_protocol_handler.h"
#include "net/url_request/url_request_context.h"
2016-08-26 22:30:02 +00:00
#include "net/url_request/url_request_intercepting_job_factory.h"
2014-09-01 07:36:37 +00:00
#include "url/url_constants.h"
using content::BrowserThread;
namespace atom {
2014-09-23 04:13:46 +00:00
namespace {
class NoCacheBackend : public net::HttpCache::BackendFactory {
int CreateBackend(net::NetLog* net_log,
2016-05-23 01:59:39 +00:00
std::unique_ptr<disk_cache::Backend>* backend,
const net::CompletionCallback& callback) override {
return net::ERR_FAILED;
}
};
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
AtomBrowserContext::AtomBrowserContext(const std::string& partition,
bool in_memory,
const base::DictionaryValue& options)
: brightray::BrowserContext(partition, in_memory),
ct_delegate_(new AtomCTDelegate),
network_delegate_(new AtomNetworkDelegate),
cookie_delegate_(new AtomCookieDelegate) {
2016-06-22 06:46:46 +00:00
// Construct user agent string.
Browser* browser = Browser::Get();
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);
}
2016-06-22 06:46:46 +00:00
user_agent_ = content::BuildUserAgentFromProduct(user_agent);
2016-07-12 13:05:07 +00:00
// Read options.
use_cache_ = true;
options.GetBoolean("cache", &use_cache_);
// Initialize Pref Registry in brightray.
InitPrefs();
2016-06-22 06:46:46 +00:00
}
AtomBrowserContext::~AtomBrowserContext() {
}
void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
user_agent_ = user_agent;
}
2016-06-22 06:46:46 +00:00
net::NetworkDelegate* AtomBrowserContext::CreateNetworkDelegate() {
return network_delegate_;
}
net::CookieMonsterDelegate* AtomBrowserContext::CreateCookieDelegate() {
return cookie_delegate();
}
2016-06-22 06:46:46 +00:00
std::string AtomBrowserContext::GetUserAgent() {
return user_agent_;
}
2016-05-23 01:59:39 +00:00
std::unique_ptr<net::URLRequestJobFactory>
2015-12-07 14:44:35 +00:00
AtomBrowserContext::CreateURLRequestJobFactory(
content::ProtocolHandlerMap* protocol_handlers) {
std::unique_ptr<AtomURLRequestJobFactory> job_factory(
new AtomURLRequestJobFactory);
for (auto& it : *protocol_handlers) {
job_factory->SetProtocolHandler(it.first,
base::WrapUnique(it.second.release()));
}
protocol_handlers->clear();
job_factory->SetProtocolHandler(
2016-07-04 06:08:55 +00:00
url::kDataScheme, base::WrapUnique(new net::DataProtocolHandler));
job_factory->SetProtocolHandler(
2016-07-04 06:08:55 +00:00
url::kFileScheme, base::WrapUnique(new asar::AsarProtocolHandler(
BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))));
job_factory->SetProtocolHandler(
url::kHttpScheme,
2016-07-04 06:08:55 +00:00
base::WrapUnique(new HttpProtocolHandler(url::kHttpScheme)));
job_factory->SetProtocolHandler(
url::kHttpsScheme,
2016-07-04 06:08:55 +00:00
base::WrapUnique(new HttpProtocolHandler(url::kHttpsScheme)));
job_factory->SetProtocolHandler(
url::kWsScheme,
2016-07-04 06:08:55 +00:00
base::WrapUnique(new HttpProtocolHandler(url::kWsScheme)));
job_factory->SetProtocolHandler(
url::kWssScheme,
2016-07-04 06:08:55 +00:00
base::WrapUnique(new HttpProtocolHandler(url::kWssScheme)));
auto host_resolver =
url_request_context_getter()->GetURLRequestContext()->host_resolver();
2015-05-21 05:37:25 +00:00
job_factory->SetProtocolHandler(
url::kFtpScheme,
2016-07-04 06:08:55 +00:00
base::WrapUnique(new net::FtpProtocolHandler(
new net::FtpNetworkLayer(host_resolver))));
2015-05-21 05:37:25 +00:00
return std::move(job_factory);
}
net::HttpCache::BackendFactory*
AtomBrowserContext::CreateHttpCacheBackendFactory(
const base::FilePath& base_path) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
2016-07-12 13:05:07 +00:00
if (!use_cache_ || command_line->HasSwitch(switches::kDisableHttpCache))
return new NoCacheBackend;
else
return brightray::BrowserContext::CreateHttpCacheBackendFactory(base_path);
}
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_)
guest_manager_.reset(new WebViewManager);
2014-10-22 14:55:13 +00:00
return guest_manager_.get();
}
content::PermissionManager* AtomBrowserContext::GetPermissionManager() {
2016-01-31 19:13:29 +00:00
if (!permission_manager_.get())
permission_manager_.reset(new AtomPermissionManager);
return permission_manager_.get();
}
2016-05-23 01:59:39 +00:00
std::unique_ptr<net::CertVerifier> AtomBrowserContext::CreateCertVerifier() {
return base::WrapUnique(new AtomCertVerifier(ct_delegate_.get()));
}
net::SSLConfigService* AtomBrowserContext::CreateSSLConfigService() {
return new AtomSSLConfigService;
}
std::vector<std::string> AtomBrowserContext::GetCookieableSchemes() {
auto default_schemes = brightray::BrowserContext::GetCookieableSchemes();
const auto& standard_schemes = atom::api::GetStandardSchemes();
default_schemes.insert(default_schemes.end(),
standard_schemes.begin(), standard_schemes.end());
return default_schemes;
}
net::TransportSecurityState::RequireCTDelegate*
AtomBrowserContext::GetRequireCTDelegate() {
return ct_delegate_.get();
}
void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) {
pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
base::FilePath());
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,
download_dir);
pref_registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
}
2016-08-31 00:08:32 +00:00
AtomBlobReader* AtomBrowserContext::GetBlobReader() {
if (!blob_reader_.get()) {
content::ChromeBlobStorageContext* blob_context =
content::ChromeBlobStorageContext::GetFor(this);
storage::FileSystemContext* file_system_context =
content::BrowserContext::GetStoragePartition(
this, nullptr)->GetFileSystemContext();
blob_reader_.reset(new AtomBlobReader(blob_context,
file_system_context));
}
return blob_reader_.get();
}
// static
scoped_refptr<AtomBrowserContext> AtomBrowserContext::From(
2016-07-12 12:53:19 +00:00
const std::string& partition, bool in_memory,
const base::DictionaryValue& options) {
auto browser_context = brightray::BrowserContext::Get(partition, in_memory);
if (browser_context)
return static_cast<AtomBrowserContext*>(browser_context.get());
2016-07-12 13:05:07 +00:00
return new AtomBrowserContext(partition, in_memory, options);
}
} // namespace atom