Commit 56e93a34 authored by anthonyvd's avatar anthonyvd Committed by Commit bot

Change ProfileDownloader to use AccountTrackerService.

BUG=466799

Review URL: https://codereview.chromium.org/1091363002

Cr-Commit-Position: refs/heads/master@{#330566}
parent b7bdcdec
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "chrome/browser/chromeos/profiles/profile_helper.h" #include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_downloader.h" #include "chrome/browser/profiles/profile_downloader.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_browser_process.h"
...@@ -216,6 +217,23 @@ class UserImageManagerTest : public LoginManagerTest, ...@@ -216,6 +217,23 @@ class UserImageManagerTest : public LoginManagerTest,
return user_data_dir_.Append(username).AddExtension(extension); return user_data_dir_.Append(username).AddExtension(extension);
} }
// Seeds the AccountTrackerService with test data so the ProfileDownloader can
// retrieve the picture URL and fetch the image.
void SeedAccountTrackerService(const std::string& username,
Profile* profile) {
AccountTrackerService::AccountInfo info;
info.account_id = std::string();
info.gaia = username;
info.email = username;
info.full_name = username;
info.given_name = username;
info.hosted_domain = std::string();
info.locale = username;
info.picture_url = "http://localhost/avatar.jpg";
AccountTrackerServiceFactory::GetForProfile(profile)->SeedAccountInfo(info);
}
// Completes the download of all non-image profile data for the user // Completes the download of all non-image profile data for the user
// |username|. This method must only be called after a profile data // |username|. This method must only be called after a profile data
// download has been started. |url_fetcher_factory| will capture // download has been started. |url_fetcher_factory| will capture
...@@ -234,17 +252,6 @@ class UserImageManagerTest : public LoginManagerTest, ...@@ -234,17 +252,6 @@ class UserImageManagerTest : public LoginManagerTest,
OnGetTokenSuccess(NULL, OnGetTokenSuccess(NULL,
std::string(), std::string(),
base::Time::Now() + base::TimeDelta::FromDays(1)); base::Time::Now() + base::TimeDelta::FromDays(1));
net::TestURLFetcher* fetcher = url_fetcher_factory->GetFetcherByID(
gaia::GaiaOAuthClient::kUrlFetcherId);
ASSERT_TRUE(fetcher);
fetcher->SetResponseString(
"{ \"picture\": \"http://localhost/avatar.jpg\" }");
fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
net::OK));
fetcher->set_response_code(200);
fetcher->delegate()->OnURLFetchComplete(fetcher);
base::RunLoop().RunUntilIdle();
} }
// Completes the download of the currently logged-in user's profile image. // Completes the download of the currently logged-in user's profile image.
...@@ -523,6 +530,8 @@ IN_PROC_BROWSER_TEST_F(UserImageManagerTest, SaveUserImageFromProfileImage) { ...@@ -523,6 +530,8 @@ IN_PROC_BROWSER_TEST_F(UserImageManagerTest, SaveUserImageFromProfileImage) {
UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting(); UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting();
LoginUser(kTestUser1); LoginUser(kTestUser1);
Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
SeedAccountTrackerService(kTestUser1, profile);
run_loop_.reset(new base::RunLoop); run_loop_.reset(new base::RunLoop);
UserImageManager* user_image_manager = UserImageManager* user_image_manager =
...@@ -574,6 +583,8 @@ IN_PROC_BROWSER_TEST_F(UserImageManagerTest, ...@@ -574,6 +583,8 @@ IN_PROC_BROWSER_TEST_F(UserImageManagerTest,
UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting(); UserImageManagerImpl::IgnoreProfileDataDownloadDelayForTesting();
LoginUser(kTestUser1); LoginUser(kTestUser1);
Profile* profile = ProfileHelper::Get()->GetProfileByUserUnsafe(user);
SeedAccountTrackerService(kTestUser1, profile);
run_loop_.reset(new base::RunLoop); run_loop_.reset(new base::RunLoop);
UserImageManager* user_image_manager = UserImageManager* user_image_manager =
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/image_decoder.h" #include "chrome/browser/image_decoder.h"
#include "google_apis/gaia/gaia_oauth_client.h" #include "components/signin/core/browser/account_tracker_service.h"
#include "google_apis/gaia/oauth2_token_service.h" #include "google_apis/gaia/oauth2_token_service.h"
#include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_fetcher_delegate.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
...@@ -27,11 +27,11 @@ class URLFetcher; ...@@ -27,11 +27,11 @@ class URLFetcher;
// 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 gaia::GaiaOAuthClient::Delegate, class ProfileDownloader : public net::URLFetcherDelegate,
public net::URLFetcherDelegate,
public ImageDecoder::ImageRequest, public ImageDecoder::ImageRequest,
public OAuth2TokenService::Observer, public OAuth2TokenService::Observer,
public OAuth2TokenService::Consumer { public OAuth2TokenService::Consumer,
public AccountTrackerService::Observer {
public: public:
enum PictureStatus { enum PictureStatus {
PICTURE_SUCCESS, PICTURE_SUCCESS,
...@@ -82,14 +82,11 @@ class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate, ...@@ -82,14 +82,11 @@ class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate,
private: private:
friend class ProfileDownloaderTest; friend class ProfileDownloaderTest;
FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData); FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, AccountInfoReady);
FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, AccountInfoNotReady);
FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL);
// gaia::GaiaOAuthClient::Delegate implementation. void FetchImageData();
void OnGetUserInfoResponse(
scoped_ptr<base::DictionaryValue> user_info) override;
void OnOAuthError() override;
void OnNetworkError(int response_code) override;
// Overriden from net::URLFetcherDelegate: // Overriden from net::URLFetcherDelegate:
void OnURLFetchComplete(const net::URLFetcher* source) override; void OnURLFetchComplete(const net::URLFetcher* source) override;
...@@ -108,16 +105,11 @@ class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate, ...@@ -108,16 +105,11 @@ class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate,
void OnGetTokenFailure(const OAuth2TokenService::Request* request, void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override; const GoogleServiceAuthError& error) override;
// Parses the entry response and gets the name, profile image URL and locale.
// |data| should be the JSON formatted data return by the response. // Implementation of AccountTrackerService::Observer.
// Returns false to indicate a parsing error. void OnAccountUpdated(
static bool ParseProfileJSON(base::DictionaryValue* root_dictionary, const AccountTrackerService::AccountInfo& info) override;
base::string16* full_name,
base::string16* given_name,
std::string* url,
int image_size,
std::string* profile_locale,
base::string16* hosted_domain);
// Returns true if the image url is url of the default profile picture. // Returns true if the image url is url of the default profile picture.
static bool IsDefaultProfileImageURL(const std::string& url); static bool IsDefaultProfileImageURL(const std::string& url);
...@@ -134,16 +126,13 @@ class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate, ...@@ -134,16 +126,13 @@ class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate,
ProfileDownloaderDelegate* delegate_; ProfileDownloaderDelegate* delegate_;
std::string account_id_; std::string account_id_;
std::string auth_token_; std::string auth_token_;
scoped_ptr<gaia::GaiaOAuthClient> gaia_client_;
scoped_ptr<net::URLFetcher> profile_image_fetcher_; scoped_ptr<net::URLFetcher> profile_image_fetcher_;
scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
base::string16 profile_hosted_domain_; AccountTrackerService::AccountInfo account_info_;
base::string16 profile_full_name_;
base::string16 profile_given_name_;
std::string profile_locale_;
SkBitmap profile_picture_; SkBitmap profile_picture_;
PictureStatus picture_status_; PictureStatus picture_status_;
std::string picture_url_; AccountTrackerService* account_tracker_service_;
bool waiting_for_account_info_;
DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); DISALLOW_COPY_AND_ASSIGN(ProfileDownloader);
}; };
......
...@@ -31,16 +31,24 @@ void FakeAccountTrackerService::StartFetchingUserInfo( ...@@ -31,16 +31,24 @@ void FakeAccountTrackerService::StartFetchingUserInfo(
} }
void FakeAccountTrackerService::FakeUserInfoFetchSuccess( void FakeAccountTrackerService::FakeUserInfoFetchSuccess(
const std::string& account_id,
const std::string& email, const std::string& email,
const std::string& gaia, const std::string& gaia,
const std::string& hosted_domain) { const std::string& hosted_domain,
const std::string& full_name,
const std::string& given_name,
const std::string& locale,
const std::string& picture_url) {
base::DictionaryValue user_info; base::DictionaryValue user_info;
user_info.SetString("id", gaia); user_info.SetString("id", gaia);
user_info.SetString("email", email); user_info.SetString("email", email);
user_info.SetString("hd", hosted_domain); user_info.SetString("hd", hosted_domain);
user_info.SetString("name", full_name);
user_info.SetString("given_name", given_name);
user_info.SetString("locale", locale);
user_info.SetString("picture", picture_url);
std::vector<std::string> service_flags; std::vector<std::string> service_flags;
SetAccountStateFromUserInfo(account_id, &user_info, &service_flags); SetAccountStateFromUserInfo(
PickAccountIdForAccount(gaia, email), &user_info, &service_flags);
} }
void FakeAccountTrackerService::SendRefreshTokenAnnotationRequest( void FakeAccountTrackerService::SendRefreshTokenAnnotationRequest(
......
...@@ -20,10 +20,13 @@ class FakeAccountTrackerService : public AccountTrackerService { ...@@ -20,10 +20,13 @@ class FakeAccountTrackerService : public AccountTrackerService {
public: public:
static KeyedService* Build(content::BrowserContext* context); static KeyedService* Build(content::BrowserContext* context);
void FakeUserInfoFetchSuccess(const std::string& account_id, void FakeUserInfoFetchSuccess(const std::string& email,
const std::string& email,
const std::string& gaia, const std::string& gaia,
const std::string& hosted_domain); const std::string& hosted_domain,
const std::string& full_name,
const std::string& given_name,
const std::string& locale,
const std::string& picture_url);
private: private:
FakeAccountTrackerService(); FakeAccountTrackerService();
......
...@@ -276,8 +276,6 @@ TEST_F(SigninManagerTest, SignInWithRefreshTokenCallsPostSignout) { ...@@ -276,8 +276,6 @@ TEST_F(SigninManagerTest, SignInWithRefreshTokenCallsPostSignout) {
AccountTrackerServiceFactory::GetForProfile(profile())); AccountTrackerServiceFactory::GetForProfile(profile()));
account_tracker_service->SeedAccountInfo(gaia_id, email); account_tracker_service->SeedAccountInfo(gaia_id, email);
account_tracker_service->EnableNetworkFetches(); account_tracker_service->EnableNetworkFetches();
std::string account_id = account_tracker_service->PickAccountIdForAccount(
gaia_id, email);
ASSERT_TRUE(signin_client()->get_signed_in_password().empty()); ASSERT_TRUE(signin_client()->get_signed_in_password().empty());
...@@ -291,8 +289,13 @@ TEST_F(SigninManagerTest, SignInWithRefreshTokenCallsPostSignout) { ...@@ -291,8 +289,13 @@ TEST_F(SigninManagerTest, SignInWithRefreshTokenCallsPostSignout) {
// PostSignedIn is not called until the AccountTrackerService returns. // PostSignedIn is not called until the AccountTrackerService returns.
ASSERT_EQ("", signin_client()->get_signed_in_password()); ASSERT_EQ("", signin_client()->get_signed_in_password());
account_tracker_service->FakeUserInfoFetchSuccess( account_tracker_service->FakeUserInfoFetchSuccess(email,
account_id, email, gaia_id, "google.com"); gaia_id,
"google.com",
"full_name",
"given_name",
"locale",
"http://www.google.com");
// AccountTracker and SigninManager are both done and PostSignedIn was called. // AccountTracker and SigninManager are both done and PostSignedIn was called.
ASSERT_EQ("password", signin_client()->get_signed_in_password()); ASSERT_EQ("password", signin_client()->get_signed_in_password());
......
...@@ -34,6 +34,7 @@ const char kAccountHostedDomainPath[] = "hd"; ...@@ -34,6 +34,7 @@ const char kAccountHostedDomainPath[] = "hd";
const char kAccountFullNamePath[] = "full_name"; const char kAccountFullNamePath[] = "full_name";
const char kAccountGivenNamePath[] = "given_name"; const char kAccountGivenNamePath[] = "given_name";
const char kAccountLocalePath[] = "locale"; const char kAccountLocalePath[] = "locale";
const char kAccountPictureURLPath[] = "picture_url";
const char kAccountServiceFlagsPath[] = "service_flags"; const char kAccountServiceFlagsPath[] = "service_flags";
const base::TimeDelta kRefreshFromTokenServiceDelay = const base::TimeDelta kRefreshFromTokenServiceDelay =
...@@ -53,6 +54,9 @@ bool IsRefreshTokenDeviceIdExperimentEnabled() { ...@@ -53,6 +54,9 @@ bool IsRefreshTokenDeviceIdExperimentEnabled() {
// This must be a string which can never be a valid domain. // This must be a string which can never be a valid domain.
const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN"; const char AccountTrackerService::kNoHostedDomainFound[] = "NO_HOSTED_DOMAIN";
// This must be a string which can never be a valid picture URL.
const char AccountTrackerService::kNoPictureURLFound[] = "NO_PICTURE_URL";
class AccountInfoFetcher : public OAuth2TokenService::Consumer, class AccountInfoFetcher : public OAuth2TokenService::Consumer,
public gaia::GaiaOAuthClient::Delegate, public gaia::GaiaOAuthClient::Delegate,
public GaiaAuthConsumer { public GaiaAuthConsumer {
...@@ -230,10 +234,10 @@ void AccountInfoFetcher::OnNetworkError(int response_code) { ...@@ -230,10 +234,10 @@ void AccountInfoFetcher::OnNetworkError(int response_code) {
AccountTrackerService::AccountInfo::AccountInfo() {} AccountTrackerService::AccountInfo::AccountInfo() {}
AccountTrackerService::AccountInfo::~AccountInfo() {} AccountTrackerService::AccountInfo::~AccountInfo() {}
bool AccountTrackerService::AccountInfo::IsValid() { bool AccountTrackerService::AccountInfo::IsValid() const {
return !account_id.empty() && !email.empty() && !gaia.empty() && return !account_id.empty() && !email.empty() && !gaia.empty() &&
!hosted_domain.empty() && !full_name.empty() && !given_name.empty() && !hosted_domain.empty() && !full_name.empty() && !given_name.empty() &&
!locale.empty(); !locale.empty() && !picture_url.empty();
} }
...@@ -520,6 +524,13 @@ void AccountTrackerService::SetAccountStateFromUserInfo( ...@@ -520,6 +524,13 @@ void AccountTrackerService::SetAccountStateFromUserInfo(
user_info->GetString("given_name", &state.info.given_name); user_info->GetString("given_name", &state.info.given_name);
user_info->GetString("locale", &state.info.locale); user_info->GetString("locale", &state.info.locale);
std::string picture_url;
if(user_info->GetString("picture", &picture_url)) {
state.info.picture_url = picture_url;
} else {
state.info.picture_url = kNoPictureURLFound;
}
state.info.service_flags = *service_flags; state.info.service_flags = *service_flags;
NotifyAccountUpdated(state); NotifyAccountUpdated(state);
...@@ -578,6 +589,8 @@ void AccountTrackerService::LoadFromPrefs() { ...@@ -578,6 +589,8 @@ void AccountTrackerService::LoadFromPrefs() {
state.info.given_name = base::UTF16ToUTF8(value); state.info.given_name = base::UTF16ToUTF8(value);
if (dict->GetString(kAccountLocalePath, &value)) if (dict->GetString(kAccountLocalePath, &value))
state.info.locale = base::UTF16ToUTF8(value); state.info.locale = base::UTF16ToUTF8(value);
if (dict->GetString(kAccountPictureURLPath, &value))
state.info.picture_url = base::UTF16ToUTF8(value);
const base::ListValue* service_flags_list; const base::ListValue* service_flags_list;
if (dict->GetList(kAccountServiceFlagsPath, &service_flags_list)) { if (dict->GetList(kAccountServiceFlagsPath, &service_flags_list)) {
...@@ -626,6 +639,7 @@ void AccountTrackerService::SaveToPrefs(const AccountState& state) { ...@@ -626,6 +639,7 @@ void AccountTrackerService::SaveToPrefs(const AccountState& state) {
dict->SetString(kAccountFullNamePath, state.info.full_name); dict->SetString(kAccountFullNamePath, state.info.full_name);
dict->SetString(kAccountGivenNamePath, state.info.given_name); dict->SetString(kAccountGivenNamePath, state.info.given_name);
dict->SetString(kAccountLocalePath, state.info.locale); dict->SetString(kAccountLocalePath, state.info.locale);
dict->SetString(kAccountPictureURLPath, state.info.picture_url);
scoped_ptr<base::ListValue> service_flags_list; scoped_ptr<base::ListValue> service_flags_list;
service_flags_list.reset(new base::ListValue); service_flags_list.reset(new base::ListValue);
...@@ -759,3 +773,22 @@ std::string AccountTrackerService::SeedAccountInfo(const std::string& gaia, ...@@ -759,3 +773,22 @@ std::string AccountTrackerService::SeedAccountInfo(const std::string& gaia,
return account_id; return account_id;
} }
void AccountTrackerService::SeedAccountInfo(
AccountTrackerService::AccountInfo info) {
info.account_id = PickAccountIdForAccount(info.gaia, info.email);
if (info.hosted_domain.empty()) {
info.hosted_domain = kNoHostedDomainFound;
}
if(info.IsValid()) {
if(!ContainsKey(accounts_, info.account_id)) {
SeedAccountInfo(info.gaia, info.email);
}
AccountState& state = accounts_[info.account_id];
state.info = info;
NotifyAccountUpdated(state);
SaveToPrefs(state);
}
}
...@@ -46,6 +46,9 @@ class AccountTrackerService : public KeyedService, ...@@ -46,6 +46,9 @@ class AccountTrackerService : public KeyedService,
// Value representing no hosted domain in the kProfileHostedDomain preference. // Value representing no hosted domain in the kProfileHostedDomain preference.
static const char kNoHostedDomainFound[]; static const char kNoHostedDomainFound[];
// Value representing no picture URL associated with an account.
static const char kNoPictureURLFound[];
// Information about a specific account. // Information about a specific account.
struct AccountInfo { struct AccountInfo {
AccountInfo(); AccountInfo();
...@@ -58,11 +61,10 @@ class AccountTrackerService : public KeyedService, ...@@ -58,11 +61,10 @@ class AccountTrackerService : public KeyedService,
std::string given_name; std::string given_name;
std::string hosted_domain; std::string hosted_domain;
std::string locale; std::string locale;
std::string picture_url;
std::vector<std::string> service_flags; std::vector<std::string> service_flags;
// TODO(rogerta): eventually this structure will include other information
// about the account, like full name, profile picture URL, etc.
bool IsValid(); bool IsValid() const;
}; };
// Clients of AccountTrackerService can implement this interface and register // Clients of AccountTrackerService can implement this interface and register
...@@ -125,6 +127,7 @@ class AccountTrackerService : public KeyedService, ...@@ -125,6 +127,7 @@ class AccountTrackerService : public KeyedService,
// value PickAccountIdForAccount() when given the same arguments. // value PickAccountIdForAccount() when given the same arguments.
std::string SeedAccountInfo(const std::string& gaia, std::string SeedAccountInfo(const std::string& gaia,
const std::string& email); const std::string& email);
void SeedAccountInfo(AccountInfo info);
AccountIdMigrationState GetMigrationState(); AccountIdMigrationState GetMigrationState();
static AccountIdMigrationState GetMigrationState(PrefService* pref_service); static AccountIdMigrationState GetMigrationState(PrefService* pref_service);
......
...@@ -29,7 +29,8 @@ const std::string kTokenInfoResponseFormat = ...@@ -29,7 +29,8 @@ const std::string kTokenInfoResponseFormat =
\"hd\": \"\", \ \"hd\": \"\", \
\"name\": \"%s\", \ \"name\": \"%s\", \
\"given_name\": \"%s\", \ \"given_name\": \"%s\", \
\"locale\": \"%s\" \ \"locale\": \"%s\", \
\"picture\": \"%s\" \
}"; }";
const std::string kTokenInfoIncompleteResponseFormat = const std::string kTokenInfoIncompleteResponseFormat =
...@@ -68,6 +69,10 @@ std::string AccountIdToLocale(const std::string account_id) { ...@@ -68,6 +69,10 @@ std::string AccountIdToLocale(const std::string account_id) {
return "locale-" + account_id; return "locale-" + account_id;
} }
std::string AccountIdToPictureURL(const std::string account_id) {
return "picture_url-" + account_id;
}
void CheckAccountDetails(const std::string account_id, void CheckAccountDetails(const std::string account_id,
const AccountTrackerService::AccountInfo& info) { const AccountTrackerService::AccountInfo& info) {
EXPECT_EQ(account_id, info.account_id); EXPECT_EQ(account_id, info.account_id);
...@@ -291,7 +296,8 @@ class AccountTrackerServiceTest : public testing::Test { ...@@ -291,7 +296,8 @@ class AccountTrackerServiceTest : public testing::Test {
AccountIdToEmail(account_id).c_str(), AccountIdToEmail(account_id).c_str(),
AccountIdToFullName(account_id).c_str(), AccountIdToFullName(account_id).c_str(),
AccountIdToGivenName(account_id).c_str(), AccountIdToGivenName(account_id).c_str(),
AccountIdToLocale(account_id).c_str()); AccountIdToLocale(account_id).c_str(),
AccountIdToPictureURL(account_id).c_str());
} }
std::string GenerateIncompleteTokenInfoResponse( std::string GenerateIncompleteTokenInfoResponse(
......
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