blob: 3e3feb336e83d984987c1822c1bbf94187d96ce1 [file] [log] [blame]
[email protected]67e79992012-02-13 20:47:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]30fde822011-10-28 09:49:052// 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]abc77242012-03-16 04:32:3112#include "base/memory/ref_counted.h"
[email protected]30fde822011-10-28 09:49:0513#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]67e79992012-02-13 20:47:5817#include "chrome/browser/profiles/refcounted_profile_keyed_service.h"
18#include "chrome/browser/profiles/refcounted_profile_keyed_service_factory.h"
[email protected]30fde822011-10-28 09:49:0519#include "chrome/common/content_settings.h"
20#include "content/public/browser/notification_observer.h"
21
22class ContentSettingsPattern;
23class CookieSettingsWrapper;
24class GURL;
25class PrefService;
26class 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
32class CookieSettings
33 : public content::NotificationObserver,
[email protected]67e79992012-02-13 20:47:5834 public RefcountedProfileKeyedService {
[email protected]30fde822011-10-28 09:49:0535 public:
36 CookieSettings(
37 HostContentSettingsMap* host_content_settings_map,
38 PrefService* prefs);
39
[email protected]30fde822011-10-28 09:49:0540 // 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]67e79992012-02-13 20:47:58103 virtual void ShutdownOnUIThread() OVERRIDE;
[email protected]30fde822011-10-28 09:49:05104
105 // A helper for applying third party cookie blocking rules.
[email protected]1cf786a2011-11-08 19:08:32106 ContentSetting GetCookieSetting(
107 const GURL& url,
108 const GURL& first_party_url,
109 bool setting_cookie,
110 content_settings::SettingSource* source) const;
[email protected]30fde822011-10-28 09:49:05111
112 static void RegisterUserPrefs(PrefService* prefs);
113
[email protected]67e79992012-02-13 20:47:58114 class Factory : public RefcountedProfileKeyedServiceFactory {
[email protected]30fde822011-10-28 09:49:05115 public:
[email protected]67e79992012-02-13 20:47:58116 // Returns the |CookieSettings| associated with the |profile|.
117 //
118 // This should only be called on the UI thread.
[email protected]abc77242012-03-16 04:32:31119 static scoped_refptr<CookieSettings> GetForProfile(Profile* profile);
[email protected]67e79992012-02-13 20:47:58120
[email protected]30fde822011-10-28 09:49:05121 static Factory* GetInstance();
[email protected]30fde822011-10-28 09:49:05122
123 private:
124 friend struct DefaultSingletonTraits<Factory>;
125
126 Factory();
[email protected]67e79992012-02-13 20:47:58127 virtual ~Factory();
[email protected]30fde822011-10-28 09:49:05128
[email protected]0894c4e2012-02-13 23:39:02129 // |ProfileKeyedBaseFactory| methods:
130 virtual void RegisterUserPrefs(PrefService* user_prefs) OVERRIDE;
[email protected]67e79992012-02-13 20:47:58131 virtual bool ServiceRedirectedInIncognito() OVERRIDE;
[email protected]abc77242012-03-16 04:32:31132 virtual scoped_refptr<RefcountedProfileKeyedService>
133 BuildServiceInstanceFor(Profile* profile) const OVERRIDE;
[email protected]30fde822011-10-28 09:49:05134 };
135
136 private:
[email protected]649d1c02012-04-27 02:56:21137 virtual ~CookieSettings();
138
[email protected]30fde822011-10-28 09:49:05139 // 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_