| 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> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" | 23 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" |
| 24 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h" | 24 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h" |
| 25 #include "chrome/browser/browsing_data/cookies_tree_model.h" | 25 #include "chrome/browser/browsing_data/cookies_tree_model.h" |
| 26 #include "chrome/browser/browsing_data/local_data_container.h" | 26 #include "chrome/browser/browsing_data/local_data_container.h" |
| 27 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 27 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 28 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 28 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 29 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 29 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 30 #include "chrome/browser/content_settings/web_site_settings_uma_util.h" | 30 #include "chrome/browser/content_settings/web_site_settings_uma_util.h" |
| 31 #include "chrome/browser/engagement/important_sites_util.h" | 31 #include "chrome/browser/engagement/important_sites_util.h" |
| 32 #include "chrome/browser/notifications/desktop_notification_profile_util.h" | 32 #include "chrome/browser/notifications/desktop_notification_profile_util.h" |
| 33 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
| 34 #include "chrome/browser/permissions/permission_manager.h" |
| 33 #include "chrome/browser/permissions/permission_uma_util.h" | 35 #include "chrome/browser/permissions/permission_uma_util.h" |
| 34 #include "chrome/browser/permissions/permission_util.h" | 36 #include "chrome/browser/permissions/permission_util.h" |
| 35 #include "chrome/browser/profiles/profile.h" | 37 #include "chrome/browser/profiles/profile.h" |
| 36 #include "chrome/browser/profiles/profile_manager.h" | 38 #include "chrome/browser/profiles/profile_manager.h" |
| 37 #include "chrome/browser/storage/storage_info_fetcher.h" | 39 #include "chrome/browser/storage/storage_info_fetcher.h" |
| 38 #include "chrome/browser/usb/usb_chooser_context.h" | 40 #include "chrome/browser/usb/usb_chooser_context.h" |
| 39 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 41 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
| 40 #include "components/content_settings/core/browser/cookie_settings.h" | 42 #include "components/content_settings/core/browser/cookie_settings.h" |
| 41 #include "components/content_settings/core/browser/host_content_settings_map.h" | 43 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 42 #include "content/public/browser/browser_thread.h" | 44 #include "content/public/browser/browser_thread.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 56 using base::android::ScopedJavaLocalRef; | 58 using base::android::ScopedJavaLocalRef; |
| 57 using content::BrowserThread; | 59 using content::BrowserThread; |
| 58 | 60 |
| 59 namespace { | 61 namespace { |
| 60 // We need to limit our size due to the algorithm in ImportantSiteUtil, but we | 62 // We need to limit our size due to the algorithm in ImportantSiteUtil, but we |
| 61 // want to be more on the liberal side here as we're not exposing these sites | 63 // want to be more on the liberal side here as we're not exposing these sites |
| 62 // to the user, we're just using them for our 'clear unimportant' feature in | 64 // to the user, we're just using them for our 'clear unimportant' feature in |
| 63 // ManageSpaceActivity.java. | 65 // ManageSpaceActivity.java. |
| 64 const int kMaxImportantSites = 10; | 66 const int kMaxImportantSites = 10; |
| 65 | 67 |
| 68 const char* kHttpPortSuffix = ":80"; |
| 69 const char* kHttpsPortSuffix = ":443"; |
| 70 |
| 66 Profile* GetActiveUserProfile(bool is_incognito) { | 71 Profile* GetActiveUserProfile(bool is_incognito) { |
| 67 Profile* profile = ProfileManager::GetActiveUserProfile(); | 72 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 68 if (is_incognito) | 73 if (is_incognito) |
| 69 profile = profile->GetOffTheRecordProfile(); | 74 profile = profile->GetOffTheRecordProfile(); |
| 70 return profile; | 75 return profile; |
| 71 } | 76 } |
| 72 | 77 |
| 73 HostContentSettingsMap* GetHostContentSettingsMap(bool is_incognito) { | 78 HostContentSettingsMap* GetHostContentSettingsMap(bool is_incognito) { |
| 74 return HostContentSettingsMapFactory::GetForProfile( | 79 return HostContentSettingsMapFactory::GetForProfile( |
| 75 GetActiveUserProfile(is_incognito)); | 80 GetActiveUserProfile(is_incognito)); |
| 76 } | 81 } |
| 77 | 82 |
| 83 ScopedJavaLocalRef<jstring> ConvertOriginToJavaString( |
| 84 JNIEnv* env, |
| 85 const std::string& origin) { |
| 86 // The string |jorigin| is used to group permissions together in the Site |
| 87 // Settings list. In order to group sites with the same origin, remove any |
| 88 // standard port from the end of the URL if it's present (i.e. remove :443 |
| 89 // for HTTPS sites and :80 for HTTP sites). |
| 90 // TODO(sashab,lgarron): Find out which settings are being saved with the |
| 91 // port and omit it if it's the standard port. |
| 92 // TODO(mvanouwerkerk): Remove all this logic and take two passes through |
| 93 // HostContentSettingsMap: once to get all the 'interesting' hosts, and once |
| 94 // (on SingleWebsitePreferences) to find permission patterns which match |
| 95 // each of these hosts. |
| 96 if (base::StartsWith(origin, url::kHttpsScheme, |
| 97 base::CompareCase::INSENSITIVE_ASCII) && |
| 98 base::EndsWith(origin, kHttpsPortSuffix, |
| 99 base::CompareCase::INSENSITIVE_ASCII)) { |
| 100 return ConvertUTF8ToJavaString( |
| 101 env, origin.substr(0, origin.size() - strlen(kHttpsPortSuffix))); |
| 102 } else if (base::StartsWith(origin, url::kHttpScheme, |
| 103 base::CompareCase::INSENSITIVE_ASCII) && |
| 104 base::EndsWith(origin, kHttpPortSuffix, |
| 105 base::CompareCase::INSENSITIVE_ASCII)) { |
| 106 return ConvertUTF8ToJavaString( |
| 107 env, origin.substr(0, origin.size() - strlen(kHttpPortSuffix))); |
| 108 } else { |
| 109 return ConvertUTF8ToJavaString(env, origin); |
| 110 } |
| 111 } |
| 112 |
| 78 typedef void (*InfoListInsertionFunction)( | 113 typedef void (*InfoListInsertionFunction)( |
| 79 JNIEnv*, | 114 JNIEnv*, |
| 80 const base::android::JavaRefOrBare<jobject>&, | 115 const base::android::JavaRefOrBare<jobject>&, |
| 81 const base::android::JavaRefOrBare<jstring>&, | 116 const base::android::JavaRefOrBare<jstring>&, |
| 82 const base::android::JavaRefOrBare<jstring>&); | 117 const base::android::JavaRefOrBare<jstring>&); |
| 83 | 118 |
| 84 void GetOrigins(JNIEnv* env, | 119 void GetOrigins(JNIEnv* env, |
| 85 ContentSettingsType content_type, | 120 ContentSettingsType content_type, |
| 86 InfoListInsertionFunction insertionFunc, | 121 InfoListInsertionFunction insertionFunc, |
| 87 jobject list, | 122 jobject list, |
| 88 jboolean managedOnly) { | 123 jboolean managedOnly) { |
| 89 ContentSettingsForOneType all_settings; | |
| 90 HostContentSettingsMap* content_settings_map = | 124 HostContentSettingsMap* content_settings_map = |
| 91 GetHostContentSettingsMap(false); // is_incognito | 125 GetHostContentSettingsMap(false); // is_incognito |
| 126 ContentSettingsForOneType all_settings; |
| 127 ContentSettingsForOneType embargo_settings; |
| 128 |
| 92 content_settings_map->GetSettingsForOneType( | 129 content_settings_map->GetSettingsForOneType( |
| 93 content_type, std::string(), &all_settings); | 130 content_type, std::string(), &all_settings); |
| 131 content_settings_map->GetSettingsForOneType( |
| 132 CONTENT_SETTINGS_TYPE_PERMISSION_AUTOBLOCKER_DATA, std::string(), |
| 133 &embargo_settings); |
| 94 ContentSetting default_content_setting = content_settings_map-> | 134 ContentSetting default_content_setting = content_settings_map-> |
| 95 GetDefaultContentSetting(content_type, NULL); | 135 GetDefaultContentSetting(content_type, NULL); |
| 136 |
| 137 // Use a vector since the overall number of origins should be small. |
| 138 std::vector<std::string> seen_origins; |
| 139 |
| 96 // Now add all origins that have a non-default setting to the list. | 140 // Now add all origins that have a non-default setting to the list. |
| 97 for (const auto& settings_it : all_settings) { | 141 for (const auto& settings_it : all_settings) { |
| 98 if (settings_it.setting == default_content_setting) | 142 if (settings_it.setting == default_content_setting) |
| 99 continue; | 143 continue; |
| 100 if (managedOnly && | 144 if (managedOnly && |
| 101 HostContentSettingsMap::GetProviderTypeFromSource(settings_it.source) != | 145 HostContentSettingsMap::GetProviderTypeFromSource(settings_it.source) != |
| 102 HostContentSettingsMap::ProviderType::POLICY_PROVIDER) { | 146 HostContentSettingsMap::ProviderType::POLICY_PROVIDER) { |
| 103 continue; | 147 continue; |
| 104 } | 148 } |
| 105 const std::string origin = settings_it.primary_pattern.ToString(); | 149 const std::string origin = settings_it.primary_pattern.ToString(); |
| 106 const std::string embedder = settings_it.secondary_pattern.ToString(); | 150 const std::string embedder = settings_it.secondary_pattern.ToString(); |
| 107 | 151 |
| 108 // The string |jorigin| is used to group permissions together in the Site | |
| 109 // Settings list. In order to group sites with the same origin, remove any | |
| 110 // standard port from the end of the URL if it's present (i.e. remove :443 | |
| 111 // for HTTPS sites and :80 for HTTP sites). | |
| 112 // TODO(sashab,lgarron): Find out which settings are being saved with the | |
| 113 // port and omit it if it's the standard port. | |
| 114 // TODO(mvanouwerkerk): Remove all this logic and take two passes through | |
| 115 // HostContentSettingsMap: once to get all the 'interesting' hosts, and once | |
| 116 // (on SingleWebsitePreferences) to find permission patterns which match | |
| 117 // each of these hosts. | |
| 118 const char* kHttpPortSuffix = ":80"; | |
| 119 const char* kHttpsPortSuffix = ":443"; | |
| 120 ScopedJavaLocalRef<jstring> jorigin; | |
| 121 if (base::StartsWith(origin, url::kHttpsScheme, | |
| 122 base::CompareCase::INSENSITIVE_ASCII) && | |
| 123 base::EndsWith(origin, kHttpsPortSuffix, | |
| 124 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 125 jorigin = ConvertUTF8ToJavaString( | |
| 126 env, origin.substr(0, origin.size() - strlen(kHttpsPortSuffix))); | |
| 127 } else if (base::StartsWith(origin, url::kHttpScheme, | |
| 128 base::CompareCase::INSENSITIVE_ASCII) && | |
| 129 base::EndsWith(origin, kHttpPortSuffix, | |
| 130 base::CompareCase::INSENSITIVE_ASCII)) { | |
| 131 jorigin = ConvertUTF8ToJavaString( | |
| 132 env, origin.substr(0, origin.size() - strlen(kHttpPortSuffix))); | |
| 133 } else { | |
| 134 jorigin = ConvertUTF8ToJavaString(env, origin); | |
| 135 } | |
| 136 | |
| 137 ScopedJavaLocalRef<jstring> jembedder; | 152 ScopedJavaLocalRef<jstring> jembedder; |
| 138 if (embedder != origin) | 153 if (embedder != origin) |
| 139 jembedder = ConvertUTF8ToJavaString(env, embedder); | 154 jembedder = ConvertUTF8ToJavaString(env, embedder); |
| 140 | 155 |
| 141 insertionFunc(env, list, jorigin, jembedder); | 156 seen_origins.push_back(origin); |
| 157 insertionFunc(env, list, ConvertOriginToJavaString(env, origin), jembedder); |
| 158 } |
| 159 |
| 160 // Add any origins which have a default content setting value (thus skipped |
| 161 // above), but have been automatically blocked for this permission type. |
| 162 // We use an empty embedder since embargo doesn't care about it. |
| 163 PermissionDecisionAutoBlocker* auto_blocker = |
| 164 PermissionDecisionAutoBlocker::GetForProfile( |
| 165 GetActiveUserProfile(false /* is_incognito */)); |
| 166 ScopedJavaLocalRef<jstring> jembedder; |
| 167 |
| 168 for (const auto& settings_it : embargo_settings) { |
| 169 const std::string origin = settings_it.primary_pattern.ToString(); |
| 170 if (std::find(seen_origins.begin(), seen_origins.end(), origin) != |
| 171 seen_origins.end()) { |
| 172 // This origin has already been added to the list, so don't add it again. |
| 173 continue; |
| 174 } |
| 175 |
| 176 if (auto_blocker->GetEmbargoResult(GURL(origin), content_type) |
| 177 .content_setting == CONTENT_SETTING_BLOCK) { |
| 178 insertionFunc(env, list, ConvertOriginToJavaString(env, origin), |
| 179 jembedder); |
| 180 } |
| 142 } | 181 } |
| 143 } | 182 } |
| 144 | 183 |
| 145 ContentSetting GetSettingForOrigin(JNIEnv* env, | 184 ContentSetting GetSettingForOrigin(JNIEnv* env, |
| 146 ContentSettingsType content_type, | 185 ContentSettingsType content_type, |
| 147 jstring origin, | 186 jstring origin, |
| 148 jstring embedder, | 187 jstring embedder, |
| 149 jboolean is_incognito) { | 188 jboolean is_incognito) { |
| 150 GURL url(ConvertJavaStringToUTF8(env, origin)); | 189 GURL url(ConvertJavaStringToUTF8(env, origin)); |
| 151 GURL embedder_url(ConvertJavaStringToUTF8(env, embedder)); | 190 GURL embedder_url(ConvertJavaStringToUTF8(env, embedder)); |
| 152 ContentSetting setting = | 191 return PermissionManager::Get(GetActiveUserProfile(is_incognito)) |
| 153 GetHostContentSettingsMap(is_incognito) | 192 ->GetPermissionStatus(content_type, url, embedder_url) |
| 154 ->GetContentSetting(url, embedder_url, content_type, std::string()); | 193 .content_setting; |
| 155 return setting; | |
| 156 } | 194 } |
| 157 | 195 |
| 158 void SetSettingForOrigin(JNIEnv* env, | 196 void SetSettingForOrigin(JNIEnv* env, |
| 159 ContentSettingsType content_type, | 197 ContentSettingsType content_type, |
| 160 jstring origin, | 198 jstring origin, |
| 161 jstring embedder, | 199 jstring embedder, |
| 162 ContentSetting setting, | 200 ContentSetting setting, |
| 163 jboolean is_incognito) { | 201 jboolean is_incognito) { |
| 164 GURL origin_url(ConvertJavaStringToUTF8(env, origin)); | 202 GURL origin_url(ConvertJavaStringToUTF8(env, origin)); |
| 165 GURL embedder_url = | 203 GURL embedder_url = |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 SearchGeolocationService* search_helper = | 807 SearchGeolocationService* search_helper = |
| 770 SearchGeolocationService::Factory::GetForBrowserContext( | 808 SearchGeolocationService::Factory::GetForBrowserContext( |
| 771 GetActiveUserProfile(false /* is_incognito */)); | 809 GetActiveUserProfile(false /* is_incognito */)); |
| 772 return search_helper->SetDSEGeolocationSetting(setting); | 810 return search_helper->SetDSEGeolocationSetting(setting); |
| 773 } | 811 } |
| 774 | 812 |
| 775 // Register native methods | 813 // Register native methods |
| 776 bool RegisterWebsitePreferenceBridge(JNIEnv* env) { | 814 bool RegisterWebsitePreferenceBridge(JNIEnv* env) { |
| 777 return RegisterNativesImpl(env); | 815 return RegisterNativesImpl(env); |
| 778 } | 816 } |
| OLD | NEW |