[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_ |
| 6 | #define CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/compiler_specific.h" |
[email protected] | abc7724 | 2012-03-16 04:32:31 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 13 | #include "base/memory/singleton.h" |
| 14 | #include "base/synchronization/lock.h" |
| 15 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 16 | #include "chrome/browser/prefs/pref_change_registrar.h" |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 17 | #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" |
| 18 | #include "chrome/browser/profiles/refcounted_profile_keyed_service_factory.h" |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 19 | #include "chrome/common/content_settings.h" |
| 20 | #include "content/public/browser/notification_observer.h" |
| 21 | |
| 22 | class ContentSettingsPattern; |
| 23 | class CookieSettingsWrapper; |
| 24 | class GURL; |
| 25 | class PrefService; |
| 26 | class Profile; |
| 27 | |
| 28 | // A frontend to the cookie settings of |HostContentSettingsMap|. Handles |
| 29 | // cookie-specific logic such as blocking third-party cookies. Written on the UI |
| 30 | // thread and read on any thread. One instance per profile. |
| 31 | |
| 32 | class CookieSettings |
| 33 | : public content::NotificationObserver, |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 34 | public RefcountedProfileKeyedService { |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 35 | public: |
| 36 | CookieSettings( |
| 37 | HostContentSettingsMap* host_content_settings_map, |
| 38 | PrefService* prefs); |
| 39 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 40 | // Returns the default content setting (CONTENT_SETTING_ALLOW, |
| 41 | // CONTENT_SETTING_BLOCK, or CONTENT_SETTING_SESSION_ONLY) for cookies. If |
| 42 | // |provider_id| is not NULL, the id of the provider which provided the |
| 43 | // default setting is assigned to it. |
| 44 | // |
| 45 | // This may be called on any thread. |
| 46 | ContentSetting GetDefaultCookieSetting(std::string* provider_id) const; |
| 47 | |
| 48 | // Returns true if the page identified by (|url|, |first_party_url|) is |
| 49 | // allowed to read cookies. |
| 50 | // |
| 51 | // This may be called on any thread. |
| 52 | bool IsReadingCookieAllowed(const GURL& url, |
| 53 | const GURL& first_party_url) const; |
| 54 | |
| 55 | // Returns true if the page identified by (|url|, |first_party_url|) is |
| 56 | // allowed to set cookies (permanent or session only). |
| 57 | // |
| 58 | // This may be called on any thread. |
| 59 | bool IsSettingCookieAllowed(const GURL& url, |
| 60 | const GURL& first_party_url) const; |
| 61 | |
| 62 | // Returns true if the cookie set by a page identified by |url| should be |
| 63 | // session only. Querying this only makes sense if |IsSettingCookieAllowed| |
| 64 | // has returned true. |
| 65 | // |
| 66 | // This may be called on any thread. |
| 67 | bool IsCookieSessionOnly(const GURL& url) const; |
| 68 | |
| 69 | // Returns all patterns with a non-default cookie setting, mapped to their |
| 70 | // actual settings, in the precedence order of the setting rules. |settings| |
| 71 | // must be a non-NULL outparam. |
| 72 | // |
| 73 | // This may be called on any thread. |
| 74 | void GetCookieSettings(ContentSettingsForOneType* settings) const; |
| 75 | |
| 76 | // Sets the default content setting (CONTENT_SETTING_ALLOW, |
| 77 | // CONTENT_SETTING_BLOCK, or CONTENT_SETTING_SESSION_ONLY) for cookies. |
| 78 | // |
| 79 | // This should only be called on the UI thread. |
| 80 | void SetDefaultCookieSetting(ContentSetting setting); |
| 81 | |
| 82 | // Sets the cookie setting for the given patterns. |
| 83 | // |
| 84 | // This should only be called on the UI thread. |
| 85 | void SetCookieSetting(const ContentSettingsPattern& primary_pattern, |
| 86 | const ContentSettingsPattern& secondary_pattern, |
| 87 | ContentSetting setting); |
| 88 | |
| 89 | // Resets the cookie setting for the given patterns. |
| 90 | // |
| 91 | // This should only be called on the UI thread. |
| 92 | void ResetCookieSetting(const ContentSettingsPattern& primary_pattern, |
| 93 | const ContentSettingsPattern& secondary_pattern); |
| 94 | |
| 95 | // |NotificationObserver| implementation. |
| 96 | virtual void Observe(int type, |
| 97 | const content::NotificationSource& source, |
| 98 | const content::NotificationDetails& details) OVERRIDE; |
| 99 | |
| 100 | // Detaches the |CookieSettings| from all |Profile|-related objects like |
| 101 | // |PrefService|. This methods needs to be called before destroying the |
| 102 | // |Profile|. Afterwards, only const methods can be called. |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 103 | virtual void ShutdownOnUIThread() OVERRIDE; |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 104 | |
| 105 | // A helper for applying third party cookie blocking rules. |
[email protected] | 1cf786a | 2011-11-08 19:08:32 | [diff] [blame] | 106 | ContentSetting GetCookieSetting( |
| 107 | const GURL& url, |
| 108 | const GURL& first_party_url, |
| 109 | bool setting_cookie, |
| 110 | content_settings::SettingSource* source) const; |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 111 | |
| 112 | static void RegisterUserPrefs(PrefService* prefs); |
| 113 | |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 114 | class Factory : public RefcountedProfileKeyedServiceFactory { |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 115 | public: |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 116 | // Returns the |CookieSettings| associated with the |profile|. |
| 117 | // |
| 118 | // This should only be called on the UI thread. |
[email protected] | abc7724 | 2012-03-16 04:32:31 | [diff] [blame] | 119 | static scoped_refptr<CookieSettings> GetForProfile(Profile* profile); |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 120 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 121 | static Factory* GetInstance(); |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | friend struct DefaultSingletonTraits<Factory>; |
| 125 | |
| 126 | Factory(); |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 127 | virtual ~Factory(); |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 128 | |
[email protected] | 0894c4e | 2012-02-13 23:39:02 | [diff] [blame] | 129 | // |ProfileKeyedBaseFactory| methods: |
| 130 | virtual void RegisterUserPrefs(PrefService* user_prefs) OVERRIDE; |
[email protected] | 67e7999 | 2012-02-13 20:47:58 | [diff] [blame] | 131 | virtual bool ServiceRedirectedInIncognito() OVERRIDE; |
[email protected] | abc7724 | 2012-03-16 04:32:31 | [diff] [blame] | 132 | virtual scoped_refptr<RefcountedProfileKeyedService> |
| 133 | BuildServiceInstanceFor(Profile* profile) const OVERRIDE; |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | private: |
[email protected] | 649d1c0 | 2012-04-27 02:56:21 | [diff] [blame] | 137 | virtual ~CookieSettings(); |
| 138 | |
[email protected] | 30fde82 | 2011-10-28 09:49:05 | [diff] [blame] | 139 | // Returns true if the "block third party cookies" preference is set. |
| 140 | // |
| 141 | // This method may be called on any thread. |
| 142 | bool ShouldBlockThirdPartyCookies() const; |
| 143 | |
| 144 | scoped_refptr<HostContentSettingsMap> host_content_settings_map_; |
| 145 | PrefChangeRegistrar pref_change_registrar_; |
| 146 | |
| 147 | // Used around accesses to |block_third_party_cookies_| to guarantee thread |
| 148 | // safety. |
| 149 | mutable base::Lock lock_; |
| 150 | |
| 151 | bool block_third_party_cookies_; |
| 152 | }; |
| 153 | |
| 154 | #endif // CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_ |