1dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Use of this source code is governed by a BSD-style license that can be 3dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// found in the LICENSE file. 4dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 5dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#ifndef CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_BASE_PROVIDER_H_ 6dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#define CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_BASE_PROVIDER_H_ 7dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#pragma once 8dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 9dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include <map> 10dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include <string> 11dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include <utility> 12dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 13dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "base/synchronization/lock.h" 14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/content_settings/content_settings_provider.h" 15dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 16dc0f95d653279beabeb9817299e2902918ba123eKristian Monsennamespace content_settings { 17dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 18dc0f95d653279beabeb9817299e2902918ba123eKristian Monsentypedef std::pair<ContentSettingsType, std::string> 19dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsTypeResourceIdentifierPair; 20dc0f95d653279beabeb9817299e2902918ba123eKristian Monsentypedef std::map<ContentSettingsTypeResourceIdentifierPair, ContentSetting> 21dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ResourceContentSettings; 22dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 23dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenstruct ExtendedContentSettings { 24dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ExtendedContentSettings(); 25dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ExtendedContentSettings(const ExtendedContentSettings& rhs); 26dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ~ExtendedContentSettings(); 27dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 28dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettings content_settings; 29dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ResourceContentSettings content_settings_for_resources; 30dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen}; 31dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 32dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Map for ContentSettings. 33dc0f95d653279beabeb9817299e2902918ba123eKristian Monsentypedef std::map<std::string, ExtendedContentSettings> HostContentSettings; 34dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 35dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// BaseProvider is the abstract base class for all content-settings-provider 36dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// classes. 37dc0f95d653279beabeb9817299e2902918ba123eKristian Monsenclass BaseProvider : public ProviderInterface { 38dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen public: 39dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Maps CONTENT_SETTING_ASK for the CONTENT_SETTINGS_TYPE_PLUGINS to 40dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // CONTENT_SETTING_BLOCK if click-to-play is not enabled. 41dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen static ContentSetting ClickToPlayFixup(ContentSettingsType content_type, 42dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSetting setting); 43dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 44dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen explicit BaseProvider(bool is_otr); 45dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual ~BaseProvider(); 46dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 47dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 48dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Initializes the Provider. 49dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual void Init() = 0; 50dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 51dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // ProviderInterface Implementation 52dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual bool ContentSettingsTypeIsManaged( 53dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type) = 0; 54dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 55dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual ContentSetting GetContentSetting( 56dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const GURL& requesting_url, 57dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const GURL& embedding_url, 58dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type, 59dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ResourceIdentifier& resource_identifier) const; 60dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 61dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual void SetContentSetting( 62dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ContentSettingsPattern& requesting_pattern, 63dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ContentSettingsPattern& embedding_pattern, 64dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type, 65dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ResourceIdentifier& resource_identifier, 66dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSetting content_setting) = 0; 67dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 68dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual void GetAllContentSettingsRules( 69dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type, 70dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ResourceIdentifier& resource_identifier, 71dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen Rules* content_setting_rules) const; 72dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 73dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual void ClearAllContentSettingsRules( 74dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type) = 0; 75dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 76dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen virtual void ResetToDefaults() = 0; 77dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 78dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen protected: 79dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Returns true if the |content_type| requires a resource identifier. 80dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool RequiresResourceIdentifier( 81dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type) const; 82dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 83dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Returns true if the passed |settings| object contains only 84dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // CONTENT_SETTING_DEFAULT values. 85dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen bool AllDefault(const ExtendedContentSettings& settings) const; 86dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 87dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen void UpdateContentSettingsMap( 88dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ContentSettingsPattern& requesting_pattern, 89dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ContentSettingsPattern& embedding_pattern, 90dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettingsType content_type, 91dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen const ResourceIdentifier& resource_identifier, 92dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSetting content_setting); 93dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 94dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // TODO(markusheintz): LEGACY method. Will be removed in a future re-factoring 95dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // step. 96dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen ContentSettings GetNonDefaultContentSettings(const GURL& url) const; 97dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 98dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Accessors 99dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen HostContentSettings* host_content_settings() { 100dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen return &host_content_settings_; 101dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen } 102dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 103ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen HostContentSettings* incognito_settings() { 104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen return &incognito_settings_; 105dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen } 106dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 107dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen base::Lock& lock() const { 108dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen return lock_; 109dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen } 110dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 111ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool is_incognito() const { 112ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen return is_incognito_; 113dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen } 114dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 115dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen private: 116dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Copies of the pref data, so that we can read it on threads other than the 117dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // UI thread. 118dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen HostContentSettings host_content_settings_; 119dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 120dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Whether this settings map is for an OTR session. 121ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen bool is_incognito_; 122dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 123dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Differences to the preference-stored host content settings for 124ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen // incognito settings. 125ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen HostContentSettings incognito_settings_; 126dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 127dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // Used around accesses to the content_settings_ object to guarantee 128dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen // thread safety. 129dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen mutable base::Lock lock_; 130dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen}; 131dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 132dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen} // namespace content_settings 133dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen 134dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#endif // CHROME_BROWSER_CONTENT_SETTINGS_CONTENT_SETTINGS_BASE_PROVIDER_H_ 135