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