Skip to content

Commit

Permalink
Hook up ProfileIOData's URLRequestContext to a NetworkService.
Browse files Browse the repository at this point in the history
This CL adds ProfileNetworkContextService, a BrowserContext
KeyedService, that wraps ProfileIOData's URLRequestContext in a
NetworkContext (Which ProfileIOData hooks up to IOThread's in-process
NetworkService), and provides NetworkContext configuration parameters
for it as well.

When the network service is disabled, the new service provides direct
access to the net NetworkContext. When the network service is enabled,
the new service configures and provides acess to the StoragePartition's
NetworkContext instance instead (Though it still sets up a
NetworkContext for ProfileIOData's URLRequestContext as well).

Bug=715695

Review-Url: https://codereview.chromium.org/2976323002
Cr-Commit-Position: refs/heads/master@{#488893}
  • Loading branch information
mmenke authored and Commit Bot committed Jul 23, 2017
1 parent d475282 commit 0d1d09c
Show file tree
Hide file tree
Showing 25 changed files with 439 additions and 60 deletions.
4 changes: 4 additions & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ split_static_library("browser") {
"net/predictor_tab_helper.h",
"net/probe_message.cc",
"net/probe_message.h",
"net/profile_network_context_service.cc",
"net/profile_network_context_service.h",
"net/profile_network_context_service_factory.cc",
"net/profile_network_context_service_factory.h",
"net/proxy_service_factory.cc",
"net/proxy_service_factory.h",
"net/quota_policy_channel_id_store.cc",
Expand Down
18 changes: 18 additions & 0 deletions chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
#include "chrome/browser/memory/chrome_memory_coordinator_delegate.h"
#include "chrome/browser/metrics/chrome_browser_main_extra_parts_metrics.h"
#include "chrome/browser/nacl_host/nacl_browser_delegate_impl.h"
#include "chrome/browser/net/profile_network_context_service.h"
#include "chrome/browser/net/profile_network_context_service_factory.h"
#include "chrome/browser/net_benchmarking.h"
#include "chrome/browser/notifications/platform_notification_service_impl.h"
#include "chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.h"
Expand Down Expand Up @@ -2681,6 +2683,22 @@ ::rappor::RapporService* ChromeContentBrowserClient::GetRapporService() {
return g_browser_process->rappor_service();
}

content::mojom::NetworkContextPtr
ChromeContentBrowserClient::CreateNetworkContext(
content::BrowserContext* context,
bool in_memory,
const base::FilePath& relative_partition_path) {
if (relative_partition_path.empty()) {
return ProfileNetworkContextServiceFactory::GetForContext(context)
->CreateMainNetworkContext();
}
// TODO(mmenke): Implement this once ProfileNetworkContextServiceFactory can
// create a fully functional NetworkContext for Apps when the network service
// is disabled.
return ContentBrowserClient::CreateNetworkContext(context, in_memory,
relative_partition_path);
}

void ChromeContentBrowserClient::GetAdditionalFileSystemBackends(
content::BrowserContext* browser_context,
const base::FilePath& storage_partition_path,
Expand Down
5 changes: 5 additions & 0 deletions chrome/browser/chrome_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/network_service.mojom.h"
#include "extensions/features/features.h"
#include "media/media_features.h"
#include "ppapi/features/features.h"
Expand Down Expand Up @@ -320,6 +321,10 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient {
std::unique_ptr<content::MemoryCoordinatorDelegate>
GetMemoryCoordinatorDelegate() override;
::rappor::RapporService* GetRapporService() override;
content::mojom::NetworkContextPtr CreateNetworkContext(
content::BrowserContext* context,
bool in_memory,
const base::FilePath& relative_partition_path) override;

#if BUILDFLAG(ENABLE_MEDIA_REMOTING)
void CreateMediaRemoter(content::RenderFrameHost* render_frame_host,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/system_network_context_manager.h"

#include "base/test/scoped_feature_list.h"
#include "chrome/browser/net/profile_network_context_service.h"
#include "chrome/browser/net/profile_network_context_service_factory.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
Expand All @@ -27,35 +30,66 @@ enum class NetworkServiceState {
kEnabled,
};

class SystemNetworkContextManagerTest
enum class NetworkContextType {
kSystem,
kProfile,
kIncognitoProfile,
};

struct TestCase {
NetworkServiceState network_service_state;
NetworkContextType network_context_type;
};

// Tests the system, profile, and incognito profile NetworkContexts.
class NetworkContextConfigurationBrowserTest
: public InProcessBrowserTest,
public testing::WithParamInterface<NetworkServiceState> {
public testing::WithParamInterface<TestCase> {
public:
SystemNetworkContextManagerTest() {
NetworkContextConfigurationBrowserTest() {
EXPECT_TRUE(embedded_test_server()->Start());
}

~SystemNetworkContextManagerTest() override {}
~NetworkContextConfigurationBrowserTest() override {}

void SetUpInProcessBrowserTestFixture() override {
feature_list_.InitAndEnableFeature(features::kNetworkService);
}

void SetUpOnMainThread() override {
SystemNetworkContextManager::Context()->CreateURLLoaderFactory(
MakeRequest(&loader_factory_), 0);
switch (GetParam().network_context_type) {
case NetworkContextType::kSystem: {
network_context_ = SystemNetworkContextManager::Context();
break;
}
case NetworkContextType::kProfile: {
network_context_ = ProfileNetworkContextServiceFactory::GetInstance()
->GetForContext(browser()->profile())
->MainContext();
break;
}
case NetworkContextType::kIncognitoProfile: {
Browser* incognito = CreateIncognitoBrowser();
network_context_ = ProfileNetworkContextServiceFactory::GetInstance()
->GetForContext(incognito->profile())
->MainContext();
break;
}
}
network_context_->CreateURLLoaderFactory(MakeRequest(&loader_factory_), 0);
}

content::mojom::URLLoaderFactory* loader_factory() {
return loader_factory_.get();
}

private:
content::mojom::NetworkContext* network_context_ = nullptr;
content::mojom::URLLoaderFactoryPtr loader_factory_;
base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_P(SystemNetworkContextManagerTest, BasicRequest) {
IN_PROC_BROWSER_TEST_P(NetworkContextConfigurationBrowserTest, BasicRequest) {
content::mojom::URLLoaderAssociatedPtr loader;
content::ResourceRequest request;
content::TestURLLoaderClient client;
Expand Down Expand Up @@ -83,9 +117,27 @@ IN_PROC_BROWSER_TEST_P(SystemNetworkContextManagerTest, BasicRequest) {
}

INSTANTIATE_TEST_CASE_P(
/* no prefix */,
SystemNetworkContextManagerTest,
::testing::Values(NetworkServiceState::kDisabled,
NetworkServiceState::kEnabled));
SystemNetworkContext,
NetworkContextConfigurationBrowserTest,
::testing::Values(TestCase({NetworkServiceState::kDisabled,
NetworkContextType::kSystem}),
TestCase({NetworkServiceState::kEnabled,
NetworkContextType::kSystem})));

INSTANTIATE_TEST_CASE_P(
ProfileMainNetworkContext,
NetworkContextConfigurationBrowserTest,
::testing::Values(TestCase({NetworkServiceState::kDisabled,
NetworkContextType::kProfile}),
TestCase({NetworkServiceState::kEnabled,
NetworkContextType::kProfile})));

INSTANTIATE_TEST_CASE_P(
IncognitoProfileMainNetworkContext,
NetworkContextConfigurationBrowserTest,
::testing::Values(TestCase({NetworkServiceState::kDisabled,
NetworkContextType::kIncognitoProfile}),
TestCase({NetworkServiceState::kEnabled,
NetworkContextType::kIncognitoProfile})));

} // namespace
66 changes: 66 additions & 0 deletions chrome/browser/net/profile_network_context_service.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/profile_network_context_service.h"

#include "base/feature_list.h"
#include "base/logging.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/storage_partition.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"

namespace {

content::mojom::NetworkContextParamsPtr CreateMainNetworkContextParams() {
// TODO(mmenke): Set up parameters here.
return content::mojom::NetworkContextParams::New();
}

} // namespace

ProfileNetworkContextService::ProfileNetworkContextService(Profile* profile)
: profile_(profile) {}

ProfileNetworkContextService::~ProfileNetworkContextService() {}

void ProfileNetworkContextService::SetUpProfileIODataMainContext(
content::mojom::NetworkContextRequest* network_context_request,
content::mojom::NetworkContextParamsPtr* network_context_params) {
DCHECK(!profile_io_data_main_network_context_);
*network_context_request =
mojo::MakeRequest(&profile_io_data_main_network_context_);
if (!base::FeatureList::IsEnabled(features::kNetworkService)) {
*network_context_params = CreateMainNetworkContextParams();
} else {
// Just use default if network service is enabled, to avoid the legacy
// in-process URLRequestContext from fighting with the NetworkService over
// ownership of on-disk files.
*network_context_params = content::mojom::NetworkContextParams::New();
}
}

content::mojom::NetworkContext* ProfileNetworkContextService::MainContext() {
// ProfileIOData must be initialized before this call.
DCHECK(profile_io_data_main_network_context_);
if (!base::FeatureList::IsEnabled(features::kNetworkService))
return profile_io_data_main_network_context_.get();

return content::BrowserContext::GetDefaultStoragePartition(profile_)
->GetNetworkContext();
}

content::mojom::NetworkContextPtr
ProfileNetworkContextService::CreateMainNetworkContext() {
DCHECK(base::FeatureList::IsEnabled(features::kNetworkService));

content::mojom::NetworkContextPtr network_context;
content::GetNetworkService()->CreateNetworkContext(
MakeRequest(&network_context), CreateMainNetworkContextParams());
return network_context;
}
68 changes: 68 additions & 0 deletions chrome/browser/net/profile_network_context_service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_NET_PROFILE_NETWORK_CONTEXT_SERVICE_H_
#define CHROME_BROWSER_NET_PROFILE_NETWORK_CONTEXT_SERVICE_H_

#include "base/macros.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/common/network_service.mojom.h"

class Profile;

// KeyedService that initializes and provides access to the NetworkContexts for
// a Profile. This will eventually replace ProfileIOData.
class ProfileNetworkContextService : public KeyedService {
public:
explicit ProfileNetworkContextService(Profile* profile);
~ProfileNetworkContextService() override;

// Initializes |*network_context_params| to set up the ProfileIOData's
// main URLRequestContext and |*network_context_request| to be one end of a
// Mojo pipe to be bound to the NetworkContext for that URLRequestContext.
// The caller will need to send these parameters to the IOThread's in-process
// NetworkService. This class retains the NetworkContext at the other end of
// the |*network_context_request| pipe for later vending.
//
// If the network service is disabled, MainContext() will return that end of
// the pipe. In this case, all requests associated with this profile will use
// the associated URLRequestContext (either through MainContext() or
// directly).
//
// If the network service is enabled, MainContext() will instead return a
// network context vended by the network service's NetworkService (Instead of
// the IOThread's in-process one). In this case, the ProfileIOData's
// URLRequest context will be configured not to use on-disk storage (so as not
// to conflict with the network service vended context), and will only be used
// for legacy requests that use it directly.
//
// Must be called before anything uses the NetworkContext vended by this
// class.
void SetUpProfileIODataMainContext(
content::mojom::NetworkContextRequest* network_context_request,
content::mojom::NetworkContextParamsPtr* network_context_params);

// Returns the main NetworkContext for the BrowserContext. If the network
// service is disabled, this will be the
// ProfileIOData NetworkContext set up above. Otherwise, it will be a
// NetworkContext vended from the network service.
content::mojom::NetworkContext* MainContext();

// Creates the main NetworkContext for the BrowserContext, using the network
// service. May only be called when the network service is enabled. Must only
// be called once for a profile, from the ChromeContentBrowserClient.
content::mojom::NetworkContextPtr CreateMainNetworkContext();

private:
Profile* const profile_;

// This is a NetworkContext that wraps ProfileIOData's main URLRequestContext.
// Always initialized in SetUpProfileIODataMainContext, but it's only returned
// by Context() when the network service is disabled.
content::mojom::NetworkContextPtr profile_io_data_main_network_context_;

DISALLOW_COPY_AND_ASSIGN(ProfileNetworkContextService);
};

#endif // CHROME_BROWSER_NET_PROFILE_NETWORK_CONTEXT_SERVICE_H_
40 changes: 40 additions & 0 deletions chrome/browser/net/profile_network_context_service_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/net/profile_network_context_service_factory.h"

#include "chrome/browser/net/profile_network_context_service.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"

ProfileNetworkContextService*
ProfileNetworkContextServiceFactory::GetForContext(
content::BrowserContext* browser_context) {
return static_cast<ProfileNetworkContextService*>(
GetInstance()->GetServiceForBrowserContext(browser_context, true));
}

ProfileNetworkContextServiceFactory*
ProfileNetworkContextServiceFactory::GetInstance() {
return base::Singleton<ProfileNetworkContextServiceFactory>::get();
}

ProfileNetworkContextServiceFactory::ProfileNetworkContextServiceFactory()
: BrowserContextKeyedServiceFactory(
"ProfileNetworkContextService",
BrowserContextDependencyManager::GetInstance()) {}

ProfileNetworkContextServiceFactory::~ProfileNetworkContextServiceFactory() {}

KeyedService* ProfileNetworkContextServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* profile) const {
return new ProfileNetworkContextService(Profile::FromBrowserContext(profile));
}

content::BrowserContext*
ProfileNetworkContextServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
// Create separate service for incognito profiles.
return context;
}
Loading

0 comments on commit 0d1d09c

Please sign in to comment.