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::WhitelistSite(const GURL& url) { |
| 79 base::AutoReset<bool> resetter(&ignore_settings_changes_, true); |
| 80 settings_map_->SetContentSettingDefaultScope( |
| 81 url, GURL(), |
| 82 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 83 std::string(), CONTENT_SETTING_BLOCK); |
| 84 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlockedFromUI); |
| 85 } |
| 86 |
| 87 void SubresourceFilterContentSettingsManager::OnDidShowUI(const GURL& url) { |
| 88 if (!should_use_smart_ui()) |
| 89 return; |
| 90 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 91 double now = clock_->Now().ToDoubleT(); |
| 92 dict->SetDouble(kInfobarLastShownTimeKey, now); |
| 93 SetSiteMetadata(url, std::move(dict)); |
| 94 } |
| 95 |
| 96 bool SubresourceFilterContentSettingsManager::ShouldShowUIForSite( |
| 97 const GURL& url) const { |
| 98 if (!should_use_smart_ui()) |
| 99 return true; |
| 100 |
| 101 std::unique_ptr<base::DictionaryValue> dict = GetSiteMetadata(url); |
| 102 if (!dict) |
| 103 return true; |
| 104 |
| 105 double last_shown_time_double = 0; |
| 106 if (dict->GetDouble(kInfobarLastShownTimeKey, &last_shown_time_double)) { |
| 107 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); |
| 108 if (clock_->Now() - last_shown < kDelayBeforeShowingInfobarAgain) |
| 109 return false; |
| 110 } |
| 111 return true; |
| 112 } |
| 113 |
| 114 std::unique_ptr<base::DictionaryValue> |
| 115 SubresourceFilterContentSettingsManager::GetSiteMetadata( |
| 116 const GURL& url) const { |
| 117 return base::DictionaryValue::From(settings_map_->GetWebsiteSetting( |
| 118 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, std::string(), |
| 119 nullptr)); |
| 120 } |
| 121 |
| 122 void SubresourceFilterContentSettingsManager::SetSiteMetadata( |
| 123 const GURL& url, |
| 124 std::unique_ptr<base::DictionaryValue> dict) { |
| 125 settings_map_->SetWebsiteSettingDefaultScope( |
| 126 url, GURL(), |
| 127 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, |
| 128 std::string(), std::move(dict)); |
28 } | 129 } |
29 | 130 |
30 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( | 131 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( |
31 const ContentSettingsPattern& primary_pattern, | 132 const ContentSettingsPattern& primary_pattern, |
32 const ContentSettingsPattern& secondary_pattern, | 133 const ContentSettingsPattern& secondary_pattern, |
33 ContentSettingsType content_type, | 134 ContentSettingsType content_type, |
34 std::string resource_identifier) { | 135 std::string resource_identifier) { |
35 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) | 136 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER || |
| 137 ignore_settings_changes_) |
36 return; | 138 return; |
37 | 139 |
38 const ContentSettingsDetails details(primary_pattern, secondary_pattern, | 140 const ContentSettingsDetails details(primary_pattern, secondary_pattern, |
39 content_type, resource_identifier); | 141 content_type, resource_identifier); |
40 if (details.update_all()) { | 142 if (details.update_all()) { |
41 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( | 143 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( |
42 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); | 144 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); |
43 if (global_setting == CONTENT_SETTING_BLOCK) { | 145 if (global_setting == CONTENT_SETTING_BLOCK) { |
44 ChromeSubresourceFilterClient::LogAction( | 146 ChromeSubresourceFilterClient::LogAction( |
45 kActionContentSettingsBlockedGlobal); | 147 kActionContentSettingsBlockedGlobal); |
(...skipping 23 matching lines...) Expand all Loading... |
69 ContentSetting setting = settings_map_->GetContentSetting( | 171 ContentSetting setting = settings_map_->GetContentSetting( |
70 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 172 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
71 std::string()); | 173 std::string()); |
72 if (setting == CONTENT_SETTING_BLOCK) { | 174 if (setting == CONTENT_SETTING_BLOCK) { |
73 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); | 175 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
74 } else if (setting == CONTENT_SETTING_ALLOW) { | 176 } else if (setting == CONTENT_SETTING_ALLOW) { |
75 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); | 177 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); |
76 } else { | 178 } else { |
77 NOTREACHED(); | 179 NOTREACHED(); |
78 } | 180 } |
| 181 |
| 182 if (!should_use_smart_ui()) |
| 183 return; |
| 184 |
| 185 if (!ShouldShowUIForSite(url)) { |
| 186 ChromeSubresourceFilterClient::LogAction( |
| 187 kActionContentSettingsBlockedWhileUISuppressed); |
| 188 } |
| 189 |
| 190 // Reset the smart UI to ensure the user can easily change their decision. |
| 191 SetSiteMetadata(url, nullptr); |
79 } | 192 } |
| 193 |
| 194 // When history URLs are deleted, clear the metadata for the smart UI. |
| 195 void SubresourceFilterContentSettingsManager::OnURLsDeleted( |
| 196 history::HistoryService* history_service, |
| 197 bool all_history, |
| 198 bool expired, |
| 199 const history::URLRows& deleted_rows, |
| 200 const std::set<GURL>& favicon_urls) { |
| 201 DCHECK(should_use_smart_ui()); |
| 202 if (all_history) { |
| 203 settings_map_->ClearSettingsForOneType( |
| 204 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA); |
| 205 return; |
| 206 } |
| 207 |
| 208 // Careful note: This clears the setting even if there are other URLs with the |
| 209 // same origin in the history. i.e. this is an aggressive policy. |
| 210 for (const auto& deleted_row : deleted_rows) { |
| 211 SetSiteMetadata(deleted_row.url(), nullptr); |
| 212 } |
| 213 } |
OLD | NEW |