Merge pull request #15618 from electron/system_netwok_context

fix: create system network context for global requests
This commit is contained in:
Cheng Zhao 2018-11-19 11:25:34 +09:00 committed by GitHub
commit e12c0dcf66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 607 additions and 224 deletions

View file

@ -7,10 +7,10 @@
#include <utility>
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "base/command_line.h"
#include "chrome/browser/browser_process.h"
#include "components/net_log/chrome_net_log.h"
#include "content/public/browser/storage_partition.h"
#include "native_mate/dictionary.h"
@ -27,8 +27,7 @@ NetLog::NetLog(v8::Isolate* isolate, AtomBrowserContext* browser_context)
: browser_context_(browser_context) {
Init(isolate);
net_log_writer_ =
atom::AtomBrowserMainParts::Get()->net_log()->net_export_file_writer();
net_log_writer_ = g_browser_process->net_log()->net_export_file_writer();
net_log_writer_->AddObserver(this);
}

View file

@ -45,6 +45,7 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "components/net_log/chrome_net_log.h"
#include "content/public/browser/browser_ppapi_host.h"
#include "content/public/browser/client_certificate_delegate.h"
@ -662,7 +663,7 @@ std::unique_ptr<base::Value> AtomBrowserClient::GetServiceManifestOverlay(
}
net::NetLog* AtomBrowserClient::GetNetLog() {
return AtomBrowserMainParts::Get()->net_log();
return g_browser_process->net_log();
}
content::BrowserMainParts* AtomBrowserClient::CreateBrowserMainParts(
@ -803,6 +804,21 @@ base::FilePath AtomBrowserClient::GetDefaultDownloadDirectory() {
return path;
}
scoped_refptr<network::SharedURLLoaderFactory>
AtomBrowserClient::GetSystemSharedURLLoaderFactory() {
if (!g_browser_process)
return nullptr;
return g_browser_process->shared_url_loader_factory();
}
void AtomBrowserClient::OnNetworkServiceCreated(
network::mojom::NetworkService* network_service) {
if (!g_browser_process)
return;
g_browser_process->system_network_context_manager()->OnNetworkServiceCreated(
network_service);
}
std::string AtomBrowserClient::GetApplicationLocale() {
if (BrowserThread::CurrentlyOn(BrowserThread::IO))
return g_io_thread_application_locale.Get();

View file

@ -144,6 +144,10 @@ class AtomBrowserClient : public content::ContentBrowserClient,
content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams&) override;
base::FilePath GetDefaultDownloadDirectory() override;
scoped_refptr<network::SharedURLLoaderFactory>
GetSystemSharedURLLoaderFactory() override;
void OnNetworkServiceCreated(
network::mojom::NetworkService* network_service) override;
// content::RenderProcessHostObserver:
void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;

View file

@ -19,7 +19,6 @@
#include "atom/browser/atom_web_ui_controller_factory.h"
#include "atom/browser/browser.h"
#include "atom/browser/browser_process_impl.h"
#include "atom/browser/io_thread.h"
#include "atom/browser/javascript_environment.h"
#include "atom/browser/media/media_capture_devices_dispatcher.h"
#include "atom/browser/node_debugger.h"
@ -36,9 +35,6 @@
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/icon_manager.h"
#include "chrome/browser/net/chrome_net_log_helper.h"
#include "components/net_log/chrome_net_log.h"
#include "components/net_log/net_export_file_writer.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/web_ui_controller_factory.h"
@ -49,7 +45,6 @@
#include "electron/buildflags/buildflags.h"
#include "media/base/localized_strings.h"
#include "services/device/public/mojom/constants.mojom.h"
#include "services/network/public/cpp/network_switches.h"
#include "services/service_manager/public/cpp/connector.h"
#include "ui/base/idle/idle.h"
#include "ui/base/l10n/l10n_util.h"
@ -348,6 +343,9 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
// command-line changes.
base::FeatureList::ClearInstanceForTesting();
InitializeFeatureList();
// Initialize after user script environment creation.
fake_browser_process_->PostEarlyInitialization();
}
int AtomBrowserMainParts::PreCreateThreads() {
@ -379,23 +377,7 @@ int AtomBrowserMainParts::PreCreateThreads() {
ui::InitIdleMonitor();
#endif
net_log_ = std::make_unique<net_log::ChromeNetLog>();
auto& command_line = main_function_params_.command_line;
// start net log trace if --log-net-log is passed in the command line.
if (command_line.HasSwitch(network::switches::kLogNetLog)) {
base::FilePath log_file =
command_line.GetSwitchValuePath(network::switches::kLogNetLog);
if (!log_file.empty()) {
net_log_->StartWritingToFile(
log_file, GetNetCaptureModeFromCommandLine(command_line),
command_line.GetCommandLineString(), std::string());
}
}
// Initialize net log file exporter.
net_log_->net_export_file_writer()->Initialize();
// Manage global state of net and other IO thread related.
io_thread_ = std::make_unique<IOThread>(net_log_.get());
fake_browser_process_->PreCreateThreads(main_function_params_.command_line);
return 0;
}
@ -405,7 +387,7 @@ void AtomBrowserMainParts::PostDestroyThreads() {
device::BluetoothAdapterFactory::Shutdown();
bluez::DBusBluezManagerWrapperLinux::Shutdown();
#endif
io_thread_.reset();
fake_browser_process_->PostDestroyThreads();
}
void AtomBrowserMainParts::ToolkitInitialized() {
@ -523,6 +505,8 @@ void AtomBrowserMainParts::PostMainMessageLoopRun() {
std::move(callback).Run();
++iter;
}
fake_browser_process_->PostMainMessageLoopRun();
}
#if !defined(OS_MACOSX)

View file

@ -20,10 +20,6 @@
class BrowserProcess;
class IconManager;
namespace net_log {
class ChromeNetLog;
}
#if defined(USE_AURA)
namespace wm {
class WMState;
@ -34,7 +30,6 @@ namespace atom {
class AtomBindings;
class Browser;
class IOThread;
class JavascriptEnvironment;
class NodeBindings;
class NodeDebugger;
@ -75,8 +70,6 @@ class AtomBrowserMainParts : public content::BrowserMainParts {
IconManager* GetIconManager();
Browser* browser() { return browser_.get(); }
IOThread* io_thread() const { return io_thread_.get(); }
net_log::ChromeNetLog* net_log() { return net_log_.get(); }
protected:
// content::BrowserMainParts:
@ -134,8 +127,6 @@ class AtomBrowserMainParts : public content::BrowserMainParts {
std::unique_ptr<AtomBindings> atom_bindings_;
std::unique_ptr<NodeEnvironment> node_env_;
std::unique_ptr<NodeDebugger> node_debugger_;
std::unique_ptr<IOThread> io_thread_;
std::unique_ptr<net_log::ChromeNetLog> net_log_;
std::unique_ptr<IconManager> icon_manager_;
base::RepeatingTimer gc_timer_;

View file

@ -4,6 +4,26 @@
#include "atom/browser/browser_process_impl.h"
#include <utility>
#include "chrome/browser/net/chrome_net_log_helper.h"
#include "chrome/common/chrome_switches.h"
#include "components/net_log/chrome_net_log.h"
#include "components/net_log/net_export_file_writer.h"
#include "components/prefs/in_memory_pref_store.h"
#include "components/prefs/overlay_user_pref_store.h"
#include "components/prefs/pref_registry.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service_factory.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "content/public/common/content_switches.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_config_service.h"
#include "net/proxy_resolution/proxy_config_with_annotation.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "services/network/public/cpp/network_switches.h"
#include "ui/base/l10n/l10n_util.h"
#if BUILDFLAG(ENABLE_PRINTING)
@ -18,6 +38,90 @@ BrowserProcessImpl::~BrowserProcessImpl() {
g_browser_process = nullptr;
}
// static
void BrowserProcessImpl::ApplyProxyModeFromCommandLine(
ValueMapPrefStore* pref_store) {
if (!pref_store)
return;
auto* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kNoProxyServer)) {
pref_store->SetValue(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateDirect(),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
} else if (command_line->HasSwitch(switches::kProxyPacUrl)) {
std::string pac_script_url =
command_line->GetSwitchValueASCII(switches::kProxyPacUrl);
pref_store->SetValue(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreatePacScript(
pac_script_url, false /* pac_mandatory */),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
} else if (command_line->HasSwitch(switches::kProxyAutoDetect)) {
pref_store->SetValue(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateAutoDetect(),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
} else if (command_line->HasSwitch(switches::kProxyServer)) {
std::string proxy_server =
command_line->GetSwitchValueASCII(switches::kProxyServer);
std::string bypass_list =
command_line->GetSwitchValueASCII(switches::kProxyBypassList);
pref_store->SetValue(
proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateFixedServers(proxy_server, bypass_list),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
}
void BrowserProcessImpl::PostEarlyInitialization() {
// Mock user prefs, as we only need to track changes for a
// in memory pref store. There are no persistent preferences
PrefServiceFactory prefs_factory;
auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry.get());
auto pref_store = base::MakeRefCounted<ValueMapPrefStore>();
ApplyProxyModeFromCommandLine(pref_store.get());
prefs_factory.set_command_line_prefs(std::move(pref_store));
prefs_factory.set_user_prefs(new OverlayUserPrefStore(new InMemoryPrefStore));
local_state_ = prefs_factory.Create(std::move(pref_registry));
}
void BrowserProcessImpl::PreCreateThreads(
const base::CommandLine& command_line) {
// Must be created before the IOThread.
// Once IOThread class is no longer needed,
// this can be created on first use.
system_network_context_manager_ =
std::make_unique<SystemNetworkContextManager>();
net_log_ = std::make_unique<net_log::ChromeNetLog>();
// start net log trace if --log-net-log is passed in the command line.
if (command_line.HasSwitch(network::switches::kLogNetLog)) {
base::FilePath log_file =
command_line.GetSwitchValuePath(network::switches::kLogNetLog);
if (!log_file.empty()) {
net_log_->StartWritingToFile(
log_file, GetNetCaptureModeFromCommandLine(command_line),
command_line.GetCommandLineString(), std::string());
}
}
// Initialize net log file exporter.
net_log_->net_export_file_writer()->Initialize();
// Manage global state of net and other IO thread related.
io_thread_ = std::make_unique<IOThread>(
net_log_.get(), system_network_context_manager_.get());
}
void BrowserProcessImpl::PostDestroyThreads() {
io_thread_.reset();
}
void BrowserProcessImpl::PostMainMessageLoopRun() {
// This expects to be destroyed before the task scheduler is torn down.
system_network_context_manager_.reset();
}
bool BrowserProcessImpl::IsShuttingDown() {
return false;
}
@ -40,7 +144,8 @@ ProfileManager* BrowserProcessImpl::profile_manager() {
}
PrefService* BrowserProcessImpl::local_state() {
return nullptr;
DCHECK(local_state_.get());
return local_state_.get();
}
net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() {
@ -49,7 +154,7 @@ net::URLRequestContextGetter* BrowserProcessImpl::system_request_context() {
scoped_refptr<network::SharedURLLoaderFactory>
BrowserProcessImpl::shared_url_loader_factory() {
return nullptr;
return system_network_context_manager()->GetSharedURLLoaderFactory();
}
variations::VariationsService* BrowserProcessImpl::variations_service() {
@ -74,12 +179,14 @@ NotificationPlatformBridge* BrowserProcessImpl::notification_platform_bridge() {
}
IOThread* BrowserProcessImpl::io_thread() {
return nullptr;
DCHECK(io_thread_.get());
return io_thread_.get();
}
SystemNetworkContextManager*
BrowserProcessImpl::system_network_context_manager() {
return nullptr;
DCHECK(system_network_context_manager_.get());
return system_network_context_manager_.get();
}
network::NetworkQualityTracker* BrowserProcessImpl::network_quality_tracker() {
@ -158,7 +265,8 @@ BrowserProcessImpl::optimization_guide_service() {
}
net_log::ChromeNetLog* BrowserProcessImpl::net_log() {
return nullptr;
DCHECK(net_log_.get());
return net_log_.get();
}
component_updater::ComponentUpdateService*

View file

@ -13,11 +13,20 @@
#include <memory>
#include <string>
#include "atom/browser/io_thread.h"
#include "atom/browser/net/system_network_context_manager.h"
#include "base/command_line.h"
#include "base/macros.h"
#include "chrome/browser/browser_process.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/value_map_pref_store.h"
#include "printing/buildflags/buildflags.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace net_log {
class ChromeNetLog;
}
namespace printing {
class PrintJobManager;
}
@ -32,6 +41,13 @@ class BrowserProcessImpl : public BrowserProcess {
BrowserProcessImpl();
~BrowserProcessImpl() override;
static void ApplyProxyModeFromCommandLine(ValueMapPrefStore* pref_store);
void PostEarlyInitialization();
void PreCreateThreads(const base::CommandLine& command_line);
void PostDestroyThreads();
void PostMainMessageLoopRun();
void ResourceDispatcherHostCreated() override {}
void EndSession() override {}
void FlushLocalStateAndReply(base::OnceClosure reply) override {}
@ -102,6 +118,10 @@ class BrowserProcessImpl : public BrowserProcess {
#if BUILDFLAG(ENABLE_PRINTING)
std::unique_ptr<printing::PrintJobManager> print_job_manager_;
#endif
std::unique_ptr<PrefService> local_state_;
std::unique_ptr<IOThread> io_thread_;
std::unique_ptr<net_log::ChromeNetLog> net_log_;
std::unique_ptr<SystemNetworkContextManager> system_network_context_manager_;
std::string locale_;
DISALLOW_COPY_AND_ASSIGN(BrowserProcessImpl);

View file

@ -3,59 +3,31 @@
// found in the LICENSE file.
#include "atom/browser/io_thread.h"
#include "atom/common/options_switches.h"
#include <utility>
#include "components/net_log/chrome_net_log.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "net/cert/caching_cert_verifier.h"
#include "net/cert/cert_verifier.h"
#include "net/cert/cert_verify_proc.h"
#include "net/cert/multi_threaded_cert_verifier.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/network_service.h"
#if defined(USE_NSS_CERTS)
#include "net/cert_net/nss_ocsp.h"
#endif
#if defined(OS_LINUX) || defined(OS_MACOSX)
#include "net/cert/cert_net_fetcher.h"
#include "net/cert_net/cert_net_fetcher_impl.h"
#endif
#include "services/network/url_request_context_builder_mojo.h"
using content::BrowserThread;
namespace atom {
namespace {
network::mojom::HttpAuthStaticParamsPtr CreateHttpAuthStaticParams() {
network::mojom::HttpAuthStaticParamsPtr auth_static_params =
network::mojom::HttpAuthStaticParams::New();
auth_static_params->supported_schemes = {"basic", "digest", "ntlm",
"negotiate"};
return auth_static_params;
}
network::mojom::HttpAuthDynamicParamsPtr CreateHttpAuthDynamicParams(
const base::CommandLine& command_line) {
network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params =
network::mojom::HttpAuthDynamicParams::New();
auth_dynamic_params->server_whitelist =
command_line.GetSwitchValueASCII(switches::kAuthServerWhitelist);
auth_dynamic_params->delegate_whitelist = command_line.GetSwitchValueASCII(
switches::kAuthNegotiateDelegateWhitelist);
return auth_dynamic_params;
}
} // namespace
IOThread::IOThread(net_log::ChromeNetLog* net_log) : net_log_(net_log) {
IOThread::IOThread(net_log::ChromeNetLog* net_log,
SystemNetworkContextManager* system_network_context_manager)
: net_log_(net_log) {
BrowserThread::SetIOThreadDelegate(this);
system_network_context_manager->SetUp(
&network_context_request_, &network_context_params_,
&http_auth_static_params_, &http_auth_dynamic_params_);
}
IOThread::~IOThread() {
@ -63,56 +35,32 @@ IOThread::~IOThread() {
}
void IOThread::Init() {
std::unique_ptr<network::URLRequestContextBuilderMojo> builder =
std::make_unique<network::URLRequestContextBuilderMojo>();
auto cert_verifier = std::make_unique<net::CachingCertVerifier>(
std::make_unique<net::MultiThreadedCertVerifier>(
net::CertVerifyProc::CreateDefault()));
builder->SetCertVerifier(std::move(cert_verifier));
// Create the network service, so that shared host resolver
// gets created which is required to set the auth preferences below.
auto& command_line = *base::CommandLine::ForCurrentProcess();
auto* network_service = content::GetNetworkServiceImpl();
network_service->SetUpHttpAuth(CreateHttpAuthStaticParams());
network_service->ConfigureHttpAuthPrefs(
CreateHttpAuthDynamicParams(command_line));
network::NetworkService* network_service = content::GetNetworkServiceImpl();
network_service->SetUpHttpAuth(std::move(http_auth_static_params_));
network_service->ConfigureHttpAuthPrefs(std::move(http_auth_dynamic_params_));
net::URLRequestContextBuilder builder;
// TODO(deepak1556): We need to respoect user proxy configurations,
// the following initialization has to happen before any request
// contexts are utilized by the io thread, so that proper cert validation
// take place, solutions:
// 1) Use the request context from default partition, but since
// an app can completely run on a custom session without ever creating
// the default session, we will have to force create the default session
// in those scenarios.
// 2) Add a new api on app module that sets the proxy configuration
// for the global requests, like the cert fetchers below and
// geolocation requests.
// 3) There is also ongoing work in upstream which will eventually allow
// localizing these global fetchers to their own URLRequestContexts.
builder.set_proxy_resolution_service(
net::ProxyResolutionService::CreateDirect());
url_request_context_ = builder.Build();
url_request_context_getter_ = new net::TrivialURLRequestContextGetter(
url_request_context_.get(), base::ThreadTaskRunnerHandle::Get());
#if defined(USE_NSS_CERTS)
net::SetURLRequestContextForNSSHttpIO(url_request_context_.get());
#endif
#if defined(OS_LINUX) || defined(OS_MACOSX)
net::SetGlobalCertNetFetcher(
net::CreateCertNetFetcher(url_request_context_.get()));
#endif
system_network_context_ =
network_service
->CreateNetworkContextWithBuilder(std::move(network_context_request_),
std::move(network_context_params_),
std::move(builder),
&system_request_context_)
.release();
}
void IOThread::CleanUp() {
#if defined(USE_NSS_CERTS)
net::SetURLRequestContextForNSSHttpIO(nullptr);
#endif
#if defined(OS_LINUX) || defined(OS_MACOSX)
net::ShutdownGlobalCertNetFetcher();
#endif
// Explicitly release before the IO thread gets destroyed.
url_request_context_.reset();
url_request_context_getter_ = nullptr;
system_request_context_->proxy_resolution_service()->OnShutdown();
if (net_log_)
net_log_->ShutDownBeforeTaskScheduler();
}
} // namespace atom

View file

@ -7,30 +7,26 @@
#include <memory>
#include "atom/browser/net/system_network_context_manager.h"
#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "content/public/browser/browser_thread_delegate.h"
#include "services/network/public/mojom/network_service.mojom.h"
namespace net {
class URLRequestContext;
class URLRequestContextGetter;
} // namespace net
}
namespace net_log {
class ChromeNetLog;
}
namespace atom {
class IOThread : public content::BrowserThreadDelegate {
public:
explicit IOThread(net_log::ChromeNetLog* net_log);
explicit IOThread(
net_log::ChromeNetLog* net_log,
SystemNetworkContextManager* system_network_context_manager);
~IOThread() override;
net::URLRequestContextGetter* GetRequestContext() {
return url_request_context_getter_.get();
}
protected:
// BrowserThreadDelegate Implementation, runs on the IO thread.
void Init() override;
@ -40,12 +36,30 @@ class IOThread : public content::BrowserThreadDelegate {
// The NetLog is owned by the browser process, to allow logging from other
// threads during shutdown, but is used most frequently on the IOThread.
net_log::ChromeNetLog* net_log_;
std::unique_ptr<net::URLRequestContext> url_request_context_;
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
// When the network service is disabled, this holds on to a
// content::NetworkContext class that owns |system_request_context_|.
// TODO(deepak1556): primary network context has to be destroyed after
// other active contexts, but since the ownership of latter is not released
// before IO thread is destroyed, it results in a DCHECK failure.
// We leak the reference to primary context to workaround this issue,
// since there is only one instance for the entire lifetime of app, it is
// safe.
network::mojom::NetworkContext* system_network_context_;
net::URLRequestContext* system_request_context_;
// These are set on the UI thread, and then consumed during initialization on
// the IO thread.
network::mojom::NetworkContextRequest network_context_request_;
network::mojom::NetworkContextParamsPtr network_context_params_;
// Initial HTTP auth configuration used when setting up the NetworkService on
// the IO Thread. Future updates are sent using the NetworkService mojo
// interface, but initial state needs to be set non-racily.
network::mojom::HttpAuthStaticParamsPtr http_auth_static_params_;
network::mojom::HttpAuthDynamicParamsPtr http_auth_dynamic_params_;
DISALLOW_COPY_AND_ASSIGN(IOThread);
};
} // namespace atom
#endif // ATOM_BROWSER_IO_THREAD_H_

View file

@ -0,0 +1,232 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/net/system_network_context_manager.h"
#include <string>
#include <utility>
#include "atom/browser/io_thread.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/chrome_mojo_proxy_resolver_factory.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_features.h"
#include "content/public/common/service_names.mojom.h"
#include "mojo/public/cpp/bindings/associated_interface_ptr.h"
#include "net/net_buildflags.h"
#include "services/network/network_service.h"
#include "services/network/public/cpp/cross_thread_shared_url_loader_factory_info.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "url/gurl.h"
base::LazyInstance<SystemNetworkContextManager>::Leaky
g_system_network_context_manager = LAZY_INSTANCE_INITIALIZER;
namespace {
network::mojom::HttpAuthStaticParamsPtr CreateHttpAuthStaticParams() {
network::mojom::HttpAuthStaticParamsPtr auth_static_params =
network::mojom::HttpAuthStaticParams::New();
auth_static_params->supported_schemes = {"basic", "digest", "ntlm",
"negotiate"};
return auth_static_params;
}
network::mojom::HttpAuthDynamicParamsPtr CreateHttpAuthDynamicParams() {
auto* command_line = base::CommandLine::ForCurrentProcess();
network::mojom::HttpAuthDynamicParamsPtr auth_dynamic_params =
network::mojom::HttpAuthDynamicParams::New();
auth_dynamic_params->server_whitelist =
command_line->GetSwitchValueASCII(atom::switches::kAuthServerWhitelist);
auth_dynamic_params->delegate_whitelist = command_line->GetSwitchValueASCII(
atom::switches::kAuthNegotiateDelegateWhitelist);
return auth_dynamic_params;
}
} // namespace
// SharedURLLoaderFactory backed by a SystemNetworkContextManager and its
// network context. Transparently handles crashes.
class SystemNetworkContextManager::URLLoaderFactoryForSystem
: public network::SharedURLLoaderFactory {
public:
explicit URLLoaderFactoryForSystem(SystemNetworkContextManager* manager)
: manager_(manager) {}
// mojom::URLLoaderFactory implementation:
void CreateLoaderAndStart(network::mojom::URLLoaderRequest request,
int32_t routing_id,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& url_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag&
traffic_annotation) override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!manager_)
return;
manager_->GetURLLoaderFactory()->CreateLoaderAndStart(
std::move(request), routing_id, request_id, options, url_request,
std::move(client), traffic_annotation);
}
void Clone(network::mojom::URLLoaderFactoryRequest request) override {
if (!manager_)
return;
manager_->GetURLLoaderFactory()->Clone(std::move(request));
}
// SharedURLLoaderFactory implementation:
std::unique_ptr<network::SharedURLLoaderFactoryInfo> Clone() override {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return std::make_unique<network::CrossThreadSharedURLLoaderFactoryInfo>(
this);
}
void Shutdown() { manager_ = nullptr; }
private:
friend class base::RefCounted<URLLoaderFactoryForSystem>;
~URLLoaderFactoryForSystem() override {}
SystemNetworkContextManager* manager_;
DISALLOW_COPY_AND_ASSIGN(URLLoaderFactoryForSystem);
};
network::mojom::NetworkContext* SystemNetworkContextManager::GetContext() {
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
// SetUp should already have been called.
DCHECK(io_thread_network_context_);
return io_thread_network_context_.get();
}
if (!network_service_network_context_ ||
network_service_network_context_.encountered_error()) {
// This should call into OnNetworkServiceCreated(), which will re-create
// the network service, if needed. There's a chance that it won't be
// invoked, if the NetworkContext has encountered an error but the
// NetworkService has not yet noticed its pipe was closed. In that case,
// trying to create a new NetworkContext would fail, anyways, and hopefully
// a new NetworkContext will be created on the next GetContext() call.
content::GetNetworkService();
DCHECK(network_service_network_context_);
}
return network_service_network_context_.get();
}
network::mojom::URLLoaderFactory*
SystemNetworkContextManager::GetURLLoaderFactory() {
// Create the URLLoaderFactory as needed.
if (url_loader_factory_ && !url_loader_factory_.encountered_error()) {
return url_loader_factory_.get();
}
network::mojom::URLLoaderFactoryParamsPtr params =
network::mojom::URLLoaderFactoryParams::New();
params->process_id = network::mojom::kBrowserProcessId;
params->is_corb_enabled = false;
GetContext()->CreateURLLoaderFactory(mojo::MakeRequest(&url_loader_factory_),
std::move(params));
return url_loader_factory_.get();
}
scoped_refptr<network::SharedURLLoaderFactory>
SystemNetworkContextManager::GetSharedURLLoaderFactory() {
return shared_url_loader_factory_;
}
// static
network::mojom::NetworkContextParamsPtr
SystemNetworkContextManager::CreateDefaultNetworkContextParams() {
network::mojom::NetworkContextParamsPtr network_context_params =
network::mojom::NetworkContextParams::New();
network_context_params->enable_brotli =
base::FeatureList::IsEnabled(features::kBrotliEncoding);
network_context_params->enable_referrers = true;
network_context_params->proxy_resolver_factory =
ChromeMojoProxyResolverFactory::CreateWithStrongBinding().PassInterface();
return network_context_params;
}
void SystemNetworkContextManager::SetUp(
network::mojom::NetworkContextRequest* network_context_request,
network::mojom::NetworkContextParamsPtr* network_context_params,
network::mojom::HttpAuthStaticParamsPtr* http_auth_static_params,
network::mojom::HttpAuthDynamicParamsPtr* http_auth_dynamic_params) {
if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) {
*network_context_request = mojo::MakeRequest(&io_thread_network_context_);
*network_context_params = CreateNetworkContextParams();
} else {
// Just use defaults if the network service is enabled, since
// CreateNetworkContextParams() can only be called once.
*network_context_params = CreateDefaultNetworkContextParams();
}
*http_auth_static_params = CreateHttpAuthStaticParams();
*http_auth_dynamic_params = CreateHttpAuthDynamicParams();
}
SystemNetworkContextManager::SystemNetworkContextManager()
: proxy_config_monitor_(g_browser_process->local_state()) {
shared_url_loader_factory_ = new URLLoaderFactoryForSystem(this);
}
SystemNetworkContextManager::~SystemNetworkContextManager() {
shared_url_loader_factory_->Shutdown();
}
void SystemNetworkContextManager::OnNetworkServiceCreated(
network::mojom::NetworkService* network_service) {
if (!base::FeatureList::IsEnabled(network::features::kNetworkService))
return;
network_service->SetUpHttpAuth(CreateHttpAuthStaticParams());
network_service->ConfigureHttpAuthPrefs(CreateHttpAuthDynamicParams());
// The system NetworkContext must be created first, since it sets
// |primary_network_context| to true.
network_service->CreateNetworkContext(
MakeRequest(&network_service_network_context_),
CreateNetworkContextParams());
}
network::mojom::NetworkContextParamsPtr
SystemNetworkContextManager::CreateNetworkContextParams() {
// TODO(mmenke): Set up parameters here (in memory cookie store, etc).
network::mojom::NetworkContextParamsPtr network_context_params =
CreateDefaultNetworkContextParams();
network_context_params->context_name = std::string("system");
network_context_params->http_cache_enabled = false;
// These are needed for PAC scripts that use file or data URLs (Or FTP URLs?).
// TODO(crbug.com/839566): remove file support for all cases.
network_context_params->enable_data_url_support = true;
if (!base::FeatureList::IsEnabled(network::features::kNetworkService))
network_context_params->enable_file_url_support = true;
#if !BUILDFLAG(DISABLE_FTP_SUPPORT)
network_context_params->enable_ftp_url_support = true;
#endif
network_context_params->primary_network_context = true;
proxy_config_monitor_.AddToNetworkContextParams(network_context_params.get());
return network_context_params;
}

View file

@ -0,0 +1,104 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_NET_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
#define ATOM_BROWSER_NET_SYSTEM_NETWORK_CONTEXT_MANAGER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "chrome/browser/net/proxy_config_monitor.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
namespace network {
namespace mojom {
class URLLoaderFactory;
}
class SharedURLLoaderFactory;
} // namespace network
// Responsible for creating and managing access to the system NetworkContext.
// Lives on the UI thread. The NetworkContext this owns is intended for requests
// not associated with a session. It stores no data on disk, and has no HTTP
// cache, but it does have ephemeral cookie and channel ID stores.
//
// This class is also responsible for configuring global NetworkService state.
//
// The "system" NetworkContext will either share a URLRequestContext with
// IOThread's SystemURLRequestContext and be part of IOThread's NetworkService
// (If the network service is disabled) or be an independent NetworkContext
// using the actual network service.
class SystemNetworkContextManager {
public:
SystemNetworkContextManager();
~SystemNetworkContextManager();
// Returns default set of parameters for configuring the network service.
static network::mojom::NetworkContextParamsPtr
CreateDefaultNetworkContextParams();
// Initializes |network_context_params| as needed to set up a system
// NetworkContext. If the network service is disabled,
// |network_context_request| will be for the NetworkContext used by the
// SystemNetworkContextManager. Otherwise, this method can still be used to
// help set up the IOThread's in-process URLRequestContext.
//
// Must be called before the system NetworkContext is first used.
void SetUp(
network::mojom::NetworkContextRequest* network_context_request,
network::mojom::NetworkContextParamsPtr* network_context_params,
network::mojom::HttpAuthStaticParamsPtr* http_auth_static_params,
network::mojom::HttpAuthDynamicParamsPtr* http_auth_dynamic_params);
// Returns the System NetworkContext. May only be called after SetUp(). Does
// any initialization of the NetworkService that may be needed when first
// called.
network::mojom::NetworkContext* GetContext();
// Returns a URLLoaderFactory owned by the SystemNetworkContextManager that is
// backed by the SystemNetworkContext. Allows sharing of the URLLoaderFactory.
// Prefer this to creating a new one. Call Clone() on the value returned by
// this method to get a URLLoaderFactory that can be used on other threads.
network::mojom::URLLoaderFactory* GetURLLoaderFactory();
// Returns a SharedURLLoaderFactory owned by the SystemNetworkContextManager
// that is backed by the SystemNetworkContext.
scoped_refptr<network::SharedURLLoaderFactory> GetSharedURLLoaderFactory();
// Called when content creates a NetworkService. Creates the
// SystemNetworkContext, if the network service is enabled.
void OnNetworkServiceCreated(network::mojom::NetworkService* network_service);
private:
class URLLoaderFactoryForSystem;
// Creates parameters for the NetworkContext. May only be called once, since
// it initializes some class members.
network::mojom::NetworkContextParamsPtr CreateNetworkContextParams();
ProxyConfigMonitor proxy_config_monitor_;
// NetworkContext using the network service, if the network service is
// enabled. nullptr, otherwise.
network::mojom::NetworkContextPtr network_service_network_context_;
// This is a NetworkContext that wraps the IOThread's SystemURLRequestContext.
// Always initialized in SetUp, but it's only returned by Context() when the
// network service is disabled.
network::mojom::NetworkContextPtr io_thread_network_context_;
// URLLoaderFactory backed by the NetworkContext returned by GetContext(), so
// consumers don't all need to create their own factory.
scoped_refptr<URLLoaderFactoryForSystem> shared_url_loader_factory_;
network::mojom::URLLoaderFactoryPtr url_loader_factory_;
DISALLOW_COPY_AND_ASSIGN(SystemNetworkContextManager);
};
#endif // ATOM_BROWSER_NET_SYSTEM_NETWORK_CONTEXT_MANAGER_H_

View file

@ -11,6 +11,7 @@
#include "atom/browser/api/atom_api_protocol.h"
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/browser_process_impl.h"
#include "atom/browser/net/about_protocol_handler.h"
#include "atom/browser/net/asar/asar_protocol_handler.h"
#include "atom/browser/net/atom_cert_verifier.h"
@ -18,22 +19,18 @@
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "atom/browser/net/http_protocol_handler.h"
#include "atom/browser/net/require_ct_delegate.h"
#include "atom/browser/net/system_network_context_manager.h"
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/task_scheduler/post_task.h"
#include "chrome/browser/net/chrome_mojo_proxy_resolver_factory.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/prefs/value_map_pref_store.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_network_transaction_factory.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/resource_context.h"
#include "content/public/common/content_switches.h"
#include "net/base/host_mapping_rules.h"
#include "net/cert/multi_log_ct_verifier.h"
#include "net/cookies/cookie_monster.h"
@ -43,10 +40,6 @@
#include "net/http/http_auth_scheme.h"
#include "net/http/http_transaction_factory.h"
#include "net/log/net_log.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_config_service.h"
#include "net/proxy_resolution/proxy_config_with_annotation.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/data_protocol_handler.h"
#include "net/url_request/static_http_user_agent_settings.h"
@ -68,43 +61,6 @@ namespace atom {
namespace {
network::mojom::NetworkContextParamsPtr CreateDefaultNetworkContextParams(
const base::FilePath& base_path,
const std::string& user_agent,
bool in_memory,
bool use_cache,
int max_cache_size) {
network::mojom::NetworkContextParamsPtr network_context_params =
network::mojom::NetworkContextParams::New();
network_context_params->enable_brotli = true;
network_context_params->user_agent = user_agent;
network_context_params->http_cache_enabled = use_cache;
network_context_params->accept_language =
net::HttpUtil::GenerateAcceptLanguageHeader(
AtomBrowserClient::Get()->GetApplicationLocale());
network_context_params->enable_data_url_support = false;
network_context_params->proxy_resolver_factory =
ChromeMojoProxyResolverFactory::CreateWithStrongBinding().PassInterface();
if (!in_memory) {
network_context_params->http_cache_path =
base_path.Append(chrome::kCacheDirname);
network_context_params->http_cache_max_size = max_cache_size;
network_context_params->http_server_properties_path =
base_path.Append(chrome::kNetworkPersistentStateFilename);
network_context_params->cookie_path =
base_path.Append(chrome::kCookieFilename);
network_context_params->channel_id_path =
base_path.Append(chrome::kChannelIDFilename);
network_context_params->restore_old_session_cookies = false;
network_context_params->persist_session_cookies = false;
}
// TODO(deepak1556): Decide the stand on chrome ct policy and
// enable it.
// See //net/docs/certificate-transparency.md
// network_context_params->enforce_chrome_ct_policy = true;
return network_context_params;
}
void SetupAtomURLRequestJobFactory(
content::ProtocolHandlerMap* protocol_handlers,
net::URLRequestContext* url_request_context,
@ -143,39 +99,6 @@ void SetupAtomURLRequestJobFactory(
#endif
}
void ApplyProxyModeFromCommandLine(ValueMapPrefStore* pref_store) {
if (!pref_store)
return;
auto* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(::switches::kNoProxyServer)) {
pref_store->SetValue(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateDirect(),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
} else if (command_line->HasSwitch(::switches::kProxyPacUrl)) {
std::string pac_script_url =
command_line->GetSwitchValueASCII(::switches::kProxyPacUrl);
pref_store->SetValue(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreatePacScript(
pac_script_url, false /* pac_mandatory */),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
} else if (command_line->HasSwitch(::switches::kProxyAutoDetect)) {
pref_store->SetValue(proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateAutoDetect(),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
} else if (command_line->HasSwitch(::switches::kProxyServer)) {
std::string proxy_server =
command_line->GetSwitchValueASCII(::switches::kProxyServer);
std::string bypass_list =
command_line->GetSwitchValueASCII(::switches::kProxyBypassList);
pref_store->SetValue(
proxy_config::prefs::kProxy,
ProxyConfigDictionary::CreateFixedServers(proxy_server, bypass_list),
WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
}
} // namespace
class ResourceContext : public content::ResourceContext {
@ -235,21 +158,58 @@ URLRequestContextGetter::Handle::GetNetworkContext() {
return std::move(main_network_context_);
}
network::mojom::NetworkContextParamsPtr
URLRequestContextGetter::Handle::CreateNetworkContextParams() {
network::mojom::NetworkContextParamsPtr network_context_params =
SystemNetworkContextManager::CreateDefaultNetworkContextParams();
network_context_params->user_agent = browser_context_->GetUserAgent();
network_context_params->http_cache_enabled =
browser_context_->CanUseHttpCache();
network_context_params->accept_language =
net::HttpUtil::GenerateAcceptLanguageHeader(
AtomBrowserClient::Get()->GetApplicationLocale());
network_context_params->enable_data_url_support = false;
if (!browser_context_->IsOffTheRecord()) {
auto base_path = browser_context_->GetPath();
network_context_params->http_cache_path =
base_path.Append(chrome::kCacheDirname);
network_context_params->http_cache_max_size =
browser_context_->GetMaxCacheSize();
network_context_params->http_server_properties_path =
base_path.Append(chrome::kNetworkPersistentStateFilename);
network_context_params->cookie_path =
base_path.Append(chrome::kCookieFilename);
network_context_params->channel_id_path =
base_path.Append(chrome::kChannelIDFilename);
network_context_params->restore_old_session_cookies = false;
network_context_params->persist_session_cookies = false;
}
// TODO(deepak1556): Decide the stand on chrome ct policy and
// enable it.
// See //net/docs/certificate-transparency.md
// network_context_params->enforce_chrome_ct_policy = true;
return network_context_params;
}
void URLRequestContextGetter::Handle::LazyInitialize() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (initialized_)
return;
initialized_ = true;
main_network_context_params_ = CreateDefaultNetworkContextParams(
browser_context_->GetPath(), browser_context_->GetUserAgent(),
browser_context_->IsOffTheRecord(), browser_context_->CanUseHttpCache(),
browser_context_->GetMaxCacheSize());
main_network_context_params_ = CreateNetworkContextParams();
browser_context_->proxy_config_monitor()->AddToNetworkContextParams(
main_network_context_params_.get());
ApplyProxyModeFromCommandLine(browser_context_->in_memory_pref_store());
BrowserProcessImpl::ApplyProxyModeFromCommandLine(
browser_context_->in_memory_pref_store());
if (!main_network_context_request_.is_pending()) {
main_network_context_request_ = mojo::MakeRequest(&main_network_context_);

View file

@ -60,6 +60,7 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
content::ResourceContext* GetResourceContext();
scoped_refptr<URLRequestContextGetter> GetMainRequestContextGetter();
network::mojom::NetworkContextPtr GetNetworkContext();
network::mojom::NetworkContextParamsPtr CreateNetworkContextParams();
void ShutdownOnUIThread();

View file

@ -320,6 +320,8 @@ filenames = {
"atom/browser/net/require_ct_delegate.h",
"atom/browser/net/resolve_proxy_helper.cc",
"atom/browser/net/resolve_proxy_helper.h",
"atom/browser/net/system_network_context_manager.cc",
"atom/browser/net/system_network_context_manager.h",
"atom/browser/net/url_request_about_job.cc",
"atom/browser/net/url_request_about_job.h",
"atom/browser/net/url_request_async_asar_job.cc",