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_string.h" | 9 #include "base/android/jni_string.h" |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
12 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
13 #include "chrome/browser/sync/profile_sync_service_android.h" | 14 #include "chrome/browser/sync/profile_sync_service_android.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "jni/AndroidProfileOAuth2TokenServiceHelper_jni.h" | 16 #include "jni/AndroidProfileOAuth2TokenServiceHelper_jni.h" |
16 | 17 |
17 using base::android::AttachCurrentThread; | 18 using base::android::AttachCurrentThread; |
18 using base::android::CheckException; | |
19 using base::android::ConvertJavaStringToUTF8; | 19 using base::android::ConvertJavaStringToUTF8; |
20 using base::android::ConvertUTF8ToJavaString; | 20 using base::android::ConvertUTF8ToJavaString; |
21 using base::android::ScopedJavaLocalRef; | 21 using base::android::ScopedJavaLocalRef; |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { | 26 std::string CombineScopes(const OAuth2TokenService::ScopeSet& scopes) { |
27 // The Android AccountManager supports multiple scopes separated by a space: | 27 // The Android AccountManager supports multiple scopes separated by a space: |
28 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android | 28 // https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android |
(...skipping 18 matching lines...) Expand all Loading... | |
47 FetchOAuth2TokenCallback; | 47 FetchOAuth2TokenCallback; |
48 | 48 |
49 } // namespace | 49 } // namespace |
50 | 50 |
51 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { | 51 AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() { |
52 } | 52 } |
53 | 53 |
54 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() { | 54 AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() { |
55 } | 55 } |
56 | 56 |
57 scoped_ptr<OAuth2TokenService::Request> | 57 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable( |
58 AndroidProfileOAuth2TokenService::StartRequestForUsername( | 58 const std::string& account_id) { |
59 const std::string& username, | 59 JNIEnv* env = AttachCurrentThread(); |
60 const OAuth2TokenService::ScopeSet& scopes, | 60 ScopedJavaLocalRef<jstring> j_account_id = |
61 OAuth2TokenService::Consumer* consumer) { | 61 ConvertUTF8ToJavaString(env, account_id); |
62 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 62 jboolean refresh_token_is_available = |
63 | 63 Java_AndroidProfileOAuth2TokenServiceHelper_hasOAuth2RefreshToken( |
64 scoped_ptr<RequestImpl> request(new RequestImpl(consumer)); | 64 env, base::android::GetApplicationContext(), |
65 FetchOAuth2TokenWithUsername(request.get(), username, scopes); | 65 j_account_id.obj()); |
66 return request.PassAs<Request>(); | 66 return (bool)refresh_token_is_available; |
fgorski
2013/08/30 22:29:03
I am not sure what the preferred treatment of the
nyquist
2013/08/31 17:51:10
This is what what you do does behind the scenes no
fgorski
2013/09/03 20:50:40
Done.
| |
67 } | |
68 | |
69 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() { | |
70 SigninManagerBase* signin_manager = | |
71 SigninManagerFactory::GetForProfile(profile()); | |
72 return !signin_manager->GetAuthenticatedUsername().empty(); | |
73 } | 67 } |
74 | 68 |
75 void AndroidProfileOAuth2TokenService::InvalidateToken( | 69 void AndroidProfileOAuth2TokenService::InvalidateToken( |
76 const ScopeSet& scopes, | 70 const ScopeSet& scopes, |
77 const std::string& invalid_token) { | 71 const std::string& invalid_token) { |
78 OAuth2TokenService::InvalidateToken(scopes, invalid_token); | 72 OAuth2TokenService::InvalidateToken(scopes, invalid_token); |
79 | 73 |
80 JNIEnv* env = AttachCurrentThread(); | 74 JNIEnv* env = AttachCurrentThread(); |
81 ScopedJavaLocalRef<jstring> j_invalid_token = | 75 ScopedJavaLocalRef<jstring> j_invalid_token = |
82 ConvertUTF8ToJavaString(env, invalid_token); | 76 ConvertUTF8ToJavaString(env, invalid_token); |
83 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( | 77 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( |
84 env, base::android::GetApplicationContext(), | 78 env, base::android::GetApplicationContext(), |
85 j_invalid_token.obj()); | 79 j_invalid_token.obj()); |
86 } | 80 } |
87 | 81 |
82 std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { | |
83 std::vector<std::string> accounts; | |
84 JNIEnv* env = AttachCurrentThread(); | |
85 ScopedJavaLocalRef<jobjectArray> j_accounts = | |
86 Java_AndroidProfileOAuth2TokenServiceHelper_getAccounts( | |
87 env, base::android::GetApplicationContext()); | |
88 base::android::AppendJavaStringArrayToStringVector(env, | |
89 j_accounts.obj(), | |
Andrew T Wilson (Slow)
2013/09/03 14:04:24
Is the idea that we'll ultimately give the user co
fgorski
2013/09/03 20:50:40
I raised that with Roger in a phone call before I
| |
90 &accounts); | |
91 return accounts; | |
92 } | |
93 | |
88 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( | 94 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( |
89 RequestImpl* request, | 95 RequestImpl* request, |
96 const std::string& account_id, | |
90 net::URLRequestContextGetter* getter, | 97 net::URLRequestContextGetter* getter, |
91 const std::string& client_id, | 98 const std::string& client_id, |
92 const std::string& client_secret, | 99 const std::string& client_secret, |
93 const OAuth2TokenService::ScopeSet& scopes) { | 100 const OAuth2TokenService::ScopeSet& scopes) { |
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 101 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
95 std::string username = SigninManagerFactory::GetForProfile(profile())-> | 102 DCHECK(!account_id.empty()); |
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 | 103 |
102 void AndroidProfileOAuth2TokenService::FetchOAuth2TokenWithUsername( | |
103 RequestImpl* request, | |
104 const std::string& username, | |
105 const OAuth2TokenService::ScopeSet& scopes) { | |
106 JNIEnv* env = AttachCurrentThread(); | 104 JNIEnv* env = AttachCurrentThread(); |
107 std::string scope = CombineScopes(scopes); | 105 std::string scope = CombineScopes(scopes); |
108 ScopedJavaLocalRef<jstring> j_username = | 106 ScopedJavaLocalRef<jstring> j_username = |
109 ConvertUTF8ToJavaString(env, username); | 107 ConvertUTF8ToJavaString(env, account_id); |
110 ScopedJavaLocalRef<jstring> j_scope = | 108 ScopedJavaLocalRef<jstring> j_scope = |
111 ConvertUTF8ToJavaString(env, scope); | 109 ConvertUTF8ToJavaString(env, scope); |
112 | 110 |
113 // Allocate a copy of the request WeakPtr on the heap, because the object | 111 // Allocate a copy of the request WeakPtr on the heap, because the object |
Andrew T Wilson (Slow)
2013/09/03 14:04:24
nit: indent
fgorski
2013/09/03 20:50:40
Done.
| |
114 // needs to be passed through JNI as an int. | 112 // needs to be passed through JNI as an int. |
115 // It will be passed back to OAuth2TokenFetched(), where it will be freed. | 113 // It will be passed back to OAuth2TokenFetched(), where it will be freed. |
116 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 114 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
117 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, | 115 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, |
118 request->AsWeakPtr()))); | 116 request->AsWeakPtr()))); |
119 | 117 |
120 // Call into Java to get a new token. | 118 // Call into Java to get a new token. |
121 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( | 119 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( |
122 env, base::android::GetApplicationContext(), | 120 env, base::android::GetApplicationContext(), |
123 j_username.obj(), | 121 j_username.obj(), |
(...skipping 13 matching lines...) Expand all Loading... | |
137 GoogleServiceAuthError err(result ? | 135 GoogleServiceAuthError err(result ? |
138 GoogleServiceAuthError::NONE : | 136 GoogleServiceAuthError::NONE : |
139 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 137 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
140 heap_callback->Run(err, token, base::Time()); | 138 heap_callback->Run(err, token, base::Time()); |
141 } | 139 } |
142 | 140 |
143 // static | 141 // static |
144 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 142 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
145 return RegisterNativesImpl(env); | 143 return RegisterNativesImpl(env); |
146 } | 144 } |
OLD | NEW |