Commit db982eea authored by Anastasiia N's avatar Anastasiia N Committed by Chromium LUCI CQ

Move SigninHelper to a separate file

Move `SigninHelper` class from
chrome/browser/ui/webui/signin/inline_login_handler_chromeos.cc to
chrome/browser/ui/webui/signin/signin_helper_chromeos.cc

Bug: 1140469
Change-Id: Ia6c79410085240dc6c56e053ac595f9e3138d805
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2550546
Commit-Queue: Anastasiia N <anastasiian@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarKush Sinha <sinhak@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844018}
parent f705b023
......@@ -2588,6 +2588,8 @@ static_library("ui") {
"webui/signin/inline_login_handler_chromeos.h",
"webui/signin/inline_login_handler_modal_delegate.cc",
"webui/signin/inline_login_handler_modal_delegate.h",
"webui/signin/signin_helper_chromeos.cc",
"webui/signin/signin_helper_chromeos.h",
"webui/version/version_handler_chromeos.cc",
"webui/version/version_handler_chromeos.h",
"window_sizer/window_sizer_chromeos.cc",
......
......@@ -7,4 +7,5 @@ xiyuan@chromium.org
per-file inline_login_handler_impl*=file://chrome/credential_provider/OWNERS
per-file inline_login_handler.*=file://chrome/credential_provider/OWNERS
per-file inline_login_*_chromeos.*=file://chromeos/components/account_manager/OWNERS
per-file inline_login_*_chromeos*=file://chromeos/components/account_manager/OWNERS
per-file signin_helper_chromeos.*=file://chromeos/components/account_manager/OWNERS
......@@ -25,6 +25,7 @@
#include "chrome/browser/ui/webui/chromeos/edu_coexistence_state_tracker.h"
#include "chrome/browser/ui/webui/signin/inline_login_dialog_chromeos.h"
#include "chrome/browser/ui/webui/signin/inline_login_handler.h"
#include "chrome/browser/ui/webui/signin/signin_helper_chromeos.h"
#include "chrome/common/pref_names.h"
#include "chromeos/components/account_manager/account_manager_factory.h"
#include "chromeos/constants/chromeos_features.h"
......@@ -81,102 +82,6 @@ std::string GetInlineLoginFlowName(Profile* profile, const std::string* email) {
return kCrosAddAccountEduFlow;
}
// A helper class for completing the inline login flow. Primarily, it is
// responsible for exchanging the auth code, obtained after a successful user
// sign in, for OAuth tokens and subsequently populating Chrome OS
// AccountManager with these tokens.
// This object is supposed to be used in a one-shot fashion and it deletes
// itself after its work is complete.
class SigninHelper : public GaiaAuthConsumer {
public:
SigninHelper(
chromeos::AccountManager* account_manager,
const base::RepeatingClosure& close_dialog_closure,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& gaia_id,
const std::string& email,
const std::string& auth_code,
const std::string& signin_scoped_device_id)
: account_manager_(account_manager),
close_dialog_closure_(close_dialog_closure),
email_(email),
url_loader_factory_(url_loader_factory),
gaia_auth_fetcher_(this,
gaia::GaiaSource::kChrome,
url_loader_factory) {
account_key_ = ::account_manager::AccountKey{
gaia_id, account_manager::AccountType::kGaia};
DCHECK(!signin_scoped_device_id.empty());
gaia_auth_fetcher_.StartAuthCodeForOAuth2TokenExchangeWithDeviceId(
auth_code, signin_scoped_device_id);
}
~SigninHelper() override = default;
protected:
// GaiaAuthConsumer overrides.
void OnClientOAuthSuccess(const ClientOAuthResult& result) override {
// Flow of control after this call:
// |AccountManager::UpsertAccount| updates / inserts the account and calls
// its |Observer|s, one of which is
// |ProfileOAuth2TokenServiceDelegateChromeOS|.
// |ProfileOAuth2TokenServiceDelegateChromeOS::OnTokenUpserted| seeds the
// Gaia id and email id for this account in |AccountTrackerService| and
// invokes |FireRefreshTokenAvailable|. This causes the account to propagate
// throughout the Identity Service chain, including in
// |AccountFetcherService|. |AccountFetcherService::OnRefreshTokenAvailable|
// invokes |AccountTrackerService::StartTrackingAccount|, triggers a fetch
// for the account information from Gaia and updates this information into
// |AccountTrackerService|. At this point the account will be fully added to
// the system.
UpsertAccount(result.refresh_token);
CloseDialogAndExit();
}
void OnClientOAuthFailure(const GoogleServiceAuthError& error) override {
// TODO(sinhak): Display an error.
CloseDialogAndExit();
}
void UpsertAccount(const std::string& refresh_token) {
account_manager_->UpsertAccount(account_key_, email_, refresh_token);
}
void CloseDialogAndExit() {
close_dialog_closure_.Run();
Exit();
}
void Exit() {
base::SequencedTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
}
chromeos::AccountManager* GetAccountManager() { return account_manager_; }
const std::string GetEmail() { return email_; }
const scoped_refptr<network::SharedURLLoaderFactory> GetUrlLoaderFactory() {
return url_loader_factory_;
}
private:
// A non-owning pointer to Chrome OS AccountManager.
chromeos::AccountManager* const account_manager_;
// A closure to close the hosting dialog window.
base::RepeatingClosure close_dialog_closure_;
// The user's AccountKey for which |this| object has been created.
::account_manager::AccountKey account_key_;
// The user's email for which |this| object has been created.
const std::string email_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// Used for exchanging auth code for OAuth tokens.
GaiaAuthFetcher gaia_auth_fetcher_;
DISALLOW_COPY_AND_ASSIGN(SigninHelper);
};
// A version of SigninHelper for child users. After obtaining OAuth token it
// logs the parental consent with provided parent id and rapt. After successful
// consent logging populates Chrome OS AccountManager with the token.
......
// Copyright 2021 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/ui/webui/signin/signin_helper_chromeos.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
namespace chromeos {
SigninHelper::SigninHelper(
chromeos::AccountManager* account_manager,
const base::RepeatingClosure& close_dialog_closure,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& gaia_id,
const std::string& email,
const std::string& auth_code,
const std::string& signin_scoped_device_id)
: account_manager_(account_manager),
close_dialog_closure_(close_dialog_closure),
email_(email),
url_loader_factory_(std::move(url_loader_factory)),
gaia_auth_fetcher_(this, gaia::GaiaSource::kChrome, url_loader_factory) {
account_key_ = ::account_manager::AccountKey{
gaia_id, ::account_manager::AccountType::kGaia};
DCHECK(!signin_scoped_device_id.empty());
gaia_auth_fetcher_.StartAuthCodeForOAuth2TokenExchangeWithDeviceId(
auth_code, signin_scoped_device_id);
}
SigninHelper::~SigninHelper() = default;
void SigninHelper::OnClientOAuthSuccess(const ClientOAuthResult& result) {
// Flow of control after this call:
// |AccountManager::UpsertAccount| updates / inserts the account and calls
// its |Observer|s, one of which is
// |ProfileOAuth2TokenServiceDelegateChromeOS|.
// |ProfileOAuth2TokenServiceDelegateChromeOS::OnTokenUpserted| seeds the
// Gaia id and email id for this account in |AccountTrackerService| and
// invokes |FireRefreshTokenAvailable|. This causes the account to propagate
// throughout the Identity Service chain, including in
// |AccountFetcherService|. |AccountFetcherService::OnRefreshTokenAvailable|
// invokes |AccountTrackerService::StartTrackingAccount|, triggers a fetch
// for the account information from Gaia and updates this information into
// |AccountTrackerService|. At this point the account will be fully added to
// the system.
UpsertAccount(result.refresh_token);
CloseDialogAndExit();
}
void SigninHelper::OnClientOAuthFailure(const GoogleServiceAuthError& error) {
// TODO(sinhak): Display an error.
CloseDialogAndExit();
}
void SigninHelper::UpsertAccount(const std::string& refresh_token) {
account_manager_->UpsertAccount(account_key_, email_, refresh_token);
}
void SigninHelper::CloseDialogAndExit() {
close_dialog_closure_.Run();
Exit();
}
void SigninHelper::Exit() {
base::SequencedTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
}
chromeos::AccountManager* SigninHelper::GetAccountManager() {
return account_manager_;
}
std::string SigninHelper::GetEmail() {
return email_;
}
scoped_refptr<network::SharedURLLoaderFactory>
SigninHelper::GetUrlLoaderFactory() {
return url_loader_factory_;
}
} // namespace chromeos
// Copyright 2021 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_UI_WEBUI_SIGNIN_SIGNIN_HELPER_CHROMEOS_H_
#define CHROME_BROWSER_UI_WEBUI_SIGNIN_SIGNIN_HELPER_CHROMEOS_H_
#include "chromeos/components/account_manager/account_manager.h"
#include "google_apis/gaia/gaia_auth_consumer.h"
#include "google_apis/gaia/gaia_auth_fetcher.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace chromeos {
// A helper class for completing the inline login flow. Primarily, it is
// responsible for exchanging the auth code, obtained after a successful user
// sign in, for OAuth tokens and subsequently populating Chrome OS
// AccountManager with these tokens.
// This object is supposed to be used in a one-shot fashion and it deletes
// itself after its work is complete.
class SigninHelper : public GaiaAuthConsumer {
public:
SigninHelper(
chromeos::AccountManager* account_manager,
const base::RepeatingClosure& close_dialog_closure,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
const std::string& gaia_id,
const std::string& email,
const std::string& auth_code,
const std::string& signin_scoped_device_id);
SigninHelper(const SigninHelper&) = delete;
SigninHelper& operator=(const SigninHelper&) = delete;
~SigninHelper() override;
protected:
// GaiaAuthConsumer overrides.
void OnClientOAuthSuccess(const ClientOAuthResult& result) override;
void OnClientOAuthFailure(const GoogleServiceAuthError& error) override;
void UpsertAccount(const std::string& refresh_token);
// Closes the inline login dialog and calls `Exit`.
void CloseDialogAndExit();
// Deletes this object.
void Exit();
chromeos::AccountManager* GetAccountManager();
// Returns email address of the account being added.
std::string GetEmail();
scoped_refptr<network::SharedURLLoaderFactory> GetUrlLoaderFactory();
private:
// A non-owning pointer to Chrome OS AccountManager.
chromeos::AccountManager* const account_manager_;
// A closure to close the hosting dialog window.
base::RepeatingClosure close_dialog_closure_;
// The user's AccountKey for which |this| object has been created.
::account_manager::AccountKey account_key_;
// The user's email for which |this| object has been created.
const std::string email_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// Used for exchanging auth code for OAuth tokens.
GaiaAuthFetcher gaia_auth_fetcher_;
};
} // namespace chromeos
#endif // CHROME_BROWSER_UI_WEBUI_SIGNIN_SIGNIN_HELPER_CHROMEOS_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