electron/atom/browser/atom_browser_context.cc

149 lines
5 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 17:49:37 +08:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
2014-03-16 08:30:26 +08:00
#include "atom/browser/atom_browser_context.h"
2016-08-31 05:38:32 +05:30
#include "atom/browser/atom_blob_reader.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_download_manager_delegate.h"
2016-08-26 15:30:02 -07:00
#include "atom/browser/atom_permission_manager.h"
#include "atom/browser/browser.h"
#include "atom/browser/request_context_delegate.h"
#include "atom/browser/special_storage_policy.h"
2015-02-04 15:17:28 -08: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/path_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
2016-08-31 05:38:32 +05:30
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/public/common/user_agent.h"
namespace atom {
2014-09-23 12:13:46 +08:00
namespace {
std::string RemoveWhitespace(const std::string& str) {
std::string trimmed;
if (base::RemoveChars(str, " ", &trimmed))
return trimmed;
else
return str;
}
2014-09-23 12:13:46 +08:00
} // namespace
AtomBrowserContext::AtomBrowserContext(const std::string& partition,
bool in_memory,
const base::DictionaryValue& options)
: brightray::BrowserContext(partition, in_memory),
url_request_context_getter_(nullptr),
storage_policy_(new SpecialStoragePolicy) {
2016-06-22 15:46:46 +09:00
// Construct user agent string.
Browser* browser = Browser::Get();
std::string name = RemoveWhitespace(browser->GetName());
std::string user_agent;
if (name == ATOM_PRODUCT_NAME) {
2018-04-17 21:55:30 -04:00
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,
2018-04-17 21:55:30 -04:00
name.c_str(), browser->GetVersion().c_str(), CHROME_VERSION_STRING);
}
2016-06-22 15:46:46 +09:00
user_agent_ = content::BuildUserAgentFromProduct(user_agent);
2016-07-12 22:05:07 +09:00
// Read options.
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));
// Initialize Pref Registry in brightray.
InitPrefs();
2016-06-22 15:46:46 +09:00
}
AtomBrowserContext::~AtomBrowserContext() {
url_request_context_getter_->set_delegate(nullptr);
}
2016-06-22 15:46:46 +09:00
void AtomBrowserContext::SetUserAgent(const std::string& user_agent) {
user_agent_ = user_agent;
}
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 22:55:13 +08:00
content::BrowserPluginGuestManager* AtomBrowserContext::GetGuestManager() {
if (!guest_manager_)
guest_manager_.reset(new WebViewManager);
2014-10-22 22:55:13 +08:00
return guest_manager_.get();
}
content::PermissionManager* AtomBrowserContext::GetPermissionManager() {
2016-02-01 00:43:29 +05:30
if (!permission_manager_.get())
permission_manager_.reset(new AtomPermissionManager);
return permission_manager_.get();
}
storage::SpecialStoragePolicy* AtomBrowserContext::GetSpecialStoragePolicy() {
return storage_policy_.get();
}
void AtomBrowserContext::RegisterPrefs(PrefRegistrySimple* pref_registry) {
pref_registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
base::FilePath());
base::FilePath download_dir;
feat: upgrade to Chromium 68.0.3440.128 and Node 10.11.0 (#14677) * Update to Chromium 68.0.3440.128 and Node 10.10.0 * update v8, ffmpeg, chromium, crashpad, boringssl, and webrtc patches * fix SSL_get_tlsext_status_type patch * pass encryption_modes_supported to CdmInfo * kNoSandbox moved into service_manager * bump CHROME_VERSION_STRING TODO: automatically pull in the real chrome version * PathService -> base::PathService * net::X509Certificate::Equals -> net::X509Certificate::EqualsExcludingChain * use content::ChildProcessTerminationInfo * GetHandle() -> GetProcess().Handle() * ScopedNestableTaskAllower doesn't take an argument * net::HttpAuthCache::ClearEntriesAddedWithin -> ClearAllEntries * std::unique_ptr<WebContents> * blink::WebFullscreenOptions * OnAudioStateChanged doesn't take a WebContents * content::RESULT_CODE_NORMAL_EXIT -> service_manager::RESULT_CODE_NORMAL_EXIT * MessageLoopCurrent * WasResized -> SynchronizeVisualProperties * SetTimeStamp takes a base::TimeTicks * ExecuteScriptInIsolatedWorld is single-script only * DispatchNonPersistentCloseEvent takes a callback now * expose URLRequestContextGetter::{Add,Remove}Observer * test: remove no longer existing Chromium test deps cc_blink_unittests has been removed in https://chromium-review.googlesource.com/1053765 mojo_common_unittests has been removed in https://chromium-review.googlesource.com/1028000 * SetFdLimit -> IncreaseFdLimitTo NOTE: the behaviour of this API has changed slightly, and we should mention that in the notes. * MessageLoop::QuitWhenIdleClosure -> RunLoop::QuitCurrentWhenIdleClosureDeprecated * certificate_transparency moved out of net/ pending a clearer decision about what to do with CT in the mean time, copy CreateLogVerifiersForKnownLogs from deleted chromium source * add secure_origin_whitelist to chrome source list NOTE: is this something we actually want? cc @deepak1556 * DrainBackgroundTasks -> DrainTasks * use new node options parser * fix disable_scroll_begin_dcheck.patch * ViewsDelegate::CreateWebContents went away see https://chromium-review.googlesource.com/c/chromium/src/+/1031314 * kZygoteProcess moved into service_manager * test: minor improvements to the Node spec - reformat some parts - better failures reporting with `expect` - skip some tests instead of marking them as passed * chromium removed *_posix.cc from the source filters * test: fix :electron_tests compilation * better crash diagnostics in ffmpeg test * fix: enable back a DCHECK in viz::ServerSharedBitmapManager Fixes #14327. Backports https://chromium-review.googlesource.com/802574. * chore: update linux sysroots * chore: remove obsolete "install-sysroot.py" script * test: fix frame-subscriber test on Mac * disable OSR for now * test: make before-input-event test more robust * test: make run-as-node --inspect test more robust on windows * roll node to v10.11.0 * avoid duplicate files when building a zip * disable failing assert in beginFrameSubscription dirty-rectangle test * experiment with is_cfi = false * fix: build torque with x64 toolchain Co-Authored-By: Alexey Kuzmin <github@alexeykuzmin.com> * test: disable the "app.relaunch" test on Linux * chore: bump node to get header tar file * chore: bump node to fix tar.py line endings
2018-10-03 19:02:14 -07:00
base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir);
2015-07-26 16:30:02 +08:00
pref_registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
download_dir);
pref_registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
}
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 05:38:32 +05:30
AtomBlobReader* AtomBrowserContext::GetBlobReader() {
if (!blob_reader_.get()) {
content::ChromeBlobStorageContext* blob_context =
content::ChromeBlobStorageContext::GetFor(this);
blob_reader_.reset(new AtomBlobReader(blob_context));
2016-08-31 05:38:32 +05:30
}
return blob_reader_.get();
}
// static
scoped_refptr<AtomBrowserContext> AtomBrowserContext::From(
2018-04-17 21:55:30 -04:00
const std::string& partition,
bool in_memory,
2016-07-12 21:53:19 +09:00
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 22:05:07 +09:00
return new AtomBrowserContext(partition, in_memory, options);
}
} // namespace atom