Chromium Code Reviews| 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/feature_list.h" | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/time/default_clock.h" | |
| 8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | 14 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 11 #include "components/content_settings/core/browser/content_settings_details.h" | 15 #include "components/content_settings/core/browser/content_settings_details.h" |
| 12 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 13 #include "components/content_settings/core/common/content_settings.h" | 17 #include "components/content_settings/core/common/content_settings.h" |
| 14 #include "components/content_settings/core/common/content_settings_pattern.h" | 18 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 19 #include "components/subresource_filter/core/browser/subresource_filter_features .h" | |
| 15 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 16 | 21 |
| 22 namespace { | |
| 23 | |
| 24 // Key into the website setting dict for the smart UI. | |
| 25 const char kUILastShowTime[] = "LastShowTime"; | |
|
engedy
2017/04/12 14:02:50
nit: InfobarLastShownTime ?
Charlie Harrison
2017/04/12 17:53:44
Done.
| |
| 26 | |
| 27 bool ShouldUseSmartUI() { | |
| 28 #if defined(OS_ANDROID) | |
| 29 return base::FeatureList::IsEnabled( | |
| 30 subresource_filter::kSafeBrowsingSubresourceFilterExperimentalUI); | |
| 31 #endif | |
| 32 return false; | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 constexpr base::TimeDelta | |
| 38 SubresourceFilterContentSettingsManager::kUIShowThresholdTime = | |
|
engedy
2017/04/12 14:02:51
nit: Something along the lines of kDelayBeforeShow
Charlie Harrison
2017/04/12 17:53:44
Done.
| |
| 39 base::TimeDelta::FromHours(24); | |
| 40 | |
| 17 SubresourceFilterContentSettingsManager:: | 41 SubresourceFilterContentSettingsManager:: |
| 18 SubresourceFilterContentSettingsManager(Profile* profile) | 42 SubresourceFilterContentSettingsManager(Profile* profile) |
| 19 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { | 43 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)), |
| 44 clock_(base::MakeUnique<base::DefaultClock>(base::DefaultClock())), | |
| 45 should_use_smart_ui_(ShouldUseSmartUI()) { | |
| 20 DCHECK(profile); | 46 DCHECK(profile); |
| 21 DCHECK(settings_map_); | 47 DCHECK(settings_map_); |
| 22 settings_map_->AddObserver(this); | 48 settings_map_->AddObserver(this); |
| 23 } | 49 } |
| 24 | 50 |
| 25 SubresourceFilterContentSettingsManager:: | 51 SubresourceFilterContentSettingsManager:: |
| 26 ~SubresourceFilterContentSettingsManager() {} | 52 ~SubresourceFilterContentSettingsManager() {} |
| 27 | 53 |
| 28 void SubresourceFilterContentSettingsManager::Shutdown() { | 54 void SubresourceFilterContentSettingsManager::Shutdown() { |
| 29 settings_map_->RemoveObserver(this); | 55 settings_map_->RemoveObserver(this); |
| 30 settings_map_ = nullptr; | 56 settings_map_ = nullptr; |
| 31 } | 57 } |
| 32 | 58 |
| 59 ContentSetting SubresourceFilterContentSettingsManager::GetContentSetting( | |
| 60 const GURL& url) const { | |
| 61 return settings_map_->GetContentSetting( | |
| 62 url, GURL(), | |
| 63 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
| 64 std::string()); | |
| 65 } | |
| 66 | |
| 67 void SubresourceFilterContentSettingsManager::SetContentSetting( | |
| 68 const GURL& url, | |
| 69 ContentSetting setting, | |
| 70 bool log_metrics) { | |
| 71 base::AutoReset<bool>(&ignore_settings_changes_, !log_metrics); | |
|
engedy
2017/04/12 14:02:50
|ignore_settings_changes_| is never read. Let's al
Charlie Harrison
2017/04/12 17:53:44
Done. Great catch. Fallout from the previous refac
| |
| 72 settings_map_->SetContentSettingDefaultScope( | |
| 73 url, GURL(), | |
| 74 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
| 75 std::string(), setting); | |
| 76 } | |
| 77 | |
| 78 void SubresourceFilterContentSettingsManager::ClearContentSetting( | |
| 79 const GURL& url) { | |
| 80 base::AutoReset<bool>(&ignore_settings_changes_, true); | |
| 81 auto predicate = base::Bind( | |
|
engedy
2017/04/12 14:02:50
#include "base/bind.h"
Charlie Harrison
2017/04/12 17:53:44
Done.
| |
| 82 [](const ContentSettingsPattern& pattern_to_delete, | |
| 83 const ContentSettingsPattern& primary, | |
| 84 const ContentSettingsPattern& seconary) { | |
| 85 return pattern_to_delete == primary; | |
| 86 }, | |
| 87 ContentSettingsPattern::FromURLNoWildcard(url)); | |
|
engedy
2017/04/12 14:02:50
If I understand correctly, this is per-origin gran
Charlie Harrison
2017/04/12 17:53:44
Ok. So, if safebrowsing does chunking it means tha
| |
| 88 settings_map_->ClearSettingsForOneTypeWithPredicate( | |
| 89 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
| 90 std::move(predicate)); | |
| 91 } | |
| 92 | |
| 93 void SubresourceFilterContentSettingsManager::OnDidShowUI(const GURL& url) { | |
| 94 // Explicitly set the content setting to "Allow" to ensure it always shows up | |
| 95 // in the settings UI. | |
| 96 SetContentSetting(url, CONTENT_SETTING_ALLOW, false /* log_metrics */); | |
| 97 | |
| 98 if (!should_use_smart_ui()) | |
| 99 return; | |
| 100 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
|
engedy
2017/04/12 14:02:51
nit: Do we need to include "base/values.h"?
Charlie Harrison
2017/04/12 17:53:44
Done.
| |
| 101 double now = clock_->Now().ToDoubleT(); | |
| 102 dict->SetDouble(kUILastShowTime, now); | |
| 103 SetWebsiteSetting(url, std::move(dict)); | |
| 104 } | |
| 105 | |
| 106 bool SubresourceFilterContentSettingsManager::ShouldShowUIForSite( | |
| 107 const GURL& url) const { | |
| 108 if (!should_use_smart_ui()) | |
| 109 return true; | |
| 110 | |
| 111 std::unique_ptr<base::DictionaryValue> dict = GetWebsiteSetting(url); | |
| 112 if (!dict) | |
| 113 return true; | |
| 114 | |
| 115 double last_shown_time_double = 0; | |
| 116 if (dict->GetDouble(kUILastShowTime, &last_shown_time_double)) { | |
| 117 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); | |
| 118 if (clock_->Now() - last_shown < kUIShowThresholdTime) | |
| 119 return false; | |
| 120 } | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 std::unique_ptr<base::DictionaryValue> | |
| 125 SubresourceFilterContentSettingsManager::GetWebsiteSetting( | |
| 126 const GURL& url) const { | |
| 127 return base::DictionaryValue::From(settings_map_->GetWebsiteSetting( | |
| 128 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, std::string(), | |
| 129 nullptr)); | |
| 130 } | |
| 131 | |
| 132 void SubresourceFilterContentSettingsManager::SetWebsiteSetting( | |
| 133 const GURL& url, | |
| 134 std::unique_ptr<base::DictionaryValue> dict) { | |
| 135 settings_map_->SetWebsiteSettingDefaultScope( | |
| 136 url, GURL(), | |
| 137 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, | |
| 138 std::string(), std::move(dict)); | |
| 139 } | |
| 140 | |
| 33 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( | 141 void SubresourceFilterContentSettingsManager::OnContentSettingChanged( |
| 34 const ContentSettingsPattern& primary_pattern, | 142 const ContentSettingsPattern& primary_pattern, |
| 35 const ContentSettingsPattern& secondary_pattern, | 143 const ContentSettingsPattern& secondary_pattern, |
| 36 ContentSettingsType content_type, | 144 ContentSettingsType content_type, |
| 37 std::string resource_identifier) { | 145 std::string resource_identifier) { |
| 38 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) | 146 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) |
| 39 return; | 147 return; |
| 40 | 148 |
| 41 const ContentSettingsDetails details(primary_pattern, secondary_pattern, | 149 const ContentSettingsDetails details(primary_pattern, secondary_pattern, |
| 42 content_type, resource_identifier); | 150 content_type, resource_identifier); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 72 ContentSetting setting = settings_map_->GetContentSetting( | 180 ContentSetting setting = settings_map_->GetContentSetting( |
| 73 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 181 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 74 std::string()); | 182 std::string()); |
| 75 if (setting == CONTENT_SETTING_BLOCK) { | 183 if (setting == CONTENT_SETTING_BLOCK) { |
| 76 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); | 184 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
| 77 } else if (setting == CONTENT_SETTING_ALLOW) { | 185 } else if (setting == CONTENT_SETTING_ALLOW) { |
| 78 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); | 186 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); |
| 79 } else { | 187 } else { |
| 80 NOTREACHED(); | 188 NOTREACHED(); |
| 81 } | 189 } |
| 190 | |
| 191 if (!should_use_smart_ui()) | |
| 192 return; | |
| 193 | |
| 194 if (!ShouldShowUIForSite(url)) { | |
| 195 ChromeSubresourceFilterClient::LogAction( | |
| 196 kActionContentSettingsBlockedWhileUISuppressed); | |
|
engedy
2017/04/12 14:02:50
Wouldn't we reach this even if the user toggled th
Charlie Harrison
2017/04/12 17:53:44
I've fixed this by changing "log_metrics" to "from
| |
| 197 } | |
| 198 | |
| 199 // Reset the smart UI to ensure tne user can easily change their decision. | |
| 200 SetWebsiteSetting(url, nullptr); | |
| 82 } | 201 } |
| OLD | NEW |