[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" |
zea | b1b09b7 | 2015-05-01 17:56:40 | [diff] [blame] | 14 | #include "base/metrics/field_trial.h" |
[email protected] | 34ee051 | 2013-03-12 17:49:54 | [diff] [blame] | 15 | #include "base/prefs/pref_service.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 17 | #include "base/time/time.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 18 | #include "chrome/browser/browser_process.h" |
| 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] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 25 | #include "chrome/grit/generated_resources.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 "google/cacheinvalidation/types.pb.h" |
| 31 | #include "google_apis/gaia/gaia_constants.h" |
| 32 | #include "google_apis/gaia/google_service_auth_error.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 33 | #include "jni/ProfileSyncService_jni.h" |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 34 | #include "sync/internal_api/public/network_resources.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 35 | #include "sync/internal_api/public/read_transaction.h" |
| 36 | #include "ui/base/l10n/l10n_util.h" |
| 37 | |
| 38 | using base::android::AttachCurrentThread; |
| 39 | using base::android::CheckException; |
| 40 | using base::android::ConvertJavaStringToUTF8; |
| 41 | using base::android::ConvertUTF8ToJavaString; |
| 42 | using base::android::ScopedJavaLocalRef; |
| 43 | using content::BrowserThread; |
| 44 | |
| 45 | namespace { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 46 | |
mkosiba | dfdc7fb2 | 2014-10-23 13:56:12 | [diff] [blame] | 47 | // This enum contains the list of sync ModelTypes that Android can register for |
| 48 | // invalidations for. |
| 49 | // |
| 50 | // A Java counterpart will be generated for this enum. |
| 51 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.sync |
| 52 | enum ModelTypeSelection { |
| 53 | AUTOFILL = 1 << 0, |
| 54 | BOOKMARK = 1 << 1, |
| 55 | PASSWORD = 1 << 2, |
| 56 | SESSION = 1 << 3, |
| 57 | TYPED_URL = 1 << 4, |
| 58 | AUTOFILL_PROFILE = 1 << 5, |
| 59 | HISTORY_DELETE_DIRECTIVE = 1 << 6, |
| 60 | PROXY_TABS = 1 << 7, |
| 61 | FAVICON_IMAGE = 1 << 8, |
| 62 | FAVICON_TRACKING = 1 << 9, |
| 63 | NIGORI = 1 << 10, |
| 64 | DEVICE_INFO = 1 << 11, |
| 65 | EXPERIMENTS = 1 << 12, |
| 66 | SUPERVISED_USER_SETTING = 1 << 13, |
bauerb | 4da3613 | 2014-12-26 19:53:13 | [diff] [blame] | 67 | SUPERVISED_USER_WHITELIST = 1 << 14, |
zea | e628112 | 2015-03-26 00:30:30 | [diff] [blame] | 68 | AUTOFILL_WALLET = 1 << 15, |
rouslan | 39765cf | 2015-05-22 16:57:10 | [diff] [blame] | 69 | AUTOFILL_WALLET_METADATA = 1 << 16, |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 70 | }; |
| 71 | |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 72 | // Native callback for the JNI GetAllNodes method. When |
| 73 | // ProfileSyncService::GetAllNodes completes, this method is called and the |
| 74 | // results are sent to the Java callback. |
| 75 | void NativeGetAllNodesCallback( |
| 76 | const base::android::ScopedJavaGlobalRef<jobject>& callback, |
| 77 | scoped_ptr<base::ListValue> result) { |
| 78 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 79 | std::string json_string; |
| 80 | if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { |
| 81 | DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; |
| 82 | json_string = std::string(); |
| 83 | } |
| 84 | |
| 85 | ScopedJavaLocalRef<jstring> java_json_string = |
| 86 | ConvertUTF8ToJavaString(env, json_string); |
| 87 | Java_ProfileSyncService_onGetAllNodesResult(env, |
| 88 | callback.obj(), |
| 89 | java_json_string.obj()); |
| 90 | } |
| 91 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 92 | } // namespace |
| 93 | |
| 94 | ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj) |
| 95 | : profile_(NULL), |
| 96 | sync_service_(NULL), |
| 97 | weak_java_profile_sync_service_(env, obj) { |
| 98 | if (g_browser_process == NULL || |
| 99 | g_browser_process->profile_manager() == NULL) { |
| 100 | NOTREACHED() << "Browser process or profile manager not initialized"; |
| 101 | return; |
| 102 | } |
| 103 | |
[email protected] | dd547a2 | 2013-12-12 18:37:41 | [diff] [blame] | 104 | profile_ = ProfileManager::GetActiveUserProfile(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 105 | if (profile_ == NULL) { |
| 106 | NOTREACHED() << "Sync Init: Profile not found."; |
| 107 | return; |
| 108 | } |
| 109 | |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 110 | sync_prefs_.reset(new sync_driver::SyncPrefs(profile_->GetPrefs())); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 111 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 112 | sync_service_ = |
| 113 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); |
| 114 | DCHECK(sync_service_); |
| 115 | } |
| 116 | |
| 117 | void ProfileSyncServiceAndroid::Init() { |
| 118 | sync_service_->AddObserver(this); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void ProfileSyncServiceAndroid::RemoveObserver() { |
| 122 | if (sync_service_->HasObserver(this)) { |
| 123 | sync_service_->RemoveObserver(this); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() { |
| 128 | RemoveObserver(); |
| 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 | |
zea | b1b09b7 | 2015-05-01 17:56:40 | [diff] [blame] | 138 | jboolean ProfileSyncServiceAndroid::IsPassphrasePrompted(JNIEnv* env, |
| 139 | jobject obj) { |
| 140 | const std::string group_name = |
| 141 | base::FieldTrialList::FindFullName("LimitSyncPassphrasePrompt"); |
| 142 | if (group_name != "Enabled") |
| 143 | return false; |
| 144 | return sync_prefs_->IsPassphrasePrompted(); |
| 145 | } |
| 146 | |
| 147 | void ProfileSyncServiceAndroid::SetPassphrasePrompted(JNIEnv* env, |
| 148 | jobject obj, |
| 149 | jboolean prompted) { |
| 150 | sync_prefs_->SetPassphrasePrompted(prompted); |
| 151 | } |
| 152 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 153 | void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 154 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 155 | // Don't need to do anything if we're already enabled. |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 156 | if (!sync_service_->IsSyncRequested()) |
| 157 | sync_service_->RequestStart(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 158 | else |
| 159 | DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; |
| 160 | } |
| 161 | |
| 162 | void ProfileSyncServiceAndroid::DisableSync(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 163 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 41e0f37 | 2013-06-05 19:58:42 | [diff] [blame] | 164 | // Don't need to do anything if we're already disabled. |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 165 | if (sync_service_->IsSyncRequested()) { |
maxbogue | 699262c9 | 2015-06-19 19:24:58 | [diff] [blame^] | 166 | sync_service_->RequestStop(ProfileSyncService::KEEP_DATA); |
[email protected] | 41e0f37 | 2013-06-05 19:58:42 | [diff] [blame] | 167 | } else { |
| 168 | DVLOG(2) |
| 169 | << "Ignoring call to DisableSync() because sync is already disabled"; |
| 170 | } |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 73da858 | 2013-07-11 10:46:36 | [diff] [blame] | 173 | void ProfileSyncServiceAndroid::SignInSync(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 174 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 175 | // Just return if sync already has everything it needs to start up (sync |
| 176 | // should start up automatically as long as it has credentials). This can |
| 177 | // happen normally if (for example) the user closes and reopens the sync |
| 178 | // settings window quickly during initial startup. |
maxbogue | 96b7d27 | 2015-06-16 02:59:58 | [diff] [blame] | 179 | if (sync_service_->CanSyncStart() && |
[email protected] | b67c18c | 2013-06-13 23:52:03 | [diff] [blame] | 180 | sync_service_->IsOAuthRefreshTokenAvailable() && |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 181 | sync_service_->HasSyncSetupCompleted()) { |
| 182 | return; |
| 183 | } |
| 184 | |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 185 | // Request that sync starts. If we don't have credentials yet, this will |
| 186 | // let sync start once credentials arrive. |
| 187 | sync_service_->RequestStart(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 191 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 192 | DCHECK(profile_); |
maxbogue | 699262c9 | 2015-06-19 19:24:58 | [diff] [blame^] | 193 | sync_service_->RequestStop(ProfileSyncService::CLEAR_DATA); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 194 | } |
| 195 | |
maxbogue | d4470ca | 2014-09-30 23:38:11 | [diff] [blame] | 196 | void ProfileSyncServiceAndroid::FlushDirectory(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 197 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | d4470ca | 2014-09-30 23:38:11 | [diff] [blame] | 198 | sync_service_->FlushDirectory(); |
| 199 | } |
| 200 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 201 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary( |
| 202 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 203 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 204 | DCHECK(profile_); |
[email protected] | 9e8df05 | 2013-09-18 10:47:18 | [diff] [blame] | 205 | std::string status(sync_service_->QuerySyncStatusSummaryString()); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 206 | return ConvertUTF8ToJavaString(env, status); |
| 207 | } |
| 208 | |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 209 | void ProfileSyncServiceAndroid::GetAllNodes(JNIEnv* env, |
| 210 | jobject obj, |
| 211 | jobject callback) { |
| 212 | base::android::ScopedJavaGlobalRef<jobject> java_callback; |
| 213 | java_callback.Reset(env, callback); |
| 214 | |
| 215 | base::Callback<void(scoped_ptr<base::ListValue>)> native_callback = |
| 216 | base::Bind(&NativeGetAllNodesCallback, java_callback); |
| 217 | sync_service_->GetAllNodes(native_callback); |
| 218 | } |
| 219 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 220 | jboolean ProfileSyncServiceAndroid::SetSyncSessionsId( |
| 221 | JNIEnv* env, jobject obj, jstring tag) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 222 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 223 | DCHECK(profile_); |
| 224 | std::string machine_tag = ConvertJavaStringToUTF8(env, tag); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 225 | sync_prefs_->SetSyncSessionsGUID(machine_tag); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
| 229 | jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 230 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 231 | return sync_service_->GetAuthError().state(); |
| 232 | } |
| 233 | |
| 234 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled( |
| 235 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 236 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 237 | return sync_service_->EncryptEverythingEnabled(); |
| 238 | } |
| 239 | |
| 240 | jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 241 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | 825174f | 2014-10-21 01:34:50 | [diff] [blame] | 242 | return sync_service_->backend_initialized(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress( |
| 246 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 247 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 248 | return sync_service_->FirstSetupInProgress(); |
| 249 | } |
| 250 | |
bauerb | f03588b9 | 2014-10-27 13:40:15 | [diff] [blame] | 251 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingAllowed( |
| 252 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 253 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
bauerb | f03588b9 | 2014-10-27 13:40:15 | [diff] [blame] | 254 | return sync_service_->EncryptEverythingAllowed(); |
| 255 | } |
| 256 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 257 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 258 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 259 | return sync_service_->IsPassphraseRequired(); |
| 260 | } |
| 261 | |
| 262 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption( |
| 263 | JNIEnv* env, jobject obj) { |
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 | // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for |
| 266 | // a passphrase if cryptographer has any pending keys. |
| 267 | if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) { |
| 268 | return !IsCryptographerReady(env, obj); |
| 269 | } |
| 270 | if (sync_service_->IsPassphraseRequiredForDecryption()) { |
| 271 | // Passwords datatype should never prompt for a passphrase, except when |
| 272 | // user is using a custom passphrase. Do not prompt for a passphrase if |
| 273 | // passwords are the only encrypted datatype. This prevents a temporary |
| 274 | // notification for passphrase when PSS has not completed configuring |
| 275 | // DataTypeManager, after configuration password datatype shall be disabled. |
| 276 | const syncer::ModelTypeSet encrypted_types = |
| 277 | sync_service_->GetEncryptedDataTypes(); |
[email protected] | 1f51ad9 | 2013-06-07 02:30:24 | [diff] [blame] | 278 | return !encrypted_types.Equals(syncer::ModelTypeSet(syncer::PASSWORDS)); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForExternalType( |
| 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 | return |
| 287 | sync_service_->passphrase_required_reason() == syncer::REASON_DECRYPTION; |
| 288 | } |
| 289 | |
| 290 | jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase( |
| 291 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 292 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 293 | return sync_service_->IsUsingSecondaryPassphrase(); |
| 294 | } |
| 295 | |
| 296 | jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase( |
| 297 | JNIEnv* env, jobject obj, jstring passphrase) { |
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 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 300 | return sync_service_->SetDecryptionPassphrase(key); |
| 301 | } |
| 302 | |
| 303 | void ProfileSyncServiceAndroid::SetEncryptionPassphrase( |
| 304 | JNIEnv* env, jobject obj, jstring passphrase, jboolean is_gaia) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 305 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 306 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 307 | sync_service_->SetEncryptionPassphrase( |
| 308 | key, |
| 309 | is_gaia ? ProfileSyncService::IMPLICIT : ProfileSyncService::EXPLICIT); |
| 310 | } |
| 311 | |
| 312 | jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) { |
| 313 | syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 314 | return sync_service_->IsCryptographerReady(&trans); |
| 315 | } |
| 316 | |
| 317 | jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 318 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 319 | return sync_service_->GetPassphraseType(); |
| 320 | } |
| 321 | |
| 322 | jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime( |
| 323 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 324 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 325 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 326 | return !passphrase_time.is_null(); |
| 327 | } |
| 328 | |
maxbogue | c021ddcc | 2014-10-09 16:21:24 | [diff] [blame] | 329 | jlong ProfileSyncServiceAndroid::GetExplicitPassphraseTime( |
| 330 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 331 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | c021ddcc | 2014-10-09 16:21:24 | [diff] [blame] | 332 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 333 | return passphrase_time.ToJavaTime(); |
| 334 | } |
| 335 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 336 | ScopedJavaLocalRef<jstring> |
| 337 | ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText( |
| 338 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 339 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 340 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 341 | base::string16 passphrase_time_str = |
| 342 | base::TimeFormatShortDate(passphrase_time); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 343 | return base::android::ConvertUTF16ToJavaString(env, |
| 344 | l10n_util::GetStringFUTF16( |
| 345 | IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE, |
| 346 | passphrase_time_str)); |
| 347 | } |
| 348 | |
| 349 | ScopedJavaLocalRef<jstring> |
| 350 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText( |
| 351 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 352 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 353 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 354 | base::string16 passphrase_time_str = |
| 355 | base::TimeFormatShortDate(passphrase_time); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 356 | return base::android::ConvertUTF16ToJavaString(env, |
| 357 | l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE, |
| 358 | passphrase_time_str)); |
| 359 | } |
| 360 | |
| 361 | ScopedJavaLocalRef<jstring> |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 362 | ProfileSyncServiceAndroid::GetCurrentSignedInAccountText( |
| 363 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 364 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 365 | const std::string& sync_username = |
| 366 | SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); |
| 367 | return base::android::ConvertUTF16ToJavaString(env, |
| 368 | l10n_util::GetStringFUTF16( |
| 369 | IDS_SYNC_ACCOUNT_SYNCING_TO_USER, |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 370 | base::ASCIIToUTF16(sync_username))); |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | ScopedJavaLocalRef<jstring> |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 374 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( |
| 375 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 376 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 377 | return ConvertUTF8ToJavaString( |
| 378 | env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); |
| 379 | } |
| 380 | |
| 381 | jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone( |
| 382 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 383 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 384 | syncer::SyncStatus status; |
| 385 | bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); |
| 386 | return is_status_valid && !status.keystore_migration_time.is_null(); |
| 387 | } |
| 388 | |
maxbogue | 86e260e | 2015-03-18 21:56:26 | [diff] [blame] | 389 | jlong ProfileSyncServiceAndroid::GetActiveDataTypes( |
| 390 | JNIEnv* env, jobject obj) { |
[email protected] | 464d74a | 2013-06-04 07:36:46 | [diff] [blame] | 391 | syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 392 | types.PutAll(syncer::ControlTypes()); |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 393 | return ModelTypeSetToSelection(types); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 394 | } |
| 395 | |
maxbogue | 86e260e | 2015-03-18 21:56:26 | [diff] [blame] | 396 | jlong ProfileSyncServiceAndroid::GetPreferredDataTypes( |
| 397 | JNIEnv* env, jobject obj) { |
| 398 | syncer::ModelTypeSet types = sync_service_->GetPreferredDataTypes(); |
| 399 | types.PutAll(syncer::ControlTypes()); |
| 400 | return ModelTypeSetToSelection(types); |
| 401 | } |
| 402 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 403 | void ProfileSyncServiceAndroid::SetPreferredDataTypes( |
| 404 | JNIEnv* env, jobject obj, |
| 405 | jboolean sync_everything, |
| 406 | jlong model_type_selection) { |
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 | syncer::ModelTypeSet types; |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 409 | // Note: only user selectable types should be included here. |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 410 | if (model_type_selection & AUTOFILL) |
| 411 | types.Put(syncer::AUTOFILL); |
| 412 | if (model_type_selection & BOOKMARK) |
| 413 | types.Put(syncer::BOOKMARKS); |
| 414 | if (model_type_selection & PASSWORD) |
| 415 | types.Put(syncer::PASSWORDS); |
[email protected] | 88dfd0e | 2013-04-02 20:55:02 | [diff] [blame] | 416 | if (model_type_selection & PROXY_TABS) |
| 417 | types.Put(syncer::PROXY_TABS); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 418 | if (model_type_selection & TYPED_URL) |
| 419 | types.Put(syncer::TYPED_URLS); |
[email protected] | 6805b60 | 2013-03-27 21:18:18 | [diff] [blame] | 420 | DCHECK(syncer::UserSelectableTypes().HasAll(types)); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 421 | sync_service_->OnUserChoseDatatypes(sync_everything, types); |
| 422 | } |
| 423 | |
| 424 | void ProfileSyncServiceAndroid::SetSetupInProgress( |
| 425 | JNIEnv* env, jobject obj, jboolean in_progress) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 426 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 427 | sync_service_->SetSetupInProgress(in_progress); |
| 428 | } |
| 429 | |
| 430 | void ProfileSyncServiceAndroid::SetSyncSetupCompleted( |
| 431 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 432 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 433 | sync_service_->SetSyncSetupCompleted(); |
| 434 | } |
| 435 | |
| 436 | jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted( |
| 437 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 438 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 439 | return sync_service_->HasSyncSetupCompleted(); |
| 440 | } |
| 441 | |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 442 | jboolean ProfileSyncServiceAndroid::IsSyncRequested( |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 443 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 444 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 445 | return sync_service_->IsSyncRequested(); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 446 | } |
| 447 | |
maxbogue | 85931a6 | 2015-06-12 22:58:01 | [diff] [blame] | 448 | jboolean ProfileSyncServiceAndroid::IsSyncActive(JNIEnv* env, jobject obj) { |
| 449 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 450 | return sync_service_->IsSyncActive(); |
| 451 | } |
| 452 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 453 | void ProfileSyncServiceAndroid::EnableEncryptEverything( |
| 454 | JNIEnv* env, jobject obj) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 455 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 456 | sync_service_->EnableEncryptEverything(); |
| 457 | } |
| 458 | |
| 459 | jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced( |
| 460 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 461 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 462 | return sync_prefs_->HasKeepEverythingSynced(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 463 | } |
| 464 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 465 | jboolean ProfileSyncServiceAndroid::HasUnrecoverableError( |
| 466 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 467 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 468 | return sync_service_->HasUnrecoverableError(); |
| 469 | } |
| 470 | |
| 471 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( |
| 472 | JNIEnv* env, jobject) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 473 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 474 | |
[email protected] | dbb9aa4 | 2013-12-23 20:08:21 | [diff] [blame] | 475 | scoped_ptr<base::DictionaryValue> about_info = |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 476 | sync_ui_util::ConstructAboutInformation(sync_service_); |
| 477 | std::string about_info_json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 478 | base::JSONWriter::Write(*about_info, &about_info_json); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 479 | |
| 480 | return ConvertUTF8ToJavaString(env, about_info_json); |
| 481 | } |
| 482 | |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 483 | jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest( |
| 484 | JNIEnv* env, jobject obj) { |
| 485 | // Use profile preferences here instead of SyncPrefs to avoid an extra |
| 486 | // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value |
| 487 | // to to base::Time. |
| 488 | return static_cast<jlong>( |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 489 | profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime)); |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 490 | } |
| 491 | |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 492 | void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest( |
| 493 | JNIEnv* env, |
| 494 | jobject obj, |
| 495 | jlong network_resources) { |
| 496 | syncer::NetworkResources* resources = |
| 497 | reinterpret_cast<syncer::NetworkResources*>(network_resources); |
| 498 | sync_service_->OverrideNetworkResourcesForTest( |
| 499 | make_scoped_ptr<syncer::NetworkResources>(resources)); |
| 500 | } |
| 501 | |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 502 | // static |
| 503 | jlong ProfileSyncServiceAndroid::ModelTypeSetToSelection( |
| 504 | syncer::ModelTypeSet types) { |
| 505 | jlong model_type_selection = 0; |
| 506 | if (types.Has(syncer::BOOKMARKS)) { |
| 507 | model_type_selection |= BOOKMARK; |
| 508 | } |
| 509 | if (types.Has(syncer::AUTOFILL)) { |
| 510 | model_type_selection |= AUTOFILL; |
| 511 | } |
| 512 | if (types.Has(syncer::AUTOFILL_PROFILE)) { |
| 513 | model_type_selection |= AUTOFILL_PROFILE; |
| 514 | } |
zea | e628112 | 2015-03-26 00:30:30 | [diff] [blame] | 515 | if (types.Has(syncer::AUTOFILL_WALLET_DATA)) { |
| 516 | model_type_selection |= AUTOFILL_WALLET; |
| 517 | } |
rouslan | 39765cf | 2015-05-22 16:57:10 | [diff] [blame] | 518 | if (types.Has(syncer::AUTOFILL_WALLET_METADATA)) { |
| 519 | model_type_selection |= AUTOFILL_WALLET_METADATA; |
| 520 | } |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 521 | if (types.Has(syncer::PASSWORDS)) { |
| 522 | model_type_selection |= PASSWORD; |
| 523 | } |
| 524 | if (types.Has(syncer::TYPED_URLS)) { |
| 525 | model_type_selection |= TYPED_URL; |
| 526 | } |
| 527 | if (types.Has(syncer::SESSIONS)) { |
| 528 | model_type_selection |= SESSION; |
| 529 | } |
| 530 | if (types.Has(syncer::HISTORY_DELETE_DIRECTIVES)) { |
| 531 | model_type_selection |= HISTORY_DELETE_DIRECTIVE; |
| 532 | } |
| 533 | if (types.Has(syncer::PROXY_TABS)) { |
| 534 | model_type_selection |= PROXY_TABS; |
| 535 | } |
| 536 | if (types.Has(syncer::FAVICON_IMAGES)) { |
| 537 | model_type_selection |= FAVICON_IMAGE; |
| 538 | } |
| 539 | if (types.Has(syncer::FAVICON_TRACKING)) { |
| 540 | model_type_selection |= FAVICON_TRACKING; |
| 541 | } |
| 542 | if (types.Has(syncer::DEVICE_INFO)) { |
| 543 | model_type_selection |= DEVICE_INFO; |
| 544 | } |
| 545 | if (types.Has(syncer::NIGORI)) { |
| 546 | model_type_selection |= NIGORI; |
| 547 | } |
| 548 | if (types.Has(syncer::EXPERIMENTS)) { |
| 549 | model_type_selection |= EXPERIMENTS; |
| 550 | } |
| 551 | if (types.Has(syncer::SUPERVISED_USER_SETTINGS)) { |
| 552 | model_type_selection |= SUPERVISED_USER_SETTING; |
| 553 | } |
bauerb | 4da3613 | 2014-12-26 19:53:13 | [diff] [blame] | 554 | if (types.Has(syncer::SUPERVISED_USER_WHITELISTS)) { |
| 555 | model_type_selection |= SUPERVISED_USER_WHITELIST; |
| 556 | } |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 557 | return model_type_selection; |
| 558 | } |
| 559 | |
| 560 | // static |
| 561 | std::string ProfileSyncServiceAndroid::ModelTypeSelectionToStringForTest( |
| 562 | jlong model_type_selection) { |
| 563 | ScopedJavaLocalRef<jstring> string = |
| 564 | Java_ProfileSyncService_modelTypeSelectionToStringForTest( |
| 565 | AttachCurrentThread(), model_type_selection); |
| 566 | return ConvertJavaStringToUTF8(string); |
| 567 | } |
| 568 | |
[email protected] | 332a8336 | 2013-03-26 08:45:55 | [diff] [blame] | 569 | // static |
| 570 | ProfileSyncServiceAndroid* |
| 571 | ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
| 572 | return reinterpret_cast<ProfileSyncServiceAndroid*>( |
| 573 | Java_ProfileSyncService_getProfileSyncServiceAndroid( |
| 574 | AttachCurrentThread(), base::android::GetApplicationContext())); |
| 575 | } |
| 576 | |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 577 | static jlong Init(JNIEnv* env, jobject obj) { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 578 | ProfileSyncServiceAndroid* profile_sync_service_android = |
| 579 | new ProfileSyncServiceAndroid(env, obj); |
| 580 | profile_sync_service_android->Init(); |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 581 | return reinterpret_cast<intptr_t>(profile_sync_service_android); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | // static |
| 585 | bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
| 586 | return RegisterNativesImpl(env); |
| 587 | } |