Commit 8caab1dc authored by Min Qin's avatar Min Qin Committed by Commit Bot

Add static method to allow creating global SystemNetworkContextManager on demand

When running network service without launching full browser process,
ChromeContentBrowserClient::OnNetworkServiceCreated() will be called
without g_browser_process. This CL allows us to create
SystemNetworkContextManager on demand and services depending on the
network service can call SystemNetworkContextManager::GetInstance()
to access it.

BUG=866028

Change-Id: I9a557921e1bb164ecd62c766dd698137498c0a47
Reviewed-on: https://chromium-review.googlesource.com/1256207
Commit-Queue: Min Qin <qinmin@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596269}
parent dbd8eb77
...@@ -440,7 +440,7 @@ void BrowserProcessImpl::StartTearDown() { ...@@ -440,7 +440,7 @@ void BrowserProcessImpl::StartTearDown() {
local_state_->CommitPendingWrite(); local_state_->CommitPendingWrite();
// This expects to be destroyed before the task scheduler is torn down. // This expects to be destroyed before the task scheduler is torn down.
system_network_context_manager_.reset(); SystemNetworkContextManager::DeleteInstance();
} }
void BrowserProcessImpl::PostDestroyThreads() { void BrowserProcessImpl::PostDestroyThreads() {
...@@ -643,8 +643,8 @@ IOThread* BrowserProcessImpl::io_thread() { ...@@ -643,8 +643,8 @@ IOThread* BrowserProcessImpl::io_thread() {
SystemNetworkContextManager* SystemNetworkContextManager*
BrowserProcessImpl::system_network_context_manager() { BrowserProcessImpl::system_network_context_manager() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(system_network_context_manager_.get()); DCHECK(SystemNetworkContextManager::GetInstance());
return system_network_context_manager_.get(); return SystemNetworkContextManager::GetInstance();
} }
scoped_refptr<network::SharedURLLoaderFactory> scoped_refptr<network::SharedURLLoaderFactory>
...@@ -1173,12 +1173,12 @@ void BrowserProcessImpl::PreCreateThreads( ...@@ -1173,12 +1173,12 @@ void BrowserProcessImpl::PreCreateThreads(
// Must be created before the IOThread. // Must be created before the IOThread.
// TODO(mmenke): Once IOThread class is no longer needed (not the thread // TODO(mmenke): Once IOThread class is no longer needed (not the thread
// itself), this can be created on first use. // itself), this can be created on first use.
system_network_context_manager_ = if (!SystemNetworkContextManager::GetInstance())
std::make_unique<SystemNetworkContextManager>(local_state_.get()); SystemNetworkContextManager::CreateInstance(local_state_.get());
io_thread_ = std::make_unique<IOThread>( io_thread_ = std::make_unique<IOThread>(
local_state(), policy_service(), net_log_.get(), local_state(), policy_service(), net_log_.get(),
extension_event_router_forwarder(), extension_event_router_forwarder(),
system_network_context_manager_.get()); SystemNetworkContextManager::GetInstance());
} }
void BrowserProcessImpl::PreMainMessageLoopRun() { void BrowserProcessImpl::PreMainMessageLoopRun() {
......
...@@ -133,6 +133,8 @@ class BrowserProcessImpl : public BrowserProcess, ...@@ -133,6 +133,8 @@ class BrowserProcessImpl : public BrowserProcess,
metrics::MetricsService* metrics_service() override; metrics::MetricsService* metrics_service() override;
rappor::RapporServiceImpl* rappor_service() override; rappor::RapporServiceImpl* rappor_service() override;
IOThread* io_thread() override; IOThread* io_thread() override;
// TODO(qinmin): Remove this method as callers can retrieve the global
// instance from SystemNetworkContextManager directly.
SystemNetworkContextManager* system_network_context_manager() override; SystemNetworkContextManager* system_network_context_manager() override;
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory() scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory()
override; override;
...@@ -256,8 +258,6 @@ class BrowserProcessImpl : public BrowserProcess, ...@@ -256,8 +258,6 @@ class BrowserProcessImpl : public BrowserProcess,
std::unique_ptr<PrefService> local_state_; std::unique_ptr<PrefService> local_state_;
std::unique_ptr<SystemNetworkContextManager> system_network_context_manager_;
std::unique_ptr<network::NetworkQualityTracker> network_quality_tracker_; std::unique_ptr<network::NetworkQualityTracker> network_quality_tracker_;
// Listens to NetworkQualityTracker and sends network quality updates to the // Listens to NetworkQualityTracker and sends network quality updates to the
......
...@@ -4583,8 +4583,16 @@ void ChromeContentBrowserClient::WillCreateWebSocket( ...@@ -4583,8 +4583,16 @@ void ChromeContentBrowserClient::WillCreateWebSocket(
void ChromeContentBrowserClient::OnNetworkServiceCreated( void ChromeContentBrowserClient::OnNetworkServiceCreated(
network::mojom::NetworkService* network_service) { network::mojom::NetworkService* network_service) {
if (!base::FeatureList::IsEnabled(network::features::kNetworkService))
return;
if (!SystemNetworkContextManager::GetInstance()) {
DCHECK(!g_browser_process);
DCHECK(chrome_feature_list_creator_->simple_local_state());
SystemNetworkContextManager::CreateInstance(
chrome_feature_list_creator_->simple_local_state());
}
// Need to set up global NetworkService state before anything else uses it. // Need to set up global NetworkService state before anything else uses it.
g_browser_process->system_network_context_manager()->OnNetworkServiceCreated( SystemNetworkContextManager::GetInstance()->OnNetworkServiceCreated(
network_service); network_service);
} }
......
...@@ -28,6 +28,8 @@ class ChromeFeatureListCreator { ...@@ -28,6 +28,8 @@ class ChromeFeatureListCreator {
void CreatePrefServiceForTesting(); void CreatePrefServiceForTesting();
PrefService* simple_local_state() { return simple_local_state_.get(); }
private: private:
void CreatePrefService(); void CreatePrefService();
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/metrics/field_trial_params.h" #include "base/metrics/field_trial_params.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
...@@ -75,6 +74,9 @@ ...@@ -75,6 +74,9 @@
namespace { namespace {
// The global instance of the SystemNetworkContextmanager.
SystemNetworkContextManager* g_system_network_context_manager = nullptr;
// Called on IOThread to disable QUIC for HttpNetworkSessions not using the // Called on IOThread to disable QUIC for HttpNetworkSessions not using the
// network service. Note that re-enabling QUIC dynamically is not supported for // network service. Note that re-enabling QUIC dynamically is not supported for
// simpliciy and requires a browser restart. // simpliciy and requires a browser restart.
...@@ -223,9 +225,6 @@ bool ShouldEnableAsyncDns() { ...@@ -223,9 +225,6 @@ bool ShouldEnableAsyncDns() {
} // namespace } // namespace
base::LazyInstance<SystemNetworkContextManager>::Leaky
g_system_network_context_manager = LAZY_INSTANCE_INITIALIZER;
// SharedURLLoaderFactory backed by a SystemNetworkContextManager and its // SharedURLLoaderFactory backed by a SystemNetworkContextManager and its
// network context. Transparently handles crashes. // network context. Transparently handles crashes.
class SystemNetworkContextManager::URLLoaderFactoryForSystem class SystemNetworkContextManager::URLLoaderFactoryForSystem
...@@ -342,6 +341,26 @@ void SystemNetworkContextManager::SetUp( ...@@ -342,6 +341,26 @@ void SystemNetworkContextManager::SetUp(
dns_over_https_servers); dns_over_https_servers);
} }
// static
SystemNetworkContextManager* SystemNetworkContextManager::CreateInstance(
PrefService* pref_service) {
DCHECK(!g_system_network_context_manager);
g_system_network_context_manager =
new SystemNetworkContextManager(pref_service);
return g_system_network_context_manager;
}
// static
SystemNetworkContextManager* SystemNetworkContextManager::GetInstance() {
return g_system_network_context_manager;
}
// static
void SystemNetworkContextManager::DeleteInstance() {
DCHECK(g_system_network_context_manager);
delete g_system_network_context_manager;
}
SystemNetworkContextManager::SystemNetworkContextManager( SystemNetworkContextManager::SystemNetworkContextManager(
PrefService* local_state) PrefService* local_state)
: local_state_(local_state), : local_state_(local_state),
......
...@@ -49,10 +49,18 @@ class SharedURLLoaderFactory; ...@@ -49,10 +49,18 @@ class SharedURLLoaderFactory;
// to being compatible with the network service. // to being compatible with the network service.
class SystemNetworkContextManager { class SystemNetworkContextManager {
public: public:
// Constructor. |pref_service| must out live this object.
explicit SystemNetworkContextManager(PrefService* pref_service);
~SystemNetworkContextManager(); ~SystemNetworkContextManager();
// Creates the global instance of SystemNetworkContextManager. If an
// instance already exists, this will cause a DCHECK failure.
static SystemNetworkContextManager* CreateInstance(PrefService* pref_service);
// Gets the global SystemNetworkContextManager instance.
static SystemNetworkContextManager* GetInstance();
// Destroys the global SystemNetworkContextManager instance.
static void DeleteInstance();
static void RegisterPrefs(PrefRegistrySimple* registry); static void RegisterPrefs(PrefRegistrySimple* registry);
// Initializes |network_context_params| as needed to set up a system // Initializes |network_context_params| as needed to set up a system
...@@ -140,6 +148,9 @@ class SystemNetworkContextManager { ...@@ -140,6 +148,9 @@ class SystemNetworkContextManager {
private: private:
class URLLoaderFactoryForSystem; class URLLoaderFactoryForSystem;
// Constructor. |pref_service| must out live this object.
explicit SystemNetworkContextManager(PrefService* pref_service);
void UpdateReferrersEnabled(); void UpdateReferrersEnabled();
// Creates parameters for the NetworkContext. May only be called once, since // Creates parameters for the NetworkContext. May only be called once, since
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment