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::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 |
172 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { | 222 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
174 // Don't need to do anything if we're already enabled. | 224 // Don't need to do anything if we're already enabled. |
175 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | 225 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); |
176 if (prefs.IsStartSuppressed()) | 226 if (prefs.IsStartSuppressed()) |
177 sync_service_->UnsuppressAndStart(); | 227 sync_service_->UnsuppressAndStart(); |
178 else | 228 else |
179 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; | 229 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; |
180 } | 230 } |
181 | 231 |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, | 577 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, |
528 jobject obj, | 578 jobject obj, |
529 jstring objectId, | 579 jstring objectId, |
530 jlong version, | 580 jlong version, |
531 jstring state) { | 581 jstring state) { |
532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
533 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, | 583 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, |
534 ConvertJavaStringToUTF8(env, state)); | 584 ConvertJavaStringToUTF8(env, state)); |
535 } | 585 } |
536 | 586 |
| 587 // static |
| 588 ProfileSyncServiceAndroid* |
| 589 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
| 590 return reinterpret_cast<ProfileSyncServiceAndroid*>( |
| 591 Java_ProfileSyncService_getProfileSyncServiceAndroid( |
| 592 AttachCurrentThread(), base::android::GetApplicationContext())); |
| 593 } |
| 594 |
537 static int Init(JNIEnv* env, jobject obj) { | 595 static int Init(JNIEnv* env, jobject obj) { |
538 ProfileSyncServiceAndroid* profile_sync_service_android = | 596 ProfileSyncServiceAndroid* profile_sync_service_android = |
539 new ProfileSyncServiceAndroid(env, obj); | 597 new ProfileSyncServiceAndroid(env, obj); |
540 profile_sync_service_android->Init(); | 598 profile_sync_service_android->Init(); |
541 return reinterpret_cast<jint>(profile_sync_service_android); | 599 return reinterpret_cast<jint>(profile_sync_service_android); |
542 } | 600 } |
543 | 601 |
544 // static | 602 // static |
545 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { | 603 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
546 return RegisterNativesImpl(env); | 604 return RegisterNativesImpl(env); |
547 } | 605 } |
OLD | NEW |