OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data_cookie_helper.h" |
6 | 6 |
7 #include "utility" | 7 #include "utility" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 #include "net/base/registry_controlled_domain.h" | 16 #include "net/base/registry_controlled_domain.h" |
| 17 #include "net/cookies/canonical_cookie.h" |
17 #include "net/cookies/parsed_cookie.h" | 18 #include "net/cookies/parsed_cookie.h" |
18 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 | 23 |
23 BrowsingDataCookieHelper::BrowsingDataCookieHelper( | 24 BrowsingDataCookieHelper::BrowsingDataCookieHelper( |
24 net::URLRequestContextGetter* request_context_getter) | 25 net::URLRequestContextGetter* request_context_getter) |
25 : is_fetching_(false), | 26 : is_fetching_(false), |
26 request_context_getter_(request_context_getter) { | 27 request_context_getter_(request_context_getter) { |
(...skipping 10 matching lines...) Expand all Loading... |
37 DCHECK(!callback.is_null()); | 38 DCHECK(!callback.is_null()); |
38 DCHECK(completion_callback_.is_null()); | 39 DCHECK(completion_callback_.is_null()); |
39 is_fetching_ = true; | 40 is_fetching_ = true; |
40 completion_callback_ = callback; | 41 completion_callback_ = callback; |
41 BrowserThread::PostTask( | 42 BrowserThread::PostTask( |
42 BrowserThread::IO, FROM_HERE, | 43 BrowserThread::IO, FROM_HERE, |
43 base::Bind(&BrowsingDataCookieHelper::FetchCookiesOnIOThread, this)); | 44 base::Bind(&BrowsingDataCookieHelper::FetchCookiesOnIOThread, this)); |
44 } | 45 } |
45 | 46 |
46 void BrowsingDataCookieHelper::DeleteCookie( | 47 void BrowsingDataCookieHelper::DeleteCookie( |
47 const net::CookieMonster::CanonicalCookie& cookie) { | 48 const net::CanonicalCookie& cookie) { |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
49 BrowserThread::PostTask( | 50 BrowserThread::PostTask( |
50 BrowserThread::IO, FROM_HERE, | 51 BrowserThread::IO, FROM_HERE, |
51 base::Bind(&BrowsingDataCookieHelper::DeleteCookieOnIOThread, | 52 base::Bind(&BrowsingDataCookieHelper::DeleteCookieOnIOThread, |
52 this, cookie)); | 53 this, cookie)); |
53 } | 54 } |
54 | 55 |
55 void BrowsingDataCookieHelper::FetchCookiesOnIOThread() { | 56 void BrowsingDataCookieHelper::FetchCookiesOnIOThread() { |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
57 scoped_refptr<net::CookieMonster> cookie_monster = | 58 scoped_refptr<net::CookieMonster> cookie_monster = |
(...skipping 17 matching lines...) Expand all Loading... |
75 void BrowsingDataCookieHelper::NotifyInUIThread( | 76 void BrowsingDataCookieHelper::NotifyInUIThread( |
76 const net::CookieList& cookies) { | 77 const net::CookieList& cookies) { |
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
78 DCHECK(is_fetching_); | 79 DCHECK(is_fetching_); |
79 is_fetching_ = false; | 80 is_fetching_ = false; |
80 completion_callback_.Run(cookies); | 81 completion_callback_.Run(cookies); |
81 completion_callback_.Reset(); | 82 completion_callback_.Reset(); |
82 } | 83 } |
83 | 84 |
84 void BrowsingDataCookieHelper::DeleteCookieOnIOThread( | 85 void BrowsingDataCookieHelper::DeleteCookieOnIOThread( |
85 const net::CookieMonster::CanonicalCookie& cookie) { | 86 const net::CanonicalCookie& cookie) { |
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
87 scoped_refptr<net::CookieMonster> cookie_monster = | 88 scoped_refptr<net::CookieMonster> cookie_monster = |
88 request_context_getter_->GetURLRequestContext()-> | 89 request_context_getter_->GetURLRequestContext()-> |
89 cookie_store()->GetCookieMonster(); | 90 cookie_store()->GetCookieMonster(); |
90 if (cookie_monster) { | 91 if (cookie_monster) { |
91 cookie_monster->DeleteCanonicalCookieAsync( | 92 cookie_monster->DeleteCanonicalCookieAsync( |
92 cookie, net::CookieMonster::DeleteCookieCallback()); | 93 cookie, net::CookieMonster::DeleteCookieCallback()); |
93 } | 94 } |
94 } | 95 } |
95 | 96 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 const net::CookieOptions& options) { | 135 const net::CookieOptions& options) { |
135 net::ParsedCookie parsed_cookie(cookie_line); | 136 net::ParsedCookie parsed_cookie(cookie_line); |
136 if (options.exclude_httponly() && parsed_cookie.IsHttpOnly()) { | 137 if (options.exclude_httponly() && parsed_cookie.IsHttpOnly()) { |
137 // Return if a Javascript cookie illegally specified the HTTP only flag. | 138 // Return if a Javascript cookie illegally specified the HTTP only flag. |
138 return; | 139 return; |
139 } | 140 } |
140 | 141 |
141 // This fails to create a canonical cookie, if the normalized cookie domain | 142 // This fails to create a canonical cookie, if the normalized cookie domain |
142 // form cookie line and the url don't have the same domain+registry, or url | 143 // form cookie line and the url don't have the same domain+registry, or url |
143 // host isn't cookie domain or one of its subdomains. | 144 // host isn't cookie domain or one of its subdomains. |
144 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie( | 145 scoped_ptr<net::CanonicalCookie> cookie( |
145 net::CookieMonster::CanonicalCookie::Create(url, parsed_cookie)); | 146 net::CanonicalCookie::Create(url, parsed_cookie)); |
146 if (cookie.get()) | 147 if (cookie.get()) |
147 AddCookie(frame_url, *cookie); | 148 AddCookie(frame_url, *cookie); |
148 } | 149 } |
149 | 150 |
150 void CannedBrowsingDataCookieHelper::Reset() { | 151 void CannedBrowsingDataCookieHelper::Reset() { |
151 STLDeleteContainerPairSecondPointers(origin_cookie_list_map_.begin(), | 152 STLDeleteContainerPairSecondPointers(origin_cookie_list_map_.begin(), |
152 origin_cookie_list_map_.end()); | 153 origin_cookie_list_map_.end()); |
153 origin_cookie_list_map_.clear(); | 154 origin_cookie_list_map_.clear(); |
154 } | 155 } |
155 | 156 |
(...skipping 28 matching lines...) Expand all Loading... |
184 it != origin_cookie_list_map_.end(); | 185 it != origin_cookie_list_map_.end(); |
185 ++it) { | 186 ++it) { |
186 cookie_list.insert(cookie_list.begin(), | 187 cookie_list.insert(cookie_list.begin(), |
187 it->second->begin(), | 188 it->second->begin(), |
188 it->second->end()); | 189 it->second->end()); |
189 } | 190 } |
190 callback.Run(cookie_list); | 191 callback.Run(cookie_list); |
191 } | 192 } |
192 | 193 |
193 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( | 194 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( |
194 const net::CookieMonster::CanonicalCookie& add_cookie, | 195 const net::CanonicalCookie& add_cookie, |
195 net::CookieList* cookie_list) { | 196 net::CookieList* cookie_list) { |
196 typedef net::CookieList::iterator cookie_iterator; | 197 typedef net::CookieList::iterator cookie_iterator; |
197 for (cookie_iterator cookie = cookie_list->begin(); | 198 for (cookie_iterator cookie = cookie_list->begin(); |
198 cookie != cookie_list->end(); ++cookie) { | 199 cookie != cookie_list->end(); ++cookie) { |
199 if (cookie->Name() == add_cookie.Name() && | 200 if (cookie->Name() == add_cookie.Name() && |
200 cookie->Domain() == add_cookie.Domain()&& | 201 cookie->Domain() == add_cookie.Domain()&& |
201 cookie->Path() == add_cookie.Path()) { | 202 cookie->Path() == add_cookie.Path()) { |
202 cookie_list->erase(cookie); | 203 cookie_list->erase(cookie); |
203 return true; | 204 return true; |
204 } | 205 } |
205 } | 206 } |
206 return false; | 207 return false; |
207 } | 208 } |
208 | 209 |
209 net::CookieList* CannedBrowsingDataCookieHelper::GetCookiesFor( | 210 net::CookieList* CannedBrowsingDataCookieHelper::GetCookiesFor( |
210 const GURL& first_party_origin) { | 211 const GURL& first_party_origin) { |
211 OriginCookieListMap::iterator it = | 212 OriginCookieListMap::iterator it = |
212 origin_cookie_list_map_.find(first_party_origin); | 213 origin_cookie_list_map_.find(first_party_origin); |
213 if (it == origin_cookie_list_map_.end()) { | 214 if (it == origin_cookie_list_map_.end()) { |
214 net::CookieList* cookies = new net::CookieList(); | 215 net::CookieList* cookies = new net::CookieList(); |
215 origin_cookie_list_map_.insert( | 216 origin_cookie_list_map_.insert( |
216 std::pair<GURL, net::CookieList*>(first_party_origin, cookies)); | 217 std::pair<GURL, net::CookieList*>(first_party_origin, cookies)); |
217 return cookies; | 218 return cookies; |
218 } | 219 } |
219 return it->second; | 220 return it->second; |
220 } | 221 } |
221 | 222 |
222 void CannedBrowsingDataCookieHelper::AddCookie( | 223 void CannedBrowsingDataCookieHelper::AddCookie( |
223 const GURL& frame_url, | 224 const GURL& frame_url, |
224 const net::CookieMonster::CanonicalCookie& cookie) { | 225 const net::CanonicalCookie& cookie) { |
225 net::CookieList* cookie_list = | 226 net::CookieList* cookie_list = |
226 GetCookiesFor(frame_url.GetOrigin()); | 227 GetCookiesFor(frame_url.GetOrigin()); |
227 DeleteMatchingCookie(cookie, cookie_list); | 228 DeleteMatchingCookie(cookie, cookie_list); |
228 cookie_list->push_back(cookie); | 229 cookie_list->push_back(cookie); |
229 } | 230 } |
OLD | NEW |