Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
      6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "google_apis/gaia/gaia_auth_consumer.h"
     14 #include "google_apis/gaia/gaia_auth_fetcher.h"
     15 #include "google_apis/gaia/oauth2_token_service.h"
     16 
     17 class ProfileOAuth2TokenService;
     18 
     19 namespace net {
     20 class URLRequestContextGetter;
     21 }
     22 
     23 // Downloads an account's list of Gaia service flags.
     24 // On construction, the download starts immediately and calls the given callback
     25 // when either the download is successful or an error is detected. It is valid
     26 // to destruct the object before the callback is called; this will cancel the
     27 // pending request.
     28 class AccountServiceFlagFetcher : public GaiaAuthConsumer,
     29                                   public OAuth2TokenService::Observer,
     30                                   public OAuth2TokenService::Consumer {
     31  public:
     32   enum ResultCode {
     33     SUCCESS,
     34     TOKEN_ERROR,    // Failed to get OAuth2 token.
     35     SERVICE_ERROR,  // Service returned an error or malformed reply.
     36   };
     37 
     38   // If the flag download is successful, this will return the list of service
     39   // flags that are set for the given account.
     40   typedef base::Callback<void(ResultCode /* result */,
     41                               const std::vector<std::string>& /* flags */)>
     42       ResultCallback;
     43 
     44   // Immediately starts fetching the flags.
     45   AccountServiceFlagFetcher(const std::string& account_id,
     46                             ProfileOAuth2TokenService* token_service,
     47                             net::URLRequestContextGetter* request_context,
     48                             const ResultCallback& callback);
     49 
     50   // Destructing the object before the callback is called cancels the request.
     51   virtual ~AccountServiceFlagFetcher();
     52 
     53  private:
     54   void Start();
     55   void StartFetchingOAuth2AccessToken();
     56 
     57   // Overridden from OAuth2TokenService::Observer:
     58   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
     59   virtual void OnRefreshTokensLoaded() OVERRIDE;
     60 
     61   // Overridden from OAuth2TokenService::Consumer:
     62   virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
     63                                  const std::string& access_token,
     64                                  const base::Time& expiration_time) OVERRIDE;
     65   virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
     66                                  const GoogleServiceAuthError& error) OVERRIDE;
     67 
     68   // Overridden from GaiaAuthConsumer:
     69   virtual void OnClientLoginSuccess(const ClientLoginResult& result) OVERRIDE;
     70   virtual void OnClientLoginFailure(const GoogleServiceAuthError& error)
     71       OVERRIDE;
     72   virtual void OnGetUserInfoSuccess(const UserInfoMap& data) OVERRIDE;
     73   virtual void OnGetUserInfoFailure(const GoogleServiceAuthError& error)
     74       OVERRIDE;
     75 
     76   const std::string account_id_;
     77   ProfileOAuth2TokenService* token_service_;
     78   GaiaAuthFetcher gaia_auth_fetcher_;
     79 
     80   ResultCallback callback_;
     81 
     82   scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(AccountServiceFlagFetcher);
     85 };
     86 
     87 #endif  // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
     88