Commit 0bb40607 authored by Jochen Eisinger's avatar Jochen Eisinger Committed by Commit Bot

Migrate ProfileDownloader to PrimaryAccountAccessTokenFetcher

BUG=801969
R=msarda@chromium.org

Change-Id: Ibfc22ab735f62622286def651ae5df065e59ebc7
Reviewed-on: https://chromium-review.googlesource.com/1163620
Commit-Queue: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581508}
parent b38bb1df
...@@ -21,21 +21,17 @@ ...@@ -21,21 +21,17 @@
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/account_fetcher_service_factory.h" #include "chrome/browser/signin/account_fetcher_service_factory.h"
#include "chrome/browser/signin/account_tracker_service_factory.h" #include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/signin/chrome_signin_client_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/data_use_measurement/core/data_use_user_data.h" #include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/signin/core/browser/account_fetcher_service.h" #include "components/signin/core/browser/account_fetcher_service.h"
#include "components/signin/core/browser/avatar_icon_util.h" #include "components/signin/core/browser/avatar_icon_util.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_client.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "google_apis/gaia/gaia_constants.h" #include "google_apis/gaia/gaia_constants.h"
#include "net/base/load_flags.h" #include "net/base/load_flags.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/identity/public/cpp/primary_account_access_token_fetcher.h"
#include "skia/ext/image_operations.h" #include "skia/ext/image_operations.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -49,12 +45,13 @@ constexpr char kAuthorizationHeader[] = "Bearer %s"; ...@@ -49,12 +45,13 @@ constexpr char kAuthorizationHeader[] = "Bearer %s";
} // namespace } // namespace
ProfileDownloader::ProfileDownloader(ProfileDownloaderDelegate* delegate) ProfileDownloader::ProfileDownloader(ProfileDownloaderDelegate* delegate)
: OAuth2TokenService::Consumer("profile_downloader"), : delegate_(delegate),
delegate_(delegate),
picture_status_(PICTURE_FAILED), picture_status_(PICTURE_FAILED),
account_tracker_service_( account_tracker_service_(AccountTrackerServiceFactory::GetForProfile(
AccountTrackerServiceFactory::GetForProfile( delegate_->GetBrowserProfile())),
delegate_->GetBrowserProfile())), identity_manager_(IdentityManagerFactory::GetForProfile(
delegate_->GetBrowserProfile())),
identity_manager_observer_(this),
waiting_for_account_info_(false) { waiting_for_account_info_(false) {
DCHECK(delegate_); DCHECK(delegate_);
account_tracker_service_->AddObserver(this); account_tracker_service_->AddObserver(this);
...@@ -68,26 +65,21 @@ void ProfileDownloader::StartForAccount(const std::string& account_id) { ...@@ -68,26 +65,21 @@ void ProfileDownloader::StartForAccount(const std::string& account_id) {
VLOG(1) << "Starting profile downloader..."; VLOG(1) << "Starting profile downloader...";
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
ProfileOAuth2TokenService* service = if (!identity_manager_) {
ProfileOAuth2TokenServiceFactory::GetForProfile(
delegate_->GetBrowserProfile());
if (!service) {
// This can happen in some test paths. // This can happen in some test paths.
LOG(WARNING) << "User has no token service"; LOG(WARNING) << "User has no identity manager";
delegate_->OnProfileDownloadFailure( delegate_->OnProfileDownloadFailure(
this, ProfileDownloaderDelegate::TOKEN_ERROR); this, ProfileDownloaderDelegate::TOKEN_ERROR);
return; return;
} }
SigninManagerBase* signin_manager = account_id_ = account_id.empty()
SigninManagerFactory::GetForProfile(delegate_->GetBrowserProfile()); ? identity_manager_->GetPrimaryAccountInfo().account_id
account_id_ = : account_id;
account_id.empty() ? if (identity_manager_->HasAccountWithRefreshToken(account_id_))
signin_manager->GetAuthenticatedAccountId() : account_id;
if (service->RefreshTokenIsAvailable(account_id_))
StartFetchingOAuth2AccessToken(); StartFetchingOAuth2AccessToken();
else else
service->AddObserver(this); identity_manager_observer_.Add(identity_manager_);
} }
base::string16 ProfileDownloader::GetProfileHostedDomain() const { base::string16 ProfileDownloader::GetProfileHostedDomain() const {
...@@ -145,26 +137,21 @@ void ProfileDownloader::StartFetchingImage() { ...@@ -145,26 +137,21 @@ void ProfileDownloader::StartFetchingImage() {
} }
void ProfileDownloader::StartFetchingOAuth2AccessToken() { void ProfileDownloader::StartFetchingOAuth2AccessToken() {
Profile* profile = delegate_->GetBrowserProfile();
OAuth2TokenService::ScopeSet scopes; OAuth2TokenService::ScopeSet scopes;
scopes.insert(GaiaConstants::kGoogleUserInfoProfile); scopes.insert(GaiaConstants::kGoogleUserInfoProfile);
// Required to determine if lock should be enabled. // Required to determine if lock should be enabled.
scopes.insert(GaiaConstants::kGoogleUserInfoEmail); scopes.insert(GaiaConstants::kGoogleUserInfoEmail);
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile); oauth2_access_token_fetcher_ =
oauth2_access_token_request_ = token_service->StartRequest( std::make_unique<identity::PrimaryAccountAccessTokenFetcher>(
account_id_, scopes, this); "profile_downloader", identity_manager_, scopes,
base::BindOnce(&ProfileDownloader::OnAccessTokenFetchComplete,
base::Unretained(this)),
identity::PrimaryAccountAccessTokenFetcher::Mode::kImmediate);
} }
ProfileDownloader::~ProfileDownloader() { ProfileDownloader::~ProfileDownloader() {
// Ensures PO2TS observation is cleared when ProfileDownloader is destructed oauth2_access_token_fetcher_.reset();
// before refresh token is available.
ProfileOAuth2TokenService* service =
ProfileOAuth2TokenServiceFactory::GetForProfile(
delegate_->GetBrowserProfile());
if (service)
service->RemoveObserver(this);
account_tracker_service_->RemoveObserver(this); account_tracker_service_->RemoveObserver(this);
} }
...@@ -302,41 +289,32 @@ void ProfileDownloader::OnDecodeImageFailed() { ...@@ -302,41 +289,32 @@ void ProfileDownloader::OnDecodeImageFailed() {
this, ProfileDownloaderDelegate::IMAGE_DECODE_FAILED); this, ProfileDownloaderDelegate::IMAGE_DECODE_FAILED);
} }
void ProfileDownloader::OnRefreshTokenAvailable(const std::string& account_id) { void ProfileDownloader::OnRefreshTokenUpdatedForAccount(
ProfileOAuth2TokenService* service = const AccountInfo& account_info,
ProfileOAuth2TokenServiceFactory::GetForProfile( bool is_valid) {
delegate_->GetBrowserProfile()); if (!is_valid || account_info.account_id != account_id_)
if (account_id != account_id_)
return; return;
service->RemoveObserver(this); identity_manager_observer_.Remove(identity_manager_);
StartFetchingOAuth2AccessToken(); StartFetchingOAuth2AccessToken();
} }
// Callback for OAuth2TokenService::Request on success. |access_token| is the void ProfileDownloader::OnAccessTokenFetchComplete(
// token used to start fetching user data. GoogleServiceAuthError error,
void ProfileDownloader::OnGetTokenSuccess( identity::AccessTokenInfo access_token_info) {
const OAuth2TokenService::Request* request, oauth2_access_token_fetcher_.reset();
const std::string& access_token, if (error.state() != GoogleServiceAuthError::NONE) {
const base::Time& expiration_time) { LOG(WARNING)
DCHECK_EQ(request, oauth2_access_token_request_.get()); << "ProfileDownloader: token request using refresh token failed:"
oauth2_access_token_request_.reset(); << error.ToString();
auth_token_ = access_token; delegate_->OnProfileDownloadFailure(this,
ProfileDownloaderDelegate::TOKEN_ERROR);
return;
}
auth_token_ = access_token_info.token;
StartFetchingImage(); StartFetchingImage();
} }
// Callback for OAuth2TokenService::Request on failure.
void ProfileDownloader::OnGetTokenFailure(
const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) {
DCHECK_EQ(request, oauth2_access_token_request_.get());
oauth2_access_token_request_.reset();
LOG(WARNING) << "ProfileDownloader: token request using refresh token failed:"
<< error.ToString();
delegate_->OnProfileDownloadFailure(
this, ProfileDownloaderDelegate::TOKEN_ERROR);
}
void ProfileDownloader::OnAccountUpdated(const AccountInfo& info) { void ProfileDownloader::OnAccountUpdated(const AccountInfo& info) {
if (info.account_id == account_id_ && info.IsValid()) { if (info.account_id == account_id_ && info.IsValid()) {
account_info_ = info; account_info_ = info;
......
...@@ -10,22 +10,25 @@ ...@@ -10,22 +10,25 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/scoped_observer.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/image_decoder.h" #include "chrome/browser/image_decoder.h"
#include "components/signin/core/browser/account_info.h" #include "components/signin/core/browser/account_info.h"
#include "components/signin/core/browser/account_tracker_service.h" #include "components/signin/core/browser/account_tracker_service.h"
#include "google_apis/gaia/oauth2_token_service.h" #include "services/identity/public/cpp/identity_manager.h"
#include "services/network/public/cpp/simple_url_loader.h" #include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h" #include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
namespace identity {
class PrimaryAccountAccessTokenFetcher;
}
class ProfileDownloaderDelegate; class ProfileDownloaderDelegate;
// Downloads user profile information. The profile picture is decoded in a // Downloads user profile information. The profile picture is decoded in a
// sandboxed process. // sandboxed process.
class ProfileDownloader : public ImageDecoder::ImageRequest, class ProfileDownloader : public ImageDecoder::ImageRequest,
public OAuth2TokenService::Observer, public identity::IdentityManager::Observer,
public OAuth2TokenService::Consumer,
public AccountTrackerService::Observer { public AccountTrackerService::Observer {
public: public:
enum PictureStatus { enum PictureStatus {
...@@ -92,20 +95,17 @@ class ProfileDownloader : public ImageDecoder::ImageRequest, ...@@ -92,20 +95,17 @@ class ProfileDownloader : public ImageDecoder::ImageRequest,
void OnImageDecoded(const SkBitmap& decoded_image) override; void OnImageDecoded(const SkBitmap& decoded_image) override;
void OnDecodeImageFailed() override; void OnDecodeImageFailed() override;
// Overriden from OAuth2TokenService::Observer: // Overriden from identity::IdentityManager::Observer:
void OnRefreshTokenAvailable(const std::string& account_id) override; void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info,
bool is_valid) override;
// Overriden from OAuth2TokenService::Consumer:
void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
const std::string& access_token,
const base::Time& expiration_time) override;
void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override;
// Implementation of AccountTrackerService::Observer. // Implementation of AccountTrackerService::Observer.
void OnAccountUpdated(const AccountInfo& info) override; void OnAccountUpdated(const AccountInfo& info) override;
// Callback for PrimaryAccountAccessTokenFetcher.
void OnAccessTokenFetchComplete(GoogleServiceAuthError error,
identity::AccessTokenInfo access_token_info);
// Issues the first request to get user profile image. // Issues the first request to get user profile image.
void StartFetchingImage(); void StartFetchingImage();
...@@ -120,11 +120,15 @@ class ProfileDownloader : public ImageDecoder::ImageRequest, ...@@ -120,11 +120,15 @@ class ProfileDownloader : public ImageDecoder::ImageRequest,
std::string account_id_; std::string account_id_;
std::string auth_token_; std::string auth_token_;
std::unique_ptr<network::SimpleURLLoader> simple_loader_; std::unique_ptr<network::SimpleURLLoader> simple_loader_;
std::unique_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; std::unique_ptr<identity::PrimaryAccountAccessTokenFetcher>
oauth2_access_token_fetcher_;
AccountInfo account_info_; AccountInfo account_info_;
SkBitmap profile_picture_; SkBitmap profile_picture_;
PictureStatus picture_status_; PictureStatus picture_status_;
AccountTrackerService* account_tracker_service_; AccountTrackerService* account_tracker_service_;
identity::IdentityManager* identity_manager_;
ScopedObserver<identity::IdentityManager, identity::IdentityManager::Observer>
identity_manager_observer_;
bool waiting_for_account_info_; bool waiting_for_account_info_;
DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); DISALLOW_COPY_AND_ASSIGN(ProfileDownloader);
......
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