Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_downloader.h
bloba1787731f855728eeb8377aa7a4627d8451ae6ad
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_
8 #include <string>
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 "google_apis/gaia/gaia_oauth_client.h"
16 #include "google_apis/gaia/oauth2_token_service.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "url/gurl.h"
21 class ProfileDownloaderDelegate;
22 class OAuth2AccessTokenFetcher;
24 namespace net {
25 class URLFetcher;
26 } // namespace net
28 // Downloads user profile information. The profile picture is decoded in a
29 // sandboxed process.
30 class ProfileDownloader : public gaia::GaiaOAuthClient::Delegate,
31 public net::URLFetcherDelegate,
32 public ImageDecoder::Delegate,
33 public OAuth2TokenService::Observer,
34 public OAuth2TokenService::Consumer {
35 public:
36 enum PictureStatus {
37 PICTURE_SUCCESS,
38 PICTURE_FAILED,
39 PICTURE_DEFAULT,
40 PICTURE_CACHED,
43 explicit ProfileDownloader(ProfileDownloaderDelegate* delegate);
44 virtual ~ProfileDownloader();
46 // Starts downloading profile information if the necessary authorization token
47 // 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 virtual void Start();
51 // Starts downloading profile information if the necessary authorization token
52 // 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 virtual void StartForAccount(const std::string& account_id);
56 // On successful download this returns the full name of the user. For example
57 // "Pat Smith".
58 virtual base::string16 GetProfileFullName() const;
60 // On successful download this returns the given name of the user. For example
61 // if the name is "Pat Smith", the given name is "Pat".
62 virtual base::string16 GetProfileGivenName() const;
64 // On successful download this returns G+ locale preference of the user.
65 virtual std::string GetProfileLocale() const;
67 // On successful download this returns the profile picture of the user.
68 // For users with no profile picture set (that is, they have the default
69 // profile picture) this will return an Null bitmap.
70 virtual SkBitmap GetProfilePicture() const;
72 // Gets the profile picture status.
73 virtual PictureStatus GetProfilePictureStatus() const;
75 // 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
77 // when the picture status is PICTURE_SUCCESS.
78 virtual std::string GetProfilePictureURL() const;
80 private:
81 friend class ProfileDownloaderTest;
82 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData);
83 FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL);
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;
91 // Overriden from net::URLFetcherDelegate:
92 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
94 // Overriden from ImageDecoder::Delegate:
95 virtual void OnImageDecoded(const ImageDecoder* decoder,
96 const SkBitmap& decoded_image) OVERRIDE;
97 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE;
99 // Overriden from OAuth2TokenService::Observer:
100 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
102 // Overriden from OAuth2TokenService::Consumer:
103 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
104 const std::string& access_token,
105 const base::Time& expiration_time) OVERRIDE;
106 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
107 const GoogleServiceAuthError& error) OVERRIDE;
109 // 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.
111 // Returns false to indicate a parsing error.
112 static bool ParseProfileJSON(base::DictionaryValue* root_dictionary,
113 base::string16* full_name,
114 base::string16* given_name,
115 std::string* url,
116 int image_size,
117 std::string* profile_locale);
118 // Returns true if the image url is url of the default profile picture.
119 static bool IsDefaultProfileImageURL(const std::string& url);
121 // Issues the first request to get user profile image.
122 void StartFetchingImage();
124 // Gets the authorization header.
125 const char* GetAuthorizationHeader() const;
127 // Starts fetching OAuth2 access token. This is needed before the GAIA info
128 // can be downloaded.
129 void StartFetchingOAuth2AccessToken();
131 ProfileDownloaderDelegate* delegate_;
132 std::string account_id_;
133 std::string auth_token_;
134 scoped_ptr<gaia::GaiaOAuthClient> gaia_client_;
135 scoped_ptr<net::URLFetcher> profile_image_fetcher_;
136 scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
137 base::string16 profile_full_name_;
138 base::string16 profile_given_name_;
139 std::string profile_locale_;
140 SkBitmap profile_picture_;
141 PictureStatus picture_status_;
142 std::string picture_url_;
144 DISALLOW_COPY_AND_ASSIGN(ProfileDownloader);
147 #endif // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_