Commit ec81befc authored by Robbie McElrath's avatar Robbie McElrath Committed by Commit Bot

Make SigninPartitionManager work with the network service enabled.

This makes the HttpAuthCache copying in SigninPartitionManager work
when the network service is enabled.

Bug: 838942
Change-Id: I679885ad0b9f8ca3fada00b777f5a955ba7b4d0f
Reviewed-on: https://chromium-review.googlesource.com/c/1368949
Commit-Queue: Robbie McElrath <rmcelrath@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#615768}
parent bb47b546
......@@ -5,29 +5,18 @@
#include "chrome/browser/chromeos/login/signin_partition_manager.h"
#include "base/guid.h"
#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chromeos/chromeos_switches.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "net/base/escape.h"
#include "net/http/http_auth_cache.h"
#include "net/http/http_network_session.h"
#include "net/http/http_transaction_factory.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace chromeos {
namespace login {
......@@ -56,33 +45,27 @@ void ClearStoragePartition(content::StoragePartition* storage_partition,
base::Time(), base::Time::Max(), std::move(partition_data_cleared));
}
net::URLRequestContextGetter* GetSystemURLRequestContextGetter() {
return g_browser_process->system_request_context();
network::mojom::NetworkContext* GetSystemNetworkContext() {
return g_browser_process->system_network_context_manager()->GetContext();
}
// Transfers HttpAuthCache content from |main_url_request_context_getter| into
// |signin_url_request_context_getter|.
void PrepareSigninURLRequestContextOnIOThread(
net::URLRequestContextGetter* main_url_request_context_getter,
net::URLRequestContextGetter* signin_url_request_context_getter) {
// Transfer proxy auth data from the main URLRequestContext.
net::HttpAuthCache* main_http_auth_cache =
main_url_request_context_getter->GetURLRequestContext()
->http_transaction_factory()
->GetSession()
->http_auth_cache();
net::HttpAuthCache* signin_http_auth_cache =
signin_url_request_context_getter->GetURLRequestContext()
->http_transaction_factory()
->GetSession()
->http_auth_cache();
signin_http_auth_cache->UpdateAllFrom(*main_http_auth_cache);
// Copies the HttpAuthCache with key |cache_key| into
// |signin_storage_partition|'s NetworkContext.
void LoadHttpAuthCache(content::StoragePartition* signin_storage_partition,
base::OnceClosure completion_callback,
const base::UnguessableToken& cache_key) {
signin_storage_partition->GetNetworkContext()->LoadHttpAuthCache(
cache_key, std::move(completion_callback));
}
void InvokeStartSigninSessionDoneCallback(
SigninPartitionManager::StartSigninSessionDoneCallback callback,
const std::string& partition_name) {
std::move(callback).Run(partition_name);
// Transfers HttpAuthCache content from |main_network_context| into
// |signin_storage_partition|'s NetworkContext.
void TransferHttpAuthCache(network::mojom::NetworkContext* main_network_context,
content::StoragePartition* signin_storage_partition,
base::OnceClosure completion_callback) {
main_network_context->SaveHttpAuthCache(base::BindOnce(
&LoadHttpAuthCache, base::Unretained(signin_storage_partition),
std::move(completion_callback)));
}
} // namespace
......@@ -92,16 +75,17 @@ SigninPartitionManager::SigninPartitionManager(
: browser_context_(browser_context),
clear_storage_partition_task_(
base::BindRepeating(&ClearStoragePartition)),
get_system_url_request_context_getter_task_(
base::BindRepeating(&GetSystemURLRequestContextGetter)) {}
get_system_network_context_task_(
base::BindRepeating(&GetSystemNetworkContext)) {}
SigninPartitionManager::~SigninPartitionManager() {}
void SigninPartitionManager::StartSigninSession(
content::WebContents* embedder_web_contents,
StartSigninSessionDoneCallback signin_session_started) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// If we already were in a sign-in session, close it first.
// This clears stale data from the last-used StorageParittion.
// This clears stale data from the last-used StoragePartition.
CloseCurrentSigninSession(base::DoNothing());
// The storage partition domain identifies the site embedding the webview. It
......@@ -117,18 +101,10 @@ void SigninPartitionManager::StartSigninSession(
content::BrowserContext::GetStoragePartitionForSite(browser_context_,
guest_site, true);
base::OnceClosure invoke_callback = base::BindOnce(
&InvokeStartSigninSessionDoneCallback, std::move(signin_session_started),
current_storage_partition_name_);
base::PostTaskWithTraitsAndReply(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(
&PrepareSigninURLRequestContextOnIOThread,
base::RetainedRef(get_system_url_request_context_getter_task_.Run()),
base::RetainedRef(
current_storage_partition_->GetURLRequestContext())),
std::move(invoke_callback));
TransferHttpAuthCache(get_system_network_context_task_.Run(),
current_storage_partition_,
base::BindOnce(std::move(signin_session_started),
current_storage_partition_name_));
}
void SigninPartitionManager::CloseCurrentSigninSession(
......@@ -152,11 +128,9 @@ void SigninPartitionManager::SetClearStoragePartitionTaskForTesting(
clear_storage_partition_task_ = clear_storage_partition_task;
}
void SigninPartitionManager::SetGetSystemURLRequestContextGetterTaskForTesting(
GetSystemURLRequestContextGetterTask
get_system_url_request_context_getter_task) {
get_system_url_request_context_getter_task_ =
get_system_url_request_context_getter_task;
void SigninPartitionManager::SetGetSystemNetworkContextForTesting(
GetSystemNetworkContextTask get_system_network_context_task) {
get_system_network_context_task_ = get_system_network_context_task;
}
const std::string& SigninPartitionManager::GetCurrentStoragePartitionName()
......
......@@ -20,9 +20,11 @@ class StoragePartition;
class WebContents;
} // namespace content
namespace net {
class URLRequestContextGetter;
}
namespace network {
namespace mojom {
class NetworkContext;
} // namespace mojom
} // namespace network
namespace chromeos {
namespace login {
......@@ -35,8 +37,8 @@ class SigninPartitionManager : public KeyedService {
base::RepeatingCallback<void(content::StoragePartition* storage_partition,
base::OnceClosure data_cleared)>;
using GetSystemURLRequestContextGetterTask =
base::RepeatingCallback<net::URLRequestContextGetter*()>;
using GetSystemNetworkContextTask =
base::RepeatingCallback<network::mojom::NetworkContext*()>;
using StartSigninSessionDoneCallback =
base::OnceCallback<void(const std::string& partition_name)>;
......@@ -50,7 +52,7 @@ class SigninPartitionManager : public KeyedService {
// |embedder_web_contents| is the WebContents instance embedding the webview
// which will display the sign-in pages.
// |signin_session_started| will be invoked with the partition name of the
// started signin session on completition.
// started signin session on completion.
void StartSigninSession(
content::WebContents* embedder_web_contents,
StartSigninSessionDoneCallback signin_session_started);
......@@ -81,9 +83,8 @@ class SigninPartitionManager : public KeyedService {
void SetClearStoragePartitionTaskForTesting(
ClearStoragePartitionTask clear_storage_partition_task);
void SetGetSystemURLRequestContextGetterTaskForTesting(
GetSystemURLRequestContextGetterTask
get_system_url_request_context_getter_task);
void SetGetSystemNetworkContextForTesting(
GetSystemNetworkContextTask get_system_network_context_task);
class Factory : public BrowserContextKeyedServiceFactory {
public:
......@@ -111,8 +112,7 @@ class SigninPartitionManager : public KeyedService {
content::BrowserContext* const browser_context_;
ClearStoragePartitionTask clear_storage_partition_task_;
GetSystemURLRequestContextGetterTask
get_system_url_request_context_getter_task_;
GetSystemNetworkContextTask get_system_network_context_task_;
// GuestView StoragePartitions use the host of the embedder site's URL as the
// domain of their StoragePartition.
......
......@@ -22,9 +22,10 @@
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "net/cookies/cookie_store.h"
#include "net/http/http_transaction_factory.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/network_context.h"
#include "services/network/network_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
......@@ -41,11 +42,9 @@ void StorePartitionNameAndQuitLoop(base::RunLoop* loop,
loop->Quit();
}
void AddEntryToHttpAuthCache(
net::URLRequestContextGetter* url_request_context_getter) {
void AddEntryToHttpAuthCache(net::URLRequestContext* url_request_context) {
net::HttpAuthCache* http_auth_cache =
url_request_context_getter->GetURLRequestContext()
->http_transaction_factory()
url_request_context->http_transaction_factory()
->GetSession()
->http_auth_cache();
http_auth_cache->Add(GURL("http://whatever.com/"), "",
......@@ -76,15 +75,19 @@ class SigninPartitionManagerTest : public ChromeRenderViewHostTestHarness {
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
system_request_context_getter_ = new net::TestURLRequestContextGetter(
base::CreateSingleThreadTaskRunnerWithTraits(
{content::BrowserThread::IO}));
signin_browser_context_ = std::make_unique<TestingProfile>();
signin_ui_web_contents_ = content::WebContentsTester::CreateTestWebContents(
GetSigninProfile(), content::SiteInstance::Create(GetSigninProfile()));
// Let Profile creation finish, which creates the NetworkService instance.
base::RunLoop().RunUntilIdle();
network::mojom::NetworkContextParamsPtr params =
network::mojom::NetworkContextParams::New();
system_network_context_ = std::make_unique<network::NetworkContext>(
network::NetworkService::GetNetworkServiceForTesting(),
mojo::MakeRequest(&system_network_context_ptr_), std::move(params));
GURL url(kEmbedderUrl);
content::WebContentsTester::For(signin_ui_web_contents())
->NavigateAndCommit(url);
......@@ -92,9 +95,9 @@ class SigninPartitionManagerTest : public ChromeRenderViewHostTestHarness {
GetSigninPartitionManager()->SetClearStoragePartitionTaskForTesting(
base::Bind(&SigninPartitionManagerTest::ClearStoragePartitionTask,
base::Unretained(this)));
GetSigninPartitionManager()
->SetGetSystemURLRequestContextGetterTaskForTesting(base::BindRepeating(
&SigninPartitionManagerTest::GetSystemURLRequestContextGetter,
GetSigninPartitionManager()->SetGetSystemNetworkContextForTesting(
base::BindRepeating(
&SigninPartitionManagerTest::GetSystemNetworkContext,
base::Unretained(this)));
}
......@@ -103,11 +106,6 @@ class SigninPartitionManagerTest : public ChromeRenderViewHostTestHarness {
signin_browser_context_.reset();
// ChromeRenderViewHostTestHarness::TearDown() simulates shutdown and
// ~URLRequestContextGetter() assumes BrowserThreads are still up so this
// must happen first.
system_request_context_getter_ = nullptr;
ChromeRenderViewHostTestHarness::TearDown();
}
......@@ -149,8 +147,12 @@ class SigninPartitionManagerTest : public ChromeRenderViewHostTestHarness {
return partition_name;
}
net::URLRequestContextGetter* GetSystemURLRequestContextGetter() {
return system_request_context_getter_.get();
net::URLRequestContext* GetSystemURLRequestContext() {
return system_network_context_->url_request_context();
}
network::mojom::NetworkContext* GetSystemNetworkContext() {
return system_network_context_.get();
}
private:
......@@ -159,8 +161,8 @@ class SigninPartitionManagerTest : public ChromeRenderViewHostTestHarness {
pending_clear_tasks_.push_back({partition, std::move(clear_done_closure)});
}
scoped_refptr<net::TestURLRequestContextGetter>
system_request_context_getter_;
std::unique_ptr<network::NetworkContext> system_network_context_;
network::mojom::NetworkContextPtr system_network_context_ptr_;
std::unique_ptr<TestingProfile> signin_browser_context_;
......@@ -222,7 +224,7 @@ TEST_F(SigninPartitionManagerTest, HttpAuthCacheTransferred) {
base::PostTaskWithTraitsAndReply(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(AddEntryToHttpAuthCache,
base::RetainedRef(GetSystemURLRequestContextGetter())),
base::Unretained(GetSystemURLRequestContext())),
loop_prepare.QuitClosure());
loop_prepare.Run();
......
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