OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/subresource_filter/subresource_filter_content_settings_
manager.h" | 5 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_
manager.h" |
6 | 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/feature_list.h" |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/time/default_clock.h" |
| 13 #include "base/values.h" |
8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 14 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
9 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 16 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
11 #include "components/content_settings/core/browser/content_settings_details.h" | 17 #include "components/content_settings/core/browser/content_settings_details.h" |
12 #include "components/content_settings/core/browser/host_content_settings_map.h" | 18 #include "components/content_settings/core/browser/host_content_settings_map.h" |
13 #include "components/content_settings/core/common/content_settings.h" | 19 #include "components/content_settings/core/common/content_settings.h" |
14 #include "components/content_settings/core/common/content_settings_pattern.h" | 20 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 21 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
15 #include "url/gurl.h" | 22 #include "url/gurl.h" |
16 | 23 |
| 24 namespace { |
| 25 |
| 26 // Key into the website setting dict for the smart UI. |
| 27 const char kInfobarLastShownTimeKey[] = "InfobarLastShownTime"; |
| 28 |
| 29 bool ShouldUseSmartUI() { |
| 30 #if defined(OS_ANDROID) |
| 31 return base::FeatureList::IsEnabled( |
| 32 subresource_filter::kSafeBrowsingSubresourceFilterExperimentalUI); |
| 33 #endif |
| 34 return false; |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 |
| 39 constexpr base::TimeDelta |
| 40 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain = |
| 41 base::TimeDelta::FromHours(24); |
| 42 |
17 SubresourceFilterContentSettingsManager:: | 43 SubresourceFilterContentSettingsManager:: |
18 SubresourceFilterContentSettingsManager(Profile* profile) | 44 SubresourceFilterContentSettingsManager(Profile* profile) |
19 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { | 45 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)), |
| 46 clock_(base::MakeUnique<base::DefaultClock>(base::DefaultClock())), |
| 47 should_use_smart_ui_(ShouldUseSmartUI()) { |
20 DCHECK(profile); | 48 DCHECK(profile); |
21 DCHECK(settings_map_); | 49 DCHECK(settings_map_); |
22 settings_map_->AddObserver(this); | 50 settings_map_->AddObserver(this); |
23 } | 51 } |
24 | 52 |
25 SubresourceFilterContentSettingsManager:: | 53 SubresourceFilterContentSettingsManager:: |
26 ~SubresourceFilterContentSettingsManager() {} | 54 ~SubresourceFilterContentSettingsManager() {} |
27 | 55 |
28 void SubresourceFilterContentSettingsManager::Shutdown() { | 56 void SubresourceFilterContentSettingsManager::Shutdown() { |
29 settings_map_->RemoveObserver(this); | 57 settings_map_->RemoveObserver(this); |
30 settings_map_ = nullptr; | 58 settings_map_ = nullptr; |
31 } | 59 } |
32 | 60 |
| 61 ContentSetting SubresourceFilterContentSettingsManager::GetContentSetting( |
| 62 const GURL& url) const { |
| 63 return settings_map_->GetContentSetting( |
| 64 url, GURL(), |
| 65 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 66 std::string()); |
| 67 } |
| 68 |
| 69 void SubresourceFilterContentSettingsManager::SetContentSetting( |
| 70 const GURL& url, |
| 71 ContentSetting setting, |
| 72 bool from_ui) { |
| 73 base::AutoReset<bool> resetter(&ignore_settings_changes_, true); |
| 74 settings_map_->SetContentSettingDefaultScope( |
| 75 url, GURL(), |
| 76 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 77 std::string(), setting); |
| 78 if (from_ui) { |
| 79 ChromeSubresourceFilterClient::LogAction( |
| 80 kActionContentSettingsBlockedFromUI); |
| 81 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
| 82 } |
| 83 } |
| 84 |
| 85 void SubresourceFilterContentSettingsManager::ClearContentSetting( |
| 86 const GURL& url) { |
| 87 base::AutoReset<bool> resetter(&ignore_settings_changes_, true); |
| 88 auto predicate = base::Bind( |
| 89 [](const ContentSettingsPattern& pattern_to_delete, |
| 90 const ContentSettingsPattern& primary, |
| 91 const ContentSettingsPattern& seconary) { |
| 92 return pattern_to_delete == primary; |
| 93 }, |
| 94 ContentSettingsPattern::FromURLNoWildcard(url)); |
| 95 settings_map_->ClearSettingsForOneTypeWithPredicate( |
| 96 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 97 std::move(predicate)); |
| 98 } |
| 99 |
| 100 void SubresourceFilterContentSettingsManager::OnDidShowUI(const GURL& url) { |
| 101 // Explicitly set the content setting to "Allow" to ensure it always shows up |
| 102 // in the settings UI. |
| 103 SetContentSetting(url, CONTENT_SETTING_ALLOW, false /* from_ui */); |
| 104 |
| 105 if (!should_use_smart_ui()) |
| 106 return; |
| 107 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 108 double now = clock_->Now().ToDoubleT(); |
| 109 dict->SetDouble(kInfobarLastShownTimeKey, now); |
| 110 SetWebsiteSetting(url, std::move(dict)); |
| 111 } |
| 112 |
| 113 bool SubresourceFilterContentSettingsManager::ShouldShowUIForSite( |
| 114 const GURL& url) const { |
| 115 if (!should_use_smart_ui()) |
| 116 return true; |
| 117 |
| 118 std::unique_ptr<base::DictionaryValue> dict = GetWebsiteSetting(url); |
| 119 if (!dict) |
| 120 return true; |
| 121 |
| 122 double last_shown_time_double = 0; |
| 123 if (dict->GetDouble(kInfobarLastShownTimeKey, &last_shown_time_double)) { |
| 124 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); |
| 125 if (clock_->Now() - last_shown < kDelayBeforeShowingInfobarAgain) |
| 126 return false; |
| 127 } |
| 128 return true; |
| 129 } |
| 130 |
| 131 std::unique_ptr<base::DictionaryValue> |
| 132 SubresourceFilterContentSettingsManager::GetWebsiteSetting( |
| 133 const GURL& url) const { |
| 134 return base::DictionaryValue::From(settings_map_->GetWebsiteSetting( |
| 135 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, std::string(), |
| 136 nullptr)); |
| 137 } |
| 138 |
| 139 void SubresourceFilterContentSettingsManager::SetWebsiteSetting( |
| 140 const GURL& url, |
| 141 std::unique_ptr<base::DictionaryValue> dict) { |
| 142 settings_map_->SetWebsiteSettingDefaultScope( |
| 143 url, GURL(), |
| 144 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, |
| 145 std::string(), std::move(dict)); |
| 146 } |
| 147 |
33 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( | 148 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( |
34 const ContentSettingsPattern& primary_pattern, | 149 const ContentSettingsPattern& primary_pattern, |
35 const ContentSettingsPattern& secondary_pattern, | 150 const ContentSettingsPattern& secondary_pattern, |
36 ContentSettingsType content_type, | 151 ContentSettingsType content_type, |
37 std::string resource_identifier) { | 152 std::string resource_identifier) { |
38 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) | 153 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER || |
| 154 ignore_settings_changes_) |
39 return; | 155 return; |
40 | 156 |
41 const ContentSettingsDetails details(primary_pattern, secondary_pattern, | 157 const ContentSettingsDetails details(primary_pattern, secondary_pattern, |
42 content_type, resource_identifier); | 158 content_type, resource_identifier); |
43 if (details.update_all()) { | 159 if (details.update_all()) { |
44 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( | 160 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( |
45 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); | 161 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); |
46 if (global_setting == CONTENT_SETTING_BLOCK) { | 162 if (global_setting == CONTENT_SETTING_BLOCK) { |
47 ChromeSubresourceFilterClient::LogAction( | 163 ChromeSubresourceFilterClient::LogAction( |
48 kActionContentSettingsBlockedGlobal); | 164 kActionContentSettingsBlockedGlobal); |
(...skipping 23 matching lines...) Expand all Loading... |
72 ContentSetting setting = settings_map_->GetContentSetting( | 188 ContentSetting setting = settings_map_->GetContentSetting( |
73 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 189 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
74 std::string()); | 190 std::string()); |
75 if (setting == CONTENT_SETTING_BLOCK) { | 191 if (setting == CONTENT_SETTING_BLOCK) { |
76 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); | 192 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
77 } else if (setting == CONTENT_SETTING_ALLOW) { | 193 } else if (setting == CONTENT_SETTING_ALLOW) { |
78 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); | 194 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); |
79 } else { | 195 } else { |
80 NOTREACHED(); | 196 NOTREACHED(); |
81 } | 197 } |
| 198 |
| 199 if (!should_use_smart_ui()) |
| 200 return; |
| 201 |
| 202 if (!ShouldShowUIForSite(url)) { |
| 203 ChromeSubresourceFilterClient::LogAction( |
| 204 kActionContentSettingsBlockedWhileUISuppressed); |
| 205 } |
| 206 |
| 207 // Reset the smart UI to ensure tne user can easily change their decision. |
| 208 SetWebsiteSetting(url, nullptr); |
82 } | 209 } |
OLD | NEW |