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
|
|
|
|
2016-08-31 00:08:32 +00:00
|
|
|
#include "atom/browser/atom_blob_reader.h"
|
2018-08-13 23:16:04 +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"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "atom/browser/atom_permission_manager.h"
|
2015-07-14 18:40:07 +00:00
|
|
|
#include "atom/browser/browser.h"
|
2018-08-14 21:07:53 +00:00
|
|
|
#include "atom/browser/request_context_delegate.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"
|
2018-08-13 23:16:04 +00:00
|
|
|
#include "atom/common/options_switches.h"
|
2015-01-05 21:40:38 +00:00
|
|
|
#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-14 18:40:07 +00:00
|
|
|
#include "base/strings/stringprintf.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"
|
2016-06-15 11:31:29 +00:00
|
|
|
#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"
|
2015-07-14 18:40:07 +00:00
|
|
|
#include "content/public/common/user_agent.h"
|
2013-09-20 08:47:47 +00:00
|
|
|
|
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-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
|
|
|
|
|
2016-10-17 10:33:24 +00:00
|
|
|
AtomBrowserContext::AtomBrowserContext(const std::string& partition,
|
|
|
|
bool in_memory,
|
|
|
|
const base::DictionaryValue& options)
|
2018-08-14 21:07:53 +00:00
|
|
|
: brightray::BrowserContext(partition, in_memory),
|
|
|
|
url_request_context_getter_(nullptr) {
|
2016-06-22 06:46:46 +00:00
|
|
|
// Construct user agent string.
|
2015-07-14 18:40:07 +00:00
|
|
|
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) {
|
2018-04-18 01:55:30 +00:00
|
|
|
user_agent = "Chrome/" CHROME_VERSION_STRING " " ATOM_PRODUCT_NAME
|
|
|
|
"/" ATOM_VERSION_STRING;
|
2015-07-14 19:12:55 +00:00
|
|
|
} else {
|
|
|
|
user_agent = base::StringPrintf(
|
|
|
|
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
|
2018-04-18 01:55:30 +00:00
|
|
|
name.c_str(), browser->GetVersion().c_str(), CHROME_VERSION_STRING);
|
2015-07-14 19:12:55 +00:00
|
|
|
}
|
2016-06-22 06:46:46 +00:00
|
|
|
user_agent_ = content::BuildUserAgentFromProduct(user_agent);
|
2016-07-12 13:05:07 +00:00
|
|
|
|
|
|
|
// Read options.
|
2018-08-14 21:07:53 +00:00
|
|
|
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
bool use_cache = !command_line->HasSwitch(switches::kDisableHttpCache);
|
|
|
|
options.GetBoolean("cache", &use_cache);
|
|
|
|
|
|
|
|
request_context_delegate_.reset(new RequestContextDelegate(use_cache));
|
2016-07-26 11:04:04 +00:00
|
|
|
|
|
|
|
// Initialize Pref Registry in brightray.
|
|
|
|
InitPrefs();
|
2016-06-22 06:46:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 21:07:53 +00:00
|
|
|
AtomBrowserContext::~AtomBrowserContext() {
|
|
|
|
url_request_context_getter_->set_delegate(nullptr);
|
|
|
|
}
|
2016-06-22 06:46:46 +00:00
|
|
|
|
2016-06-22 06:57:51 +00:00
|
|
|
void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
|
|
|
|
user_agent_ = user_agent;
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:43:49 +00:00
|
|
|
content::DownloadManagerDelegate*
|
|
|
|
AtomBrowserContext::GetDownloadManagerDelegate() {
|
|
|
|
if (!download_manager_delegate_.get()) {
|
2018-04-17 22:41:47 +00:00
|
|
|
auto* download_manager = content::BrowserContext::GetDownloadManager(this);
|
2015-06-22 11:43:49 +00:00
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2018-08-14 21:07:53 +00:00
|
|
|
std::string AtomBrowserContext::GetUserAgent() const {
|
|
|
|
return user_agent_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AtomBrowserContext::OnMainRequestContextCreated(
|
|
|
|
brightray::URLRequestContextGetter* getter) {
|
|
|
|
getter->set_delegate(request_context_delegate_.get());
|
|
|
|
url_request_context_getter_ = getter;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:08:32 +00:00
|
|
|
AtomBlobReader* AtomBrowserContext::GetBlobReader() {
|
|
|
|
if (!blob_reader_.get()) {
|
|
|
|
content::ChromeBlobStorageContext* blob_context =
|
|
|
|
content::ChromeBlobStorageContext::GetFor(this);
|
2018-04-12 08:06:23 +00:00
|
|
|
blob_reader_.reset(new AtomBlobReader(blob_context));
|
2016-08-31 00:08:32 +00:00
|
|
|
}
|
|
|
|
return blob_reader_.get();
|
|
|
|
}
|
|
|
|
|
2015-09-05 12:54:36 +00:00
|
|
|
// static
|
2016-07-12 12:39:54 +00:00
|
|
|
scoped_refptr<AtomBrowserContext> AtomBrowserContext::From(
|
2018-04-18 01:55:30 +00:00
|
|
|
const std::string& partition,
|
|
|
|
bool in_memory,
|
2016-07-12 12:53:19 +00:00
|
|
|
const base::DictionaryValue& options) {
|
2016-07-12 12:39:54 +00:00
|
|
|
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);
|
2015-09-05 12:54:36 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 12:39:54 +00:00
|
|
|
} // namespace atom
|