Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_downloader.h
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 "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"
20
21 class ProfileDownloaderDelegate;
22 class OAuth2AccessTokenFetcher;
23
24 namespace net {
25 class URLFetcher;
26 }  // namespace net
27
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,
41   };
42
43   explicit ProfileDownloader(ProfileDownloaderDelegate* delegate);
44   ~ProfileDownloader() override;
45
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();
50
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);
55
56   // On successful download this returns the hosted domain of the user.
57   virtual base::string16 GetProfileHostedDomain() const;
58
59   // On successful download this returns the full name of the user. For example
60   // "Pat Smith".
61   virtual base::string16 GetProfileFullName() const;
62
63   // On successful download this returns the given name of the user. For example
64   // if the name is "Pat Smith", the given name is "Pat".
65   virtual base::string16 GetProfileGivenName() const;
66
67   // On successful download this returns G+ locale preference of the user.
68   virtual std::string GetProfileLocale() const;
69
70   // On successful download this returns the profile picture of the user.
71   // For users with no profile picture set (that is, they have the default
72   // profile picture) this will return an Null bitmap.
73   virtual SkBitmap GetProfilePicture() const;
74
75   // Gets the profile picture status.
76   virtual PictureStatus GetProfilePictureStatus() const;
77
78   // 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   // when the picture status is PICTURE_SUCCESS.
81   virtual std::string GetProfilePictureURL() const;
82
83  private:
84   friend class ProfileDownloaderTest;
85   FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, ParseData);
86   FRIEND_TEST_ALL_PREFIXES(ProfileDownloaderTest, DefaultURL);
87
88   // gaia::GaiaOAuthClient::Delegate implementation.
89   void OnGetUserInfoResponse(
90       scoped_ptr<base::DictionaryValue> user_info) override;
91   void OnOAuthError() override;
92   void OnNetworkError(int response_code) override;
93
94   // Overriden from net::URLFetcherDelegate:
95   void OnURLFetchComplete(const net::URLFetcher* source) override;
96
97   // Overriden from ImageDecoder::Delegate:
98   void OnImageDecoded(const ImageDecoder* decoder,
99                       const SkBitmap& decoded_image) override;
100   void OnDecodeImageFailed(const ImageDecoder* decoder) override;
101
102   // Overriden from OAuth2TokenService::Observer:
103   void OnRefreshTokenAvailable(const std::string& account_id) override;
104
105   // Overriden from OAuth2TokenService::Consumer:
106   void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
107                          const std::string& access_token,
108                          const base::Time& expiration_time) override;
109   void OnGetTokenFailure(const OAuth2TokenService::Request* request,
110                          const GoogleServiceAuthError& error) override;
111
112   // Parses the entry response and gets the name, profile image URL and locale.
113   // |data| should be the JSON formatted data return by the response.
114   // Returns false to indicate a parsing error.
115   static bool ParseProfileJSON(base::DictionaryValue* root_dictionary,
116                                base::string16* full_name,
117                                base::string16* given_name,
118                                std::string* url,
119                                int image_size,
120                                std::string* profile_locale,
121                                base::string16* hosted_domain);
122   // Returns true if the image url is url of the default profile picture.
123   static bool IsDefaultProfileImageURL(const std::string& url);
124
125   // Issues the first request to get user profile image.
126   void StartFetchingImage();
127
128   // Gets the authorization header.
129   const char* GetAuthorizationHeader() const;
130
131   // Starts fetching OAuth2 access token. This is needed before the GAIA info
132   // can be downloaded.
133   void StartFetchingOAuth2AccessToken();
134
135   ProfileDownloaderDelegate* delegate_;
136   std::string account_id_;
137   std::string auth_token_;
138   scoped_ptr<gaia::GaiaOAuthClient> gaia_client_;
139   scoped_ptr<net::URLFetcher> profile_image_fetcher_;
140   scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
141   base::string16 profile_hosted_domain_;
142   base::string16 profile_full_name_;
143   base::string16 profile_given_name_;
144   std::string profile_locale_;
145   SkBitmap profile_picture_;
146   PictureStatus picture_status_;
147   std::string picture_url_;
148
149   DISALLOW_COPY_AND_ASSIGN(ProfileDownloader);
150 };
151
152 #endif  // CHROME_BROWSER_PROFILES_PROFILE_DOWNLOADER_H_