[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 | |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
dcheng | 0569a0e | 2016-04-14 17:37:05 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 12 | #include "base/android/jni_android.h" |
maxbogue | 47c7504 | 2015-08-12 18:05:01 | [diff] [blame] | 13 | #include "base/android/jni_array.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 14 | #include "base/android/jni_string.h" |
| 15 | #include "base/bind.h" |
| 16 | #include "base/i18n/time_formatting.h" |
| 17 | #include "base/json/json_writer.h" |
| 18 | #include "base/logging.h" |
dcheng | 0569a0e | 2016-04-14 17:37:05 | [diff] [blame] | 19 | #include "base/memory/ptr_util.h" |
[email protected] | e309f31 | 2013-06-07 21:50:08 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
[email protected] | cc86ccfe | 2013-06-28 00:10:50 | [diff] [blame] | 21 | #include "base/time/time.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 22 | #include "chrome/browser/browser_process.h" |
| 23 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 24 | #include "chrome/browser/signin/signin_manager_factory.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 25 | #include "chrome/browser/sync/profile_sync_service_factory.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 26 | #include "chrome/browser/sync/sync_ui_util.h" |
maxbogue | 93150a5d | 2015-08-31 21:08:32 | [diff] [blame] | 27 | #include "chrome/common/channel_info.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 28 | #include "chrome/grit/generated_resources.h" |
blundell | 7282b51 | 2015-11-09 07:21:11 | [diff] [blame] | 29 | #include "components/browser_sync/browser/profile_sync_service.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 30 | #include "components/prefs/pref_service.h" |
[email protected] | 7fbd3b1 | 2014-04-01 11:19:16 | [diff] [blame] | 31 | #include "components/signin/core/browser/signin_manager.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame^] | 32 | #include "components/sync/core/network_resources.h" |
| 33 | #include "components/sync/core/read_transaction.h" |
maxbogue | 93150a5d | 2015-08-31 21:08:32 | [diff] [blame] | 34 | #include "components/sync_driver/about_sync_util.h" |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 35 | #include "components/sync_driver/pref_names.h" |
| 36 | #include "components/sync_driver/sync_prefs.h" |
[email protected] | dfe6a9b | 2014-01-31 21:37:55 | [diff] [blame] | 37 | #include "content/public/browser/browser_thread.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 38 | #include "google/cacheinvalidation/types.pb.h" |
| 39 | #include "google_apis/gaia/gaia_constants.h" |
| 40 | #include "google_apis/gaia/google_service_auth_error.h" |
sdefresne | 78b2e91 | 2016-01-12 08:46:23 | [diff] [blame] | 41 | #include "grit/components_strings.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 42 | #include "jni/ProfileSyncService_jni.h" |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 43 | #include "ui/base/l10n/l10n_util.h" |
| 44 | |
| 45 | using base::android::AttachCurrentThread; |
| 46 | using base::android::CheckException; |
| 47 | using base::android::ConvertJavaStringToUTF8; |
| 48 | using base::android::ConvertUTF8ToJavaString; |
| 49 | using base::android::ScopedJavaLocalRef; |
| 50 | using content::BrowserThread; |
| 51 | |
| 52 | namespace { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 53 | |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 54 | // Native callback for the JNI GetAllNodes method. When |
| 55 | // ProfileSyncService::GetAllNodes completes, this method is called and the |
| 56 | // results are sent to the Java callback. |
| 57 | void NativeGetAllNodesCallback( |
| 58 | const base::android::ScopedJavaGlobalRef<jobject>& callback, |
dcheng | 0569a0e | 2016-04-14 17:37:05 | [diff] [blame] | 59 | std::unique_ptr<base::ListValue> result) { |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 60 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 61 | std::string json_string; |
| 62 | if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { |
| 63 | DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; |
| 64 | json_string = std::string(); |
| 65 | } |
| 66 | |
| 67 | ScopedJavaLocalRef<jstring> java_json_string = |
| 68 | ConvertUTF8ToJavaString(env, json_string); |
| 69 | Java_ProfileSyncService_onGetAllNodesResult(env, |
| 70 | callback.obj(), |
| 71 | java_json_string.obj()); |
| 72 | } |
| 73 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 74 | ScopedJavaLocalRef<jintArray> ModelTypeSetToJavaIntArray( |
| 75 | JNIEnv* env, |
| 76 | syncer::ModelTypeSet types) { |
| 77 | std::vector<int> type_vector; |
| 78 | for (syncer::ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { |
| 79 | type_vector.push_back(it.Get()); |
| 80 | } |
| 81 | return base::android::ToJavaIntArray(env, type_vector); |
| 82 | } |
| 83 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 84 | } // namespace |
| 85 | |
| 86 | ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj) |
| 87 | : profile_(NULL), |
| 88 | sync_service_(NULL), |
| 89 | weak_java_profile_sync_service_(env, obj) { |
| 90 | if (g_browser_process == NULL || |
| 91 | g_browser_process->profile_manager() == NULL) { |
| 92 | NOTREACHED() << "Browser process or profile manager not initialized"; |
| 93 | return; |
| 94 | } |
| 95 | |
[email protected] | dd547a2 | 2013-12-12 18:37:41 | [diff] [blame] | 96 | profile_ = ProfileManager::GetActiveUserProfile(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 97 | if (profile_ == NULL) { |
| 98 | NOTREACHED() << "Sync Init: Profile not found."; |
| 99 | return; |
| 100 | } |
| 101 | |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 102 | sync_prefs_.reset(new sync_driver::SyncPrefs(profile_->GetPrefs())); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 103 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 104 | sync_service_ = |
| 105 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 106 | } |
| 107 | |
cullinan | 3b803e2 | 2015-12-02 01:31:24 | [diff] [blame] | 108 | bool ProfileSyncServiceAndroid::Init() { |
| 109 | if (sync_service_) { |
| 110 | sync_service_->AddObserver(this); |
maxbogue | 1a4f0090 | 2015-12-15 21:39:40 | [diff] [blame] | 111 | sync_service_->SetPlatformSyncAllowedProvider( |
| 112 | base::Bind(&ProfileSyncServiceAndroid::IsSyncAllowedByAndroid, |
| 113 | base::Unretained(this))); |
cullinan | 3b803e2 | 2015-12-02 01:31:24 | [diff] [blame] | 114 | return true; |
| 115 | } else { |
| 116 | return false; |
| 117 | } |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 118 | } |
| 119 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 120 | ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() { |
maxbogue | 1a4f0090 | 2015-12-15 21:39:40 | [diff] [blame] | 121 | if (sync_service_) { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 122 | sync_service_->RemoveObserver(this); |
maxbogue | 1a4f0090 | 2015-12-15 21:39:40 | [diff] [blame] | 123 | sync_service_->SetPlatformSyncAllowedProvider( |
| 124 | ProfileSyncService::PlatformSyncAllowedProvider()); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 128 | void ProfileSyncServiceAndroid::OnStateChanged() { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 129 | // Notify the java world that our sync state has changed. |
| 130 | JNIEnv* env = AttachCurrentThread(); |
| 131 | Java_ProfileSyncService_syncStateChanged( |
| 132 | env, weak_java_profile_sync_service_.get(env).obj()); |
| 133 | } |
| 134 | |
maxbogue | 1a4f0090 | 2015-12-15 21:39:40 | [diff] [blame] | 135 | bool ProfileSyncServiceAndroid::IsSyncAllowedByAndroid() const { |
| 136 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 137 | JNIEnv* env = AttachCurrentThread(); |
| 138 | return Java_ProfileSyncService_isMasterSyncEnabled( |
| 139 | env, weak_java_profile_sync_service_.get(env).obj()); |
| 140 | } |
| 141 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 142 | // Pure ProfileSyncService calls. |
zea | b1b09b7 | 2015-05-01 17:56:40 | [diff] [blame] | 143 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 144 | jboolean ProfileSyncServiceAndroid::IsSyncRequested( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 145 | JNIEnv* env, |
| 146 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 147 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 148 | return sync_service_->IsSyncRequested(); |
zea | b1b09b7 | 2015-05-01 17:56:40 | [diff] [blame] | 149 | } |
| 150 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 151 | void ProfileSyncServiceAndroid::RequestStart(JNIEnv* env, |
| 152 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 153 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | cd3fad6b | 2015-06-09 00:44:53 | [diff] [blame] | 154 | sync_service_->RequestStart(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 155 | } |
| 156 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 157 | void ProfileSyncServiceAndroid::RequestStop(JNIEnv* env, |
| 158 | const JavaParamRef<jobject>&) { |
maxbogue | f5c53a2d | 2015-07-08 20:47:29 | [diff] [blame] | 159 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 160 | sync_service_->RequestStop(ProfileSyncService::KEEP_DATA); |
| 161 | } |
| 162 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 163 | void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, |
| 164 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 165 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 166 | DCHECK(profile_); |
maxbogue | 699262c9 | 2015-06-19 19:24:58 | [diff] [blame] | 167 | sync_service_->RequestStop(ProfileSyncService::CLEAR_DATA); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 168 | } |
| 169 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 170 | jboolean ProfileSyncServiceAndroid::IsSyncActive( |
| 171 | JNIEnv* env, |
| 172 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 173 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 174 | return sync_service_->IsSyncActive(); |
| 175 | } |
| 176 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 177 | jboolean ProfileSyncServiceAndroid::IsBackendInitialized( |
| 178 | JNIEnv* env, |
| 179 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 180 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 181 | return sync_service_->IsBackendInitialized(); |
| 182 | } |
| 183 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 184 | void ProfileSyncServiceAndroid::SetSetupInProgress( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 185 | JNIEnv* env, |
| 186 | const JavaParamRef<jobject>& obj, |
| 187 | jboolean in_progress) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 188 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
tommycli | f3a1551e | 2016-06-21 18:11:23 | [diff] [blame] | 189 | if (in_progress) { |
| 190 | sync_blocker_ = sync_service_->GetSetupInProgressHandle(); |
| 191 | } else { |
| 192 | sync_blocker_.reset(); |
| 193 | } |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 194 | } |
| 195 | |
maxbogue | fe00952a | 2016-01-19 19:02:01 | [diff] [blame] | 196 | jboolean ProfileSyncServiceAndroid::IsFirstSetupComplete( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 197 | JNIEnv* env, |
| 198 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 199 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | fe00952a | 2016-01-19 19:02:01 | [diff] [blame] | 200 | return sync_service_->IsFirstSetupComplete(); |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 201 | } |
| 202 | |
maxbogue | fe00952a | 2016-01-19 19:02:01 | [diff] [blame] | 203 | void ProfileSyncServiceAndroid::SetFirstSetupComplete( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 204 | JNIEnv* env, |
| 205 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 206 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | fe00952a | 2016-01-19 19:02:01 | [diff] [blame] | 207 | sync_service_->SetFirstSetupComplete(); |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetActiveDataTypes( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 211 | JNIEnv* env, |
| 212 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 213 | syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); |
| 214 | return ModelTypeSetToJavaIntArray(env, types); |
| 215 | } |
| 216 | |
| 217 | ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetPreferredDataTypes( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 218 | JNIEnv* env, |
| 219 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 220 | syncer::ModelTypeSet types = sync_service_->GetPreferredDataTypes(); |
| 221 | return ModelTypeSetToJavaIntArray(env, types); |
| 222 | } |
| 223 | |
| 224 | void ProfileSyncServiceAndroid::SetPreferredDataTypes( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 225 | JNIEnv* env, |
| 226 | const JavaParamRef<jobject>& obj, |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 227 | jboolean sync_everything, |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 228 | const JavaParamRef<jintArray>& model_type_array) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 229 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 230 | std::vector<int> types_vector; |
| 231 | base::android::JavaIntArrayToIntVector(env, model_type_array, &types_vector); |
| 232 | syncer::ModelTypeSet types; |
| 233 | for (size_t i = 0; i < types_vector.size(); i++) { |
| 234 | types.Put(static_cast<syncer::ModelType>(types_vector[i])); |
| 235 | } |
| 236 | sync_service_->OnUserChoseDatatypes(sync_everything, types); |
| 237 | } |
| 238 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 239 | jboolean ProfileSyncServiceAndroid::IsCryptographerReady( |
| 240 | JNIEnv* env, |
| 241 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 242 | syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 243 | return sync_service_->IsCryptographerReady(&trans); |
| 244 | } |
| 245 | |
| 246 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingAllowed( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 247 | JNIEnv* env, |
| 248 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 249 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 250 | return sync_service_->IsEncryptEverythingAllowed(); |
| 251 | } |
| 252 | |
| 253 | jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 254 | JNIEnv* env, |
| 255 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 256 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 257 | return sync_service_->IsEncryptEverythingEnabled(); |
| 258 | } |
| 259 | |
| 260 | void ProfileSyncServiceAndroid::EnableEncryptEverything( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 261 | JNIEnv* env, |
| 262 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 263 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 264 | sync_service_->EnableEncryptEverything(); |
| 265 | } |
| 266 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 267 | jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 268 | JNIEnv* env, |
| 269 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 270 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 271 | return sync_service_->IsPassphraseRequiredForDecryption(); |
| 272 | } |
| 273 | |
| 274 | jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 275 | JNIEnv* env, |
| 276 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 277 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 278 | return sync_service_->IsUsingSecondaryPassphrase(); |
| 279 | } |
| 280 | |
maxbogue | 774f317 | 2015-11-18 16:26:58 | [diff] [blame] | 281 | ScopedJavaLocalRef<jbyteArray> |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 282 | ProfileSyncServiceAndroid::GetCustomPassphraseKey( |
| 283 | JNIEnv* env, |
| 284 | const JavaParamRef<jobject>& obj) { |
maxbogue | 774f317 | 2015-11-18 16:26:58 | [diff] [blame] | 285 | std::string key = sync_service_->GetCustomPassphraseKey(); |
| 286 | return base::android::ToJavaByteArray( |
| 287 | env, reinterpret_cast<const uint8_t*>(key.data()), key.size()); |
| 288 | } |
| 289 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 290 | jint ProfileSyncServiceAndroid::GetPassphraseType( |
| 291 | JNIEnv* env, |
| 292 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 293 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 294 | return sync_service_->GetPassphraseType(); |
| 295 | } |
| 296 | |
| 297 | void ProfileSyncServiceAndroid::SetEncryptionPassphrase( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 298 | JNIEnv* env, |
| 299 | const JavaParamRef<jobject>& obj, |
| 300 | const JavaParamRef<jstring>& passphrase) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 301 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 302 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 303 | sync_service_->SetEncryptionPassphrase(key, ProfileSyncService::EXPLICIT); |
| 304 | } |
| 305 | |
| 306 | jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 307 | JNIEnv* env, |
| 308 | const JavaParamRef<jobject>& obj, |
| 309 | const JavaParamRef<jstring>& passphrase) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 310 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 311 | std::string key = ConvertJavaStringToUTF8(env, passphrase); |
| 312 | return sync_service_->SetDecryptionPassphrase(key); |
| 313 | } |
| 314 | |
| 315 | jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 316 | JNIEnv* env, |
| 317 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 318 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 319 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 320 | return !passphrase_time.is_null(); |
| 321 | } |
| 322 | |
| 323 | jlong ProfileSyncServiceAndroid::GetExplicitPassphraseTime( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 324 | JNIEnv* env, |
| 325 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 326 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 327 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
| 328 | return passphrase_time.ToJavaTime(); |
| 329 | } |
| 330 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 331 | void ProfileSyncServiceAndroid::FlushDirectory(JNIEnv* env, |
| 332 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 333 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | d4470ca | 2014-09-30 23:38:11 | [diff] [blame] | 334 | sync_service_->FlushDirectory(); |
| 335 | } |
| 336 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 337 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 338 | JNIEnv* env, |
| 339 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 340 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 341 | DCHECK(profile_); |
[email protected] | 9e8df05 | 2013-09-18 10:47:18 | [diff] [blame] | 342 | std::string status(sync_service_->QuerySyncStatusSummaryString()); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 343 | return ConvertUTF8ToJavaString(env, status); |
| 344 | } |
| 345 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 346 | void ProfileSyncServiceAndroid::GetAllNodes( |
| 347 | JNIEnv* env, |
| 348 | const JavaParamRef<jobject>& obj, |
| 349 | const JavaParamRef<jobject>& callback) { |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 350 | base::android::ScopedJavaGlobalRef<jobject> java_callback; |
| 351 | java_callback.Reset(env, callback); |
| 352 | |
dcheng | 0569a0e | 2016-04-14 17:37:05 | [diff] [blame] | 353 | base::Callback<void(std::unique_ptr<base::ListValue>)> native_callback = |
pvalenzuela | ff27ca20 | 2015-05-20 23:34:21 | [diff] [blame] | 354 | base::Bind(&NativeGetAllNodesCallback, java_callback); |
| 355 | sync_service_->GetAllNodes(native_callback); |
| 356 | } |
| 357 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 358 | jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, |
| 359 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 360 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 361 | return sync_service_->GetAuthError().state(); |
| 362 | } |
| 363 | |
| 364 | jboolean ProfileSyncServiceAndroid::HasUnrecoverableError( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 365 | JNIEnv* env, |
| 366 | const JavaParamRef<jobject>&) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 367 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 368 | return sync_service_->HasUnrecoverableError(); |
| 369 | } |
| 370 | |
| 371 | // Pure SyncPrefs calls. |
| 372 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 373 | jboolean ProfileSyncServiceAndroid::IsPassphrasePrompted( |
| 374 | JNIEnv* env, |
| 375 | const JavaParamRef<jobject>& obj) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 376 | return sync_prefs_->IsPassphrasePrompted(); |
| 377 | } |
| 378 | |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 379 | void ProfileSyncServiceAndroid::SetPassphrasePrompted( |
| 380 | JNIEnv* env, |
| 381 | const JavaParamRef<jobject>& obj, |
| 382 | jboolean prompted) { |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 383 | sync_prefs_->SetPassphrasePrompted(prompted); |
| 384 | } |
| 385 | |
maxbogue | c7f232d | 2015-07-29 19:25:40 | [diff] [blame] | 386 | void ProfileSyncServiceAndroid::SetSyncSessionsId( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 387 | JNIEnv* env, |
| 388 | const JavaParamRef<jobject>& obj, |
| 389 | const JavaParamRef<jstring>& tag) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 390 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 391 | DCHECK(profile_); |
| 392 | std::string machine_tag = ConvertJavaStringToUTF8(env, tag); |
[email protected] | aa5b2ece | 2013-05-17 00:37:36 | [diff] [blame] | 393 | sync_prefs_->SetSyncSessionsGUID(machine_tag); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 394 | } |
| 395 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 396 | jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 397 | JNIEnv* env, |
| 398 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 399 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 400 | return sync_prefs_->HasKeepEverythingSynced(); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 401 | } |
| 402 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 403 | // UI string getters. |
maxbogue | c021ddcc | 2014-10-09 16:21:24 | [diff] [blame] | 404 | |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 405 | ScopedJavaLocalRef<jstring> |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 406 | ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText( |
| 407 | JNIEnv* env, |
| 408 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 409 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 410 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 411 | base::string16 passphrase_time_str = |
| 412 | base::TimeFormatShortDate(passphrase_time); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 413 | return base::android::ConvertUTF16ToJavaString(env, |
| 414 | l10n_util::GetStringFUTF16( |
| 415 | IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE, |
| 416 | passphrase_time_str)); |
| 417 | } |
| 418 | |
| 419 | ScopedJavaLocalRef<jstring> |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 420 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText( |
| 421 | JNIEnv* env, |
| 422 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 423 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 424 | base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 425 | base::string16 passphrase_time_str = |
| 426 | base::TimeFormatShortDate(passphrase_time); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 427 | return base::android::ConvertUTF16ToJavaString(env, |
| 428 | l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE, |
| 429 | passphrase_time_str)); |
| 430 | } |
| 431 | |
| 432 | ScopedJavaLocalRef<jstring> |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 433 | ProfileSyncServiceAndroid::GetCurrentSignedInAccountText( |
| 434 | JNIEnv* env, |
| 435 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 436 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 437 | const std::string& sync_username = |
bzanotti | 1b5be29 | 2015-08-31 12:10:05 | [diff] [blame] | 438 | SigninManagerFactory::GetForProfile(profile_) |
| 439 | ->GetAuthenticatedAccountInfo() |
| 440 | .email; |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 441 | return base::android::ConvertUTF16ToJavaString(env, |
| 442 | l10n_util::GetStringFUTF16( |
| 443 | IDS_SYNC_ACCOUNT_SYNCING_TO_USER, |
[email protected] | f911df5 | 2013-12-24 23:24:23 | [diff] [blame] | 444 | base::ASCIIToUTF16(sync_username))); |
[email protected] | 4faa9de0 | 2013-03-26 10:17:20 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | ScopedJavaLocalRef<jstring> |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 448 | ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( |
| 449 | JNIEnv* env, |
| 450 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 451 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 452 | return ConvertUTF8ToJavaString( |
| 453 | env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); |
| 454 | } |
| 455 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 456 | // Functionality only available for testing purposes. |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 457 | |
| 458 | ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 459 | JNIEnv* env, |
| 460 | const JavaParamRef<jobject>&) { |
anujk.sharma | 8619448 | 2015-04-23 06:38:23 | [diff] [blame] | 461 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 462 | |
dcheng | 0569a0e | 2016-04-14 17:37:05 | [diff] [blame] | 463 | std::unique_ptr<base::DictionaryValue> about_info = |
droger | 5144c43 | 2015-09-17 10:53:11 | [diff] [blame] | 464 | sync_driver::sync_ui_util::ConstructAboutInformation( |
| 465 | sync_service_, sync_service_->signin(), chrome::GetChannel()); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 466 | std::string about_info_json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 467 | base::JSONWriter::Write(*about_info, &about_info_json); |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 468 | |
| 469 | return ConvertUTF8ToJavaString(env, about_info_json); |
| 470 | } |
| 471 | |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 472 | jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest( |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 473 | JNIEnv* env, |
| 474 | const JavaParamRef<jobject>& obj) { |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 475 | // Use profile preferences here instead of SyncPrefs to avoid an extra |
| 476 | // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value |
| 477 | // to to base::Time. |
| 478 | return static_cast<jlong>( |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 479 | profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime)); |
[email protected] | ed4d705 | 2013-09-04 19:01:37 | [diff] [blame] | 480 | } |
| 481 | |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 482 | void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest( |
| 483 | JNIEnv* env, |
torne | 0a4a3ab1 | 2015-12-10 19:45:52 | [diff] [blame] | 484 | const JavaParamRef<jobject>& obj, |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 485 | jlong network_resources) { |
| 486 | syncer::NetworkResources* resources = |
| 487 | reinterpret_cast<syncer::NetworkResources*>(network_resources); |
| 488 | sync_service_->OverrideNetworkResourcesForTest( |
dcheng | 0569a0e | 2016-04-14 17:37:05 | [diff] [blame] | 489 | base::WrapUnique<syncer::NetworkResources>(resources)); |
[email protected] | be21aaa | 2014-08-21 21:26:38 | [diff] [blame] | 490 | } |
| 491 | |
bauerb | dab6c6f | 2014-08-28 09:20:23 | [diff] [blame] | 492 | // static |
[email protected] | 332a8336 | 2013-03-26 08:45:55 | [diff] [blame] | 493 | ProfileSyncServiceAndroid* |
| 494 | ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
| 495 | return reinterpret_cast<ProfileSyncServiceAndroid*>( |
maxbogue | 89bee1d86 | 2015-08-26 21:00:07 | [diff] [blame] | 496 | Java_ProfileSyncService_getProfileSyncServiceAndroid( |
| 497 | AttachCurrentThread())); |
[email protected] | 332a8336 | 2013-03-26 08:45:55 | [diff] [blame] | 498 | } |
| 499 | |
maxbogue | 28baa96 | 2015-10-01 17:21:38 | [diff] [blame] | 500 | // static |
| 501 | bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
| 502 | return RegisterNativesImpl(env); |
| 503 | } |
| 504 | |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 505 | static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 506 | ProfileSyncServiceAndroid* profile_sync_service_android = |
| 507 | new ProfileSyncServiceAndroid(env, obj); |
cullinan | 3b803e2 | 2015-12-02 01:31:24 | [diff] [blame] | 508 | if (profile_sync_service_android->Init()) { |
| 509 | return reinterpret_cast<intptr_t>(profile_sync_service_android); |
| 510 | } else { |
| 511 | delete profile_sync_service_android; |
| 512 | return 0; |
| 513 | } |
[email protected] | 9049a7b | 2013-03-07 22:22:03 | [diff] [blame] | 514 | } |