1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_
6 #define CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/singleton.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/synchronization/lock.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/keyed_service/content/refcounted_browser_context_keyed_service_factory.h"
18 #include "components/keyed_service/core/refcounted_keyed_service.h"
20 class ContentSettingsPattern
;
21 class CookieSettingsWrapper
;
26 // A frontend to the cookie settings of |HostContentSettingsMap|. Handles
27 // cookie-specific logic such as blocking third-party cookies. Written on the UI
28 // thread and read on any thread. One instance per profile.
29 class CookieSettings
: public RefcountedKeyedService
{
32 HostContentSettingsMap
* host_content_settings_map
,
35 // Returns the default content setting (CONTENT_SETTING_ALLOW,
36 // CONTENT_SETTING_BLOCK, or CONTENT_SETTING_SESSION_ONLY) for cookies. If
37 // |provider_id| is not NULL, the id of the provider which provided the
38 // default setting is assigned to it.
40 // This may be called on any thread.
41 ContentSetting
GetDefaultCookieSetting(std::string
* provider_id
) const;
43 // Returns true if the page identified by (|url|, |first_party_url|) is
44 // allowed to read cookies.
46 // This may be called on any thread.
47 bool IsReadingCookieAllowed(const GURL
& url
,
48 const GURL
& first_party_url
) const;
50 // Returns true if the page identified by (|url|, |first_party_url|) is
51 // allowed to set cookies (permanent or session only).
53 // This may be called on any thread.
54 bool IsSettingCookieAllowed(const GURL
& url
,
55 const GURL
& first_party_url
) const;
57 // Returns true if the cookie set by a page identified by |url| should be
58 // session only. Querying this only makes sense if |IsSettingCookieAllowed|
61 // This may be called on any thread.
62 bool IsCookieSessionOnly(const GURL
& url
) const;
64 // Returns all patterns with a non-default cookie setting, mapped to their
65 // actual settings, in the precedence order of the setting rules. |settings|
66 // must be a non-NULL outparam.
68 // This may be called on any thread.
69 void GetCookieSettings(ContentSettingsForOneType
* settings
) const;
71 // Sets the default content setting (CONTENT_SETTING_ALLOW,
72 // CONTENT_SETTING_BLOCK, or CONTENT_SETTING_SESSION_ONLY) for cookies.
74 // This should only be called on the UI thread.
75 void SetDefaultCookieSetting(ContentSetting setting
);
77 // Sets the cookie setting for the given patterns.
79 // This should only be called on the UI thread.
80 void SetCookieSetting(const ContentSettingsPattern
& primary_pattern
,
81 const ContentSettingsPattern
& secondary_pattern
,
82 ContentSetting setting
);
84 // Resets the cookie setting for the given patterns.
86 // This should only be called on the UI thread.
87 void ResetCookieSetting(const ContentSettingsPattern
& primary_pattern
,
88 const ContentSettingsPattern
& secondary_pattern
);
90 // Detaches the |CookieSettings| from all |Profile|-related objects like
91 // |PrefService|. This methods needs to be called before destroying the
92 // |Profile|. Afterwards, only const methods can be called.
93 void ShutdownOnUIThread() override
;
95 // A helper for applying third party cookie blocking rules.
96 ContentSetting
GetCookieSetting(
98 const GURL
& first_party_url
,
100 content_settings::SettingSource
* source
) const;
102 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
104 class Factory
: public RefcountedBrowserContextKeyedServiceFactory
{
106 // Returns the |CookieSettings| associated with the |profile|.
108 // This should only be called on the UI thread.
109 static scoped_refptr
<CookieSettings
> GetForProfile(Profile
* profile
);
111 static Factory
* GetInstance();
114 friend struct DefaultSingletonTraits
<Factory
>;
119 // |BrowserContextKeyedBaseFactory| methods:
120 void RegisterProfilePrefs(
121 user_prefs::PrefRegistrySyncable
* registry
) override
;
122 content::BrowserContext
* GetBrowserContextToUse(
123 content::BrowserContext
* context
) const override
;
124 scoped_refptr
<RefcountedKeyedService
> BuildServiceInstanceFor(
125 content::BrowserContext
* context
) const override
;
129 ~CookieSettings() override
;
131 void OnBlockThirdPartyCookiesChanged();
133 // Returns true if the "block third party cookies" preference is set.
135 // This method may be called on any thread.
136 bool ShouldBlockThirdPartyCookies() const;
138 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
139 PrefChangeRegistrar pref_change_registrar_
;
141 // Used around accesses to |block_third_party_cookies_| to guarantee thread
143 mutable base::Lock lock_
;
145 bool block_third_party_cookies_
;
148 #endif // CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_