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 // This class observers subresource filter content settings changes for metrics |
| 26 // collection. |
| 27 // |
| 28 // TODO(csharrison): Consider moving this class to its own file if the |
| 29 // subresource filter needs to keep other profile-scoped state. |
| 30 class SubresourceFilterContentSettingsObserver |
| 31 : public KeyedService, |
| 32 public content_settings::Observer { |
| 33 public: |
| 34 explicit SubresourceFilterContentSettingsObserver(Profile* profile) |
| 35 : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { |
| 36 DCHECK(profile); |
| 37 DCHECK(settings_map_); |
| 38 settings_map_->AddObserver(this); |
| 39 } |
| 40 |
| 41 ~SubresourceFilterContentSettingsObserver() override {} |
| 42 |
| 43 private: |
| 44 // KeyedService: |
| 45 void Shutdown() override { |
| 46 settings_map_->RemoveObserver(this); |
| 47 settings_map_ = nullptr; |
| 48 } |
| 49 |
| 50 // content_settings::Observer: |
| 51 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, |
| 52 const ContentSettingsPattern& secondary_pattern, |
| 53 ContentSettingsType content_type, |
| 54 std::string resource_identifier) override { |
| 55 if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) |
| 56 return; |
| 57 |
| 58 const ContentSettingsDetails details(primary_pattern, secondary_pattern, |
| 59 content_type, resource_identifier); |
| 60 if (details.update_all()) { |
| 61 ContentSetting global_setting = settings_map_->GetDefaultContentSetting( |
| 62 CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); |
| 63 if (global_setting == CONTENT_SETTING_BLOCK) { |
| 64 ChromeSubresourceFilterClient::LogAction( |
| 65 kActionContentSettingsBlockedGlobal); |
| 66 } else if (global_setting == CONTENT_SETTING_ALLOW) { |
| 67 ChromeSubresourceFilterClient::LogAction( |
| 68 kActionContentSettingsAllowedGlobal); |
| 69 } else { |
| 70 NOTREACHED(); |
| 71 } |
| 72 return; |
| 73 } |
| 74 |
| 75 // Remove this DCHECK if extension APIs or admin policies are given the |
| 76 // ability to set secondary patterns for this setting. |
| 77 DCHECK(secondary_pattern == ContentSettingsPattern::Wildcard()); |
| 78 |
| 79 DCHECK(primary_pattern.IsValid()); |
| 80 |
| 81 // An invalid URL indicates that this is a wildcard pattern. |
| 82 GURL url = GURL(primary_pattern.ToString()); |
| 83 if (!url.is_valid()) { |
| 84 ChromeSubresourceFilterClient::LogAction( |
| 85 kActionContentSettingsWildcardUpdate); |
| 86 return; |
| 87 } |
| 88 |
| 89 ContentSetting setting = settings_map_->GetContentSetting( |
| 90 url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| 91 std::string()); |
| 92 if (setting == CONTENT_SETTING_BLOCK) { |
| 93 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsBlocked); |
| 94 } else if (setting == CONTENT_SETTING_ALLOW) { |
| 95 ChromeSubresourceFilterClient::LogAction(kActionContentSettingsAllowed); |
| 96 } else { |
| 97 NOTREACHED(); |
| 98 } |
| 99 } |
| 100 |
| 101 HostContentSettingsMap* settings_map_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsObserver); |
| 104 }; |
| 105 |
| 106 } // namespace |
| 107 |
| 108 // static |
| 109 void SubresourceFilterContentSettingsObserverFactory::EnsureForProfile( |
| 110 Profile* profile) { |
| 111 GetInstance()->GetServiceForBrowserContext(profile, true /* create */); |
| 112 } |
| 113 |
| 114 // static |
| 115 SubresourceFilterContentSettingsObserverFactory* |
| 116 SubresourceFilterContentSettingsObserverFactory::GetInstance() { |
| 117 return base::Singleton< |
| 118 SubresourceFilterContentSettingsObserverFactory>::get(); |
| 119 } |
| 120 |
| 121 SubresourceFilterContentSettingsObserverFactory:: |
| 122 SubresourceFilterContentSettingsObserverFactory() |
| 123 : BrowserContextKeyedServiceFactory( |
| 124 "SubresourceFilterContentSettingsObserver", |
| 125 BrowserContextDependencyManager::GetInstance()) {} |
| 126 |
| 127 KeyedService* |
| 128 SubresourceFilterContentSettingsObserverFactory::BuildServiceInstanceFor( |
| 129 content::BrowserContext* profile) const { |
| 130 return new SubresourceFilterContentSettingsObserver( |
| 131 static_cast<Profile*>(profile)); |
| 132 } |
OLD | NEW |