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.
5 #ifndef CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_DELEGATE_ANDROID_H_
6 #define CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_DELEGATE_ANDROID_H_
12 #include "base/android/jni_weak_ref.h"
13 #include "base/android/scoped_java_ref.h"
14 #include "base/callback.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h"
17 #include "components/signin/core/browser/profile_oauth2_token_service.h"
18 #include "google_apis/gaia/google_service_auth_error.h"
19 #include "google_apis/gaia/oauth2_token_service_delegate.h"
21 // A specialization of OAuth2TokenServiceDelegate that will be returned by
22 // OAuth2TokenServiceDelegateFactory for OS_ANDROID. This instance uses
23 // native Android features to lookup OAuth2 tokens.
25 // See |OAuth2TokenServiceDelegate| for usage details.
27 // Note: requests should be started from the UI thread. To start a
28 // request from other thread, please use OAuth2TokenServiceRequest.
29 class OAuth2TokenServiceDelegateAndroid
: public OAuth2TokenServiceDelegate
{
31 // Registers the OAuth2TokenServiceDelegateAndroid's native methods through
33 static bool Register(JNIEnv
* env
);
35 // Creates a new instance of the OAuth2TokenServiceDelegateAndroid.
36 static OAuth2TokenServiceDelegateAndroid
* Create();
38 // Returns a reference to the Java instance of this service.
39 static base::android::ScopedJavaLocalRef
<jobject
>
40 GetForProfile(JNIEnv
* env
, jclass clazz
, jobject j_profile_android
);
42 // Called by the TestingProfile class to disable account validation in
43 // tests. This prevents the token service from trying to look up system
44 // accounts which requires special permission.
45 static void set_is_testing_profile() { is_testing_profile_
= true; }
49 // OAuth2TokenServiceDelegate overrides:
50 bool RefreshTokenIsAvailable(const std::string
& account_id
) const override
;
51 bool RefreshTokenHasError(const std::string
& account_id
) const override
;
52 void UpdateAuthError(const std::string
& account_id
,
53 const GoogleServiceAuthError
& error
) override
;
54 std::vector
<std::string
> GetAccounts() override
;
56 // Lists account at the OS level.
57 std::vector
<std::string
> GetSystemAccounts();
59 void ValidateAccounts(JNIEnv
* env
,
61 jstring current_account
,
62 jboolean force_notifications
);
64 // Takes a the signed in sync account as well as all the other
65 // android account ids and check the token status of each. If
66 // |force_notifications| is true, TokenAvailable notifications will
67 // be sent anyway, even if the account was already known.
68 void ValidateAccounts(const std::string
& signed_in_account
,
69 bool force_notifications
);
71 // Triggers a notification to all observers of the OAuth2TokenService that a
72 // refresh token is now available. This may cause observers to retry
73 // operations that require authentication.
74 virtual void FireRefreshTokenAvailableFromJava(JNIEnv
* env
,
76 const jstring account_name
);
77 // Triggers a notification to all observers of the OAuth2TokenService that a
78 // refresh token is now available.
79 virtual void FireRefreshTokenRevokedFromJava(JNIEnv
* env
,
81 const jstring account_name
);
82 // Triggers a notification to all observers of the OAuth2TokenService that all
83 // refresh tokens have now been loaded.
84 virtual void FireRefreshTokensLoadedFromJava(JNIEnv
* env
, jobject obj
);
86 // Overridden from OAuth2TokenService to complete signout of all
87 // OA2TService aware accounts.
88 void RevokeAllCredentials() override
;
91 friend class ProfileOAuth2TokenServiceFactory
;
92 OAuth2TokenServiceDelegateAndroid();
93 ~OAuth2TokenServiceDelegateAndroid() override
;
95 OAuth2AccessTokenFetcher
* CreateAccessTokenFetcher(
96 const std::string
& account_id
,
97 net::URLRequestContextGetter
* getter
,
98 OAuth2AccessTokenConsumer
* consumer
) override
;
100 // Overridden from OAuth2TokenService to intercept token fetch requests and
101 // redirect them to the Account Manager.
102 void InvalidateAccessToken(const std::string
& account_id
,
103 const std::string
& client_id
,
104 const OAuth2TokenService::ScopeSet
& scopes
,
105 const std::string
& access_token
) override
;
107 // Called to notify observers when a refresh token is available.
108 void FireRefreshTokenAvailable(const std::string
& account_id
) override
;
109 // Called to notify observers when a refresh token has been revoked.
110 void FireRefreshTokenRevoked(const std::string
& account_id
) override
;
111 // Called to notify observers when refresh tokans have been loaded.
112 void FireRefreshTokensLoaded() override
;
117 explicit ErrorInfo(const GoogleServiceAuthError
& error
);
118 GoogleServiceAuthError error
;
121 // Return whether |signed_in_account| is valid and we have access
122 // to all the tokens in |curr_account_ids|. If |force_notifications| is true,
123 // TokenAvailable notifications will be sent anyway, even if the account was
125 bool ValidateAccounts(const std::string
& signed_in_account
,
126 const std::vector
<std::string
>& prev_account_ids
,
127 const std::vector
<std::string
>& curr_account_ids
,
128 std::vector
<std::string
>& refreshed_ids
,
129 std::vector
<std::string
>& revoked_ids
,
130 bool force_notifications
);
132 base::android::ScopedJavaGlobalRef
<jobject
> java_ref_
;
134 // Maps account_id to the last error for that account.
135 std::map
<std::string
, ErrorInfo
> errors_
;
137 static bool is_testing_profile_
;
139 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenServiceDelegateAndroid
);
142 #endif // CHROME_BROWSER_SIGNIN_OAUTH2_TOKEN_SERVICE_DELEGATE_ANDROID_H_