Commit 2b0e8e60 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Sync: Move DetermineAccountToUse into new sync_auth_util.h/cc

This is a followup to https://crrev.com/c/1350984.
In particular, this moves the code from /components/browser_sync/ into
/components/sync/, where autofill code can actually access it.

Bug: 903290
Change-Id: Id7b90cf5c1344cb4404d9a672d057a67e74d35b9
Reviewed-on: https://chromium-review.googlesource.com/c/1352160Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611277}
parent 52f0d911
...@@ -51,13 +51,6 @@ constexpr net::BackoffEntry::Policy kRequestAccessTokenBackoffPolicy = { ...@@ -51,13 +51,6 @@ constexpr net::BackoffEntry::Policy kRequestAccessTokenBackoffPolicy = {
} // namespace } // namespace
SyncAuthManager::SyncAccountInfo::SyncAccountInfo() = default;
SyncAuthManager::SyncAccountInfo::SyncAccountInfo(
const AccountInfo& account_info,
bool is_primary)
: account_info(account_info), is_primary(is_primary) {}
SyncAuthManager::SyncAuthManager( SyncAuthManager::SyncAuthManager(
syncer::SyncPrefs* sync_prefs, syncer::SyncPrefs* sync_prefs,
identity::IdentityManager* identity_manager, identity::IdentityManager* identity_manager,
...@@ -92,9 +85,9 @@ void SyncAuthManager::RegisterForAuthNotifications() { ...@@ -92,9 +85,9 @@ void SyncAuthManager::RegisterForAuthNotifications() {
sync_account_ = DetermineAccountToUse(); sync_account_ = DetermineAccountToUse();
} }
SyncAuthManager::SyncAccountInfo SyncAuthManager::GetActiveAccountInfo() const { syncer::SyncAccountInfo SyncAuthManager::GetActiveAccountInfo() const {
if (!registered_for_auth_notifications_) { if (!registered_for_auth_notifications_) {
return SyncAccountInfo(); return syncer::SyncAccountInfo();
} }
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -351,43 +344,16 @@ void SyncAuthManager::ResetRequestAccessTokenBackoffForTest() { ...@@ -351,43 +344,16 @@ void SyncAuthManager::ResetRequestAccessTokenBackoffForTest() {
request_access_token_backoff_.Reset(); request_access_token_backoff_.Reset();
} }
// static syncer::SyncAccountInfo SyncAuthManager::DetermineAccountToUse() const {
SyncAuthManager::SyncAccountInfo SyncAuthManager::DetermineAccountToUse(
identity::IdentityManager* identity_manager,
bool allow_secondary_accounts) {
// If there is a "primary account", i.e. the user explicitly chose to
// sign-in to Chrome, then always use that account.
if (identity_manager->HasPrimaryAccount()) {
return SyncAccountInfo(identity_manager->GetPrimaryAccountInfo(),
/*is_primary=*/true);
}
// Otherwise, fall back to the default content area signed-in account.
if (allow_secondary_accounts) {
// Check if there is a content area signed-in account, and we have a refresh
// token for it.
std::vector<AccountInfo> cookie_accounts =
identity_manager->GetAccountsInCookieJar();
if (!cookie_accounts.empty() &&
identity_manager->HasAccountWithRefreshToken(
cookie_accounts[0].account_id)) {
return SyncAccountInfo(cookie_accounts[0], /*is_primary=*/false);
}
}
return SyncAccountInfo();
}
SyncAuthManager::SyncAccountInfo SyncAuthManager::DetermineAccountToUse()
const {
DCHECK(registered_for_auth_notifications_); DCHECK(registered_for_auth_notifications_);
return DetermineAccountToUse( return syncer::DetermineAccountToUse(
identity_manager_, identity_manager_,
base::FeatureList::IsEnabled(switches::kSyncStandaloneTransport) && base::FeatureList::IsEnabled(switches::kSyncStandaloneTransport) &&
base::FeatureList::IsEnabled(switches::kSyncSupportSecondaryAccount)); base::FeatureList::IsEnabled(switches::kSyncSupportSecondaryAccount));
} }
bool SyncAuthManager::UpdateSyncAccountIfNecessary() { bool SyncAuthManager::UpdateSyncAccountIfNecessary() {
SyncAccountInfo new_account = DetermineAccountToUse(); syncer::SyncAccountInfo new_account = DetermineAccountToUse();
// If we're already using this account and its |is_primary| bit hasn't changed // If we're already using this account and its |is_primary| bit hasn't changed
// (or there was and is no account to use), then there's nothing to do. // (or there was and is no account to use), then there's nothing to do.
if (new_account.account_info.account_id == if (new_account.account_info.account_id ==
...@@ -401,7 +367,7 @@ bool SyncAuthManager::UpdateSyncAccountIfNecessary() { ...@@ -401,7 +367,7 @@ bool SyncAuthManager::UpdateSyncAccountIfNecessary() {
// Sign out of the old account (if any). // Sign out of the old account (if any).
if (!sync_account_.account_info.account_id.empty()) { if (!sync_account_.account_info.account_id.empty()) {
sync_account_ = SyncAccountInfo(); sync_account_ = syncer::SyncAccountInfo();
// Also clear any pending request or auth errors we might have, since they // Also clear any pending request or auth errors we might have, since they
// aren't meaningful anymore. // aren't meaningful anymore.
Clear(); Clear();
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "components/signin/core/browser/account_info.h" #include "components/signin/core/browser/account_info.h"
#include "components/sync/driver/sync_auth_util.h"
#include "components/sync/driver/sync_token_status.h" #include "components/sync/driver/sync_token_status.h"
#include "components/sync/engine/connection_status.h" #include "components/sync/engine/connection_status.h"
#include "google_apis/gaia/google_service_auth_error.h" #include "google_apis/gaia/google_service_auth_error.h"
...@@ -36,14 +37,6 @@ namespace browser_sync { ...@@ -36,14 +37,6 @@ namespace browser_sync {
// IdentityManager::GetPrimaryAccountInfo() etc). // IdentityManager::GetPrimaryAccountInfo() etc).
class SyncAuthManager : public identity::IdentityManager::Observer { class SyncAuthManager : public identity::IdentityManager::Observer {
public: public:
struct SyncAccountInfo {
SyncAccountInfo();
SyncAccountInfo(const AccountInfo& account_info, bool is_primary);
AccountInfo account_info;
bool is_primary = false;
};
// Called when the existence of an authenticated account changes. It's // Called when the existence of an authenticated account changes. It's
// guaranteed that this is only called for going from "no account" to "have // guaranteed that this is only called for going from "no account" to "have
// account" or vice versa, i.e. SyncAuthManager will never directly switch // account" or vice versa, i.e. SyncAuthManager will never directly switch
...@@ -72,7 +65,7 @@ class SyncAuthManager : public identity::IdentityManager::Observer { ...@@ -72,7 +65,7 @@ class SyncAuthManager : public identity::IdentityManager::Observer {
// Returns the account which should be used when communicating with the Sync // Returns the account which should be used when communicating with the Sync
// server. Note that this account may not be blessed for Sync-the-feature. // server. Note that this account may not be blessed for Sync-the-feature.
SyncAccountInfo GetActiveAccountInfo() const; syncer::SyncAccountInfo GetActiveAccountInfo() const;
const GoogleServiceAuthError& GetLastAuthError() const { const GoogleServiceAuthError& GetLastAuthError() const {
return last_auth_error_; return last_auth_error_;
...@@ -109,15 +102,8 @@ class SyncAuthManager : public identity::IdentityManager::Observer { ...@@ -109,15 +102,8 @@ class SyncAuthManager : public identity::IdentityManager::Observer {
bool IsRetryingAccessTokenFetchForTest() const; bool IsRetryingAccessTokenFetchForTest() const;
void ResetRequestAccessTokenBackoffForTest(); void ResetRequestAccessTokenBackoffForTest();
// Determines which account should be used for Sync and returns the
// corresponding SyncAccountInfo. This is exposed so that autofill metrics
// code can use it.
static SyncAccountInfo DetermineAccountToUse(
identity::IdentityManager* identity_manager,
bool allow_secondary_accounts);
private: private:
SyncAccountInfo DetermineAccountToUse() const; syncer::SyncAccountInfo DetermineAccountToUse() const;
// Updates |sync_account_| to the appropriate account (i.e. // Updates |sync_account_| to the appropriate account (i.e.
// DetermineAccountToUse) if necessary, and notifies observers of any changes // DetermineAccountToUse) if necessary, and notifies observers of any changes
...@@ -158,7 +144,7 @@ class SyncAuthManager : public identity::IdentityManager::Observer { ...@@ -158,7 +144,7 @@ class SyncAuthManager : public identity::IdentityManager::Observer {
// The account which we are using to sync. If this is non-empty, that does // The account which we are using to sync. If this is non-empty, that does
// *not* necessarily imply that Sync is actually running, e.g. because of // *not* necessarily imply that Sync is actually running, e.g. because of
// delayed startup. // delayed startup.
SyncAccountInfo sync_account_; syncer::SyncAccountInfo sync_account_;
// This is a cache of the last authentication response we received either // This is a cache of the last authentication response we received either
// from the sync server or from Chrome's identity/token management system. // from the sync server or from Chrome's identity/token management system.
......
...@@ -141,6 +141,8 @@ jumbo_static_library("sync") { ...@@ -141,6 +141,8 @@ jumbo_static_library("sync") {
"driver/startup_controller.h", "driver/startup_controller.h",
"driver/sync_api_component_factory.cc", "driver/sync_api_component_factory.cc",
"driver/sync_api_component_factory.h", "driver/sync_api_component_factory.h",
"driver/sync_auth_util.cc",
"driver/sync_auth_util.h",
"driver/sync_client.cc", "driver/sync_client.cc",
"driver/sync_client.h", "driver/sync_client.h",
"driver/sync_driver_switches.cc", "driver/sync_driver_switches.cc",
......
// Copyright 2018 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 "components/sync/driver/sync_auth_util.h"
#include <vector>
#include "services/identity/public/cpp/identity_manager.h"
namespace syncer {
SyncAccountInfo::SyncAccountInfo() = default;
SyncAccountInfo::SyncAccountInfo(const AccountInfo& account_info,
bool is_primary)
: account_info(account_info), is_primary(is_primary) {}
SyncAccountInfo DetermineAccountToUse(
identity::IdentityManager* identity_manager,
bool allow_secondary_accounts) {
// If there is a "primary account", i.e. the user explicitly chose to
// sign-in to Chrome, then always use that account.
if (identity_manager->HasPrimaryAccount()) {
return SyncAccountInfo(identity_manager->GetPrimaryAccountInfo(),
/*is_primary=*/true);
}
// Otherwise, fall back to the default content area signed-in account.
if (allow_secondary_accounts) {
// Check if there is a content area signed-in account, and we have a refresh
// token for it.
std::vector<AccountInfo> cookie_accounts =
identity_manager->GetAccountsInCookieJar();
if (!cookie_accounts.empty() &&
identity_manager->HasAccountWithRefreshToken(
cookie_accounts[0].account_id)) {
return SyncAccountInfo(cookie_accounts[0], /*is_primary=*/false);
}
}
return SyncAccountInfo();
}
} // namespace syncer
// Copyright 2018 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 COMPONENTS_SYNC_DRIVER_SYNC_AUTH_UTIL_H_
#define COMPONENTS_SYNC_DRIVER_SYNC_AUTH_UTIL_H_
#include "components/signin/core/browser/account_info.h"
namespace identity {
class IdentityManager;
} // namespace identity
namespace syncer {
struct SyncAccountInfo {
SyncAccountInfo();
SyncAccountInfo(const AccountInfo& account_info, bool is_primary);
AccountInfo account_info;
bool is_primary = false;
};
// Determines which account should be used for Sync and returns the
// corresponding SyncAccountInfo. This is exposed so that autofill metrics
// code can use it.
SyncAccountInfo DetermineAccountToUse(
identity::IdentityManager* identity_manager,
bool allow_secondary_accounts);
} // namespace syncer
#endif // COMPONENTS_SYNC_DRIVER_SYNC_AUTH_UTIL_H_
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