| 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 "google_apis/gaia/oauth2_token_service.h" | 16 #include "google_apis/gaia/oauth2_token_service.h" |
| 16 #include "net/url_request/url_fetcher_delegate.h" | 17 #include "net/url_request/url_fetcher_delegate.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 class ProfileDownloaderDelegate; | 21 class ProfileDownloaderDelegate; |
| 21 class OAuth2AccessTokenFetcher; | 22 class OAuth2AccessTokenFetcher; |
| 22 | 23 |
| 23 namespace net { | 24 namespace net { |
| 24 class URLFetcher; | 25 class URLFetcher; |
| 25 } // namespace net | 26 } // namespace net |
| 26 | 27 |
| 27 // Downloads user profile information. The profile picture is decoded in a | 28 // Downloads user profile information. The profile picture is decoded in a |
| 28 // sandboxed process. | 29 // sandboxed process. |
| 29 class ProfileDownloader : public net::URLFetcherDelegate, | 30 class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate, |
| 31 public net::URLFetcherDelegate, |
| 30 public ImageDecoder::Delegate, | 32 public ImageDecoder::Delegate, |
| 31 public OAuth2TokenService::Observer, | 33 public OAuth2TokenService::Observer, |
| 32 public OAuth2TokenService::Consumer { | 34 public OAuth2TokenService::Consumer { |
| 33 public: | 35 public: |
| 34 enum PictureStatus { | 36 enum PictureStatus { |
| 35 PICTURE_SUCCESS, | 37 PICTURE_SUCCESS, |
| 36 PICTURE_FAILED, | 38 PICTURE_FAILED, |
| 37 PICTURE_DEFAULT, | 39 PICTURE_DEFAULT, |
| 38 PICTURE_CACHED, | 40 PICTURE_CACHED, |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 explicit ProfileDownloader(ProfileDownloaderDelegate* delegate); | 43 explicit ProfileDownloader(ProfileDownloaderDelegate* delegate); |
| 42 virtual ~ProfileDownloader(); | 44 virtual ~ProfileDownloader(); |
| 43 | 45 |
| 44 // Starts downloading profile information if the necessary authorization token | 46 // Starts downloading profile information if the necessary authorization token |
| 45 // is ready. If not, subscribes to token service and starts fetching if the | 47 // is ready. If not, subscribes to token service and starts fetching if the |
| 46 // token is available. Should not be called more than once. | 48 // token is available. Should not be called more than once. |
| 47 virtual void Start(); | 49 virtual void Start(); |
| 48 | 50 |
| 49 // Starts downloading profile information if the necessary authorization token | 51 // Starts downloading profile information if the necessary authorization token |
| 50 // is ready. If not, subscribes to token service and starts fetching if the | 52 // is ready. If not, subscribes to token service and starts fetching if the |
| 51 // token is available. Should not be called more than once. | 53 // token is available. Should not be called more than once. |
| 52 virtual void StartForAccount(const std::string& account_id); | 54 virtual void StartForAccount(const std::string& account_id); |
| 53 | 55 |
| 56 // On successful download this returns the hosted domain of the user. |
| 57 virtual base::string16 GetProfileHostedDomain() const; |
| 58 |
| 54 // On successful download this returns the full name of the user. For example | 59 // On successful download this returns the full name of the user. For example |
| 55 // "Pat Smith". | 60 // "Pat Smith". |
| 56 virtual base::string16 GetProfileFullName() const; | 61 virtual base::string16 GetProfileFullName() const; |
| 57 | 62 |
| 58 // On successful download this returns the given name of the user. For example | 63 // On successful download this returns the given name of the user. For example |
| 59 // if the name is "Pat Smith", the given name is "Pat". | 64 // if the name is "Pat Smith", the given name is "Pat". |
| 60 virtual base::string16 GetProfileGivenName() const; | 65 virtual base::string16 GetProfileGivenName() const; |
| 61 | 66 |
| 62 // On successful download this returns G+ locale preference of the user. | 67 // On successful download this returns G+ locale preference of the user. |
| 63 virtual std::string GetProfileLocale() const; | 68 virtual std::string GetProfileLocale() const; |
| 64 | 69 |
| 65 // On successful download this returns the profile picture of the user. | 70 // On successful download this returns the profile picture of the user. |
| 66 // For users with no profile picture set (that is, they have the default | 71 // For users with no profile picture set (that is, they have the default |
| 67 // profile picture) this will return an Null bitmap. | 72 // profile picture) this will return an Null bitmap. |
| 68 virtual SkBitmap GetProfilePicture() const; | 73 virtual SkBitmap GetProfilePicture() const; |
| 69 | 74 |
| 70 // Gets the profile picture status. | 75 // Gets the profile picture status. |
| 71 virtual PictureStatus GetProfilePictureStatus() const; | 76 virtual PictureStatus GetProfilePictureStatus() const; |
| 72 | 77 |
| 73 // Gets the URL for the profile picture. This can be cached so that the same | 78 // Gets the URL for the profile picture. This can be cached so that the same |
| 74 // picture is not downloaded multiple times. This value should only be used | 79 // picture is not downloaded multiple times. This value should only be used |
| 75 // when the picture status is PICTURE_SUCCESS. | 80 // when the picture status is PICTURE_SUCCESS. |
| 76 virtual std::string GetProfilePictureURL() const; | 81 virtual std::string GetProfilePictureURL() const; |
| 77 | 82 |
| 78 private: | 83 private: |
| 79 friend class ProfileDownloaderTest; | 84 friend class ProfileDownloaderTest; |
| 80 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData); | 85 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData); |
| 81 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); | 86 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); |
| 82 | 87 |
| 88 // gaia::GaiaOAuthClient::Delegate implementation. |
| 89 virtual void OnGetUserInfoResponse( |
| 90 scoped_ptr<base::DictionaryValue> user_info) OVERRIDE; |
| 91 virtual void OnOAuthError() OVERRIDE; |
| 92 virtual void OnNetworkError(int response_code) OVERRIDE; |
| 93 |
| 83 // Overriden from net::URLFetcherDelegate: | 94 // Overriden from net::URLFetcherDelegate: |
| 84 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 95 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 85 | 96 |
| 86 // Overriden from ImageDecoder::Delegate: | 97 // Overriden from ImageDecoder::Delegate: |
| 87 virtual void OnImageDecoded(const ImageDecoder* decoder, | 98 virtual void OnImageDecoded(const ImageDecoder* decoder, |
| 88 const SkBitmap& decoded_image) OVERRIDE; | 99 const SkBitmap& decoded_image) OVERRIDE; |
| 89 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; | 100 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; |
| 90 | 101 |
| 91 // Overriden from OAuth2TokenService::Observer: | 102 // Overriden from OAuth2TokenService::Observer: |
| 92 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 103 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
| 93 | 104 |
| 94 // Overriden from OAuth2TokenService::Consumer: | 105 // Overriden from OAuth2TokenService::Consumer: |
| 95 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 106 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 96 const std::string& access_token, | 107 const std::string& access_token, |
| 97 const base::Time& expiration_time) OVERRIDE; | 108 const base::Time& expiration_time) OVERRIDE; |
| 98 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 109 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 99 const GoogleServiceAuthError& error) OVERRIDE; | 110 const GoogleServiceAuthError& error) OVERRIDE; |
| 100 | 111 |
| 101 // Parses the entry response and gets the name, profile image URL and locale. | 112 // Parses the entry response and gets the name, profile image URL and locale. |
| 102 // |data| should be the JSON formatted data return by the response. | 113 // |data| should be the JSON formatted data return by the response. |
| 103 // Returns false to indicate a parsing error. | 114 // Returns false to indicate a parsing error. |
| 104 static bool ParseProfileJSON(const std::string& data, | 115 static bool ParseProfileJSON(base::DictionaryValue* root_dictionary, |
| 105 base::string16* full_name, | 116 base::string16* full_name, |
| 106 base::string16* given_name, | 117 base::string16* given_name, |
| 107 std::string* url, | 118 std::string* url, |
| 108 int image_size, | 119 int image_size, |
| 109 std::string* profile_locale); | 120 std::string* profile_locale, |
| 121 base::string16* hosted_domain); |
| 110 // Returns true if the image url is url of the default profile picture. | 122 // Returns true if the image url is url of the default profile picture. |
| 111 static bool IsDefaultProfileImageURL(const std::string& url); | 123 static bool IsDefaultProfileImageURL(const std::string& url); |
| 112 | 124 |
| 113 // Issues the first request to get user profile image. | 125 // Issues the first request to get user profile image. |
| 114 void StartFetchingImage(); | 126 void StartFetchingImage(); |
| 115 | 127 |
| 116 // Gets the authorization header. | 128 // Gets the authorization header. |
| 117 const char* GetAuthorizationHeader() const; | 129 const char* GetAuthorizationHeader() const; |
| 118 | 130 |
| 119 // Starts fetching OAuth2 access token. This is needed before the GAIA info | 131 // Starts fetching OAuth2 access token. This is needed before the GAIA info |
| 120 // can be downloaded. | 132 // can be downloaded. |
| 121 void StartFetchingOAuth2AccessToken(); | 133 void StartFetchingOAuth2AccessToken(); |
| 122 | 134 |
| 123 ProfileDownloaderDelegate* delegate_; | 135 ProfileDownloaderDelegate* delegate_; |
| 124 std::string account_id_; | 136 std::string account_id_; |
| 125 std::string auth_token_; | 137 std::string auth_token_; |
| 126 scoped_ptr<net::URLFetcher> user_entry_fetcher_; | 138 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_; |
| 127 scoped_ptr<net::URLFetcher> profile_image_fetcher_; | 139 scoped_ptr<net::URLFetcher> profile_image_fetcher_; |
| 128 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; | 140 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; |
| 141 base::string16 profile_hosted_domain_; |
| 129 base::string16 profile_full_name_; | 142 base::string16 profile_full_name_; |
| 130 base::string16 profile_given_name_; | 143 base::string16 profile_given_name_; |
| 131 std::string profile_locale_; | 144 std::string profile_locale_; |
| 132 SkBitmap profile_picture_; | 145 SkBitmap profile_picture_; |
| 133 PictureStatus picture_status_; | 146 PictureStatus picture_status_; |
| 134 std::string picture_url_; | 147 std::string picture_url_; |
| 135 | 148 |
| 136 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); | 149 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); |
| 137 }; | 150 }; |
| 138 | 151 |
| 139 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ | 152 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ |
| OLD | NEW |