Chromium Code Reviews| 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" | |
| 18 #include "chrome/browser/signin/signin_manager.h" | 19 #include "chrome/browser/signin/signin_manager.h" |
| 19 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
| 20 #include "chrome/browser/signin/token_service.h" | 21 #include "chrome/browser/signin/token_service.h" |
| 21 #include "chrome/browser/signin/token_service_factory.h" | 22 #include "chrome/browser/signin/token_service_factory.h" |
| 22 #include "chrome/browser/sync/about_sync_util.h" | 23 #include "chrome/browser/sync/about_sync_util.h" |
| 23 #include "chrome/browser/sync/profile_sync_service.h" | 24 #include "chrome/browser/sync/profile_sync_service.h" |
| 24 #include "chrome/browser/sync/profile_sync_service_factory.h" | 25 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 25 #include "chrome/browser/sync/sync_prefs.h" | 26 #include "chrome/browser/sync/sync_prefs.h" |
| 26 #include "chrome/browser/sync/sync_ui_util.h" | 27 #include "chrome/browser/sync/sync_ui_util.h" |
| 27 #include "chrome/common/chrome_notification_types.h" | 28 #include "chrome/common/chrome_notification_types.h" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 env, weak_java_profile_sync_service_.get(env).obj()); | 163 env, weak_java_profile_sync_service_.get(env).obj()); |
| 163 } | 164 } |
| 164 | 165 |
| 165 void ProfileSyncServiceAndroid::TokenAvailable( | 166 void ProfileSyncServiceAndroid::TokenAvailable( |
| 166 JNIEnv* env, jobject, jstring username, jstring auth_token) { | 167 JNIEnv* env, jobject, jstring username, jstring auth_token) { |
| 167 std::string token = ConvertJavaStringToUTF8(env, auth_token); | 168 std::string token = ConvertJavaStringToUTF8(env, auth_token); |
| 168 TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( | 169 TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( |
| 169 GaiaConstants::kSyncService, token); | 170 GaiaConstants::kSyncService, token); |
| 170 } | 171 } |
| 171 | 172 |
| 173 void ProfileSyncServiceAndroid::FetchOAuth2Token( | |
| 174 const std::string& scope, const std::string& invalid_auth_token, | |
| 175 const FetchOAuth2TokenCallback& callback) { | |
| 176 const std::string& sync_username = | |
| 177 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); | |
| 178 | |
| 179 // Call into java to invalidate the current token and get a new one. | |
| 180 JNIEnv* env = AttachCurrentThread(); | |
| 181 ScopedJavaLocalRef<jstring> j_sync_username = | |
| 182 ConvertUTF8ToJavaString(env, sync_username); | |
| 183 ScopedJavaLocalRef<jstring> j_scope = | |
| 184 ConvertUTF8ToJavaString(env, scope); | |
| 185 ScopedJavaLocalRef<jstring> j_invalid_auth_token; | |
| 186 | |
| 187 if (!invalid_auth_token.empty()) { | |
| 188 j_invalid_auth_token.Reset( | |
| 189 ConvertUTF8ToJavaString(env, invalid_auth_token)); | |
| 190 } | |
| 191 | |
| 192 // Allocate a copy of the callback on the heap, because we need a pointer to | |
|
Nicolas Zea
2013/03/25 18:13:18
nit: "we" here is ambiguous. Perhaps just "because
Patrick Dubroy
2013/03/25 18:52:55
Done.
| |
| 193 // hold on to. | |
| 194 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | |
| 195 new FetchOAuth2TokenCallback(callback)); | |
| 196 | |
| 197 Java_ProfileSyncService_getOAuth2AuthToken( | |
| 198 env, weak_java_profile_sync_service_.get(env).obj(), | |
| 199 j_sync_username.obj(), | |
| 200 j_scope.obj(), | |
| 201 j_invalid_auth_token.obj(), | |
| 202 reinterpret_cast<int>(heap_callback.release())); | |
| 203 } | |
| 204 | |
| 205 void ProfileSyncServiceAndroid::OAuth2TokenFetched( | |
| 206 JNIEnv* env, jobject, int callback, jstring auth_token, jboolean result) { | |
| 207 std::string token = ConvertJavaStringToUTF8(env, auth_token); | |
| 208 scoped_ptr<FetchOAuth2TokenCallback> heap_callback( | |
| 209 reinterpret_cast<FetchOAuth2TokenCallback*>(callback)); | |
| 210 GoogleServiceAuthError err(result ? | |
| 211 GoogleServiceAuthError::NONE : | |
| 212 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | |
| 213 heap_callback->Run(err, token, base::Time()); | |
| 214 } | |
| 215 | |
| 172 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { | 216 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 174 // Don't need to do anything if we're already enabled. | 218 // Don't need to do anything if we're already enabled. |
| 175 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | 219 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); |
| 176 if (prefs.IsStartSuppressed()) | 220 if (prefs.IsStartSuppressed()) |
| 177 sync_service_->UnsuppressAndStart(); | 221 sync_service_->UnsuppressAndStart(); |
| 178 else | 222 else |
| 179 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; | 223 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; |
| 180 } | 224 } |
| 181 | 225 |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 527 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, | 571 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, |
| 528 jobject obj, | 572 jobject obj, |
| 529 jstring objectId, | 573 jstring objectId, |
| 530 jlong version, | 574 jlong version, |
| 531 jstring state) { | 575 jstring state) { |
| 532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 533 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, | 577 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, |
| 534 ConvertJavaStringToUTF8(env, state)); | 578 ConvertJavaStringToUTF8(env, state)); |
| 535 } | 579 } |
| 536 | 580 |
| 581 // static | |
| 582 ProfileSyncServiceAndroid* | |
| 583 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { | |
| 584 return reinterpret_cast<ProfileSyncServiceAndroid*>( | |
| 585 Java_ProfileSyncService_getProfileSyncServiceAndroid( | |
| 586 AttachCurrentThread(), base::android::GetApplicationContext())); | |
| 587 } | |
| 588 | |
| 537 static int Init(JNIEnv* env, jobject obj) { | 589 static int Init(JNIEnv* env, jobject obj) { |
| 538 ProfileSyncServiceAndroid* profile_sync_service_android = | 590 ProfileSyncServiceAndroid* profile_sync_service_android = |
| 539 new ProfileSyncServiceAndroid(env, obj); | 591 new ProfileSyncServiceAndroid(env, obj); |
| 540 profile_sync_service_android->Init(); | 592 profile_sync_service_android->Init(); |
| 541 return reinterpret_cast<jint>(profile_sync_service_android); | 593 return reinterpret_cast<jint>(profile_sync_service_android); |
| 542 } | 594 } |
| 543 | 595 |
| 544 // static | 596 // static |
| 545 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { | 597 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
| 546 return RegisterNativesImpl(env); | 598 return RegisterNativesImpl(env); |
| 547 } | 599 } |
| OLD | NEW |