Home | History | Annotate | Download | only in signin
      1 // Copyright 2013 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_SIGNIN_ANDROID_PROFILE_OAUTH2_TOKEN_SERVICE_H_
      6 #define CHROME_BROWSER_SIGNIN_ANDROID_PROFILE_OAUTH2_TOKEN_SERVICE_H_
      7 
      8 #include <jni.h>
      9 #include <string>
     10 
     11 #include "base/android/jni_helper.h"
     12 #include "base/callback.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/time/time.h"
     15 #include "chrome/browser/signin/profile_oauth2_token_service.h"
     16 #include "google_apis/gaia/google_service_auth_error.h"
     17 
     18 class TokenService;
     19 
     20 
     21 // A specialization of ProfileOAuth2TokenService that will be returned by
     22 // ProfileOAuth2TokenServiceFactory for OS_ANDROID.  This instance uses
     23 // native Android features to lookup OAuth2 tokens.
     24 //
     25 // See |ProfileOAuth2TokenService| for usage details.
     26 //
     27 // Note: requests should be started from the UI thread. To start a
     28 // request from other thread, please use ProfileOAuth2TokenServiceRequest.
     29 class AndroidProfileOAuth2TokenService : public ProfileOAuth2TokenService {
     30  public:
     31 
     32   // Callback from FetchOAuth2Token.
     33   // Arguments:
     34   // - the error, or NONE if the token fetch was successful.
     35   // - the OAuth2 access token.
     36   // - the expiry time of the token (may be null, indicating that the expiry
     37   //   time is unknown.
     38   typedef base::Callback<void(
     39       const GoogleServiceAuthError&, const std::string&, const base::Time&)>
     40           FetchOAuth2TokenCallback;
     41 
     42   // Start the OAuth2 access token for the given scopes using
     43   // ProfileSyncServiceAndroid.
     44   virtual scoped_ptr<OAuth2TokenService::Request> StartRequest(
     45       const OAuth2TokenService::ScopeSet& scopes,
     46       OAuth2TokenService::Consumer* consumer) OVERRIDE;
     47 
     48   // StartRequest() fetches a token for the currently signed-in account; this
     49   // version uses the account corresponding to |username|. This allows fetching
     50   // tokens before a user is signed-in (e.g. during the sign-in flow).
     51   scoped_ptr<OAuth2TokenService::Request> StartRequestForUsername(
     52       const std::string& username,
     53       const OAuth2TokenService::ScopeSet& scopes,
     54       OAuth2TokenService::Consumer* consumer);
     55 
     56   virtual bool RefreshTokenIsAvailable() OVERRIDE;
     57   virtual void InvalidateToken(const ScopeSet& scopes,
     58                                const std::string& invalid_token) OVERRIDE;
     59 
     60   // Registers the AndroidProfileOAuth2TokenService's native methods through
     61   // JNI.
     62   static bool Register(JNIEnv* env);
     63 
     64  protected:
     65   friend class ProfileOAuth2TokenServiceFactory;
     66   AndroidProfileOAuth2TokenService();
     67   virtual ~AndroidProfileOAuth2TokenService();
     68 
     69   // virtual for testing.
     70   virtual void FetchOAuth2Token(const std::string& username,
     71                                 const std::string& scope,
     72                                 const FetchOAuth2TokenCallback& callback);
     73 
     74  private:
     75   DISALLOW_COPY_AND_ASSIGN(AndroidProfileOAuth2TokenService);
     76 };
     77 
     78 #endif  // CHROME_BROWSER_SIGNIN_ANDROID_PROFILE_OAUTH2_TOKEN_SERVICE_H_
     79