OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/profile_sync_service_android.h" | 5 #include "chrome/browser/sync/profile_sync_service_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" |
8 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/i18n/time_formatting.h" | 11 #include "base/i18n/time_formatting.h" |
11 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
15 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
(...skipping 19 matching lines...) Expand all Loading... |
37 | 38 |
38 using base::android::AttachCurrentThread; | 39 using base::android::AttachCurrentThread; |
39 using base::android::CheckException; | 40 using base::android::CheckException; |
40 using base::android::ConvertJavaStringToUTF8; | 41 using base::android::ConvertJavaStringToUTF8; |
41 using base::android::ConvertUTF8ToJavaString; | 42 using base::android::ConvertUTF8ToJavaString; |
42 using base::android::ScopedJavaLocalRef; | 43 using base::android::ScopedJavaLocalRef; |
43 using content::BrowserThread; | 44 using content::BrowserThread; |
44 | 45 |
45 namespace { | 46 namespace { |
46 | 47 |
47 // This enum contains the list of sync ModelTypes that Android can register for | |
48 // invalidations for. | |
49 // | |
50 // A Java counterpart will be generated for this enum. | |
51 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.sync | |
52 enum ModelTypeSelection { | |
53 AUTOFILL = 1 << 0, | |
54 BOOKMARK = 1 << 1, | |
55 PASSWORD = 1 << 2, | |
56 SESSION = 1 << 3, | |
57 TYPED_URL = 1 << 4, | |
58 AUTOFILL_PROFILE = 1 << 5, | |
59 HISTORY_DELETE_DIRECTIVE = 1 << 6, | |
60 PROXY_TABS = 1 << 7, | |
61 FAVICON_IMAGE = 1 << 8, | |
62 FAVICON_TRACKING = 1 << 9, | |
63 NIGORI = 1 << 10, | |
64 DEVICE_INFO = 1 << 11, | |
65 EXPERIMENTS = 1 << 12, | |
66 SUPERVISED_USER_SETTING = 1 << 13, | |
67 SUPERVISED_USER_WHITELIST = 1 << 14, | |
68 AUTOFILL_WALLET = 1 << 15, | |
69 AUTOFILL_WALLET_METADATA = 1 << 16, | |
70 PREFERENCE = 1 << 17, | |
71 PRIORITY_PREFERENCE = 1 << 18, | |
72 }; | |
73 | |
74 // Native callback for the JNI GetAllNodes method. When | 48 // Native callback for the JNI GetAllNodes method. When |
75 // ProfileSyncService::GetAllNodes completes, this method is called and the | 49 // ProfileSyncService::GetAllNodes completes, this method is called and the |
76 // results are sent to the Java callback. | 50 // results are sent to the Java callback. |
77 void NativeGetAllNodesCallback( | 51 void NativeGetAllNodesCallback( |
78 const base::android::ScopedJavaGlobalRef<jobject>& callback, | 52 const base::android::ScopedJavaGlobalRef<jobject>& callback, |
79 scoped_ptr<base::ListValue> result) { | 53 scoped_ptr<base::ListValue> result) { |
80 JNIEnv* env = base::android::AttachCurrentThread(); | 54 JNIEnv* env = base::android::AttachCurrentThread(); |
81 std::string json_string; | 55 std::string json_string; |
82 if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { | 56 if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { |
83 DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; | 57 DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 } | 328 } |
355 | 329 |
356 jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone( | 330 jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone( |
357 JNIEnv* env, jobject) { | 331 JNIEnv* env, jobject) { |
358 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 332 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
359 syncer::SyncStatus status; | 333 syncer::SyncStatus status; |
360 bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); | 334 bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); |
361 return is_status_valid && !status.keystore_migration_time.is_null(); | 335 return is_status_valid && !status.keystore_migration_time.is_null(); |
362 } | 336 } |
363 | 337 |
364 jlong ProfileSyncServiceAndroid::GetActiveDataTypes( | 338 ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetActiveDataTypes( |
365 JNIEnv* env, jobject obj) { | 339 JNIEnv* env, jobject obj) { |
366 syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); | 340 syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes(); |
367 types.PutAll(syncer::ControlTypes()); | 341 types.PutAll(syncer::ControlTypes()); |
368 return ModelTypeSetToSelection(types); | 342 return ModelTypeSetToJavaIntArray(env, types); |
369 } | 343 } |
370 | 344 |
371 jlong ProfileSyncServiceAndroid::GetPreferredDataTypes( | 345 ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetPreferredDataTypes( |
372 JNIEnv* env, jobject obj) { | 346 JNIEnv* env, jobject obj) { |
373 syncer::ModelTypeSet types = sync_service_->GetPreferredDataTypes(); | 347 syncer::ModelTypeSet types = sync_service_->GetPreferredDataTypes(); |
374 types.PutAll(syncer::ControlTypes()); | 348 types.PutAll(syncer::ControlTypes()); |
375 return ModelTypeSetToSelection(types); | 349 return ModelTypeSetToJavaIntArray(env, types); |
376 } | 350 } |
377 | 351 |
378 void ProfileSyncServiceAndroid::SetPreferredDataTypes( | 352 void ProfileSyncServiceAndroid::SetPreferredDataTypes( |
379 JNIEnv* env, jobject obj, | 353 JNIEnv* env, jobject obj, |
380 jboolean sync_everything, | 354 jboolean sync_everything, |
381 jlong model_type_selection) { | 355 jintArray model_type_array) { |
382 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 356 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 357 std::vector<int> types_vector; |
| 358 base::android::JavaIntArrayToIntVector(env, model_type_array, &types_vector); |
383 syncer::ModelTypeSet types; | 359 syncer::ModelTypeSet types; |
384 // Note: only user selectable types should be included here. | 360 for (size_t i = 0; i < types_vector.size(); i++) { |
385 if (model_type_selection & AUTOFILL) | 361 types.Put(static_cast<syncer::ModelType>(types_vector[i])); |
386 types.Put(syncer::AUTOFILL); | 362 } |
387 if (model_type_selection & BOOKMARK) | 363 types.RetainAll(syncer::UserSelectableTypes()); |
388 types.Put(syncer::BOOKMARKS); | |
389 if (model_type_selection & PASSWORD) | |
390 types.Put(syncer::PASSWORDS); | |
391 if (model_type_selection & PROXY_TABS) | |
392 types.Put(syncer::PROXY_TABS); | |
393 if (model_type_selection & TYPED_URL) | |
394 types.Put(syncer::TYPED_URLS); | |
395 if (model_type_selection & PREFERENCE) | |
396 types.Put(syncer::PREFERENCES); | |
397 DCHECK(syncer::UserSelectableTypes().HasAll(types)); | |
398 sync_service_->OnUserChoseDatatypes(sync_everything, types); | 364 sync_service_->OnUserChoseDatatypes(sync_everything, types); |
399 } | 365 } |
400 | 366 |
401 void ProfileSyncServiceAndroid::SetSetupInProgress( | 367 void ProfileSyncServiceAndroid::SetSetupInProgress( |
402 JNIEnv* env, jobject obj, jboolean in_progress) { | 368 JNIEnv* env, jobject obj, jboolean in_progress) { |
403 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 369 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
404 sync_service_->SetSetupInProgress(in_progress); | 370 sync_service_->SetSetupInProgress(in_progress); |
405 } | 371 } |
406 | 372 |
407 void ProfileSyncServiceAndroid::SetSyncSetupCompleted( | 373 void ProfileSyncServiceAndroid::SetSyncSetupCompleted( |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 JNIEnv* env, | 436 JNIEnv* env, |
471 jobject obj, | 437 jobject obj, |
472 jlong network_resources) { | 438 jlong network_resources) { |
473 syncer::NetworkResources* resources = | 439 syncer::NetworkResources* resources = |
474 reinterpret_cast<syncer::NetworkResources*>(network_resources); | 440 reinterpret_cast<syncer::NetworkResources*>(network_resources); |
475 sync_service_->OverrideNetworkResourcesForTest( | 441 sync_service_->OverrideNetworkResourcesForTest( |
476 make_scoped_ptr<syncer::NetworkResources>(resources)); | 442 make_scoped_ptr<syncer::NetworkResources>(resources)); |
477 } | 443 } |
478 | 444 |
479 // static | 445 // static |
480 jlong ProfileSyncServiceAndroid::ModelTypeSetToSelection( | 446 ScopedJavaLocalRef<jintArray> |
| 447 ProfileSyncServiceAndroid::ModelTypeSetToJavaIntArray( |
| 448 JNIEnv* env, |
481 syncer::ModelTypeSet types) { | 449 syncer::ModelTypeSet types) { |
482 jlong model_type_selection = 0; | 450 std::vector<int> type_vector; |
483 if (types.Has(syncer::BOOKMARKS)) { | 451 for (syncer::ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { |
484 model_type_selection |= BOOKMARK; | 452 type_vector.push_back(it.Get()); |
485 } | 453 } |
486 if (types.Has(syncer::AUTOFILL)) { | 454 return base::android::ToJavaIntArray(env, type_vector); |
487 model_type_selection |= AUTOFILL; | |
488 } | |
489 if (types.Has(syncer::AUTOFILL_PROFILE)) { | |
490 model_type_selection |= AUTOFILL_PROFILE; | |
491 } | |
492 if (types.Has(syncer::AUTOFILL_WALLET_DATA)) { | |
493 model_type_selection |= AUTOFILL_WALLET; | |
494 } | |
495 if (types.Has(syncer::AUTOFILL_WALLET_METADATA)) { | |
496 model_type_selection |= AUTOFILL_WALLET_METADATA; | |
497 } | |
498 if (types.Has(syncer::PASSWORDS)) { | |
499 model_type_selection |= PASSWORD; | |
500 } | |
501 if (types.Has(syncer::PREFERENCES)) { | |
502 model_type_selection |= PREFERENCE; | |
503 } | |
504 if (types.Has(syncer::PRIORITY_PREFERENCES)) { | |
505 model_type_selection |= PRIORITY_PREFERENCE; | |
506 } | |
507 if (types.Has(syncer::TYPED_URLS)) { | |
508 model_type_selection |= TYPED_URL; | |
509 } | |
510 if (types.Has(syncer::SESSIONS)) { | |
511 model_type_selection |= SESSION; | |
512 } | |
513 if (types.Has(syncer::HISTORY_DELETE_DIRECTIVES)) { | |
514 model_type_selection |= HISTORY_DELETE_DIRECTIVE; | |
515 } | |
516 if (types.Has(syncer::PROXY_TABS)) { | |
517 model_type_selection |= PROXY_TABS; | |
518 } | |
519 if (types.Has(syncer::FAVICON_IMAGES)) { | |
520 model_type_selection |= FAVICON_IMAGE; | |
521 } | |
522 if (types.Has(syncer::FAVICON_TRACKING)) { | |
523 model_type_selection |= FAVICON_TRACKING; | |
524 } | |
525 if (types.Has(syncer::DEVICE_INFO)) { | |
526 model_type_selection |= DEVICE_INFO; | |
527 } | |
528 if (types.Has(syncer::NIGORI)) { | |
529 model_type_selection |= NIGORI; | |
530 } | |
531 if (types.Has(syncer::EXPERIMENTS)) { | |
532 model_type_selection |= EXPERIMENTS; | |
533 } | |
534 if (types.Has(syncer::SUPERVISED_USER_SETTINGS)) { | |
535 model_type_selection |= SUPERVISED_USER_SETTING; | |
536 } | |
537 if (types.Has(syncer::SUPERVISED_USER_WHITELISTS)) { | |
538 model_type_selection |= SUPERVISED_USER_WHITELIST; | |
539 } | |
540 return model_type_selection; | |
541 } | 455 } |
542 | 456 |
543 // static | 457 // static |
544 std::string ProfileSyncServiceAndroid::ModelTypeSelectionToStringForTest( | |
545 jlong model_type_selection) { | |
546 ScopedJavaLocalRef<jstring> string = | |
547 Java_ProfileSyncService_modelTypeSelectionToStringForTest( | |
548 AttachCurrentThread(), model_type_selection); | |
549 return ConvertJavaStringToUTF8(string); | |
550 } | |
551 | |
552 // static | |
553 ProfileSyncServiceAndroid* | 458 ProfileSyncServiceAndroid* |
554 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { | 459 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
555 return reinterpret_cast<ProfileSyncServiceAndroid*>( | 460 return reinterpret_cast<ProfileSyncServiceAndroid*>( |
556 Java_ProfileSyncService_getProfileSyncServiceAndroid( | 461 Java_ProfileSyncService_getProfileSyncServiceAndroid( |
557 AttachCurrentThread(), base::android::GetApplicationContext())); | 462 AttachCurrentThread(), base::android::GetApplicationContext())); |
558 } | 463 } |
559 | 464 |
560 static jlong Init(JNIEnv* env, jobject obj) { | 465 static jlong Init(JNIEnv* env, jobject obj) { |
561 ProfileSyncServiceAndroid* profile_sync_service_android = | 466 ProfileSyncServiceAndroid* profile_sync_service_android = |
562 new ProfileSyncServiceAndroid(env, obj); | 467 new ProfileSyncServiceAndroid(env, obj); |
563 profile_sync_service_android->Init(); | 468 profile_sync_service_android->Init(); |
564 return reinterpret_cast<intptr_t>(profile_sync_service_android); | 469 return reinterpret_cast<intptr_t>(profile_sync_service_android); |
565 } | 470 } |
566 | 471 |
567 // static | 472 // static |
568 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { | 473 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
569 return RegisterNativesImpl(env); | 474 return RegisterNativesImpl(env); |
570 } | 475 } |
OLD | NEW |