Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/chrome_subresource_filter_client.h" | 5 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | |
| 9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/time/default_clock.h" | |
| 12 #include "base/values.h" | |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 11 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 12 #include "chrome/browser/infobars/infobar_service.h" | 15 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ observer_factory.h" | 17 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ observer_factory.h" |
| 15 #include "chrome/browser/ui/android/content_settings/subresource_filter_infobar_ delegate.h" | 18 #include "chrome/browser/ui/android/content_settings/subresource_filter_infobar_ delegate.h" |
| 16 #include "components/content_settings/core/browser/host_content_settings_map.h" | 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 17 #include "components/content_settings/core/common/content_settings_types.h" | 20 #include "components/content_settings/core/common/content_settings_types.h" |
| 18 | 21 |
| 22 namespace { | |
| 23 | |
| 24 // Key into the website setting dict for the smart UI. | |
| 25 const char kUILastShowTime[] = "LastShowTime"; | |
| 26 | |
| 27 // Number of hours before showing the UI again on a domain. | |
| 28 // TODO(csharrison): Consider setting this via a finch param. | |
| 29 const int kUIShowThresholdHours = 24; | |
|
engedy
2017/04/04 13:35:59
nit: constexpr base::TimeDelta k...Threshold = bas
Charlie Harrison
2017/04/10 14:58:53
Done.
| |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 19 ChromeSubresourceFilterClient::ChromeSubresourceFilterClient( | 33 ChromeSubresourceFilterClient::ChromeSubresourceFilterClient( |
| 20 content::WebContents* web_contents) | 34 content::WebContents* web_contents) |
| 21 : web_contents_(web_contents), shown_for_navigation_(false) { | 35 : clock_(base::MakeUnique<base::DefaultClock>(base::DefaultClock())), |
| 36 web_contents_(web_contents), | |
| 37 shown_for_navigation_(false) { | |
| 22 DCHECK(web_contents); | 38 DCHECK(web_contents); |
| 23 // Ensure the content settings observer is initialized. | 39 // Ensure the content settings observer is initialized. |
| 24 SubresourceFilterContentSettingsObserverFactory::EnsureForProfile( | 40 SubresourceFilterContentSettingsObserverFactory::EnsureForProfile( |
| 25 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); | 41 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); |
| 26 } | 42 } |
| 27 | 43 |
| 28 ChromeSubresourceFilterClient::~ChromeSubresourceFilterClient() {} | 44 ChromeSubresourceFilterClient::~ChromeSubresourceFilterClient() {} |
| 29 | 45 |
| 30 void ChromeSubresourceFilterClient::ToggleNotificationVisibility( | 46 void ChromeSubresourceFilterClient::ToggleNotificationVisibility( |
| 47 const GURL& url, | |
|
engedy
2017/04/04 13:35:59
nit: Have you considered simply using web_contents
Charlie Harrison
2017/04/10 14:58:53
Ahh, much better :) Done.
| |
| 31 bool visibility) { | 48 bool visibility) { |
| 32 if (shown_for_navigation_ && visibility) | 49 if (shown_for_navigation_ && visibility) |
| 33 return; | 50 return; |
| 34 | 51 shown_for_navigation_ = false; |
| 35 shown_for_navigation_ = visibility; | |
| 36 TabSpecificContentSettings* content_settings = | |
| 37 TabSpecificContentSettings::FromWebContents(web_contents_); | |
| 38 | 52 |
| 39 // |visibility| is false when a new navigation starts. | 53 // |visibility| is false when a new navigation starts. |
| 40 if (visibility) { | 54 if (visibility) { |
| 41 content_settings->OnContentBlocked( | 55 if (!ShouldShowUIForSite(url)) { |
| 42 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER); | 56 LogAction(kActionUISuppressed); |
| 43 LogAction(kActionUIShown); | 57 return; |
| 58 } | |
| 44 #if defined(OS_ANDROID) | 59 #if defined(OS_ANDROID) |
| 45 InfoBarService* infobar_service = | 60 InfoBarService* infobar_service = |
| 46 InfoBarService::FromWebContents(web_contents_); | 61 InfoBarService::FromWebContents(web_contents_); |
| 47 SubresourceFilterInfobarDelegate::Create(infobar_service); | 62 SubresourceFilterInfobarDelegate::Create(infobar_service); |
| 48 #endif | 63 #endif |
| 64 TabSpecificContentSettings* content_settings = | |
| 65 TabSpecificContentSettings::FromWebContents(web_contents_); | |
| 66 content_settings->OnContentBlocked( | |
| 67 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER); | |
| 68 OnUIShown(url); | |
| 49 } else { | 69 } else { |
| 50 LogAction(kActionNavigationStarted); | 70 LogAction(kActionNavigationStarted); |
| 51 } | 71 } |
| 52 } | 72 } |
| 53 | 73 |
| 54 bool ChromeSubresourceFilterClient::IsWhitelistedByContentSettings( | 74 bool ChromeSubresourceFilterClient::IsWhitelistedByContentSettings( |
| 55 const GURL& url) { | 75 const GURL& url) { |
| 56 return GetContentSettingForUrl(url) == CONTENT_SETTING_BLOCK; | 76 ContentSetting setting = GetSettingsMap()->GetContentSetting( |
| 77 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
| 78 std::string()); | |
| 79 return setting == CONTENT_SETTING_BLOCK; | |
| 57 } | 80 } |
| 58 | 81 |
| 59 void ChromeSubresourceFilterClient::WhitelistByContentSettings( | 82 void ChromeSubresourceFilterClient::WhitelistByContentSettings( |
| 60 const GURL& url) { | 83 const GURL& url) { |
| 61 // Whitelist via content settings. | 84 // Whitelist via content settings. |
| 62 Profile* profile = | 85 GetSettingsMap()->SetContentSettingDefaultScope( |
| 63 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
| 64 DCHECK(profile); | |
| 65 HostContentSettingsMap* settings_map = | |
| 66 HostContentSettingsMapFactory::GetForProfile(profile); | |
| 67 settings_map->SetContentSettingDefaultScope( | |
| 68 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 86 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 69 std::string(), CONTENT_SETTING_BLOCK); | 87 std::string(), CONTENT_SETTING_BLOCK); |
| 70 | 88 |
| 71 LogAction(kActionContentSettingsBlockedFromUI); | 89 LogAction(kActionContentSettingsBlockedFromUI); |
| 72 } | 90 } |
| 73 | 91 |
| 74 // static | 92 // static |
| 75 void ChromeSubresourceFilterClient::LogAction(SubresourceFilterAction action) { | 93 void ChromeSubresourceFilterClient::LogAction(SubresourceFilterAction action) { |
| 76 UMA_HISTOGRAM_ENUMERATION("SubresourceFilter.Actions", action, | 94 UMA_HISTOGRAM_ENUMERATION("SubresourceFilter.Actions", action, |
| 77 kActionLastEntry); | 95 kActionLastEntry); |
| 78 } | 96 } |
| 79 | 97 |
| 80 ContentSetting ChromeSubresourceFilterClient::GetContentSettingForUrl( | 98 bool ChromeSubresourceFilterClient::UsingSmartUI() const { |
|
engedy
2017/04/04 13:35:59
nit: How about calling this ShouldUseSmartUI?
Charlie Harrison
2017/04/10 14:58:53
Done.
| |
| 81 const GURL& url) { | 99 #if defined(OS_ANDROID) |
| 100 return true; | |
| 101 #else | |
| 102 return false; | |
| 103 #endif | |
| 104 } | |
| 105 | |
| 106 HostContentSettingsMap* ChromeSubresourceFilterClient::GetSettingsMap() const { | |
|
engedy
2017/04/04 13:35:59
nit: Can we make this a global function in an anon
Charlie Harrison
2017/04/10 14:58:53
Would prefer not to since we'd need to inject the
engedy
2017/04/12 14:02:50
Acknowledged.
| |
| 82 Profile* profile = | 107 Profile* profile = |
| 83 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 108 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 84 DCHECK(profile); | 109 DCHECK(profile); |
| 85 HostContentSettingsMap* settings_map = | 110 HostContentSettingsMap* settings_map = |
| 86 HostContentSettingsMapFactory::GetForProfile(profile); | 111 HostContentSettingsMapFactory::GetForProfile(profile); |
| 87 return settings_map->GetContentSetting( | 112 DCHECK(settings_map); |
| 88 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 113 return settings_map; |
| 89 std::string()); | |
| 90 } | 114 } |
| 115 | |
| 116 void ChromeSubresourceFilterClient::OnUIShown(const GURL& url) { | |
| 117 LogAction(kActionUIShown); | |
| 118 shown_for_navigation_ = true; | |
| 119 | |
| 120 if (!UsingSmartUI()) | |
| 121 return; | |
| 122 auto dict = base::MakeUnique<base::DictionaryValue>(); | |
| 123 double now = clock_->Now().ToDoubleT(); | |
| 124 dict->SetDouble(kUILastShowTime, now); | |
| 125 GetSettingsMap()->SetWebsiteSettingDefaultScope( | |
| 126 url, url, | |
|
engedy
2017/04/04 13:35:59
For REQUESTING_ORIGIN_ONLY_SCOPE website settings,
Charlie Harrison
2017/04/10 14:58:53
Done.
| |
| 127 ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, | |
| 128 std::string(), std::move(dict)); | |
| 129 } | |
| 130 | |
| 131 bool ChromeSubresourceFilterClient::ShouldShowUIForSite(const GURL& url) const { | |
| 132 if (!UsingSmartUI()) | |
| 133 return true; | |
| 134 | |
| 135 std::unique_ptr<base::DictionaryValue> dict = | |
| 136 base::DictionaryValue::From(GetSettingsMap()->GetWebsiteSetting( | |
| 137 url, url, CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER_DATA, | |
| 138 std::string(), nullptr)); | |
| 139 if (!dict) | |
| 140 return true; | |
| 141 | |
| 142 double last_shown_time_double = 0; | |
| 143 if (dict->GetDouble(kUILastShowTime, &last_shown_time_double)) { | |
| 144 base::Time last_shown = base::Time::FromDoubleT(last_shown_time_double); | |
| 145 if (clock_->Now() - last_shown < | |
| 146 base::TimeDelta::FromHours(kUIShowThresholdHours)) { | |
| 147 return false; | |
| 148 } | |
| 149 } | |
| 150 return true; | |
| 151 } | |
| OLD | NEW |