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 "base/logging.h" | |
8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
9 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" | |
11 #include "components/content_settings/core/browser/content_settings_details.h" | |
12 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
13 #include "components/content_settings/core/common/content_settings_pattern.h" | |
14 #include "components/content_settings/core/common/content_settings_types.h" | |
15 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
16 #include "url/gurl.h" | |
17 | |
18 SubresourceFilterContentSettingsObserver:: | |
19 SubresourceFilterContentSettingsObserver(Profile* profile) | |
20 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { | |
21 DCHECK(profile); | |
22 settings_map_->AddObserver(this); | |
23 } | |
24 | |
25 void SubresourceFilterContentSettingsObserver::OnContentSettingChanged( | |
msramek
2017/03/29 09:41:25
This method probably deserves a unittest. You ment
Charlie Harrison
2017/03/29 15:23:32
Done.
| |
26 const ContentSettingsPattern& primary_pattern, | |
27 const ContentSettingsPattern& secondary_pattern, | |
msramek
2017/03/29 09:41:25
Do we care about the secondary pattern?
If a user
Charlie Harrison
2017/03/29 15:23:32
Done. We may add this functionality in the future
| |
28 ContentSettingsType content_type, | |
29 std::string resource_identifier) { | |
30 DCHECK(settings_map_); | |
31 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER || | |
32 !primary_pattern.IsValid()) { | |
33 return; | |
34 } | |
35 | |
36 const ContentSettingsDetails details(primary_pattern, secondary_pattern, | |
37 content_type, resource_identifier); | |
38 if (details.update_all()) { | |
39 ContentSetting global_setting = | |
40 GetContentSettingForUrl(GURL("https://example.com")); | |
msramek
2017/03/29 09:41:25
I'm so going to mess with your metrics by flipping
Charlie Harrison
2017/03/29 15:23:32
Ha! Done. I knew there was a better way than this.
| |
41 if (global_setting == CONTENT_SETTING_BLOCK) { | |
msramek
2017/03/29 09:41:25
style nit: Use a switch instead of NOTREACHED() to
Charlie Harrison
2017/03/29 15:23:32
There are 5 content settings that I'm not handling
msramek
2017/03/29 17:37:30
Sigh... For a moment, I completely forgot that we'
Charlie Harrison
2017/03/29 17:47:34
No worries! Reverted back to the if chain.
| |
42 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
43 kActionContentSettingsBlockedGlobal); | |
44 } else if (global_setting == CONTENT_SETTING_ALLOW) { | |
45 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
46 kActionContentSettingsAllowedGlobal); | |
47 } else { | |
48 NOTREACHED(); | |
49 } | |
50 return; | |
51 } | |
52 | |
53 GURL url = GURL(primary_pattern.ToString()); | |
54 if (!url.is_valid()) { | |
55 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
56 kActionContentSettingsWildcardUpdate); | |
57 return; | |
58 } | |
59 | |
60 ContentSetting setting = GetContentSettingForUrl(url); | |
61 if (setting == CONTENT_SETTING_BLOCK) { | |
msramek
2017/03/29 09:41:25
Ditto.
Charlie Harrison
2017/03/29 15:23:32
Done.
| |
62 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
63 kActionContentSettingsBlocked); | |
64 } else if (setting == CONTENT_SETTING_ALLOW) { | |
65 ChromeSubresourceFilterClient::LogSubresourceFilterAction( | |
66 kActionContentSettingsAllowed); | |
67 } else { | |
68 NOTREACHED(); | |
69 } | |
70 } | |
71 | |
72 void SubresourceFilterContentSettingsObserver::Shutdown() { | |
73 settings_map_->RemoveObserver(this); | |
74 settings_map_ = nullptr; | |
75 } | |
76 | |
77 ContentSetting | |
78 SubresourceFilterContentSettingsObserver::GetContentSettingForUrl( | |
79 const GURL& url) { | |
80 return settings_map_->GetContentSetting( | |
81 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, | |
82 std::string()); | |
83 } | |
84 | |
85 // static | |
86 SubresourceFilterContentSettingsObserver* | |
87 SubresourceFilterContentSettingsObserverFactory::GetForProfile( | |
88 Profile* profile) { | |
89 return static_cast<SubresourceFilterContentSettingsObserver*>( | |
90 GetInstance()->GetServiceForBrowserContext(profile, true /* create */)); | |
91 } | |
92 | |
93 // static | |
94 SubresourceFilterContentSettingsObserverFactory* | |
95 SubresourceFilterContentSettingsObserverFactory::GetInstance() { | |
96 return base::Singleton< | |
97 SubresourceFilterContentSettingsObserverFactory>::get(); | |
98 } | |
99 | |
100 SubresourceFilterContentSettingsObserverFactory:: | |
101 SubresourceFilterContentSettingsObserverFactory() | |
102 : BrowserContextKeyedServiceFactory( | |
103 "SubresourceFilterContentSettingsObserver", | |
104 BrowserContextDependencyManager::GetInstance()) {} | |
105 | |
106 KeyedService* | |
107 SubresourceFilterContentSettingsObserverFactory::BuildServiceInstanceFor( | |
108 content::BrowserContext* profile) const { | |
109 return new SubresourceFilterContentSettingsObserver( | |
110 static_cast<Profile*>(profile)); | |
111 } | |
OLD | NEW |