Chromium Code Reviews| 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/permissions/permission_util.h" | 5 #include "chrome/browser/permissions/permission_util.h" |
| 6 | 6 |
| 7 #include <memory> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/values.h" | |
| 8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 #include "chrome/browser/permissions/permission_uma_util.h" | 14 #include "chrome/browser/permissions/permission_uma_util.h" |
| 10 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "components/variations/variations_associated_data.h" | |
| 11 #include "content/public/browser/permission_type.h" | 17 #include "content/public/browser/permission_type.h" |
| 18 #include "url/gurl.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // The number of times that users may explicitly dismiss a permission prompt | |
| 23 // from an origin before it is automatically blocked. Configurable via Finch, | |
| 24 // and only used if features::kBlockPromptsIfDismissedOften is enabled. | |
| 25 int gPromptDismissalsBeforeBlock = 3; | |
|
kcarattini
2016/07/28 08:54:49
This value isn't used unless it's been updated fro
dominickn
2016/08/01 01:08:51
This is now a static constant - it's the default v
| |
| 26 | |
| 27 bool gUpdatedFromVariations = false; | |
|
raymes
2016/07/28 08:07:50
nit: globals tend to use camel case with a g_ pref
kcarattini
2016/07/28 08:54:49
Isn't it lowercase with underscores?
dominickn
2016/08/01 01:08:51
Now a constant not a global.
| |
| 28 | |
| 29 const char kPromptStudyName[] = "PermissionPromptsUX"; | |
| 30 const char kPromptDismissCountKey[] = "dismiss_count"; | |
| 31 | |
| 32 std::unique_ptr<base::DictionaryValue> GetOriginDict( | |
| 33 HostContentSettingsMap* settings, | |
| 34 const GURL& origin_url) { | |
| 35 if (!settings) | |
| 36 return base::WrapUnique(new base::DictionaryValue()); | |
| 37 | |
| 38 std::unique_ptr<base::DictionaryValue> dict = | |
| 39 base::DictionaryValue::From(settings->GetWebsiteSetting( | |
| 40 origin_url, origin_url, | |
| 41 CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, std::string(), | |
| 42 nullptr)); | |
| 43 if (!dict) | |
| 44 return base::WrapUnique(new base::DictionaryValue()); | |
| 45 | |
| 46 return dict; | |
| 47 } | |
| 48 | |
| 49 base::DictionaryValue* GetPermissionDict(base::DictionaryValue* origin_dict, | |
| 50 const std::string& permission) { | |
| 51 base::DictionaryValue* permission_dict = nullptr; | |
| 52 if (!origin_dict->GetDictionaryWithoutPathExpansion(permission, | |
| 53 &permission_dict)) { | |
| 54 permission_dict = new base::DictionaryValue(); | |
| 55 origin_dict->SetWithoutPathExpansion(permission, | |
| 56 base::WrapUnique(permission_dict)); | |
| 57 } | |
| 58 | |
| 59 return permission_dict; | |
| 60 } | |
| 61 | |
| 62 // Records that the user dismissed a permission prompt for type |permission| | |
| 63 // on |url| to a website setting. Returns the updated number of dismissals. | |
| 64 int RecordDismissalCount(Profile* profile, | |
| 65 const GURL& url, | |
| 66 content::PermissionType permission) { | |
| 67 HostContentSettingsMap* map = | |
| 68 HostContentSettingsMapFactory::GetForProfile(profile); | |
| 69 std::unique_ptr<base::DictionaryValue> dict = GetOriginDict(map, url); | |
| 70 if (!dict.get()) | |
| 71 return 0; | |
| 72 | |
| 73 base::DictionaryValue* permission_dict = GetPermissionDict( | |
| 74 dict.get(), PermissionUtil::GetPermissionString(permission)); | |
| 75 | |
| 76 if (!permission_dict) | |
| 77 return 0; | |
| 78 | |
| 79 int current_count = 0; | |
| 80 permission_dict->GetInteger(kPromptDismissCountKey, ¤t_count); | |
| 81 permission_dict->SetInteger(kPromptDismissCountKey, ++current_count); | |
| 82 | |
| 83 map->SetWebsiteSettingDefaultScope( | |
| 84 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, | |
| 85 std::string(), std::move(dict)); | |
| 86 | |
| 87 return current_count; | |
| 88 } | |
| 89 | |
| 90 } // anonymous namespace | |
| 12 | 91 |
| 13 using content::PermissionType; | 92 using content::PermissionType; |
| 14 | 93 |
| 15 std::size_t PermissionTypeHash::operator()( | 94 std::size_t PermissionTypeHash::operator()( |
| 16 const content::PermissionType& type) const { | 95 const content::PermissionType& type) const { |
| 17 return static_cast<size_t>(type); | 96 return static_cast<size_t>(type); |
| 18 } | 97 } |
| 19 | 98 |
| 20 // The returned strings must match the RAPPOR metrics in rappor.xml, | 99 // The returned strings must match the RAPPOR metrics in rappor.xml, |
| 21 // and any Field Trial configs for the Permissions kill switch e.g. | 100 // and any Field Trial configs for the Permissions kill switch e.g. |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 ContentSetting final_content_setting = settings_map->GetContentSetting( | 203 ContentSetting final_content_setting = settings_map->GetContentSetting( |
| 125 primary_url_, secondary_url_, content_type_, std::string()); | 204 primary_url_, secondary_url_, content_type_, std::string()); |
| 126 if (final_content_setting != CONTENT_SETTING_ALLOW) { | 205 if (final_content_setting != CONTENT_SETTING_ALLOW) { |
| 127 PermissionType permission_type; | 206 PermissionType permission_type; |
| 128 if (PermissionUtil::GetPermissionType(content_type_, &permission_type)) { | 207 if (PermissionUtil::GetPermissionType(content_type_, &permission_type)) { |
| 129 PermissionUmaUtil::PermissionRevoked(permission_type, source_ui_, | 208 PermissionUmaUtil::PermissionRevoked(permission_type, source_ui_, |
| 130 primary_url_, profile_); | 209 primary_url_, profile_); |
| 131 } | 210 } |
| 132 } | 211 } |
| 133 } | 212 } |
| 213 | |
| 214 bool PermissionUtil::ShouldChangeDismissalToBlock( | |
| 215 Profile* profile, | |
| 216 const GURL& url, | |
| 217 content::PermissionType permission) { | |
| 218 int current_dismissal_count = RecordDismissalCount(profile, url, permission); | |
|
kcarattini
2016/07/28 08:54:49
So this count is kept forever, is that correct? It
dominickn
2016/08/01 01:08:51
I think that keeping the count forever is correct.
| |
| 219 | |
| 220 if (!base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)) | |
| 221 return false; | |
| 222 | |
| 223 if (!gUpdatedFromVariations) { | |
| 224 int prompt_dismissals = -1; | |
| 225 std::string value = variations::GetVariationParamValue( | |
| 226 kPromptStudyName, kPromptDismissCountKey); | |
| 227 if (base::StringToInt(value, &prompt_dismissals) && prompt_dismissals > 0) | |
| 228 gPromptDismissalsBeforeBlock = prompt_dismissals; | |
| 229 | |
| 230 gUpdatedFromVariations = true; | |
| 231 } | |
| 232 | |
| 233 return current_dismissal_count >= gPromptDismissalsBeforeBlock; | |
| 234 } | |
|
raymes
2016/07/28 08:07:50
nit: suggestion: since there is a reasonable amoun
dominickn
2016/08/01 01:08:51
Done. Naming is hard, please let me know if you ha
| |
| OLD | NEW |