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; | |
| 26 | |
| 27 bool gUpdatedFromVariations = false; | |
| 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) | |
|
raymes
2016/07/28 08:07:50
I believe this should never be null
dominickn
2016/08/01 01:08:50
Done.
| |
| 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)); | |
|
raymes
2016/07/28 08:07:50
It might be potentially confusing that this Get* f
dominickn
2016/08/01 01:08:51
I've renamed the method to GetOrCreatePermissionDi
| |
| 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()) | |
|
raymes
2016/07/28 08:07:50
Can this ever be null?
dominickn
2016/08/01 01:08:51
Done.
| |
| 71 return 0; | |
| 72 | |
| 73 base::DictionaryValue* permission_dict = GetPermissionDict( | |
| 74 dict.get(), PermissionUtil::GetPermissionString(permission)); | |
| 75 | |
| 76 if (!permission_dict) | |
|
raymes
2016/07/28 08:07:50
Can this ever be null?
dominickn
2016/08/01 01:08:51
Done.
| |
| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 PermissionType permission_type; | 180 PermissionType permission_type; |
| 102 if (PermissionUtil::GetPermissionType(content_type, &permission_type)) { | 181 if (PermissionUtil::GetPermissionType(content_type, &permission_type)) { |
| 103 // TODO(stefanocs): Report revocations from page action as PAGE_ACTION | 182 // TODO(stefanocs): Report revocations from page action as PAGE_ACTION |
| 104 // source UI instead of SITE_SETTINGS source UI. | 183 // source UI instead of SITE_SETTINGS source UI. |
| 105 PermissionUmaUtil::PermissionRevoked(permission_type, | 184 PermissionUmaUtil::PermissionRevoked(permission_type, |
| 106 PermissionSourceUI::SITE_SETTINGS, | 185 PermissionSourceUI::SITE_SETTINGS, |
| 107 primary_url, profile); | 186 primary_url, profile); |
| 108 } | 187 } |
| 109 } | 188 } |
| 110 } | 189 } |
| 190 | |
| 191 bool PermissionUtil::ShouldChangeDismissalToBlock( | |
| 192 Profile* profile, | |
| 193 const GURL& url, | |
| 194 content::PermissionType permission) { | |
| 195 int current_dismissal_count = RecordDismissalCount(profile, url, permission); | |
|
raymes
2016/07/28 08:07:50
Do we want to record the dismissal count even if t
dominickn
2016/08/01 01:08:51
This was a deliberate choice - the overhead seems
| |
| 196 | |
| 197 if (!base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)) | |
| 198 return false; | |
| 199 | |
| 200 if (!gUpdatedFromVariations) { | |
| 201 int prompt_dismissals = -1; | |
| 202 std::string value = variations::GetVariationParamValue( | |
| 203 kPromptStudyName, kPromptDismissCountKey); | |
| 204 if (base::StringToInt(value, &prompt_dismissals) && prompt_dismissals > 0) | |
|
raymes
2016/07/28 08:07:50
Do you know in what cases this won't succeed? If i
dominickn
2016/08/01 01:08:50
Hmm, catching a potential bad Finch config seems l
| |
| 205 gPromptDismissalsBeforeBlock = prompt_dismissals; | |
| 206 | |
| 207 gUpdatedFromVariations = true; | |
| 208 } | |
| 209 | |
| 210 return current_dismissal_count >= gPromptDismissalsBeforeBlock; | |
| 211 } | |
| OLD | NEW |