Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/permissions/permission_decision_auto_blocker.h" | 5 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/time.h" | |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 15 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 16 #include "chrome/browser/permissions/permission_blacklist_client.h" | |
| 15 #include "chrome/browser/permissions/permission_util.h" | 17 #include "chrome/browser/permissions/permission_util.h" |
| 16 #include "chrome/common/chrome_features.h" | 18 #include "chrome/common/chrome_features.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/variations/variations_associated_data.h" | 20 #include "components/variations/variations_associated_data.h" |
| 19 #include "content/public/browser/permission_type.h" | 21 #include "content/public/browser/permission_type.h" |
| 22 #include "content/public/browser/web_contents.h" | |
| 20 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 // The number of times that users may explicitly dismiss a permission prompt | 27 // The number of times that users may explicitly dismiss a permission prompt |
| 25 // from an origin before it is automatically blocked. | 28 // from an origin before it is automatically blocked. |
| 26 int g_prompt_dismissals_before_block = 3; | 29 int g_prompt_dismissals_before_block = 3; |
| 27 | 30 |
| 31 // The number of days that an origin will stay under embargo for a requested | |
| 32 // permission. | |
| 33 int g_embargo_days = 7; | |
| 34 | |
| 28 std::unique_ptr<base::DictionaryValue> GetOriginDict( | 35 std::unique_ptr<base::DictionaryValue> GetOriginDict( |
| 29 HostContentSettingsMap* settings, | 36 HostContentSettingsMap* settings, |
| 30 const GURL& origin_url) { | 37 const GURL& origin_url) { |
| 31 std::unique_ptr<base::DictionaryValue> dict = | 38 std::unique_ptr<base::DictionaryValue> dict = |
| 32 base::DictionaryValue::From(settings->GetWebsiteSetting( | 39 base::DictionaryValue::From(settings->GetWebsiteSetting( |
| 33 origin_url, origin_url, | 40 origin_url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, |
| 34 CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, std::string(), | 41 std::string(), nullptr)); |
| 35 nullptr)); | |
| 36 if (!dict) | 42 if (!dict) |
| 37 return base::MakeUnique<base::DictionaryValue>(); | 43 return base::MakeUnique<base::DictionaryValue>(); |
| 38 | 44 |
| 39 return dict; | 45 return dict; |
| 40 } | 46 } |
| 41 | 47 |
| 42 base::DictionaryValue* GetOrCreatePermissionDict( | 48 base::DictionaryValue* GetOrCreatePermissionDict( |
| 43 base::DictionaryValue* origin_dict, | 49 base::DictionaryValue* origin_dict, |
| 44 const std::string& permission) { | 50 const std::string& permission) { |
| 45 base::DictionaryValue* permission_dict = nullptr; | 51 base::DictionaryValue* permission_dict = nullptr; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 75 return current_count; | 81 return current_count; |
| 76 } | 82 } |
| 77 | 83 |
| 78 int GetActionCount(const GURL& url, | 84 int GetActionCount(const GURL& url, |
| 79 content::PermissionType permission, | 85 content::PermissionType permission, |
| 80 const char* key, | 86 const char* key, |
| 81 Profile* profile) { | 87 Profile* profile) { |
| 82 HostContentSettingsMap* map = | 88 HostContentSettingsMap* map = |
| 83 HostContentSettingsMapFactory::GetForProfile(profile); | 89 HostContentSettingsMapFactory::GetForProfile(profile); |
| 84 std::unique_ptr<base::DictionaryValue> dict = GetOriginDict(map, url); | 90 std::unique_ptr<base::DictionaryValue> dict = GetOriginDict(map, url); |
| 85 | |
| 86 base::DictionaryValue* permission_dict = GetOrCreatePermissionDict( | 91 base::DictionaryValue* permission_dict = GetOrCreatePermissionDict( |
| 87 dict.get(), PermissionUtil::GetPermissionString(permission)); | 92 dict.get(), PermissionUtil::GetPermissionString(permission)); |
| 88 | 93 |
| 89 int current_count = 0; | 94 int current_count = 0; |
| 90 permission_dict->GetInteger(key, ¤t_count); | 95 permission_dict->GetInteger(key, ¤t_count); |
| 91 return current_count; | 96 return current_count; |
| 92 } | 97 } |
| 93 | 98 |
| 94 } // namespace | 99 } // namespace |
| 95 | 100 |
| 96 // static | 101 // static |
| 97 const char PermissionDecisionAutoBlocker::kPromptDismissCountKey[] = | 102 const char PermissionDecisionAutoBlocker::kPromptDismissCountKey[] = |
| 98 "dismiss_count"; | 103 "dismiss_count"; |
| 99 | 104 |
| 100 // static | 105 // static |
| 101 const char PermissionDecisionAutoBlocker::kPromptIgnoreCountKey[] = | 106 const char PermissionDecisionAutoBlocker::kPromptIgnoreCountKey[] = |
| 102 "ignore_count"; | 107 "ignore_count"; |
| 103 | 108 |
| 104 // static | 109 // static |
| 110 const char PermissionDecisionAutoBlocker::kPermissionOriginEmbargoKey[] = | |
| 111 "embargo_days"; | |
| 112 | |
| 113 // static | |
| 105 void PermissionDecisionAutoBlocker::RemoveCountsByUrl( | 114 void PermissionDecisionAutoBlocker::RemoveCountsByUrl( |
| 106 Profile* profile, | 115 Profile* profile, |
| 107 base::Callback<bool(const GURL& url)> filter) { | 116 base::Callback<bool(const GURL& url)> filter) { |
| 108 HostContentSettingsMap* map = | 117 HostContentSettingsMap* map = |
| 109 HostContentSettingsMapFactory::GetForProfile(profile); | 118 HostContentSettingsMapFactory::GetForProfile(profile); |
| 110 | 119 |
| 111 std::unique_ptr<ContentSettingsForOneType> settings( | 120 std::unique_ptr<ContentSettingsForOneType> settings( |
| 112 new ContentSettingsForOneType); | 121 new ContentSettingsForOneType); |
| 113 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, | 122 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, |
| 114 std::string(), settings.get()); | 123 std::string(), settings.get()); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 | 176 |
| 168 if (!base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)) | 177 if (!base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)) |
| 169 return false; | 178 return false; |
| 170 | 179 |
| 171 return current_dismissal_count >= g_prompt_dismissals_before_block; | 180 return current_dismissal_count >= g_prompt_dismissals_before_block; |
| 172 } | 181 } |
| 173 | 182 |
| 174 // static | 183 // static |
| 175 void PermissionDecisionAutoBlocker::UpdateFromVariations() { | 184 void PermissionDecisionAutoBlocker::UpdateFromVariations() { |
| 176 int prompt_dismissals = -1; | 185 int prompt_dismissals = -1; |
| 177 std::string value = variations::GetVariationParamValueByFeature( | 186 int embargo_days = -1; |
| 187 std::string dismissals_value = variations::GetVariationParamValueByFeature( | |
| 178 features::kBlockPromptsIfDismissedOften, kPromptDismissCountKey); | 188 features::kBlockPromptsIfDismissedOften, kPromptDismissCountKey); |
| 189 std::string embargo_value = variations::GetVariationParamValueByFeature( | |
| 190 features::kPermissionsBlacklist, kPermissionOriginEmbargoKey); | |
| 191 // If converting the value fails, stick with the current value. | |
| 192 if (base::StringToInt(dismissals_value, &prompt_dismissals) && | |
| 193 prompt_dismissals > 0) | |
| 194 g_prompt_dismissals_before_block = prompt_dismissals; | |
| 195 if (base::StringToInt(embargo_value, &embargo_days) && embargo_days > 0) | |
| 196 g_embargo_days = embargo_days; | |
| 197 } | |
| 179 | 198 |
| 180 // If converting the value fails, stick with the current value. | 199 // static |
| 181 if (base::StringToInt(value, &prompt_dismissals) && prompt_dismissals > 0) | 200 // TODO(meredithl): Have PermissionDecisionAutoBlocker handle the database |
| 182 g_prompt_dismissals_before_block = prompt_dismissals; | 201 // manager, rather than passing it in. |
| 202 void PermissionDecisionAutoBlocker::ShouldAutomaticallyBlock( | |
|
raymes
2017/01/12 02:34:10
A slightly alternative design would be to have thi
meredithl
2017/01/12 05:59:19
I'm not sure I understand what you mean. Permissio
raymes
2017/01/16 02:23:42
Yep - that's what I meant. I don't necessarily thi
meredithl
2017/01/16 04:28:19
Agreed, the name is bad. I called it this to tie i
| |
| 203 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | |
| 204 content::PermissionType permission, | |
| 205 const GURL& request_origin, | |
| 206 content::WebContents* web_contents, | |
| 207 int timeout, | |
| 208 Profile* profile, | |
| 209 base::Time current_time, | |
| 210 base::Callback<void(bool)> callback) { | |
| 211 if (!base::FeatureList::IsEnabled(features::kPermissionsBlacklist)) { | |
| 212 callback.Run(false /* permission blocked */); | |
| 213 return; | |
| 214 } else if (IsUnderEmbargo(permission, profile, request_origin, | |
| 215 current_time)) { | |
| 216 callback.Run(true /* permission blocked */); | |
| 217 return; | |
| 218 } else if (!db_manager) { | |
|
raymes
2017/01/12 00:53:14
Can this be incorporated into the feature enabled
meredithl
2017/01/12 05:59:19
The only reason I didn't was if blacklisting is en
raymes
2017/01/16 02:23:42
That makes sense, it might be worth adding a comme
meredithl
2017/01/16 04:28:19
This has since changed. Dominick suggested that I
| |
| 219 callback.Run(false /* permission blocked */); | |
| 220 return; | |
|
raymes
2017/01/12 00:53:15
Rather than all the return statements, the below s
meredithl
2017/01/12 05:59:19
Done.
| |
| 221 } | |
| 222 | |
| 223 // Request origin is not yet under embargo, so we check it with Safe Browsing. | |
| 224 // TODO(meredithl): Implement incremental back off for checking blacklist. | |
| 225 PermissionBlacklistClient::CheckSafeBrowsingBlacklist( | |
| 226 db_manager, permission, request_origin, web_contents, timeout, | |
| 227 base::Bind(&PermissionDecisionAutoBlocker::CheckSafeBrowsingResult, | |
| 228 permission, profile, request_origin, callback)); | |
| 183 } | 229 } |
| 230 | |
| 231 // static | |
| 232 void PermissionDecisionAutoBlocker::CheckSafeBrowsingResult( | |
| 233 content::PermissionType permission, | |
| 234 Profile* profile, | |
| 235 const GURL& request_origin, | |
| 236 base::Callback<void(bool)> callback, | |
| 237 bool should_be_embargoed) { | |
| 238 if (should_be_embargoed) { | |
| 239 // Requesting site is blacklisted for this permission, update the content | |
| 240 // setting to place it under embargo. | |
| 241 PlaceUnderEmbargo(permission, request_origin, | |
| 242 HostContentSettingsMapFactory::GetForProfile(profile), | |
| 243 base::Time::Now()); | |
| 244 } | |
| 245 callback.Run(should_be_embargoed /* permission blocked */); | |
| 246 } | |
| 247 | |
| 248 // static | |
| 249 bool PermissionDecisionAutoBlocker::IsUnderEmbargo( | |
| 250 content::PermissionType permission, | |
| 251 Profile* profile, | |
| 252 const GURL& request_origin, | |
| 253 base::Time current_time) { | |
| 254 HostContentSettingsMap* map = | |
| 255 HostContentSettingsMapFactory::GetForProfile(profile); | |
| 256 std::unique_ptr<base::DictionaryValue> dict = | |
| 257 GetOriginDict(map, request_origin); | |
| 258 base::DictionaryValue* permission_dict = GetOrCreatePermissionDict( | |
| 259 dict.get(), PermissionUtil::GetPermissionString(permission)); | |
| 260 double embargo_days = -1; | |
|
raymes
2017/01/12 02:34:10
nit: would this be better named embargo_time?
meredithl
2017/01/12 05:59:19
Dom had me change this from embargo_time to embarg
raymes
2017/01/16 02:23:42
I see you've changed it to embargo_date which make
meredithl
2017/01/16 04:28:19
Yes, as per your other suggestion to store the dat
| |
| 261 if ((permission_dict->GetDouble(kPermissionOriginEmbargoKey, | |
| 262 &embargo_days))) { | |
| 263 if (current_time < base::Time::FromInternalValue(embargo_days)) | |
|
raymes
2017/01/12 02:34:10
Another question is how the embargo period will re
raymes
2017/01/12 03:19:30
Dom, Ben and I chatted about this and concluded th
meredithl
2017/01/12 05:59:19
Sgtm, I'll make sure to work it into the design do
| |
| 264 return true; | |
| 265 } | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 // static | |
| 270 void PermissionDecisionAutoBlocker::PlaceUnderEmbargo( | |
| 271 content::PermissionType permission, | |
| 272 const GURL& request_origin, | |
| 273 HostContentSettingsMap* map, | |
| 274 base::Time current_time) { | |
| 275 std::unique_ptr<base::DictionaryValue> dict = | |
| 276 GetOriginDict(map, request_origin); | |
| 277 base::DictionaryValue* permission_dict = GetOrCreatePermissionDict( | |
| 278 dict.get(), PermissionUtil::GetPermissionString(permission)); | |
| 279 base::Time embargo_end = | |
| 280 current_time + base::TimeDelta::FromDays(g_embargo_days); | |
|
raymes
2017/01/12 03:19:30
Would it be better to just store the current times
meredithl
2017/01/12 05:59:19
Done.
| |
| 281 permission_dict->SetDouble(kPermissionOriginEmbargoKey, | |
| 282 embargo_end.ToInternalValue()); | |
| 283 map->SetWebsiteSettingDefaultScope( | |
| 284 request_origin, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, | |
|
raymes
2017/01/12 00:53:15
I don't quite understand what the end state of the
meredithl
2017/01/12 05:59:19
The idea that Dom and I are going with at the mome
raymes
2017/01/16 02:23:42
Ok - I still have some questions about how the dis
meredithl
2017/01/16 04:28:19
Sgtm!
| |
| 285 std::string(), std::move(dict)); | |
| 286 } | |
| OLD | NEW |