| 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/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 11 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 11 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 12 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_
observer_factory.h" |
| 14 #include "chrome/browser/ui/android/content_settings/subresource_filter_infobar_
delegate.h" | 15 #include "chrome/browser/ui/android/content_settings/subresource_filter_infobar_
delegate.h" |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "components/content_settings/core/common/content_settings.h" | |
| 17 | 17 |
| 18 ChromeSubresourceFilterClient::ChromeSubresourceFilterClient( | 18 ChromeSubresourceFilterClient::ChromeSubresourceFilterClient( |
| 19 content::WebContents* web_contents) | 19 content::WebContents* web_contents) |
| 20 : web_contents_(web_contents), shown_for_navigation_(false) { | 20 : web_contents_(web_contents), shown_for_navigation_(false) { |
| 21 DCHECK(web_contents); | 21 DCHECK(web_contents); |
| 22 // Ensure the content settings observer is initialized. |
| 23 SubresourceFilterContentSettingsObserverFactory::EnsureForProfile( |
| 24 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); |
| 22 } | 25 } |
| 23 | 26 |
| 24 ChromeSubresourceFilterClient::~ChromeSubresourceFilterClient() {} | 27 ChromeSubresourceFilterClient::~ChromeSubresourceFilterClient() {} |
| 25 | 28 |
| 26 void ChromeSubresourceFilterClient::ToggleNotificationVisibility( | 29 void ChromeSubresourceFilterClient::ToggleNotificationVisibility( |
| 27 bool visibility) { | 30 bool visibility) { |
| 28 if (shown_for_navigation_ && visibility) | 31 if (shown_for_navigation_ && visibility) |
| 29 return; | 32 return; |
| 33 |
| 34 // |visibility| is false when a new navigation starts. |
| 35 if (visibility) |
| 36 LogAction(kActionUIShown); |
| 37 else |
| 38 LogAction(kActionNavigationStarted); |
| 39 |
| 30 shown_for_navigation_ = visibility; | 40 shown_for_navigation_ = visibility; |
| 31 UMA_HISTOGRAM_BOOLEAN("SubresourceFilter.Prompt.NumVisibility", visibility); | |
| 32 TabSpecificContentSettings* content_settings = | 41 TabSpecificContentSettings* content_settings = |
| 33 TabSpecificContentSettings::FromWebContents(web_contents_); | 42 TabSpecificContentSettings::FromWebContents(web_contents_); |
| 34 content_settings->SetSubresourceBlocked(visibility); | 43 content_settings->SetSubresourceBlocked(visibility); |
| 35 #if defined(OS_ANDROID) | 44 #if defined(OS_ANDROID) |
| 36 if (visibility) { | 45 if (visibility) { |
| 37 InfoBarService* infobar_service = | 46 InfoBarService* infobar_service = |
| 38 InfoBarService::FromWebContents(web_contents_); | 47 InfoBarService::FromWebContents(web_contents_); |
| 39 | 48 |
| 40 SubresourceFilterInfobarDelegate::Create(infobar_service); | 49 SubresourceFilterInfobarDelegate::Create(infobar_service); |
| 41 content_settings->SetSubresourceBlockageIndicated(); | 50 content_settings->SetSubresourceBlockageIndicated(); |
| 42 } | 51 } |
| 43 #endif | 52 #endif |
| 44 } | 53 } |
| 45 | 54 |
| 46 bool ChromeSubresourceFilterClient::IsWhitelistedByContentSettings( | 55 bool ChromeSubresourceFilterClient::IsWhitelistedByContentSettings( |
| 47 const GURL& url) { | 56 const GURL& url) { |
| 48 Profile* profile = | 57 return GetContentSettingForUrl(url) == CONTENT_SETTING_BLOCK; |
| 49 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
| 50 DCHECK(profile); | |
| 51 HostContentSettingsMap* settings_map = | |
| 52 HostContentSettingsMapFactory::GetForProfile(profile); | |
| 53 ContentSetting setting = settings_map->GetContentSetting( | |
| 54 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
| 55 std::string()); | |
| 56 return setting == CONTENT_SETTING_BLOCK; | |
| 57 } | 58 } |
| 58 | 59 |
| 59 void ChromeSubresourceFilterClient::WhitelistByContentSettings( | 60 void ChromeSubresourceFilterClient::WhitelistByContentSettings( |
| 60 const GURL& url) { | 61 const GURL& url) { |
| 62 // Whitelist via content settings. |
| 61 Profile* profile = | 63 Profile* profile = |
| 62 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | 64 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 63 DCHECK(profile); | 65 DCHECK(profile); |
| 64 HostContentSettingsMap* settings_map = | 66 HostContentSettingsMap* settings_map = |
| 65 HostContentSettingsMapFactory::GetForProfile(profile); | 67 HostContentSettingsMapFactory::GetForProfile(profile); |
| 66 settings_map->SetContentSettingDefaultScope( | 68 settings_map->SetContentSettingDefaultScope( |
| 67 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | 69 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 68 std::string(), CONTENT_SETTING_BLOCK); | 70 std::string(), CONTENT_SETTING_BLOCK); |
| 71 |
| 72 LogAction(kActionContentSettingsBlockedFromUI); |
| 69 } | 73 } |
| 74 |
| 75 // static |
| 76 void ChromeSubresourceFilterClient::LogAction(SubresourceFilterAction action) { |
| 77 UMA_HISTOGRAM_ENUMERATION("SubresourceFilter.Actions", action, |
| 78 kActionLastEntry); |
| 79 } |
| 80 |
| 81 ContentSetting ChromeSubresourceFilterClient::GetContentSettingForUrl( |
| 82 const GURL& url) { |
| 83 Profile* profile = |
| 84 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 85 DCHECK(profile); |
| 86 HostContentSettingsMap* settings_map = |
| 87 HostContentSettingsMapFactory::GetForProfile(profile); |
| 88 return settings_map->GetContentSetting( |
| 89 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 90 std::string()); |
| 91 } |
| OLD | NEW |