Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: chrome/browser/subresource_filter/chrome_subresource_filter_client.cc

Issue 2838063002: [on-hold][subresource_filter] Add ActivationTracker to track all activated contents (Closed)
Patch Set: fix incognito Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/browser_process.h" 10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
12 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 12 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
13 #include "chrome/browser/infobars/infobar_service.h" 13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/subresource_filter/subresource_filter_activation_tracke r.h"
16 #include "chrome/browser/subresource_filter/subresource_filter_profile_context.h "
15 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f actory.h" 17 #include "chrome/browser/subresource_filter/subresource_filter_profile_context_f actory.h"
16 #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"
17 #include "components/content_settings/core/browser/host_content_settings_map.h" 19 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/content_settings/core/common/content_settings_types.h" 20 #include "components/content_settings/core/common/content_settings_types.h"
19 #include "components/subresource_filter/content/browser/content_ruleset_service. h" 21 #include "components/subresource_filter/content/browser/content_ruleset_service. h"
22 #include "components/subresource_filter/core/common/activation_level.h"
23 #include "components/subresource_filter/core/common/activation_state.h"
20 #include "content/public/browser/navigation_handle.h" 24 #include "content/public/browser/navigation_handle.h"
21 25
22 ChromeSubresourceFilterClient::ChromeSubresourceFilterClient( 26 ChromeSubresourceFilterClient::ChromeSubresourceFilterClient(
23 content::WebContents* web_contents) 27 content::WebContents* web_contents)
24 : web_contents_(web_contents), did_show_ui_for_navigation_(false) { 28 : content::WebContentsObserver(web_contents),
25 DCHECK(web_contents); 29 profile_context_(SubresourceFilterProfileContextFactory::GetForProfile(
26 SubresourceFilterProfileContextFactory::EnsureForProfile( 30 Profile::FromBrowserContext(web_contents->GetBrowserContext()))),
27 Profile::FromBrowserContext(web_contents_->GetBrowserContext())); 31 did_show_ui_for_navigation_(false) {
32 DCHECK(profile_context_);
28 } 33 }
29 34
30 ChromeSubresourceFilterClient::~ChromeSubresourceFilterClient() {} 35 ChromeSubresourceFilterClient::~ChromeSubresourceFilterClient() {}
31 36
32 void ChromeSubresourceFilterClient::ToggleNotificationVisibility( 37 void ChromeSubresourceFilterClient::ToggleNotificationVisibility(
33 bool visibility) { 38 bool visibility) {
34 if (did_show_ui_for_navigation_ && visibility) 39 if (did_show_ui_for_navigation_ && visibility)
35 return; 40 return;
36 41
37 did_show_ui_for_navigation_ = visibility; 42 did_show_ui_for_navigation_ = visibility;
38 TabSpecificContentSettings* content_settings = 43 TabSpecificContentSettings* content_settings =
39 TabSpecificContentSettings::FromWebContents(web_contents_); 44 TabSpecificContentSettings::FromWebContents(web_contents());
40 45
41 // |visibility| is false when a new navigation starts. 46 // |visibility| is false when a new navigation starts.
42 if (visibility) { 47 if (visibility) {
43 content_settings->OnContentBlocked( 48 content_settings->OnContentBlocked(
44 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER); 49 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER);
45 LogAction(kActionUIShown); 50 LogAction(kActionUIShown);
46 #if defined(OS_ANDROID) 51 #if defined(OS_ANDROID)
47 InfoBarService* infobar_service = 52 InfoBarService* infobar_service =
48 InfoBarService::FromWebContents(web_contents_); 53 InfoBarService::FromWebContents(web_contents());
49 SubresourceFilterInfobarDelegate::Create(infobar_service); 54 SubresourceFilterInfobarDelegate::Create(infobar_service);
50 #endif 55 #endif
51 } else { 56 } else {
52 LogAction(kActionNavigationStarted); 57 LogAction(kActionNavigationStarted);
53 } 58 }
54 } 59 }
55 60
56 bool ChromeSubresourceFilterClient::ShouldSuppressActivation( 61 bool ChromeSubresourceFilterClient::ShouldSuppressActivation(
57 content::NavigationHandle* navigation_handle) { 62 content::NavigationHandle* navigation_handle) {
58 const GURL& url(navigation_handle->GetURL()); 63 const GURL& url(navigation_handle->GetURL());
59 return navigation_handle->IsInMainFrame() && 64 return navigation_handle->IsInMainFrame() &&
60 (whitelisted_hosts_.find(url.host()) != whitelisted_hosts_.end() || 65 (whitelisted_hosts_.find(url.host()) != whitelisted_hosts_.end() ||
61 GetContentSettingForUrl(url) == CONTENT_SETTING_BLOCK); 66 GetContentSettingForUrl(url) == CONTENT_SETTING_BLOCK);
62 } 67 }
63 68
64 void ChromeSubresourceFilterClient::WhitelistByContentSettings( 69 void ChromeSubresourceFilterClient::WhitelistByContentSettings(
65 const GURL& url) { 70 const GURL& url) {
66 // Whitelist via content settings. 71 // Whitelist via content settings.
67 Profile* profile = 72 Profile* profile =
68 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 73 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
69 DCHECK(profile); 74 DCHECK(profile);
70 HostContentSettingsMap* settings_map = 75 HostContentSettingsMap* settings_map =
71 HostContentSettingsMapFactory::GetForProfile(profile); 76 HostContentSettingsMapFactory::GetForProfile(profile);
72 settings_map->SetContentSettingDefaultScope( 77 settings_map->SetContentSettingDefaultScope(
73 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, 78 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
74 std::string(), CONTENT_SETTING_BLOCK); 79 std::string(), CONTENT_SETTING_BLOCK);
75 80
76 LogAction(kActionContentSettingsBlockedFromUI); 81 LogAction(kActionContentSettingsBlockedFromUI);
77 } 82 }
78 83
79 void ChromeSubresourceFilterClient::WhitelistInCurrentWebContents( 84 void ChromeSubresourceFilterClient::WhitelistInCurrentWebContents(
80 const GURL& url) { 85 const GURL& url) {
81 if (url.SchemeIsHTTPOrHTTPS()) 86 if (url.SchemeIsHTTPOrHTTPS())
82 whitelisted_hosts_.insert(url.host()); 87 whitelisted_hosts_.insert(url.host());
83 } 88 }
84 89
90 void ChromeSubresourceFilterClient::OnPageActivated(
91 const subresource_filter::ActivationState& state) {
92 DCHECK_NE(state.activation_level,
93 subresource_filter::ActivationLevel::DISABLED);
94
95 // Do not care about DRYRUN mode.
96 bool activated =
97 state.activation_level == subresource_filter::ActivationLevel::ENABLED;
98 profile_context_->activation_tracker()->OnWebContentsActivated(web_contents(),
99 activated);
100 }
101
85 // static 102 // static
86 void ChromeSubresourceFilterClient::LogAction(SubresourceFilterAction action) { 103 void ChromeSubresourceFilterClient::LogAction(SubresourceFilterAction action) {
87 UMA_HISTOGRAM_ENUMERATION("SubresourceFilter.Actions", action, 104 UMA_HISTOGRAM_ENUMERATION("SubresourceFilter.Actions", action,
88 kActionLastEntry); 105 kActionLastEntry);
89 } 106 }
90 107
91 ContentSetting ChromeSubresourceFilterClient::GetContentSettingForUrl( 108 ContentSetting ChromeSubresourceFilterClient::GetContentSettingForUrl(
92 const GURL& url) { 109 const GURL& url) {
93 Profile* profile = 110 Profile* profile =
94 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 111 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
95 DCHECK(profile); 112 DCHECK(profile);
96 HostContentSettingsMap* settings_map = 113 HostContentSettingsMap* settings_map =
97 HostContentSettingsMapFactory::GetForProfile(profile); 114 HostContentSettingsMapFactory::GetForProfile(profile);
98 return settings_map->GetContentSetting( 115 return settings_map->GetContentSetting(
99 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, 116 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER,
100 std::string()); 117 std::string());
101 } 118 }
102 119
103 subresource_filter::VerifiedRulesetDealer::Handle* 120 subresource_filter::VerifiedRulesetDealer::Handle*
104 ChromeSubresourceFilterClient::GetRulesetDealer() { 121 ChromeSubresourceFilterClient::GetRulesetDealer() {
105 subresource_filter::ContentRulesetService* ruleset_service = 122 subresource_filter::ContentRulesetService* ruleset_service =
106 g_browser_process->subresource_filter_ruleset_service(); 123 g_browser_process->subresource_filter_ruleset_service();
107 return ruleset_service ? ruleset_service->ruleset_dealer() : nullptr; 124 return ruleset_service ? ruleset_service->ruleset_dealer() : nullptr;
108 } 125 }
126
127 void ChromeSubresourceFilterClient::DidStartNavigation(
128 content::NavigationHandle* navigation_handle) {
129 if (navigation_handle->IsInMainFrame()) {
130 profile_context_->activation_tracker()->OnWebContentsActivated(
131 web_contents(), false /* activated */);
132 }
133 }
134
135 void ChromeSubresourceFilterClient::WebContentsDestroyed() {
136 profile_context_->activation_tracker()->OnWebContentsDestroyed(
137 web_contents());
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698