Chromium Code Reviews| 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 std::string CombineScopes(const std::vector<std::string>& scopes) { |
|
acleung1
2014/06/30 22:53:25
Since we no longer need this in the outer namespac
Roger Tawa OOO till Jul 10th
2014/07/01 01:38:44
Done.
| |
| 27 // The Android AccountManager supports multiple scopes separated by a space: | 28 // The Android AccountManager supports multiple scopes separated by a space: |
| 28 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android | 29 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android |
| 29 std::string scope; | 30 std::string scope; |
| 30 for (OAuth2TokenService::ScopeSet::const_iterator it = scopes.begin(); | 31 for (std::vector<std::string>::const_iterator it = scopes.begin(); |
| 31 it != scopes.end(); ++it) { | 32 it != scopes.end(); ++it) { |
| 32 if (!scope.empty()) | 33 if (!scope.empty()) |
| 33 scope += " "; | 34 scope += " "; |
| 34 scope += *it; | 35 scope += *it; |
| 35 } | 36 } |
| 36 return scope; | 37 return scope; |
| 37 } | 38 } |
| 38 | 39 |
| 39 // Callback from FetchOAuth2TokenWithUsername(). | 40 // Callback from FetchOAuth2TokenWithUsername(). |
| 40 // Arguments: | 41 // Arguments: |
| 41 // - the error, or NONE if the token fetch was successful. | 42 // - the error, or NONE if the token fetch was successful. |
| 42 // - the OAuth2 access token. | 43 // - the OAuth2 access token. |
| 43 // - the expiry time of the token (may be null, indicating that the expiry | 44 // - the expiry time of the token (may be null, indicating that the expiry |
| 44 // time is unknown. | 45 // time is unknown. |
| 45 typedef base::Callback<void( | 46 typedef base::Callback<void( |
| 46 const GoogleServiceAuthError&, const std::string&, const base::Time&)> | 47 const GoogleServiceAuthError&, const std::string&, const base::Time&)> |
| 47 FetchOAuth2TokenCallback; | 48 FetchOAuth2TokenCallback; |
| 48 | 49 |
| 50 class AndroidAccessTokenFetcher : public OAuth2AccessTokenFetcher { | |
| 51 public: | |
| 52 AndroidAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, | |
| 53 const std::string& account_id); | |
| 54 virtual ~AndroidAccessTokenFetcher(); | |
| 55 | |
| 56 // Overrides from OAuth2AccessTokenFetcher: | |
| 57 virtual void Start(const std::string& client_id, | |
| 58 const std::string& client_secret, | |
| 59 const std::vector<std::string>& scopes) OVERRIDE; | |
| 60 virtual void CancelRequest() OVERRIDE; | |
|
acleung1
2014/06/30 22:53:25
Why is this called? When the profile went away bef
Roger Tawa OOO till Jul 10th
2014/07/01 01:38:44
This is the implementation of a pure virtual metho
| |
| 61 | |
| 62 // Handles an access token response. | |
| 63 void OnAccessTokenResponse(const GoogleServiceAuthError& error, | |
| 64 const std::string& access_token, | |
| 65 const base::Time& expiration_time); | |
| 66 | |
| 67 private: | |
| 68 base::WeakPtrFactory<AndroidAccessTokenFetcher> weak_factory_; | |
| 69 std::string account_id_; | |
| 70 bool request_was_cancelled_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(AndroidAccessTokenFetcher); | |
| 73 }; | |
| 74 | |
| 75 AndroidAccessTokenFetcher::AndroidAccessTokenFetcher( | |
| 76 OAuth2AccessTokenConsumer* consumer, | |
| 77 const std::string& account_id) | |
| 78 : OAuth2AccessTokenFetcher(consumer), | |
| 79 weak_factory_(this), | |
| 80 account_id_(account_id), | |
| 81 request_was_cancelled_(false) { | |
| 82 } | |
| 83 | |
| 84 AndroidAccessTokenFetcher::~AndroidAccessTokenFetcher() {} | |
| 85 | |
| 86 void AndroidAccessTokenFetcher::Start(const std::string& client_id, | |
| 87 const std::string& client_secret, | |
| 88 const std::vector<std::string>& scopes) { | |
| 89 JNIEnv* env = AttachCurrentThread(); | |
| 90 std::string scope = CombineScopes(scopes); | |
| 91 ScopedJavaLocalRef<jstring> j_username = | |
| 92 ConvertUTF8ToJavaString(env, account_id_); | |
| 93 ScopedJavaLocalRef<jstring> j_scope = | |
| 94 ConvertUTF8ToJavaString(env, scope); | |
| 95 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | |
| 96 new FetchOAuth2TokenCallback( | |
| 97 base::Bind(&AndroidAccessTokenFetcher::OnAccessTokenResponse, | |
| 98 weak_factory_.GetWeakPtr()))); | |
| 99 | |
| 100 // Call into Java to get a new token. | |
| 101 Java_OAuth2TokenService_getOAuth2AuthToken( | |
| 102 env, base::android::GetApplicationContext(), | |
| 103 j_username.obj(), | |
| 104 j_scope.obj(), | |
| 105 reinterpret_cast<intptr_t>(heap_callback.release())); | |
| 106 } | |
| 107 | |
| 108 void AndroidAccessTokenFetcher::CancelRequest() { | |
| 109 request_was_cancelled_ = true; | |
| 110 } | |
| 111 | |
| 112 void AndroidAccessTokenFetcher::OnAccessTokenResponse( | |
| 113 const GoogleServiceAuthError& error, | |
| 114 const std::string& access_token, | |
| 115 const base::Time& expiration_time) { | |
| 116 if (request_was_cancelled_) { | |
| 117 // Ignore the callback if the request was cancelled. | |
| 118 return; | |
| 119 } | |
| 120 if (error.state() == GoogleServiceAuthError::NONE) { | |
| 121 FireOnGetTokenSuccess(access_token, expiration_time); | |
| 122 } else { | |
| 123 FireOnGetTokenFailure(error); | |
| 124 } | |
| 125 } | |
| 126 | |
| 49 } // namespace | 127 } // namespace |
| 50 | 128 |
| 51 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; | 129 bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; |
| 52 | 130 |
| 53 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { | 131 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { |
| 54 VLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; | 132 VLOG(1) << "AndroidProfileOAuth2TokenService::ctor"; |
| 55 JNIEnv* env = AttachCurrentThread(); | 133 JNIEnv* env = AttachCurrentThread(); |
| 56 base::android::ScopedJavaLocalRef<jobject> local_java_ref = | 134 base::android::ScopedJavaLocalRef<jobject> local_java_ref = |
| 57 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); | 135 Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this)); |
| 58 java_ref_.Reset(env, local_java_ref.obj()); | 136 java_ref_.Reset(env, local_java_ref.obj()); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 JNIEnv* env = AttachCurrentThread(); | 195 JNIEnv* env = AttachCurrentThread(); |
| 118 ScopedJavaLocalRef<jobjectArray> j_accounts = | 196 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 119 Java_OAuth2TokenService_getSystemAccounts( | 197 Java_OAuth2TokenService_getSystemAccounts( |
| 120 env, base::android::GetApplicationContext()); | 198 env, base::android::GetApplicationContext()); |
| 121 base::android::AppendJavaStringArrayToStringVector(env, | 199 base::android::AppendJavaStringArrayToStringVector(env, |
| 122 j_accounts.obj(), | 200 j_accounts.obj(), |
| 123 &accounts); | 201 &accounts); |
| 124 return accounts; | 202 return accounts; |
| 125 } | 203 } |
| 126 | 204 |
| 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* | 205 OAuth2AccessTokenFetcher* |
| 160 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( | 206 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( |
| 161 const std::string& account_id, | 207 const std::string& account_id, |
| 162 net::URLRequestContextGetter* getter, | 208 net::URLRequestContextGetter* getter, |
| 163 OAuth2AccessTokenConsumer* consumer) { | 209 OAuth2AccessTokenConsumer* consumer) { |
| 164 NOTREACHED(); | 210 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 165 return NULL; | 211 DCHECK(!account_id.empty()); |
| 212 return new AndroidAccessTokenFetcher(consumer, account_id); | |
| 166 } | 213 } |
| 167 | 214 |
| 168 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( | 215 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( |
| 169 const std::string& account_id, | 216 const std::string& account_id, |
| 170 const std::string& client_id, | 217 const std::string& client_id, |
| 171 const ScopeSet& scopes, | 218 const ScopeSet& scopes, |
| 172 const std::string& access_token) { | 219 const std::string& access_token) { |
| 173 OAuth2TokenService::InvalidateOAuth2Token(account_id, | 220 OAuth2TokenService::InvalidateOAuth2Token(account_id, |
| 174 client_id, | 221 client_id, |
| 175 scopes, | 222 scopes, |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 VLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; | 412 VLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; |
| 366 std::vector<std::string> accounts = GetAccounts(); | 413 std::vector<std::string> accounts = GetAccounts(); |
| 367 for (std::vector<std::string>::iterator it = accounts.begin(); | 414 for (std::vector<std::string>::iterator it = accounts.begin(); |
| 368 it != accounts.end(); it++) { | 415 it != accounts.end(); it++) { |
| 369 FireRefreshTokenRevoked(*it); | 416 FireRefreshTokenRevoked(*it); |
| 370 } | 417 } |
| 371 } | 418 } |
| 372 | 419 |
| 373 // Called from Java when fetching of an OAuth2 token is finished. The | 420 // Called from Java when fetching of an OAuth2 token is finished. The |
| 374 // |authToken| param is only valid when |result| is true. | 421 // |authToken| param is only valid when |result| is true. |
| 375 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 422 void OAuth2TokenFetched( |
| 423 JNIEnv* env, | |
| 424 jclass clazz, | |
| 376 jstring authToken, | 425 jstring authToken, |
| 377 jboolean result, | 426 jboolean result, |
| 378 jlong nativeCallback) { | 427 jlong nativeCallback) { |
| 379 std::string token = ConvertJavaStringToUTF8(env, authToken); | 428 std::string token = ConvertJavaStringToUTF8(env, authToken); |
| 380 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 429 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 381 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 430 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 382 // Android does not provide enough information to know if the credentials are | 431 // Android does not provide enough information to know if the credentials are |
| 383 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 432 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
| 384 GoogleServiceAuthError err(result ? | 433 GoogleServiceAuthError err(result ? |
| 385 GoogleServiceAuthError::NONE : | 434 GoogleServiceAuthError::NONE : |
| 386 GoogleServiceAuthError::CONNECTION_FAILED); | 435 GoogleServiceAuthError::CONNECTION_FAILED); |
| 387 heap_callback->Run(err, token, base::Time()); | 436 heap_callback->Run(err, token, base::Time()); |
| 388 } | 437 } |
| 389 | 438 |
| 390 // static | 439 // static |
| 391 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 440 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
| 392 return RegisterNativesImpl(env); | 441 return RegisterNativesImpl(env); |
| 393 } | 442 } |
| OLD | NEW |