Commit 69d3373d authored by rogerta's avatar rogerta Committed by Commit bot

Use OAuth2AccessTokenFetcher implementation on Android. This cleans up the...

Use OAuth2AccessTokenFetcher implementation on Android.  This cleans up the way that AO2TS overrides functionality from the base class to differentiate the way that access tokens are minted, and makes the overriding mechanism for android the same as for iOS.  Its possible to reimplement OAuth2AccessTokenFetcher now that Mihai extended for use in bling.

BUG=390353

Review URL: https://codereview.chromium.org/345703003

Cr-Commit-Position: refs/heads/master@{#292205}
parent 2e2ec7c1
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/sync/profile_sync_service_android.h" #include "chrome/browser/sync/profile_sync_service_android.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "jni/OAuth2TokenService_jni.h" #include "jni/OAuth2TokenService_jni.h"
using base::android::AttachCurrentThread; using base::android::AttachCurrentThread;
...@@ -23,11 +24,102 @@ using content::BrowserThread; ...@@ -23,11 +24,102 @@ using content::BrowserThread;
namespace { namespace {
std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { // Callback from FetchOAuth2TokenWithUsername().
// Arguments:
// - the error, or NONE if the token fetch was successful.
// - the OAuth2 access token.
// - the expiry time of the token (may be null, indicating that the expiry
// time is unknown.
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;
// Handles an access token response.
void OnAccessTokenResponse(const GoogleServiceAuthError& error,
const std::string& access_token,
const base::Time& expiration_time);
private:
std::string CombineScopes(const std::vector<std::string>& scopes);
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);
}
}
// static
std::string AndroidAccessTokenFetcher::CombineScopes(
const std::vector<std::string>& scopes) {
// The Android AccountManager supports multiple scopes separated by a space: // The Android AccountManager supports multiple scopes separated by a space:
// https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android
std::string scope; 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) { it != scopes.end(); ++it) {
if (!scope.empty()) if (!scope.empty())
scope += " "; scope += " ";
...@@ -36,16 +128,6 @@ std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { ...@@ -36,16 +128,6 @@ std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) {
return scope; return scope;
} }
// Callback from FetchOAuth2TokenWithUsername().
// Arguments:
// - the error, or NONE if the token fetch was successful.
// - the OAuth2 access token.
// - the expiry time of the token (may be null, indicating that the expiry
// time is unknown.
typedef base::Callback<void(
const GoogleServiceAuthError&, const std::string&, const base::Time&)>
FetchOAuth2TokenCallback;
} // namespace } // namespace
bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false; bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false;
...@@ -99,6 +181,12 @@ bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable( ...@@ -99,6 +181,12 @@ bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable(
return refresh_token_is_available == JNI_TRUE; return refresh_token_is_available == JNI_TRUE;
} }
void AndroidProfileOAuth2TokenService::UpdateAuthError(
const std::string& account_id,
const GoogleServiceAuthError& error) {
// TODO(rogerta): do we need to update anything, or does the system handle it?
}
std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() {
std::vector<std::string> accounts; std::vector<std::string> accounts;
JNIEnv* env = AttachCurrentThread(); JNIEnv* env = AttachCurrentThread();
...@@ -124,45 +212,13 @@ std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() { ...@@ -124,45 +212,13 @@ std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() {
return accounts; 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* OAuth2AccessTokenFetcher*
AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher( AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher(
const std::string& account_id, const std::string& account_id,
net::URLRequestContextGetter* getter, net::URLRequestContextGetter* getter,
OAuth2AccessTokenConsumer* consumer) { OAuth2AccessTokenConsumer* consumer) {
NOTREACHED(); DCHECK(!account_id.empty());
return NULL; return new AndroidAccessTokenFetcher(consumer, account_id);
} }
void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token(
...@@ -383,7 +439,9 @@ void AndroidProfileOAuth2TokenService::RevokeAllCredentials() { ...@@ -383,7 +439,9 @@ void AndroidProfileOAuth2TokenService::RevokeAllCredentials() {
// Called from Java when fetching of an OAuth2 token is finished. The // Called from Java when fetching of an OAuth2 token is finished. The
// |authToken| param is only valid when |result| is true. // |authToken| param is only valid when |result| is true.
void OAuth2TokenFetched(JNIEnv* env, jclass clazz, void OAuth2TokenFetched(
JNIEnv* env,
jclass clazz,
jstring authToken, jstring authToken,
jboolean result, jboolean result,
jlong nativeCallback) { jlong nativeCallback) {
......
...@@ -47,6 +47,9 @@ class AndroidProfileOAuth2TokenService : public ProfileOAuth2TokenService { ...@@ -47,6 +47,9 @@ class AndroidProfileOAuth2TokenService : public ProfileOAuth2TokenService {
virtual void Initialize(SigninClient* client) OVERRIDE; virtual void Initialize(SigninClient* client) OVERRIDE;
virtual bool RefreshTokenIsAvailable( virtual bool RefreshTokenIsAvailable(
const std::string& account_id) const OVERRIDE; const std::string& account_id) const OVERRIDE;
virtual void UpdateAuthError(
const std::string& account_id,
const GoogleServiceAuthError& error) OVERRIDE;
virtual std::vector<std::string> GetAccounts() OVERRIDE; virtual std::vector<std::string> GetAccounts() OVERRIDE;
// Lists account at the OS level. // Lists account at the OS level.
...@@ -88,18 +91,6 @@ class AndroidProfileOAuth2TokenService : public ProfileOAuth2TokenService { ...@@ -88,18 +91,6 @@ class AndroidProfileOAuth2TokenService : public ProfileOAuth2TokenService {
AndroidProfileOAuth2TokenService(); AndroidProfileOAuth2TokenService();
virtual ~AndroidProfileOAuth2TokenService(); virtual ~AndroidProfileOAuth2TokenService();
// Overridden from OAuth2TokenService to intercept token fetch requests and
// redirect them to the Account Manager.
virtual void FetchOAuth2Token(RequestImpl* request,
const std::string& account_id,
net::URLRequestContextGetter* getter,
const std::string& client_id,
const std::string& client_secret,
const ScopeSet& scopes) OVERRIDE;
// Overriden from OAuth2TokenService to avoid compile errors. Has NOTREACHED()
// implementation as |AndroidProfileOAuth2TokenService| overrides
// |FetchOAuth2Token| and thus bypasses this method entirely.
virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher( virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
const std::string& account_id, const std::string& account_id,
net::URLRequestContextGetter* getter, net::URLRequestContextGetter* getter,
......
...@@ -344,6 +344,8 @@ size_t OAuth2TokenService::Fetcher::GetWaitingRequestCount() const { ...@@ -344,6 +344,8 @@ size_t OAuth2TokenService::Fetcher::GetWaitingRequestCount() const {
} }
void OAuth2TokenService::Fetcher::Cancel() { void OAuth2TokenService::Fetcher::Cancel() {
if (fetcher_)
fetcher_->CancelRequest();
fetcher_.reset(); fetcher_.reset();
retry_timer_.Stop(); retry_timer_.Stop();
error_ = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); error_ = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment