1 // Copyright (c) 2011 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 // Maps hostnames to custom content settings. Written on the UI thread and read 6 // on any thread. One instance per profile. 7 8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ 9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ 10 #pragma once 11 12 #include <map> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 17 #include "base/basictypes.h" 18 #include "base/memory/linked_ptr.h" 19 #include "base/memory/ref_counted.h" 20 #include "base/synchronization/lock.h" 21 #include "chrome/browser/content_settings/content_settings_pattern.h" 22 #include "chrome/browser/prefs/pref_change_registrar.h" 23 #include "chrome/common/content_settings.h" 24 #include "content/browser/browser_thread.h" 25 #include "content/common/notification_observer.h" 26 #include "content/common/notification_registrar.h" 27 28 namespace content_settings { 29 class DefaultProviderInterface; 30 class ProviderInterface; 31 } // namespace content_settings 32 33 class ContentSettingsDetails; 34 class DictionaryValue; 35 class GURL; 36 class PrefService; 37 class Profile; 38 39 class HostContentSettingsMap 40 : public NotificationObserver, 41 public base::RefCountedThreadSafe<HostContentSettingsMap, 42 BrowserThread::DeleteOnUIThread> { 43 public: 44 typedef std::pair<ContentSettingsPattern, ContentSetting> PatternSettingPair; 45 typedef std::vector<PatternSettingPair> SettingsForOneType; 46 47 explicit HostContentSettingsMap(Profile* profile); 48 49 static void RegisterUserPrefs(PrefService* prefs); 50 51 // Returns the default setting for a particular content type. 52 // 53 // This may be called on any thread. 54 ContentSetting GetDefaultContentSetting( 55 ContentSettingsType content_type) const; 56 57 // Returns a single ContentSetting which applies to a given URL. Note that 58 // certain internal schemes are whitelisted. For ContentSettingsTypes that 59 // require an resource identifier to be specified, the |resource_identifier| 60 // must be non-empty. 61 // 62 // This may be called on any thread. 63 ContentSetting GetContentSetting( 64 const GURL& url, 65 ContentSettingsType content_type, 66 const std::string& resource_identifier) const; 67 68 // Returns a single ContentSetting which applies to a given URL or 69 // CONTENT_SETTING_DEFAULT, if no exception applies. Note that certain 70 // internal schemes are whitelisted. For ContentSettingsTypes that require an 71 // resource identifier to be specified, the |resource_identifier| must be 72 // non-empty. 73 // 74 // This may be called on any thread. 75 ContentSetting GetNonDefaultContentSetting( 76 const GURL& url, 77 ContentSettingsType content_type, 78 const std::string& resource_identifier) const; 79 80 // Returns all ContentSettings which apply to a given URL. For content 81 // setting types that require an additional resource identifier, the default 82 // content setting is returned. 83 // 84 // This may be called on any thread. 85 ContentSettings GetContentSettings(const GURL& url) const; 86 87 // Returns all non-default ContentSettings which apply to a given URL. For 88 // content setting types that require an additional resource identifier, 89 // CONTENT_SETTING_DEFAULT is returned. 90 // 91 // This may be called on any thread. 92 ContentSettings GetNonDefaultContentSettings(const GURL& url) const; 93 94 // For a given content type, returns all patterns with a non-default setting, 95 // mapped to their actual settings, in lexicographical order. |settings| 96 // must be a non-NULL outparam. If this map was created for the 97 // incognito profile, it will only return those settings differing from 98 // the main map. For ContentSettingsTypes that require an resource identifier 99 // to be specified, the |resource_identifier| must be non-empty. 100 // 101 // This may be called on any thread. 102 void GetSettingsForOneType(ContentSettingsType content_type, 103 const std::string& resource_identifier, 104 SettingsForOneType* settings) const; 105 106 // Sets the default setting for a particular content type. This method must 107 // not be invoked on an incognito map. 108 // 109 // This should only be called on the UI thread. 110 void SetDefaultContentSetting(ContentSettingsType content_type, 111 ContentSetting setting); 112 113 // Sets the blocking setting for a particular pattern and content type. 114 // Setting the value to CONTENT_SETTING_DEFAULT causes the default setting 115 // for that type to be used when loading pages matching this pattern. For 116 // ContentSettingsTypes that require an resource identifier to be specified, 117 // the |resource_identifier| must be non-empty. 118 // 119 // This should only be called on the UI thread. 120 void SetContentSetting(const ContentSettingsPattern& pattern, 121 ContentSettingsType content_type, 122 const std::string& resource_identifier, 123 ContentSetting setting); 124 125 // Convenience method to add a content setting for a given URL, making sure 126 // that there is no setting overriding it. For ContentSettingsTypes that 127 // require an resource identifier to be specified, the |resource_identifier| 128 // must be non-empty. 129 // 130 // This should only be called on the UI thread. 131 void AddExceptionForURL(const GURL& url, 132 ContentSettingsType content_type, 133 const std::string& resource_identifier, 134 ContentSetting setting); 135 136 // Clears all host-specific settings for one content type. 137 // 138 // This should only be called on the UI thread. 139 void ClearSettingsForOneType(ContentSettingsType content_type); 140 141 // This setting trumps any host-specific settings. 142 bool BlockThirdPartyCookies() const { return block_third_party_cookies_; } 143 bool IsBlockThirdPartyCookiesManaged() const { 144 return is_block_third_party_cookies_managed_; 145 } 146 147 // Sets whether we block all third-party cookies. This method must not be 148 // invoked on an incognito map. 149 // 150 // This should only be called on the UI thread. 151 void SetBlockThirdPartyCookies(bool block); 152 153 bool GetBlockNonsandboxedPlugins() const { 154 return block_nonsandboxed_plugins_; 155 } 156 157 void SetBlockNonsandboxedPlugins(bool block); 158 159 // Resets all settings levels. 160 // 161 // This should only be called on the UI thread. 162 void ResetToDefaults(); 163 164 // Returns true if the default setting for the |content_type| is managed. 165 bool IsDefaultContentSettingManaged(ContentSettingsType content_type) const; 166 167 // NotificationObserver implementation. 168 virtual void Observe(NotificationType type, 169 const NotificationSource& source, 170 const NotificationDetails& details); 171 172 private: 173 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; 174 friend class DeleteTask<HostContentSettingsMap>; 175 176 ~HostContentSettingsMap(); 177 178 // Informs observers that content settings have changed. Make sure that 179 // |lock_| is not held when calling this, as listeners will usually call one 180 // of the GetSettings functions in response, which would then lead to a 181 // mutex deadlock. 182 void NotifyObservers(const ContentSettingsDetails& details); 183 184 void UnregisterObservers(); 185 186 // Various migration methods (old cookie, popup and per-host data gets 187 // migrated to the new format). 188 void MigrateObsoleteCookiePref(PrefService* prefs); 189 190 // The profile we're associated with. 191 Profile* profile_; 192 193 NotificationRegistrar notification_registrar_; 194 PrefChangeRegistrar pref_change_registrar_; 195 196 // Whether this settings map is for an OTR session. 197 bool is_off_the_record_; 198 199 // Whether we are currently updating preferences, this is used to ignore 200 // notifications from the preferences service that we triggered ourself. 201 bool updating_preferences_; 202 203 // Default content setting providers. 204 std::vector<linked_ptr<content_settings::DefaultProviderInterface> > 205 default_content_settings_providers_; 206 207 // Content setting providers. 208 std::vector<linked_ptr<content_settings::ProviderInterface> > 209 content_settings_providers_; 210 211 // Used around accesses to the following objects to guarantee thread safety. 212 mutable base::Lock lock_; 213 214 // Misc global settings. 215 bool block_third_party_cookies_; 216 bool is_block_third_party_cookies_managed_; 217 bool block_nonsandboxed_plugins_; 218 219 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); 220 }; 221 222 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ 223