1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/image_decoder.h"
15 #include "components/signin/core/browser/account_info.h"
16 #include "components/signin/core/browser/account_tracker_service.h"
17 #include "google_apis/gaia/oauth2_token_service.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
22 class ProfileDownloaderDelegate
;
23 class OAuth2AccessTokenFetcher
;
29 // Downloads user profile information. The profile picture is decoded in a
31 class ProfileDownloader
: public net::URLFetcherDelegate
,
32 public ImageDecoder::ImageRequest
,
33 public OAuth2TokenService::Observer
,
34 public OAuth2TokenService::Consumer
,
35 public AccountTrackerService::Observer
{
44 explicit ProfileDownloader(ProfileDownloaderDelegate
* delegate
);
45 ~ProfileDownloader() override
;
47 // Starts downloading profile information if the necessary authorization token
48 // is ready. If not, subscribes to token service and starts fetching if the
49 // token is available. Should not be called more than once.
52 // Starts downloading profile information if the necessary authorization token
53 // is ready. If not, subscribes to token service and starts fetching if the
54 // token is available. Should not be called more than once.
55 virtual void StartForAccount(const std::string
& account_id
);
57 // On successful download this returns the hosted domain of the user.
58 virtual base::string16
GetProfileHostedDomain() const;
60 // On successful download this returns the full name of the user. For example
62 virtual base::string16
GetProfileFullName() const;
64 // On successful download this returns the given name of the user. For example
65 // if the name is "Pat Smith", the given name is "Pat".
66 virtual base::string16
GetProfileGivenName() const;
68 // On successful download this returns G+ locale preference of the user.
69 virtual std::string
GetProfileLocale() const;
71 // On successful download this returns the profile picture of the user.
72 // For users with no profile picture set (that is, they have the default
73 // profile picture) this will return an Null bitmap.
74 virtual SkBitmap
GetProfilePicture() const;
76 // Gets the profile picture status.
77 virtual PictureStatus
GetProfilePictureStatus() const;
79 // Gets the URL for the profile picture. This can be cached so that the same
80 // picture is not downloaded multiple times. This value should only be used
81 // when the picture status is PICTURE_SUCCESS.
82 virtual std::string
GetProfilePictureURL() const;
85 friend class ProfileDownloaderTest
;
86 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest
, AccountInfoReady
);
87 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest
, AccountInfoNotReady
);
88 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest
, DefaultURL
);
90 void FetchImageData();
92 // Overriden from net::URLFetcherDelegate:
93 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
95 // Overriden from ImageDecoder::ImageRequest:
96 void OnImageDecoded(const SkBitmap
& decoded_image
) override
;
97 void OnDecodeImageFailed() override
;
99 // Overriden from OAuth2TokenService::Observer:
100 void OnRefreshTokenAvailable(const std::string
& account_id
) override
;
102 // Overriden from OAuth2TokenService::Consumer:
103 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
104 const std::string
& access_token
,
105 const base::Time
& expiration_time
) override
;
106 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
107 const GoogleServiceAuthError
& error
) override
;
110 // Implementation of AccountTrackerService::Observer.
111 void OnAccountUpdated(const AccountInfo
& info
) override
;
113 // Returns true if the image url is url of the default profile picture.
114 static bool IsDefaultProfileImageURL(const std::string
& url
);
116 // Issues the first request to get user profile image.
117 void StartFetchingImage();
119 // Gets the authorization header.
120 const char* GetAuthorizationHeader() const;
122 // Starts fetching OAuth2 access token. This is needed before the GAIA info
123 // can be downloaded.
124 void StartFetchingOAuth2AccessToken();
126 ProfileDownloaderDelegate
* delegate_
;
127 std::string account_id_
;
128 std::string auth_token_
;
129 scoped_ptr
<net::URLFetcher
> profile_image_fetcher_
;
130 scoped_ptr
<OAuth2TokenService::Request
> oauth2_access_token_request_
;
131 AccountInfo account_info_
;
132 SkBitmap profile_picture_
;
133 PictureStatus picture_status_
;
134 AccountTrackerService
* account_tracker_service_
;
135 bool waiting_for_account_info_
;
137 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader
);
140 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_