OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/subresource_filter/subresource_filter_content_settings_ observer_factory.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/logging.h" | |
10 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
11 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | |
13 #include "components/content_settings/core/browser/content_settings_details.h" | |
14 #include "components/content_settings/core/browser/content_settings_observer.h" | |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
16 #include "components/content_settings/core/common/content_settings.h" | |
17 #include "components/content_settings/core/common/content_settings_pattern.h" | |
18 #include "components/content_settings/core/common/content_settings_types.h" | |
19 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
20 #include "components/keyed_service/core/keyed_service.h" | |
21 #include "url/gurl.h" | |
22 | |
23 namespace { | |
24 | |
25 void LogActionForContentSettingChange( | |
26 ContentSetting setting, | |
27 SubresourceFilterAction action_for_block, | |
28 SubresourceFilterAction action_for_allow) { | |
29 switch (setting) { | |
30 case CONTENT_SETTING_BLOCK: | |
31 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
32 action_for_block); | |
33 break; | |
34 case CONTENT_SETTING_ALLOW: | |
35 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
36 action_for_allow); | |
37 break; | |
38 case CONTENT_SETTING_DEFAULT: | |
39 case CONTENT_SETTING_ASK: | |
40 case CONTENT_SETTING_SESSION_ONLY: | |
41 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: | |
42 case CONTENT_SETTING_NUM_SETTINGS: | |
43 NOTREACHED(); | |
44 break; | |
45 } | |
46 } | |
47 | |
48 // This class observers subresource filter content settings changes for metrics | |
49 // collection. | |
50 // | |
51 // TODO(csharrison): Consider moving this class to its own file if the | |
52 // subresource filter needs to keep other profile-scoped state. | |
53 class SubresourceFilterContentSettingsObserver | |
54 : public KeyedService, | |
55 public content_settings::Observer { | |
56 public: | |
57 explicit SubresourceFilterContentSettingsObserver(Profile* profile) | |
58 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { | |
59 DCHECK(profile); | |
60 DCHECK(settings_map_); | |
61 settings_map_->AddObserver(this); | |
62 } | |
63 | |
64 ~SubresourceFilterContentSettingsObserver() override {} | |
65 | |
66 private: | |
67 // KeyedService: | |
68 void Shutdown() override { | |
69 settings_map_->RemoveObserver(this); | |
70 settings_map_ = nullptr; | |
71 } | |
72 | |
73 // content_settings::Observer: | |
74 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, | |
75 const ContentSettingsPattern& secondary_pattern, | |
76 ContentSettingsType content_type, | |
77 std::string resource_identifier) override { | |
78 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) | |
79 return; | |
80 | |
81 const ContentSettingsDetails details(primary_pattern, secondary_pattern, | |
82 content_type, resource_identifier); | |
83 if (details.update_all()) { | |
msramek
2017/03/29 17:37:30
To your question, yes, DefaultProvider apparently
| |
84 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( | |
85 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); | |
86 LogActionForContentSettingChange(global_setting, | |
87 kActionContentSettingsBlockedGlobal, | |
88 kActionContentSettingsAllowedGlobal); | |
89 return; | |
90 } | |
91 | |
92 // Remove this DCHECK if extension APIs or admin policies are given the | |
93 // ability to set secondary patterns for this setting. | |
94 DCHECK(secondary_pattern == ContentSettingsPattern::Wildcard()); | |
95 | |
96 DCHECK(primary_pattern.IsValid()); | |
97 | |
98 // An invalid URL indicates that this is a wildcard pattern. | |
99 GURL url = GURL(primary_pattern.ToString()); | |
100 if (!url.is_valid()) { | |
101 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
102 kActionContentSettingsWildcardUpdate); | |
103 return; | |
104 } | |
105 | |
106 ContentSetting setting = settings_map_->GetContentSetting( | |
107 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
108 std::string()); | |
109 LogActionForContentSettingChange(setting, kActionContentSettingsBlocked, | |
110 kActionContentSettingsAllowed); | |
111 } | |
112 | |
113 HostContentSettingsMap* settings_map_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsObserver); | |
116 }; | |
117 | |
118 } // namespace | |
119 | |
120 // static | |
121 void SubresourceFilterContentSettingsObserverFactory::EnsureForProfile( | |
122 Profile* profile) { | |
123 GetInstance()->GetServiceForBrowserContext(profile, true /* create */); | |
124 } | |
125 | |
126 // static | |
127 SubresourceFilterContentSettingsObserverFactory* | |
128 SubresourceFilterContentSettingsObserverFactory::GetInstance() { | |
129 return base::Singleton< | |
130 SubresourceFilterContentSettingsObserverFactory>::get(); | |
131 } | |
132 | |
133 SubresourceFilterContentSettingsObserverFactory:: | |
134 SubresourceFilterContentSettingsObserverFactory() | |
135 : BrowserContextKeyedServiceFactory( | |
136 "SubresourceFilterContentSettingsObserver", | |
137 BrowserContextDependencyManager::GetInstance()) {} | |
138 | |
139 KeyedService* | |
140 SubresourceFilterContentSettingsObserverFactory::BuildServiceInstanceFor( | |
141 content::BrowserContext* profile) const { | |
142 return new SubresourceFilterContentSettingsObserver( | |
143 static_cast<Profile*>(profile)); | |
144 } | |
OLD | NEW |