| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ | 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ |
| 6 #define CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ | 6 #define CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "chrome/browser/image_decoder.h" | 14 #include "chrome/browser/image_decoder.h" |
| 15 #include "google_apis/gaia/gaia_oauth_client.h" | 15 #include "components/signin/core/browser/account_tracker_service.h" |
| 16 #include "google_apis/gaia/oauth2_token_service.h" | 16 #include "google_apis/gaia/oauth2_token_service.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 class ProfileDownloaderDelegate; | 21 class ProfileDownloaderDelegate; |
| 22 class OAuth2AccessTokenFetcher; | 22 class OAuth2AccessTokenFetcher; |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 class URLFetcher; | 25 class URLFetcher; |
| 26 } // namespace net | 26 } // namespace net |
| 27 | 27 |
| 28 // Downloads user profile information. The profile picture is decoded in a | 28 // Downloads user profile information. The profile picture is decoded in a |
| 29 // sandboxed process. | 29 // sandboxed process. |
| 30 class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate, | 30 class ProfileDownloader : public net::URLFetcherDelegate, |
| 31 public net::URLFetcherDelegate, | |
| 32 public ImageDecoder::ImageRequest, | 31 public ImageDecoder::ImageRequest, |
| 33 public OAuth2TokenService::Observer, | 32 public OAuth2TokenService::Observer, |
| 34 public OAuth2TokenService::Consumer { | 33 public OAuth2TokenService::Consumer, |
| 34 public AccountTrackerService::Observer { |
| 35 public: | 35 public: |
| 36 enum PictureStatus { | 36 enum PictureStatus { |
| 37 PICTURE_SUCCESS, | 37 PICTURE_SUCCESS, |
| 38 PICTURE_FAILED, | 38 PICTURE_FAILED, |
| 39 PICTURE_DEFAULT, | 39 PICTURE_DEFAULT, |
| 40 PICTURE_CACHED, | 40 PICTURE_CACHED, |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 explicit ProfileDownloader(ProfileDownloaderDelegate* delegate); | 43 explicit ProfileDownloader(ProfileDownloaderDelegate* delegate, |
| 44 SigninClient* signin_client); |
| 44 ~ProfileDownloader() override; | 45 ~ProfileDownloader() override; |
| 45 | 46 |
| 46 // Starts downloading profile information if the necessary authorization token | 47 // Starts downloading profile information if the necessary authorization token |
| 47 // is ready. If not, subscribes to token service and starts fetching if the | 48 // is ready. If not, subscribes to token service and starts fetching if the |
| 48 // token is available. Should not be called more than once. | 49 // token is available. Should not be called more than once. |
| 49 virtual void Start(); | 50 virtual void Start(); |
| 50 | 51 |
| 51 // Starts downloading profile information if the necessary authorization token | 52 // Starts downloading profile information if the necessary authorization token |
| 52 // is ready. If not, subscribes to token service and starts fetching if the | 53 // is ready. If not, subscribes to token service and starts fetching if the |
| 53 // token is available. Should not be called more than once. | 54 // token is available. Should not be called more than once. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 // Gets the profile picture status. | 76 // Gets the profile picture status. |
| 76 virtual PictureStatus GetProfilePictureStatus() const; | 77 virtual PictureStatus GetProfilePictureStatus() const; |
| 77 | 78 |
| 78 // Gets the URL for the profile picture. This can be cached so that the same | 79 // Gets the URL for the profile picture. This can be cached so that the same |
| 79 // picture is not downloaded multiple times. This value should only be used | 80 // picture is not downloaded multiple times. This value should only be used |
| 80 // when the picture status is PICTURE_SUCCESS. | 81 // when the picture status is PICTURE_SUCCESS. |
| 81 virtual std::string GetProfilePictureURL() const; | 82 virtual std::string GetProfilePictureURL() const; |
| 82 | 83 |
| 83 private: | 84 private: |
| 84 friend class ProfileDownloaderTest; | 85 friend class ProfileDownloaderTest; |
| 85 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData); | 86 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, AccountInfoReady); |
| 87 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, AccountInfoNotReady); |
| 86 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); | 88 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); |
| 87 | 89 |
| 88 // gaia::GaiaOAuthClient::Delegate implementation. | 90 void FetchImageData(); |
| 89 void OnGetUserInfoResponse( | |
| 90 scoped_ptr<base::DictionaryValue> user_info) override; | |
| 91 void OnOAuthError() override; | |
| 92 void OnNetworkError(int response_code) override; | |
| 93 | 91 |
| 94 // Overriden from net::URLFetcherDelegate: | 92 // Overriden from net::URLFetcherDelegate: |
| 95 void OnURLFetchComplete(const net::URLFetcher* source) override; | 93 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 96 | 94 |
| 97 // Overriden from ImageDecoder::ImageRequest: | 95 // Overriden from ImageDecoder::ImageRequest: |
| 98 void OnImageDecoded(const SkBitmap& decoded_image) override; | 96 void OnImageDecoded(const SkBitmap& decoded_image) override; |
| 99 void OnDecodeImageFailed() override; | 97 void OnDecodeImageFailed() override; |
| 100 | 98 |
| 101 // Overriden from OAuth2TokenService::Observer: | 99 // Overriden from OAuth2TokenService::Observer: |
| 102 void OnRefreshTokenAvailable(const std::string& account_id) override; | 100 void OnRefreshTokenAvailable(const std::string& account_id) override; |
| 103 | 101 |
| 104 // Overriden from OAuth2TokenService::Consumer: | 102 // Overriden from OAuth2TokenService::Consumer: |
| 105 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 103 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 106 const std::string& access_token, | 104 const std::string& access_token, |
| 107 const base::Time& expiration_time) override; | 105 const base::Time& expiration_time) override; |
| 108 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 106 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 109 const GoogleServiceAuthError& error) override; | 107 const GoogleServiceAuthError& error) override; |
| 110 | 108 |
| 111 // Parses the entry response and gets the name, profile image URL and locale. | 109 |
| 112 // |data| should be the JSON formatted data return by the response. | 110 // Implementation of AccountTrackerService::Observer. |
| 113 // Returns false to indicate a parsing error. | 111 void OnAccountUpdated( |
| 114 static bool ParseProfileJSON(base::DictionaryValue* root_dictionary, | 112 const AccountTrackerService::AccountInfo& info) override; |
| 115 base::string16* full_name, | 113 |
| 116 base::string16* given_name, | |
| 117 std::string* url, | |
| 118 int image_size, | |
| 119 std::string* profile_locale, | |
| 120 base::string16* hosted_domain); | |
| 121 // Returns true if the image url is url of the default profile picture. | 114 // Returns true if the image url is url of the default profile picture. |
| 122 static bool IsDefaultProfileImageURL(const std::string& url); | 115 static bool IsDefaultProfileImageURL(const std::string& url); |
| 123 | 116 |
| 124 // Issues the first request to get user profile image. | 117 // Issues the first request to get user profile image. |
| 125 void StartFetchingImage(); | 118 void StartFetchingImage(); |
| 126 | 119 |
| 127 // Gets the authorization header. | 120 // Gets the authorization header. |
| 128 const char* GetAuthorizationHeader() const; | 121 const char* GetAuthorizationHeader() const; |
| 129 | 122 |
| 130 // Starts fetching OAuth2 access token. This is needed before the GAIA info | 123 // Starts fetching OAuth2 access token. This is needed before the GAIA info |
| 131 // can be downloaded. | 124 // can be downloaded. |
| 132 void StartFetchingOAuth2AccessToken(); | 125 void StartFetchingOAuth2AccessToken(); |
| 133 | 126 |
| 134 ProfileDownloaderDelegate* delegate_; | 127 ProfileDownloaderDelegate* delegate_; |
| 135 std::string account_id_; | 128 std::string account_id_; |
| 136 std::string auth_token_; | 129 std::string auth_token_; |
| 137 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_; | |
| 138 scoped_ptr<net::URLFetcher> profile_image_fetcher_; | 130 scoped_ptr<net::URLFetcher> profile_image_fetcher_; |
| 139 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; | 131 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; |
| 140 base::string16 profile_hosted_domain_; | 132 AccountTrackerService::AccountInfo account_info_; |
| 141 base::string16 profile_full_name_; | |
| 142 base::string16 profile_given_name_; | |
| 143 std::string profile_locale_; | |
| 144 SkBitmap profile_picture_; | 133 SkBitmap profile_picture_; |
| 145 PictureStatus picture_status_; | 134 PictureStatus picture_status_; |
| 146 std::string picture_url_; | 135 AccountTrackerService* account_tracker_service_; |
| 136 bool waiting_for_account_info_; |
| 137 |
| 138 SigninClient* signin_client_; |
| 147 | 139 |
| 148 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); | 140 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); |
| 149 }; | 141 }; |
| 150 | 142 |
| 151 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ | 143 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ |
| OLD | NEW |