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