OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" | 5 #include "chrome/browser/signin/android_profile_oauth2_token_service.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "chrome/browser/profiles/profile_android.h" | 12 #include "chrome/browser/profiles/profile_android.h" |
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
14 #include "chrome/browser/sync/profile_sync_service_android.h" | 14 #include "chrome/browser/sync/profile_sync_service_android.h" |
15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
16 #include "jni/OAuth2TokenService_jni.h" | 17 #include "jni/OAuth2TokenService_jni.h" |
17 | 18 |
18 using base::android::AttachCurrentThread; | 19 using base::android::AttachCurrentThread; |
19 using base::android::ConvertJavaStringToUTF8; | 20 using base::android::ConvertJavaStringToUTF8; |
20 using base::android::ConvertUTF8ToJavaString; | 21 using base::android::ConvertUTF8ToJavaString; |
21 using base::android::ScopedJavaLocalRef; | 22 using base::android::ScopedJavaLocalRef; |
22 using content::BrowserThread; | 23 using content::BrowserThread; |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { | 27 // Callback from FetchOAuth2TokenWithUsername(). |
| 28 // Arguments: |
| 29 // - the error, or NONE if the token fetch was successful. |
| 30 // - the OAuth2 access token. |
| 31 // - the expiry time of the token (may be null, indicating that the expiry |
| 32 // time is unknown. |
| 33 typedef base::Callback<void( |
| 34 const GoogleServiceAuthError&, const std::string&, const base::Time&)> |
| 35 FetchOAuth2TokenCallback; |
| 36 |
| 37 class AndroidAccessTokenFetcher : public OAuth2AccessTokenFetcher { |
| 38 public: |
| 39 AndroidAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, |
| 40 const std::string& account_id); |
| 41 virtual ~AndroidAccessTokenFetcher(); |
| 42 |
| 43 // Overrides from OAuth2AccessTokenFetcher: |
| 44 virtual void Start(const std::string& client_id, |
| 45 const std::string& client_secret, |
| 46 const std::vector<std::string>& scopes) OVERRIDE; |
| 47 virtual void CancelRequest() OVERRIDE; |
| 48 |
| 49 // Handles an access token response. |
| 50 void OnAccessTokenResponse(const GoogleServiceAuthError& error, |
| 51 const std::string& access_token, |
| 52 const base::Time& expiration_time); |
| 53 |
| 54 private: |
| 55 std::string CombineScopes(const std::vector<std::string>& scopes); |
| 56 |
| 57 base::WeakPtrFactory<AndroidAccessTokenFetcher> weak_factory_; |
| 58 std::string account_id_; |
| 59 bool request_was_cancelled_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(AndroidAccessTokenFetcher); |
| 62 }; |
| 63 |
| 64 AndroidAccessTokenFetcher::AndroidAccessTokenFetcher( |
| 65 OAuth2AccessTokenConsumer* consumer, |
| 66 const std::string& account_id) |
| 67 : OAuth2AccessTokenFetcher(consumer), |
| 68 weak_factory_(this), |
| 69 account_id_(account_id), |
| 70 request_was_cancelled_(false) { |
| 71 } |
| 72 |
| 73 AndroidAccessTokenFetcher::~AndroidAccessTokenFetcher() {} |
| 74 |
| 75 void AndroidAccessTokenFetcher::Start(const std::string& client_id, |
| 76 const std::string& client_secret, |
| 77 const std::vector<std::string>& scopes) { |
| 78 JNIEnv* env = AttachCurrentThread(); |
| 79 std::string scope = CombineScopes(scopes); |
| 80 ScopedJavaLocalRef<jstring> j_username = |
| 81 ConvertUTF8ToJavaString(env, account_id_); |
| 82 ScopedJavaLocalRef<jstring> j_scope = |
| 83 ConvertUTF8ToJavaString(env, scope); |
| 84 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 85 new FetchOAuth2TokenCallback( |
| 86 base::Bind(&AndroidAccessTokenFetcher::OnAccessTokenResponse, |
| 87 weak_factory_.GetWeakPtr()))); |
| 88 |
| 89 // Call into Java to get a new token. |
| 90 Java_OAuth2TokenService_getOAuth2AuthToken( |
| 91 env, base::android::GetApplicationContext(), |
| 92 j_username.obj(), |
| 93 j_scope.obj(), |
| 94 reinterpret_cast<intptr_t>(heap_callback.release())); |
| 95 } |
| 96 |
| 97 void AndroidAccessTokenFetcher::CancelRequest() { |
| 98 request_was_cancelled_ = true; |
| 99 } |
| 100 |
| 101 void AndroidAccessTokenFetcher::OnAccessTokenResponse( |
| 102 const GoogleServiceAuthError& error, |
| 103 const std::string& access_token, |
| 104 const base::Time& expiration_time) { |
| 105 if (request_was_cancelled_) { |
| 106 // Ignore the callback if the request was cancelled. |
| 107 return; |
| 108 } |
| 109 if (error.state() == GoogleServiceAuthError::NONE) { |
| 110 FireOnGetTokenSuccess(access_token, expiration_time); |
| 111 } else { |
| 112 FireOnGetTokenFailure(error); |
| 113 } |
| 114 } |
| 115 |
| 116 // static |
| 117 std::string AndroidAccessTokenFetcher::CombineScopes( |
| 118 const std::vector<std::string>& scopes) { |
27 // The Android AccountManager supports multiple scopes separated by a space: | 119 // The Android AccountManager supports multiple scopes separated by a space: |
28 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android | 120 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android |
29 std::string scope; | 121 std::string scope; |
30 for (OAuth2TokenService::ScopeSet::const_iterator it = scopes.begin(); | 122 for (std::vector<std::string>::const_iterator it = scopes.begin(); |
31 it != scopes.end(); ++it) { | 123 it != scopes.end(); ++it) { |
32 if (!scope.empty()) | 124 if (!scope.empty()) |
33 scope += " "; | 125 scope += " "; |
34 scope += *it; | 126 scope += *it; |
35 } | 127 } |
36 return scope; | 128 return scope; |
37 } | 129 } |
38 | 130 |
39 // Callback from FetchOAuth2TokenWithUsername(). | |
40 // Arguments: | |
41 // - the error, or NONE if the token fetch was successful. | |
42 // - the OAuth2 access token. | |
43 // - the expiry time of the token (may be null, indicating that the expiry | |
44 // time is unknown. | |
45 typedef base::Callback<void( | |
46 const GoogleServiceAuthError&, const std::string&, const base::Time&)> | |
47 FetchOAuth2TokenCallback; | |
48 | |
49 } // namespace | 131 } // namespace |
50 | 132 |
51 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; | 133 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; |
52 | 134 |
53 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { | 135 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { |
54 VLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; | 136 VLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; |
55 JNIEnv* env = AttachCurrentThread(); | 137 JNIEnv* env = AttachCurrentThread(); |
56 base::android::ScopedJavaLocalRef<jobject> local_java_ref = | 138 base::android::ScopedJavaLocalRef<jobject> local_java_ref = |
57 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); | 139 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); |
58 java_ref_.Reset(env, local_java_ref.obj()); | 140 java_ref_.Reset(env, local_java_ref.obj()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 JNIEnv* env = AttachCurrentThread(); | 174 JNIEnv* env = AttachCurrentThread(); |
93 ScopedJavaLocalRef<jstring> j_account_id = | 175 ScopedJavaLocalRef<jstring> j_account_id = |
94 ConvertUTF8ToJavaString(env, account_id); | 176 ConvertUTF8ToJavaString(env, account_id); |
95 jboolean refresh_token_is_available = | 177 jboolean refresh_token_is_available = |
96 Java_OAuth2TokenService_hasOAuth2RefreshToken( | 178 Java_OAuth2TokenService_hasOAuth2RefreshToken( |
97 env, base::android::GetApplicationContext(), | 179 env, base::android::GetApplicationContext(), |
98 j_account_id.obj()); | 180 j_account_id.obj()); |
99 return refresh_token_is_available == JNI_TRUE; | 181 return refresh_token_is_available == JNI_TRUE; |
100 } | 182 } |
101 | 183 |
| 184 void AndroidProfileOAuth2TokenService::UpdateAuthError( |
| 185 const std::string& account_id, |
| 186 const GoogleServiceAuthError& error) { |
| 187 // TODO(rogerta): do we need to update anything, or does the system handle it? |
| 188 } |
| 189 |
102 std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { | 190 std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { |
103 std::vector<std::string> accounts; | 191 std::vector<std::string> accounts; |
104 JNIEnv* env = AttachCurrentThread(); | 192 JNIEnv* env = AttachCurrentThread(); |
105 ScopedJavaLocalRef<jobjectArray> j_accounts = | 193 ScopedJavaLocalRef<jobjectArray> j_accounts = |
106 Java_OAuth2TokenService_getAccounts( | 194 Java_OAuth2TokenService_getAccounts( |
107 env, base::android::GetApplicationContext()); | 195 env, base::android::GetApplicationContext()); |
108 // TODO(fgorski): We may decide to filter out some of the accounts. | 196 // TODO(fgorski): We may decide to filter out some of the accounts. |
109 base::android::AppendJavaStringArrayToStringVector(env, | 197 base::android::AppendJavaStringArrayToStringVector(env, |
110 j_accounts.obj(), | 198 j_accounts.obj(), |
111 &accounts); | 199 &accounts); |
112 return accounts; | 200 return accounts; |
113 } | 201 } |
114 | 202 |
115 std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() { | 203 std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() { |
116 std::vector<std::string> accounts; | 204 std::vector<std::string> accounts; |
117 JNIEnv* env = AttachCurrentThread(); | 205 JNIEnv* env = AttachCurrentThread(); |
118 ScopedJavaLocalRef<jobjectArray> j_accounts = | 206 ScopedJavaLocalRef<jobjectArray> j_accounts = |
119 Java_OAuth2TokenService_getSystemAccounts( | 207 Java_OAuth2TokenService_getSystemAccounts( |
120 env, base::android::GetApplicationContext()); | 208 env, base::android::GetApplicationContext()); |
121 base::android::AppendJavaStringArrayToStringVector(env, | 209 base::android::AppendJavaStringArrayToStringVector(env, |
122 j_accounts.obj(), | 210 j_accounts.obj(), |
123 &accounts); | 211 &accounts); |
124 return accounts; | 212 return accounts; |
125 } | 213 } |
126 | 214 |
127 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( | |
128 RequestImpl* request, | |
129 const std::string& account_id, | |
130 net::URLRequestContextGetter* getter, | |
131 const std::string& client_id, | |
132 const std::string& client_secret, | |
133 const OAuth2TokenService::ScopeSet& scopes) { | |
134 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
135 DCHECK(!account_id.empty()); | |
136 | |
137 JNIEnv* env = AttachCurrentThread(); | |
138 std::string scope = CombineScopes(scopes); | |
139 ScopedJavaLocalRef<jstring> j_username = | |
140 ConvertUTF8ToJavaString(env, account_id); | |
141 ScopedJavaLocalRef<jstring> j_scope = | |
142 ConvertUTF8ToJavaString(env, scope); | |
143 | |
144 // Allocate a copy of the request WeakPtr on the heap, because the object | |
145 // needs to be passed through JNI as an int. | |
146 // It will be passed back to OAuth2TokenFetched(), where it will be freed. | |
147 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | |
148 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, | |
149 request->AsWeakPtr()))); | |
150 | |
151 // Call into Java to get a new token. | |
152 Java_OAuth2TokenService_getOAuth2AuthToken( | |
153 env, base::android::GetApplicationContext(), | |
154 j_username.obj(), | |
155 j_scope.obj(), | |
156 reinterpret_cast<intptr_t>(heap_callback.release())); | |
157 } | |
158 | |
159 OAuth2AccessTokenFetcher* | 215 OAuth2AccessTokenFetcher* |
160 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( | 216 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( |
161 const std::string& account_id, | 217 const std::string& account_id, |
162 net::URLRequestContextGetter* getter, | 218 net::URLRequestContextGetter* getter, |
163 OAuth2AccessTokenConsumer* consumer) { | 219 OAuth2AccessTokenConsumer* consumer) { |
164 NOTREACHED(); | 220 DCHECK(!account_id.empty()); |
165 return NULL; | 221 return new AndroidAccessTokenFetcher(consumer, account_id); |
166 } | 222 } |
167 | 223 |
168 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( | 224 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( |
169 const std::string& account_id, | 225 const std::string& account_id, |
170 const std::string& client_id, | 226 const std::string& client_id, |
171 const ScopeSet& scopes, | 227 const ScopeSet& scopes, |
172 const std::string& access_token) { | 228 const std::string& access_token) { |
173 OAuth2TokenService::InvalidateOAuth2Token(account_id, | 229 OAuth2TokenService::InvalidateOAuth2Token(account_id, |
174 client_id, | 230 client_id, |
175 scopes, | 231 scopes, |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 std::vector<std::string> empty; | 432 std::vector<std::string> empty; |
377 JNIEnv* env = AttachCurrentThread(); | 433 JNIEnv* env = AttachCurrentThread(); |
378 ScopedJavaLocalRef<jobjectArray> java_accounts( | 434 ScopedJavaLocalRef<jobjectArray> java_accounts( |
379 base::android::ToJavaArrayOfStrings(env, empty)); | 435 base::android::ToJavaArrayOfStrings(env, empty)); |
380 Java_OAuth2TokenService_saveStoredAccounts( | 436 Java_OAuth2TokenService_saveStoredAccounts( |
381 env, base::android::GetApplicationContext(), java_accounts.obj()); | 437 env, base::android::GetApplicationContext(), java_accounts.obj()); |
382 } | 438 } |
383 | 439 |
384 // Called from Java when fetching of an OAuth2 token is finished. The | 440 // Called from Java when fetching of an OAuth2 token is finished. The |
385 // |authToken| param is only valid when |result| is true. | 441 // |authToken| param is only valid when |result| is true. |
386 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 442 void OAuth2TokenFetched( |
| 443 JNIEnv* env, |
| 444 jclass clazz, |
387 jstring authToken, | 445 jstring authToken, |
388 jboolean result, | 446 jboolean result, |
389 jlong nativeCallback) { | 447 jlong nativeCallback) { |
390 std::string token = ConvertJavaStringToUTF8(env, authToken); | 448 std::string token = ConvertJavaStringToUTF8(env, authToken); |
391 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 449 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
392 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 450 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
393 // Android does not provide enough information to know if the credentials are | 451 // Android does not provide enough information to know if the credentials are |
394 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 452 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
395 GoogleServiceAuthError err(result ? | 453 GoogleServiceAuthError err(result ? |
396 GoogleServiceAuthError::NONE : | 454 GoogleServiceAuthError::NONE : |
397 GoogleServiceAuthError::CONNECTION_FAILED); | 455 GoogleServiceAuthError::CONNECTION_FAILED); |
398 heap_callback->Run(err, token, base::Time()); | 456 heap_callback->Run(err, token, base::Time()); |
399 } | 457 } |
400 | 458 |
401 // static | 459 // static |
402 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 460 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
403 return RegisterNativesImpl(env); | 461 return RegisterNativesImpl(env); |
404 } | 462 } |
OLD | NEW |