Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/signin/android_profile_oauth2_token_service.cc

Issue 23068005: Convert UserPolicySigninService to use OAuth2TokenService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with ToT Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 18 matching lines...) Expand all
29 std::string scope; 29 std::string scope;
30 for (OAuth2TokenService::ScopeSet::const_iterator it = scopes.begin(); 30 for (OAuth2TokenService::ScopeSet::const_iterator it = scopes.begin();
31 it != scopes.end(); ++it) { 31 it != scopes.end(); ++it) {
32 if (!scope.empty()) 32 if (!scope.empty())
33 scope += " "; 33 scope += " ";
34 scope += *it; 34 scope += *it;
35 } 35 }
36 return scope; 36 return scope;
37 } 37 }
38 38
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
39 } // namespace 49 } // namespace
40 50
41 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { 51 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() {
42 } 52 }
43 53
44 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() { 54 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() {
45 } 55 }
46 56
47 scoped_ptr<OAuth2TokenService::Request> 57 scoped_ptr<OAuth2TokenService::Request>
48 AndroidProfileOAuth2TokenService::StartRequest(
49 const OAuth2TokenService::ScopeSet& scopes,
50 OAuth2TokenService::Consumer* consumer) {
51 const std::string& sync_username =
52 SigninManagerFactory::GetForProfile(profile())->
53 GetAuthenticatedUsername();
54 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
55 DCHECK(!sync_username.empty());
56 return StartRequestForUsername(sync_username, scopes, consumer);
57 }
58
59 scoped_ptr<OAuth2TokenService::Request>
60 AndroidProfileOAuth2TokenService::StartRequestForUsername( 58 AndroidProfileOAuth2TokenService::StartRequestForUsername(
61 const std::string& username, 59 const std::string& username,
62 const OAuth2TokenService::ScopeSet& scopes, 60 const OAuth2TokenService::ScopeSet& scopes,
63 OAuth2TokenService::Consumer* consumer) { 61 OAuth2TokenService::Consumer* consumer) {
64 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
65 63
66 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); 64 scoped_ptr<RequestImpl> request(new RequestImpl(consumer));
67 FetchOAuth2Token( 65 FetchOAuth2TokenWithUsername(request.get(), username, scopes);
68 username,
69 CombineScopes(scopes),
70 base::Bind(&RequestImpl::InformConsumer, request->AsWeakPtr()));
71 return request.PassAs<Request>(); 66 return request.PassAs<Request>();
72 } 67 }
73 68
74 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() { 69 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() {
75 SigninManagerBase* signin_manager = 70 SigninManagerBase* signin_manager =
76 SigninManagerFactory::GetForProfile(profile()); 71 SigninManagerFactory::GetForProfile(profile());
77 return !signin_manager->GetAuthenticatedUsername().empty(); 72 return !signin_manager->GetAuthenticatedUsername().empty();
78 } 73 }
79 74
80 void AndroidProfileOAuth2TokenService::InvalidateToken( 75 void AndroidProfileOAuth2TokenService::InvalidateToken(
81 const ScopeSet& scopes, 76 const ScopeSet& scopes,
82 const std::string& invalid_token) { 77 const std::string& invalid_token) {
83 OAuth2TokenService::InvalidateToken(scopes, invalid_token); 78 OAuth2TokenService::InvalidateToken(scopes, invalid_token);
84 79
85 JNIEnv* env = AttachCurrentThread(); 80 JNIEnv* env = AttachCurrentThread();
86 ScopedJavaLocalRef<jstring> j_invalid_token = 81 ScopedJavaLocalRef<jstring> j_invalid_token =
87 ConvertUTF8ToJavaString(env, invalid_token); 82 ConvertUTF8ToJavaString(env, invalid_token);
88 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( 83 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken(
89 env, base::android::GetApplicationContext(), 84 env, base::android::GetApplicationContext(),
90 j_invalid_token.obj()); 85 j_invalid_token.obj());
91 } 86 }
92 87
93 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( 88 void AndroidProfileOAuth2TokenService::FetchOAuth2Token(
89 RequestImpl* request,
90 net::URLRequestContextGetter* getter,
91 const std::string& client_id,
92 const std::string& client_secret,
93 const OAuth2TokenService::ScopeSet& scopes) {
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
95 std::string username = SigninManagerFactory::GetForProfile(profile())->
96 GetAuthenticatedUsername();
97 DCHECK(!username.empty());
98 // Just ignore client_id, getter, etc since we don't use them on Android.
99 FetchOAuth2TokenWithUsername(request, username, scopes);
100 }
101
102 void AndroidProfileOAuth2TokenService::FetchOAuth2TokenWithUsername(
103 RequestImpl* request,
94 const std::string& username, 104 const std::string& username,
95 const std::string& scope, 105 const OAuth2TokenService::ScopeSet& scopes) {
96 const FetchOAuth2TokenCallback& callback) {
97 JNIEnv* env = AttachCurrentThread(); 106 JNIEnv* env = AttachCurrentThread();
107 std::string scope = CombineScopes(scopes);
98 ScopedJavaLocalRef<jstring> j_username = 108 ScopedJavaLocalRef<jstring> j_username =
99 ConvertUTF8ToJavaString(env, username); 109 ConvertUTF8ToJavaString(env, username);
100 ScopedJavaLocalRef<jstring> j_scope = 110 ScopedJavaLocalRef<jstring> j_scope =
101 ConvertUTF8ToJavaString(env, scope); 111 ConvertUTF8ToJavaString(env, scope);
102 112
103 // Allocate a copy of the callback on the heap, because the callback 113 // Allocate a copy of the request WeakPtr on the heap, because the object
104 // needs to be passed through JNI as an int. 114 // needs to be passed through JNI as an int.
105 // It will be passed back to OAuth2TokenFetched(), where it will be freed. 115 // It will be passed back to OAuth2TokenFetched(), where it will be freed.
106 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( 116 scoped_ptr<FetchOAuth2TokenCallback> heap_callback(
107 new FetchOAuth2TokenCallback(callback)); 117 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer,
118 request->AsWeakPtr())));
108 119
109 // Call into Java to get a new token. 120 // Call into Java to get a new token.
110 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( 121 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken(
111 env, base::android::GetApplicationContext(), 122 env, base::android::GetApplicationContext(),
112 j_username.obj(), 123 j_username.obj(),
113 j_scope.obj(), 124 j_scope.obj(),
114 reinterpret_cast<int>(heap_callback.release())); 125 reinterpret_cast<int>(heap_callback.release()));
115 } 126 }
116 127
117 // Called from Java when fetching of an OAuth2 token is finished. The 128 // Called from Java when fetching of an OAuth2 token is finished. The
118 // |authToken| param is only valid when |result| is true. 129 // |authToken| param is only valid when |result| is true.
119 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, 130 void OAuth2TokenFetched(JNIEnv* env, jclass clazz,
120 jstring authToken, 131 jstring authToken,
121 jboolean result, 132 jboolean result,
122 jint nativeCallback) { 133 jint nativeCallback) {
123 std::string token = ConvertJavaStringToUTF8(env, authToken); 134 std::string token = ConvertJavaStringToUTF8(env, authToken);
124 scoped_ptr<AndroidProfileOAuth2TokenService::FetchOAuth2TokenCallback> 135 scoped_ptr<FetchOAuth2TokenCallback> heap_callback(
125 heap_callback( 136 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback));
126 reinterpret_cast<
127 AndroidProfileOAuth2TokenService::FetchOAuth2TokenCallback*>(
128 nativeCallback));
129 GoogleServiceAuthError err(result ? 137 GoogleServiceAuthError err(result ?
130 GoogleServiceAuthError::NONE : 138 GoogleServiceAuthError::NONE :
131 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 139 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
132 heap_callback->Run(err, token, base::Time()); 140 heap_callback->Run(err, token, base::Time());
133 } 141 }
134 142
135 // static 143 // static
136 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { 144 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) {
137 return RegisterNativesImpl(env); 145 return RegisterNativesImpl(env);
138 } 146 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698