| 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" | |
| 16 #include "google_apis/gaia/oauth2_token_service.h" | 15 #include "google_apis/gaia/oauth2_token_service.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 20 | 19 |
| 21 class ProfileDownloaderDelegate; | 20 class ProfileDownloaderDelegate; |
| 22 class OAuth2AccessTokenFetcher; | 21 class OAuth2AccessTokenFetcher; |
| 23 | 22 |
| 24 namespace net { | 23 namespace net { |
| 25 class URLFetcher; | 24 class URLFetcher; |
| 26 } // namespace net | 25 } // namespace net |
| 27 | 26 |
| 28 // Downloads user profile information. The profile picture is decoded in a | 27 // Downloads user profile information. The profile picture is decoded in a |
| 29 // sandboxed process. | 28 // sandboxed process. |
| 30 class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate, | 29 class ProfileDownloader : public net::URLFetcherDelegate, |
| 31 public net::URLFetcherDelegate, | |
| 32 public ImageDecoder::Delegate, | 30 public ImageDecoder::Delegate, |
| 33 public OAuth2TokenService::Observer, | 31 public OAuth2TokenService::Observer, |
| 34 public OAuth2TokenService::Consumer { | 32 public OAuth2TokenService::Consumer { |
| 35 public: | 33 public: |
| 36 enum PictureStatus { | 34 enum PictureStatus { |
| 37 PICTURE_SUCCESS, | 35 PICTURE_SUCCESS, |
| 38 PICTURE_FAILED, | 36 PICTURE_FAILED, |
| 39 PICTURE_DEFAULT, | 37 PICTURE_DEFAULT, |
| 40 PICTURE_CACHED, | 38 PICTURE_CACHED, |
| 41 }; | 39 }; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 // Gets the URL for the profile picture. This can be cached so that the same | 73 // Gets the URL for the profile picture. This can be cached so that the same |
| 76 // picture is not downloaded multiple times. This value should only be used | 74 // picture is not downloaded multiple times. This value should only be used |
| 77 // when the picture status is PICTURE_SUCCESS. | 75 // when the picture status is PICTURE_SUCCESS. |
| 78 virtual std::string GetProfilePictureURL() const; | 76 virtual std::string GetProfilePictureURL() const; |
| 79 | 77 |
| 80 private: | 78 private: |
| 81 friend class ProfileDownloaderTest; | 79 friend class ProfileDownloaderTest; |
| 82 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData); | 80 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData); |
| 83 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); | 81 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL); |
| 84 | 82 |
| 85 // gaia::GaiaOAuthClient::Delegate implementation. | |
| 86 virtual void OnGetUserInfoResponse( | |
| 87 scoped_ptr<base::DictionaryValue> user_info) OVERRIDE; | |
| 88 virtual void OnOAuthError() OVERRIDE; | |
| 89 virtual void OnNetworkError(int response_code) OVERRIDE; | |
| 90 | |
| 91 // Overriden from net::URLFetcherDelegate: | 83 // Overriden from net::URLFetcherDelegate: |
| 92 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 84 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 93 | 85 |
| 94 // Overriden from ImageDecoder::Delegate: | 86 // Overriden from ImageDecoder::Delegate: |
| 95 virtual void OnImageDecoded(const ImageDecoder* decoder, | 87 virtual void OnImageDecoded(const ImageDecoder* decoder, |
| 96 const SkBitmap& decoded_image) OVERRIDE; | 88 const SkBitmap& decoded_image) OVERRIDE; |
| 97 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; | 89 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE; |
| 98 | 90 |
| 99 // Overriden from OAuth2TokenService::Observer: | 91 // Overriden from OAuth2TokenService::Observer: |
| 100 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; | 92 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; |
| 101 | 93 |
| 102 // Overriden from OAuth2TokenService::Consumer: | 94 // Overriden from OAuth2TokenService::Consumer: |
| 103 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 95 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 104 const std::string& access_token, | 96 const std::string& access_token, |
| 105 const base::Time& expiration_time) OVERRIDE; | 97 const base::Time& expiration_time) OVERRIDE; |
| 106 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 98 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 107 const GoogleServiceAuthError& error) OVERRIDE; | 99 const GoogleServiceAuthError& error) OVERRIDE; |
| 108 | 100 |
| 109 // Parses the entry response and gets the name, profile image URL and locale. | 101 // Parses the entry response and gets the name, profile image URL and locale. |
| 110 // |data| should be the JSON formatted data return by the response. | 102 // |data| should be the JSON formatted data return by the response. |
| 111 // Returns false to indicate a parsing error. | 103 // Returns false to indicate a parsing error. |
| 112 static bool ParseProfileJSON(base::DictionaryValue* root_dictionary, | 104 static bool ParseProfileJSON(const std::string& data, |
| 113 base::string16* full_name, | 105 base::string16* full_name, |
| 114 base::string16* given_name, | 106 base::string16* given_name, |
| 115 std::string* url, | 107 std::string* url, |
| 116 int image_size, | 108 int image_size, |
| 117 std::string* profile_locale); | 109 std::string* profile_locale); |
| 118 // Returns true if the image url is url of the default profile picture. | 110 // Returns true if the image url is url of the default profile picture. |
| 119 static bool IsDefaultProfileImageURL(const std::string& url); | 111 static bool IsDefaultProfileImageURL(const std::string& url); |
| 120 | 112 |
| 121 // Issues the first request to get user profile image. | 113 // Issues the first request to get user profile image. |
| 122 void StartFetchingImage(); | 114 void StartFetchingImage(); |
| 123 | 115 |
| 124 // Gets the authorization header. | 116 // Gets the authorization header. |
| 125 const char* GetAuthorizationHeader() const; | 117 const char* GetAuthorizationHeader() const; |
| 126 | 118 |
| 127 // Starts fetching OAuth2 access token. This is needed before the GAIA info | 119 // Starts fetching OAuth2 access token. This is needed before the GAIA info |
| 128 // can be downloaded. | 120 // can be downloaded. |
| 129 void StartFetchingOAuth2AccessToken(); | 121 void StartFetchingOAuth2AccessToken(); |
| 130 | 122 |
| 131 ProfileDownloaderDelegate* delegate_; | 123 ProfileDownloaderDelegate* delegate_; |
| 132 std::string account_id_; | 124 std::string account_id_; |
| 133 std::string auth_token_; | 125 std::string auth_token_; |
| 134 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_; | 126 scoped_ptr<net::URLFetcher> user_entry_fetcher_; |
| 135 scoped_ptr<net::URLFetcher> profile_image_fetcher_; | 127 scoped_ptr<net::URLFetcher> profile_image_fetcher_; |
| 136 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; | 128 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_; |
| 137 base::string16 profile_full_name_; | 129 base::string16 profile_full_name_; |
| 138 base::string16 profile_given_name_; | 130 base::string16 profile_given_name_; |
| 139 std::string profile_locale_; | 131 std::string profile_locale_; |
| 140 SkBitmap profile_picture_; | 132 SkBitmap profile_picture_; |
| 141 PictureStatus picture_status_; | 133 PictureStatus picture_status_; |
| 142 std::string picture_url_; | 134 std::string picture_url_; |
| 143 | 135 |
| 144 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); | 136 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader); |
| 145 }; | 137 }; |
| 146 | 138 |
| 147 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ | 139 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_ |
| OLD | NEW |