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/sync/profile_sync_service_android.h" | 5 #include "chrome/browser/sync/profile_sync_service_android.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/i18n/time_formatting.h" | 10 #include "base/i18n/time_formatting.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
18 #include "chrome/browser/signin/oauth2_token_service.h" | |
19 #include "chrome/browser/signin/signin_manager.h" | 18 #include "chrome/browser/signin/signin_manager.h" |
20 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
21 #include "chrome/browser/signin/token_service.h" | 20 #include "chrome/browser/signin/token_service.h" |
22 #include "chrome/browser/signin/token_service_factory.h" | 21 #include "chrome/browser/signin/token_service_factory.h" |
23 #include "chrome/browser/sync/about_sync_util.h" | 22 #include "chrome/browser/sync/about_sync_util.h" |
24 #include "chrome/browser/sync/profile_sync_service.h" | 23 #include "chrome/browser/sync/profile_sync_service.h" |
25 #include "chrome/browser/sync/profile_sync_service_factory.h" | 24 #include "chrome/browser/sync/profile_sync_service_factory.h" |
26 #include "chrome/browser/sync/sync_prefs.h" | 25 #include "chrome/browser/sync/sync_prefs.h" |
27 #include "chrome/browser/sync/sync_ui_util.h" | 26 #include "chrome/browser/sync/sync_ui_util.h" |
28 #include "chrome/common/chrome_notification_types.h" | 27 #include "chrome/common/chrome_notification_types.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 env, weak_java_profile_sync_service_.get(env).obj()); | 162 env, weak_java_profile_sync_service_.get(env).obj()); |
164 } | 163 } |
165 | 164 |
166 void ProfileSyncServiceAndroid::TokenAvailable( | 165 void ProfileSyncServiceAndroid::TokenAvailable( |
167 JNIEnv* env, jobject, jstring username, jstring auth_token) { | 166 JNIEnv* env, jobject, jstring username, jstring auth_token) { |
168 std::string token = ConvertJavaStringToUTF8(env, auth_token); | 167 std::string token = ConvertJavaStringToUTF8(env, auth_token); |
169 TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( | 168 TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( |
170 GaiaConstants::kSyncService, token); | 169 GaiaConstants::kSyncService, token); |
171 } | 170 } |
172 | 171 |
173 void ProfileSyncServiceAndroid::InvalidateOAuth2Token( | |
174 const std::string& scope, const std::string& invalid_token) { | |
175 JNIEnv* env = AttachCurrentThread(); | |
176 ScopedJavaLocalRef<jstring> j_scope = | |
177 ConvertUTF8ToJavaString(env, scope); | |
178 ScopedJavaLocalRef<jstring> j_invalid_token = | |
179 ConvertUTF8ToJavaString(env, invalid_token); | |
180 Java_ProfileSyncService_invalidateOAuth2AuthToken( | |
181 env, weak_java_profile_sync_service_.get(env).obj(), | |
182 j_scope.obj(), | |
183 j_invalid_token.obj()); | |
184 } | |
185 | |
186 void ProfileSyncServiceAndroid::FetchOAuth2Token( | |
187 const std::string& scope, const FetchOAuth2TokenCallback& callback) { | |
188 const std::string& sync_username = | |
189 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); | |
190 | |
191 JNIEnv* env = AttachCurrentThread(); | |
192 ScopedJavaLocalRef<jstring> j_sync_username = | |
193 ConvertUTF8ToJavaString(env, sync_username); | |
194 ScopedJavaLocalRef<jstring> j_scope = | |
195 ConvertUTF8ToJavaString(env, scope); | |
196 | |
197 // Allocate a copy of the callback on the heap, because the callback | |
198 // needs to be passed through JNI as an int. | |
199 // It will be passed back to OAuth2TokenFetched(), where it will be freed. | |
200 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | |
201 new FetchOAuth2TokenCallback(callback)); | |
202 | |
203 // Call into Java to get a new token. | |
204 Java_ProfileSyncService_getOAuth2AuthToken( | |
205 env, weak_java_profile_sync_service_.get(env).obj(), | |
206 j_sync_username.obj(), | |
207 j_scope.obj(), | |
208 reinterpret_cast<int>(heap_callback.release())); | |
209 } | |
210 | |
211 void ProfileSyncServiceAndroid::OAuth2TokenFetched( | |
212 JNIEnv* env, jobject, int callback, jstring auth_token, jboolean result) { | |
213 std::string token = ConvertJavaStringToUTF8(env, auth_token); | |
214 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | |
215 reinterpret_cast<FetchOAuth2TokenCallback*>(callback)); | |
216 GoogleServiceAuthError err(result ? | |
217 GoogleServiceAuthError::NONE : | |
218 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | |
219 heap_callback->Run(err, token, base::Time()); | |
220 } | |
221 | |
222 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { | 172 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { |
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
224 // Don't need to do anything if we're already enabled. | 174 // Don't need to do anything if we're already enabled. |
225 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | 175 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); |
226 if (prefs.IsStartSuppressed()) | 176 if (prefs.IsStartSuppressed()) |
227 sync_service_->UnsuppressAndStart(); | 177 sync_service_->UnsuppressAndStart(); |
228 else | 178 else |
229 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; | 179 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; |
230 } | 180 } |
231 | 181 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, | 527 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, |
578 jobject obj, | 528 jobject obj, |
579 jstring objectId, | 529 jstring objectId, |
580 jlong version, | 530 jlong version, |
581 jstring state) { | 531 jstring state) { |
582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
583 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, | 533 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, |
584 ConvertJavaStringToUTF8(env, state)); | 534 ConvertJavaStringToUTF8(env, state)); |
585 } | 535 } |
586 | 536 |
587 // static | |
588 ProfileSyncServiceAndroid* | |
589 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { | |
590 return reinterpret_cast<ProfileSyncServiceAndroid*>( | |
591 Java_ProfileSyncService_getProfileSyncServiceAndroid( | |
592 AttachCurrentThread(), base::android::GetApplicationContext())); | |
593 } | |
594 | |
595 static int Init(JNIEnv* env, jobject obj) { | 537 static int Init(JNIEnv* env, jobject obj) { |
596 ProfileSyncServiceAndroid* profile_sync_service_android = | 538 ProfileSyncServiceAndroid* profile_sync_service_android = |
597 new ProfileSyncServiceAndroid(env, obj); | 539 new ProfileSyncServiceAndroid(env, obj); |
598 profile_sync_service_android->Init(); | 540 profile_sync_service_android->Init(); |
599 return reinterpret_cast<jint>(profile_sync_service_android); | 541 return reinterpret_cast<jint>(profile_sync_service_android); |
600 } | 542 } |
601 | 543 |
602 // static | 544 // static |
603 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { | 545 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
604 return RegisterNativesImpl(env); | 546 return RegisterNativesImpl(env); |
605 } | 547 } |
OLD | NEW |