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