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" | 7 #include "base/auto_reset.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 SubresourceFilterContentSettingsManager:: | 45 SubresourceFilterContentSettingsManager:: |
46 SubresourceFilterContentSettingsManager(Profile* profile) | 46 SubresourceFilterContentSettingsManager(Profile* profile) |
47 : history_observer_(this), | 47 : history_observer_(this), |
48 settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)), | 48 settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)), |
49 clock_(base::MakeUnique<base::DefaultClock>(base::DefaultClock())), | 49 clock_(base::MakeUnique<base::DefaultClock>(base::DefaultClock())), |
50 should_use_smart_ui_(ShouldUseSmartUI()) { | 50 should_use_smart_ui_(ShouldUseSmartUI()) { |
51 DCHECK(profile); | 51 DCHECK(profile); |
52 DCHECK(settings_map_); | 52 DCHECK(settings_map_); |
53 settings_map_->AddObserver(this); | 53 settings_map_->AddObserver(this); |
54 | 54 |
55 if (should_use_smart_ui()) { | 55 if (auto* history_service = HistoryServiceFactory::GetForProfile( |
56 if (auto* history_service = HistoryServiceFactory::GetForProfile( | 56 profile, ServiceAccessType::EXPLICIT_ACCESS)) { |
57 profile, ServiceAccessType::EXPLICIT_ACCESS)) { | 57 history_observer_.Add(history_service); |
58 history_observer_.Add(history_service); | |
59 } | |
60 } | 58 } |
61 } | 59 } |
62 | 60 |
63 SubresourceFilterContentSettingsManager:: | 61 SubresourceFilterContentSettingsManager:: |
64 ~SubresourceFilterContentSettingsManager() { | 62 ~SubresourceFilterContentSettingsManager() { |
65 settings_map_->RemoveObserver(this); | 63 settings_map_->RemoveObserver(this); |
66 settings_map_ = nullptr; | 64 settings_map_ = nullptr; |
67 history_observer_.RemoveAll(); | 65 history_observer_.RemoveAll(); |
68 } | 66 } |
69 | 67 |
70 ContentSetting SubresourceFilterContentSettingsManager::GetSitePermission( | 68 ContentSetting SubresourceFilterContentSettingsManager::GetSitePermission( |
71 const GURL& url) const { | 69 const GURL& url) const { |
72 return settings_map_->GetContentSetting( | 70 return settings_map_->GetContentSetting( |
73 url, GURL(), | 71 url, GURL(), |
74 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 72 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
75 std::string()); | 73 std::string()); |
76 } | 74 } |
77 | 75 |
78 void SubresourceFilterContentSettingsManager::WhitelistSite(const GURL& url) { | 76 void SubresourceFilterContentSettingsManager::WhitelistSite(const GURL& url) { |
79 base::AutoReset<bool> resetter(&ignore_settings_changes_, true); | 77 base::AutoReset<bool> resetter(&ignore_settings_changes_, true); |
80 settings_map_->SetContentSettingDefaultScope( | 78 settings_map_->SetContentSettingDefaultScope( |
81 url, GURL(), | 79 url, GURL(), |
82 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 80 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
83 std::string(), CONTENT_SETTING_BLOCK); | 81 std::string(), CONTENT_SETTING_BLOCK); |
84 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlockedFromUI); | 82 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlockedFromUI); |
85 } | 83 } |
86 | 84 |
87 void SubresourceFilterContentSettingsManager::OnDidShowUI(const GURL& url) { | 85 void SubresourceFilterContentSettingsManager::OnDidShowUI(const GURL& url) { |
88 if (!should_use_smart_ui()) | |
89 return; | |
90 auto dict = base::MakeUnique<base::DictionaryValue>(); | 86 auto dict = base::MakeUnique<base::DictionaryValue>(); |
91 double now = clock_->Now().ToDoubleT(); | 87 double now = clock_->Now().ToDoubleT(); |
92 dict->SetDouble(kInfobarLastShownTimeKey, now); | 88 dict->SetDouble(kInfobarLastShownTimeKey, now); |
93 SetSiteMetadata(url, std::move(dict)); | 89 SetSiteMetadata(url, std::move(dict)); |
94 } | 90 } |
95 | 91 |
96 bool SubresourceFilterContentSettingsManager::ShouldShowUIForSite( | 92 bool SubresourceFilterContentSettingsManager::ShouldShowUIForSite( |
97 const GURL& url) const { | 93 const GURL& url) const { |
98 if (!should_use_smart_ui()) | 94 if (!should_use_smart_ui()) |
99 return true; | 95 return true; |
100 | 96 |
101 std::unique_ptr<base::DictionaryValue> dict = GetSiteMetadata(url); | 97 std::unique_ptr<base::DictionaryValue> dict = GetSiteMetadata(url); |
102 if (!dict) | 98 if (!dict) |
103 return true; | 99 return true; |
104 | 100 |
105 double last_shown_time_double = 0; | 101 double last_shown_time_double = 0; |
106 if (dict->GetDouble(kInfobarLastShownTimeKey, &last_shown_time_double)) { | 102 if (dict->GetDouble(kInfobarLastShownTimeKey, &last_shown_time_double)) { |
107 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); | 103 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); |
108 if (clock_->Now() - last_shown < kDelayBeforeShowingInfobarAgain) | 104 if (clock_->Now() - last_shown < kDelayBeforeShowingInfobarAgain) |
109 return false; | 105 return false; |
110 } | 106 } |
111 return true; | 107 return true; |
112 } | 108 } |
113 | 109 |
| 110 void SubresourceFilterContentSettingsManager::ClearSiteMetadata( |
| 111 const GURL& url) { |
| 112 SetSiteMetadata(url, nullptr); |
| 113 } |
| 114 |
114 std::unique_ptr<base::DictionaryValue> | 115 std::unique_ptr<base::DictionaryValue> |
115 SubresourceFilterContentSettingsManager::GetSiteMetadata( | 116 SubresourceFilterContentSettingsManager::GetSiteMetadata( |
116 const GURL& url) const { | 117 const GURL& url) const { |
117 return base::DictionaryValue::From(settings_map_->GetWebsiteSetting( | 118 return base::DictionaryValue::From(settings_map_->GetWebsiteSetting( |
118 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, std::string(), | 119 url, GURL(), CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, std::string(), |
119 nullptr)); | 120 nullptr)); |
120 } | 121 } |
121 | 122 |
122 void SubresourceFilterContentSettingsManager::SetSiteMetadata( | 123 void SubresourceFilterContentSettingsManager::SetSiteMetadata( |
123 const GURL& url, | 124 const GURL& url, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 173 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
173 std::string()); | 174 std::string()); |
174 if (setting == CONTENT_SETTING_BLOCK) { | 175 if (setting == CONTENT_SETTING_BLOCK) { |
175 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); | 176 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
176 } else if (setting == CONTENT_SETTING_ALLOW) { | 177 } else if (setting == CONTENT_SETTING_ALLOW) { |
177 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); | 178 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); |
178 } else { | 179 } else { |
179 NOTREACHED(); | 180 NOTREACHED(); |
180 } | 181 } |
181 | 182 |
182 if (!should_use_smart_ui()) | |
183 return; | |
184 | |
185 if (!ShouldShowUIForSite(url)) { | 183 if (!ShouldShowUIForSite(url)) { |
186 ChromeSubresourceFilterClient::LogAction( | 184 ChromeSubresourceFilterClient::LogAction( |
187 kActionContentSettingsBlockedWhileUISuppressed); | 185 kActionContentSettingsBlockedWhileUISuppressed); |
188 } | 186 } |
189 | 187 // Note: could potentially reset the smart UI here to enable the user to |
190 // Reset the smart UI to ensure the user can easily change their decision. | 188 // easily change their decision. If that code is added we should make sure not |
191 SetSiteMetadata(url, nullptr); | 189 // to delete the metadata, but merely remove / reset the timestamp. |
192 } | 190 } |
193 | 191 |
194 // When history URLs are deleted, clear the metadata for the smart UI. | 192 // When history URLs are deleted, clear the metadata for the smart UI. |
195 void SubresourceFilterContentSettingsManager::OnURLsDeleted( | 193 void SubresourceFilterContentSettingsManager::OnURLsDeleted( |
196 history::HistoryService* history_service, | 194 history::HistoryService* history_service, |
197 bool all_history, | 195 bool all_history, |
198 bool expired, | 196 bool expired, |
199 const history::URLRows& deleted_rows, | 197 const history::URLRows& deleted_rows, |
200 const std::set<GURL>& favicon_urls) { | 198 const std::set<GURL>& favicon_urls) { |
201 DCHECK(should_use_smart_ui()); | |
202 if (all_history) { | 199 if (all_history) { |
203 settings_map_->ClearSettingsForOneType( | 200 settings_map_->ClearSettingsForOneType( |
204 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA); | 201 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA); |
205 return; | 202 return; |
206 } | 203 } |
207 | 204 |
208 // Careful note: This clears the setting even if there are other URLs with the | 205 // 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. | 206 // same origin in the history. i.e. this is an aggressive policy. |
210 for (const auto& deleted_row : deleted_rows) { | 207 for (const auto& deleted_row : deleted_rows) { |
211 SetSiteMetadata(deleted_row.url(), nullptr); | 208 SetSiteMetadata(deleted_row.url(), nullptr); |
212 } | 209 } |
213 } | 210 } |
OLD | NEW |