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/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data/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 "net/base/registry_controlled_domains/registry_controlled_domain.h" | 15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
16 #include "net/cookies/canonical_cookie.h" | 16 #include "net/cookies/canonical_cookie.h" |
17 #include "net/cookies/cookie_util.h" | 17 #include "net/cookies/cookie_util.h" |
18 #include "net/cookies/parsed_cookie.h" | 18 #include "net/cookies/parsed_cookie.h" |
19 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
20 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 using content::BrowserThread; | 23 using content::BrowserThread; |
24 | 24 |
25 namespace { | 25 namespace { |
26 const char kGlobalCookieListURL[] = "chrome://cookielist"; | 26 const char kGlobalCookieSetURL[] = "chrome://cookieset"; |
27 } | 27 } |
28 | 28 |
29 BrowsingDataCookieHelper::BrowsingDataCookieHelper( | 29 BrowsingDataCookieHelper::BrowsingDataCookieHelper( |
30 net::URLRequestContextGetter* request_context_getter) | 30 net::URLRequestContextGetter* request_context_getter) |
31 : is_fetching_(false), | 31 : is_fetching_(false), |
32 request_context_getter_(request_context_getter) { | 32 request_context_getter_(request_context_getter) { |
33 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 33 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
34 } | 34 } |
35 | 35 |
36 BrowsingDataCookieHelper::~BrowsingDataCookieHelper() { | 36 BrowsingDataCookieHelper::~BrowsingDataCookieHelper() { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 106 |
107 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() { | 107 CannedBrowsingDataCookieHelper::~CannedBrowsingDataCookieHelper() { |
108 Reset(); | 108 Reset(); |
109 } | 109 } |
110 | 110 |
111 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { | 111 CannedBrowsingDataCookieHelper* CannedBrowsingDataCookieHelper::Clone() { |
112 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
113 CannedBrowsingDataCookieHelper* clone = | 113 CannedBrowsingDataCookieHelper* clone = |
114 new CannedBrowsingDataCookieHelper(request_context_getter()); | 114 new CannedBrowsingDataCookieHelper(request_context_getter()); |
115 | 115 |
116 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); | 116 for (OriginCookieSetMap::iterator it = origin_cookie_set_map_.begin(); |
117 it != origin_cookie_list_map_.end(); | 117 it != origin_cookie_set_map_.end(); |
118 ++it) { | 118 ++it) { |
119 net::CookieList* cookies = clone->GetCookiesFor(it->first); | 119 canonical_cookie::CookieHashSet* cookies = clone->GetCookiesFor(it->first); |
120 cookies->insert(cookies->begin(), it->second->begin(), it->second->end()); | 120 cookies->insert(it->second->begin(), it->second->end()); |
121 } | 121 } |
122 return clone; | 122 return clone; |
123 } | 123 } |
124 | 124 |
125 void CannedBrowsingDataCookieHelper::AddReadCookies( | 125 void CannedBrowsingDataCookieHelper::AddReadCookies( |
126 const GURL& frame_url, | 126 const GURL& frame_url, |
127 const GURL& url, | 127 const GURL& url, |
128 const net::CookieList& cookie_list) { | 128 const net::CookieList& cookie_list) { |
129 typedef net::CookieList::const_iterator cookie_iterator; | 129 typedef net::CookieList::const_iterator cookie_iterator; |
130 for (cookie_iterator add_cookie = cookie_list.begin(); | 130 for (cookie_iterator add_cookie = cookie_list.begin(); |
131 add_cookie != cookie_list.end(); ++add_cookie) { | 131 add_cookie != cookie_list.end(); ++add_cookie) { |
132 AddCookie(frame_url, *add_cookie); | 132 AddCookie(frame_url, *add_cookie); |
133 } | 133 } |
134 } | 134 } |
135 | 135 |
136 void CannedBrowsingDataCookieHelper::AddChangedCookie( | 136 void CannedBrowsingDataCookieHelper::AddChangedCookie( |
137 const GURL& frame_url, | 137 const GURL& frame_url, |
138 const GURL& url, | 138 const GURL& url, |
139 const std::string& cookie_line, | 139 const std::string& cookie_line, |
140 const net::CookieOptions& options) { | 140 const net::CookieOptions& options) { |
141 scoped_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( | 141 scoped_ptr<net::CanonicalCookie> cookie(net::CanonicalCookie::Create( |
142 url, cookie_line, base::Time::Now(), options)); | 142 url, cookie_line, base::Time::Now(), options)); |
143 if (cookie.get()) | 143 if (cookie.get()) |
144 AddCookie(frame_url, *cookie); | 144 AddCookie(frame_url, *cookie); |
145 } | 145 } |
146 | 146 |
147 void CannedBrowsingDataCookieHelper::Reset() { | 147 void CannedBrowsingDataCookieHelper::Reset() { |
148 STLDeleteContainerPairSecondPointers(origin_cookie_list_map_.begin(), | 148 STLDeleteContainerPairSecondPointers(origin_cookie_set_map_.begin(), |
149 origin_cookie_list_map_.end()); | 149 origin_cookie_set_map_.end()); |
150 origin_cookie_list_map_.clear(); | 150 origin_cookie_set_map_.clear(); |
151 } | 151 } |
152 | 152 |
153 bool CannedBrowsingDataCookieHelper::empty() const { | 153 bool CannedBrowsingDataCookieHelper::empty() const { |
154 for (OriginCookieListMap::const_iterator it = | 154 for (OriginCookieSetMap::const_iterator it = origin_cookie_set_map_.begin(); |
155 origin_cookie_list_map_.begin(); | 155 it != origin_cookie_set_map_.end(); |
156 it != origin_cookie_list_map_.end(); | |
157 ++it) { | 156 ++it) { |
158 if (!it->second->empty()) | 157 if (!it->second->empty()) |
159 return false; | 158 return false; |
160 } | 159 } |
161 return true; | 160 return true; |
162 } | 161 } |
163 | 162 |
164 | 163 |
165 size_t CannedBrowsingDataCookieHelper::GetCookieCount() const { | 164 size_t CannedBrowsingDataCookieHelper::GetCookieCount() const { |
166 size_t count = 0; | 165 size_t count = 0; |
167 for (OriginCookieListMap::const_iterator it = origin_cookie_list_map_.begin(); | 166 for (OriginCookieSetMap::const_iterator it = origin_cookie_set_map_.begin(); |
168 it != origin_cookie_list_map_.end(); | 167 it != origin_cookie_set_map_.end(); |
169 ++it) { | 168 ++it) { |
170 count += it->second->size(); | 169 count += it->second->size(); |
171 } | 170 } |
172 return count; | 171 return count; |
173 } | 172 } |
174 | 173 |
175 | 174 |
176 void CannedBrowsingDataCookieHelper::StartFetching( | 175 void CannedBrowsingDataCookieHelper::StartFetching( |
177 const net::CookieMonster::GetCookieListCallback& callback) { | 176 const net::CookieMonster::GetCookieListCallback& callback) { |
178 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
179 net::CookieList cookie_list; | 178 net::CookieList cookie_list; |
180 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); | 179 for (OriginCookieSetMap::iterator it = origin_cookie_set_map_.begin(); |
181 it != origin_cookie_list_map_.end(); | 180 it != origin_cookie_set_map_.end(); |
182 ++it) { | 181 ++it) { |
183 cookie_list.insert(cookie_list.begin(), | 182 cookie_list.insert(cookie_list.begin(), |
184 it->second->begin(), | 183 it->second->begin(), |
185 it->second->end()); | 184 it->second->end()); |
186 } | 185 } |
187 callback.Run(cookie_list); | 186 callback.Run(cookie_list); |
188 } | 187 } |
189 | 188 |
190 void CannedBrowsingDataCookieHelper::DeleteCookie( | 189 void CannedBrowsingDataCookieHelper::DeleteCookie( |
191 const net::CanonicalCookie& cookie) { | 190 const net::CanonicalCookie& cookie) { |
192 for (OriginCookieListMap::iterator it = origin_cookie_list_map_.begin(); | 191 for (OriginCookieSetMap::iterator it = origin_cookie_set_map_.begin(); |
193 it != origin_cookie_list_map_.end(); | 192 it != origin_cookie_set_map_.end(); |
194 ++it) { | 193 ++it) { |
195 DeleteMatchingCookie(cookie, it->second); | 194 DeleteMatchingCookie(cookie, it->second); |
196 } | 195 } |
197 BrowsingDataCookieHelper::DeleteCookie(cookie); | 196 BrowsingDataCookieHelper::DeleteCookie(cookie); |
198 } | 197 } |
199 | 198 |
200 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( | 199 bool CannedBrowsingDataCookieHelper::DeleteMatchingCookie( |
201 const net::CanonicalCookie& add_cookie, | 200 const net::CanonicalCookie& add_cookie, |
202 net::CookieList* cookie_list) { | 201 canonical_cookie::CookieHashSet* cookie_set) { |
203 typedef net::CookieList::iterator cookie_iterator; | 202 return cookie_set->erase(add_cookie) > 0; |
204 for (cookie_iterator cookie = cookie_list->begin(); | |
205 cookie != cookie_list->end(); ++cookie) { | |
206 if (cookie->Name() == add_cookie.Name() && | |
207 cookie->Domain() == add_cookie.Domain() && | |
208 cookie->Path() == add_cookie.Path()) { | |
209 cookie_list->erase(cookie); | |
210 return true; | |
211 } | |
212 } | |
213 return false; | |
214 } | 203 } |
215 | 204 |
216 net::CookieList* CannedBrowsingDataCookieHelper::GetCookiesFor( | 205 canonical_cookie::CookieHashSet* CannedBrowsingDataCookieHelper::GetCookiesFor( |
217 const GURL& first_party_origin) { | 206 const GURL& first_party_origin) { |
218 OriginCookieListMap::iterator it = | 207 OriginCookieSetMap::iterator it = |
219 origin_cookie_list_map_.find(first_party_origin); | 208 origin_cookie_set_map_.find(first_party_origin); |
220 if (it == origin_cookie_list_map_.end()) { | 209 if (it == origin_cookie_set_map_.end()) { |
221 net::CookieList* cookies = new net::CookieList(); | 210 canonical_cookie::CookieHashSet* cookies = |
222 origin_cookie_list_map_.insert( | 211 new canonical_cookie::CookieHashSet; |
223 std::pair<GURL, net::CookieList*>(first_party_origin, cookies)); | 212 origin_cookie_set_map_.insert( |
| 213 std::pair<GURL, canonical_cookie::CookieHashSet*>(first_party_origin, |
| 214 cookies)); |
224 return cookies; | 215 return cookies; |
225 } | 216 } |
226 return it->second; | 217 return it->second; |
227 } | 218 } |
228 | 219 |
229 void CannedBrowsingDataCookieHelper::AddCookie( | 220 void CannedBrowsingDataCookieHelper::AddCookie( |
230 const GURL& frame_url, | 221 const GURL& frame_url, |
231 const net::CanonicalCookie& cookie) { | 222 const net::CanonicalCookie& cookie) { |
232 // Storing cookies in separate cookie lists per frame origin makes the | 223 // Storing cookies in separate cookie sets per frame origin makes the |
233 // GetCookieCount method count a cookie multiple times if it is stored in | 224 // GetCookieCount method count a cookie multiple times if it is stored in |
234 // multiple lists. | 225 // multiple sets. |
235 // E.g. let "example.com" be redirected to "www.example.com". A cookie set | 226 // E.g. let "example.com" be redirected to "www.example.com". A cookie set |
236 // with the cookie string "A=B; Domain=.example.com" would be sent to both | 227 // with the cookie string "A=B; Domain=.example.com" would be sent to both |
237 // hosts. This means it would be stored in the separate cookie lists for both | 228 // hosts. This means it would be stored in the separate cookie sets for both |
238 // hosts ("example.com", "www.example.com"). The method GetCookieCount would | 229 // hosts ("example.com", "www.example.com"). The method GetCookieCount would |
239 // count this cookie twice. To prevent this, we us a single global cookie | 230 // count this cookie twice. To prevent this, we us a single global cookie |
240 // list as a work-around to store all added cookies. Per frame URL cookie | 231 // set as a work-around to store all added cookies. Per frame URL cookie |
241 // lists are currently not used. In the future they will be used for | 232 // sets are currently not used. In the future they will be used for |
242 // collecting cookies per origin in redirect chains. | 233 // collecting cookies per origin in redirect chains. |
243 // TODO(markusheintz): A) Change the GetCookiesCount method to prevent | 234 // TODO(markusheintz): A) Change the GetCookiesCount method to prevent |
244 // counting cookies multiple times if they are stored in multiple cookie | 235 // counting cookies multiple times if they are stored in multiple cookie |
245 // lists. B) Replace the GetCookieFor method call below with: | 236 // sets. B) Replace the GetCookieFor method call below with: |
246 // "GetCookiesFor(frame_url.GetOrigin());" | 237 // "GetCookiesFor(frame_url.GetOrigin());" |
247 net::CookieList* cookie_list = | 238 CR_DEFINE_STATIC_LOCAL(const GURL, origin_cookie_url, (kGlobalCookieSetURL)); |
248 GetCookiesFor(GURL(kGlobalCookieListURL)); | 239 canonical_cookie::CookieHashSet* cookie_set = |
249 DeleteMatchingCookie(cookie, cookie_list); | 240 GetCookiesFor(origin_cookie_url); |
250 cookie_list->push_back(cookie); | 241 DeleteMatchingCookie(cookie, cookie_set); |
| 242 cookie_set->insert(cookie); |
251 } | 243 } |
OLD | NEW |