Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / android_profile_oauth2_token_service.h
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_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
18 // A specialization of ProfileOAuth2TokenService that will be returned by
19 // ProfileOAuth2TokenServiceFactory for OS_ANDROID.  This instance uses
20 // native Android features to lookup OAuth2 tokens.
21 //
22 // See |ProfileOAuth2TokenService| for usage details.
23 //
24 // Note: requests should be started from the UI thread. To start a
25 // request from other thread, please use ProfileOAuth2TokenServiceRequest.
26 class AndroidProfileOAuth2TokenService : public ProfileOAuth2TokenService {
27  public:
28   // Registers the AndroidProfileOAuth2TokenService's native methods through
29   // JNI.
30   static bool Register(JNIEnv* env);
31
32   // Creates a new instance of the AndroidProfileOAuth2TokenService.
33   static AndroidProfileOAuth2TokenService* Create();
34
35   // Returns a reference to the Java instance of this service.
36   static jobject GetForProfile(
37       JNIEnv* env, jclass clazz, jobject j_profile_android);
38
39   virtual bool RefreshTokenIsAvailable(
40       const std::string& account_id) const OVERRIDE;
41
42   // Lists account IDs of all accounts with a refresh token.
43   virtual std::vector<std::string> GetAccounts() OVERRIDE;
44
45   // Lists account at the OS level.
46   std::vector<std::string> GetSystemAccounts();
47
48   void ValidateAccounts(JNIEnv* env,
49                         jobject obj,
50                         jstring current_account);
51
52   // Takes a the signed in sync account as well as all the other
53   // android account ids and check the token status of each.
54   void ValidateAccounts(const std::string& signed_in_account);
55
56   bool ValidateAccounts(const std::string& signed_in_account,
57                         const std::vector<std::string>& prev_account_ids,
58                         const std::vector<std::string>& curr_account_ids);
59
60   // Triggers a notification to all observers of the OAuth2TokenService that a
61   // refresh token is now available. This may cause observers to retry
62   // operations that require authentication.
63   virtual void FireRefreshTokenAvailableFromJava(JNIEnv* env,
64                                                  jobject obj,
65                                                  const jstring account_name);
66   // Triggers a notification to all observers of the OAuth2TokenService that a
67   // refresh token is now available.
68   virtual void FireRefreshTokenRevokedFromJava(JNIEnv* env,
69                                                jobject obj,
70                                                const jstring account_name);
71   // Triggers a notification to all observers of the OAuth2TokenService that all
72   // refresh tokens have now been loaded.
73   virtual void FireRefreshTokensLoadedFromJava(JNIEnv* env, jobject obj);
74
75   // Overridden from OAuth2TokenService to complete signout of all
76   // OA2TService aware accounts.
77   virtual void RevokeAllCredentials() OVERRIDE;
78
79  protected:
80   friend class ProfileOAuth2TokenServiceFactory;
81   AndroidProfileOAuth2TokenService();
82   virtual ~AndroidProfileOAuth2TokenService();
83
84   // Overridden from OAuth2TokenService to intercept token fetch requests and
85   // redirect them to the Account Manager.
86   virtual void FetchOAuth2Token(RequestImpl* request,
87                                 const std::string& account_id,
88                                 net::URLRequestContextGetter* getter,
89                                 const std::string& client_id,
90                                 const std::string& client_secret,
91                                 const ScopeSet& scopes) OVERRIDE;
92
93   // Overriden from OAuth2TokenService to avoid compile errors. Has NOTREACHED()
94   // implementation as |AndroidProfileOAuth2TokenService| overrides
95   // |FetchOAuth2Token| and thus bypasses this method entirely.
96   virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
97       const std::string& account_id,
98       net::URLRequestContextGetter* getter,
99       OAuth2AccessTokenConsumer* consumer) OVERRIDE;
100
101   // Overridden from OAuth2TokenService to intercept token fetch requests and
102   // redirect them to the Account Manager.
103   virtual void InvalidateOAuth2Token(const std::string& account_id,
104                                      const std::string& client_id,
105                                      const ScopeSet& scopes,
106                                      const std::string& access_token) OVERRIDE;
107
108   // Called to notify observers when a refresh token is available.
109   virtual void FireRefreshTokenAvailable(
110       const std::string& account_id) OVERRIDE;
111   // Called to notify observers when a refresh token has been revoked.
112   virtual void FireRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
113   // Called to notify observers when refresh tokans have been loaded.
114   virtual void FireRefreshTokensLoaded() OVERRIDE;
115
116  private:
117   base::android::ScopedJavaGlobalRef<jobject> java_ref_;
118
119   DISALLOW_COPY_AND_ASSIGN(AndroidProfileOAuth2TokenService);
120 };
121
122 #endif  // CHROME_BROWSER_SIGNIN_ANDROID_PROFILE_OAUTH2_TOKEN_SERVICE_H_