OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/website_preference_bridge.h" | 5 #include "chrome/browser/android/preferences/website_preference_bridge.h" |
6 | 6 |
7 #include <algorithm> | |
8 | |
7 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
9 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
12 #include "base/macros.h" | 14 #include "base/macros.h" |
13 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" | 15 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" |
14 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h" | 16 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h" |
15 #include "chrome/browser/browsing_data/cookies_tree_model.h" | 17 #include "chrome/browser/browsing_data/cookies_tree_model.h" |
16 #include "chrome/browser/browsing_data/local_data_container.h" | 18 #include "chrome/browser/browsing_data/local_data_container.h" |
17 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 19 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 20 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
19 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 21 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
20 #include "chrome/browser/content_settings/web_site_settings_uma_util.h" | 22 #include "chrome/browser/content_settings/web_site_settings_uma_util.h" |
23 #include "chrome/browser/engagement/site_engagement_service.h" | |
21 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 24 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
22 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/profiles/profile_manager.h" | 26 #include "chrome/browser/profiles/profile_manager.h" |
24 #include "chrome/browser/storage/storage_info_fetcher.h" | 27 #include "chrome/browser/storage/storage_info_fetcher.h" |
25 #include "components/content_settings/core/browser/cookie_settings.h" | 28 #include "components/content_settings/core/browser/cookie_settings.h" |
26 #include "components/content_settings/core/browser/host_content_settings_map.h" | 29 #include "components/content_settings/core/browser/host_content_settings_map.h" |
27 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/storage_partition.h" | 31 #include "content/public/browser/storage_partition.h" |
29 #include "jni/WebsitePreferenceBridge_jni.h" | 32 #include "jni/WebsitePreferenceBridge_jni.h" |
30 #include "storage/browser/quota/quota_manager.h" | 33 #include "storage/browser/quota/quota_manager.h" |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
649 }; | 652 }; |
650 | 653 |
651 class LocalStorageInfoReadyCallback { | 654 class LocalStorageInfoReadyCallback { |
652 public: | 655 public: |
653 explicit LocalStorageInfoReadyCallback(const JavaRef<jobject>& java_callback) | 656 explicit LocalStorageInfoReadyCallback(const JavaRef<jobject>& java_callback) |
654 : env_(base::android::AttachCurrentThread()), | 657 : env_(base::android::AttachCurrentThread()), |
655 java_callback_(java_callback) { | 658 java_callback_(java_callback) { |
656 } | 659 } |
657 | 660 |
658 void OnLocalStorageModelInfoLoaded( | 661 void OnLocalStorageModelInfoLoaded( |
662 Profile* profile, | |
659 const std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>& | 663 const std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>& |
660 local_storage_info) { | 664 local_storage_info) { |
661 ScopedJavaLocalRef<jobject> map = | 665 ScopedJavaLocalRef<jobject> map = |
662 Java_WebsitePreferenceBridge_createLocalStorageInfoMap(env_); | 666 Java_WebsitePreferenceBridge_createLocalStorageInfoMap(env_); |
667 SiteEngagementService* service = SiteEngagementService::Get(profile); | |
668 std::map<GURL, double> score_map = service->GetScoreMap(); | |
663 | 669 |
664 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>::const_iterator | 670 std::list<BrowsingDataLocalStorageHelper::LocalStorageInfo>::const_iterator |
665 i; | 671 i; |
666 for (i = local_storage_info.begin(); i != local_storage_info.end(); ++i) { | 672 for (i = local_storage_info.begin(); i != local_storage_info.end(); ++i) { |
667 ScopedJavaLocalRef<jstring> full_origin = | 673 ScopedJavaLocalRef<jstring> full_origin = |
668 ConvertUTF8ToJavaString(env_, i->origin_url.spec()); | 674 ConvertUTF8ToJavaString(env_, i->origin_url.spec()); |
675 std::string origin_str = i->origin_url.GetOrigin().spec(); | |
676 double engagement_score = 0; | |
677 auto engagement_score_it = score_map.find(i->origin_url.GetOrigin()); | |
678 if (engagement_score_it != score_map.end()) { | |
679 engagement_score = engagement_score_it->second; | |
680 } | |
669 // Remove the trailing backslash so the origin is matched correctly in | 681 // Remove the trailing backslash so the origin is matched correctly in |
670 // SingleWebsitePreferences.mergePermissionInfoForTopLevelOrigin. | 682 // SingleWebsitePreferences.mergePermissionInfoForTopLevelOrigin. |
671 std::string origin_str = i->origin_url.GetOrigin().spec(); | |
672 DCHECK(origin_str[origin_str.size() - 1] == '/'); | 683 DCHECK(origin_str[origin_str.size() - 1] == '/'); |
673 origin_str = origin_str.substr(0, origin_str.size() - 1); | 684 origin_str = origin_str.substr(0, origin_str.size() - 1); |
674 ScopedJavaLocalRef<jstring> origin = | 685 ScopedJavaLocalRef<jstring> origin = |
675 ConvertUTF8ToJavaString(env_, origin_str); | 686 ConvertUTF8ToJavaString(env_, origin_str); |
676 Java_WebsitePreferenceBridge_insertLocalStorageInfoIntoMap( | 687 Java_WebsitePreferenceBridge_insertLocalStorageInfoIntoMap( |
677 env_, map.obj(), origin.obj(), full_origin.obj(), i->size); | 688 env_, map.obj(), origin.obj(), full_origin.obj(), i->size, |
689 engagement_score); | |
678 } | 690 } |
679 | 691 |
680 Java_LocalStorageInfoReadyCallback_onLocalStorageInfoReady( | 692 Java_LocalStorageInfoReadyCallback_onLocalStorageInfoReady( |
681 env_, java_callback_.obj(), map.obj()); | 693 env_, java_callback_.obj(), map.obj()); |
682 delete this; | 694 delete this; |
683 } | 695 } |
684 | 696 |
685 private: | 697 private: |
686 JNIEnv* env_; | 698 JNIEnv* env_; |
687 ScopedJavaGlobalRef<jobject> java_callback_; | 699 ScopedJavaGlobalRef<jobject> java_callback_; |
688 }; | 700 }; |
689 | 701 |
690 } // anonymous namespace | 702 } // anonymous namespace |
691 | 703 |
692 // TODO(jknotten): These methods should not be static. Instead we should | 704 // TODO(jknotten): These methods should not be static. Instead we should |
693 // expose a class to Java so that the fetch requests can be cancelled, | 705 // expose a class to Java so that the fetch requests can be cancelled, |
694 // and manage the lifetimes of the callback (and indirectly the helper | 706 // and manage the lifetimes of the callback (and indirectly the helper |
695 // by having a reference to it). | 707 // by having a reference to it). |
696 | 708 |
697 // The helper methods (StartFetching, DeleteLocalStorageFile, DeleteDatabase) | 709 // The helper methods (StartFetching, DeleteLocalStorageFile, DeleteDatabase) |
698 // are asynchronous. A "use after free" error is not possible because the | 710 // are asynchronous. A "use after free" error is not possible because the |
699 // helpers keep a reference to themselves for the duration of their tasks, | 711 // helpers keep a reference to themselves for the duration of their tasks, |
700 // which includes callback invocation. | 712 // which includes callback invocation. |
701 | 713 |
714 static void FetchImportantOriginInfo( | |
715 JNIEnv* env, | |
716 const JavaParamRef<jclass>& clazz, | |
717 const JavaParamRef<jobject>& java_callback) { | |
718 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
719 ScopedJavaLocalRef<jobject> map = | |
720 Java_WebsitePreferenceBridge_createImportantOriginInfoMap(env); | |
721 ScopedJavaLocalRef<jobject> list = | |
722 Java_WebsitePreferenceBridge_createImportantOriginList(env); | |
723 SiteEngagementService* service = SiteEngagementService::Get(profile); | |
724 std::map<GURL, double> score_map = service->GetScoreMap(); | |
725 std::vector<std::pair<GURL, double>> ranked_origins; | |
Finnur
2016/03/03 14:15:44
Shouldn't this be named top_ranking_origins, or so
dmurph
2016/04/26 21:43:13
chagned.
| |
726 | |
727 for (const auto& url_engagement_pair : score_map) { | |
728 if (!service->IsEngagementAtLeast( | |
729 url_engagement_pair.first, | |
730 SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)) { | |
731 continue; | |
732 } | |
733 ranked_origins.push_back(url_engagement_pair); | |
734 } | |
735 | |
736 std::sort( | |
737 ranked_origins.begin(), ranked_origins.end(), | |
738 [](const std::pair<GURL, double>& a, const std::pair<GURL, double>& b) { | |
739 return a.second > b.second; | |
740 }); | |
741 ranked_origins.resize(std::min(ranked_origins.size(), 5u)); | |
Finnur
2016/03/03 14:15:44
Um... this is non-obvious. Can you add a comment o
dmurph
2016/04/26 21:43:13
Moved to a separate class in previous patch.
| |
742 | |
743 for (const auto& url_engagement_pair : ranked_origins) { | |
744 // Remove the trailing backslash so the origin is matched correctly in | |
745 // SingleWebsitePreferences.mergePermissionInfoForTopLevelOrigin, and with | |
746 // maps returned in FetchLocalStorageInfo. | |
747 std::string origin_str = url_engagement_pair.first.GetOrigin().spec(); | |
748 DCHECK(origin_str[origin_str.size() - 1] == '/'); | |
749 origin_str = origin_str.substr(0, origin_str.size() - 1); | |
750 ScopedJavaLocalRef<jstring> origin = | |
751 ConvertUTF8ToJavaString(env, origin_str); | |
752 Java_WebsitePreferenceBridge_insertImportantOriginInfoIntoMap( | |
753 env, map.obj(), origin.obj(), 0, url_engagement_pair.second); | |
754 Java_WebsitePreferenceBridge_insertImportantOriginIntoList(env, list.obj(), | |
755 origin.obj()); | |
756 } | |
757 | |
758 Java_ImportantOriginsReadyCallback_onImportantOriginsReady( | |
759 env, java_callback.obj(), list.obj(), map.obj()); | |
760 } | |
761 | |
702 static void FetchLocalStorageInfo(JNIEnv* env, | 762 static void FetchLocalStorageInfo(JNIEnv* env, |
703 const JavaParamRef<jclass>& clazz, | 763 const JavaParamRef<jclass>& clazz, |
704 const JavaParamRef<jobject>& java_callback) { | 764 const JavaParamRef<jobject>& java_callback) { |
705 Profile* profile = ProfileManager::GetActiveUserProfile(); | 765 Profile* profile = ProfileManager::GetActiveUserProfile(); |
706 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( | 766 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( |
707 new BrowsingDataLocalStorageHelper(profile)); | 767 new BrowsingDataLocalStorageHelper(profile)); |
708 // local_storage_callback will delete itself when it is run. | 768 // local_storage_callback will delete itself when it is run. |
709 LocalStorageInfoReadyCallback* local_storage_callback = | 769 LocalStorageInfoReadyCallback* local_storage_callback = |
710 new LocalStorageInfoReadyCallback(java_callback); | 770 new LocalStorageInfoReadyCallback(java_callback); |
711 local_storage_helper->StartFetching( | 771 local_storage_helper->StartFetching( |
712 base::Bind(&LocalStorageInfoReadyCallback::OnLocalStorageModelInfoLoaded, | 772 base::Bind(&LocalStorageInfoReadyCallback::OnLocalStorageModelInfoLoaded, |
713 base::Unretained(local_storage_callback))); | 773 base::Unretained(local_storage_callback), profile)); |
714 } | 774 } |
715 | 775 |
716 static void FetchStorageInfo(JNIEnv* env, | 776 static void FetchStorageInfo(JNIEnv* env, |
717 const JavaParamRef<jclass>& clazz, | 777 const JavaParamRef<jclass>& clazz, |
718 const JavaParamRef<jobject>& java_callback) { | 778 const JavaParamRef<jobject>& java_callback) { |
719 Profile* profile = ProfileManager::GetActiveUserProfile(); | 779 Profile* profile = ProfileManager::GetActiveUserProfile(); |
720 | 780 |
721 // storage_info_ready_callback will delete itself when it is run. | 781 // storage_info_ready_callback will delete itself when it is run. |
722 StorageInfoReadyCallback* storage_info_ready_callback = | 782 StorageInfoReadyCallback* storage_info_ready_callback = |
723 new StorageInfoReadyCallback(java_callback); | 783 new StorageInfoReadyCallback(java_callback); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
765 GURL url(ConvertJavaStringToUTF8(env, jorigin)); | 825 GURL url(ConvertJavaStringToUTF8(env, jorigin)); |
766 scoped_refptr<SiteDataDeleteHelper> site_data_deleter( | 826 scoped_refptr<SiteDataDeleteHelper> site_data_deleter( |
767 new SiteDataDeleteHelper(profile, url)); | 827 new SiteDataDeleteHelper(profile, url)); |
768 site_data_deleter->Run(); | 828 site_data_deleter->Run(); |
769 } | 829 } |
770 | 830 |
771 // Register native methods | 831 // Register native methods |
772 bool RegisterWebsitePreferenceBridge(JNIEnv* env) { | 832 bool RegisterWebsitePreferenceBridge(JNIEnv* env) { |
773 return RegisterNativesImpl(env); | 833 return RegisterNativesImpl(env); |
774 } | 834 } |
OLD | NEW |