Commit ba7f02d6 authored by Kush Sinha's avatar Kush Sinha Committed by Commit Bot

Add an |AccountMapperUtil| class

Move the functions to translate back and forth between the account id
used by the OAuth service chain and the account representation used by
Chrome OS Account Manager, to a util class.

This function was previously being performed internally in
|ChromeOSOAuth2TokenServiceDelegate|. Move it to a common util class
because other usages (ARC++) have cropped up that should not depend on
|ChromeOSOAuth2TokenServiceDelegate| for this use case.

Bug: 820046, 871690
Test: unit_tests --gtest_filter="*CrOSOAuthDelegateTest*"
Change-Id: Ie5d9fe672c521c0ddf3d6e376649d3a3fb9d33cb
Reviewed-on: https://chromium-review.googlesource.com/1165352
Commit-Queue: Kush Sinha <sinhak@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarLutz Justen <ljusten@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582093}
parent 86c816cf
...@@ -264,6 +264,8 @@ source_set("chromeos") { ...@@ -264,6 +264,8 @@ source_set("chromeos") {
"accessibility/spoken_feedback_event_rewriter_delegate.h", "accessibility/spoken_feedback_event_rewriter_delegate.h",
"accessibility/switch_access_event_handler.cc", "accessibility/switch_access_event_handler.cc",
"accessibility/switch_access_event_handler.h", "accessibility/switch_access_event_handler.h",
"account_mapper_util.cc",
"account_mapper_util.h",
"app_mode/app_launch_utils.cc", "app_mode/app_launch_utils.cc",
"app_mode/app_launch_utils.h", "app_mode/app_launch_utils.h",
"app_mode/app_session.cc", "app_mode/app_session.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 "chrome/browser/chromeos/account_mapper_util.h"
#include "components/signin/core/browser/account_tracker_service.h"
namespace chromeos {
AccountMapperUtil::AccountMapperUtil(
AccountTrackerService* account_tracker_service)
: account_tracker_service_(account_tracker_service) {}
AccountMapperUtil::~AccountMapperUtil() = default;
std::string AccountMapperUtil::AccountKeyToOAuthAccountId(
const AccountManager::AccountKey& account_key) const {
DCHECK(account_key.IsValid());
if (account_key.account_type !=
account_manager::AccountType::ACCOUNT_TYPE_GAIA) {
return std::string();
}
const std::string& account_id =
account_tracker_service_->FindAccountInfoByGaiaId(account_key.id)
.account_id;
DCHECK(!account_id.empty()) << "Can't find account id";
return account_id;
}
AccountManager::AccountKey AccountMapperUtil::OAuthAccountIdToAccountKey(
const std::string& account_id) const {
DCHECK(!account_id.empty());
const AccountInfo& account_info =
account_tracker_service_->GetAccountInfo(account_id);
DCHECK(!account_info.gaia.empty()) << "Can't find account info";
return AccountManager::AccountKey{
account_info.gaia, account_manager::AccountType::ACCOUNT_TYPE_GAIA};
}
} // namespace chromeos
// 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 CHROME_BROWSER_CHROMEOS_ACCOUNT_MAPPER_UTIL_H_
#define CHROME_BROWSER_CHROMEOS_ACCOUNT_MAPPER_UTIL_H_
#include <string>
#include "base/macros.h"
#include "chromeos/account_manager/account_manager.h"
class AccountTrackerService;
namespace chromeos {
class AccountMapperUtil {
public:
explicit AccountMapperUtil(AccountTrackerService* account_tracker_service);
~AccountMapperUtil();
// A utility method to map an |account_key| to the account id used by the
// OAuth2TokenService chain (see |AccountInfo|). Returns an empty string for
// non-Gaia accounts.
std::string AccountKeyToOAuthAccountId(
const AccountManager::AccountKey& account_key) const;
// A utility method to map the |account_id| used by the OAuth2TokenService
// chain (see |AccountInfo|) to an |AccountManager::AccountKey|.
AccountManager::AccountKey OAuthAccountIdToAccountKey(
const std::string& account_id) const;
private:
// A non-owning pointer to |AccountTrackerService|, which itself is a
// |KeyedService|.
AccountTrackerService* const account_tracker_service_;
DISALLOW_COPY_AND_ASSIGN(AccountMapperUtil);
};
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_ACCOUNT_MAPPER_UTIL_H_
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
#include "chrome/browser/chromeos/oauth2_token_service_delegate.h" #include "chrome/browser/chromeos/oauth2_token_service_delegate.h"
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/chromeos/account_mapper_util.h"
#include "chromeos/account_manager/account_manager.h" #include "chromeos/account_manager/account_manager.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
namespace chromeos { namespace chromeos {
...@@ -18,10 +19,10 @@ namespace chromeos { ...@@ -18,10 +19,10 @@ namespace chromeos {
ChromeOSOAuth2TokenServiceDelegate::ChromeOSOAuth2TokenServiceDelegate( ChromeOSOAuth2TokenServiceDelegate::ChromeOSOAuth2TokenServiceDelegate(
AccountTrackerService* account_tracker_service, AccountTrackerService* account_tracker_service,
chromeos::AccountManager* account_manager) chromeos::AccountManager* account_manager)
: account_tracker_service_(account_tracker_service), : account_mapper_util_(
std::make_unique<AccountMapperUtil>(account_tracker_service)),
account_manager_(account_manager), account_manager_(account_manager),
weak_factory_(this) { weak_factory_(this) {}
}
ChromeOSOAuth2TokenServiceDelegate::~ChromeOSOAuth2TokenServiceDelegate() { ChromeOSOAuth2TokenServiceDelegate::~ChromeOSOAuth2TokenServiceDelegate() {
account_manager_->RemoveObserver(this); account_manager_->RemoveObserver(this);
...@@ -38,7 +39,7 @@ ChromeOSOAuth2TokenServiceDelegate::CreateAccessTokenFetcher( ...@@ -38,7 +39,7 @@ ChromeOSOAuth2TokenServiceDelegate::CreateAccessTokenFetcher(
ValidateAccountId(account_id); ValidateAccountId(account_id);
const AccountManager::AccountKey& account_key = const AccountManager::AccountKey& account_key =
MapAccountIdToAccountKey(account_id); account_mapper_util_->OAuthAccountIdToAccountKey(account_id);
// |OAuth2TokenService| will manage the lifetime of the released pointer. // |OAuth2TokenService| will manage the lifetime of the released pointer.
return account_manager_ return account_manager_
...@@ -53,7 +54,7 @@ bool ChromeOSOAuth2TokenServiceDelegate::RefreshTokenIsAvailable( ...@@ -53,7 +54,7 @@ bool ChromeOSOAuth2TokenServiceDelegate::RefreshTokenIsAvailable(
} }
return account_manager_->IsTokenAvailable( return account_manager_->IsTokenAvailable(
MapAccountIdToAccountKey(account_id)); account_mapper_util_->OAuthAccountIdToAccountKey(account_id));
} }
void ChromeOSOAuth2TokenServiceDelegate::UpdateAuthError( void ChromeOSOAuth2TokenServiceDelegate::UpdateAuthError(
...@@ -94,7 +95,8 @@ std::vector<std::string> ChromeOSOAuth2TokenServiceDelegate::GetAccounts() { ...@@ -94,7 +95,8 @@ std::vector<std::string> ChromeOSOAuth2TokenServiceDelegate::GetAccounts() {
std::vector<std::string> accounts; std::vector<std::string> accounts;
for (auto& account_key : account_keys_) { for (auto& account_key : account_keys_) {
std::string account_id = MapAccountKeyToAccountId(account_key); std::string account_id =
account_mapper_util_->AccountKeyToOAuthAccountId(account_key);
if (!account_id.empty()) { if (!account_id.empty()) {
accounts.emplace_back(account_id); accounts.emplace_back(account_id);
} }
...@@ -131,7 +133,7 @@ void ChromeOSOAuth2TokenServiceDelegate::UpdateCredentials( ...@@ -131,7 +133,7 @@ void ChromeOSOAuth2TokenServiceDelegate::UpdateCredentials(
ValidateAccountId(account_id); ValidateAccountId(account_id);
const AccountManager::AccountKey& account_key = const AccountManager::AccountKey& account_key =
MapAccountIdToAccountKey(account_id); account_mapper_util_->OAuthAccountIdToAccountKey(account_id);
// Will result in AccountManager calling // Will result in AccountManager calling
// |ChromeOSOAuth2TokenServiceDelegate::OnTokenUpserted|. // |ChromeOSOAuth2TokenServiceDelegate::OnTokenUpserted|.
...@@ -173,41 +175,13 @@ void ChromeOSOAuth2TokenServiceDelegate::GetAccountsCallback( ...@@ -173,41 +175,13 @@ void ChromeOSOAuth2TokenServiceDelegate::GetAccountsCallback(
FireRefreshTokensLoaded(); FireRefreshTokensLoaded();
} }
std::string ChromeOSOAuth2TokenServiceDelegate::MapAccountKeyToAccountId(
const AccountManager::AccountKey& account_key) const {
DCHECK(account_key.IsValid());
if (account_key.account_type !=
account_manager::AccountType::ACCOUNT_TYPE_GAIA) {
return std::string();
}
const std::string& account_id =
account_tracker_service_->FindAccountInfoByGaiaId(account_key.id)
.account_id;
DCHECK(!account_id.empty()) << "Can't find account id";
return account_id;
}
AccountManager::AccountKey
ChromeOSOAuth2TokenServiceDelegate::MapAccountIdToAccountKey(
const std::string& account_id) const {
DCHECK(!account_id.empty());
const AccountInfo& account_info =
account_tracker_service_->GetAccountInfo(account_id);
DCHECK(!account_info.gaia.empty()) << "Can't find account info";
return AccountManager::AccountKey{
account_info.gaia, account_manager::AccountType::ACCOUNT_TYPE_GAIA};
}
void ChromeOSOAuth2TokenServiceDelegate::OnTokenUpserted( void ChromeOSOAuth2TokenServiceDelegate::OnTokenUpserted(
const AccountManager::AccountKey& account_key) { const AccountManager::AccountKey& account_key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
account_keys_.insert(account_key); account_keys_.insert(account_key);
std::string account_id = MapAccountKeyToAccountId(account_key); std::string account_id =
account_mapper_util_->AccountKeyToOAuthAccountId(account_key);
if (account_id.empty()) { if (account_id.empty()) {
return; return;
} }
...@@ -237,7 +211,8 @@ void ChromeOSOAuth2TokenServiceDelegate::OnAccountRemoved( ...@@ -237,7 +211,8 @@ void ChromeOSOAuth2TokenServiceDelegate::OnAccountRemoved(
} }
account_keys_.erase(it); account_keys_.erase(it);
std::string account_id = MapAccountKeyToAccountId(account_key); std::string account_id =
account_mapper_util_->AccountKeyToOAuthAccountId(account_key);
if (account_id.empty()) { if (account_id.empty()) {
return; return;
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define CHROME_BROWSER_CHROMEOS_OAUTH2_TOKEN_SERVICE_DELEGATE_H_ #define CHROME_BROWSER_CHROMEOS_OAUTH2_TOKEN_SERVICE_DELEGATE_H_
#include <map> #include <map>
#include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -20,6 +21,8 @@ class AccountTrackerService; ...@@ -20,6 +21,8 @@ class AccountTrackerService;
namespace chromeos { namespace chromeos {
class AccountMapperUtil;
class ChromeOSOAuth2TokenServiceDelegate : public OAuth2TokenServiceDelegate, class ChromeOSOAuth2TokenServiceDelegate : public OAuth2TokenServiceDelegate,
public AccountManager::Observer { public AccountManager::Observer {
public: public:
...@@ -61,23 +64,10 @@ class ChromeOSOAuth2TokenServiceDelegate : public OAuth2TokenServiceDelegate, ...@@ -61,23 +64,10 @@ class ChromeOSOAuth2TokenServiceDelegate : public OAuth2TokenServiceDelegate,
void GetAccountsCallback( void GetAccountsCallback(
std::vector<AccountManager::AccountKey> account_keys); std::vector<AccountManager::AccountKey> account_keys);
// A utility method to map an |account_key| to the account id used by the
// OAuth2TokenService chain (see |AccountInfo|). Returns an empty string for
// non-Gaia accounts.
std::string MapAccountKeyToAccountId(
const AccountManager::AccountKey& account_key) const;
// A utility method to map the |account_id| used by the OAuth2TokenService
// chain (see |AccountInfo|) to an |AccountManager::AccountKey|.
AccountManager::AccountKey MapAccountIdToAccountKey(
const std::string& account_id) const;
LoadCredentialsState load_credentials_state_ = LoadCredentialsState load_credentials_state_ =
LoadCredentialsState::LOAD_CREDENTIALS_NOT_STARTED; LoadCredentialsState::LOAD_CREDENTIALS_NOT_STARTED;
// A non-owning pointer to |AccountTrackerService|, which itself is a std::unique_ptr<AccountMapperUtil> account_mapper_util_;
// |KeyedService|.
AccountTrackerService* account_tracker_service_;
// A non-owning pointer to |AccountManager|. |AccountManager| is available // A non-owning pointer to |AccountManager|. |AccountManager| is available
// throughout the lifetime of a user session. // throughout the lifetime of a user session.
......
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