OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/browsing_data_cookie_helper.h" | |
6 | |
7 #include "utility" | |
8 | |
9 #include "base/bind.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 "googleurl/src/gurl.h" | |
16 #include "net/base/registry_controlled_domain.h" | |
17 #include "net/cookies/canonical_cookie.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" | |
21 | |
22 using content::BrowserThread; | |
23 | |
24 BrowsingDataCookieHelper::BrowsingDataCookieHelper( | |
25 net::URLRequestContextGetter* request_context_getter) | |
26 : is_fetching_(false), | |
27 request_context_getter_(request_context_getter) { | |
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
29 } | |
30 | |
31 BrowsingDataCookieHelper::~BrowsingDataCookieHelper() { | |
32 } | |
33 | |
34 void BrowsingDataCookieHelper::StartFetching( | |
35 const base::Callback<void(const net::CookieList& cookies)>& callback) { | |
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
37 DCHECK(!is_fetching_); | |
38 DCHECK(!callback.is_null()); | |
39 DCHECK(completion_callback_.is_null()); | |
40 is_fetching_ = true; | |
41 completion_callback_ = callback; | |
42 BrowserThread::PostTask( | |
43 BrowserThread::IO, FROM_HERE, | |
44 base::Bind(&BrowsingDataCookieHelper::FetchCookiesOnIOThread, this)); | |
45 } | |
46 | |
47 void BrowsingDataCookieHelper::DeleteCookie( | |
48 const net::CanonicalCookie& cookie) { | |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
50 BrowserThread::PostTask( | |
51 BrowserThread::IO, FROM_HERE, | |
52 base::Bind(&BrowsingDataCookieHelper::DeleteCookieOnIOThread, | |
53 this, cookie)); | |
54 } | |
55 | |
56 void BrowsingDataCookieHelper::FetchCookiesOnIOThread() { | |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
58 scoped_refptr<net::CookieMonster> cookie_monster = | |
59 request_context_getter_->GetURLRequestContext()-> | |
60 cookie_store()->GetCookieMonster(); | |
61 if (cookie_monster) { | |
62 cookie_monster->GetAllCookiesAsync( | |
63 base::Bind(&BrowsingDataCookieHelper::OnFetchComplete, this)); | |
64 } else { | |
65 OnFetchComplete(net::CookieList()); | |
66 } | |
67 } | |
68 | |
69 void BrowsingDataCookieHelper::OnFetchComplete(const net::CookieList& cookies) { | |
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
71 BrowserThread::PostTask( | |
72 BrowserThread::UI, FROM_HERE, | |
73 base::Bind(&BrowsingDataCookieHelper::NotifyInUIThread, this, cookies)); | |
74 } | |
75 | |
76 void BrowsingDataCookieHelper::NotifyInUIThread( | |
77 const net::CookieList& cookies) { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
79 DCHECK(is_fetching_); | |
80 is_fetching_ = false; | |
81 completion_callback_.Run(cookies); | |
82 completion_callback_.Reset(); | |
83 } | |
84 | |
85 void BrowsingDataCookieHelper::DeleteCookieOnIOThread( | |
86 const net::CanonicalCookie& cookie) { | |
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
88 scoped_refptr<net::CookieMonster> cookie_monster = | |
89 request_context_getter_->GetURLRequestContext()-> | |
90 cookie_store()->GetCookieMonster(); | |
91 if (cookie_monster) { | |
92 cookie_monster->DeleteCanonicalCookieAsync( | |
93 cookie, net::CookieMonster::DeleteCookieCallback()); | |
94 } | |
95 } | |
96 | |
97 CannedBrowsingDataCookieHelper::CannedBrowsingDataCookieHelper( | |
98 net::URLRequestContextGetter* request_context_getter) | |
99 : BrowsingDataCookieHelper(request_context_getter) { | |
100 } | |
101 | |
102 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() { | |
103 Reset(); | |
104 } | |
105 | |
106 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { | |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
108 CannedBrowsingDataCookieHelper* clone = | |
109 new CannedBrowsingDataCookieHelper(request_context_getter()); | |
110 | |
111 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); | |
112 it != origin_cookie_list_map_.end(); | |
113 ++it) { | |
114 net::CookieList* cookies = clone->GetCookiesFor(it->first); | |
115 cookies->insert(cookies->begin(), it->second->begin(), it->second->end()); | |
116 } | |
117 return clone; | |
118 } | |
119 | |
120 void CannedBrowsingDataCookieHelper::AddReadCookies( | |
121 const GURL& frame_url, | |
122 const GURL& url, | |
123 const net::CookieList& cookie_list) { | |
124 typedef net::CookieList::const_iterator cookie_iterator; | |
125 for (cookie_iterator add_cookie = cookie_list.begin(); | |
126 add_cookie != cookie_list.end(); ++add_cookie) { | |
127 AddCookie(frame_url, *add_cookie); | |
128 } | |
129 } | |
130 | |
131 void CannedBrowsingDataCookieHelper::AddChangedCookie( | |
132 const GURL& frame_url, | |
133 const GURL& url, | |
134 const std::string& cookie_line, | |
135 const net::CookieOptions& options) { | |
136 net::ParsedCookie parsed_cookie(cookie_line); | |
137 if (options.exclude_httponly() && parsed_cookie.IsHttpOnly()) { | |
138 // Return if a Javascript cookie illegally specified the HTTP only flag. | |
139 return; | |
140 } | |
141 | |
142 // This fails to create a canonical cookie, if the normalized cookie domain | |
143 // form cookie line and the url don't have the same domain+registry, or url | |
144 // host isn't cookie domain or one of its subdomains. | |
145 scoped_ptr<net::CanonicalCookie> cookie( | |
146 net::CanonicalCookie::Create(url, parsed_cookie)); | |
147 if (cookie.get()) | |
148 AddCookie(frame_url, *cookie); | |
149 } | |
150 | |
151 void CannedBrowsingDataCookieHelper::Reset() { | |
152 STLDeleteContainerPairSecondPointers(origin_cookie_list_map_.begin(), | |
153 origin_cookie_list_map_.end()); | |
154 origin_cookie_list_map_.clear(); | |
155 } | |
156 | |
157 bool CannedBrowsingDataCookieHelper::empty() const { | |
158 for (OriginCookieListMap::const_iterator it = | |
159 origin_cookie_list_map_.begin(); | |
160 it != origin_cookie_list_map_.end(); | |
161 ++it) { | |
162 if (!it->second->empty()) | |
163 return false; | |
164 } | |
165 return true; | |
166 } | |
167 | |
168 | |
169 size_t CannedBrowsingDataCookieHelper::GetCookieCount() const { | |
170 size_t count = 0; | |
171 for (OriginCookieListMap::const_iterator it = origin_cookie_list_map_.begin(); | |
172 it != origin_cookie_list_map_.end(); | |
173 ++it) { | |
174 count += it->second->size(); | |
175 } | |
176 return count; | |
177 } | |
178 | |
179 | |
180 void CannedBrowsingDataCookieHelper::StartFetching( | |
181 const net::CookieMonster::GetCookieListCallback& callback) { | |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
183 net::CookieList cookie_list; | |
184 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); | |
185 it != origin_cookie_list_map_.end(); | |
186 ++it) { | |
187 cookie_list.insert(cookie_list.begin(), | |
188 it->second->begin(), | |
189 it->second->end()); | |
190 } | |
191 callback.Run(cookie_list); | |
192 } | |
193 | |
194 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( | |
195 const net::CanonicalCookie& add_cookie, | |
196 net::CookieList* cookie_list) { | |
197 typedef net::CookieList::iterator cookie_iterator; | |
198 for (cookie_iterator cookie = cookie_list->begin(); | |
199 cookie != cookie_list->end(); ++cookie) { | |
200 if (cookie->Name() == add_cookie.Name() && | |
201 cookie->Domain() == add_cookie.Domain()&& | |
202 cookie->Path() == add_cookie.Path()) { | |
203 cookie_list->erase(cookie); | |
204 return true; | |
205 } | |
206 } | |
207 return false; | |
208 } | |
209 | |
210 net::CookieList* CannedBrowsingDataCookieHelper::GetCookiesFor( | |
211 const GURL& first_party_origin) { | |
212 OriginCookieListMap::iterator it = | |
213 origin_cookie_list_map_.find(first_party_origin); | |
214 if (it == origin_cookie_list_map_.end()) { | |
215 net::CookieList* cookies = new net::CookieList(); | |
216 origin_cookie_list_map_.insert( | |
217 std::pair<GURL, net::CookieList*>(first_party_origin, cookies)); | |
218 return cookies; | |
219 } | |
220 return it->second; | |
221 } | |
222 | |
223 void CannedBrowsingDataCookieHelper::AddCookie( | |
224 const GURL& frame_url, | |
225 const net::CanonicalCookie& cookie) { | |
226 net::CookieList* cookie_list = | |
227 GetCookiesFor(frame_url.GetOrigin()); | |
228 DeleteMatchingCookie(cookie, cookie_list); | |
229 cookie_list->push_back(cookie); | |
230 } | |
OLD | NEW |