Chromium Code Reviews| Index: chrome/browser/subresource_filter/subresource_filter_content_settings_observer_factory.cc |
| diff --git a/chrome/browser/subresource_filter/subresource_filter_content_settings_observer_factory.cc b/chrome/browser/subresource_filter/subresource_filter_content_settings_observer_factory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..053fa7bceb3c73f3d320222196ac613dab2c72e3 |
| --- /dev/null |
| +++ b/chrome/browser/subresource_filter/subresource_filter_content_settings_observer_factory.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/subresource_filter/subresource_filter_content_settings_observer_factory.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/subresource_filter/chrome_subresource_filter_client.h" |
| +#include "components/content_settings/core/browser/content_settings_details.h" |
| +#include "components/content_settings/core/browser/content_settings_observer.h" |
| +#include "components/content_settings/core/browser/host_content_settings_map.h" |
| +#include "components/content_settings/core/common/content_settings.h" |
| +#include "components/content_settings/core/common/content_settings_pattern.h" |
| +#include "components/content_settings/core/common/content_settings_types.h" |
| +#include "components/keyed_service/content/browser_context_dependency_manager.h" |
| +#include "components/keyed_service/core/keyed_service.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +void LogActionForContentSettingChange( |
| + ContentSetting setting, |
| + SubresourceFilterAction action_for_block, |
| + SubresourceFilterAction action_for_allow) { |
| + switch (setting) { |
| + case CONTENT_SETTING_BLOCK: |
| + ChromeSubresourceFilterClient::LogSubresourceFilterAction( |
| + action_for_block); |
| + break; |
| + case CONTENT_SETTING_ALLOW: |
| + ChromeSubresourceFilterClient::LogSubresourceFilterAction( |
| + action_for_allow); |
| + break; |
| + case CONTENT_SETTING_DEFAULT: |
| + case CONTENT_SETTING_ASK: |
| + case CONTENT_SETTING_SESSION_ONLY: |
| + case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: |
| + case CONTENT_SETTING_NUM_SETTINGS: |
| + NOTREACHED(); |
| + break; |
| + } |
| +} |
| + |
| +// This class observers subresource filter content settings changes for metrics |
| +// collection. |
| +// |
| +// TODO(csharrison): Consider moving this class to its own file if the |
| +// subresource filter needs to keep other profile-scoped state. |
| +class SubresourceFilterContentSettingsObserver |
| + : public KeyedService, |
| + public content_settings::Observer { |
| + public: |
| + explicit SubresourceFilterContentSettingsObserver(Profile* profile) |
| + : settings_map_(HostContentSettingsMapFactory::GetForProfile(profile)) { |
| + DCHECK(profile); |
| + DCHECK(settings_map_); |
| + settings_map_->AddObserver(this); |
| + } |
| + |
| + ~SubresourceFilterContentSettingsObserver() override {} |
| + |
| + private: |
| + // KeyedService: |
| + void Shutdown() override { |
| + settings_map_->RemoveObserver(this); |
| + settings_map_ = nullptr; |
| + } |
| + |
| + // content_settings::Observer: |
| + void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, |
| + const ContentSettingsPattern& secondary_pattern, |
| + ContentSettingsType content_type, |
| + std::string resource_identifier) override { |
| + if (content_type != CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER) |
| + return; |
| + |
| + const ContentSettingsDetails details(primary_pattern, secondary_pattern, |
| + content_type, resource_identifier); |
| + if (details.update_all()) { |
|
msramek
2017/03/29 17:37:30
To your question, yes, DefaultProvider apparently
|
| + ContentSetting global_setting = settings_map_->GetDefaultContentSetting( |
| + CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, nullptr); |
| + LogActionForContentSettingChange(global_setting, |
| + kActionContentSettingsBlockedGlobal, |
| + kActionContentSettingsAllowedGlobal); |
| + return; |
| + } |
| + |
| + // Remove this DCHECK if extension APIs or admin policies are given the |
| + // ability to set secondary patterns for this setting. |
| + DCHECK(secondary_pattern == ContentSettingsPattern::Wildcard()); |
| + |
| + DCHECK(primary_pattern.IsValid()); |
| + |
| + // An invalid URL indicates that this is a wildcard pattern. |
| + GURL url = GURL(primary_pattern.ToString()); |
| + if (!url.is_valid()) { |
| + ChromeSubresourceFilterClient::LogSubresourceFilterAction( |
| + kActionContentSettingsWildcardUpdate); |
| + return; |
| + } |
| + |
| + ContentSetting setting = settings_map_->GetContentSetting( |
| + url, url, ContentSettingsType::CONTENT_SETTINGS_TYPE_SUBRESOURCE_FILTER, |
| + std::string()); |
| + LogActionForContentSettingChange(setting, kActionContentSettingsBlocked, |
| + kActionContentSettingsAllowed); |
| + } |
| + |
| + HostContentSettingsMap* settings_map_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SubresourceFilterContentSettingsObserver); |
| +}; |
| + |
| +} // namespace |
| + |
| +// static |
| +void SubresourceFilterContentSettingsObserverFactory::EnsureForProfile( |
| + Profile* profile) { |
| + GetInstance()->GetServiceForBrowserContext(profile, true /* create */); |
| +} |
| + |
| +// static |
| +SubresourceFilterContentSettingsObserverFactory* |
| +SubresourceFilterContentSettingsObserverFactory::GetInstance() { |
| + return base::Singleton< |
| + SubresourceFilterContentSettingsObserverFactory>::get(); |
| +} |
| + |
| +SubresourceFilterContentSettingsObserverFactory:: |
| + SubresourceFilterContentSettingsObserverFactory() |
| + : BrowserContextKeyedServiceFactory( |
| + "SubresourceFilterContentSettingsObserver", |
| + BrowserContextDependencyManager::GetInstance()) {} |
| + |
| +KeyedService* |
| +SubresourceFilterContentSettingsObserverFactory::BuildServiceInstanceFor( |
| + content::BrowserContext* profile) const { |
| + return new SubresourceFilterContentSettingsObserver( |
| + static_cast<Profile*>(profile)); |
| +} |