Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/android/preferences/pref_service_bridge.h" | 5 #include "chrome/browser/android/preferences/pref_service_bridge.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/android/build_info.h" | 11 #include "base/android/build_info.h" |
| 12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_array.h" | 13 #include "base/android/jni_array.h" |
| 14 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 15 #include "base/android/jni_weak_ref.h" | 15 #include "base/android/jni_weak_ref.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
| 20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h" | 23 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h" |
| 24 #include "chrome/browser/browsing_data/browsing_data_helper.h" | 24 #include "chrome/browser/browsing_data/browsing_data_helper.h" |
| 25 #include "chrome/browser/browsing_data/browsing_data_remover.h" | 25 #include "chrome/browser/browsing_data/browsing_data_remover.h" |
| 26 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" | 26 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" |
| 27 #include "chrome/browser/browsing_data/origin_filter_builder.h" | |
| 27 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 28 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 28 #include "chrome/browser/net/prediction_options.h" | 29 #include "chrome/browser/net/prediction_options.h" |
| 29 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 30 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 30 #include "chrome/browser/profiles/profile_manager.h" | 31 #include "chrome/browser/profiles/profile_manager.h" |
| 31 #include "chrome/browser/translate/chrome_translate_client.h" | 32 #include "chrome/browser/translate/chrome_translate_client.h" |
| 32 #include "chrome/browser/ui/android/android_about_app_info.h" | 33 #include "chrome/browser/ui/android/android_about_app_info.h" |
| 33 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
| 34 #include "chrome/grit/locale_settings.h" | 35 #include "chrome/grit/locale_settings.h" |
| 35 #include "components/content_settings/core/browser/host_content_settings_map.h" | 36 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 36 #include "components/content_settings/core/common/content_settings.h" | 37 #include "components/content_settings/core/common/content_settings.h" |
| 37 #include "components/content_settings/core/common/content_settings_pattern.h" | 38 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 38 #include "components/content_settings/core/common/pref_names.h" | 39 #include "components/content_settings/core/common/pref_names.h" |
| 39 #include "components/metrics/metrics_pref_names.h" | 40 #include "components/metrics/metrics_pref_names.h" |
| 40 #include "components/password_manager/core/common/password_manager_pref_names.h" | 41 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 41 #include "components/prefs/pref_service.h" | 42 #include "components/prefs/pref_service.h" |
| 42 #include "components/signin/core/common/signin_pref_names.h" | 43 #include "components/signin/core/common/signin_pref_names.h" |
| 43 #include "components/strings/grit/components_locale_settings.h" | 44 #include "components/strings/grit/components_locale_settings.h" |
| 44 #include "components/translate/core/browser/translate_prefs.h" | 45 #include "components/translate/core/browser/translate_prefs.h" |
| 45 #include "components/translate/core/common/translate_pref_names.h" | 46 #include "components/translate/core/common/translate_pref_names.h" |
| 46 #include "components/version_info/version_info.h" | 47 #include "components/version_info/version_info.h" |
| 47 #include "components/web_resource/web_resource_pref_names.h" | 48 #include "components/web_resource/web_resource_pref_names.h" |
| 48 #include "content/public/browser/browser_thread.h" | 49 #include "content/public/browser/browser_thread.h" |
| 49 #include "content/public/browser/user_metrics.h" | 50 #include "content/public/browser/user_metrics.h" |
| 50 #include "jni/PrefServiceBridge_jni.h" | 51 #include "jni/PrefServiceBridge_jni.h" |
| 51 #include "ui/base/l10n/l10n_util.h" | 52 #include "ui/base/l10n/l10n_util.h" |
| 53 #include "url/origin.h" | |
| 52 | 54 |
| 53 using base::android::AttachCurrentThread; | 55 using base::android::AttachCurrentThread; |
| 54 using base::android::CheckException; | 56 using base::android::CheckException; |
| 55 using base::android::ConvertJavaStringToUTF8; | 57 using base::android::ConvertJavaStringToUTF8; |
| 56 using base::android::ConvertUTF8ToJavaString; | 58 using base::android::ConvertUTF8ToJavaString; |
| 57 using base::android::ScopedJavaLocalRef; | 59 using base::android::ScopedJavaLocalRef; |
| 58 using base::android::ScopedJavaGlobalRef; | 60 using base::android::ScopedJavaGlobalRef; |
| 59 using content::BrowserThread; | 61 using content::BrowserThread; |
| 60 | 62 |
| 61 namespace { | 63 namespace { |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 | 491 |
| 490 Java_PrefServiceBridge_browsingDataCleared( | 492 Java_PrefServiceBridge_browsingDataCleared( |
| 491 env, weak_chrome_native_preferences_.get(env).obj()); | 493 env, weak_chrome_native_preferences_.get(env).obj()); |
| 492 } | 494 } |
| 493 | 495 |
| 494 private: | 496 private: |
| 495 JavaObjectWeakGlobalRef weak_chrome_native_preferences_; | 497 JavaObjectWeakGlobalRef weak_chrome_native_preferences_; |
| 496 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer> observer_; | 498 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer> observer_; |
| 497 }; | 499 }; |
| 498 | 500 |
| 501 // Redirects a BrowsingDataRemover completion callback back into the java | |
| 502 // callback. | |
| 503 class ClearBrowsingDataObserverWithCallback | |
|
newt (away)
2016/03/09 05:40:02
There's no reason to add this new class; just modi
| |
| 504 : public BrowsingDataRemover::Observer { | |
| 505 public: | |
| 506 ClearBrowsingDataObserverWithCallback( | |
| 507 JNIEnv* env, | |
| 508 jobject java_callback, | |
| 509 BrowsingDataRemover* browsing_data_remover) | |
| 510 : weak_java_callback_(env, java_callback), observer_(this) { | |
| 511 observer_.Add(browsing_data_remover); | |
| 512 } | |
| 513 | |
| 514 void OnBrowsingDataRemoverDone() override { | |
| 515 // We delete ourselves when done. | |
| 516 scoped_ptr<ClearBrowsingDataObserverWithCallback> auto_delete(this); | |
| 517 | |
| 518 JNIEnv* env = AttachCurrentThread(); | |
| 519 if (weak_java_callback_.get(env).is_null()) | |
|
newt (away)
2016/03/09 05:40:02
Incidentally, this use of the weak pointer is brok
| |
| 520 return; | |
| 521 | |
| 522 Java_OnClearBrowsingDataListener_onBrowsingDataCleared( | |
| 523 env, weak_java_callback_.get(env).obj()); | |
| 524 } | |
| 525 | |
| 526 private: | |
| 527 JavaObjectWeakGlobalRef weak_java_callback_; | |
|
newt (away)
2016/03/09 05:40:02
This shouldn't be weak; it should just be a regula
| |
| 528 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer> observer_; | |
| 529 }; | |
| 530 | |
| 499 } // namespace | 531 } // namespace |
| 500 | 532 |
| 501 static jboolean GetBrowsingDataDeletionPreference( | 533 static jboolean GetBrowsingDataDeletionPreference( |
| 502 JNIEnv* env, | 534 JNIEnv* env, |
| 503 const JavaParamRef<jobject>& obj, | 535 const JavaParamRef<jobject>& obj, |
| 504 jint data_type) { | 536 jint data_type) { |
| 505 DCHECK_GE(data_type, 0); | 537 DCHECK_GE(data_type, 0); |
| 506 DCHECK_LT(data_type, BrowsingDataType::NUM_TYPES); | 538 DCHECK_LT(data_type, BrowsingDataType::NUM_TYPES); |
| 507 | 539 |
| 508 // If there is no corresponding preference for this |data_type|, pretend | 540 // If there is no corresponding preference for this |data_type|, pretend |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 550 GetPrefService()->SetInteger(prefs::kDeleteTimePeriod, time_period); | 582 GetPrefService()->SetInteger(prefs::kDeleteTimePeriod, time_period); |
| 551 } | 583 } |
| 552 | 584 |
| 553 static void ClearBrowsingData(JNIEnv* env, | 585 static void ClearBrowsingData(JNIEnv* env, |
| 554 const JavaParamRef<jobject>& obj, | 586 const JavaParamRef<jobject>& obj, |
| 555 const JavaParamRef<jintArray>& data_types) { | 587 const JavaParamRef<jintArray>& data_types) { |
| 556 BrowsingDataRemover* browsing_data_remover = | 588 BrowsingDataRemover* browsing_data_remover = |
| 557 BrowsingDataRemoverFactory::GetForBrowserContext(GetOriginalProfile()); | 589 BrowsingDataRemoverFactory::GetForBrowserContext(GetOriginalProfile()); |
| 558 // ClearBrowsingDataObserver deletes itself when |browsing_data_remover| is | 590 // ClearBrowsingDataObserver deletes itself when |browsing_data_remover| is |
| 559 // done. | 591 // done. |
| 560 new ClearBrowsingDataObserver(env, obj, browsing_data_remover); | 592 new ClearBrowsingDataObserverWithCallback(env, obj, browsing_data_remover); |
| 561 | 593 |
| 562 std::vector<int> data_types_vector; | 594 std::vector<int> data_types_vector; |
| 563 base::android::JavaIntArrayToIntVector(env, data_types, &data_types_vector); | 595 base::android::JavaIntArrayToIntVector(env, data_types, &data_types_vector); |
| 564 | 596 |
| 565 int remove_mask = 0; | 597 int remove_mask = 0; |
| 566 for (const int data_type : data_types_vector) { | 598 for (const int data_type : data_types_vector) { |
| 567 switch (static_cast<BrowsingDataType>(data_type)) { | 599 switch (static_cast<BrowsingDataType>(data_type)) { |
| 568 case HISTORY: | 600 case HISTORY: |
| 569 remove_mask |= BrowsingDataRemover::REMOVE_HISTORY; | 601 remove_mask |= BrowsingDataRemover::REMOVE_HISTORY; |
| 570 break; | 602 break; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 590 } | 622 } |
| 591 } | 623 } |
| 592 | 624 |
| 593 int period_selected = GetPrefService()->GetInteger(prefs::kDeleteTimePeriod); | 625 int period_selected = GetPrefService()->GetInteger(prefs::kDeleteTimePeriod); |
| 594 browsing_data_remover->Remove( | 626 browsing_data_remover->Remove( |
| 595 BrowsingDataRemover::Period( | 627 BrowsingDataRemover::Period( |
| 596 static_cast<BrowsingDataRemover::TimePeriod>(period_selected)), | 628 static_cast<BrowsingDataRemover::TimePeriod>(period_selected)), |
| 597 remove_mask, BrowsingDataHelper::UNPROTECTED_WEB); | 629 remove_mask, BrowsingDataHelper::UNPROTECTED_WEB); |
| 598 } | 630 } |
| 599 | 631 |
| 632 static void ClearBrowsingDataExcludingOrigins( | |
|
newt (away)
2016/03/09 05:40:02
As Martin mentioned, please combine this method wi
| |
| 633 JNIEnv* env, | |
| 634 const JavaParamRef<jclass>& clazz, | |
| 635 const JavaParamRef<jobject>& java_callback, | |
| 636 const JavaParamRef<jintArray>& data_types, | |
| 637 const JavaParamRef<jobjectArray>& excluding_origins) { | |
|
newt (away)
2016/03/09 05:40:02
call this "excluded_origins" or just "origins"
| |
| 638 BrowsingDataRemover* browsing_data_remover = | |
| 639 BrowsingDataRemoverFactory::GetForBrowserContext(GetOriginalProfile()); | |
| 640 // ClearBrowsingDataObserver deletes itself when |browsing_data_remover| is | |
| 641 // done. | |
| 642 new ClearBrowsingDataObserver(env, java_callback.obj(), | |
| 643 browsing_data_remover); | |
| 644 | |
| 645 std::vector<int> data_types_vector; | |
| 646 base::android::JavaIntArrayToIntVector(env, data_types, &data_types_vector); | |
| 647 | |
| 648 int remove_mask = 0; | |
| 649 for (const int data_type : data_types_vector) { | |
| 650 switch (static_cast<BrowsingDataType>(data_type)) { | |
| 651 case HISTORY: | |
| 652 remove_mask |= BrowsingDataRemover::REMOVE_HISTORY; | |
| 653 break; | |
| 654 case CACHE: | |
| 655 remove_mask |= BrowsingDataRemover::REMOVE_CACHE; | |
| 656 break; | |
| 657 case COOKIES: | |
| 658 remove_mask |= BrowsingDataRemover::REMOVE_COOKIES; | |
| 659 remove_mask |= BrowsingDataRemover::REMOVE_SITE_DATA; | |
| 660 break; | |
| 661 case PASSWORDS: | |
| 662 remove_mask |= BrowsingDataRemover::REMOVE_PASSWORDS; | |
| 663 break; | |
| 664 case FORM_DATA: | |
| 665 remove_mask |= BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 666 break; | |
| 667 case BOOKMARKS: | |
| 668 // Bookmarks are deleted separately on the Java side. | |
| 669 NOTREACHED(); | |
| 670 break; | |
| 671 case NUM_TYPES: | |
| 672 NOTREACHED(); | |
| 673 } | |
| 674 } | |
| 675 | |
| 676 OriginFilterBuilder filter_builder(OriginFilterBuilder::BLACKLIST); | |
| 677 jsize origin_count = env->GetArrayLength(excluding_origins.obj()); | |
| 678 for (jsize i = 0; i < origin_count; ++i) { | |
| 679 jstring url = | |
| 680 (jstring)env->GetObjectArrayElement(excluding_origins.obj(), i); | |
| 681 filter_builder.AddOrigin(url::Origin(ConvertJavaStringToUTF8(env, url))) | |
| 682 } | |
| 683 | |
| 684 /* Commented out, as we don't have the filterbuilder hookup yet. | |
| 685 int period_selected = | |
| 686 GetPrefService()->GetInteger(prefs::kDeleteTimePeriod); | |
| 687 browsing_data_remover->Remove( | |
| 688 BrowsingDataRemover::Period( | |
| 689 static_cast<BrowsingDataRemover::TimePeriod>(period_selected)), | |
| 690 remove_mask, filter_builder, BrowsingDataHelper::UNPROTECTED_WEB); | |
| 691 */ | |
| 692 } | |
| 693 | |
| 600 static jboolean CanDeleteBrowsingHistory(JNIEnv* env, | 694 static jboolean CanDeleteBrowsingHistory(JNIEnv* env, |
| 601 const JavaParamRef<jobject>& obj) { | 695 const JavaParamRef<jobject>& obj) { |
| 602 return GetPrefService()->GetBoolean(prefs::kAllowDeletingBrowserHistory); | 696 return GetPrefService()->GetBoolean(prefs::kAllowDeletingBrowserHistory); |
| 603 } | 697 } |
| 604 | 698 |
| 605 static void SetAllowCookiesEnabled(JNIEnv* env, | 699 static void SetAllowCookiesEnabled(JNIEnv* env, |
| 606 const JavaParamRef<jobject>& obj, | 700 const JavaParamRef<jobject>& obj, |
| 607 jboolean allow) { | 701 jboolean allow) { |
| 608 HostContentSettingsMap* host_content_settings_map = | 702 HostContentSettingsMap* host_content_settings_map = |
| 609 HostContentSettingsMapFactory::GetForProfile(GetOriginalProfile()); | 703 HostContentSettingsMapFactory::GetForProfile(GetOriginalProfile()); |
| (...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1036 | 1130 |
| 1037 return ConvertJavaStringToUTF8(android_permission); | 1131 return ConvertJavaStringToUTF8(android_permission); |
| 1038 } | 1132 } |
| 1039 | 1133 |
| 1040 static void SetSupervisedUserId(JNIEnv* env, | 1134 static void SetSupervisedUserId(JNIEnv* env, |
| 1041 const JavaParamRef<jobject>& obj, | 1135 const JavaParamRef<jobject>& obj, |
| 1042 const JavaParamRef<jstring>& pref) { | 1136 const JavaParamRef<jstring>& pref) { |
| 1043 GetPrefService()->SetString(prefs::kSupervisedUserId, | 1137 GetPrefService()->SetString(prefs::kSupervisedUserId, |
| 1044 ConvertJavaStringToUTF8(env, pref)); | 1138 ConvertJavaStringToUTF8(env, pref)); |
| 1045 } | 1139 } |
| OLD | NEW |