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 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
16 #include "net/cookies/canonical_cookie.h"
17 #include "net/cookies/cookie_util.h"
18 #include "net/cookies/parsed_cookie.h"
19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_getter.h"
23 using content::BrowserThread
;
26 const char kGlobalCookieSetURL
[] = "chrome://cookieset";
29 BrowsingDataCookieHelper::BrowsingDataCookieHelper(
30 net::URLRequestContextGetter
* request_context_getter
)
31 : request_context_getter_(request_context_getter
) {
32 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
35 BrowsingDataCookieHelper::~BrowsingDataCookieHelper() {
38 void BrowsingDataCookieHelper::StartFetching(
39 const base::Callback
<void(const net::CookieList
& cookies
)>& callback
) {
40 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
41 DCHECK(!is_fetching_
);
42 DCHECK(!callback
.is_null());
43 DCHECK(completion_callback_
.is_null());
45 completion_callback_
= callback
;
46 BrowserThread::PostTask(
47 BrowserThread::IO
, FROM_HERE
,
48 base::Bind(&BrowsingDataCookieHelper::FetchCookiesOnIOThread
, this));
51 void BrowsingDataCookieHelper::DeleteCookie(
52 const net::CanonicalCookie
& cookie
) {
53 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
54 BrowserThread::PostTask(
55 BrowserThread::IO
, FROM_HERE
,
56 base::Bind(&BrowsingDataCookieHelper::DeleteCookieOnIOThread
,
60 void BrowsingDataCookieHelper::FetchCookiesOnIOThread() {
61 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
62 scoped_refptr
<net::CookieMonster
> cookie_monster
=
63 request_context_getter_
->GetURLRequestContext()->
64 cookie_store()->GetCookieMonster();
65 if (cookie_monster
.get()) {
66 cookie_monster
->GetAllCookiesAsync(
67 base::Bind(&BrowsingDataCookieHelper::OnFetchComplete
, this));
69 OnFetchComplete(net::CookieList());
73 void BrowsingDataCookieHelper::OnFetchComplete(const net::CookieList
& cookies
) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
75 BrowserThread::PostTask(
76 BrowserThread::UI
, FROM_HERE
,
77 base::Bind(&BrowsingDataCookieHelper::NotifyInUIThread
, this, cookies
));
80 void BrowsingDataCookieHelper::NotifyInUIThread(
81 const net::CookieList
& cookies
) {
82 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
85 completion_callback_
.Run(cookies
);
86 completion_callback_
.Reset();
89 void BrowsingDataCookieHelper::DeleteCookieOnIOThread(
90 const net::CanonicalCookie
& cookie
) {
91 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
92 scoped_refptr
<net::CookieMonster
> cookie_monster
=
93 request_context_getter_
->GetURLRequestContext()->
94 cookie_store()->GetCookieMonster();
95 if (cookie_monster
.get()) {
96 cookie_monster
->DeleteCanonicalCookieAsync(
97 cookie
, net::CookieMonster::DeleteCookieCallback());
101 CannedBrowsingDataCookieHelper::CannedBrowsingDataCookieHelper(
102 net::URLRequestContextGetter
* request_context_getter
)
103 : BrowsingDataCookieHelper(request_context_getter
) {
106 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() {
110 void CannedBrowsingDataCookieHelper::AddReadCookies(
111 const GURL
& frame_url
,
113 const net::CookieList
& cookie_list
) {
114 for (const auto& add_cookie
: cookie_list
)
115 AddCookie(frame_url
, add_cookie
);
118 void CannedBrowsingDataCookieHelper::AddChangedCookie(
119 const GURL
& frame_url
,
121 const std::string
& cookie_line
,
122 const net::CookieOptions
& options
) {
123 scoped_ptr
<net::CanonicalCookie
> cookie(net::CanonicalCookie::Create(
124 url
, cookie_line
, base::Time::Now(), options
));
126 AddCookie(frame_url
, *cookie
);
129 void CannedBrowsingDataCookieHelper::Reset() {
130 STLDeleteContainerPairSecondPointers(origin_cookie_set_map_
.begin(),
131 origin_cookie_set_map_
.end());
132 origin_cookie_set_map_
.clear();
135 bool CannedBrowsingDataCookieHelper::empty() const {
136 for (const auto& pair
: origin_cookie_set_map_
) {
137 if (!pair
.second
->empty())
144 size_t CannedBrowsingDataCookieHelper::GetCookieCount() const {
146 for (const auto& pair
: origin_cookie_set_map_
)
147 count
+= pair
.second
->size();
152 void CannedBrowsingDataCookieHelper::StartFetching(
153 const net::CookieMonster::GetCookieListCallback
& callback
) {
154 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
155 net::CookieList cookie_list
;
156 for (const auto& pair
: origin_cookie_set_map_
) {
157 cookie_list
.insert(cookie_list
.begin(), pair
.second
->begin(),
160 callback
.Run(cookie_list
);
163 void CannedBrowsingDataCookieHelper::DeleteCookie(
164 const net::CanonicalCookie
& cookie
) {
165 for (const auto& pair
: origin_cookie_set_map_
)
166 DeleteMatchingCookie(cookie
, pair
.second
);
167 BrowsingDataCookieHelper::DeleteCookie(cookie
);
170 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie(
171 const net::CanonicalCookie
& add_cookie
,
172 canonical_cookie::CookieHashSet
* cookie_set
) {
173 return cookie_set
->erase(add_cookie
) > 0;
176 canonical_cookie::CookieHashSet
* CannedBrowsingDataCookieHelper::GetCookiesFor(
177 const GURL
& first_party_origin
) {
178 OriginCookieSetMap::iterator it
=
179 origin_cookie_set_map_
.find(first_party_origin
);
180 if (it
== origin_cookie_set_map_
.end()) {
181 canonical_cookie::CookieHashSet
* cookies
=
182 new canonical_cookie::CookieHashSet
;
183 origin_cookie_set_map_
.insert(
184 std::pair
<GURL
, canonical_cookie::CookieHashSet
*>(first_party_origin
,
191 void CannedBrowsingDataCookieHelper::AddCookie(
192 const GURL
& frame_url
,
193 const net::CanonicalCookie
& cookie
) {
194 // Storing cookies in separate cookie sets per frame origin makes the
195 // GetCookieCount method count a cookie multiple times if it is stored in
197 // E.g. let "example.com" be redirected to "www.example.com". A cookie set
198 // with the cookie string "A=B; Domain=.example.com" would be sent to both
199 // hosts. This means it would be stored in the separate cookie sets for both
200 // hosts ("example.com", "www.example.com"). The method GetCookieCount would
201 // count this cookie twice. To prevent this, we us a single global cookie
202 // set as a work-around to store all added cookies. Per frame URL cookie
203 // sets are currently not used. In the future they will be used for
204 // collecting cookies per origin in redirect chains.
205 // TODO(markusheintz): A) Change the GetCookiesCount method to prevent
206 // counting cookies multiple times if they are stored in multiple cookie
207 // sets. B) Replace the GetCookieFor method call below with:
208 // "GetCookiesFor(frame_url.GetOrigin());"
209 CR_DEFINE_STATIC_LOCAL(const GURL
, origin_cookie_url
, (kGlobalCookieSetURL
));
210 canonical_cookie::CookieHashSet
* cookie_set
=
211 GetCookiesFor(origin_cookie_url
);
212 DeleteMatchingCookie(cookie
, cookie_set
);
213 cookie_set
->insert(cookie
);