Chromium Code Reviews| Index: chrome/browser/signin/android_profile_oauth2_token_service.cc |
| diff --git a/chrome/browser/signin/android_profile_oauth2_token_service.cc b/chrome/browser/signin/android_profile_oauth2_token_service.cc |
| index cb2dea71834a658cafb506030c836a5e2b44fbc8..ff986d83f4d68305811fc693d2ca63eb46f9b022 100644 |
| --- a/chrome/browser/signin/android_profile_oauth2_token_service.cc |
| +++ b/chrome/browser/signin/android_profile_oauth2_token_service.cc |
| @@ -13,6 +13,7 @@ |
| #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| #include "chrome/browser/sync/profile_sync_service_android.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| #include "jni/OAuth2TokenService_jni.h" |
| using base::android::AttachCurrentThread; |
| @@ -23,11 +24,11 @@ using content::BrowserThread; |
| namespace { |
| -std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { |
| +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.
|
| // The Android AccountManager supports multiple scopes separated by a space: |
| // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android |
| std::string scope; |
| - for (OAuth2TokenService::ScopeSet::const_iterator it = scopes.begin(); |
| + for (std::vector<std::string>::const_iterator it = scopes.begin(); |
| it != scopes.end(); ++it) { |
| if (!scope.empty()) |
| scope += " "; |
| @@ -46,6 +47,83 @@ typedef base::Callback<void( |
| const GoogleServiceAuthError&, const std::string&, const base::Time&)> |
| FetchOAuth2TokenCallback; |
| +class AndroidAccessTokenFetcher : public OAuth2AccessTokenFetcher { |
| + public: |
| + AndroidAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, |
| + const std::string& account_id); |
| + virtual ~AndroidAccessTokenFetcher(); |
| + |
| + // Overrides from OAuth2AccessTokenFetcher: |
| + virtual void Start(const std::string& client_id, |
| + const std::string& client_secret, |
| + const std::vector<std::string>& scopes) OVERRIDE; |
| + 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
|
| + |
| + // Handles an access token response. |
| + void OnAccessTokenResponse(const GoogleServiceAuthError& error, |
| + const std::string& access_token, |
| + const base::Time& expiration_time); |
| + |
| + private: |
| + base::WeakPtrFactory<AndroidAccessTokenFetcher> weak_factory_; |
| + std::string account_id_; |
| + bool request_was_cancelled_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AndroidAccessTokenFetcher); |
| +}; |
| + |
| +AndroidAccessTokenFetcher::AndroidAccessTokenFetcher( |
| + OAuth2AccessTokenConsumer* consumer, |
| + const std::string& account_id) |
| + : OAuth2AccessTokenFetcher(consumer), |
| + weak_factory_(this), |
| + account_id_(account_id), |
| + request_was_cancelled_(false) { |
| +} |
| + |
| +AndroidAccessTokenFetcher::~AndroidAccessTokenFetcher() {} |
| + |
| +void AndroidAccessTokenFetcher::Start(const std::string& client_id, |
| + const std::string& client_secret, |
| + const std::vector<std::string>& scopes) { |
| + JNIEnv* env = AttachCurrentThread(); |
| + std::string scope = CombineScopes(scopes); |
| + ScopedJavaLocalRef<jstring> j_username = |
| + ConvertUTF8ToJavaString(env, account_id_); |
| + ScopedJavaLocalRef<jstring> j_scope = |
| + ConvertUTF8ToJavaString(env, scope); |
| + scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| + new FetchOAuth2TokenCallback( |
| + base::Bind(&AndroidAccessTokenFetcher::OnAccessTokenResponse, |
| + weak_factory_.GetWeakPtr()))); |
| + |
| + // Call into Java to get a new token. |
| + Java_OAuth2TokenService_getOAuth2AuthToken( |
| + env, base::android::GetApplicationContext(), |
| + j_username.obj(), |
| + j_scope.obj(), |
| + reinterpret_cast<intptr_t>(heap_callback.release())); |
| +} |
| + |
| +void AndroidAccessTokenFetcher::CancelRequest() { |
| + request_was_cancelled_ = true; |
| +} |
| + |
| +void AndroidAccessTokenFetcher::OnAccessTokenResponse( |
| + const GoogleServiceAuthError& error, |
| + const std::string& access_token, |
| + const base::Time& expiration_time) { |
| + if (request_was_cancelled_) { |
| + // Ignore the callback if the request was cancelled. |
| + return; |
| + } |
| + if (error.state() == GoogleServiceAuthError::NONE) { |
| + FireOnGetTokenSuccess(access_token, expiration_time); |
| + } else { |
| + FireOnGetTokenFailure(error); |
| + } |
| +} |
| + |
| } // namespace |
| bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; |
| @@ -124,45 +202,14 @@ std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() { |
| return accounts; |
| } |
| -void AndroidProfileOAuth2TokenService::FetchOAuth2Token( |
| - RequestImpl* request, |
| - const std::string& account_id, |
| - net::URLRequestContextGetter* getter, |
| - const std::string& client_id, |
| - const std::string& client_secret, |
| - const OAuth2TokenService::ScopeSet& scopes) { |
| - DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| - DCHECK(!account_id.empty()); |
| - |
| - JNIEnv* env = AttachCurrentThread(); |
| - std::string scope = CombineScopes(scopes); |
| - ScopedJavaLocalRef<jstring> j_username = |
| - ConvertUTF8ToJavaString(env, account_id); |
| - ScopedJavaLocalRef<jstring> j_scope = |
| - ConvertUTF8ToJavaString(env, scope); |
| - |
| - // Allocate a copy of the request WeakPtr on the heap, because the object |
| - // needs to be passed through JNI as an int. |
| - // It will be passed back to OAuth2TokenFetched(), where it will be freed. |
| - scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
| - new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, |
| - request->AsWeakPtr()))); |
| - |
| - // Call into Java to get a new token. |
| - Java_OAuth2TokenService_getOAuth2AuthToken( |
| - env, base::android::GetApplicationContext(), |
| - j_username.obj(), |
| - j_scope.obj(), |
| - reinterpret_cast<intptr_t>(heap_callback.release())); |
| -} |
| - |
| OAuth2AccessTokenFetcher* |
| AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( |
| const std::string& account_id, |
| net::URLRequestContextGetter* getter, |
| OAuth2AccessTokenConsumer* consumer) { |
| - NOTREACHED(); |
| - return NULL; |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + DCHECK(!account_id.empty()); |
| + return new AndroidAccessTokenFetcher(consumer, account_id); |
| } |
| void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( |
| @@ -372,7 +419,9 @@ void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { |
| // Called from Java when fetching of an OAuth2 token is finished. The |
| // |authToken| param is only valid when |result| is true. |
| -void OAuth2TokenFetched(JNIEnv* env, jclass clazz, |
| +void OAuth2TokenFetched( |
| + JNIEnv* env, |
| + jclass clazz, |
| jstring authToken, |
| jboolean result, |
| jlong nativeCallback) { |