Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / signin / oauth2_token_service_delegate_android.h
blob8db8a5bfb5b161044c6d42628e9f561e0ea6eb72
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_DELEGATE_ANDROID_OAUTH2_TOKEN_SERVICE_H_
6 #define CHROME_BROWSER_SIGNIN_DELEGATE_ANDROID_OAUTH2_TOKEN_SERVICE_H_
8 #include <jni.h>
9 #include <string>
11 #include "base/android/jni_weak_ref.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "components/signin/core/browser/profile_oauth2_token_service.h"
16 #include "google_apis/gaia/google_service_auth_error.h"
17 #include "google_apis/gaia/oauth2_token_service_delegate.h"
19 // A specialization of OAuth2TokenServiceDelegate that will be returned by
20 // OAuth2TokenServiceDelegateFactory for OS_ANDROID. This instance uses
21 // native Android features to lookup OAuth2 tokens.
23 // See |OAuth2TokenServiceDelegate| for usage details.
25 // Note: requests should be started from the UI thread. To start a
26 // request from other thread, please use OAuth2TokenServiceRequest.
27 class OAuth2TokenServiceDelegateAndroid : public OAuth2TokenServiceDelegate {
28 public:
29 // Registers the OAuth2TokenServiceDelegateAndroid's native methods through
30 // JNI.
31 static bool Register(JNIEnv* env);
33 // Creates a new instance of the OAuth2TokenServiceDelegateAndroid.
34 static OAuth2TokenServiceDelegateAndroid* Create();
36 // Returns a reference to the Java instance of this service.
37 static jobject GetForProfile(JNIEnv* env,
38 jclass clazz,
39 jobject j_profile_android);
41 // Called by the TestingProfile class to disable account validation in
42 // tests. This prevents the token service from trying to look up system
43 // accounts which requires special permission.
44 static void set_is_testing_profile() { is_testing_profile_ = true; }
46 void Initialize();
48 // OAuth2TokenServiceDelegate overrides:
49 bool RefreshTokenIsAvailable(const std::string& account_id) const override;
50 void UpdateAuthError(const std::string& account_id,
51 const GoogleServiceAuthError& error) override;
52 std::vector<std::string> GetAccounts() override;
54 // Lists account at the OS level.
55 std::vector<std::string> GetSystemAccounts();
57 void ValidateAccounts(JNIEnv* env,
58 jobject obj,
59 jstring current_account,
60 jboolean force_notifications);
62 // Takes a the signed in sync account as well as all the other
63 // android account ids and check the token status of each. If
64 // |force_notifications| is true, TokenAvailable notifications will
65 // be sent anyway, even if the account was already known.
66 void ValidateAccounts(const std::string& signed_in_account,
67 bool force_notifications);
69 // Triggers a notification to all observers of the OAuth2TokenService that a
70 // refresh token is now available. This may cause observers to retry
71 // operations that require authentication.
72 virtual void FireRefreshTokenAvailableFromJava(JNIEnv* env,
73 jobject obj,
74 const jstring account_name);
75 // Triggers a notification to all observers of the OAuth2TokenService that a
76 // refresh token is now available.
77 virtual void FireRefreshTokenRevokedFromJava(JNIEnv* env,
78 jobject obj,
79 const jstring account_name);
80 // Triggers a notification to all observers of the OAuth2TokenService that all
81 // refresh tokens have now been loaded.
82 virtual void FireRefreshTokensLoadedFromJava(JNIEnv* env, jobject obj);
84 // Overridden from OAuth2TokenService to complete signout of all
85 // OA2TService aware accounts.
86 void RevokeAllCredentials() override;
88 protected:
89 friend class ProfileOAuth2TokenServiceFactory;
90 OAuth2TokenServiceDelegateAndroid();
91 ~OAuth2TokenServiceDelegateAndroid() override;
93 OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
94 const std::string& account_id,
95 net::URLRequestContextGetter* getter,
96 OAuth2AccessTokenConsumer* consumer) override;
98 // Overridden from OAuth2TokenService to intercept token fetch requests and
99 // redirect them to the Account Manager.
100 void InvalidateAccessToken(const std::string& account_id,
101 const std::string& client_id,
102 const OAuth2TokenService::ScopeSet& scopes,
103 const std::string& access_token) override;
105 // Called to notify observers when a refresh token is available.
106 void FireRefreshTokenAvailable(const std::string& account_id) override;
107 // Called to notify observers when a refresh token has been revoked.
108 void FireRefreshTokenRevoked(const std::string& account_id) override;
109 // Called to notify observers when refresh tokans have been loaded.
110 void FireRefreshTokensLoaded() override;
112 private:
113 // Return whether |signed_in_account| is valid and we have access
114 // to all the tokens in |curr_account_ids|. If |force_notifications| is true,
115 // TokenAvailable notifications will be sent anyway, even if the account was
116 // already known.
117 bool ValidateAccounts(const std::string& signed_in_account,
118 const std::vector<std::string>& prev_account_ids,
119 const std::vector<std::string>& curr_account_ids,
120 std::vector<std::string>& refreshed_ids,
121 std::vector<std::string>& revoked_ids,
122 bool force_notifications);
124 base::android::ScopedJavaGlobalRef<jobject> java_ref_;
126 static bool is_testing_profile_;
128 DISALLOW_COPY_AND_ASSIGN(OAuth2TokenServiceDelegateAndroid);
131 #endif // CHROME_BROWSER_SIGNIN_DELEGATE_ANDROID_OAUTH2_TOKEN_SERVICE_H_