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/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 15 #include "chrome/browser/permissions/permission_util.h" | 15 #include "chrome/browser/permissions/permission_util.h" |
| 16 #include "chrome/common/chrome_features.h" | 16 #include "chrome/common/chrome_features.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/variations/variations_associated_data.h" | 18 #include "components/variations/variations_associated_data.h" |
| 19 #include "content/public/browser/permission_type.h" | 19 #include "content/public/browser/permission_type.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // The default number of times that users may explicitly dismiss a permission | 24 // The number of times that users may explicitly dismiss a permission prompt |
| 25 // prompt from an origin before it is automatically blocked. | 25 // from an origin before it is automatically blocked. |
| 26 const int kPromptDismissalsBeforeBlock = 3; | 26 int g_PromptDismissalsBeforeBlock = 3; |
|
kcarattini
2016/10/04 05:26:05
Aren't globals lowercase with underscores?
raymes
2016/10/05 05:02:32
+1
dominickn
2016/10/05 05:55:30
Done, though we do seem to have a consistency issu
| |
| 27 | 27 |
| 28 std::unique_ptr<base::DictionaryValue> GetOriginDict( | 28 std::unique_ptr<base::DictionaryValue> GetOriginDict( |
| 29 HostContentSettingsMap* settings, | 29 HostContentSettingsMap* settings, |
| 30 const GURL& origin_url) { | 30 const GURL& origin_url) { |
| 31 std::unique_ptr<base::DictionaryValue> dict = | 31 std::unique_ptr<base::DictionaryValue> dict = |
| 32 base::DictionaryValue::From(settings->GetWebsiteSetting( | 32 base::DictionaryValue::From(settings->GetWebsiteSetting( |
| 33 origin_url, origin_url, | 33 origin_url, origin_url, |
| 34 CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, std::string(), | 34 CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, std::string(), |
| 35 nullptr)); | 35 nullptr)); |
| 36 if (!dict) | 36 if (!dict) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 } | 133 } |
| 134 | 134 |
| 135 // static | 135 // static |
| 136 int PermissionDecisionAutoBlocker::GetIgnoreCount( | 136 int PermissionDecisionAutoBlocker::GetIgnoreCount( |
| 137 const GURL& url, | 137 const GURL& url, |
| 138 content::PermissionType permission, | 138 content::PermissionType permission, |
| 139 Profile* profile) { | 139 Profile* profile) { |
| 140 return GetActionCount(url, permission, kPromptIgnoreCountKey, profile); | 140 return GetActionCount(url, permission, kPromptIgnoreCountKey, profile); |
| 141 } | 141 } |
| 142 | 142 |
| 143 PermissionDecisionAutoBlocker::PermissionDecisionAutoBlocker(Profile* profile) | 143 // static |
| 144 : profile_(profile), | 144 int PermissionDecisionAutoBlocker::RecordDismiss( |
| 145 prompt_dismissals_before_block_(kPromptDismissalsBeforeBlock) { | 145 const GURL& url, |
| 146 UpdateFromVariations(); | 146 content::PermissionType permission, |
| 147 Profile* profile) { | |
| 148 return RecordActionInWebsiteSettings(url, permission, kPromptDismissCountKey, | |
| 149 profile); | |
| 147 } | 150 } |
| 148 | 151 |
| 152 // static | |
| 149 int PermissionDecisionAutoBlocker::RecordIgnore( | 153 int PermissionDecisionAutoBlocker::RecordIgnore( |
| 150 const GURL& url, | 154 const GURL& url, |
| 151 content::PermissionType permission) { | 155 content::PermissionType permission, |
| 156 Profile* profile) { | |
| 152 return RecordActionInWebsiteSettings(url, permission, kPromptIgnoreCountKey, | 157 return RecordActionInWebsiteSettings(url, permission, kPromptIgnoreCountKey, |
| 153 profile_); | 158 profile); |
| 154 } | 159 } |
| 155 | 160 |
| 161 // static | |
| 156 bool PermissionDecisionAutoBlocker::ShouldChangeDismissalToBlock( | 162 bool PermissionDecisionAutoBlocker::ShouldChangeDismissalToBlock( |
| 157 const GURL& url, | 163 const GURL& url, |
| 158 content::PermissionType permission) { | 164 content::PermissionType permission, |
| 159 int current_dismissal_count = RecordActionInWebsiteSettings( | 165 Profile* profile) { |
| 160 url, permission, kPromptDismissCountKey, profile_); | 166 int current_dismissal_count = RecordDismiss(url, permission, profile); |
| 161 | 167 |
| 162 if (!base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)) | 168 if (!base::FeatureList::IsEnabled(features::kBlockPromptsIfDismissedOften)) |
| 163 return false; | 169 return false; |
| 164 | 170 |
| 165 return current_dismissal_count >= prompt_dismissals_before_block_; | 171 return current_dismissal_count >= g_PromptDismissalsBeforeBlock; |
| 166 } | 172 } |
| 167 | 173 |
| 174 // static | |
| 168 void PermissionDecisionAutoBlocker::UpdateFromVariations() { | 175 void PermissionDecisionAutoBlocker::UpdateFromVariations() { |
| 169 int prompt_dismissals = -1; | 176 int prompt_dismissals = -1; |
| 170 std::string value = variations::GetVariationParamValueByFeature( | 177 std::string value = variations::GetVariationParamValueByFeature( |
| 171 features::kBlockPromptsIfDismissedOften, kPromptDismissCountKey); | 178 features::kBlockPromptsIfDismissedOften, kPromptDismissCountKey); |
| 172 | 179 |
| 173 // If converting the value fails, stick with the default value. | 180 // If converting the value fails, stick with the current value. |
| 174 if (base::StringToInt(value, &prompt_dismissals) && prompt_dismissals > 0) | 181 if (base::StringToInt(value, &prompt_dismissals) && prompt_dismissals > 0) |
| 175 prompt_dismissals_before_block_ = prompt_dismissals; | 182 g_PromptDismissalsBeforeBlock = prompt_dismissals; |
| 176 } | 183 } |
| OLD | NEW |