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_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 Loading... | |
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&)> | |
awong
2013/08/16 17:53:16
nit: passing base::Time by value is oft preferred
Andrew T Wilson (Slow)
2013/08/19 12:15:56
This callback basically matches OAuth2TokenService
| |
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(), |
68 username, | 66 username, |
69 CombineScopes(scopes), | 67 scopes); |
70 base::Bind(&RequestImpl::InformConsumer, request->AsWeakPtr())); | |
71 return request.PassAs<Request>(); | 68 return request.PassAs<Request>(); |
72 } | 69 } |
73 | 70 |
74 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() { | 71 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() { |
75 SigninManagerBase* signin_manager = | 72 SigninManagerBase* signin_manager = |
76 SigninManagerFactory::GetForProfile(profile()); | 73 SigninManagerFactory::GetForProfile(profile()); |
77 return !signin_manager->GetAuthenticatedUsername().empty(); | 74 return !signin_manager->GetAuthenticatedUsername().empty(); |
78 } | 75 } |
79 | 76 |
80 void AndroidProfileOAuth2TokenService::InvalidateToken( | 77 void AndroidProfileOAuth2TokenService::InvalidateToken( |
81 const ScopeSet& scopes, | 78 const ScopeSet& scopes, |
82 const std::string& invalid_token) { | 79 const std::string& invalid_token) { |
83 OAuth2TokenService::InvalidateToken(scopes, invalid_token); | 80 OAuth2TokenService::InvalidateToken(scopes, invalid_token); |
84 | 81 |
85 JNIEnv* env = AttachCurrentThread(); | 82 JNIEnv* env = AttachCurrentThread(); |
86 ScopedJavaLocalRef<jstring> j_invalid_token = | 83 ScopedJavaLocalRef<jstring> j_invalid_token = |
87 ConvertUTF8ToJavaString(env, invalid_token); | 84 ConvertUTF8ToJavaString(env, invalid_token); |
88 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( | 85 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( |
89 env, base::android::GetApplicationContext(), | 86 env, base::android::GetApplicationContext(), |
90 j_invalid_token.obj()); | 87 j_invalid_token.obj()); |
91 } | 88 } |
92 | 89 |
93 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( | 90 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( |
91 RequestImpl* request, | |
92 net::URLRequestContextGetter* getter, | |
93 const std::string& client_id, | |
94 const std::string& client_secret, | |
95 const OAuth2TokenService::ScopeSet& scopes) { | |
96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
97 const std::string& username = | |
awong
2013/08/16 17:53:16
nit: you rely on RVO on line 110 but use a const &
Andrew T Wilson (Slow)
2013/08/19 12:15:56
Done.
| |
98 SigninManagerFactory::GetForProfile(profile())-> | |
99 GetAuthenticatedUsername(); | |
100 DCHECK(!username.empty()); | |
101 // Just ignore client_id, getter, etc since we don't use them on Android. | |
102 FetchOAuth2TokenWithUsername(request, username, scopes); | |
103 } | |
104 | |
105 void AndroidProfileOAuth2TokenService::FetchOAuth2TokenWithUsername( | |
106 RequestImpl* request, | |
94 const std::string& username, | 107 const std::string& username, |
95 const std::string& scope, | 108 const OAuth2TokenService::ScopeSet& scopes) { |
96 const FetchOAuth2TokenCallback& callback) { | |
97 JNIEnv* env = AttachCurrentThread(); | 109 JNIEnv* env = AttachCurrentThread(); |
110 std::string scope = CombineScopes(scopes); | |
98 ScopedJavaLocalRef<jstring> j_username = | 111 ScopedJavaLocalRef<jstring> j_username = |
99 ConvertUTF8ToJavaString(env, username); | 112 ConvertUTF8ToJavaString(env, username); |
100 ScopedJavaLocalRef<jstring> j_scope = | 113 ScopedJavaLocalRef<jstring> j_scope = |
101 ConvertUTF8ToJavaString(env, scope); | 114 ConvertUTF8ToJavaString(env, scope); |
102 | 115 |
103 // Allocate a copy of the callback on the heap, because the callback | 116 // Allocate a copy of the request WeakPtr on the heap, because the object |
104 // needs to be passed through JNI as an int. | 117 // needs to be passed through JNI as an int. |
105 // It will be passed back to OAuth2TokenFetched(), where it will be freed. | 118 // It will be passed back to OAuth2TokenFetched(), where it will be freed. |
106 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 119 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
107 new FetchOAuth2TokenCallback(callback)); | 120 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, |
121 request->AsWeakPtr()))); | |
108 | 122 |
109 // Call into Java to get a new token. | 123 // Call into Java to get a new token. |
110 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( | 124 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( |
111 env, base::android::GetApplicationContext(), | 125 env, base::android::GetApplicationContext(), |
112 j_username.obj(), | 126 j_username.obj(), |
113 j_scope.obj(), | 127 j_scope.obj(), |
114 reinterpret_cast<int>(heap_callback.release())); | 128 reinterpret_cast<int>(heap_callback.release())); |
115 } | 129 } |
116 | 130 |
117 // Called from Java when fetching of an OAuth2 token is finished. The | 131 // Called from Java when fetching of an OAuth2 token is finished. The |
118 // |authToken| param is only valid when |result| is true. | 132 // |authToken| param is only valid when |result| is true. |
119 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 133 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, |
120 jstring authToken, | 134 jstring authToken, |
121 jboolean result, | 135 jboolean result, |
122 jint nativeCallback) { | 136 jint nativeCallback) { |
123 std::string token = ConvertJavaStringToUTF8(env, authToken); | 137 std::string token = ConvertJavaStringToUTF8(env, authToken); |
124 scoped_ptr<AndroidProfileOAuth2TokenService::FetchOAuth2TokenCallback> | 138 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
125 heap_callback( | 139 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
126 reinterpret_cast< | |
127 AndroidProfileOAuth2TokenService::FetchOAuth2TokenCallback*>( | |
128 nativeCallback)); | |
129 GoogleServiceAuthError err(result ? | 140 GoogleServiceAuthError err(result ? |
130 GoogleServiceAuthError::NONE : | 141 GoogleServiceAuthError::NONE : |
131 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 142 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
132 heap_callback->Run(err, token, base::Time()); | 143 heap_callback->Run(err, token, base::Time()); |
133 } | 144 } |
134 | 145 |
135 // static | 146 // static |
136 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 147 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
137 return RegisterNativesImpl(env); | 148 return RegisterNativesImpl(env); |
138 } | 149 } |
OLD | NEW |