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 refresh_token_is_available != JNI_FALSE; |
67 } | 67 } |
68 | 68 |
69 bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable() { | 69 std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() { |
70 SigninManagerBase* signin_manager = | 70 std::vector<std::string> accounts; |
71 SigninManagerFactory::GetForProfile(profile()); | |
72 return !signin_manager->GetAuthenticatedUsername().empty(); | |
73 } | |
74 | |
75 void AndroidProfileOAuth2TokenService::InvalidateToken( | |
76 const ScopeSet& scopes, | |
77 const std::string& invalid_token) { | |
78 OAuth2TokenService::InvalidateToken(scopes, invalid_token); | |
79 | |
80 JNIEnv* env = AttachCurrentThread(); | 71 JNIEnv* env = AttachCurrentThread(); |
81 ScopedJavaLocalRef<jstring> j_invalid_token = | 72 ScopedJavaLocalRef<jobjectArray> j_accounts = |
82 ConvertUTF8ToJavaString(env, invalid_token); | 73 Java_AndroidProfileOAuth2TokenServiceHelper_getAccounts( |
83 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( | 74 env, base::android::GetApplicationContext()); |
84 env, base::android::GetApplicationContext(), | 75 // TODO(fgorski): We may decide to filter out some of the accounts. |
85 j_invalid_token.obj()); | 76 base::android::AppendJavaStringArrayToStringVector(env, |
| 77 j_accounts.obj(), |
| 78 &accounts); |
| 79 return accounts; |
86 } | 80 } |
87 | 81 |
88 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( | 82 void AndroidProfileOAuth2TokenService::FetchOAuth2Token( |
89 RequestImpl* request, | 83 RequestImpl* request, |
| 84 const std::string& account_id, |
90 net::URLRequestContextGetter* getter, | 85 net::URLRequestContextGetter* getter, |
91 const std::string& client_id, | 86 const std::string& client_id, |
92 const std::string& client_secret, | 87 const std::string& client_secret, |
93 const OAuth2TokenService::ScopeSet& scopes) { | 88 const OAuth2TokenService::ScopeSet& scopes) { |
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
95 std::string username = SigninManagerFactory::GetForProfile(profile())-> | 90 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 | 91 |
102 void AndroidProfileOAuth2TokenService::FetchOAuth2TokenWithUsername( | |
103 RequestImpl* request, | |
104 const std::string& username, | |
105 const OAuth2TokenService::ScopeSet& scopes) { | |
106 JNIEnv* env = AttachCurrentThread(); | 92 JNIEnv* env = AttachCurrentThread(); |
107 std::string scope = CombineScopes(scopes); | 93 std::string scope = CombineScopes(scopes); |
108 ScopedJavaLocalRef<jstring> j_username = | 94 ScopedJavaLocalRef<jstring> j_username = |
109 ConvertUTF8ToJavaString(env, username); | 95 ConvertUTF8ToJavaString(env, account_id); |
110 ScopedJavaLocalRef<jstring> j_scope = | 96 ScopedJavaLocalRef<jstring> j_scope = |
111 ConvertUTF8ToJavaString(env, scope); | 97 ConvertUTF8ToJavaString(env, scope); |
112 | 98 |
113 // Allocate a copy of the request WeakPtr on the heap, because the object | 99 // Allocate a copy of the request WeakPtr on the heap, because the object |
114 // needs to be passed through JNI as an int. | 100 // needs to be passed through JNI as an int. |
115 // It will be passed back to OAuth2TokenFetched(), where it will be freed. | 101 // It will be passed back to OAuth2TokenFetched(), where it will be freed. |
116 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 102 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
117 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, | 103 new FetchOAuth2TokenCallback(base::Bind(&RequestImpl::InformConsumer, |
118 request->AsWeakPtr()))); | 104 request->AsWeakPtr()))); |
119 | 105 |
120 // Call into Java to get a new token. | 106 // Call into Java to get a new token. |
121 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( | 107 Java_AndroidProfileOAuth2TokenServiceHelper_getOAuth2AuthToken( |
122 env, base::android::GetApplicationContext(), | 108 env, base::android::GetApplicationContext(), |
123 j_username.obj(), | 109 j_username.obj(), |
124 j_scope.obj(), | 110 j_scope.obj(), |
125 reinterpret_cast<int>(heap_callback.release())); | 111 reinterpret_cast<int>(heap_callback.release())); |
126 } | 112 } |
127 | 113 |
| 114 void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token( |
| 115 const std::string& account_id, |
| 116 const std::string& client_id, |
| 117 const ScopeSet& scopes, |
| 118 const std::string& access_token) { |
| 119 OAuth2TokenService::InvalidateOAuth2Token(account_id, |
| 120 client_id, |
| 121 scopes, |
| 122 access_token); |
| 123 |
| 124 JNIEnv* env = AttachCurrentThread(); |
| 125 ScopedJavaLocalRef<jstring> j_access_token = |
| 126 ConvertUTF8ToJavaString(env, access_token); |
| 127 Java_AndroidProfileOAuth2TokenServiceHelper_invalidateOAuth2AuthToken( |
| 128 env, base::android::GetApplicationContext(), |
| 129 j_access_token.obj()); |
| 130 } |
| 131 |
128 // Called from Java when fetching of an OAuth2 token is finished. The | 132 // Called from Java when fetching of an OAuth2 token is finished. The |
129 // |authToken| param is only valid when |result| is true. | 133 // |authToken| param is only valid when |result| is true. |
130 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, | 134 void OAuth2TokenFetched(JNIEnv* env, jclass clazz, |
131 jstring authToken, | 135 jstring authToken, |
132 jboolean result, | 136 jboolean result, |
133 jint nativeCallback) { | 137 jint nativeCallback) { |
134 std::string token = ConvertJavaStringToUTF8(env, authToken); | 138 std::string token = ConvertJavaStringToUTF8(env, authToken); |
135 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | 139 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( |
136 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); | 140 reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback)); |
137 GoogleServiceAuthError err(result ? | 141 GoogleServiceAuthError err(result ? |
138 GoogleServiceAuthError::NONE : | 142 GoogleServiceAuthError::NONE : |
139 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 143 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
140 heap_callback->Run(err, token, base::Time()); | 144 heap_callback->Run(err, token, base::Time()); |
141 } | 145 } |
142 | 146 |
143 // static | 147 // static |
144 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { | 148 bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) { |
145 return RegisterNativesImpl(env); | 149 return RegisterNativesImpl(env); |
146 } | 150 } |
OLD | NEW |