blob: 3f5b517d81efa4c116f71de9167c681eb55b3f8e [file] [log] [blame]
[email protected]9049a7b2013-03-07 22:22:031// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/sync/profile_sync_service_android.h"
6
7#include "base/android/jni_android.h"
8#include "base/android/jni_string.h"
9#include "base/bind.h"
10#include "base/i18n/time_formatting.h"
11#include "base/json/json_writer.h"
12#include "base/logging.h"
13#include "base/memory/scoped_ptr.h"
[email protected]34ee0512013-03-12 17:49:5414#include "base/prefs/pref_service.h"
[email protected]e309f312013-06-07 21:50:0815#include "base/strings/utf_string_conversions.h"
[email protected]cc86ccfe2013-06-28 00:10:5016#include "base/time/time.h"
[email protected]9049a7b2013-03-07 22:22:0317#include "chrome/browser/browser_process.h"
[email protected]dcc8fbc2013-07-12 00:54:0918#include "chrome/browser/chrome_notification_types.h"
[email protected]9049a7b2013-03-07 22:22:0319#include "chrome/browser/profiles/profile_manager.h"
[email protected]9049a7b2013-03-07 22:22:0320#include "chrome/browser/signin/signin_manager_factory.h"
[email protected]9049a7b2013-03-07 22:22:0321#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]9049a7b2013-03-07 22:22:0324#include "chrome/browser/sync/sync_ui_util.h"
[email protected]001bbfdc2014-07-17 19:28:4625#include "components/invalidation/object_id_invalidation_map.h"
[email protected]7fbd3b12014-04-01 11:19:1626#include "components/signin/core/browser/signin_manager.h"
[email protected]34f54052014-03-20 21:33:4027#include "components/sync_driver/pref_names.h"
28#include "components/sync_driver/sync_prefs.h"
[email protected]dfe6a9b2014-01-31 21:37:5529#include "content/public/browser/browser_thread.h"
[email protected]9049a7b2013-03-07 22:22:0330#include "content/public/browser/notification_service.h"
[email protected]9a3de842013-06-12 20:16:5131#include "content/public/browser/notification_source.h"
[email protected]9049a7b2013-03-07 22:22:0332#include "google/cacheinvalidation/types.pb.h"
33#include "google_apis/gaia/gaia_constants.h"
34#include "google_apis/gaia/google_service_auth_error.h"
35#include "grit/generated_resources.h"
36#include "jni/ProfileSyncService_jni.h"
[email protected]be21aaa2014-08-21 21:26:3837#include "sync/internal_api/public/network_resources.h"
[email protected]9049a7b2013-03-07 22:22:0338#include "sync/internal_api/public/read_transaction.h"
39#include "ui/base/l10n/l10n_util.h"
40
41using base::android::AttachCurrentThread;
42using base::android::CheckException;
43using base::android::ConvertJavaStringToUTF8;
44using base::android::ConvertUTF8ToJavaString;
45using base::android::ScopedJavaLocalRef;
46using content::BrowserThread;
47
48namespace {
[email protected]9049a7b2013-03-07 22:22:0349
50enum {
51#define DEFINE_MODEL_TYPE_SELECTION(name,value) name = value,
52#include "chrome/browser/sync/profile_sync_service_model_type_selection_android.h"
53#undef DEFINE_MODEL_TYPE_SELECTION
54};
55
56} // namespace
57
58ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj)
59 : profile_(NULL),
60 sync_service_(NULL),
61 weak_java_profile_sync_service_(env, obj) {
62 if (g_browser_process == NULL ||
63 g_browser_process->profile_manager() == NULL) {
64 NOTREACHED() << "Browser process or profile manager not initialized";
65 return;
66 }
67
[email protected]dd547a22013-12-12 18:37:4168 profile_ = ProfileManager::GetActiveUserProfile();
[email protected]9049a7b2013-03-07 22:22:0369 if (profile_ == NULL) {
70 NOTREACHED() << "Sync Init: Profile not found.";
71 return;
72 }
73
[email protected]34f54052014-03-20 21:33:4074 sync_prefs_.reset(new sync_driver::SyncPrefs(profile_->GetPrefs()));
[email protected]aa5b2ece2013-05-17 00:37:3675
[email protected]9049a7b2013-03-07 22:22:0376 sync_service_ =
77 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
78 DCHECK(sync_service_);
79}
80
81void ProfileSyncServiceAndroid::Init() {
82 sync_service_->AddObserver(this);
[email protected]9049a7b2013-03-07 22:22:0383}
84
85void ProfileSyncServiceAndroid::RemoveObserver() {
86 if (sync_service_->HasObserver(this)) {
87 sync_service_->RemoveObserver(this);
88 }
89}
90
91ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() {
92 RemoveObserver();
93}
94
95void ProfileSyncServiceAndroid::SendNudgeNotification(
[email protected]2fd6b7032013-09-11 00:20:2596 int object_source,
[email protected]9049a7b2013-03-07 22:22:0397 const std::string& str_object_id,
98 int64 version,
99 const std::string& state) {
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101
102 // TODO(nileshagrawal): Merge this with ChromeInvalidationClient::Invalidate.
103 // Construct the ModelTypeStateMap and send it over with the notification.
[email protected]c56ab9022013-07-12 03:10:08104 invalidation::ObjectId object_id(
[email protected]2fd6b7032013-09-11 00:20:25105 object_source,
[email protected]c56ab9022013-07-12 03:10:08106 str_object_id);
[email protected]163d0632013-10-04 03:51:01107 syncer::ObjectIdInvalidationMap object_ids_with_states;
[email protected]2fd6b7032013-09-11 00:20:25108 if (version == ipc::invalidation::Constants::UNKNOWN) {
[email protected]163d0632013-10-04 03:51:01109 object_ids_with_states.Insert(
110 syncer::Invalidation::InitUnknownVersion(object_id));
[email protected]2fd6b7032013-09-11 00:20:25111 } else {
[email protected]c56ab9022013-07-12 03:10:08112 ObjectIdVersionMap::iterator it =
113 max_invalidation_versions_.find(object_id);
[email protected]9049a7b2013-03-07 22:22:03114 if ((it != max_invalidation_versions_.end()) &&
115 (version <= it->second)) {
116 DVLOG(1) << "Dropping redundant invalidation with version " << version;
117 return;
118 }
[email protected]c56ab9022013-07-12 03:10:08119 max_invalidation_versions_[object_id] = version;
[email protected]163d0632013-10-04 03:51:01120 object_ids_with_states.Insert(
121 syncer::Invalidation::Init(object_id, version, state));
[email protected]9049a7b2013-03-07 22:22:03122 }
123
[email protected]9049a7b2013-03-07 22:22:03124 content::NotificationService::current()->Notify(
125 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
126 content::Source<Profile>(profile_),
[email protected]c56ab9022013-07-12 03:10:08127 content::Details<const syncer::ObjectIdInvalidationMap>(
128 &object_ids_with_states));
[email protected]9049a7b2013-03-07 22:22:03129}
130
[email protected]9049a7b2013-03-07 22:22:03131void ProfileSyncServiceAndroid::OnStateChanged() {
[email protected]9049a7b2013-03-07 22:22:03132 // Notify the java world that our sync state has changed.
133 JNIEnv* env = AttachCurrentThread();
134 Java_ProfileSyncService_syncStateChanged(
135 env, weak_java_profile_sync_service_.get(env).obj());
136}
137
[email protected]9049a7b2013-03-07 22:22:03138void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
140 // Don't need to do anything if we're already enabled.
[email protected]aa5b2ece2013-05-17 00:37:36141 if (sync_prefs_->IsStartSuppressed())
[email protected]9049a7b2013-03-07 22:22:03142 sync_service_->UnsuppressAndStart();
143 else
144 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled";
145}
146
147void ProfileSyncServiceAndroid::DisableSync(JNIEnv* env, jobject) {
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]41e0f372013-06-05 19:58:42149 // Don't need to do anything if we're already disabled.
150 if (!sync_prefs_->IsStartSuppressed()) {
151 sync_service_->StopAndSuppress();
152 } else {
153 DVLOG(2)
154 << "Ignoring call to DisableSync() because sync is already disabled";
155 }
[email protected]9049a7b2013-03-07 22:22:03156}
157
[email protected]73da8582013-07-11 10:46:36158void ProfileSyncServiceAndroid::SignInSync(JNIEnv* env, jobject) {
[email protected]9049a7b2013-03-07 22:22:03159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
160 // Just return if sync already has everything it needs to start up (sync
161 // should start up automatically as long as it has credentials). This can
162 // happen normally if (for example) the user closes and reopens the sync
163 // settings window quickly during initial startup.
164 if (sync_service_->IsSyncEnabledAndLoggedIn() &&
[email protected]b67c18c2013-06-13 23:52:03165 sync_service_->IsOAuthRefreshTokenAvailable() &&
[email protected]9049a7b2013-03-07 22:22:03166 sync_service_->HasSyncSetupCompleted()) {
167 return;
168 }
169
[email protected]9049a7b2013-03-07 22:22:03170 // Enable sync (if we don't have credentials yet, this will enable sync but
171 // will not start it up - sync will start once credentials arrive).
172 sync_service_->UnsuppressAndStart();
173}
174
175void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177 DCHECK(profile_);
178 sync_service_->DisableForUser();
179
[email protected]9049a7b2013-03-07 22:22:03180 // Need to clear suppress start flag manually
[email protected]aa5b2ece2013-05-17 00:37:36181 sync_prefs_->SetStartSuppressed(false);
[email protected]9049a7b2013-03-07 22:22:03182}
183
184ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary(
185 JNIEnv* env, jobject) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
187 DCHECK(profile_);
[email protected]9e8df052013-09-18 10:47:18188 std::string status(sync_service_->QuerySyncStatusSummaryString());
[email protected]9049a7b2013-03-07 22:22:03189 return ConvertUTF8ToJavaString(env, status);
190}
191
192jboolean ProfileSyncServiceAndroid::SetSyncSessionsId(
193 JNIEnv* env, jobject obj, jstring tag) {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
195 DCHECK(profile_);
196 std::string machine_tag = ConvertJavaStringToUTF8(env, tag);
[email protected]aa5b2ece2013-05-17 00:37:36197 sync_prefs_->SetSyncSessionsGUID(machine_tag);
[email protected]9049a7b2013-03-07 22:22:03198 return true;
199}
200
201jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
203 return sync_service_->GetAuthError().state();
204}
205
206jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled(
207 JNIEnv* env, jobject) {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
209 return sync_service_->EncryptEverythingEnabled();
210}
211
212jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) {
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
214 return sync_service_->sync_initialized();
215}
216
217jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress(
218 JNIEnv* env, jobject) {
219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
220 return sync_service_->FirstSetupInProgress();
221}
222
223jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) {
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
225 return sync_service_->IsPassphraseRequired();
226}
227
228jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption(
229 JNIEnv* env, jobject obj) {
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
231 // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for
232 // a passphrase if cryptographer has any pending keys.
233 if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) {
234 return !IsCryptographerReady(env, obj);
235 }
236 if (sync_service_->IsPassphraseRequiredForDecryption()) {
237 // Passwords datatype should never prompt for a passphrase, except when
238 // user is using a custom passphrase. Do not prompt for a passphrase if
239 // passwords are the only encrypted datatype. This prevents a temporary
240 // notification for passphrase when PSS has not completed configuring
241 // DataTypeManager, after configuration password datatype shall be disabled.
242 const syncer::ModelTypeSet encrypted_types =
243 sync_service_->GetEncryptedDataTypes();
[email protected]1f51ad92013-06-07 02:30:24244 return !encrypted_types.Equals(syncer::ModelTypeSet(syncer::PASSWORDS));
[email protected]9049a7b2013-03-07 22:22:03245 }
246 return false;
247}
248
249jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForExternalType(
250 JNIEnv* env, jobject) {
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
252 return
253 sync_service_->passphrase_required_reason() == syncer::REASON_DECRYPTION;
254}
255
256jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase(
257 JNIEnv* env, jobject) {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
259 return sync_service_->IsUsingSecondaryPassphrase();
260}
261
262jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase(
263 JNIEnv* env, jobject obj, jstring passphrase) {
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
265 std::string key = ConvertJavaStringToUTF8(env, passphrase);
266 return sync_service_->SetDecryptionPassphrase(key);
267}
268
269void ProfileSyncServiceAndroid::SetEncryptionPassphrase(
270 JNIEnv* env, jobject obj, jstring passphrase, jboolean is_gaia) {
271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
272 std::string key = ConvertJavaStringToUTF8(env, passphrase);
273 sync_service_->SetEncryptionPassphrase(
274 key,
275 is_gaia ? ProfileSyncService::IMPLICIT : ProfileSyncService::EXPLICIT);
276}
277
278jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) {
279 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
280 return sync_service_->IsCryptographerReady(&trans);
281}
282
283jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) {
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
285 return sync_service_->GetPassphraseType();
286}
287
288jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime(
289 JNIEnv* env, jobject) {
290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
291 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
292 return !passphrase_time.is_null();
293}
294
295ScopedJavaLocalRef<jstring>
296 ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText(
297 JNIEnv* env, jobject) {
298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
299 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
[email protected]a04db822013-12-11 19:14:40300 base::string16 passphrase_time_str =
301 base::TimeFormatShortDate(passphrase_time);
[email protected]9049a7b2013-03-07 22:22:03302 return base::android::ConvertUTF16ToJavaString(env,
303 l10n_util::GetStringFUTF16(
304 IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE,
305 passphrase_time_str));
306}
307
308ScopedJavaLocalRef<jstring>
309 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText(
310 JNIEnv* env, jobject) {
311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
312 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
[email protected]a04db822013-12-11 19:14:40313 base::string16 passphrase_time_str =
314 base::TimeFormatShortDate(passphrase_time);
[email protected]9049a7b2013-03-07 22:22:03315 return base::android::ConvertUTF16ToJavaString(env,
316 l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE,
317 passphrase_time_str));
318}
319
320ScopedJavaLocalRef<jstring>
[email protected]4faa9de02013-03-26 10:17:20321 ProfileSyncServiceAndroid::GetCurrentSignedInAccountText(
322 JNIEnv* env, jobject) {
323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
324 const std::string& sync_username =
325 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername();
326 return base::android::ConvertUTF16ToJavaString(env,
327 l10n_util::GetStringFUTF16(
328 IDS_SYNC_ACCOUNT_SYNCING_TO_USER,
[email protected]f911df52013-12-24 23:24:23329 base::ASCIIToUTF16(sync_username)));
[email protected]4faa9de02013-03-26 10:17:20330}
331
332ScopedJavaLocalRef<jstring>
[email protected]9049a7b2013-03-07 22:22:03333 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText(
334 JNIEnv* env, jobject) {
335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
336 return ConvertUTF8ToJavaString(
337 env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY));
338}
339
340jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone(
341 JNIEnv* env, jobject) {
342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
343 syncer::SyncStatus status;
344 bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status);
345 return is_status_valid && !status.keystore_migration_time.is_null();
346}
347
[email protected]6805b602013-03-27 21:18:18348jlong ProfileSyncServiceAndroid::GetEnabledDataTypes(JNIEnv* env,
349 jobject obj) {
350 jlong model_type_selection = 0;
[email protected]464d74a2013-06-04 07:36:46351 syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes();
[email protected]6805b602013-03-27 21:18:18352 types.PutAll(syncer::ControlTypes());
353 if (types.Has(syncer::BOOKMARKS)) {
354 model_type_selection |= BOOKMARK;
355 }
356 if (types.Has(syncer::AUTOFILL)) {
357 model_type_selection |= AUTOFILL;
358 }
359 if (types.Has(syncer::AUTOFILL_PROFILE)) {
360 model_type_selection |= AUTOFILL_PROFILE;
361 }
362 if (types.Has(syncer::PASSWORDS)) {
363 model_type_selection |= PASSWORD;
364 }
365 if (types.Has(syncer::TYPED_URLS)) {
366 model_type_selection |= TYPED_URL;
367 }
368 if (types.Has(syncer::SESSIONS)) {
369 model_type_selection |= SESSION;
370 }
371 if (types.Has(syncer::HISTORY_DELETE_DIRECTIVES)) {
372 model_type_selection |= HISTORY_DELETE_DIRECTIVE;
373 }
374 if (types.Has(syncer::PROXY_TABS)) {
375 model_type_selection |= PROXY_TABS;
376 }
377 if (types.Has(syncer::FAVICON_IMAGES)) {
378 model_type_selection |= FAVICON_IMAGE;
379 }
380 if (types.Has(syncer::FAVICON_TRACKING)) {
381 model_type_selection |= FAVICON_TRACKING;
382 }
383 if (types.Has(syncer::DEVICE_INFO)) {
384 model_type_selection |= DEVICE_INFO;
385 }
386 if (types.Has(syncer::NIGORI)) {
387 model_type_selection |= NIGORI;
388 }
389 if (types.Has(syncer::EXPERIMENTS)) {
390 model_type_selection |= EXPERIMENTS;
391 }
392 return model_type_selection;
393}
394
[email protected]9049a7b2013-03-07 22:22:03395void ProfileSyncServiceAndroid::SetPreferredDataTypes(
396 JNIEnv* env, jobject obj,
397 jboolean sync_everything,
398 jlong model_type_selection) {
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
400 syncer::ModelTypeSet types;
[email protected]6805b602013-03-27 21:18:18401 // Note: only user selectable types should be included here.
[email protected]9049a7b2013-03-07 22:22:03402 if (model_type_selection & AUTOFILL)
403 types.Put(syncer::AUTOFILL);
404 if (model_type_selection & BOOKMARK)
405 types.Put(syncer::BOOKMARKS);
406 if (model_type_selection & PASSWORD)
407 types.Put(syncer::PASSWORDS);
[email protected]88dfd0e2013-04-02 20:55:02408 if (model_type_selection & PROXY_TABS)
409 types.Put(syncer::PROXY_TABS);
[email protected]9049a7b2013-03-07 22:22:03410 if (model_type_selection & TYPED_URL)
411 types.Put(syncer::TYPED_URLS);
[email protected]6805b602013-03-27 21:18:18412 DCHECK(syncer::UserSelectableTypes().HasAll(types));
[email protected]9049a7b2013-03-07 22:22:03413 sync_service_->OnUserChoseDatatypes(sync_everything, types);
414}
415
416void ProfileSyncServiceAndroid::SetSetupInProgress(
417 JNIEnv* env, jobject obj, jboolean in_progress) {
418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
419 sync_service_->SetSetupInProgress(in_progress);
420}
421
422void ProfileSyncServiceAndroid::SetSyncSetupCompleted(
423 JNIEnv* env, jobject obj) {
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
425 sync_service_->SetSyncSetupCompleted();
426}
427
428jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted(
429 JNIEnv* env, jobject obj) {
430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
431 return sync_service_->HasSyncSetupCompleted();
432}
433
[email protected]aa5b2ece2013-05-17 00:37:36434jboolean ProfileSyncServiceAndroid::IsStartSuppressed(
435 JNIEnv* env, jobject obj) {
436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
437 return sync_prefs_->IsStartSuppressed();
438}
439
[email protected]9049a7b2013-03-07 22:22:03440void ProfileSyncServiceAndroid::EnableEncryptEverything(
441 JNIEnv* env, jobject obj) {
442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
443 sync_service_->EnableEncryptEverything();
444}
445
446jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced(
447 JNIEnv* env, jobject) {
448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]aa5b2ece2013-05-17 00:37:36449 return sync_prefs_->HasKeepEverythingSynced();
[email protected]9049a7b2013-03-07 22:22:03450}
451
[email protected]9049a7b2013-03-07 22:22:03452jboolean ProfileSyncServiceAndroid::HasUnrecoverableError(
453 JNIEnv* env, jobject) {
454 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
455 return sync_service_->HasUnrecoverableError();
456}
457
458ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest(
459 JNIEnv* env, jobject) {
460 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
461
[email protected]dbb9aa42013-12-23 20:08:21462 scoped_ptr<base::DictionaryValue> about_info =
[email protected]9049a7b2013-03-07 22:22:03463 sync_ui_util::ConstructAboutInformation(sync_service_);
464 std::string about_info_json;
465 base::JSONWriter::Write(about_info.get(), &about_info_json);
466
467 return ConvertUTF8ToJavaString(env, about_info_json);
468}
469
[email protected]ed4d7052013-09-04 19:01:37470jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest(
471 JNIEnv* env, jobject obj) {
472 // Use profile preferences here instead of SyncPrefs to avoid an extra
473 // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value
474 // to to base::Time.
475 return static_cast<jlong>(
[email protected]34f54052014-03-20 21:33:40476 profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime));
[email protected]ed4d7052013-09-04 19:01:37477}
478
[email protected]be21aaa2014-08-21 21:26:38479void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest(
480 JNIEnv* env,
481 jobject obj,
482 jlong network_resources) {
483 syncer::NetworkResources* resources =
484 reinterpret_cast<syncer::NetworkResources*>(network_resources);
485 sync_service_->OverrideNetworkResourcesForTest(
486 make_scoped_ptr<syncer::NetworkResources>(resources));
487}
488
[email protected]9049a7b2013-03-07 22:22:03489void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env,
490 jobject obj,
[email protected]2fd6b7032013-09-11 00:20:25491 jint objectSource,
[email protected]9049a7b2013-03-07 22:22:03492 jstring objectId,
493 jlong version,
494 jstring state) {
495 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]2fd6b7032013-09-11 00:20:25496 SendNudgeNotification(objectSource, ConvertJavaStringToUTF8(env, objectId),
497 version, ConvertJavaStringToUTF8(env, state));
[email protected]9049a7b2013-03-07 22:22:03498}
499
[email protected]8f63ef22013-07-17 02:09:37500void ProfileSyncServiceAndroid::NudgeSyncerForAllTypes(JNIEnv* env,
501 jobject obj) {
502 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
503 syncer::ObjectIdInvalidationMap object_ids_with_states;
504 content::NotificationService::current()->Notify(
505 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
506 content::Source<Profile>(profile_),
507 content::Details<const syncer::ObjectIdInvalidationMap>(
508 &object_ids_with_states));
509}
510
[email protected]332a83362013-03-26 08:45:55511// static
512ProfileSyncServiceAndroid*
513 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() {
514 return reinterpret_cast<ProfileSyncServiceAndroid*>(
515 Java_ProfileSyncService_getProfileSyncServiceAndroid(
516 AttachCurrentThread(), base::android::GetApplicationContext()));
517}
518
[email protected]d557b6042013-11-21 16:34:31519static jlong Init(JNIEnv* env, jobject obj) {
[email protected]9049a7b2013-03-07 22:22:03520 ProfileSyncServiceAndroid* profile_sync_service_android =
521 new ProfileSyncServiceAndroid(env, obj);
522 profile_sync_service_android->Init();
[email protected]d557b6042013-11-21 16:34:31523 return reinterpret_cast<intptr_t>(profile_sync_service_android);
[email protected]9049a7b2013-03-07 22:22:03524}
525
526// static
527bool ProfileSyncServiceAndroid::Register(JNIEnv* env) {
528 return RegisterNativesImpl(env);
529}