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 // 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 JNIEnv* env = AttachCurrentThread(); | 199 JNIEnv* env = AttachCurrentThread(); |
| 118 ScopedJavaLocalRef<jobjectArray> j_accounts = | 200 ScopedJavaLocalRef<jobjectArray> j_accounts = |
| 119 Java_OAuth2TokenService_getSystemAccounts( | 201 Java_OAuth2TokenService_getSystemAccounts( |
| 120 env, base::android::GetApplicationContext()); | 202 env, base::android::GetApplicationContext()); |
| 121 base::android::AppendJavaStringArrayToStringVector(env, | 203 base::android::AppendJavaStringArrayToStringVector(env, |
| 122 j_accounts.obj(), | 204 j_accounts.obj(), |
| 123 &accounts); | 205 &accounts); |
| 124 return accounts; | 206 return accounts; |
| 125 } | 207 } |
| 126 | 208 |
| 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* | 209 OAuth2AccessTokenFetcher* |
| 160 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( | 210 AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( |
| 161 const std::string& account_id, | 211 const std::string& account_id, |
| 162 net::URLRequestContextGetter* getter, | 212 net::URLRequestContextGetter* getter, |
| 163 OAuth2AccessTokenConsumer* consumer) { | 213 OAuth2AccessTokenConsumer* consumer) { |
| 164 NOTREACHED(); | 214 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
|
msarda
2014/07/01 11:36:48
I do not see the rest of the methods DCHECKing the
| |
| 165 return NULL; | 215 DCHECK(!account_id.empty()); |
| 216 return new AndroidAccessTokenFetcher(consumer, account_id); | |
| 166 } | 217 } |
| 167 | 218 |
| 168 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( | 219 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( |
| 169 const std::string& account_id, | 220 const std::string& account_id, |
| 170 const std::string& client_id, | 221 const std::string& client_id, |
| 171 const ScopeSet& scopes, | 222 const ScopeSet& scopes, |
| 172 const std::string& access_token) { | 223 const std::string& access_token) { |
| 173 OAuth2TokenService::InvalidateOAuth2Token(account_id, | 224 OAuth2TokenService::InvalidateOAuth2Token(account_id, |
| 174 client_id, | 225 client_id, |
| 175 scopes, | 226 scopes, |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 VLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; | 416 VLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials"; |
| 366 std::vector<std::string> accounts = GetAccounts(); | 417 std::vector<std::string> accounts = GetAccounts(); |
| 367 for (std::vector<std::string>::iterator it = accounts.begin(); | 418 for (std::vector<std::string>::iterator it = accounts.begin(); |
| 368 it != accounts.end(); it++) { | 419 it != accounts.end(); it++) { |
| 369 FireRefreshTokenRevoked(*it); | 420 FireRefreshTokenRevoked(*it); |
| 370 } | 421 } |
| 371 } | 422 } |
| 372 | 423 |
| 373 // Called from Java when fetching of an OAuth2 token is finished. The | 424 // Called from Java when fetching of an OAuth2 token is finished. The |
| 374 // |authToken| param is only valid when |result| is true. | 425 // |authToken| param is only valid when |result| is true. |
| 375 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 426 void OAuth2TokenFetched( |
| 427 JNIEnv* env, | |
| 428 jclass clazz, | |
| 376 jstring authToken, | 429 jstring authToken, |
| 377 jboolean result, | 430 jboolean result, |
| 378 jlong nativeCallback) { | 431 jlong nativeCallback) { |
| 379 std::string token = ConvertJavaStringToUTF8(env, authToken); | 432 std::string token = ConvertJavaStringToUTF8(env, authToken); |
| 380 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 433 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| 381 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 434 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
| 382 // Android does not provide enough information to know if the credentials are | 435 // Android does not provide enough information to know if the credentials are |
| 383 // wrong, so assume any error is transient by using CONNECTION_FAILED. | 436 // wrong, so assume any error is transient by using CONNECTION_FAILED. |
| 384 GoogleServiceAuthError err(result ? | 437 GoogleServiceAuthError err(result ? |
| 385 GoogleServiceAuthError::NONE : | 438 GoogleServiceAuthError::NONE : |
| 386 GoogleServiceAuthError::CONNECTION_FAILED); | 439 GoogleServiceAuthError::CONNECTION_FAILED); |
| 387 heap_callback->Run(err, token, base::Time()); | 440 heap_callback->Run(err, token, base::Time()); |
| 388 } | 441 } |
| 389 | 442 |
| 390 // static | 443 // static |
| 391 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 444 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
| 392 return RegisterNativesImpl(env); | 445 return RegisterNativesImpl(env); |
| 393 } | 446 } |
| OLD | NEW |