[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" |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 8 | #include "base/android/jni_array.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 9 | #include "base/android/jni_string.h" |
| 10 | #include "base/bind.h" |
| 11 | #include "base/i18n/time_formatting.h" |
| 12 | #include "base/json/json_writer.h" |
| 13 | #include "base/logging.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
zea | b1b09b7 | 2015-05-01 17:56:40 | [diff] [blame] | 15 | #include "base/metrics/field_trial.h" |
[email protected] | 34ee051 | 2013-03-12 17:49:54 | [diff] [blame] | 16 | #include "base/prefs/pref_service.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 18 | #include "base/time/time.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 19 | #include "chrome/browser/browser_process.h" |
| 20 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 21 | #include "chrome/browser/signin/signin_manager_factory.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 22 | #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_factory.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 25 | #include "chrome/browser/sync/sync_ui_util.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 26 | #include "chrome/grit/generated_resources.h" |
[email protected] | 7fbd3b1 | 2014-04-01 11:19:16 | [diff] [blame] | 27 | #include "components/signin/core/browser/signin_manager.h" |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 28 | #include "components/sync_driver/pref_names.h" |
| 29 | #include "components/sync_driver/sync_prefs.h" |
[email protected] | dfe6a9b | 2014-01-31 21:37:55 | [diff] [blame] | 30 | #include "content/public/browser/browser_thread.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 31 | #include "google/cacheinvalidation/types.pb.h" |
| 32 | #include "google_apis/gaia/gaia_constants.h" |
| 33 | #include "google_apis/gaia/google_service_auth_error.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 34 | #include "jni/ProfileSyncService_jni.h" |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 35 | #include "sync/internal_api/public/network_resources.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 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 { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 47 | |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 48 | // Native callback for the JNI GetAllNodes method. When |
| 49 | // ProfileSyncService::GetAllNodes completes, this method is called and the |
| 50 | // results are sent to the Java callback. |
| 51 | void NativeGetAllNodesCallback( |
| 52 | const base::android::ScopedJavaGlobalRef<jobject>& callback, |
| 53 | scoped_ptr<base::ListValue> result) { |
| 54 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 55 | std::string json_string; |
| 56 | if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { |
| 57 | DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; |
| 58 | json_string = std::string(); |
| 59 | } |
| 60 | |
| 61 | ScopedJavaLocalRef<jstring> java_json_string = |
| 62 | ConvertUTF8ToJavaString(env, json_string); |
| 63 | Java_ProfileSyncService_onGetAllNodesResult(env, |
| 64 | callback.obj(), |
| 65 | java_json_string.obj()); |
| 66 | } |
| 67 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| 70 | ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj) |
| 71 | : profile_(NULL), |
| 72 | sync_service_(NULL), |
| 73 | weak_java_profile_sync_service_(env, obj) { |
| 74 | if (g_browser_process == NULL || |
| 75 | g_browser_process->profile_manager() == NULL) { |
| 76 | NOTREACHED() << "Browser process or profile manager not initialized"; |
| 77 | return; |
| 78 | } |
| 79 | |
[email protected] | dd547a2 | 2013-12-12 18:37:41 | [diff] [blame] | 80 | profile_ = ProfileManager::GetActiveUserProfile(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 81 | if (profile_ == NULL) { |
| 82 | NOTREACHED() << "Sync Init: Profile not found."; |
| 83 | return; |
| 84 | } |
| 85 | |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 86 | sync_prefs_.reset(new sync_driver::SyncPrefs(profile_->GetPrefs())); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 87 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 88 | sync_service_ = |
| 89 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); |
| 90 | DCHECK(sync_service_); |
| 91 | } |
| 92 | |
| 93 | void ProfileSyncServiceAndroid::Init() { |
| 94 | sync_service_->AddObserver(this); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ProfileSyncServiceAndroid::RemoveObserver() { |
| 98 | if (sync_service_->HasObserver(this)) { |
| 99 | sync_service_->RemoveObserver(this); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() { |
| 104 | RemoveObserver(); |
| 105 | } |
| 106 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 107 | void ProfileSyncServiceAndroid::OnStateChanged() { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 108 | // Notify the java world that our sync state has changed. |
| 109 | JNIEnv* env = AttachCurrentThread(); |
| 110 | Java_ProfileSyncService_syncStateChanged( |
| 111 | env, weak_java_profile_sync_service_.get(env).obj()); |
| 112 | } |
| 113 | |
zea | b1b09b7 | 2015-05-01 17:56:40 | [diff] [blame] | 114 | jboolean ProfileSyncServiceAndroid::IsPassphrasePrompted(JNIEnv* env, |
| 115 | jobject obj) { |
| 116 | const std::string group_name = |
| 117 | base::FieldTrialList::FindFullName("LimitSyncPassphrasePrompt"); |
| 118 | if (group_name != "Enabled") |
| 119 | return false; |
| 120 | return sync_prefs_->IsPassphrasePrompted(); |
| 121 | } |
| 122 | |
| 123 | void ProfileSyncServiceAndroid::SetPassphrasePrompted(JNIEnv* env, |
| 124 | jobject obj, |
| 125 | jboolean prompted) { |
| 126 | sync_prefs_->SetPassphrasePrompted(prompted); |
| 127 | } |
| 128 | |
maxbogue | f5c53a2d | 2015-07-08 20:47:29 | [diff] [blame] | 129 | void ProfileSyncServiceAndroid::RequestStart(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 130 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 131 | sync_service_->RequestStart(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 132 | } |
| 133 | |
maxbogue | f5c53a2d | 2015-07-08 20:47:29 | [diff] [blame] | 134 | void ProfileSyncServiceAndroid::RequestStop(JNIEnv* env, jobject) { |
| 135 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 136 | sync_service_->RequestStop(ProfileSyncService::KEEP_DATA); |
| 137 | } |
| 138 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 139 | void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 140 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 141 | DCHECK(profile_); |
maxbogue | 699262c9 | 2015-06-19 19:24:58 | [diff] [blame] | 142 | sync_service_->RequestStop(ProfileSyncService::CLEAR_DATA); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 143 | } |
| 144 | |
maxbogue | d4470ca | 2014-09-30 23:38:11 | [diff] [blame] | 145 | void ProfileSyncServiceAndroid::FlushDirectory(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 146 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | d4470ca | 2014-09-30 23:38:11 | [diff] [blame] | 147 | sync_service_->FlushDirectory(); |
| 148 | } |
| 149 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 150 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary( |
| 151 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 152 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 153 | DCHECK(profile_); |
[email protected] | 9e8df05 | 2013-09-18 10:47:18 | [diff] [blame] | 154 | std::string status(sync_service_->QuerySyncStatusSummaryString()); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 155 | return ConvertUTF8ToJavaString(env, status); |
| 156 | } |
| 157 | |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 158 | void ProfileSyncServiceAndroid::GetAllNodes(JNIEnv* env, |
| 159 | jobject obj, |
| 160 | jobject callback) { |
| 161 | base::android::ScopedJavaGlobalRef<jobject> java_callback; |
| 162 | java_callback.Reset(env, callback); |
| 163 | |
| 164 | base::Callback<void(scoped_ptr<base::ListValue>)> native_callback = |
| 165 | base::Bind(&NativeGetAllNodesCallback, java_callback); |
| 166 | sync_service_->GetAllNodes(native_callback); |
| 167 | } |
| 168 | |
maxbogue | c7f232d | 2015-07-29 19:25:40 | [diff] [blame] | 169 | void ProfileSyncServiceAndroid::SetSyncSessionsId( |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 170 | JNIEnv* env, jobject obj, jstring tag) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 171 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 172 | DCHECK(profile_); |
| 173 | std::string machine_tag = ConvertJavaStringToUTF8(env, tag); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 174 | sync_prefs_->SetSyncSessionsGUID(machine_tag); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 178 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 179 | return sync_service_->GetAuthError().state(); |
| 180 | } |
| 181 | |
| 182 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled( |
| 183 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 184 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 185 | return sync_service_->EncryptEverythingEnabled(); |
| 186 | } |
| 187 | |
| 188 | jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 189 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | 825174f | 2014-10-21 01:34:50 | [diff] [blame] | 190 | return sync_service_->backend_initialized(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress( |
| 194 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 195 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 196 | return sync_service_->FirstSetupInProgress(); |
| 197 | } |
| 198 | |
bauerb | f03588b9 | 2014-10-27 13:40:15 | [diff] [blame] | 199 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingAllowed( |
| 200 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 201 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
bauerb | f03588b9 | 2014-10-27 13:40:15 | [diff] [blame] | 202 | return sync_service_->EncryptEverythingAllowed(); |
| 203 | } |
| 204 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 205 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 206 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 207 | return sync_service_->IsPassphraseRequired(); |
| 208 | } |
| 209 | |
| 210 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption( |
| 211 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 212 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 213 | // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for |
| 214 | // a passphrase if cryptographer has any pending keys. |
| 215 | if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) { |
| 216 | return !IsCryptographerReady(env, obj); |
| 217 | } |
| 218 | if (sync_service_->IsPassphraseRequiredForDecryption()) { |
| 219 | // Passwords datatype should never prompt for a passphrase, except when |
| 220 | // user is using a custom passphrase. Do not prompt for a passphrase if |
| 221 | // passwords are the only encrypted datatype. This prevents a temporary |
| 222 | // notification for passphrase when PSS has not completed configuring |
| 223 | // DataTypeManager, after configuration password datatype shall be disabled. |
| 224 | const syncer::ModelTypeSet encrypted_types = |
| 225 | sync_service_->GetEncryptedDataTypes(); |
[email protected] | 1f51ad9 | 2013-06-07 02:30:24 | [diff] [blame] | 226 | return !encrypted_types.Equals(syncer::ModelTypeSet(syncer::PASSWORDS)); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForExternalType( |
| 232 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 233 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 234 | return |
| 235 | sync_service_->passphrase_required_reason() == syncer::REASON_DECRYPTION; |
| 236 | } |
| 237 | |
| 238 | jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase( |
| 239 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 240 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 241 | return sync_service_->IsUsingSecondaryPassphrase(); |
| 242 | } |
| 243 | |
| 244 | jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase( |
| 245 | JNIEnv* env, jobject obj, jstring passphrase) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 246 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 247 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 248 | return sync_service_->SetDecryptionPassphrase(key); |
| 249 | } |
| 250 | |
| 251 | void ProfileSyncServiceAndroid::SetEncryptionPassphrase( |
maxbogue | fd97c03 | 2015-07-29 19:29:35 | [diff] [blame] | 252 | JNIEnv* env, jobject obj, jstring passphrase) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 253 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 254 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
maxbogue | fd97c03 | 2015-07-29 19:29:35 | [diff] [blame] | 255 | sync_service_->SetEncryptionPassphrase(key, ProfileSyncService::EXPLICIT); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) { |
| 259 | syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 260 | return sync_service_->IsCryptographerReady(&trans); |
| 261 | } |
| 262 | |
| 263 | jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 264 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 265 | return sync_service_->GetPassphraseType(); |
| 266 | } |
| 267 | |
| 268 | jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime( |
| 269 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 270 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 271 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 272 | return !passphrase_time.is_null(); |
| 273 | } |
| 274 | |
maxbogue | c021ddcc | 2014-10-09 16:21:24 | [diff] [blame] | 275 | jlong ProfileSyncServiceAndroid::GetExplicitPassphraseTime( |
| 276 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 277 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | c021ddcc | 2014-10-09 16:21:24 | [diff] [blame] | 278 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 279 | return passphrase_time.ToJavaTime(); |
| 280 | } |
| 281 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 282 | ScopedJavaLocalRef<jstring> |
| 283 | ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText( |
| 284 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 285 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 286 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 287 | base::string16 passphrase_time_str = |
| 288 | base::TimeFormatShortDate(passphrase_time); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 289 | return base::android::ConvertUTF16ToJavaString(env, |
| 290 | l10n_util::GetStringFUTF16( |
| 291 | IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE, |
| 292 | passphrase_time_str)); |
| 293 | } |
| 294 | |
| 295 | ScopedJavaLocalRef<jstring> |
| 296 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText( |
| 297 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 298 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 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(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE, |
| 304 | passphrase_time_str)); |
| 305 | } |
| 306 | |
| 307 | ScopedJavaLocalRef<jstring> |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 308 | ProfileSyncServiceAndroid::GetCurrentSignedInAccountText( |
| 309 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 310 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 311 | const std::string& sync_username = |
| 312 | SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); |
| 313 | return base::android::ConvertUTF16ToJavaString(env, |
| 314 | l10n_util::GetStringFUTF16( |
| 315 | IDS_SYNC_ACCOUNT_SYNCING_TO_USER, |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 316 | base::ASCIIToUTF16(sync_username))); |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | ScopedJavaLocalRef<jstring> |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 320 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( |
| 321 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 322 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 323 | return ConvertUTF8ToJavaString( |
| 324 | env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); |
| 325 | } |
| 326 | |
| 327 | jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone( |
| 328 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 329 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 330 | syncer::SyncStatus status; |
| 331 | bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); |
| 332 | return is_status_valid && !status.keystore_migration_time.is_null(); |
| 333 | } |
| 334 | |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 335 | ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetActiveDataTypes( |
maxbogue | 86e260e | 2015-03-18 21:56:26 | [diff] [blame] | 336 | JNIEnv* env, jobject obj) { |
[email protected] | 464d74a | 2013-06-04 07:36:46 | [diff] [blame] | 337 | syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 338 | types.PutAll(syncer::ControlTypes()); |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 339 | return ModelTypeSetToJavaIntArray(env, types); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 340 | } |
| 341 | |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 342 | ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetPreferredDataTypes( |
maxbogue | 86e260e | 2015-03-18 21:56:26 | [diff] [blame] | 343 | JNIEnv* env, jobject obj) { |
| 344 | syncer::ModelTypeSet types = sync_service_->GetPreferredDataTypes(); |
| 345 | types.PutAll(syncer::ControlTypes()); |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 346 | return ModelTypeSetToJavaIntArray(env, types); |
maxbogue | 86e260e | 2015-03-18 21:56:26 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 349 | void ProfileSyncServiceAndroid::SetPreferredDataTypes( |
| 350 | JNIEnv* env, jobject obj, |
| 351 | jboolean sync_everything, |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 352 | jintArray model_type_array) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 353 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 354 | std::vector<int> types_vector; |
| 355 | base::android::JavaIntArrayToIntVector(env, model_type_array, &types_vector); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 356 | syncer::ModelTypeSet types; |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 357 | for (size_t i = 0; i < types_vector.size(); i++) { |
| 358 | types.Put(static_cast<syncer::ModelType>(types_vector[i])); |
| 359 | } |
| 360 | types.RetainAll(syncer::UserSelectableTypes()); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 361 | sync_service_->OnUserChoseDatatypes(sync_everything, types); |
| 362 | } |
| 363 | |
| 364 | void ProfileSyncServiceAndroid::SetSetupInProgress( |
| 365 | JNIEnv* env, jobject obj, jboolean in_progress) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 366 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 367 | sync_service_->SetSetupInProgress(in_progress); |
| 368 | } |
| 369 | |
| 370 | void ProfileSyncServiceAndroid::SetSyncSetupCompleted( |
| 371 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 372 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 373 | sync_service_->SetSyncSetupCompleted(); |
| 374 | } |
| 375 | |
| 376 | jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted( |
| 377 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 378 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 379 | return sync_service_->HasSyncSetupCompleted(); |
| 380 | } |
| 381 | |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 382 | jboolean ProfileSyncServiceAndroid::IsSyncRequested( |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 383 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 384 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 385 | return sync_service_->IsSyncRequested(); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 386 | } |
| 387 | |
maxbogue | 85931a6 | 2015-06-12 22:58:01 | [diff] [blame] | 388 | jboolean ProfileSyncServiceAndroid::IsSyncActive(JNIEnv* env, jobject obj) { |
| 389 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 390 | return sync_service_->IsSyncActive(); |
| 391 | } |
| 392 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 393 | void ProfileSyncServiceAndroid::EnableEncryptEverything( |
| 394 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 395 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 396 | sync_service_->EnableEncryptEverything(); |
| 397 | } |
| 398 | |
| 399 | jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced( |
| 400 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 401 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 402 | return sync_prefs_->HasKeepEverythingSynced(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 403 | } |
| 404 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 405 | jboolean ProfileSyncServiceAndroid::HasUnrecoverableError( |
| 406 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 407 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 408 | return sync_service_->HasUnrecoverableError(); |
| 409 | } |
| 410 | |
| 411 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( |
| 412 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 413 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 414 | |
[email protected] | dbb9aa4 | 2013-12-23 20:08:21 | [diff] [blame] | 415 | scoped_ptr<base::DictionaryValue> about_info = |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 416 | sync_ui_util::ConstructAboutInformation(sync_service_); |
| 417 | std::string about_info_json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 418 | base::JSONWriter::Write(*about_info, &about_info_json); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 419 | |
| 420 | return ConvertUTF8ToJavaString(env, about_info_json); |
| 421 | } |
| 422 | |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 423 | jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest( |
| 424 | JNIEnv* env, jobject obj) { |
| 425 | // Use profile preferences here instead of SyncPrefs to avoid an extra |
| 426 | // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value |
| 427 | // to to base::Time. |
| 428 | return static_cast<jlong>( |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 429 | profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime)); |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 430 | } |
| 431 | |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 432 | void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest( |
| 433 | JNIEnv* env, |
| 434 | jobject obj, |
| 435 | jlong network_resources) { |
| 436 | syncer::NetworkResources* resources = |
| 437 | reinterpret_cast<syncer::NetworkResources*>(network_resources); |
| 438 | sync_service_->OverrideNetworkResourcesForTest( |
| 439 | make_scoped_ptr<syncer::NetworkResources>(resources)); |
| 440 | } |
| 441 | |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 442 | // static |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 443 | ScopedJavaLocalRef<jintArray> |
| 444 | ProfileSyncServiceAndroid::ModelTypeSetToJavaIntArray( |
| 445 | JNIEnv* env, |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 446 | syncer::ModelTypeSet types) { |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 447 | std::vector<int> type_vector; |
| 448 | for (syncer::ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { |
| 449 | type_vector.push_back(it.Get()); |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 450 | } |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 451 | return base::android::ToJavaIntArray(env, type_vector); |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 452 | } |
| 453 | |
[email protected] | 332a8336 | 2013-03-26 08:45:55 | [diff] [blame] | 454 | // static |
| 455 | ProfileSyncServiceAndroid* |
| 456 | ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
| 457 | return reinterpret_cast<ProfileSyncServiceAndroid*>( |
maxbogue | 89bee1d86 | 2015-08-26 21:00:07 | [diff] [blame^] | 458 | Java_ProfileSyncService_getProfileSyncServiceAndroid( |
| 459 | AttachCurrentThread())); |
[email protected] | 332a8336 | 2013-03-26 08:45:55 | [diff] [blame] | 460 | } |
| 461 | |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 462 | static jlong Init(JNIEnv* env, jobject obj) { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 463 | ProfileSyncServiceAndroid* profile_sync_service_android = |
| 464 | new ProfileSyncServiceAndroid(env, obj); |
| 465 | profile_sync_service_android->Init(); |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 466 | return reinterpret_cast<intptr_t>(profile_sync_service_android); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // static |
| 470 | bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
| 471 | return RegisterNativesImpl(env); |
| 472 | } |