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" |
| 15 #include "chrome/browser/history/history_service_factory.h" |
9 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 17 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
11 #include "components/content_settings/core/browser/content_settings_details.h" | 18 #include "components/content_settings/core/browser/content_settings_details.h" |
12 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
13 #include "components/content_settings/core/common/content_settings.h" | 20 #include "components/content_settings/core/common/content_settings.h" |
14 #include "components/content_settings/core/common/content_settings_pattern.h" | 21 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 22 #include "components/history/core/browser/history_service.h" |
| 23 #include "components/keyed_service/core/service_access_type.h" |
| 24 #include "components/subresource_filter/core/browser/subresource_filter_features
.h" |
15 #include "url/gurl.h" | 25 #include "url/gurl.h" |
16 | 26 |
| 27 namespace { |
| 28 |
| 29 // Key into the website setting dict for the smart UI. |
| 30 const char kInfobarLastShownTimeKey[] = "InfobarLastShownTime"; |
| 31 |
| 32 bool ShouldUseSmartUI() { |
| 33 #if defined(OS_ANDROID) |
| 34 return base::FeatureList::IsEnabled( |
| 35 subresource_filter::kSafeBrowsingSubresourceFilterExperimentalUI); |
| 36 #endif |
| 37 return false; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 constexpr base::TimeDelta |
| 43 SubresourceFilterContentSettingsManager::kDelayBeforeShowingInfobarAgain; |
| 44 |
17 SubresourceFilterContentSettingsManager:: | 45 SubresourceFilterContentSettingsManager:: |
18 SubresourceFilterContentSettingsManager(Profile* profile) | 46 SubresourceFilterContentSettingsManager(Profile* profile) |
19 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { | 47 : history_observer_(this), |
| 48 settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)), |
| 49 clock_(base::MakeUnique<base::DefaultClock>(base::DefaultClock())), |
| 50 should_use_smart_ui_(ShouldUseSmartUI()) { |
20 DCHECK(profile); | 51 DCHECK(profile); |
21 DCHECK(settings_map_); | 52 DCHECK(settings_map_); |
22 settings_map_->AddObserver(this); | 53 settings_map_->AddObserver(this); |
| 54 |
| 55 if (should_use_smart_ui()) { |
| 56 if (auto* history_service = HistoryServiceFactory::GetForProfile( |
| 57 profile, ServiceAccessType::EXPLICIT_ACCESS)) { |
| 58 history_observer_.Add(history_service); |
| 59 } |
| 60 } |
23 } | 61 } |
24 | 62 |
25 SubresourceFilterContentSettingsManager:: | 63 SubresourceFilterContentSettingsManager:: |
26 ~SubresourceFilterContentSettingsManager() { | 64 ~SubresourceFilterContentSettingsManager() { |
27 settings_map_->RemoveObserver(this); | 65 settings_map_->RemoveObserver(this); |
| 66 settings_map_ = nullptr; |
| 67 history_observer_.RemoveAll(); |
| 68 } |
| 69 |
| 70 ContentSetting SubresourceFilterContentSettingsManager::GetSitePermission( |
| 71 const GURL& url) const { |
| 72 return settings_map_->GetContentSetting( |
| 73 url, GURL(), |
| 74 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 75 std::string()); |
| 76 } |
| 77 |
| 78 void SubresourceFilterContentSettingsManager::SetSitePermission( |
| 79 const GURL& url, |
| 80 ContentSetting setting, |
| 81 bool from_ui) { |
| 82 base::AutoReset<bool> resetter(&ignore_settings_changes_, true); |
| 83 settings_map_->SetContentSettingDefaultScope( |
| 84 url, GURL(), |
| 85 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 86 std::string(), setting); |
| 87 if (from_ui) { |
| 88 DCHECK_EQ(CONTENT_SETTING_BLOCK, setting); |
| 89 ChromeSubresourceFilterClient::LogAction( |
| 90 kActionContentSettingsBlockedFromUI); |
| 91 } |
| 92 } |
| 93 |
| 94 void SubresourceFilterContentSettingsManager::OnDidShowUI(const GURL& url) { |
| 95 if (!should_use_smart_ui()) |
| 96 return; |
| 97 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 98 double now = clock_->Now().ToDoubleT(); |
| 99 dict->SetDouble(kInfobarLastShownTimeKey, now); |
| 100 SetSiteMetadata(url, std::move(dict)); |
| 101 } |
| 102 |
| 103 bool SubresourceFilterContentSettingsManager::ShouldShowUIForSite( |
| 104 const GURL& url) const { |
| 105 if (!should_use_smart_ui()) |
| 106 return true; |
| 107 |
| 108 std::unique_ptr<base::DictionaryValue> dict = GetSiteMetadata(url); |
| 109 if (!dict) |
| 110 return true; |
| 111 |
| 112 double last_shown_time_double = 0; |
| 113 if (dict->GetDouble(kInfobarLastShownTimeKey, &last_shown_time_double)) { |
| 114 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); |
| 115 if (clock_->Now() - last_shown < kDelayBeforeShowingInfobarAgain) |
| 116 return false; |
| 117 } |
| 118 return true; |
| 119 } |
| 120 |
| 121 std::unique_ptr<base::DictionaryValue> |
| 122 SubresourceFilterContentSettingsManager::GetSiteMetadata( |
| 123 const GURL& url) const { |
| 124 return base::DictionaryValue::From(settings_map_->GetWebsiteSetting( |
| 125 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, std::string(), |
| 126 nullptr)); |
| 127 } |
| 128 |
| 129 void SubresourceFilterContentSettingsManager::SetSiteMetadata( |
| 130 const GURL& url, |
| 131 std::unique_ptr<base::DictionaryValue> dict) { |
| 132 settings_map_->SetWebsiteSettingDefaultScope( |
| 133 url, GURL(), |
| 134 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, |
| 135 std::string(), std::move(dict)); |
28 } | 136 } |
29 | 137 |
30 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( | 138 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( |
31 const ContentSettingsPattern& primary_pattern, | 139 const ContentSettingsPattern& primary_pattern, |
32 const ContentSettingsPattern& secondary_pattern, | 140 const ContentSettingsPattern& secondary_pattern, |
33 ContentSettingsType content_type, | 141 ContentSettingsType content_type, |
34 std::string resource_identifier) { | 142 std::string resource_identifier) { |
35 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) | 143 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER || |
| 144 ignore_settings_changes_) |
36 return; | 145 return; |
37 | 146 |
38 const ContentSettingsDetails details(primary_pattern, secondary_pattern, | 147 const ContentSettingsDetails details(primary_pattern, secondary_pattern, |
39 content_type, resource_identifier); | 148 content_type, resource_identifier); |
40 if (details.update_all()) { | 149 if (details.update_all()) { |
41 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( | 150 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( |
42 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); | 151 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); |
43 if (global_setting == CONTENT_SETTING_BLOCK) { | 152 if (global_setting == CONTENT_SETTING_BLOCK) { |
44 ChromeSubresourceFilterClient::LogAction( | 153 ChromeSubresourceFilterClient::LogAction( |
45 kActionContentSettingsBlockedGlobal); | 154 kActionContentSettingsBlockedGlobal); |
(...skipping 23 matching lines...) Expand all Loading... |
69 ContentSetting setting = settings_map_->GetContentSetting( | 178 ContentSetting setting = settings_map_->GetContentSetting( |
70 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 179 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
71 std::string()); | 180 std::string()); |
72 if (setting == CONTENT_SETTING_BLOCK) { | 181 if (setting == CONTENT_SETTING_BLOCK) { |
73 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); | 182 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
74 } else if (setting == CONTENT_SETTING_ALLOW) { | 183 } else if (setting == CONTENT_SETTING_ALLOW) { |
75 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); | 184 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); |
76 } else { | 185 } else { |
77 NOTREACHED(); | 186 NOTREACHED(); |
78 } | 187 } |
| 188 |
| 189 if (!should_use_smart_ui()) |
| 190 return; |
| 191 |
| 192 if (!ShouldShowUIForSite(url)) { |
| 193 ChromeSubresourceFilterClient::LogAction( |
| 194 kActionContentSettingsBlockedWhileUISuppressed); |
| 195 } |
| 196 |
| 197 // Reset the smart UI to ensure the user can easily change their decision. |
| 198 SetSiteMetadata(url, nullptr); |
79 } | 199 } |
| 200 |
| 201 // When history URLs are deleted, clear the metadata for the smart UI. |
| 202 void SubresourceFilterContentSettingsManager::OnURLsDeleted( |
| 203 history::HistoryService* history_service, |
| 204 bool all_history, |
| 205 bool expired, |
| 206 const history::URLRows& deleted_rows, |
| 207 const std::set<GURL>& favicon_urls) { |
| 208 DCHECK(should_use_smart_ui()); |
| 209 if (all_history) { |
| 210 settings_map_->ClearSettingsForOneType( |
| 211 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA); |
| 212 return; |
| 213 } |
| 214 |
| 215 for (const auto& deleted_row : deleted_rows) { |
| 216 SetSiteMetadata(deleted_row.url(), nullptr); |
| 217 } |
| 218 } |
OLD | NEW |