[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 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" |
[email protected] | 34ee051 | 2013-03-12 17:49:54 | [diff] [blame] | 14 | #include "base/prefs/pref_service.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 16 | #include "base/time/time.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 17 | #include "chrome/browser/browser_process.h" |
[email protected] | dcc8fbc | 2013-07-12 00:54:09 | [diff] [blame] | 18 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 19 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 20 | #include "chrome/browser/signin/signin_manager.h" |
| 21 | #include "chrome/browser/signin/signin_manager_factory.h" |
| 22 | #include "chrome/browser/signin/token_service.h" |
| 23 | #include "chrome/browser/signin/token_service_factory.h" |
| 24 | #include "chrome/browser/sync/about_sync_util.h" |
| 25 | #include "chrome/browser/sync/profile_sync_service.h" |
| 26 | #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 27 | #include "chrome/browser/sync/sync_prefs.h" |
| 28 | #include "chrome/browser/sync/sync_ui_util.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 29 | #include "chrome/common/pref_names.h" |
| 30 | #include "content/public/browser/notification_service.h" |
[email protected] | 9a3de84 | 2013-06-12 20:16:51 | [diff] [blame] | 31 | #include "content/public/browser/notification_source.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 32 | #include "google/cacheinvalidation/types.pb.h" |
| 33 | #include "google_apis/gaia/gaia_constants.h" |
| 34 | #include "google_apis/gaia/google_service_auth_error.h" |
[email protected] | 42f4121 | 2013-08-26 09:24:19 | [diff] [blame] | 35 | #include "google_apis/gaia/oauth2_token_service.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 36 | #include "grit/generated_resources.h" |
| 37 | #include "jni/ProfileSyncService_jni.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 38 | #include "sync/internal_api/public/read_transaction.h" |
| 39 | #include "ui/base/l10n/l10n_util.h" |
| 40 | |
| 41 | using base::android::AttachCurrentThread; |
| 42 | using base::android::CheckException; |
| 43 | using base::android::ConvertJavaStringToUTF8; |
| 44 | using base::android::ConvertUTF8ToJavaString; |
| 45 | using base::android::ScopedJavaLocalRef; |
| 46 | using content::BrowserThread; |
| 47 | |
| 48 | namespace { |
| 49 | const char kSyncDisabledStatus[] = "OFFLINE_DISABLED"; |
| 50 | |
| 51 | enum { |
| 52 | #define DEFINE_MODEL_TYPE_SELECTION(name,value) name = value, |
| 53 | #include "chrome/browser/sync/profile_sync_service_model_type_selection_android.h" |
| 54 | #undef DEFINE_MODEL_TYPE_SELECTION |
| 55 | }; |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj) |
| 60 | : profile_(NULL), |
| 61 | sync_service_(NULL), |
| 62 | weak_java_profile_sync_service_(env, obj) { |
| 63 | if (g_browser_process == NULL || |
| 64 | g_browser_process->profile_manager() == NULL) { |
| 65 | NOTREACHED() << "Browser process or profile manager not initialized"; |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | profile_ = g_browser_process->profile_manager()->GetDefaultProfile(); |
| 70 | if (profile_ == NULL) { |
| 71 | NOTREACHED() << "Sync Init: Profile not found."; |
| 72 | return; |
| 73 | } |
| 74 | |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 75 | sync_prefs_.reset(new browser_sync::SyncPrefs(profile_->GetPrefs())); |
| 76 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 77 | sync_service_ = |
| 78 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); |
| 79 | DCHECK(sync_service_); |
| 80 | } |
| 81 | |
| 82 | void ProfileSyncServiceAndroid::Init() { |
| 83 | sync_service_->AddObserver(this); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void ProfileSyncServiceAndroid::RemoveObserver() { |
| 87 | if (sync_service_->HasObserver(this)) { |
| 88 | sync_service_->RemoveObserver(this); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() { |
| 93 | RemoveObserver(); |
| 94 | } |
| 95 | |
| 96 | void ProfileSyncServiceAndroid::SendNudgeNotification( |
| 97 | const std::string& str_object_id, |
| 98 | int64 version, |
| 99 | const std::string& state) { |
| 100 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 101 | |
| 102 | // TODO(nileshagrawal): Merge this with ChromeInvalidationClient::Invalidate. |
| 103 | // Construct the ModelTypeStateMap and send it over with the notification. |
[email protected] | c56ab902 | 2013-07-12 03:10:08 | [diff] [blame] | 104 | invalidation::ObjectId object_id( |
| 105 | ipc::invalidation::ObjectSource::CHROME_SYNC, |
| 106 | str_object_id); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 107 | if (version != ipc::invalidation::Constants::UNKNOWN) { |
[email protected] | c56ab902 | 2013-07-12 03:10:08 | [diff] [blame] | 108 | ObjectIdVersionMap::iterator it = |
| 109 | max_invalidation_versions_.find(object_id); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 110 | if ((it != max_invalidation_versions_.end()) && |
| 111 | (version <= it->second)) { |
| 112 | DVLOG(1) << "Dropping redundant invalidation with version " << version; |
| 113 | return; |
| 114 | } |
[email protected] | c56ab902 | 2013-07-12 03:10:08 | [diff] [blame] | 115 | max_invalidation_versions_[object_id] = version; |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | c56ab902 | 2013-07-12 03:10:08 | [diff] [blame] | 118 | syncer::ObjectIdSet object_ids; |
| 119 | object_ids.insert(object_id); |
| 120 | syncer::ObjectIdInvalidationMap object_ids_with_states = |
[email protected] | ba79a79 | 2013-07-18 22:05:43 | [diff] [blame] | 121 | syncer::ObjectIdSetToInvalidationMap(object_ids, version, state); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 122 | |
| 123 | content::NotificationService::current()->Notify( |
| 124 | chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 125 | content::Source<Profile>(profile_), |
[email protected] | c56ab902 | 2013-07-12 03:10:08 | [diff] [blame] | 126 | content::Details<const syncer::ObjectIdInvalidationMap>( |
| 127 | &object_ids_with_states)); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 130 | void ProfileSyncServiceAndroid::OnStateChanged() { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 131 | // Notify the java world that our sync state has changed. |
| 132 | JNIEnv* env = AttachCurrentThread(); |
| 133 | Java_ProfileSyncService_syncStateChanged( |
| 134 | env, weak_java_profile_sync_service_.get(env).obj()); |
| 135 | } |
| 136 | |
| 137 | void ProfileSyncServiceAndroid::TokenAvailable( |
| 138 | JNIEnv* env, jobject, jstring username, jstring auth_token) { |
| 139 | std::string token = ConvertJavaStringToUTF8(env, auth_token); |
| 140 | TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( |
| 141 | GaiaConstants::kSyncService, token); |
| 142 | } |
| 143 | |
| 144 | void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { |
| 145 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 146 | // Don't need to do anything if we're already enabled. |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 147 | if (sync_prefs_->IsStartSuppressed()) |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 148 | sync_service_->UnsuppressAndStart(); |
| 149 | else |
| 150 | DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; |
| 151 | } |
| 152 | |
| 153 | void ProfileSyncServiceAndroid::DisableSync(JNIEnv* env, jobject) { |
| 154 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 41e0f37 | 2013-06-05 19:58:42 | [diff] [blame] | 155 | // Don't need to do anything if we're already disabled. |
| 156 | if (!sync_prefs_->IsStartSuppressed()) { |
| 157 | sync_service_->StopAndSuppress(); |
| 158 | } else { |
| 159 | DVLOG(2) |
| 160 | << "Ignoring call to DisableSync() because sync is already disabled"; |
| 161 | } |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 73da858 | 2013-07-11 10:46:36 | [diff] [blame] | 164 | void ProfileSyncServiceAndroid::SignInSync(JNIEnv* env, jobject) { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 165 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 166 | // Just return if sync already has everything it needs to start up (sync |
| 167 | // should start up automatically as long as it has credentials). This can |
| 168 | // happen normally if (for example) the user closes and reopens the sync |
| 169 | // settings window quickly during initial startup. |
| 170 | if (sync_service_->IsSyncEnabledAndLoggedIn() && |
[email protected] | b67c18c | 2013-06-13 23:52:03 | [diff] [blame] | 171 | sync_service_->IsOAuthRefreshTokenAvailable() && |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 172 | sync_service_->HasSyncSetupCompleted()) { |
| 173 | return; |
| 174 | } |
| 175 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 176 | // Enable sync (if we don't have credentials yet, this will enable sync but |
| 177 | // will not start it up - sync will start once credentials arrive). |
| 178 | sync_service_->UnsuppressAndStart(); |
| 179 | } |
| 180 | |
| 181 | void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) { |
| 182 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 183 | DCHECK(profile_); |
| 184 | sync_service_->DisableForUser(); |
| 185 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 186 | // Need to clear suppress start flag manually |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 187 | sync_prefs_->SetStartSuppressed(false); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary( |
| 191 | JNIEnv* env, jobject) { |
| 192 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 193 | DCHECK(profile_); |
| 194 | std::string status(sync_service_->QuerySyncStatusSummary()); |
| 195 | return ConvertUTF8ToJavaString(env, status); |
| 196 | } |
| 197 | |
| 198 | jboolean ProfileSyncServiceAndroid::SetSyncSessionsId( |
| 199 | JNIEnv* env, jobject obj, jstring tag) { |
| 200 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 201 | DCHECK(profile_); |
| 202 | std::string machine_tag = ConvertJavaStringToUTF8(env, tag); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 203 | sync_prefs_->SetSyncSessionsGUID(machine_tag); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | |
| 207 | jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) { |
| 208 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 209 | return sync_service_->GetAuthError().state(); |
| 210 | } |
| 211 | |
| 212 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled( |
| 213 | JNIEnv* env, jobject) { |
| 214 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 215 | return sync_service_->EncryptEverythingEnabled(); |
| 216 | } |
| 217 | |
| 218 | jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) { |
| 219 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 220 | return sync_service_->sync_initialized(); |
| 221 | } |
| 222 | |
| 223 | jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress( |
| 224 | JNIEnv* env, jobject) { |
| 225 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 226 | return sync_service_->FirstSetupInProgress(); |
| 227 | } |
| 228 | |
| 229 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) { |
| 230 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 231 | return sync_service_->IsPassphraseRequired(); |
| 232 | } |
| 233 | |
| 234 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption( |
| 235 | JNIEnv* env, jobject obj) { |
| 236 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 237 | // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for |
| 238 | // a passphrase if cryptographer has any pending keys. |
| 239 | if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) { |
| 240 | return !IsCryptographerReady(env, obj); |
| 241 | } |
| 242 | if (sync_service_->IsPassphraseRequiredForDecryption()) { |
| 243 | // Passwords datatype should never prompt for a passphrase, except when |
| 244 | // user is using a custom passphrase. Do not prompt for a passphrase if |
| 245 | // passwords are the only encrypted datatype. This prevents a temporary |
| 246 | // notification for passphrase when PSS has not completed configuring |
| 247 | // DataTypeManager, after configuration password datatype shall be disabled. |
| 248 | const syncer::ModelTypeSet encrypted_types = |
| 249 | sync_service_->GetEncryptedDataTypes(); |
[email protected] | 1f51ad9 | 2013-06-07 02:30:24 | [diff] [blame] | 250 | return !encrypted_types.Equals(syncer::ModelTypeSet(syncer::PASSWORDS)); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForExternalType( |
| 256 | JNIEnv* env, jobject) { |
| 257 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 258 | return |
| 259 | sync_service_->passphrase_required_reason() == syncer::REASON_DECRYPTION; |
| 260 | } |
| 261 | |
| 262 | jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase( |
| 263 | JNIEnv* env, jobject) { |
| 264 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 265 | return sync_service_->IsUsingSecondaryPassphrase(); |
| 266 | } |
| 267 | |
| 268 | jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase( |
| 269 | JNIEnv* env, jobject obj, jstring passphrase) { |
| 270 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 271 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 272 | return sync_service_->SetDecryptionPassphrase(key); |
| 273 | } |
| 274 | |
| 275 | void ProfileSyncServiceAndroid::SetEncryptionPassphrase( |
| 276 | JNIEnv* env, jobject obj, jstring passphrase, jboolean is_gaia) { |
| 277 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 278 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 279 | sync_service_->SetEncryptionPassphrase( |
| 280 | key, |
| 281 | is_gaia ? ProfileSyncService::IMPLICIT : ProfileSyncService::EXPLICIT); |
| 282 | } |
| 283 | |
| 284 | jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) { |
| 285 | syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 286 | return sync_service_->IsCryptographerReady(&trans); |
| 287 | } |
| 288 | |
| 289 | jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) { |
| 290 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 291 | return sync_service_->GetPassphraseType(); |
| 292 | } |
| 293 | |
| 294 | jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime( |
| 295 | JNIEnv* env, jobject) { |
| 296 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 297 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 298 | return !passphrase_time.is_null(); |
| 299 | } |
| 300 | |
| 301 | ScopedJavaLocalRef<jstring> |
| 302 | ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText( |
| 303 | JNIEnv* env, jobject) { |
| 304 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 305 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 306 | string16 passphrase_time_str = base::TimeFormatShortDate(passphrase_time); |
| 307 | return base::android::ConvertUTF16ToJavaString(env, |
| 308 | l10n_util::GetStringFUTF16( |
| 309 | IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE, |
| 310 | passphrase_time_str)); |
| 311 | } |
| 312 | |
| 313 | ScopedJavaLocalRef<jstring> |
| 314 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText( |
| 315 | JNIEnv* env, jobject) { |
| 316 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 317 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 318 | string16 passphrase_time_str = base::TimeFormatShortDate(passphrase_time); |
| 319 | return base::android::ConvertUTF16ToJavaString(env, |
| 320 | l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE, |
| 321 | passphrase_time_str)); |
| 322 | } |
| 323 | |
| 324 | ScopedJavaLocalRef<jstring> |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 325 | ProfileSyncServiceAndroid::GetCurrentSignedInAccountText( |
| 326 | JNIEnv* env, jobject) { |
| 327 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 328 | const std::string& sync_username = |
| 329 | SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); |
| 330 | return base::android::ConvertUTF16ToJavaString(env, |
| 331 | l10n_util::GetStringFUTF16( |
| 332 | IDS_SYNC_ACCOUNT_SYNCING_TO_USER, |
| 333 | ASCIIToUTF16(sync_username))); |
| 334 | } |
| 335 | |
| 336 | ScopedJavaLocalRef<jstring> |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 337 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( |
| 338 | JNIEnv* env, jobject) { |
| 339 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 340 | return ConvertUTF8ToJavaString( |
| 341 | env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); |
| 342 | } |
| 343 | |
| 344 | jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone( |
| 345 | JNIEnv* env, jobject) { |
| 346 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 347 | syncer::SyncStatus status; |
| 348 | bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); |
| 349 | return is_status_valid && !status.keystore_migration_time.is_null(); |
| 350 | } |
| 351 | |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 352 | jlong ProfileSyncServiceAndroid::GetEnabledDataTypes(JNIEnv* env, |
| 353 | jobject obj) { |
| 354 | jlong model_type_selection = 0; |
[email protected] | 464d74a | 2013-06-04 07:36:46 | [diff] [blame] | 355 | syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 356 | types.PutAll(syncer::ControlTypes()); |
| 357 | if (types.Has(syncer::BOOKMARKS)) { |
| 358 | model_type_selection |= BOOKMARK; |
| 359 | } |
| 360 | if (types.Has(syncer::AUTOFILL)) { |
| 361 | model_type_selection |= AUTOFILL; |
| 362 | } |
| 363 | if (types.Has(syncer::AUTOFILL_PROFILE)) { |
| 364 | model_type_selection |= AUTOFILL_PROFILE; |
| 365 | } |
| 366 | if (types.Has(syncer::PASSWORDS)) { |
| 367 | model_type_selection |= PASSWORD; |
| 368 | } |
| 369 | if (types.Has(syncer::TYPED_URLS)) { |
| 370 | model_type_selection |= TYPED_URL; |
| 371 | } |
| 372 | if (types.Has(syncer::SESSIONS)) { |
| 373 | model_type_selection |= SESSION; |
| 374 | } |
| 375 | if (types.Has(syncer::HISTORY_DELETE_DIRECTIVES)) { |
| 376 | model_type_selection |= HISTORY_DELETE_DIRECTIVE; |
| 377 | } |
| 378 | if (types.Has(syncer::PROXY_TABS)) { |
| 379 | model_type_selection |= PROXY_TABS; |
| 380 | } |
| 381 | if (types.Has(syncer::FAVICON_IMAGES)) { |
| 382 | model_type_selection |= FAVICON_IMAGE; |
| 383 | } |
| 384 | if (types.Has(syncer::FAVICON_TRACKING)) { |
| 385 | model_type_selection |= FAVICON_TRACKING; |
| 386 | } |
| 387 | if (types.Has(syncer::DEVICE_INFO)) { |
| 388 | model_type_selection |= DEVICE_INFO; |
| 389 | } |
| 390 | if (types.Has(syncer::NIGORI)) { |
| 391 | model_type_selection |= NIGORI; |
| 392 | } |
| 393 | if (types.Has(syncer::EXPERIMENTS)) { |
| 394 | model_type_selection |= EXPERIMENTS; |
| 395 | } |
| 396 | return model_type_selection; |
| 397 | } |
| 398 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 399 | void ProfileSyncServiceAndroid::SetPreferredDataTypes( |
| 400 | JNIEnv* env, jobject obj, |
| 401 | jboolean sync_everything, |
| 402 | jlong model_type_selection) { |
| 403 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 404 | syncer::ModelTypeSet types; |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 405 | // Note: only user selectable types should be included here. |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 406 | if (model_type_selection & AUTOFILL) |
| 407 | types.Put(syncer::AUTOFILL); |
| 408 | if (model_type_selection & BOOKMARK) |
| 409 | types.Put(syncer::BOOKMARKS); |
| 410 | if (model_type_selection & PASSWORD) |
| 411 | types.Put(syncer::PASSWORDS); |
[email protected] | 88dfd0e | 2013-04-02 20:55:02 | [diff] [blame] | 412 | if (model_type_selection & PROXY_TABS) |
| 413 | types.Put(syncer::PROXY_TABS); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 414 | if (model_type_selection & TYPED_URL) |
| 415 | types.Put(syncer::TYPED_URLS); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 416 | DCHECK(syncer::UserSelectableTypes().HasAll(types)); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 417 | sync_service_->OnUserChoseDatatypes(sync_everything, types); |
| 418 | } |
| 419 | |
| 420 | void ProfileSyncServiceAndroid::SetSetupInProgress( |
| 421 | JNIEnv* env, jobject obj, jboolean in_progress) { |
| 422 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 423 | sync_service_->SetSetupInProgress(in_progress); |
| 424 | } |
| 425 | |
| 426 | void ProfileSyncServiceAndroid::SetSyncSetupCompleted( |
| 427 | JNIEnv* env, jobject obj) { |
| 428 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 429 | sync_service_->SetSyncSetupCompleted(); |
| 430 | } |
| 431 | |
| 432 | jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted( |
| 433 | JNIEnv* env, jobject obj) { |
| 434 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 435 | return sync_service_->HasSyncSetupCompleted(); |
| 436 | } |
| 437 | |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 438 | jboolean ProfileSyncServiceAndroid::IsStartSuppressed( |
| 439 | JNIEnv* env, jobject obj) { |
| 440 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 441 | return sync_prefs_->IsStartSuppressed(); |
| 442 | } |
| 443 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 444 | void ProfileSyncServiceAndroid::EnableEncryptEverything( |
| 445 | JNIEnv* env, jobject obj) { |
| 446 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 447 | sync_service_->EnableEncryptEverything(); |
| 448 | } |
| 449 | |
| 450 | jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced( |
| 451 | JNIEnv* env, jobject) { |
| 452 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 453 | return sync_prefs_->HasKeepEverythingSynced(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 456 | jboolean ProfileSyncServiceAndroid::HasUnrecoverableError( |
| 457 | JNIEnv* env, jobject) { |
| 458 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 459 | return sync_service_->HasUnrecoverableError(); |
| 460 | } |
| 461 | |
| 462 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( |
| 463 | JNIEnv* env, jobject) { |
| 464 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 465 | |
| 466 | scoped_ptr<DictionaryValue> about_info = |
| 467 | sync_ui_util::ConstructAboutInformation(sync_service_); |
| 468 | std::string about_info_json; |
| 469 | base::JSONWriter::Write(about_info.get(), &about_info_json); |
| 470 | |
| 471 | return ConvertUTF8ToJavaString(env, about_info_json); |
| 472 | } |
| 473 | |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame^] | 474 | jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest( |
| 475 | JNIEnv* env, jobject obj) { |
| 476 | // Use profile preferences here instead of SyncPrefs to avoid an extra |
| 477 | // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value |
| 478 | // to to base::Time. |
| 479 | return static_cast<jlong>( |
| 480 | profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime)); |
| 481 | } |
| 482 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 483 | void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, |
| 484 | jobject obj, |
| 485 | jstring objectId, |
| 486 | jlong version, |
| 487 | jstring state) { |
| 488 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 489 | SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, |
| 490 | ConvertJavaStringToUTF8(env, state)); |
| 491 | } |
| 492 | |
[email protected] | 8f63ef2 | 2013-07-17 02:09:37 | [diff] [blame] | 493 | void ProfileSyncServiceAndroid::NudgeSyncerForAllTypes(JNIEnv* env, |
| 494 | jobject obj) { |
| 495 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 496 | syncer::ObjectIdInvalidationMap object_ids_with_states; |
| 497 | content::NotificationService::current()->Notify( |
| 498 | chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 499 | content::Source<Profile>(profile_), |
| 500 | content::Details<const syncer::ObjectIdInvalidationMap>( |
| 501 | &object_ids_with_states)); |
| 502 | } |
| 503 | |
[email protected] | 332a8336 | 2013-03-26 08:45:55 | [diff] [blame] | 504 | // static |
| 505 | ProfileSyncServiceAndroid* |
| 506 | ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
| 507 | return reinterpret_cast<ProfileSyncServiceAndroid*>( |
| 508 | Java_ProfileSyncService_getProfileSyncServiceAndroid( |
| 509 | AttachCurrentThread(), base::android::GetApplicationContext())); |
| 510 | } |
| 511 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 512 | static int Init(JNIEnv* env, jobject obj) { |
| 513 | ProfileSyncServiceAndroid* profile_sync_service_android = |
| 514 | new ProfileSyncServiceAndroid(env, obj); |
| 515 | profile_sync_service_android->Init(); |
| 516 | return reinterpret_cast<jint>(profile_sync_service_android); |
| 517 | } |
| 518 | |
| 519 | // static |
| 520 | bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
| 521 | return RegisterNativesImpl(env); |
| 522 | } |