Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/profile_sync_service_android.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/i18n/time_formatting.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/prefs/pref_service.h" | |
| 15 #include "base/time.h" | |
| 16 #include "chrome/browser/browser_process.h" | |
| 17 #include "chrome/browser/prefs/pref_registry_syncable.h" | |
| 18 #include "chrome/browser/profiles/profile_manager.h" | |
| 19 #include "chrome/browser/signin/signin_manager.h" | |
| 20 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 21 #include "chrome/browser/signin/token_service.h" | |
| 22 #include "chrome/browser/signin/token_service_factory.h" | |
| 23 #include "chrome/browser/sync/about_sync_util.h" | |
| 24 #include "chrome/browser/sync/profile_sync_service.h" | |
| 25 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 26 #include "chrome/browser/sync/sync_ui_util.h" | |
| 27 #include "chrome/common/chrome_notification_types.h" | |
| 28 #include "chrome/common/pref_names.h" | |
| 29 #include "content/public/browser/notification_service.h" | |
| 30 #include "google/cacheinvalidation/types.pb.h" | |
| 31 #include "google_apis/gaia/gaia_constants.h" | |
| 32 #include "google_apis/gaia/google_service_auth_error.h" | |
| 33 #include "grit/generated_resources.h" | |
| 34 #include "jni/ProfileSyncService_jni.h" | |
| 35 #include "sync/internal_api/public/base/model_type_invalidation_map.h" | |
| 36 #include "sync/internal_api/public/read_transaction.h" | |
| 37 #include "ui/base/l10n/l10n_util.h" | |
| 38 | |
| 39 using base::android::AttachCurrentThread; | |
| 40 using base::android::CheckException; | |
| 41 using base::android::ConvertJavaStringToUTF8; | |
| 42 using base::android::ConvertUTF8ToJavaString; | |
| 43 using base::android::ScopedJavaLocalRef; | |
| 44 using content::BrowserThread; | |
| 45 | |
| 46 namespace { | |
| 47 const char kSyncDisabledStatus[] = "OFFLINE_DISABLED"; | |
| 48 | |
| 49 // The ProfileSyncService#mNativeProfileSyncServiceAndroid field. | |
| 50 ProfileSyncServiceAndroid* g_profile_sync_service_android = NULL; | |
| 51 } // namespace | |
| 52 | |
| 53 ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj) | |
| 54 : weak_java_profile_sync_service_(env, obj) { | |
| 55 profile_ = NULL; | |
| 56 sync_service_ = NULL; | |
| 57 if (g_browser_process == NULL || | |
| 58 g_browser_process->profile_manager() == NULL) { | |
| 59 LOG(ERROR) << "Browser process or profile manager not initialized"; | |
|
Andrew T Wilson (Slow)
2013/02/25 16:23:35
I suspect all these LOG(ERROR) these should be NOT
nyquist
2013/02/27 05:19:16
Done.
| |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 profile_ = g_browser_process->profile_manager()->GetDefaultProfile(); | |
| 64 if (profile_ == NULL) { | |
| 65 LOG(ERROR) << "Sync Init: Profile not found."; | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 sync_service_ = | |
| 70 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); | |
| 71 AddObserver(); | |
| 72 } | |
| 73 | |
| 74 void ProfileSyncServiceAndroid::AddObserver() { | |
| 75 if (sync_service_ == NULL) { | |
|
Andrew T Wilson (Slow)
2013/02/25 16:23:35
These checks are odd. I think they should be DCHEC
nyquist
2013/02/27 05:19:16
Done.
| |
| 76 LOG(ERROR) << "Sync service not found."; | |
| 77 return; | |
| 78 } | |
| 79 if (!sync_service_->HasObserver(this)) { | |
| 80 sync_service_->AddObserver(this); | |
| 81 } | |
| 82 | |
| 83 std::string signed_in_username = | |
| 84 profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | |
| 85 if (!signed_in_username.empty()) { | |
| 86 // If the user is logged in, see if he has a valid token - if not, fetch | |
| 87 // a new one. | |
| 88 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | |
| 89 if (!token_service->HasTokenForService(GaiaConstants::kSyncService) || | |
| 90 (sync_service_->GetAuthError().state() == | |
| 91 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)) { | |
| 92 VLOG(2) << "Trying to update token for user " << signed_in_username; | |
|
Yaron
2013/02/23 01:38:49
DVLOG?
nyquist
2013/02/27 05:19:16
Done.
| |
| 93 InvalidateAuthToken(); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void ProfileSyncServiceAndroid::RemoveObserver() { | |
| 99 if (sync_service_ == NULL) { | |
| 100 LOG(ERROR) << "Sync service not found."; | |
|
Yaron
2013/02/23 01:38:49
Redundant error: remove
nyquist
2013/02/27 05:19:16
Done.
| |
| 101 return; | |
| 102 } | |
| 103 if (sync_service_->HasObserver(this)) { | |
| 104 sync_service_->RemoveObserver(this); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() { | |
| 109 RemoveObserver(); | |
| 110 g_profile_sync_service_android = NULL; | |
| 111 } | |
| 112 | |
| 113 void ProfileSyncServiceAndroid::Destroy(JNIEnv* env, jobject obj) { | |
| 114 delete this; | |
| 115 } | |
| 116 | |
| 117 // Must be called on the UI thread. | |
| 118 void ProfileSyncServiceAndroid::SendNudgeNotification( | |
| 119 const std::string& str_object_id, | |
| 120 int64 version, | |
| 121 const std::string& state) { | |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 123 | |
| 124 // TODO(nileshagrawal): Merge this with ChromeInvalidationClient::Invalidate. | |
| 125 // Construct the ModelTypeStateMap and send it over with the notification. | |
| 126 syncer::ModelType model_type; | |
| 127 if (!syncer::NotificationTypeToRealModelType(str_object_id, &model_type)) { | |
| 128 VLOG(1) << "Could not get invalidation model type; " | |
| 129 << "Sending notification with empty state map."; | |
| 130 syncer::ModelTypeInvalidationMap model_types_with_states; | |
| 131 content::NotificationService::current()->Notify( | |
| 132 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | |
| 133 content::Source<Profile>(profile_), | |
| 134 content::Details<const syncer::ModelTypeInvalidationMap>( | |
| 135 &model_types_with_states)); | |
| 136 return; | |
| 137 } | |
| 138 | |
| 139 if (version != ipc::invalidation::Constants::UNKNOWN) { | |
| 140 std::map<syncer::ModelType, int64>::const_iterator it = | |
| 141 max_invalidation_versions_.find(model_type); | |
| 142 if ((it != max_invalidation_versions_.end()) && | |
| 143 (version <= it->second)) { | |
| 144 VLOG(1) << "Dropping redundant invalidation with version " << version; | |
| 145 return; | |
| 146 } | |
| 147 max_invalidation_versions_[model_type] = version; | |
| 148 } | |
| 149 | |
| 150 syncer::ModelTypeSet types; | |
| 151 types.Put(model_type); | |
| 152 syncer::ModelTypeInvalidationMap model_types_with_states = | |
| 153 syncer::ModelTypeSetToInvalidationMap(types, state); | |
| 154 | |
| 155 content::NotificationService::current()->Notify( | |
| 156 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | |
| 157 content::Source<Profile>(profile_), | |
| 158 content::Details<const syncer::ModelTypeInvalidationMap>( | |
| 159 &model_types_with_states)); | |
| 160 } | |
| 161 | |
| 162 | |
| 163 void ProfileSyncServiceAndroid::OnStateChanged() { | |
| 164 // Check for auth errors. | |
| 165 const GoogleServiceAuthError& auth_error = sync_service_->GetAuthError(); | |
| 166 if (auth_error.state() == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS) { | |
| 167 VLOG(2) << "Updating auth token."; | |
| 168 InvalidateAuthToken(); | |
| 169 } | |
| 170 | |
| 171 // Notify the java world that our sync state has changed. | |
| 172 JNIEnv* env = AttachCurrentThread(); | |
| 173 Java_ProfileSyncService_syncStateChanged( | |
| 174 env, weak_java_profile_sync_service_.get(env).obj()); | |
| 175 } | |
| 176 | |
| 177 void ProfileSyncServiceAndroid::TokenAvailable( | |
| 178 JNIEnv* env, jobject, jstring username, jstring auth_token) { | |
| 179 std::string token = ConvertJavaStringToUTF8(env, auth_token); | |
| 180 TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( | |
| 181 GaiaConstants::kSyncService, token); | |
| 182 } | |
| 183 | |
| 184 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { | |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 186 DCHECK(sync_service_); | |
| 187 // Don't need to do anything if we're already enabled. | |
| 188 if (profile_->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)) | |
| 189 sync_service_->UnsuppressAndStart(); | |
| 190 else | |
| 191 VLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; | |
|
Andrew T Wilson (Slow)
2013/02/25 16:23:35
Change all VLOGs to DVLOG()
nyquist
2013/02/27 05:19:16
Done.
| |
| 192 } | |
| 193 | |
| 194 void ProfileSyncServiceAndroid::DisableSync(JNIEnv* env, jobject) { | |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 196 DCHECK(sync_service_); | |
| 197 sync_service_->StopAndSuppress(); | |
| 198 } | |
| 199 | |
| 200 void ProfileSyncServiceAndroid::SignInSync( | |
| 201 JNIEnv* env, jobject, jstring username, jstring auth_token) { | |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 203 DCHECK(sync_service_); | |
| 204 // Just return if sync already has everything it needs to start up (sync | |
| 205 // should start up automatically as long as it has credentials). This can | |
| 206 // happen normally if (for example) the user closes and reopens the sync | |
| 207 // settings window quickly during initial startup. | |
| 208 if (sync_service_->IsSyncEnabledAndLoggedIn() && | |
| 209 sync_service_->IsSyncTokenAvailable() && | |
| 210 sync_service_->HasSyncSetupCompleted()) | |
| 211 return; | |
| 212 | |
| 213 if (!sync_service_->IsSyncEnabledAndLoggedIn() || | |
| 214 !sync_service_->IsSyncTokenAvailable()) { | |
| 215 // Set the currently-signed-in username, fetch an auth token if necessary, | |
| 216 // and enable sync. | |
| 217 std::string name = ConvertJavaStringToUTF8(env, username); | |
| 218 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, name); | |
|
Andrew T Wilson (Slow)
2013/02/25 16:23:35
Should change our uses of kGoogleServicesUsername
nyquist
2013/02/27 05:19:16
Done.
Yaron
2013/03/12 02:26:50
Is it possible that this is causing crbug.com/1815
Yaron
2013/03/12 02:31:14
It looks like it's set down in UnsuppressAndStart
| |
| 219 std::string token = ConvertJavaStringToUTF8(env, auth_token); | |
| 220 if (token.empty()) { | |
| 221 // No credentials passed in - request an auth token. | |
| 222 // If fetching the auth token is successful, this will cause | |
| 223 // ProfileSyncService to start sync when it receives | |
| 224 // NOTIFICATION_TOKEN_AVAILABLE. | |
| 225 VLOG(2) << "Fetching auth token for " << name; | |
| 226 InvalidateAuthToken(); | |
| 227 } else { | |
| 228 // OnIssueAuthTokenSuccess will send out a notification to the sync | |
| 229 // service that will cause the sync backend to initialize. | |
| 230 TokenService* token_service = | |
| 231 TokenServiceFactory::GetForProfile(profile_); | |
| 232 token_service->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, | |
| 233 token); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 // Enable sync (if we don't have credentials yet, this will enable sync but | |
| 238 // will not start it up - sync will start once credentials arrive). | |
| 239 sync_service_->UnsuppressAndStart(); | |
| 240 } | |
| 241 | |
| 242 void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) { | |
| 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 244 DCHECK(sync_service_); | |
| 245 DCHECK(profile_); | |
| 246 sync_service_->DisableForUser(); | |
| 247 | |
| 248 // Clear the tokens. | |
| 249 SigninManagerFactory::GetForProfile(profile_)->SignOut(); | |
| 250 | |
| 251 // Need to clear suppress start flag manually | |
| 252 PrefService* pref_service = profile_->GetPrefs(); | |
|
Andrew T Wilson (Slow)
2013/02/25 16:23:35
We should use SyncPrefs for accessing any sync pre
nyquist
2013/02/27 05:19:16
Done.
| |
| 253 pref_service->SetBoolean(prefs::kSyncSuppressStart, false); | |
| 254 } | |
| 255 | |
| 256 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary( | |
| 257 JNIEnv* env, jobject) { | |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 259 DCHECK(sync_service_); | |
| 260 DCHECK(profile_); | |
| 261 std::string status(sync_service_->QuerySyncStatusSummary()); | |
| 262 return ConvertUTF8ToJavaString(env, status); | |
| 263 } | |
| 264 | |
| 265 jboolean ProfileSyncServiceAndroid::SetSyncSessionsId( | |
| 266 JNIEnv* env, jobject obj, jstring tag) { | |
| 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 268 DCHECK(profile_); | |
| 269 PrefService* pref_service = profile_->GetPrefs(); | |
| 270 if (!pref_service) { | |
| 271 return false; | |
| 272 } | |
| 273 if (pref_service->FindPreference(prefs::kSyncSessionsGUID) == NULL) { | |
| 274 static_cast<PrefRegistrySyncable*>( | |
| 275 pref_service->DeprecatedGetPrefRegistry())->RegisterStringPref( | |
| 276 prefs::kSyncSessionsGUID, | |
| 277 std::string(), | |
| 278 PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 279 } | |
| 280 std::string machine_tag = ConvertJavaStringToUTF8(env, tag); | |
| 281 pref_service->SetString(prefs::kSyncSessionsGUID, machine_tag); | |
| 282 return true; | |
| 283 } | |
| 284 | |
| 285 jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) { | |
| 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 287 DCHECK(sync_service_); | |
| 288 return sync_service_->GetAuthError().state(); | |
| 289 } | |
| 290 | |
| 291 jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled( | |
| 292 JNIEnv* env, jobject) { | |
| 293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 294 DCHECK(sync_service_); | |
| 295 return sync_service_->EncryptEverythingEnabled(); | |
| 296 } | |
| 297 | |
| 298 jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) { | |
| 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 300 DCHECK(sync_service_); | |
| 301 return sync_service_->sync_initialized(); | |
| 302 } | |
| 303 | |
| 304 jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress( | |
| 305 JNIEnv* env, jobject) { | |
| 306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 307 DCHECK(sync_service_); | |
| 308 return sync_service_->FirstSetupInProgress(); | |
| 309 } | |
| 310 | |
| 311 jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) { | |
| 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 313 DCHECK(sync_service_); | |
| 314 return sync_service_->IsPassphraseRequired(); | |
| 315 } | |
| 316 | |
| 317 jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption( | |
| 318 JNIEnv* env, jobject obj) { | |
| 319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 320 DCHECK(sync_service_); | |
| 321 // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for | |
| 322 // a passphrase if cryptographer has any pending keys. | |
| 323 if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) { | |
| 324 return !IsCryptographerReady(env, obj); | |
| 325 } | |
| 326 if (sync_service_->IsPassphraseRequiredForDecryption()) { | |
| 327 // Passwords datatype should never prompt for a passphrase, except when | |
| 328 // user is using a custom passphrase. Do not prompt for a passphrase if | |
| 329 // passwords are the only encrypted datatype. This prevents a temporary | |
| 330 // notification for passphrase when PSS has not completed configuring | |
| 331 // DataTypeManager, after configuration password datatype shall be disabled. | |
| 332 const syncer::ModelTypeSet encrypted_types = | |
| 333 sync_service_->GetEncryptedDataTypes(); | |
| 334 const bool are_passwords_the_only_encrypted_type = | |
| 335 encrypted_types.Has(syncer::PASSWORDS) && encrypted_types.Size() == 1 && | |
| 336 !sync_service_->ShouldEnablePasswordSyncForAndroid(); | |
| 337 return !are_passwords_the_only_encrypted_type; | |
| 338 } | |
| 339 return false; | |
| 340 } | |
| 341 | |
| 342 jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForExternalType( | |
| 343 JNIEnv* env, jobject) { | |
| 344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 345 DCHECK(sync_service_); | |
| 346 return | |
| 347 sync_service_->passphrase_required_reason() == syncer::REASON_DECRYPTION; | |
| 348 } | |
| 349 | |
| 350 jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase( | |
| 351 JNIEnv* env, jobject) { | |
| 352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 353 DCHECK(sync_service_); | |
| 354 return sync_service_->IsUsingSecondaryPassphrase(); | |
| 355 } | |
| 356 | |
| 357 jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase( | |
| 358 JNIEnv* env, jobject obj, jstring passphrase) { | |
| 359 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 360 DCHECK(sync_service_); | |
| 361 std::string key = ConvertJavaStringToUTF8(env, passphrase); | |
| 362 return sync_service_->SetDecryptionPassphrase(key); | |
| 363 } | |
| 364 | |
| 365 void ProfileSyncServiceAndroid::SetEncryptionPassphrase( | |
| 366 JNIEnv* env, jobject obj, jstring passphrase, jboolean is_gaia) { | |
| 367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 368 DCHECK(sync_service_); | |
| 369 std::string key = ConvertJavaStringToUTF8(env, passphrase); | |
| 370 sync_service_->SetEncryptionPassphrase( | |
| 371 key, | |
| 372 is_gaia ? ProfileSyncService::IMPLICIT : ProfileSyncService::EXPLICIT); | |
| 373 } | |
| 374 | |
| 375 jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) { | |
| 376 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | |
| 377 return sync_service_->IsCryptographerReady(&trans); | |
| 378 } | |
| 379 | |
| 380 jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) { | |
| 381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 382 DCHECK(sync_service_); | |
| 383 return sync_service_->GetPassphraseType(); | |
| 384 } | |
| 385 | |
| 386 jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime( | |
| 387 JNIEnv* env, jobject) { | |
| 388 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 389 DCHECK(sync_service_); | |
| 390 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); | |
| 391 return !passphrase_time.is_null(); | |
| 392 } | |
| 393 | |
| 394 ScopedJavaLocalRef<jstring> | |
| 395 ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText( | |
| 396 JNIEnv* env, jobject) { | |
| 397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 398 DCHECK(sync_service_); | |
| 399 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); | |
| 400 string16 passphrase_time_str = base::TimeFormatShortDate(passphrase_time); | |
| 401 return base::android::ConvertUTF16ToJavaString(env, | |
| 402 l10n_util::GetStringFUTF16( | |
| 403 IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE, | |
| 404 passphrase_time_str)); | |
| 405 } | |
| 406 | |
| 407 ScopedJavaLocalRef<jstring> | |
| 408 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText( | |
| 409 JNIEnv* env, jobject) { | |
| 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 411 DCHECK(sync_service_); | |
| 412 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); | |
| 413 string16 passphrase_time_str = base::TimeFormatShortDate(passphrase_time); | |
| 414 return base::android::ConvertUTF16ToJavaString(env, | |
| 415 l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE, | |
| 416 passphrase_time_str)); | |
| 417 } | |
| 418 | |
| 419 static jstring GetSyncEnterCustomPassphraseBodyText(JNIEnv* env, jclass clazz) { | |
| 420 return ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( | |
| 421 env, clazz).Release(); | |
| 422 } | |
| 423 | |
| 424 ScopedJavaLocalRef<jstring> | |
| 425 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( | |
| 426 JNIEnv* env, jclass) { | |
| 427 return ConvertUTF8ToJavaString( | |
| 428 env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); | |
| 429 } | |
| 430 | |
| 431 jboolean ProfileSyncServiceAndroid::IsMigrated(JNIEnv* env, jobject) { | |
| 432 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 433 DCHECK(sync_service_); | |
| 434 syncer::SyncStatus status; | |
| 435 bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); | |
| 436 return is_status_valid && !status.keystore_migration_time.is_null(); | |
| 437 } | |
| 438 | |
| 439 void ProfileSyncServiceAndroid::SetPreferredDataTypes( | |
| 440 JNIEnv* env, jobject obj, | |
| 441 jboolean sync_everything, | |
| 442 jboolean sync_autofill, | |
| 443 jboolean sync_bookmarks, | |
| 444 jboolean sync_passwords, | |
| 445 jboolean sync_sessions, | |
| 446 jboolean sync_typed_urls) { | |
| 447 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 448 DCHECK(sync_service_); | |
| 449 syncer::ModelTypeSet types; | |
| 450 if (sync_autofill) | |
| 451 types.Put(syncer::AUTOFILL); | |
| 452 if (sync_bookmarks) | |
| 453 types.Put(syncer::BOOKMARKS); | |
| 454 if (sync_passwords) | |
| 455 types.Put(syncer::PASSWORDS); | |
| 456 if (sync_sessions) | |
| 457 types.Put(syncer::SESSIONS); | |
| 458 if (sync_typed_urls) | |
| 459 types.Put(syncer::TYPED_URLS); | |
| 460 sync_service_->OnUserChoseDatatypes(sync_everything, types); | |
| 461 } | |
| 462 | |
| 463 void ProfileSyncServiceAndroid::SetSetupInProgress( | |
| 464 JNIEnv* env, jobject obj, jboolean in_progress) { | |
| 465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 466 DCHECK(sync_service_); | |
| 467 sync_service_->SetSetupInProgress(in_progress); | |
| 468 } | |
| 469 | |
| 470 void ProfileSyncServiceAndroid::SetSyncSetupCompleted( | |
| 471 JNIEnv* env, jobject obj) { | |
| 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 473 DCHECK(sync_service_); | |
| 474 sync_service_->SetSyncSetupCompleted(); | |
| 475 } | |
| 476 | |
| 477 jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted( | |
| 478 JNIEnv* env, jobject obj) { | |
| 479 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 480 DCHECK(sync_service_); | |
| 481 return sync_service_->HasSyncSetupCompleted(); | |
| 482 } | |
| 483 | |
| 484 void ProfileSyncServiceAndroid::EnableEncryptEverything( | |
| 485 JNIEnv* env, jobject obj) { | |
| 486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 487 DCHECK(sync_service_); | |
| 488 sync_service_->EnableEncryptEverything(); | |
| 489 } | |
| 490 | |
| 491 jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced( | |
| 492 JNIEnv* env, jobject) { | |
| 493 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 494 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | |
| 495 return prefs.HasKeepEverythingSynced(); | |
| 496 } | |
| 497 | |
| 498 jboolean ProfileSyncServiceAndroid::IsAutofillSyncEnabled( | |
| 499 JNIEnv* env, jobject obj) { | |
| 500 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 501 DCHECK(sync_service_); | |
| 502 return HasKeepEverythingSynced(env, obj) || | |
| 503 sync_service_->GetPreferredDataTypes().Has(syncer::AUTOFILL); | |
| 504 } | |
| 505 | |
| 506 jboolean ProfileSyncServiceAndroid::IsBookmarkSyncEnabled( | |
| 507 JNIEnv* env, jobject obj) { | |
| 508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 509 DCHECK(sync_service_); | |
| 510 return HasKeepEverythingSynced(env, obj) || | |
| 511 sync_service_->GetPreferredDataTypes().Has(syncer::BOOKMARKS); | |
| 512 } | |
| 513 | |
| 514 jboolean ProfileSyncServiceAndroid::IsPasswordSyncEnabled( | |
| 515 JNIEnv* env, jobject obj) { | |
| 516 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 517 DCHECK(sync_service_); | |
| 518 return HasKeepEverythingSynced(env, obj) || | |
| 519 sync_service_->GetPreferredDataTypes().Has(syncer::PASSWORDS); | |
| 520 } | |
| 521 | |
| 522 jboolean ProfileSyncServiceAndroid::IsTypedUrlSyncEnabled( | |
| 523 JNIEnv* env, jobject obj) { | |
| 524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 525 DCHECK(sync_service_); | |
| 526 return HasKeepEverythingSynced(env, obj) || | |
| 527 sync_service_->GetPreferredDataTypes().Has(syncer::TYPED_URLS); | |
| 528 } | |
| 529 | |
| 530 jboolean ProfileSyncServiceAndroid::IsSessionSyncEnabled( | |
| 531 JNIEnv* env, jobject obj) { | |
| 532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 533 DCHECK(sync_service_); | |
| 534 return HasKeepEverythingSynced(env, obj) || | |
| 535 sync_service_->GetPreferredDataTypes().Has(syncer::SESSIONS); | |
| 536 } | |
| 537 | |
| 538 jboolean ProfileSyncServiceAndroid::HasUnrecoverableError( | |
| 539 JNIEnv* env, jobject) { | |
| 540 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 541 DCHECK(sync_service_); | |
| 542 return sync_service_->HasUnrecoverableError(); | |
| 543 } | |
| 544 | |
| 545 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( | |
| 546 JNIEnv* env, jobject) { | |
| 547 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 548 DCHECK(sync_service_); | |
| 549 | |
| 550 scoped_ptr<DictionaryValue> about_info = | |
| 551 sync_ui_util::ConstructAboutInformation(sync_service_); | |
| 552 std::string about_info_json; | |
| 553 base::JSONWriter::Write(about_info.get(), &about_info_json); | |
| 554 | |
| 555 return ConvertUTF8ToJavaString(env, about_info_json); | |
| 556 } | |
| 557 | |
| 558 void ProfileSyncServiceAndroid::InvalidateAuthToken() { | |
| 559 // Get the token from token-db. If there's no token yet, this must be the | |
| 560 // the first time the user is signing in so we don't need to invalidate | |
| 561 // anything. | |
| 562 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | |
| 563 std::string invalid_token; | |
| 564 if (token_service->HasTokenForService(GaiaConstants::kSyncService)) { | |
| 565 invalid_token = token_service->GetTokenForService( | |
| 566 GaiaConstants::kSyncService); | |
| 567 } | |
| 568 const std::string& sync_username = | |
| 569 profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | |
| 570 // Call into java to invalidate the current token and get a new one. | |
| 571 JNIEnv* env = AttachCurrentThread(); | |
| 572 ScopedJavaLocalRef<jstring> j_sync_username = | |
| 573 ConvertUTF8ToJavaString(env, sync_username); | |
| 574 ScopedJavaLocalRef<jstring> j_invalid_token = | |
| 575 ConvertUTF8ToJavaString(env, invalid_token); | |
| 576 Java_ProfileSyncService_getNewAuthToken( | |
| 577 env, weak_java_profile_sync_service_.get(env).obj(), | |
| 578 j_sync_username.obj(), j_invalid_token.obj()); | |
| 579 } | |
| 580 | |
| 581 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, | |
| 582 jobject obj, | |
| 583 jstring objectId, | |
| 584 jlong version, | |
| 585 jstring state) { | |
| 586 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 587 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, | |
| 588 ConvertJavaStringToUTF8(env, state)); | |
| 589 } else { | |
| 590 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 591 base::Bind(&ProfileSyncServiceAndroid::SendNudgeNotification, | |
| 592 base::Unretained(this), /* this will not go away. */ | |
| 593 ConvertJavaStringToUTF8(env, objectId), version, | |
| 594 ConvertJavaStringToUTF8(env, state))); | |
| 595 } | |
| 596 } | |
| 597 | |
| 598 static int Init(JNIEnv* env, jobject obj) { | |
| 599 // We should have only one ProfileSyncServiceAndroid instance. | |
| 600 DCHECK(!g_profile_sync_service_android); | |
| 601 g_profile_sync_service_android = new ProfileSyncServiceAndroid(env, obj); | |
|
Yaron
2013/02/23 01:38:49
This is asserted in the java-layer. Remove global.
nyquist
2013/02/27 05:19:16
Done.
| |
| 602 return reinterpret_cast<jint>(g_profile_sync_service_android); | |
| 603 } | |
| 604 | |
| 605 // Register native methods | |
| 606 | |
| 607 bool RegisterProfileSyncServiceAndroid(JNIEnv* env) { | |
| 608 return RegisterNativesImpl(env); | |
| 609 } | |
| OLD | NEW |