Commit 6bbdd0a4 authored by Marton Hunyady's avatar Marton Hunyady Committed by Commit Bot

Support using mirror account consistency on local new tab page.

Mirror account consistency can be required by an enterprise policy
or in case the signed in user is a child account.
This is done by adding the
"X-Chrome-Connected: enable_account_consistency=true" header to the
request. For regular requests, this is done by
chrome/browser/signin/chrome_signin_helper.cc's
FixAccountConsistencyRequestHeader, but the local new tab page uses
a different code path.

Bug: 790970
Change-Id: I81c86075c2b7488639beb4e919d429d53411c6fb
Reviewed-on: https://chromium-review.googlesource.com/879141
Commit-Queue: Marton Hunyady <hunyadym@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532839}
parent 4632ec08
......@@ -16,6 +16,8 @@
#include "chrome/common/chrome_content_client.h"
#include "components/google/core/browser/google_url_tracker.h"
#include "components/google/core/browser/google_util.h"
#include "components/signin/core/browser/chrome_connected_header_helper.h"
#include "components/signin/core/browser/signin_header_helper.h"
#include "components/variations/net/variations_http_headers.h"
#include "content/public/common/service_manager_connection.h"
#include "net/base/load_flags.h"
......@@ -135,6 +137,7 @@ class OneGoogleBarFetcherImpl::AuthenticatedURLFetcher
const GURL& google_base_url,
const std::string& application_locale,
const base::Optional<std::string>& api_url_override,
bool account_consistency_mirror_required,
FetchDoneCallback callback);
~AuthenticatedURLFetcher() override = default;
......@@ -151,6 +154,9 @@ class OneGoogleBarFetcherImpl::AuthenticatedURLFetcher
const GURL google_base_url_;
const std::string application_locale_;
const base::Optional<std::string> api_url_override_;
#if defined(OS_CHROMEOS)
const bool account_consistency_mirror_required_;
#endif
FetchDoneCallback callback_;
......@@ -163,11 +169,15 @@ OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::AuthenticatedURLFetcher(
const GURL& google_base_url,
const std::string& application_locale,
const base::Optional<std::string>& api_url_override,
bool account_consistency_mirror_required,
FetchDoneCallback callback)
: request_context_(request_context),
google_base_url_(google_base_url),
application_locale_(application_locale),
api_url_override_(api_url_override),
#if defined(OS_CHROMEOS)
account_consistency_mirror_required_(account_consistency_mirror_required),
#endif
callback_(std::move(callback)) {}
GURL OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetApiUrl() const {
......@@ -193,6 +203,27 @@ OneGoogleBarFetcherImpl::AuthenticatedURLFetcher::GetExtraRequestHeaders(
// transmission of experiments coming from the variations server.
variations::AppendVariationHeaders(url, variations::InIncognito::kNo,
variations::SignedIn::kNo, &headers);
#if defined(OS_CHROMEOS)
signin::ChromeConnectedHeaderHelper chrome_connected_header_helper(
account_consistency_mirror_required_);
int profile_mode = signin::PROFILE_MODE_DEFAULT;
if (account_consistency_mirror_required_) {
// For the child account case (where currently
// |account_consistency_mirror_required_| is true on Chrome OS), we always
// want to disable adding an account and going to incognito.
profile_mode = signin::PROFILE_MODE_INCOGNITO_DISABLED |
signin::PROFILE_MODE_ADD_ACCOUNT_DISABLED;
}
std::string chrome_connected_header_value =
chrome_connected_header_helper.BuildRequestHeader(
/*is_header_request=*/true, url,
// Account ID is only needed for (drive|docs).google.com.
/*account_id=*/std::string(), profile_mode);
if (!chrome_connected_header_value.empty()) {
headers.SetHeader(signin::kChromeConnectedHeader,
chrome_connected_header_value);
}
#endif
return headers.ToString();
}
......@@ -241,11 +272,13 @@ OneGoogleBarFetcherImpl::OneGoogleBarFetcherImpl(
net::URLRequestContextGetter* request_context,
GoogleURLTracker* google_url_tracker,
const std::string& application_locale,
const base::Optional<std::string>& api_url_override)
const base::Optional<std::string>& api_url_override,
bool account_consistency_mirror_required)
: request_context_(request_context),
google_url_tracker_(google_url_tracker),
application_locale_(application_locale),
api_url_override_(api_url_override),
account_consistency_mirror_required_(account_consistency_mirror_required),
weak_ptr_factory_(this) {}
OneGoogleBarFetcherImpl::~OneGoogleBarFetcherImpl() = default;
......@@ -263,6 +296,7 @@ void OneGoogleBarFetcherImpl::Fetch(OneGoogleCallback callback) {
// the result obsolete.
pending_request_ = base::MakeUnique<AuthenticatedURLFetcher>(
request_context_, google_base_url, application_locale_, api_url_override_,
account_consistency_mirror_required_,
base::BindOnce(&OneGoogleBarFetcherImpl::FetchDone,
base::Unretained(this)));
pending_request_->Start();
......
......@@ -37,7 +37,8 @@ class OneGoogleBarFetcherImpl : public OneGoogleBarFetcher {
OneGoogleBarFetcherImpl(net::URLRequestContextGetter* request_context,
GoogleURLTracker* google_url_tracker,
const std::string& application_locale,
const base::Optional<std::string>& api_url_override);
const base::Optional<std::string>& api_url_override,
bool account_consistency_mirror_required);
~OneGoogleBarFetcherImpl() override;
void Fetch(OneGoogleCallback callback) override;
......@@ -56,6 +57,7 @@ class OneGoogleBarFetcherImpl : public OneGoogleBarFetcher {
GoogleURLTracker* google_url_tracker_;
const std::string application_locale_;
const base::Optional<std::string> api_url_override_;
const bool account_consistency_mirror_required_;
std::vector<OneGoogleCallback> callbacks_;
std::unique_ptr<AuthenticatedURLFetcher> pending_request_;
......
......@@ -15,6 +15,7 @@
#include "base/time/time.h"
#include "chrome/browser/search/one_google_bar/one_google_bar_data.h"
#include "components/google/core/browser/google_url_tracker.h"
#include "components/signin/core/browser/signin_header_helper.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_service_manager_context.h"
#include "net/http/http_request_headers.h"
......@@ -59,10 +60,24 @@ class GoogleURLTrackerClientStub : public GoogleURLTrackerClient {
class OneGoogleBarFetcherImplTest : public testing::Test {
public:
OneGoogleBarFetcherImplTest() : OneGoogleBarFetcherImplTest(base::nullopt) {}
OneGoogleBarFetcherImplTest()
: OneGoogleBarFetcherImplTest(
/*api_url_override=*/base::nullopt,
/*account_consistency_mirror_required=*/false) {}
OneGoogleBarFetcherImplTest(
explicit OneGoogleBarFetcherImplTest(
const base::Optional<std::string>& api_url_override)
: OneGoogleBarFetcherImplTest(
api_url_override,
/*account_consistency_mirror_required=*/false) {}
explicit OneGoogleBarFetcherImplTest(bool account_consistency_mirror_required)
: OneGoogleBarFetcherImplTest(/*api_url_override=*/base::nullopt,
account_consistency_mirror_required) {}
OneGoogleBarFetcherImplTest(
const base::Optional<std::string>& api_url_override,
bool account_consistency_mirror_required)
: task_runner_(new base::TestSimpleTaskRunner()),
request_context_getter_(
new net::TestURLRequestContextGetter(task_runner_)),
......@@ -71,7 +86,8 @@ class OneGoogleBarFetcherImplTest : public testing::Test {
one_google_bar_fetcher_(request_context_getter_.get(),
&google_url_tracker_,
kApplicationLocale,
api_url_override) {}
api_url_override,
account_consistency_mirror_required) {}
net::TestURLFetcher* GetRunningURLFetcher() {
// All created URLFetchers have ID 0 by default.
......@@ -292,6 +308,58 @@ TEST_F(OneGoogleBarFetcherImplTest, IncompleteJsonErrorIsFatal) {
}}})json");
}
TEST_F(OneGoogleBarFetcherImplTest, MirrorAccountConsistencyNotRequired) {
// Trigger a request.
base::MockCallback<OneGoogleBarFetcher::OneGoogleCallback> callback;
one_google_bar_fetcher()->Fetch(callback.Get());
net::TestURLFetcher* fetcher = GetRunningURLFetcher();
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
#if defined(OS_CHROMEOS)
// On Chrome OS, X-Chrome-Connected header is present, but
// enable_account_consistency is set to false.
std::string header_value;
EXPECT_TRUE(headers.GetHeader(signin::kChromeConnectedHeader, &header_value));
// mode = PROFILE_MODE_DEFAULT
EXPECT_EQ("mode=0,enable_account_consistency=false", header_value);
#else
// On not Chrome OS, the X-Chrome-Connected header must not be present.
EXPECT_FALSE(headers.HasHeader(signin::kChromeConnectedHeader));
#endif
}
class OneGoogleBarFetcherImplWithMirrorAccountConsistencyTest
: public OneGoogleBarFetcherImplTest {
public:
OneGoogleBarFetcherImplWithMirrorAccountConsistencyTest()
: OneGoogleBarFetcherImplTest(true) {}
};
TEST_F(OneGoogleBarFetcherImplWithMirrorAccountConsistencyTest,
MirrorAccountConsistencyRequired) {
// Trigger a request.
base::MockCallback<OneGoogleBarFetcher::OneGoogleCallback> callback;
one_google_bar_fetcher()->Fetch(callback.Get());
net::TestURLFetcher* fetcher = GetRunningURLFetcher();
// Make sure mirror account consistency is requested.
net::HttpRequestHeaders headers;
fetcher->GetExtraRequestHeaders(&headers);
#if defined(OS_CHROMEOS)
// On Chrome OS, X-Chrome-Connected header is present, and
// enable_account_consistency is set to true.
std::string header_value;
EXPECT_TRUE(headers.GetHeader(signin::kChromeConnectedHeader, &header_value));
// mode = PROFILE_MODE_INCOGNITO_DISABLED | PROFILE_MODE_ADD_ACCOUNT_DISABLED
EXPECT_EQ("mode=3,enable_account_consistency=true", header_value);
#else
// This is not a valid case (mirror account consistency can only be required
// on Chrome OS). This ensures in this case nothing happens.
EXPECT_FALSE(headers.HasHeader(signin::kChromeConnectedHeader));
#endif
}
class OneGoogleBarFetcherImplWithRelativeApiUrlOverrideTest
: public OneGoogleBarFetcherImplTest {
public:
......
......@@ -4,18 +4,24 @@
#include "chrome/browser/search/one_google_bar/one_google_bar_service_factory.h"
#include <string>
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial_params.h"
#include "base/optional.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/google/google_url_tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/one_google_bar/one_google_bar_fetcher_impl.h"
#include "chrome/browser/search/one_google_bar/one_google_bar_service.h"
#include "chrome/browser/signin/account_consistency_mode_manager.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/common/chrome_features.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/signin/core/browser/signin_header_helper.h"
#include "content/public/browser/browser_context.h"
// static
......@@ -34,6 +40,7 @@ OneGoogleBarServiceFactory::OneGoogleBarServiceFactory()
: BrowserContextKeyedServiceFactory(
"OneGoogleBarService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(CookieSettingsFactory::GetInstance());
DependsOn(GaiaCookieManagerServiceFactory::GetInstance());
DependsOn(GoogleURLTrackerFactory::GetInstance());
}
......@@ -57,9 +64,13 @@ KeyedService* OneGoogleBarServiceFactory::BuildServiceInstanceFor(
if (!override_api_url_str.empty()) {
override_api_url = override_api_url_str;
}
content_settings::CookieSettings* cookie_settings =
CookieSettingsFactory::GetForProfile(profile).get();
return new OneGoogleBarService(
cookie_service,
base::MakeUnique<OneGoogleBarFetcherImpl>(
profile->GetRequestContext(), google_url_tracker,
g_browser_process->GetApplicationLocale(), override_api_url));
g_browser_process->GetApplicationLocale(), override_api_url,
AccountConsistencyModeManager::IsMirrorEnabledForProfile(profile) &&
signin::SettingsAllowSigninCookies(cookie_settings)));
}
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