Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: net/cookies/cookie_monster.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Portions of this code based on Mozilla: 5 // Portions of this code based on Mozilla:
6 // (netwerk/cookie/src/nsCookieService.cpp) 6 // (netwerk/cookie/src/nsCookieService.cpp)
7 /* ***** BEGIN LICENSE BLOCK ***** 7 /* ***** BEGIN LICENSE BLOCK *****
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 * 9 *
10 * The contents of this file are subject to the Mozilla Public License Version 10 * The contents of this file are subject to the Mozilla Public License Version
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving 89 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving
90 // notification of key load completion triggered by the first request for the 90 // notification of key load completion triggered by the first request for the
91 // same eTLD+1. 91 // same eTLD+1.
92 92
93 static const int kMinutesInTenYears = 10 * 365 * 24 * 60; 93 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
94 94
95 namespace net { 95 namespace net {
96 96
97 // See comments at declaration of these variables in cookie_monster.h 97 // See comments at declaration of these variables in cookie_monster.h
98 // for details. 98 // for details.
99 const size_t CookieMonster::kDomainMaxCookies = 180; 99 const size_t CookieMonster::kDomainMaxCookies = 180;
100 const size_t CookieMonster::kDomainPurgeCookies = 30; 100 const size_t CookieMonster::kDomainPurgeCookies = 30;
101 const size_t CookieMonster::kMaxCookies = 3300; 101 const size_t CookieMonster::kMaxCookies = 3300;
102 const size_t CookieMonster::kPurgeCookies = 300; 102 const size_t CookieMonster::kPurgeCookies = 300;
103 103
104 const size_t CookieMonster::kDomainCookiesQuotaLow = 30; 104 const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
105 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50; 105 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
106 const size_t CookieMonster::kDomainCookiesQuotaHigh = 106 const size_t CookieMonster::kDomainCookiesQuotaHigh =
107 kDomainMaxCookies - kDomainPurgeCookies 107 kDomainMaxCookies - kDomainPurgeCookies - kDomainCookiesQuotaLow -
108 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium; 108 kDomainCookiesQuotaMedium;
109 109
110 const int CookieMonster::kSafeFromGlobalPurgeDays = 30; 110 const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
111 111
112 namespace { 112 namespace {
113 113
114 bool ContainsControlCharacter(const std::string& s) { 114 bool ContainsControlCharacter(const std::string& s) {
115 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { 115 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
116 if ((*i >= 0) && (*i <= 31)) 116 if ((*i >= 0) && (*i <= 31))
117 return true; 117 return true;
118 } 118 }
119 119
120 return false; 120 return false;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 // (2) For each list with more than 1 entry, keep the cookie having the 169 // (2) For each list with more than 1 entry, keep the cookie having the
170 // most recent creation time, and delete the others. 170 // most recent creation time, and delete the others.
171 // 171 //
172 // Two cookies are considered equivalent if they have the same domain, 172 // Two cookies are considered equivalent if they have the same domain,
173 // name, and path. 173 // name, and path.
174 struct CookieSignature { 174 struct CookieSignature {
175 public: 175 public:
176 CookieSignature(const std::string& name, 176 CookieSignature(const std::string& name,
177 const std::string& domain, 177 const std::string& domain,
178 const std::string& path) 178 const std::string& path)
179 : name(name), domain(domain), path(path) { 179 : name(name), domain(domain), path(path) {}
180 }
181 180
182 // To be a key for a map this class needs to be assignable, copyable, 181 // To be a key for a map this class needs to be assignable, copyable,
183 // and have an operator<. The default assignment operator 182 // and have an operator<. The default assignment operator
184 // and copy constructor are exactly what we want. 183 // and copy constructor are exactly what we want.
185 184
186 bool operator<(const CookieSignature& cs) const { 185 bool operator<(const CookieSignature& cs) const {
187 // Name compare dominates, then domain, then path. 186 // Name compare dominates, then domain, then path.
188 int diff = name.compare(cs.name); 187 int diff = name.compare(cs.name);
189 if (diff != 0) 188 if (diff != 0)
190 return diff < 0; 189 return diff < 0;
191 190
192 diff = domain.compare(cs.domain); 191 diff = domain.compare(cs.domain);
193 if (diff != 0) 192 if (diff != 0)
194 return diff < 0; 193 return diff < 0;
195 194
196 return path.compare(cs.path) < 0; 195 return path.compare(cs.path) < 0;
197 } 196 }
198 197
199 std::string name; 198 std::string name;
200 std::string domain; 199 std::string domain;
201 std::string path; 200 std::string path;
202 }; 201 };
203 202
204 // For a CookieItVector iterator range [|it_begin|, |it_end|), 203 // For a CookieItVector iterator range [|it_begin|, |it_end|),
205 // sorts the first |num_sort| + 1 elements by LastAccessDate(). 204 // sorts the first |num_sort| + 1 elements by LastAccessDate().
206 // The + 1 element exists so for any interval of length <= |num_sort| starting 205 // The + 1 element exists so for any interval of length <= |num_sort| starting
207 // from |cookies_its_begin|, a LastAccessDate() bound can be found. 206 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
208 void SortLeastRecentlyAccessed( 207 void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin,
209 CookieMonster::CookieItVector::iterator it_begin, 208 CookieMonster::CookieItVector::iterator it_end,
210 CookieMonster::CookieItVector::iterator it_end, 209 size_t num_sort) {
211 size_t num_sort) {
212 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin); 210 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
213 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter); 211 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
214 } 212 }
215 213
216 // Predicate to support PartitionCookieByPriority(). 214 // Predicate to support PartitionCookieByPriority().
217 struct CookiePriorityEqualsTo 215 struct CookiePriorityEqualsTo
218 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> { 216 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
219 CookiePriorityEqualsTo(CookiePriority priority) 217 CookiePriorityEqualsTo(CookiePriority priority) : priority_(priority) {}
220 : priority_(priority) {}
221 218
222 bool operator()(const CookieMonster::CookieMap::iterator it) const { 219 bool operator()(const CookieMonster::CookieMap::iterator it) const {
223 return it->second->Priority() == priority_; 220 return it->second->Priority() == priority_;
224 } 221 }
225 222
226 const CookiePriority priority_; 223 const CookiePriority priority_;
227 }; 224 };
228 225
229 // For a CookieItVector iterator range [|it_begin|, |it_end|), 226 // For a CookieItVector iterator range [|it_begin|, |it_end|),
230 // moves all cookies with a given |priority| to the beginning of the list. 227 // moves all cookies with a given |priority| to the beginning of the list.
231 // Returns: An iterator in [it_begin, it_end) to the first element with 228 // Returns: An iterator in [it_begin, it_end) to the first element with
232 // priority != |priority|, or |it_end| if all have priority == |priority|. 229 // priority != |priority|, or |it_end| if all have priority == |priority|.
233 CookieMonster::CookieItVector::iterator PartitionCookieByPriority( 230 CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
234 CookieMonster::CookieItVector::iterator it_begin, 231 CookieMonster::CookieItVector::iterator it_begin,
235 CookieMonster::CookieItVector::iterator it_end, 232 CookieMonster::CookieItVector::iterator it_end,
236 CookiePriority priority) { 233 CookiePriority priority) {
237 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority)); 234 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
238 } 235 }
239 236
240 bool LowerBoundAccessDateComparator( 237 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it,
241 const CookieMonster::CookieMap::iterator it, const Time& access_date) { 238 const Time& access_date) {
242 return it->second->LastAccessDate() < access_date; 239 return it->second->LastAccessDate() < access_date;
243 } 240 }
244 241
245 // For a CookieItVector iterator range [|it_begin|, |it_end|) 242 // For a CookieItVector iterator range [|it_begin|, |it_end|)
246 // from a CookieItVector sorted by LastAccessDate(), returns the 243 // from a CookieItVector sorted by LastAccessDate(), returns the
247 // first iterator with access date >= |access_date|, or cookie_its_end if this 244 // first iterator with access date >= |access_date|, or cookie_its_end if this
248 // holds for all. 245 // holds for all.
249 CookieMonster::CookieItVector::iterator LowerBoundAccessDate( 246 CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
250 const CookieMonster::CookieItVector::iterator its_begin, 247 const CookieMonster::CookieItVector::iterator its_begin,
251 const CookieMonster::CookieItVector::iterator its_end, 248 const CookieMonster::CookieItVector::iterator its_end,
252 const Time& access_date) { 249 const Time& access_date) {
253 return std::lower_bound(its_begin, its_end, access_date, 250 return std::lower_bound(
254 LowerBoundAccessDateComparator); 251 its_begin, its_end, access_date, LowerBoundAccessDateComparator);
255 } 252 }
256 253
257 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the 254 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
258 // mapping also provides a boolean that specifies whether or not an 255 // mapping also provides a boolean that specifies whether or not an
259 // OnCookieChanged notification ought to be generated. 256 // OnCookieChanged notification ought to be generated.
260 typedef struct ChangeCausePair_struct { 257 typedef struct ChangeCausePair_struct {
261 CookieMonsterDelegate::ChangeCause cause; 258 CookieMonsterDelegate::ChangeCause cause;
262 bool notify; 259 bool notify;
263 } ChangeCausePair; 260 } ChangeCausePair;
264 ChangeCausePair ChangeCauseMapping[] = { 261 ChangeCausePair ChangeCauseMapping[] = {
265 // DELETE_COOKIE_EXPLICIT 262 // DELETE_COOKIE_EXPLICIT
266 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true }, 263 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true},
267 // DELETE_COOKIE_OVERWRITE 264 // DELETE_COOKIE_OVERWRITE
268 { CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true }, 265 {CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true},
269 // DELETE_COOKIE_EXPIRED 266 // DELETE_COOKIE_EXPIRED
270 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true }, 267 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true},
271 // DELETE_COOKIE_EVICTED 268 // DELETE_COOKIE_EVICTED
272 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, 269 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
273 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE 270 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
274 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }, 271 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false},
275 // DELETE_COOKIE_DONT_RECORD 272 // DELETE_COOKIE_DONT_RECORD
276 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }, 273 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false},
277 // DELETE_COOKIE_EVICTED_DOMAIN 274 // DELETE_COOKIE_EVICTED_DOMAIN
278 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, 275 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
279 // DELETE_COOKIE_EVICTED_GLOBAL 276 // DELETE_COOKIE_EVICTED_GLOBAL
280 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, 277 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
281 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE 278 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
282 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, 279 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
283 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE 280 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
284 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, 281 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
285 // DELETE_COOKIE_EXPIRED_OVERWRITE 282 // DELETE_COOKIE_EXPIRED_OVERWRITE
286 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true }, 283 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true},
287 // DELETE_COOKIE_CONTROL_CHAR 284 // DELETE_COOKIE_CONTROL_CHAR
288 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true}, 285 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
289 // DELETE_COOKIE_LAST_ENTRY 286 // DELETE_COOKIE_LAST_ENTRY
290 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false } 287 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false}};
291 };
292 288
293 std::string BuildCookieLine(const CanonicalCookieVector& cookies) { 289 std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
294 std::string cookie_line; 290 std::string cookie_line;
295 for (CanonicalCookieVector::const_iterator it = cookies.begin(); 291 for (CanonicalCookieVector::const_iterator it = cookies.begin();
296 it != cookies.end(); ++it) { 292 it != cookies.end();
293 ++it) {
297 if (it != cookies.begin()) 294 if (it != cookies.begin())
298 cookie_line += "; "; 295 cookie_line += "; ";
299 // In Mozilla if you set a cookie like AAAA, it will have an empty token 296 // In Mozilla if you set a cookie like AAAA, it will have an empty token
300 // and a value of AAAA. When it sends the cookie back, it will send AAAA, 297 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
301 // so we need to avoid sending =AAAA for a blank token value. 298 // so we need to avoid sending =AAAA for a blank token value.
302 if (!(*it)->Name().empty()) 299 if (!(*it)->Name().empty())
303 cookie_line += (*it)->Name() + "="; 300 cookie_line += (*it)->Name() + "=";
304 cookie_line += (*it)->Value(); 301 cookie_line += (*it)->Value();
305 } 302 }
306 return cookie_line; 303 return cookie_line;
(...skipping 25 matching lines...) Expand all
332 last_access_threshold_(base::TimeDelta::FromMilliseconds( 329 last_access_threshold_(base::TimeDelta::FromMilliseconds(
333 last_access_threshold_milliseconds)), 330 last_access_threshold_milliseconds)),
334 delegate_(delegate), 331 delegate_(delegate),
335 last_statistic_record_time_(base::Time::Now()), 332 last_statistic_record_time_(base::Time::Now()),
336 keep_expired_cookies_(false), 333 keep_expired_cookies_(false),
337 persist_session_cookies_(false) { 334 persist_session_cookies_(false) {
338 InitializeHistograms(); 335 InitializeHistograms();
339 SetDefaultCookieableSchemes(); 336 SetDefaultCookieableSchemes();
340 } 337 }
341 338
342
343 // Task classes for queueing the coming request. 339 // Task classes for queueing the coming request.
344 340
345 class CookieMonster::CookieMonsterTask 341 class CookieMonster::CookieMonsterTask
346 : public base::RefCountedThreadSafe<CookieMonsterTask> { 342 : public base::RefCountedThreadSafe<CookieMonsterTask> {
347 public: 343 public:
348 // Runs the task and invokes the client callback on the thread that 344 // Runs the task and invokes the client callback on the thread that
349 // originally constructed the task. 345 // originally constructed the task.
350 virtual void Run() = 0; 346 virtual void Run() = 0;
351 347
352 protected: 348 protected:
353 explicit CookieMonsterTask(CookieMonster* cookie_monster); 349 explicit CookieMonsterTask(CookieMonster* cookie_monster);
354 virtual ~CookieMonsterTask(); 350 virtual ~CookieMonsterTask();
355 351
356 // Invokes the callback immediately, if the current thread is the one 352 // Invokes the callback immediately, if the current thread is the one
357 // that originated the task, or queues the callback for execution on the 353 // that originated the task, or queues the callback for execution on the
358 // appropriate thread. Maintains a reference to this CookieMonsterTask 354 // appropriate thread. Maintains a reference to this CookieMonsterTask
359 // instance until the callback completes. 355 // instance until the callback completes.
360 void InvokeCallback(base::Closure callback); 356 void InvokeCallback(base::Closure callback);
361 357
362 CookieMonster* cookie_monster() { 358 CookieMonster* cookie_monster() { return cookie_monster_; }
363 return cookie_monster_;
364 }
365 359
366 private: 360 private:
367 friend class base::RefCountedThreadSafe<CookieMonsterTask>; 361 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
368 362
369 CookieMonster* cookie_monster_; 363 CookieMonster* cookie_monster_;
370 scoped_refptr<base::MessageLoopProxy> thread_; 364 scoped_refptr<base::MessageLoopProxy> thread_;
371 365
372 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); 366 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
373 }; 367 };
374 368
375 CookieMonster::CookieMonsterTask::CookieMonsterTask( 369 CookieMonster::CookieMonsterTask::CookieMonsterTask(
376 CookieMonster* cookie_monster) 370 CookieMonster* cookie_monster)
377 : cookie_monster_(cookie_monster), 371 : cookie_monster_(cookie_monster),
378 thread_(base::MessageLoopProxy::current()) { 372 thread_(base::MessageLoopProxy::current()) {
379 } 373 }
380 374
381 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {} 375 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
376 }
382 377
383 // Unfortunately, one cannot re-bind a Callback with parameters into a closure. 378 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
384 // Therefore, the closure passed to InvokeCallback is a clumsy binding of 379 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
385 // Callback::Run on a wrapped Callback instance. Since Callback is not 380 // Callback::Run on a wrapped Callback instance. Since Callback is not
386 // reference counted, we bind to an instance that is a member of the 381 // reference counted, we bind to an instance that is a member of the
387 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a 382 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
388 // message loop because the underlying instance may be destroyed (along with the 383 // message loop because the underlying instance may be destroyed (along with the
389 // CookieMonsterTask instance) in the interim. Therefore, we post a callback 384 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
390 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing 385 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
391 // destruction of the original callback), and which invokes the closure (which 386 // destruction of the original callback), and which invokes the closure (which
392 // invokes the original callback with the returned data). 387 // invokes the original callback with the returned data).
393 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) { 388 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
394 if (thread_->BelongsToCurrentThread()) { 389 if (thread_->BelongsToCurrentThread()) {
395 callback.Run(); 390 callback.Run();
396 } else { 391 } else {
397 thread_->PostTask(FROM_HERE, base::Bind( 392 thread_->PostTask(
398 &CookieMonsterTask::InvokeCallback, this, callback)); 393 FROM_HERE,
394 base::Bind(&CookieMonsterTask::InvokeCallback, this, callback));
399 } 395 }
400 } 396 }
401 397
402 // Task class for SetCookieWithDetails call. 398 // Task class for SetCookieWithDetails call.
403 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask { 399 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
404 public: 400 public:
405 SetCookieWithDetailsTask(CookieMonster* cookie_monster, 401 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
406 const GURL& url, 402 const GURL& url,
407 const std::string& name, 403 const std::string& name,
408 const std::string& value, 404 const std::string& value,
409 const std::string& domain, 405 const std::string& domain,
410 const std::string& path, 406 const std::string& path,
411 const base::Time& expiration_time, 407 const base::Time& expiration_time,
412 bool secure, 408 bool secure,
413 bool http_only, 409 bool http_only,
414 CookiePriority priority, 410 CookiePriority priority,
415 const SetCookiesCallback& callback) 411 const SetCookiesCallback& callback)
416 : CookieMonsterTask(cookie_monster), 412 : CookieMonsterTask(cookie_monster),
417 url_(url), 413 url_(url),
418 name_(name), 414 name_(name),
419 value_(value), 415 value_(value),
420 domain_(domain), 416 domain_(domain),
421 path_(path), 417 path_(path),
422 expiration_time_(expiration_time), 418 expiration_time_(expiration_time),
423 secure_(secure), 419 secure_(secure),
424 http_only_(http_only), 420 http_only_(http_only),
425 priority_(priority), 421 priority_(priority),
426 callback_(callback) { 422 callback_(callback) {}
427 }
428 423
429 // CookieMonsterTask: 424 // CookieMonsterTask:
430 virtual void Run() OVERRIDE; 425 virtual void Run() OVERRIDE;
431 426
432 protected: 427 protected:
433 virtual ~SetCookieWithDetailsTask() {} 428 virtual ~SetCookieWithDetailsTask() {}
434 429
435 private: 430 private:
436 GURL url_; 431 GURL url_;
437 std::string name_; 432 std::string name_;
438 std::string value_; 433 std::string value_;
439 std::string domain_; 434 std::string domain_;
440 std::string path_; 435 std::string path_;
441 base::Time expiration_time_; 436 base::Time expiration_time_;
442 bool secure_; 437 bool secure_;
443 bool http_only_; 438 bool http_only_;
444 CookiePriority priority_; 439 CookiePriority priority_;
445 SetCookiesCallback callback_; 440 SetCookiesCallback callback_;
446 441
447 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); 442 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
448 }; 443 };
449 444
450 void CookieMonster::SetCookieWithDetailsTask::Run() { 445 void CookieMonster::SetCookieWithDetailsTask::Run() {
451 bool success = this->cookie_monster()-> 446 bool success = this->cookie_monster()->SetCookieWithDetails(url_,
452 SetCookieWithDetails(url_, name_, value_, domain_, path_, 447 name_,
453 expiration_time_, secure_, http_only_, priority_); 448 value_,
449 domain_,
450 path_,
451 expiration_time_,
452 secure_,
453 http_only_,
454 priority_);
454 if (!callback_.is_null()) { 455 if (!callback_.is_null()) {
455 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run, 456 this->InvokeCallback(base::Bind(
456 base::Unretained(&callback_), success)); 457 &SetCookiesCallback::Run, base::Unretained(&callback_), success));
457 } 458 }
458 } 459 }
459 460
460 // Task class for GetAllCookies call. 461 // Task class for GetAllCookies call.
461 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask { 462 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
462 public: 463 public:
463 GetAllCookiesTask(CookieMonster* cookie_monster, 464 GetAllCookiesTask(CookieMonster* cookie_monster,
464 const GetCookieListCallback& callback) 465 const GetCookieListCallback& callback)
465 : CookieMonsterTask(cookie_monster), 466 : CookieMonsterTask(cookie_monster), callback_(callback) {}
466 callback_(callback) {
467 }
468 467
469 // CookieMonsterTask 468 // CookieMonsterTask
470 virtual void Run() OVERRIDE; 469 virtual void Run() OVERRIDE;
471 470
472 protected: 471 protected:
473 virtual ~GetAllCookiesTask() {} 472 virtual ~GetAllCookiesTask() {}
474 473
475 private: 474 private:
476 GetCookieListCallback callback_; 475 GetCookieListCallback callback_;
477 476
478 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); 477 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
479 }; 478 };
480 479
481 void CookieMonster::GetAllCookiesTask::Run() { 480 void CookieMonster::GetAllCookiesTask::Run() {
482 if (!callback_.is_null()) { 481 if (!callback_.is_null()) {
483 CookieList cookies = this->cookie_monster()->GetAllCookies(); 482 CookieList cookies = this->cookie_monster()->GetAllCookies();
484 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run, 483 this->InvokeCallback(base::Bind(
485 base::Unretained(&callback_), cookies)); 484 &GetCookieListCallback::Run, base::Unretained(&callback_), cookies));
486 } 485 }
487 } 486 }
488 487
489 // Task class for GetAllCookiesForURLWithOptions call. 488 // Task class for GetAllCookiesForURLWithOptions call.
490 class CookieMonster::GetAllCookiesForURLWithOptionsTask 489 class CookieMonster::GetAllCookiesForURLWithOptionsTask
491 : public CookieMonsterTask { 490 : public CookieMonsterTask {
492 public: 491 public:
493 GetAllCookiesForURLWithOptionsTask( 492 GetAllCookiesForURLWithOptionsTask(CookieMonster* cookie_monster,
494 CookieMonster* cookie_monster, 493 const GURL& url,
495 const GURL& url, 494 const CookieOptions& options,
496 const CookieOptions& options, 495 const GetCookieListCallback& callback)
497 const GetCookieListCallback& callback)
498 : CookieMonsterTask(cookie_monster), 496 : CookieMonsterTask(cookie_monster),
499 url_(url), 497 url_(url),
500 options_(options), 498 options_(options),
501 callback_(callback) { 499 callback_(callback) {}
502 }
503 500
504 // CookieMonsterTask: 501 // CookieMonsterTask:
505 virtual void Run() OVERRIDE; 502 virtual void Run() OVERRIDE;
506 503
507 protected: 504 protected:
508 virtual ~GetAllCookiesForURLWithOptionsTask() {} 505 virtual ~GetAllCookiesForURLWithOptionsTask() {}
509 506
510 private: 507 private:
511 GURL url_; 508 GURL url_;
512 CookieOptions options_; 509 CookieOptions options_;
513 GetCookieListCallback callback_; 510 GetCookieListCallback callback_;
514 511
515 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask); 512 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
516 }; 513 };
517 514
518 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() { 515 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
519 if (!callback_.is_null()) { 516 if (!callback_.is_null()) {
520 CookieList cookies = this->cookie_monster()-> 517 CookieList cookies =
521 GetAllCookiesForURLWithOptions(url_, options_); 518 this->cookie_monster()->GetAllCookiesForURLWithOptions(url_, options_);
522 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run, 519 this->InvokeCallback(base::Bind(
523 base::Unretained(&callback_), cookies)); 520 &GetCookieListCallback::Run, base::Unretained(&callback_), cookies));
524 } 521 }
525 } 522 }
526 523
527 template <typename Result> struct CallbackType { 524 template <typename Result>
525 struct CallbackType {
528 typedef base::Callback<void(Result)> Type; 526 typedef base::Callback<void(Result)> Type;
529 }; 527 };
530 528
531 template <> struct CallbackType<void> { 529 template <>
530 struct CallbackType<void> {
532 typedef base::Closure Type; 531 typedef base::Closure Type;
533 }; 532 };
534 533
535 // Base task class for Delete*Task. 534 // Base task class for Delete*Task.
536 template <typename Result> 535 template <typename Result>
537 class CookieMonster::DeleteTask : public CookieMonsterTask { 536 class CookieMonster::DeleteTask : public CookieMonsterTask {
538 public: 537 public:
539 DeleteTask(CookieMonster* cookie_monster, 538 DeleteTask(CookieMonster* cookie_monster,
540 const typename CallbackType<Result>::Type& callback) 539 const typename CallbackType<Result>::Type& callback)
541 : CookieMonsterTask(cookie_monster), 540 : CookieMonsterTask(cookie_monster), callback_(callback) {}
542 callback_(callback) {
543 }
544 541
545 // CookieMonsterTask: 542 // CookieMonsterTask:
546 virtual void Run() OVERRIDE; 543 virtual void Run() OVERRIDE;
547 544
548 private: 545 private:
549 // Runs the delete task and returns a result. 546 // Runs the delete task and returns a result.
550 virtual Result RunDeleteTask() = 0; 547 virtual Result RunDeleteTask() = 0;
551 base::Closure RunDeleteTaskAndBindCallback(); 548 base::Closure RunDeleteTaskAndBindCallback();
552 void FlushDone(const base::Closure& callback); 549 void FlushDone(const base::Closure& callback);
553 550
554 typename CallbackType<Result>::Type callback_; 551 typename CallbackType<Result>::Type callback_;
555 552
556 DISALLOW_COPY_AND_ASSIGN(DeleteTask); 553 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
557 }; 554 };
558 555
559 template <typename Result> 556 template <typename Result>
560 base::Closure CookieMonster::DeleteTask<Result>:: 557 base::Closure
561 RunDeleteTaskAndBindCallback() { 558 CookieMonster::DeleteTask<Result>::RunDeleteTaskAndBindCallback() {
562 Result result = RunDeleteTask(); 559 Result result = RunDeleteTask();
563 if (callback_.is_null()) 560 if (callback_.is_null())
564 return base::Closure(); 561 return base::Closure();
565 return base::Bind(callback_, result); 562 return base::Bind(callback_, result);
566 } 563 }
567 564
568 template <> 565 template <>
569 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() { 566 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
570 RunDeleteTask(); 567 RunDeleteTask();
571 return callback_; 568 return callback_;
572 } 569 }
573 570
574 template <typename Result> 571 template <typename Result>
575 void CookieMonster::DeleteTask<Result>::Run() { 572 void CookieMonster::DeleteTask<Result>::Run() {
576 this->cookie_monster()->FlushStore( 573 this->cookie_monster()->FlushStore(base::Bind(
577 base::Bind(&DeleteTask<Result>::FlushDone, this, 574 &DeleteTask<Result>::FlushDone, this, RunDeleteTaskAndBindCallback()));
578 RunDeleteTaskAndBindCallback()));
579 } 575 }
580 576
581 template <typename Result> 577 template <typename Result>
582 void CookieMonster::DeleteTask<Result>::FlushDone( 578 void CookieMonster::DeleteTask<Result>::FlushDone(
583 const base::Closure& callback) { 579 const base::Closure& callback) {
584 if (!callback.is_null()) { 580 if (!callback.is_null()) {
585 this->InvokeCallback(callback); 581 this->InvokeCallback(callback);
586 } 582 }
587 } 583 }
588 584
589 // Task class for DeleteAll call. 585 // Task class for DeleteAll call.
590 class CookieMonster::DeleteAllTask : public DeleteTask<int> { 586 class CookieMonster::DeleteAllTask : public DeleteTask<int> {
591 public: 587 public:
592 DeleteAllTask(CookieMonster* cookie_monster, 588 DeleteAllTask(CookieMonster* cookie_monster, const DeleteCallback& callback)
593 const DeleteCallback& callback) 589 : DeleteTask<int>(cookie_monster, callback) {}
594 : DeleteTask<int>(cookie_monster, callback) {
595 }
596 590
597 // DeleteTask: 591 // DeleteTask:
598 virtual int RunDeleteTask() OVERRIDE; 592 virtual int RunDeleteTask() OVERRIDE;
599 593
600 protected: 594 protected:
601 virtual ~DeleteAllTask() {} 595 virtual ~DeleteAllTask() {}
602 596
603 private: 597 private:
604 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask); 598 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
605 }; 599 };
606 600
607 int CookieMonster::DeleteAllTask::RunDeleteTask() { 601 int CookieMonster::DeleteAllTask::RunDeleteTask() {
608 return this->cookie_monster()->DeleteAll(true); 602 return this->cookie_monster()->DeleteAll(true);
609 } 603 }
610 604
611 // Task class for DeleteAllCreatedBetween call. 605 // Task class for DeleteAllCreatedBetween call.
612 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> { 606 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
613 public: 607 public:
614 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster, 608 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
615 const Time& delete_begin, 609 const Time& delete_begin,
616 const Time& delete_end, 610 const Time& delete_end,
617 const DeleteCallback& callback) 611 const DeleteCallback& callback)
618 : DeleteTask<int>(cookie_monster, callback), 612 : DeleteTask<int>(cookie_monster, callback),
619 delete_begin_(delete_begin), 613 delete_begin_(delete_begin),
620 delete_end_(delete_end) { 614 delete_end_(delete_end) {}
621 }
622 615
623 // DeleteTask: 616 // DeleteTask:
624 virtual int RunDeleteTask() OVERRIDE; 617 virtual int RunDeleteTask() OVERRIDE;
625 618
626 protected: 619 protected:
627 virtual ~DeleteAllCreatedBetweenTask() {} 620 virtual ~DeleteAllCreatedBetweenTask() {}
628 621
629 private: 622 private:
630 Time delete_begin_; 623 Time delete_begin_;
631 Time delete_end_; 624 Time delete_end_;
632 625
633 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); 626 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
634 }; 627 };
635 628
636 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() { 629 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
637 return this->cookie_monster()-> 630 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_,
638 DeleteAllCreatedBetween(delete_begin_, delete_end_); 631 delete_end_);
639 } 632 }
640 633
641 // Task class for DeleteAllForHost call. 634 // Task class for DeleteAllForHost call.
642 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> { 635 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
643 public: 636 public:
644 DeleteAllForHostTask(CookieMonster* cookie_monster, 637 DeleteAllForHostTask(CookieMonster* cookie_monster,
645 const GURL& url, 638 const GURL& url,
646 const DeleteCallback& callback) 639 const DeleteCallback& callback)
647 : DeleteTask<int>(cookie_monster, callback), 640 : DeleteTask<int>(cookie_monster, callback), url_(url) {}
648 url_(url) {
649 }
650 641
651 // DeleteTask: 642 // DeleteTask:
652 virtual int RunDeleteTask() OVERRIDE; 643 virtual int RunDeleteTask() OVERRIDE;
653 644
654 protected: 645 protected:
655 virtual ~DeleteAllForHostTask() {} 646 virtual ~DeleteAllForHostTask() {}
656 647
657 private: 648 private:
658 GURL url_; 649 GURL url_;
659 650
660 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask); 651 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
661 }; 652 };
662 653
663 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() { 654 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
664 return this->cookie_monster()->DeleteAllForHost(url_); 655 return this->cookie_monster()->DeleteAllForHost(url_);
665 } 656 }
666 657
667 // Task class for DeleteAllCreatedBetweenForHost call. 658 // Task class for DeleteAllCreatedBetweenForHost call.
668 class CookieMonster::DeleteAllCreatedBetweenForHostTask 659 class CookieMonster::DeleteAllCreatedBetweenForHostTask
669 : public DeleteTask<int> { 660 : public DeleteTask<int> {
670 public: 661 public:
671 DeleteAllCreatedBetweenForHostTask( 662 DeleteAllCreatedBetweenForHostTask(CookieMonster* cookie_monster,
672 CookieMonster* cookie_monster, 663 Time delete_begin,
673 Time delete_begin, 664 Time delete_end,
674 Time delete_end, 665 const GURL& url,
675 const GURL& url, 666 const DeleteCallback& callback)
676 const DeleteCallback& callback)
677 : DeleteTask<int>(cookie_monster, callback), 667 : DeleteTask<int>(cookie_monster, callback),
678 delete_begin_(delete_begin), 668 delete_begin_(delete_begin),
679 delete_end_(delete_end), 669 delete_end_(delete_end),
680 url_(url) { 670 url_(url) {}
681 }
682 671
683 // DeleteTask: 672 // DeleteTask:
684 virtual int RunDeleteTask() OVERRIDE; 673 virtual int RunDeleteTask() OVERRIDE;
685 674
686 protected: 675 protected:
687 virtual ~DeleteAllCreatedBetweenForHostTask() {} 676 virtual ~DeleteAllCreatedBetweenForHostTask() {}
688 677
689 private: 678 private:
690 Time delete_begin_; 679 Time delete_begin_;
691 Time delete_end_; 680 Time delete_end_;
692 GURL url_; 681 GURL url_;
693 682
694 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask); 683 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
695 }; 684 };
696 685
697 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() { 686 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
698 return this->cookie_monster()->DeleteAllCreatedBetweenForHost( 687 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
699 delete_begin_, delete_end_, url_); 688 delete_begin_, delete_end_, url_);
700 } 689 }
701 690
702 // Task class for DeleteCanonicalCookie call. 691 // Task class for DeleteCanonicalCookie call.
703 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> { 692 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
704 public: 693 public:
705 DeleteCanonicalCookieTask(CookieMonster* cookie_monster, 694 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
706 const CanonicalCookie& cookie, 695 const CanonicalCookie& cookie,
707 const DeleteCookieCallback& callback) 696 const DeleteCookieCallback& callback)
708 : DeleteTask<bool>(cookie_monster, callback), 697 : DeleteTask<bool>(cookie_monster, callback), cookie_(cookie) {}
709 cookie_(cookie) {
710 }
711 698
712 // DeleteTask: 699 // DeleteTask:
713 virtual bool RunDeleteTask() OVERRIDE; 700 virtual bool RunDeleteTask() OVERRIDE;
714 701
715 protected: 702 protected:
716 virtual ~DeleteCanonicalCookieTask() {} 703 virtual ~DeleteCanonicalCookieTask() {}
717 704
718 private: 705 private:
719 CanonicalCookie cookie_; 706 CanonicalCookie cookie_;
720 707
721 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); 708 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
722 }; 709 };
723 710
724 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() { 711 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
725 return this->cookie_monster()->DeleteCanonicalCookie(cookie_); 712 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
726 } 713 }
727 714
728 // Task class for SetCookieWithOptions call. 715 // Task class for SetCookieWithOptions call.
729 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask { 716 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
730 public: 717 public:
731 SetCookieWithOptionsTask(CookieMonster* cookie_monster, 718 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
732 const GURL& url, 719 const GURL& url,
733 const std::string& cookie_line, 720 const std::string& cookie_line,
734 const CookieOptions& options, 721 const CookieOptions& options,
735 const SetCookiesCallback& callback) 722 const SetCookiesCallback& callback)
736 : CookieMonsterTask(cookie_monster), 723 : CookieMonsterTask(cookie_monster),
737 url_(url), 724 url_(url),
738 cookie_line_(cookie_line), 725 cookie_line_(cookie_line),
739 options_(options), 726 options_(options),
740 callback_(callback) { 727 callback_(callback) {}
741 }
742 728
743 // CookieMonsterTask: 729 // CookieMonsterTask:
744 virtual void Run() OVERRIDE; 730 virtual void Run() OVERRIDE;
745 731
746 protected: 732 protected:
747 virtual ~SetCookieWithOptionsTask() {} 733 virtual ~SetCookieWithOptionsTask() {}
748 734
749 private: 735 private:
750 GURL url_; 736 GURL url_;
751 std::string cookie_line_; 737 std::string cookie_line_;
752 CookieOptions options_; 738 CookieOptions options_;
753 SetCookiesCallback callback_; 739 SetCookiesCallback callback_;
754 740
755 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); 741 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
756 }; 742 };
757 743
758 void CookieMonster::SetCookieWithOptionsTask::Run() { 744 void CookieMonster::SetCookieWithOptionsTask::Run() {
759 bool result = this->cookie_monster()-> 745 bool result = this->cookie_monster()->SetCookieWithOptions(
760 SetCookieWithOptions(url_, cookie_line_, options_); 746 url_, cookie_line_, options_);
761 if (!callback_.is_null()) { 747 if (!callback_.is_null()) {
762 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run, 748 this->InvokeCallback(base::Bind(
763 base::Unretained(&callback_), result)); 749 &SetCookiesCallback::Run, base::Unretained(&callback_), result));
764 } 750 }
765 } 751 }
766 752
767 // Task class for GetCookiesWithOptions call. 753 // Task class for GetCookiesWithOptions call.
768 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask { 754 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
769 public: 755 public:
770 GetCookiesWithOptionsTask(CookieMonster* cookie_monster, 756 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
771 const GURL& url, 757 const GURL& url,
772 const CookieOptions& options, 758 const CookieOptions& options,
773 const GetCookiesCallback& callback) 759 const GetCookiesCallback& callback)
774 : CookieMonsterTask(cookie_monster), 760 : CookieMonsterTask(cookie_monster),
775 url_(url), 761 url_(url),
776 options_(options), 762 options_(options),
777 callback_(callback) { 763 callback_(callback) {}
778 }
779 764
780 // CookieMonsterTask: 765 // CookieMonsterTask:
781 virtual void Run() OVERRIDE; 766 virtual void Run() OVERRIDE;
782 767
783 protected: 768 protected:
784 virtual ~GetCookiesWithOptionsTask() {} 769 virtual ~GetCookiesWithOptionsTask() {}
785 770
786 private: 771 private:
787 GURL url_; 772 GURL url_;
788 CookieOptions options_; 773 CookieOptions options_;
789 GetCookiesCallback callback_; 774 GetCookiesCallback callback_;
790 775
791 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); 776 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
792 }; 777 };
793 778
794 void CookieMonster::GetCookiesWithOptionsTask::Run() { 779 void CookieMonster::GetCookiesWithOptionsTask::Run() {
795 std::string cookie = this->cookie_monster()-> 780 std::string cookie =
796 GetCookiesWithOptions(url_, options_); 781 this->cookie_monster()->GetCookiesWithOptions(url_, options_);
797 if (!callback_.is_null()) { 782 if (!callback_.is_null()) {
798 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run, 783 this->InvokeCallback(base::Bind(
799 base::Unretained(&callback_), cookie)); 784 &GetCookiesCallback::Run, base::Unretained(&callback_), cookie));
800 } 785 }
801 } 786 }
802 787
803 // Task class for DeleteCookie call. 788 // Task class for DeleteCookie call.
804 class CookieMonster::DeleteCookieTask : public DeleteTask<void> { 789 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
805 public: 790 public:
806 DeleteCookieTask(CookieMonster* cookie_monster, 791 DeleteCookieTask(CookieMonster* cookie_monster,
807 const GURL& url, 792 const GURL& url,
808 const std::string& cookie_name, 793 const std::string& cookie_name,
809 const base::Closure& callback) 794 const base::Closure& callback)
810 : DeleteTask<void>(cookie_monster, callback), 795 : DeleteTask<void>(cookie_monster, callback),
811 url_(url), 796 url_(url),
812 cookie_name_(cookie_name) { 797 cookie_name_(cookie_name) {}
813 }
814 798
815 // DeleteTask: 799 // DeleteTask:
816 virtual void RunDeleteTask() OVERRIDE; 800 virtual void RunDeleteTask() OVERRIDE;
817 801
818 protected: 802 protected:
819 virtual ~DeleteCookieTask() {} 803 virtual ~DeleteCookieTask() {}
820 804
821 private: 805 private:
822 GURL url_; 806 GURL url_;
823 std::string cookie_name_; 807 std::string cookie_name_;
824 808
825 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); 809 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
826 }; 810 };
827 811
828 void CookieMonster::DeleteCookieTask::RunDeleteTask() { 812 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
829 this->cookie_monster()->DeleteCookie(url_, cookie_name_); 813 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
830 } 814 }
831 815
832 // Task class for DeleteSessionCookies call. 816 // Task class for DeleteSessionCookies call.
833 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> { 817 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
834 public: 818 public:
835 DeleteSessionCookiesTask(CookieMonster* cookie_monster, 819 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
836 const DeleteCallback& callback) 820 const DeleteCallback& callback)
837 : DeleteTask<int>(cookie_monster, callback) { 821 : DeleteTask<int>(cookie_monster, callback) {}
838 }
839 822
840 // DeleteTask: 823 // DeleteTask:
841 virtual int RunDeleteTask() OVERRIDE; 824 virtual int RunDeleteTask() OVERRIDE;
842 825
843 protected: 826 protected:
844 virtual ~DeleteSessionCookiesTask() {} 827 virtual ~DeleteSessionCookiesTask() {}
845 828
846 private: 829 private:
847
848 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask); 830 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
849 }; 831 };
850 832
851 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() { 833 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
852 return this->cookie_monster()->DeleteSessionCookies(); 834 return this->cookie_monster()->DeleteSessionCookies();
853 } 835 }
854 836
855 // Task class for HasCookiesForETLDP1Task call. 837 // Task class for HasCookiesForETLDP1Task call.
856 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask { 838 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
857 public: 839 public:
858 HasCookiesForETLDP1Task( 840 HasCookiesForETLDP1Task(CookieMonster* cookie_monster,
859 CookieMonster* cookie_monster, 841 const std::string& etldp1,
860 const std::string& etldp1, 842 const HasCookiesForETLDP1Callback& callback)
861 const HasCookiesForETLDP1Callback& callback)
862 : CookieMonsterTask(cookie_monster), 843 : CookieMonsterTask(cookie_monster),
863 etldp1_(etldp1), 844 etldp1_(etldp1),
864 callback_(callback) { 845 callback_(callback) {}
865 }
866 846
867 // CookieMonsterTask: 847 // CookieMonsterTask:
868 virtual void Run() OVERRIDE; 848 virtual void Run() OVERRIDE;
869 849
870 protected: 850 protected:
871 virtual ~HasCookiesForETLDP1Task() {} 851 virtual ~HasCookiesForETLDP1Task() {}
872 852
873 private: 853 private:
874 std::string etldp1_; 854 std::string etldp1_;
875 HasCookiesForETLDP1Callback callback_; 855 HasCookiesForETLDP1Callback callback_;
876 856
877 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task); 857 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
878 }; 858 };
879 859
880 void CookieMonster::HasCookiesForETLDP1Task::Run() { 860 void CookieMonster::HasCookiesForETLDP1Task::Run() {
881 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_); 861 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
882 if (!callback_.is_null()) { 862 if (!callback_.is_null()) {
883 this->InvokeCallback( 863 this->InvokeCallback(base::Bind(&HasCookiesForETLDP1Callback::Run,
884 base::Bind(&HasCookiesForETLDP1Callback::Run, 864 base::Unretained(&callback_),
885 base::Unretained(&callback_), result)); 865 result));
886 } 866 }
887 } 867 }
888 868
889 // Asynchronous CookieMonster API 869 // Asynchronous CookieMonster API
890 870
891 void CookieMonster::SetCookieWithDetailsAsync( 871 void CookieMonster::SetCookieWithDetailsAsync(
892 const GURL& url, 872 const GURL& url,
893 const std::string& name, 873 const std::string& name,
894 const std::string& value, 874 const std::string& value,
895 const std::string& domain, 875 const std::string& domain,
896 const std::string& path, 876 const std::string& path,
897 const Time& expiration_time, 877 const Time& expiration_time,
898 bool secure, 878 bool secure,
899 bool http_only, 879 bool http_only,
900 CookiePriority priority, 880 CookiePriority priority,
901 const SetCookiesCallback& callback) { 881 const SetCookiesCallback& callback) {
902 scoped_refptr<SetCookieWithDetailsTask> task = 882 scoped_refptr<SetCookieWithDetailsTask> task =
903 new SetCookieWithDetailsTask(this, url, name, value, domain, path, 883 new SetCookieWithDetailsTask(this,
904 expiration_time, secure, http_only, priority, 884 url,
885 name,
886 value,
887 domain,
888 path,
889 expiration_time,
890 secure,
891 http_only,
892 priority,
905 callback); 893 callback);
906 894
907 DoCookieTaskForURL(task, url); 895 DoCookieTaskForURL(task, url);
908 } 896 }
909 897
910 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { 898 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
911 scoped_refptr<GetAllCookiesTask> task = 899 scoped_refptr<GetAllCookiesTask> task = new GetAllCookiesTask(this, callback);
912 new GetAllCookiesTask(this, callback);
913 900
914 DoCookieTask(task); 901 DoCookieTask(task);
915 } 902 }
916 903
917
918 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( 904 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
919 const GURL& url, 905 const GURL& url,
920 const CookieOptions& options, 906 const CookieOptions& options,
921 const GetCookieListCallback& callback) { 907 const GetCookieListCallback& callback) {
922 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = 908 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
923 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); 909 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
924 910
925 DoCookieTaskForURL(task, url); 911 DoCookieTaskForURL(task, url);
926 } 912 }
927 913
928 void CookieMonster::GetAllCookiesForURLAsync( 914 void CookieMonster::GetAllCookiesForURLAsync(
929 const GURL& url, const GetCookieListCallback& callback) { 915 const GURL& url,
916 const GetCookieListCallback& callback) {
930 CookieOptions options; 917 CookieOptions options;
931 options.set_include_httponly(); 918 options.set_include_httponly();
932 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = 919 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
933 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); 920 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
934 921
935 DoCookieTaskForURL(task, url); 922 DoCookieTaskForURL(task, url);
936 } 923 }
937 924
938 void CookieMonster::HasCookiesForETLDP1Async( 925 void CookieMonster::HasCookiesForETLDP1Async(
939 const std::string& etldp1, 926 const std::string& etldp1,
940 const HasCookiesForETLDP1Callback& callback) { 927 const HasCookiesForETLDP1Callback& callback) {
941 scoped_refptr<HasCookiesForETLDP1Task> task = 928 scoped_refptr<HasCookiesForETLDP1Task> task =
942 new HasCookiesForETLDP1Task(this, etldp1, callback); 929 new HasCookiesForETLDP1Task(this, etldp1, callback);
943 930
944 DoCookieTaskForURL(task, GURL("http://" + etldp1)); 931 DoCookieTaskForURL(task, GURL("http://" + etldp1));
945 } 932 }
946 933
947 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { 934 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
948 scoped_refptr<DeleteAllTask> task = 935 scoped_refptr<DeleteAllTask> task = new DeleteAllTask(this, callback);
949 new DeleteAllTask(this, callback);
950 936
951 DoCookieTask(task); 937 DoCookieTask(task);
952 } 938 }
953 939
954 void CookieMonster::DeleteAllCreatedBetweenAsync( 940 void CookieMonster::DeleteAllCreatedBetweenAsync(
955 const Time& delete_begin, const Time& delete_end, 941 const Time& delete_begin,
942 const Time& delete_end,
956 const DeleteCallback& callback) { 943 const DeleteCallback& callback) {
957 scoped_refptr<DeleteAllCreatedBetweenTask> task = 944 scoped_refptr<DeleteAllCreatedBetweenTask> task =
958 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, 945 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, callback);
959 callback);
960 946
961 DoCookieTask(task); 947 DoCookieTask(task);
962 } 948 }
963 949
964 void CookieMonster::DeleteAllCreatedBetweenForHostAsync( 950 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
965 const Time delete_begin, 951 const Time delete_begin,
966 const Time delete_end, 952 const Time delete_end,
967 const GURL& url, 953 const GURL& url,
968 const DeleteCallback& callback) { 954 const DeleteCallback& callback) {
969 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task = 955 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
970 new DeleteAllCreatedBetweenForHostTask( 956 new DeleteAllCreatedBetweenForHostTask(
971 this, delete_begin, delete_end, url, callback); 957 this, delete_begin, delete_end, url, callback);
972 958
973 DoCookieTaskForURL(task, url); 959 DoCookieTaskForURL(task, url);
974 } 960 }
975 961
976 void CookieMonster::DeleteAllForHostAsync( 962 void CookieMonster::DeleteAllForHostAsync(const GURL& url,
977 const GURL& url, const DeleteCallback& callback) { 963 const DeleteCallback& callback) {
978 scoped_refptr<DeleteAllForHostTask> task = 964 scoped_refptr<DeleteAllForHostTask> task =
979 new DeleteAllForHostTask(this, url, callback); 965 new DeleteAllForHostTask(this, url, callback);
980 966
981 DoCookieTaskForURL(task, url); 967 DoCookieTaskForURL(task, url);
982 } 968 }
983 969
984 void CookieMonster::DeleteCanonicalCookieAsync( 970 void CookieMonster::DeleteCanonicalCookieAsync(
985 const CanonicalCookie& cookie, 971 const CanonicalCookie& cookie,
986 const DeleteCookieCallback& callback) { 972 const DeleteCookieCallback& callback) {
987 scoped_refptr<DeleteCanonicalCookieTask> task = 973 scoped_refptr<DeleteCanonicalCookieTask> task =
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 void CookieMonster::DoCookieTaskForURL( 1031 void CookieMonster::DoCookieTaskForURL(
1046 const scoped_refptr<CookieMonsterTask>& task_item, 1032 const scoped_refptr<CookieMonsterTask>& task_item,
1047 const GURL& url) { 1033 const GURL& url) {
1048 { 1034 {
1049 base::AutoLock autolock(lock_); 1035 base::AutoLock autolock(lock_);
1050 InitIfNecessary(); 1036 InitIfNecessary();
1051 // If cookies for the requested domain key (eTLD+1) have been loaded from DB 1037 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1052 // then run the task, otherwise load from DB. 1038 // then run the task, otherwise load from DB.
1053 if (!loaded_) { 1039 if (!loaded_) {
1054 // Checks if the domain key has been loaded. 1040 // Checks if the domain key has been loaded.
1055 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), 1041 std::string key(
1056 url.host())); 1042 cookie_util::GetEffectiveDomain(url.scheme(), url.host()));
1057 if (keys_loaded_.find(key) == keys_loaded_.end()) { 1043 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1058 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > 1044 std::map<std::string,
1059 ::iterator it = tasks_pending_for_key_.find(key); 1045 std::deque<scoped_refptr<CookieMonsterTask> > >::iterator it =
1046 tasks_pending_for_key_.find(key);
1060 if (it == tasks_pending_for_key_.end()) { 1047 if (it == tasks_pending_for_key_.end()) {
1061 store_->LoadCookiesForKey(key, 1048 store_->LoadCookiesForKey(
1062 base::Bind(&CookieMonster::OnKeyLoaded, this, key)); 1049 key, base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1063 it = tasks_pending_for_key_.insert(std::make_pair(key, 1050 it = tasks_pending_for_key_
1064 std::deque<scoped_refptr<CookieMonsterTask> >())).first; 1051 .insert(std::make_pair(
1052 key, std::deque<scoped_refptr<CookieMonsterTask> >()))
1053 .first;
1065 } 1054 }
1066 it->second.push_back(task_item); 1055 it->second.push_back(task_item);
1067 return; 1056 return;
1068 } 1057 }
1069 } 1058 }
1070 } 1059 }
1071 task_item->Run(); 1060 task_item->Run();
1072 } 1061 }
1073 1062
1074 bool CookieMonster::SetCookieWithDetails(const GURL& url, 1063 bool CookieMonster::SetCookieWithDetails(const GURL& url,
1075 const std::string& name, 1064 const std::string& name,
1076 const std::string& value, 1065 const std::string& value,
1077 const std::string& domain, 1066 const std::string& domain,
1078 const std::string& path, 1067 const std::string& path,
1079 const base::Time& expiration_time, 1068 const base::Time& expiration_time,
1080 bool secure, 1069 bool secure,
1081 bool http_only, 1070 bool http_only,
1082 CookiePriority priority) { 1071 CookiePriority priority) {
1083 base::AutoLock autolock(lock_); 1072 base::AutoLock autolock(lock_);
1084 1073
1085 if (!HasCookieableScheme(url)) 1074 if (!HasCookieableScheme(url))
1086 return false; 1075 return false;
1087 1076
1088 Time creation_time = CurrentTime(); 1077 Time creation_time = CurrentTime();
1089 last_time_seen_ = creation_time; 1078 last_time_seen_ = creation_time;
1090 1079
1091 scoped_ptr<CanonicalCookie> cc; 1080 scoped_ptr<CanonicalCookie> cc;
1092 cc.reset(CanonicalCookie::Create(url, name, value, domain, path, 1081 cc.reset(CanonicalCookie::Create(url,
1093 creation_time, expiration_time, 1082 name,
1094 secure, http_only, priority)); 1083 value,
1084 domain,
1085 path,
1086 creation_time,
1087 expiration_time,
1088 secure,
1089 http_only,
1090 priority));
1095 1091
1096 if (!cc.get()) 1092 if (!cc.get())
1097 return false; 1093 return false;
1098 1094
1099 CookieOptions options; 1095 CookieOptions options;
1100 options.set_include_httponly(); 1096 options.set_include_httponly();
1101 return SetCanonicalCookie(&cc, creation_time, options); 1097 return SetCanonicalCookie(&cc, creation_time, options);
1102 } 1098 }
1103 1099
1104 bool CookieMonster::InitializeFrom(const CookieList& list) { 1100 bool CookieMonster::InitializeFrom(const CookieList& list) {
1105 base::AutoLock autolock(lock_); 1101 base::AutoLock autolock(lock_);
1106 InitIfNecessary(); 1102 InitIfNecessary();
1107 for (net::CookieList::const_iterator iter = list.begin(); 1103 for (net::CookieList::const_iterator iter = list.begin(); iter != list.end();
1108 iter != list.end(); ++iter) { 1104 ++iter) {
1109 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter)); 1105 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
1110 net::CookieOptions options; 1106 net::CookieOptions options;
1111 options.set_include_httponly(); 1107 options.set_include_httponly();
1112 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options)) 1108 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
1113 return false; 1109 return false;
1114 } 1110 }
1115 return true; 1111 return true;
1116 } 1112 }
1117 1113
1118 CookieList CookieMonster::GetAllCookies() { 1114 CookieList CookieMonster::GetAllCookies() {
1119 base::AutoLock autolock(lock_); 1115 base::AutoLock autolock(lock_);
1120 1116
1121 // This function is being called to scrape the cookie list for management UI 1117 // This function is being called to scrape the cookie list for management UI
1122 // or similar. We shouldn't show expired cookies in this list since it will 1118 // or similar. We shouldn't show expired cookies in this list since it will
1123 // just be confusing to users, and this function is called rarely enough (and 1119 // just be confusing to users, and this function is called rarely enough (and
1124 // is already slow enough) that it's OK to take the time to garbage collect 1120 // is already slow enough) that it's OK to take the time to garbage collect
1125 // the expired cookies now. 1121 // the expired cookies now.
1126 // 1122 //
1127 // Note that this does not prune cookies to be below our limits (if we've 1123 // Note that this does not prune cookies to be below our limits (if we've
1128 // exceeded them) the way that calling GarbageCollect() would. 1124 // exceeded them) the way that calling GarbageCollect() would.
1129 GarbageCollectExpired(Time::Now(), 1125 GarbageCollectExpired(
1130 CookieMapItPair(cookies_.begin(), cookies_.end()), 1126 Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL);
1131 NULL);
1132 1127
1133 // Copy the CanonicalCookie pointers from the map so that we can use the same 1128 // Copy the CanonicalCookie pointers from the map so that we can use the same
1134 // sorter as elsewhere, then copy the result out. 1129 // sorter as elsewhere, then copy the result out.
1135 std::vector<CanonicalCookie*> cookie_ptrs; 1130 std::vector<CanonicalCookie*> cookie_ptrs;
1136 cookie_ptrs.reserve(cookies_.size()); 1131 cookie_ptrs.reserve(cookies_.size());
1137 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it) 1132 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1138 cookie_ptrs.push_back(it->second); 1133 cookie_ptrs.push_back(it->second);
1139 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); 1134 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1140 1135
1141 CookieList cookie_list; 1136 CookieList cookie_list;
1142 cookie_list.reserve(cookie_ptrs.size()); 1137 cookie_list.reserve(cookie_ptrs.size());
1143 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); 1138 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1144 it != cookie_ptrs.end(); ++it) 1139 it != cookie_ptrs.end();
1140 ++it)
1145 cookie_list.push_back(**it); 1141 cookie_list.push_back(**it);
1146 1142
1147 return cookie_list; 1143 return cookie_list;
1148 } 1144 }
1149 1145
1150 CookieList CookieMonster::GetAllCookiesForURLWithOptions( 1146 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1151 const GURL& url, 1147 const GURL& url,
1152 const CookieOptions& options) { 1148 const CookieOptions& options) {
1153 base::AutoLock autolock(lock_); 1149 base::AutoLock autolock(lock_);
1154 1150
1155 std::vector<CanonicalCookie*> cookie_ptrs; 1151 std::vector<CanonicalCookie*> cookie_ptrs;
1156 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); 1152 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1157 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); 1153 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1158 1154
1159 CookieList cookies; 1155 CookieList cookies;
1160 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); 1156 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1161 it != cookie_ptrs.end(); it++) 1157 it != cookie_ptrs.end();
1158 it++)
1162 cookies.push_back(**it); 1159 cookies.push_back(**it);
1163 1160
1164 return cookies; 1161 return cookies;
1165 } 1162 }
1166 1163
1167 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { 1164 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1168 CookieOptions options; 1165 CookieOptions options;
1169 options.set_include_httponly(); 1166 options.set_include_httponly();
1170 1167
1171 return GetAllCookiesForURLWithOptions(url, options); 1168 return GetAllCookiesForURLWithOptions(url, options);
1172 } 1169 }
1173 1170
1174 int CookieMonster::DeleteAll(bool sync_to_store) { 1171 int CookieMonster::DeleteAll(bool sync_to_store) {
1175 base::AutoLock autolock(lock_); 1172 base::AutoLock autolock(lock_);
1176 1173
1177 int num_deleted = 0; 1174 int num_deleted = 0;
1178 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1175 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1179 CookieMap::iterator curit = it; 1176 CookieMap::iterator curit = it;
1180 ++it; 1177 ++it;
1181 InternalDeleteCookie(curit, sync_to_store, 1178 InternalDeleteCookie(curit,
1182 sync_to_store ? DELETE_COOKIE_EXPLICIT : 1179 sync_to_store,
1183 DELETE_COOKIE_DONT_RECORD /* Destruction. */); 1180 sync_to_store
1181 ? DELETE_COOKIE_EXPLICIT
1182 : DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1184 ++num_deleted; 1183 ++num_deleted;
1185 } 1184 }
1186 1185
1187 return num_deleted; 1186 return num_deleted;
1188 } 1187 }
1189 1188
1190 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, 1189 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1191 const Time& delete_end) { 1190 const Time& delete_end) {
1192 base::AutoLock autolock(lock_); 1191 base::AutoLock autolock(lock_);
1193 1192
1194 int num_deleted = 0; 1193 int num_deleted = 0;
1195 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1194 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1196 CookieMap::iterator curit = it; 1195 CookieMap::iterator curit = it;
1197 CanonicalCookie* cc = curit->second; 1196 CanonicalCookie* cc = curit->second;
1198 ++it; 1197 ++it;
1199 1198
1200 if (cc->CreationDate() >= delete_begin && 1199 if (cc->CreationDate() >= delete_begin &&
1201 (delete_end.is_null() || cc->CreationDate() < delete_end)) { 1200 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1202 InternalDeleteCookie(curit, 1201 InternalDeleteCookie(curit,
1203 true, /*sync_to_store*/ 1202 true, /*sync_to_store*/
1204 DELETE_COOKIE_EXPLICIT); 1203 DELETE_COOKIE_EXPLICIT);
1205 ++num_deleted; 1204 ++num_deleted;
1206 } 1205 }
1207 } 1206 }
1208 1207
1209 return num_deleted; 1208 return num_deleted;
1210 } 1209 }
1211 1210
1212 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin, 1211 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1213 const Time delete_end, 1212 const Time delete_end,
(...skipping 27 matching lines...) Expand all
1241 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); 1240 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1242 } 1241 }
1243 } 1242 }
1244 return num_deleted; 1243 return num_deleted;
1245 } 1244 }
1246 1245
1247 int CookieMonster::DeleteAllForHost(const GURL& url) { 1246 int CookieMonster::DeleteAllForHost(const GURL& url) {
1248 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url); 1247 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1249 } 1248 }
1250 1249
1251
1252 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { 1250 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
1253 base::AutoLock autolock(lock_); 1251 base::AutoLock autolock(lock_);
1254 1252
1255 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); 1253 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1256 its.first != its.second; ++its.first) { 1254 its.first != its.second;
1255 ++its.first) {
1257 // The creation date acts as our unique index... 1256 // The creation date acts as our unique index...
1258 if (its.first->second->CreationDate() == cookie.CreationDate()) { 1257 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1259 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); 1258 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1260 return true; 1259 return true;
1261 } 1260 }
1262 } 1261 }
1263 return false; 1262 return false;
1264 } 1263 }
1265 1264
1266 void CookieMonster::SetCookieableSchemes(const char* schemes[], 1265 void CookieMonster::SetCookieableSchemes(const char* schemes[],
1267 size_t num_schemes) { 1266 size_t num_schemes) {
1268 base::AutoLock autolock(lock_); 1267 base::AutoLock autolock(lock_);
1269 1268
1270 // Cookieable Schemes must be set before first use of function. 1269 // Cookieable Schemes must be set before first use of function.
1271 DCHECK(!initialized_); 1270 DCHECK(!initialized_);
1272 1271
1273 cookieable_schemes_.clear(); 1272 cookieable_schemes_.clear();
1274 cookieable_schemes_.insert(cookieable_schemes_.end(), 1273 cookieable_schemes_.insert(
1275 schemes, schemes + num_schemes); 1274 cookieable_schemes_.end(), schemes, schemes + num_schemes);
1276 } 1275 }
1277 1276
1278 void CookieMonster::SetEnableFileScheme(bool accept) { 1277 void CookieMonster::SetEnableFileScheme(bool accept) {
1279 // This assumes "file" is always at the end of the array. See the comment 1278 // This assumes "file" is always at the end of the array. See the comment
1280 // above kDefaultCookieableSchemes. 1279 // above kDefaultCookieableSchemes.
1281 int num_schemes = accept ? kDefaultCookieableSchemesCount : 1280 int num_schemes = accept ? kDefaultCookieableSchemesCount
1282 kDefaultCookieableSchemesCount - 1; 1281 : kDefaultCookieableSchemesCount - 1;
1283 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); 1282 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1284 } 1283 }
1285 1284
1286 void CookieMonster::SetKeepExpiredCookies() { 1285 void CookieMonster::SetKeepExpiredCookies() {
1287 keep_expired_cookies_ = true; 1286 keep_expired_cookies_ = true;
1288 } 1287 }
1289 1288
1290 void CookieMonster::FlushStore(const base::Closure& callback) { 1289 void CookieMonster::FlushStore(const base::Closure& callback) {
1291 base::AutoLock autolock(lock_); 1290 base::AutoLock autolock(lock_);
1292 if (initialized_ && store_.get()) 1291 if (initialized_ && store_.get())
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1337 return; 1336 return;
1338 1337
1339 CookieOptions options; 1338 CookieOptions options;
1340 options.set_include_httponly(); 1339 options.set_include_httponly();
1341 // Get the cookies for this host and its domain(s). 1340 // Get the cookies for this host and its domain(s).
1342 std::vector<CanonicalCookie*> cookies; 1341 std::vector<CanonicalCookie*> cookies;
1343 FindCookiesForHostAndDomain(url, options, true, &cookies); 1342 FindCookiesForHostAndDomain(url, options, true, &cookies);
1344 std::set<CanonicalCookie*> matching_cookies; 1343 std::set<CanonicalCookie*> matching_cookies;
1345 1344
1346 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); 1345 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1347 it != cookies.end(); ++it) { 1346 it != cookies.end();
1347 ++it) {
1348 if ((*it)->Name() != cookie_name) 1348 if ((*it)->Name() != cookie_name)
1349 continue; 1349 continue;
1350 if (url.path().find((*it)->Path())) 1350 if (url.path().find((*it)->Path()))
1351 continue; 1351 continue;
1352 matching_cookies.insert(*it); 1352 matching_cookies.insert(*it);
1353 } 1353 }
1354 1354
1355 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1355 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1356 CookieMap::iterator curit = it; 1356 CookieMap::iterator curit = it;
1357 ++it; 1357 ++it;
1358 if (matching_cookies.find(curit->second) != matching_cookies.end()) { 1358 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1359 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); 1359 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1360 } 1360 }
1361 } 1361 }
1362 } 1362 }
1363 1363
1364 int CookieMonster::DeleteSessionCookies() { 1364 int CookieMonster::DeleteSessionCookies() {
1365 base::AutoLock autolock(lock_); 1365 base::AutoLock autolock(lock_);
1366 1366
1367 int num_deleted = 0; 1367 int num_deleted = 0;
1368 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1368 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1369 CookieMap::iterator curit = it; 1369 CookieMap::iterator curit = it;
1370 CanonicalCookie* cc = curit->second; 1370 CanonicalCookie* cc = curit->second;
1371 ++it; 1371 ++it;
1372 1372
1373 if (!cc->IsPersistent()) { 1373 if (!cc->IsPersistent()) {
1374 InternalDeleteCookie(curit, 1374 InternalDeleteCookie(curit,
1375 true, /*sync_to_store*/ 1375 true, /*sync_to_store*/
1376 DELETE_COOKIE_EXPIRED); 1376 DELETE_COOKIE_EXPIRED);
1377 ++num_deleted; 1377 ++num_deleted;
1378 } 1378 }
1379 } 1379 }
1380 1380
1381 return num_deleted; 1381 return num_deleted;
1382 } 1382 }
1383 1383
1384 bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) { 1384 bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1385 base::AutoLock autolock(lock_); 1385 base::AutoLock autolock(lock_);
(...skipping 28 matching lines...) Expand all
1414 const std::string& cookie_line, 1414 const std::string& cookie_line,
1415 const base::Time& creation_time) { 1415 const base::Time& creation_time) {
1416 DCHECK(!store_.get()) << "This method is only to be used by unit-tests."; 1416 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
1417 base::AutoLock autolock(lock_); 1417 base::AutoLock autolock(lock_);
1418 1418
1419 if (!HasCookieableScheme(url)) { 1419 if (!HasCookieableScheme(url)) {
1420 return false; 1420 return false;
1421 } 1421 }
1422 1422
1423 InitIfNecessary(); 1423 InitIfNecessary();
1424 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, 1424 return SetCookieWithCreationTimeAndOptions(
1425 CookieOptions()); 1425 url, cookie_line, creation_time, CookieOptions());
1426 } 1426 }
1427 1427
1428 void CookieMonster::InitStore() { 1428 void CookieMonster::InitStore() {
1429 DCHECK(store_.get()) << "Store must exist to initialize"; 1429 DCHECK(store_.get()) << "Store must exist to initialize";
1430 1430
1431 // We bind in the current time so that we can report the wall-clock time for 1431 // We bind in the current time so that we can report the wall-clock time for
1432 // loading cookies. 1432 // loading cookies.
1433 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now())); 1433 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1434 } 1434 }
1435 1435
(...skipping 11 matching lines...) Expand all
1447 // This function does its own separate locking. 1447 // This function does its own separate locking.
1448 StoreLoadedCookies(cookies); 1448 StoreLoadedCookies(cookies);
1449 1449
1450 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key; 1450 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key;
1451 1451
1452 // We need to do this repeatedly until no more tasks were added to the queue 1452 // We need to do this repeatedly until no more tasks were added to the queue
1453 // during the period where we release the lock. 1453 // during the period where we release the lock.
1454 while (true) { 1454 while (true) {
1455 { 1455 {
1456 base::AutoLock autolock(lock_); 1456 base::AutoLock autolock(lock_);
1457 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > 1457 std::map<std::string,
1458 ::iterator it = tasks_pending_for_key_.find(key); 1458 std::deque<scoped_refptr<CookieMonsterTask> > >::iterator it =
1459 tasks_pending_for_key_.find(key);
1459 if (it == tasks_pending_for_key_.end()) { 1460 if (it == tasks_pending_for_key_.end()) {
1460 keys_loaded_.insert(key); 1461 keys_loaded_.insert(key);
1461 return; 1462 return;
1462 } 1463 }
1463 if (it->second.empty()) { 1464 if (it->second.empty()) {
1464 keys_loaded_.insert(key); 1465 keys_loaded_.insert(key);
1465 tasks_pending_for_key_.erase(it); 1466 tasks_pending_for_key_.erase(it);
1466 return; 1467 return;
1467 } 1468 }
1468 it->second.swap(tasks_pending_for_key); 1469 it->second.swap(tasks_pending_for_key);
(...skipping 10 matching lines...) Expand all
1479 void CookieMonster::StoreLoadedCookies( 1480 void CookieMonster::StoreLoadedCookies(
1480 const std::vector<CanonicalCookie*>& cookies) { 1481 const std::vector<CanonicalCookie*>& cookies) {
1481 // Initialize the store and sync in any saved persistent cookies. We don't 1482 // Initialize the store and sync in any saved persistent cookies. We don't
1482 // care if it's expired, insert it so it can be garbage collected, removed, 1483 // care if it's expired, insert it so it can be garbage collected, removed,
1483 // and sync'd. 1484 // and sync'd.
1484 base::AutoLock autolock(lock_); 1485 base::AutoLock autolock(lock_);
1485 1486
1486 CookieItVector cookies_with_control_chars; 1487 CookieItVector cookies_with_control_chars;
1487 1488
1488 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); 1489 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1489 it != cookies.end(); ++it) { 1490 it != cookies.end();
1491 ++it) {
1490 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); 1492 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1491 1493
1492 if (creation_times_.insert(cookie_creation_time).second) { 1494 if (creation_times_.insert(cookie_creation_time).second) {
1493 CookieMap::iterator inserted = 1495 CookieMap::iterator inserted =
1494 InternalInsertCookie(GetKey((*it)->Domain()), *it, false); 1496 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
1495 const Time cookie_access_time((*it)->LastAccessDate()); 1497 const Time cookie_access_time((*it)->LastAccessDate());
1496 if (earliest_access_time_.is_null() || 1498 if (earliest_access_time_.is_null() ||
1497 cookie_access_time < earliest_access_time_) 1499 cookie_access_time < earliest_access_time_)
1498 earliest_access_time_ = cookie_access_time; 1500 earliest_access_time_ = cookie_access_time;
1499 1501
1500 if (ContainsControlCharacter((*it)->Name()) || 1502 if (ContainsControlCharacter((*it)->Name()) ||
1501 ContainsControlCharacter((*it)->Value())) { 1503 ContainsControlCharacter((*it)->Value())) {
1502 cookies_with_control_chars.push_back(inserted); 1504 cookies_with_control_chars.push_back(inserted);
1503 } 1505 }
1504 } else { 1506 } else {
1505 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " 1507 LOG(ERROR) << base::StringPrintf(
1506 "times in backing store: " 1508 "Found cookies with duplicate creation "
1507 "{name='%s', domain='%s', path='%s'}", 1509 "times in backing store: "
1508 (*it)->Name().c_str(), 1510 "{name='%s', domain='%s', path='%s'}",
1509 (*it)->Domain().c_str(), 1511 (*it)->Name().c_str(),
1510 (*it)->Path().c_str()); 1512 (*it)->Domain().c_str(),
1513 (*it)->Path().c_str());
1511 // We've been given ownership of the cookie and are throwing it 1514 // We've been given ownership of the cookie and are throwing it
1512 // away; reclaim the space. 1515 // away; reclaim the space.
1513 delete (*it); 1516 delete (*it);
1514 } 1517 }
1515 } 1518 }
1516 1519
1517 // Any cookies that contain control characters that we have loaded from the 1520 // Any cookies that contain control characters that we have loaded from the
1518 // persistent store should be deleted. See http://crbug.com/238041. 1521 // persistent store should be deleted. See http://crbug.com/238041.
1519 for (CookieItVector::iterator it = cookies_with_control_chars.begin(); 1522 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1520 it != cookies_with_control_chars.end();) { 1523 it != cookies_with_control_chars.end();) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1568 // Ensure no equivalent cookies for this host. 1571 // Ensure no equivalent cookies for this host.
1569 num_duplicates_trimmed += 1572 num_duplicates_trimmed +=
1570 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end); 1573 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1571 } 1574 }
1572 1575
1573 // Record how many duplicates were found in the database. 1576 // Record how many duplicates were found in the database.
1574 // See InitializeHistograms() for details. 1577 // See InitializeHistograms() for details.
1575 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed); 1578 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed);
1576 } 1579 }
1577 1580
1578 int CookieMonster::TrimDuplicateCookiesForKey( 1581 int CookieMonster::TrimDuplicateCookiesForKey(const std::string& key,
1579 const std::string& key, 1582 CookieMap::iterator begin,
1580 CookieMap::iterator begin, 1583 CookieMap::iterator end) {
1581 CookieMap::iterator end) {
1582 lock_.AssertAcquired(); 1584 lock_.AssertAcquired();
1583 1585
1584 // Set of cookies ordered by creation time. 1586 // Set of cookies ordered by creation time.
1585 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet; 1587 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1586 1588
1587 // Helper map we populate to find the duplicates. 1589 // Helper map we populate to find the duplicates.
1588 typedef std::map<CookieSignature, CookieSet> EquivalenceMap; 1590 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1589 EquivalenceMap equivalent_cookies; 1591 EquivalenceMap equivalent_cookies;
1590 1592
1591 // The number of duplicate cookies that have been found. 1593 // The number of duplicate cookies that have been found.
1592 int num_duplicates = 0; 1594 int num_duplicates = 0;
1593 1595
1594 // Iterate through all of the cookies in our range, and insert them into 1596 // Iterate through all of the cookies in our range, and insert them into
1595 // the equivalence map. 1597 // the equivalence map.
1596 for (CookieMap::iterator it = begin; it != end; ++it) { 1598 for (CookieMap::iterator it = begin; it != end; ++it) {
1597 DCHECK_EQ(key, it->first); 1599 DCHECK_EQ(key, it->first);
1598 CanonicalCookie* cookie = it->second; 1600 CanonicalCookie* cookie = it->second;
1599 1601
1600 CookieSignature signature(cookie->Name(), cookie->Domain(), 1602 CookieSignature signature(cookie->Name(), cookie->Domain(), cookie->Path());
1601 cookie->Path());
1602 CookieSet& set = equivalent_cookies[signature]; 1603 CookieSet& set = equivalent_cookies[signature];
1603 1604
1604 // We found a duplicate! 1605 // We found a duplicate!
1605 if (!set.empty()) 1606 if (!set.empty())
1606 num_duplicates++; 1607 num_duplicates++;
1607 1608
1608 // We save the iterator into |cookies_| rather than the actual cookie 1609 // We save the iterator into |cookies_| rather than the actual cookie
1609 // pointer, since we may need to delete it later. 1610 // pointer, since we may need to delete it later.
1610 bool insert_success = set.insert(it).second; 1611 bool insert_success = set.insert(it).second;
1611 DCHECK(insert_success) << 1612 DCHECK(insert_success)
1612 "Duplicate creation times found in duplicate cookie name scan."; 1613 << "Duplicate creation times found in duplicate cookie name scan.";
1613 } 1614 }
1614 1615
1615 // If there were no duplicates, we are done! 1616 // If there were no duplicates, we are done!
1616 if (num_duplicates == 0) 1617 if (num_duplicates == 0)
1617 return 0; 1618 return 0;
1618 1619
1619 // Make sure we find everything below that we did above. 1620 // Make sure we find everything below that we did above.
1620 int num_duplicates_found = 0; 1621 int num_duplicates_found = 0;
1621 1622
1622 // Otherwise, delete all the duplicate cookies, both from our in-memory store 1623 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1623 // and from the backing store. 1624 // and from the backing store.
1624 for (EquivalenceMap::iterator it = equivalent_cookies.begin(); 1625 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1625 it != equivalent_cookies.end(); 1626 it != equivalent_cookies.end();
1626 ++it) { 1627 ++it) {
1627 const CookieSignature& signature = it->first; 1628 const CookieSignature& signature = it->first;
1628 CookieSet& dupes = it->second; 1629 CookieSet& dupes = it->second;
1629 1630
1630 if (dupes.size() <= 1) 1631 if (dupes.size() <= 1)
1631 continue; // This cookiename/path has no duplicates. 1632 continue; // This cookiename/path has no duplicates.
1632 num_duplicates_found += dupes.size() - 1; 1633 num_duplicates_found += dupes.size() - 1;
1633 1634
1634 // Since |dups| is sorted by creation time (descending), the first cookie 1635 // Since |dups| is sorted by creation time (descending), the first cookie
1635 // is the most recent one, so we will keep it. The rest are duplicates. 1636 // is the most recent one, so we will keep it. The rest are duplicates.
1636 dupes.erase(dupes.begin()); 1637 dupes.erase(dupes.begin());
1637 1638
1638 LOG(ERROR) << base::StringPrintf( 1639 LOG(ERROR) << base::StringPrintf(
1639 "Found %d duplicate cookies for host='%s', " 1640 "Found %d duplicate cookies for host='%s', "
1640 "with {name='%s', domain='%s', path='%s'}", 1641 "with {name='%s', domain='%s', path='%s'}",
1641 static_cast<int>(dupes.size()), 1642 static_cast<int>(dupes.size()),
1642 key.c_str(), 1643 key.c_str(),
1643 signature.name.c_str(), 1644 signature.name.c_str(),
1644 signature.domain.c_str(), 1645 signature.domain.c_str(),
1645 signature.path.c_str()); 1646 signature.path.c_str());
1646 1647
1647 // Remove all the cookies identified by |dupes|. It is valid to delete our 1648 // Remove all the cookies identified by |dupes|. It is valid to delete our
1648 // list of iterators one at a time, since |cookies_| is a multimap (they 1649 // list of iterators one at a time, since |cookies_| is a multimap (they
1649 // don't invalidate existing iterators following deletion). 1650 // don't invalidate existing iterators following deletion).
1650 for (CookieSet::iterator dupes_it = dupes.begin(); 1651 for (CookieSet::iterator dupes_it = dupes.begin(); dupes_it != dupes.end();
1651 dupes_it != dupes.end();
1652 ++dupes_it) { 1652 ++dupes_it) {
1653 InternalDeleteCookie(*dupes_it, true, 1653 InternalDeleteCookie(
1654 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); 1654 *dupes_it, true, DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1655 } 1655 }
1656 } 1656 }
1657 DCHECK_EQ(num_duplicates, num_duplicates_found); 1657 DCHECK_EQ(num_duplicates, num_duplicates_found);
1658 1658
1659 return num_duplicates; 1659 return num_duplicates;
1660 } 1660 }
1661 1661
1662 // Note: file must be the last scheme. 1662 // Note: file must be the last scheme.
1663 const char* CookieMonster::kDefaultCookieableSchemes[] = 1663 const char* CookieMonster::kDefaultCookieableSchemes[] = {"http", "https", "ws",
1664 { "http", "https", "ws", "wss", "file" }; 1664 "wss", "file"};
1665 const int CookieMonster::kDefaultCookieableSchemesCount = 1665 const int CookieMonster::kDefaultCookieableSchemesCount =
1666 arraysize(kDefaultCookieableSchemes); 1666 arraysize(kDefaultCookieableSchemes);
1667 1667
1668 void CookieMonster::SetDefaultCookieableSchemes() { 1668 void CookieMonster::SetDefaultCookieableSchemes() {
1669 // Always disable file scheme unless SetEnableFileScheme(true) is called. 1669 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1670 SetCookieableSchemes(kDefaultCookieableSchemes, 1670 SetCookieableSchemes(kDefaultCookieableSchemes,
1671 kDefaultCookieableSchemesCount - 1); 1671 kDefaultCookieableSchemesCount - 1);
1672 } 1672 }
1673 1673
1674 void CookieMonster::FindCookiesForHostAndDomain( 1674 void CookieMonster::FindCookiesForHostAndDomain(
1675 const GURL& url, 1675 const GURL& url,
1676 const CookieOptions& options, 1676 const CookieOptions& options,
1677 bool update_access_time, 1677 bool update_access_time,
1678 std::vector<CanonicalCookie*>* cookies) { 1678 std::vector<CanonicalCookie*>* cookies) {
1679 lock_.AssertAcquired(); 1679 lock_.AssertAcquired();
1680 1680
1681 const Time current_time(CurrentTime()); 1681 const Time current_time(CurrentTime());
1682 1682
1683 // Probe to save statistics relatively frequently. We do it here rather 1683 // Probe to save statistics relatively frequently. We do it here rather
1684 // than in the set path as many websites won't set cookies, and we 1684 // than in the set path as many websites won't set cookies, and we
1685 // want to collect statistics whenever the browser's being used. 1685 // want to collect statistics whenever the browser's being used.
1686 RecordPeriodicStats(current_time); 1686 RecordPeriodicStats(current_time);
1687 1687
1688 // Can just dispatch to FindCookiesForKey 1688 // Can just dispatch to FindCookiesForKey
1689 const std::string key(GetKey(url.host())); 1689 const std::string key(GetKey(url.host()));
1690 FindCookiesForKey(key, url, options, current_time, 1690 FindCookiesForKey(
1691 update_access_time, cookies); 1691 key, url, options, current_time, update_access_time, cookies);
1692 } 1692 }
1693 1693
1694 void CookieMonster::FindCookiesForKey(const std::string& key, 1694 void CookieMonster::FindCookiesForKey(const std::string& key,
1695 const GURL& url, 1695 const GURL& url,
1696 const CookieOptions& options, 1696 const CookieOptions& options,
1697 const Time& current, 1697 const Time& current,
1698 bool update_access_time, 1698 bool update_access_time,
1699 std::vector<CanonicalCookie*>* cookies) { 1699 std::vector<CanonicalCookie*>* cookies) {
1700 lock_.AssertAcquired(); 1700 lock_.AssertAcquired();
1701 1701
1702 for (CookieMapItPair its = cookies_.equal_range(key); 1702 for (CookieMapItPair its = cookies_.equal_range(key);
1703 its.first != its.second; ) { 1703 its.first != its.second;) {
1704 CookieMap::iterator curit = its.first; 1704 CookieMap::iterator curit = its.first;
1705 CanonicalCookie* cc = curit->second; 1705 CanonicalCookie* cc = curit->second;
1706 ++its.first; 1706 ++its.first;
1707 1707
1708 // If the cookie is expired, delete it. 1708 // If the cookie is expired, delete it.
1709 if (cc->IsExpired(current) && !keep_expired_cookies_) { 1709 if (cc->IsExpired(current) && !keep_expired_cookies_) {
1710 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); 1710 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1711 continue; 1711 continue;
1712 } 1712 }
1713 1713
(...skipping 14 matching lines...) Expand all
1728 1728
1729 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key, 1729 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1730 const CanonicalCookie& ecc, 1730 const CanonicalCookie& ecc,
1731 bool skip_httponly, 1731 bool skip_httponly,
1732 bool already_expired) { 1732 bool already_expired) {
1733 lock_.AssertAcquired(); 1733 lock_.AssertAcquired();
1734 1734
1735 bool found_equivalent_cookie = false; 1735 bool found_equivalent_cookie = false;
1736 bool skipped_httponly = false; 1736 bool skipped_httponly = false;
1737 for (CookieMapItPair its = cookies_.equal_range(key); 1737 for (CookieMapItPair its = cookies_.equal_range(key);
1738 its.first != its.second; ) { 1738 its.first != its.second;) {
1739 CookieMap::iterator curit = its.first; 1739 CookieMap::iterator curit = its.first;
1740 CanonicalCookie* cc = curit->second; 1740 CanonicalCookie* cc = curit->second;
1741 ++its.first; 1741 ++its.first;
1742 1742
1743 if (ecc.IsEquivalent(*cc)) { 1743 if (ecc.IsEquivalent(*cc)) {
1744 // We should never have more than one equivalent cookie, since they should 1744 // We should never have more than one equivalent cookie, since they should
1745 // overwrite each other. 1745 // overwrite each other.
1746 CHECK(!found_equivalent_cookie) << 1746 CHECK(!found_equivalent_cookie)
1747 "Duplicate equivalent cookies found, cookie store is corrupted."; 1747 << "Duplicate equivalent cookies found, cookie store is corrupted.";
1748 if (skip_httponly && cc->IsHttpOnly()) { 1748 if (skip_httponly && cc->IsHttpOnly()) {
1749 skipped_httponly = true; 1749 skipped_httponly = true;
1750 } else { 1750 } else {
1751 InternalDeleteCookie(curit, true, already_expired ? 1751 InternalDeleteCookie(curit,
1752 DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE); 1752 true,
1753 already_expired ? DELETE_COOKIE_EXPIRED_OVERWRITE
1754 : DELETE_COOKIE_OVERWRITE);
1753 } 1755 }
1754 found_equivalent_cookie = true; 1756 found_equivalent_cookie = true;
1755 } 1757 }
1756 } 1758 }
1757 return skipped_httponly; 1759 return skipped_httponly;
1758 } 1760 }
1759 1761
1760 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie( 1762 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1761 const std::string& key, 1763 const std::string& key,
1762 CanonicalCookie* cc, 1764 CanonicalCookie* cc,
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1799 return false; 1801 return false;
1800 } 1802 }
1801 return SetCanonicalCookie(&cc, creation_time, options); 1803 return SetCanonicalCookie(&cc, creation_time, options);
1802 } 1804 }
1803 1805
1804 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc, 1806 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
1805 const Time& creation_time, 1807 const Time& creation_time,
1806 const CookieOptions& options) { 1808 const CookieOptions& options) {
1807 const std::string key(GetKey((*cc)->Domain())); 1809 const std::string key(GetKey((*cc)->Domain()));
1808 bool already_expired = (*cc)->IsExpired(creation_time); 1810 bool already_expired = (*cc)->IsExpired(creation_time);
1809 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(), 1811 if (DeleteAnyEquivalentCookie(
1810 already_expired)) { 1812 key, **cc, options.exclude_httponly(), already_expired)) {
1811 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie"; 1813 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
1812 return false; 1814 return false;
1813 } 1815 }
1814 1816
1815 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: " 1817 VLOG(kVlogSetCookies) << "SetCookie() key: " << key
1816 << (*cc)->DebugString(); 1818 << " cc: " << (*cc)->DebugString();
1817 1819
1818 // Realize that we might be setting an expired cookie, and the only point 1820 // Realize that we might be setting an expired cookie, and the only point
1819 // was to delete the cookie which we've already done. 1821 // was to delete the cookie which we've already done.
1820 if (!already_expired || keep_expired_cookies_) { 1822 if (!already_expired || keep_expired_cookies_) {
1821 // See InitializeHistograms() for details. 1823 // See InitializeHistograms() for details.
1822 if ((*cc)->IsPersistent()) { 1824 if ((*cc)->IsPersistent()) {
1823 histogram_expiration_duration_minutes_->Add( 1825 histogram_expiration_duration_minutes_->Add(
1824 ((*cc)->ExpiryDate() - creation_time).InMinutes()); 1826 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1825 } 1827 }
1826 1828
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1887 1889
1888 if (mapping.notify) 1890 if (mapping.notify)
1889 delegate_->OnCookieChanged(*cc, true, mapping.cause); 1891 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1890 } 1892 }
1891 cookies_.erase(it); 1893 cookies_.erase(it);
1892 delete cc; 1894 delete cc;
1893 } 1895 }
1894 1896
1895 // Domain expiry behavior is unchanged by key/expiry scheme (the 1897 // Domain expiry behavior is unchanged by key/expiry scheme (the
1896 // meaning of the key is different, but that's not visible to this routine). 1898 // meaning of the key is different, but that's not visible to this routine).
1897 int CookieMonster::GarbageCollect(const Time& current, 1899 int CookieMonster::GarbageCollect(const Time& current, const std::string& key) {
1898 const std::string& key) {
1899 lock_.AssertAcquired(); 1900 lock_.AssertAcquired();
1900 1901
1901 int num_deleted = 0; 1902 int num_deleted = 0;
1902 Time safe_date( 1903 Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
1903 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
1904 1904
1905 // Collect garbage for this key, minding cookie priorities. 1905 // Collect garbage for this key, minding cookie priorities.
1906 if (cookies_.count(key) > kDomainMaxCookies) { 1906 if (cookies_.count(key) > kDomainMaxCookies) {
1907 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key; 1907 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
1908 1908
1909 CookieItVector cookie_its; 1909 CookieItVector cookie_its;
1910 num_deleted += GarbageCollectExpired( 1910 num_deleted +=
1911 current, cookies_.equal_range(key), &cookie_its); 1911 GarbageCollectExpired(current, cookies_.equal_range(key), &cookie_its);
1912 if (cookie_its.size() > kDomainMaxCookies) { 1912 if (cookie_its.size() > kDomainMaxCookies) {
1913 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; 1913 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1914 size_t purge_goal = 1914 size_t purge_goal =
1915 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies); 1915 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1916 DCHECK(purge_goal > kDomainPurgeCookies); 1916 DCHECK(purge_goal > kDomainPurgeCookies);
1917 1917
1918 // Boundary iterators into |cookie_its| for different priorities. 1918 // Boundary iterators into |cookie_its| for different priorities.
1919 CookieItVector::iterator it_bdd[4]; 1919 CookieItVector::iterator it_bdd[4];
1920 // Intialize |it_bdd| while sorting |cookie_its| by priorities. 1920 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1921 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries. 1921 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1922 it_bdd[0] = cookie_its.begin(); 1922 it_bdd[0] = cookie_its.begin();
1923 it_bdd[3] = cookie_its.end(); 1923 it_bdd[3] = cookie_its.end();
1924 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3], 1924 it_bdd[1] =
1925 COOKIE_PRIORITY_LOW); 1925 PartitionCookieByPriority(it_bdd[0], it_bdd[3], COOKIE_PRIORITY_LOW);
1926 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3], 1926 it_bdd[2] = PartitionCookieByPriority(
1927 COOKIE_PRIORITY_MEDIUM); 1927 it_bdd[1], it_bdd[3], COOKIE_PRIORITY_MEDIUM);
1928 size_t quota[3] = { 1928 size_t quota[3] = {kDomainCookiesQuotaLow, kDomainCookiesQuotaMedium,
1929 kDomainCookiesQuotaLow, 1929 kDomainCookiesQuotaHigh};
1930 kDomainCookiesQuotaMedium,
1931 kDomainCookiesQuotaHigh
1932 };
1933 1930
1934 // Purge domain cookies in 3 rounds. 1931 // Purge domain cookies in 3 rounds.
1935 // Round 1: consider low-priority cookies only: evict least-recently 1932 // Round 1: consider low-priority cookies only: evict least-recently
1936 // accessed, while protecting quota[0] of these from deletion. 1933 // accessed, while protecting quota[0] of these from deletion.
1937 // Round 2: consider {low, medium}-priority cookies, evict least-recently 1934 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1938 // accessed, while protecting quota[0] + quota[1]. 1935 // accessed, while protecting quota[0] + quota[1].
1939 // Round 3: consider all cookies, evict least-recently accessed. 1936 // Round 3: consider all cookies, evict least-recently accessed.
1940 size_t accumulated_quota = 0; 1937 size_t accumulated_quota = 0;
1941 CookieItVector::iterator it_purge_begin = it_bdd[0]; 1938 CookieItVector::iterator it_purge_begin = it_bdd[0];
1942 for (int i = 0; i < 3 && purge_goal > 0; ++i) { 1939 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1943 accumulated_quota += quota[i]; 1940 accumulated_quota += quota[i];
1944 1941
1945 size_t num_considered = it_bdd[i + 1] - it_purge_begin; 1942 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1946 if (num_considered <= accumulated_quota) 1943 if (num_considered <= accumulated_quota)
1947 continue; 1944 continue;
1948 1945
1949 // Number of cookies that will be purged in this round. 1946 // Number of cookies that will be purged in this round.
1950 size_t round_goal = 1947 size_t round_goal =
1951 std::min(purge_goal, num_considered - accumulated_quota); 1948 std::min(purge_goal, num_considered - accumulated_quota);
1952 purge_goal -= round_goal; 1949 purge_goal -= round_goal;
1953 1950
1954 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal); 1951 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1955 // Cookies accessed on or after |safe_date| would have been safe from 1952 // Cookies accessed on or after |safe_date| would have been safe from
1956 // global purge, and we want to keep track of this. 1953 // global purge, and we want to keep track of this.
1957 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal; 1954 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1958 CookieItVector::iterator it_purge_middle = 1955 CookieItVector::iterator it_purge_middle =
1959 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date); 1956 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1960 // Delete cookies accessed before |safe_date|. 1957 // Delete cookies accessed before |safe_date|.
1961 num_deleted += GarbageCollectDeleteRange( 1958 num_deleted +=
1962 current, 1959 GarbageCollectDeleteRange(current,
1963 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, 1960 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1964 it_purge_begin, 1961 it_purge_begin,
1965 it_purge_middle); 1962 it_purge_middle);
1966 // Delete cookies accessed on or after |safe_date|. 1963 // Delete cookies accessed on or after |safe_date|.
1967 num_deleted += GarbageCollectDeleteRange( 1964 num_deleted +=
1968 current, 1965 GarbageCollectDeleteRange(current,
1969 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, 1966 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1970 it_purge_middle, 1967 it_purge_middle,
1971 it_purge_end); 1968 it_purge_end);
1972 it_purge_begin = it_purge_end; 1969 it_purge_begin = it_purge_end;
1973 } 1970 }
1974 DCHECK_EQ(0U, purge_goal); 1971 DCHECK_EQ(0U, purge_goal);
1975 } 1972 }
1976 } 1973 }
1977 1974
1978 // Collect garbage for everything. With firefox style we want to preserve 1975 // Collect garbage for everything. With firefox style we want to preserve
1979 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. 1976 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
1980 if (cookies_.size() > kMaxCookies && 1977 if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) {
1981 earliest_access_time_ < safe_date) {
1982 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything"; 1978 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
1983 CookieItVector cookie_its; 1979 CookieItVector cookie_its;
1984 num_deleted += GarbageCollectExpired( 1980 num_deleted +=
1985 current, CookieMapItPair(cookies_.begin(), cookies_.end()), 1981 GarbageCollectExpired(current,
1986 &cookie_its); 1982 CookieMapItPair(cookies_.begin(), cookies_.end()),
1983 &cookie_its);
1987 if (cookie_its.size() > kMaxCookies) { 1984 if (cookie_its.size() > kMaxCookies) {
1988 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything."; 1985 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
1989 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies); 1986 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
1990 DCHECK(purge_goal > kPurgeCookies); 1987 DCHECK(purge_goal > kPurgeCookies);
1991 // Sorts up to *and including* |cookie_its[purge_goal]|, so 1988 // Sorts up to *and including* |cookie_its[purge_goal]|, so
1992 // |earliest_access_time| will be properly assigned even if 1989 // |earliest_access_time| will be properly assigned even if
1993 // |global_purge_it| == |cookie_its.begin() + purge_goal|. 1990 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
1994 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(), 1991 SortLeastRecentlyAccessed(
1995 purge_goal); 1992 cookie_its.begin(), cookie_its.end(), purge_goal);
1996 // Find boundary to cookies older than safe_date. 1993 // Find boundary to cookies older than safe_date.
1997 CookieItVector::iterator global_purge_it = 1994 CookieItVector::iterator global_purge_it = LowerBoundAccessDate(
1998 LowerBoundAccessDate(cookie_its.begin(), 1995 cookie_its.begin(), cookie_its.begin() + purge_goal, safe_date);
1999 cookie_its.begin() + purge_goal,
2000 safe_date);
2001 // Only delete the old cookies. 1996 // Only delete the old cookies.
2002 num_deleted += GarbageCollectDeleteRange( 1997 num_deleted += GarbageCollectDeleteRange(current,
2003 current, 1998 DELETE_COOKIE_EVICTED_GLOBAL,
2004 DELETE_COOKIE_EVICTED_GLOBAL, 1999 cookie_its.begin(),
2005 cookie_its.begin(), 2000 global_purge_it);
2006 global_purge_it);
2007 // Set access day to the oldest cookie that wasn't deleted. 2001 // Set access day to the oldest cookie that wasn't deleted.
2008 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate(); 2002 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
2009 } 2003 }
2010 } 2004 }
2011 2005
2012 return num_deleted; 2006 return num_deleted;
2013 } 2007 }
2014 2008
2015 int CookieMonster::GarbageCollectExpired( 2009 int CookieMonster::GarbageCollectExpired(const Time& current,
2016 const Time& current, 2010 const CookieMapItPair& itpair,
2017 const CookieMapItPair& itpair, 2011 CookieItVector* cookie_its) {
2018 CookieItVector* cookie_its) {
2019 if (keep_expired_cookies_) 2012 if (keep_expired_cookies_)
2020 return 0; 2013 return 0;
2021 2014
2022 lock_.AssertAcquired(); 2015 lock_.AssertAcquired();
2023 2016
2024 int num_deleted = 0; 2017 int num_deleted = 0;
2025 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { 2018 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2026 CookieMap::iterator curit = it; 2019 CookieMap::iterator curit = it;
2027 ++it; 2020 ++it;
2028 2021
2029 if (curit->second->IsExpired(current)) { 2022 if (curit->second->IsExpired(current)) {
2030 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); 2023 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
2031 ++num_deleted; 2024 ++num_deleted;
2032 } else if (cookie_its) { 2025 } else if (cookie_its) {
2033 cookie_its->push_back(curit); 2026 cookie_its->push_back(curit);
2034 } 2027 }
2035 } 2028 }
2036 2029
2037 return num_deleted; 2030 return num_deleted;
2038 } 2031 }
2039 2032
2040 int CookieMonster::GarbageCollectDeleteRange( 2033 int CookieMonster::GarbageCollectDeleteRange(const Time& current,
2041 const Time& current, 2034 DeletionCause cause,
2042 DeletionCause cause, 2035 CookieItVector::iterator it_begin,
2043 CookieItVector::iterator it_begin, 2036 CookieItVector::iterator it_end) {
2044 CookieItVector::iterator it_end) {
2045 for (CookieItVector::iterator it = it_begin; it != it_end; it++) { 2037 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2046 histogram_evicted_last_access_minutes_->Add( 2038 histogram_evicted_last_access_minutes_->Add(
2047 (current - (*it)->second->LastAccessDate()).InMinutes()); 2039 (current - (*it)->second->LastAccessDate()).InMinutes());
2048 InternalDeleteCookie((*it), true, cause); 2040 InternalDeleteCookie((*it), true, cause);
2049 } 2041 }
2050 return it_end - it_begin; 2042 return it_end - it_begin;
2051 } 2043 }
2052 2044
2053 // A wrapper around registry_controlled_domains::GetDomainAndRegistry 2045 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2054 // to make clear we're creating a key for our local map. Here and 2046 // to make clear we're creating a key for our local map. Here and
(...skipping 26 matching lines...) Expand all
2081 effective_domain = domain; 2073 effective_domain = domain;
2082 2074
2083 if (!effective_domain.empty() && effective_domain[0] == '.') 2075 if (!effective_domain.empty() && effective_domain[0] == '.')
2084 return effective_domain.substr(1); 2076 return effective_domain.substr(1);
2085 return effective_domain; 2077 return effective_domain;
2086 } 2078 }
2087 2079
2088 bool CookieMonster::IsCookieableScheme(const std::string& scheme) { 2080 bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2089 base::AutoLock autolock(lock_); 2081 base::AutoLock autolock(lock_);
2090 2082
2091 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(), 2083 return std::find(cookieable_schemes_.begin(),
2084 cookieable_schemes_.end(),
2092 scheme) != cookieable_schemes_.end(); 2085 scheme) != cookieable_schemes_.end();
2093 } 2086 }
2094 2087
2095 bool CookieMonster::HasCookieableScheme(const GURL& url) { 2088 bool CookieMonster::HasCookieableScheme(const GURL& url) {
2096 lock_.AssertAcquired(); 2089 lock_.AssertAcquired();
2097 2090
2098 // Make sure the request is on a cookie-able url scheme. 2091 // Make sure the request is on a cookie-able url scheme.
2099 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { 2092 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2100 // We matched a scheme. 2093 // We matched a scheme.
2101 if (url.SchemeIs(cookieable_schemes_[i].c_str())) { 2094 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2102 // We've matched a supported scheme. 2095 // We've matched a supported scheme.
2103 return true; 2096 return true;
2104 } 2097 }
2105 } 2098 }
2106 2099
2107 // The scheme didn't match any in our whitelist. 2100 // The scheme didn't match any in our whitelist.
2108 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: " 2101 VLOG(kVlogPerCookieMonster)
2109 << url.scheme(); 2102 << "WARNING: Unsupported cookie scheme: " << url.scheme();
2110 return false; 2103 return false;
2111 } 2104 }
2112 2105
2113 // Test to see if stats should be recorded, and record them if so. 2106 // Test to see if stats should be recorded, and record them if so.
2114 // The goal here is to get sampling for the average browser-hour of 2107 // The goal here is to get sampling for the average browser-hour of
2115 // activity. We won't take samples when the web isn't being surfed, 2108 // activity. We won't take samples when the web isn't being surfed,
2116 // and when the web is being surfed, we'll take samples about every 2109 // and when the web is being surfed, we'll take samples about every
2117 // kRecordStatisticsIntervalSeconds. 2110 // kRecordStatisticsIntervalSeconds.
2118 // last_statistic_record_time_ is initialized to Now() rather than null 2111 // last_statistic_record_time_ is initialized to Now() rather than null
2119 // in the constructor so that we won't take statistics right after 2112 // in the constructor so that we won't take statistics right after
2120 // startup, to avoid bias from browsers that are started but not used. 2113 // startup, to avoid bias from browsers that are started but not used.
2121 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) { 2114 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2122 const base::TimeDelta kRecordStatisticsIntervalTime( 2115 const base::TimeDelta kRecordStatisticsIntervalTime(
2123 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds)); 2116 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2124 2117
2125 // If we've taken statistics recently, return. 2118 // If we've taken statistics recently, return.
2126 if (current_time - last_statistic_record_time_ <= 2119 if (current_time - last_statistic_record_time_ <=
2127 kRecordStatisticsIntervalTime) { 2120 kRecordStatisticsIntervalTime) {
2128 return; 2121 return;
2129 } 2122 }
2130 2123
2131 // See InitializeHistograms() for details. 2124 // See InitializeHistograms() for details.
2132 histogram_count_->Add(cookies_.size()); 2125 histogram_count_->Add(cookies_.size());
2133 2126
2134 // More detailed statistics on cookie counts at different granularities. 2127 // More detailed statistics on cookie counts at different granularities.
2135 TimeTicks beginning_of_time(TimeTicks::Now()); 2128 TimeTicks beginning_of_time(TimeTicks::Now());
2136 2129
2137 for (CookieMap::const_iterator it_key = cookies_.begin(); 2130 for (CookieMap::const_iterator it_key = cookies_.begin();
2138 it_key != cookies_.end(); ) { 2131 it_key != cookies_.end();) {
2139 const std::string& key(it_key->first); 2132 const std::string& key(it_key->first);
2140 2133
2141 int key_count = 0; 2134 int key_count = 0;
2142 typedef std::map<std::string, unsigned int> DomainMap; 2135 typedef std::map<std::string, unsigned int> DomainMap;
2143 DomainMap domain_map; 2136 DomainMap domain_map;
2144 CookieMapItPair its_cookies = cookies_.equal_range(key); 2137 CookieMapItPair its_cookies = cookies_.equal_range(key);
2145 while (its_cookies.first != its_cookies.second) { 2138 while (its_cookies.first != its_cookies.second) {
2146 key_count++; 2139 key_count++;
2147 const std::string& cookie_domain(its_cookies.first->second->Domain()); 2140 const std::string& cookie_domain(its_cookies.first->second->Domain());
2148 domain_map[cookie_domain]++; 2141 domain_map[cookie_domain]++;
2149 2142
2150 its_cookies.first++; 2143 its_cookies.first++;
2151 } 2144 }
2152 histogram_etldp1_count_->Add(key_count); 2145 histogram_etldp1_count_->Add(key_count);
2153 histogram_domain_per_etldp1_count_->Add(domain_map.size()); 2146 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2154 for (DomainMap::const_iterator domain_map_it = domain_map.begin(); 2147 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2155 domain_map_it != domain_map.end(); domain_map_it++) 2148 domain_map_it != domain_map.end();
2149 domain_map_it++)
2156 histogram_domain_count_->Add(domain_map_it->second); 2150 histogram_domain_count_->Add(domain_map_it->second);
2157 2151
2158 it_key = its_cookies.second; 2152 it_key = its_cookies.second;
2159 } 2153 }
2160 2154
2161 VLOG(kVlogPeriodic) 2155 VLOG(kVlogPeriodic) << "Time for recording cookie stats (us): "
2162 << "Time for recording cookie stats (us): " 2156 << (TimeTicks::Now() - beginning_of_time)
2163 << (TimeTicks::Now() - beginning_of_time).InMicroseconds(); 2157 .InMicroseconds();
2164 2158
2165 last_statistic_record_time_ = current_time; 2159 last_statistic_record_time_ = current_time;
2166 } 2160 }
2167 2161
2168 // Initialize all histogram counter variables used in this class. 2162 // Initialize all histogram counter variables used in this class.
2169 // 2163 //
2170 // Normal histogram usage involves using the macros defined in 2164 // Normal histogram usage involves using the macros defined in
2171 // histogram.h, which automatically takes care of declaring these 2165 // histogram.h, which automatically takes care of declaring these
2172 // variables (as statics), initializing them, and accumulating into 2166 // variables (as statics), initializing them, and accumulating into
2173 // them, all from a single entry point. Unfortunately, that solution 2167 // them, all from a single entry point. Unfortunately, that solution
2174 // doesn't work for the CookieMonster, as it's vulnerable to races between 2168 // doesn't work for the CookieMonster, as it's vulnerable to races between
2175 // separate threads executing the same functions and hence initializing the 2169 // separate threads executing the same functions and hence initializing the
2176 // same static variables. There isn't a race danger in the histogram 2170 // same static variables. There isn't a race danger in the histogram
2177 // accumulation calls; they are written to be resilient to simultaneous 2171 // accumulation calls; they are written to be resilient to simultaneous
2178 // calls from multiple threads. 2172 // calls from multiple threads.
2179 // 2173 //
2180 // The solution taken here is to have per-CookieMonster instance 2174 // The solution taken here is to have per-CookieMonster instance
2181 // variables that are constructed during CookieMonster construction. 2175 // variables that are constructed during CookieMonster construction.
2182 // Note that these variables refer to the same underlying histogram, 2176 // Note that these variables refer to the same underlying histogram,
2183 // so we still race (but safely) with other CookieMonster instances 2177 // so we still race (but safely) with other CookieMonster instances
2184 // for accumulation. 2178 // for accumulation.
2185 // 2179 //
2186 // To do this we've expanded out the individual histogram macros calls, 2180 // To do this we've expanded out the individual histogram macros calls,
2187 // with declarations of the variables in the class decl, initialization here 2181 // with declarations of the variables in the class decl, initialization here
2188 // (done from the class constructor) and direct calls to the accumulation 2182 // (done from the class constructor) and direct calls to the accumulation
2189 // methods where needed. The specific histogram macro calls on which the 2183 // methods where needed. The specific histogram macro calls on which the
2190 // initialization is based are included in comments below. 2184 // initialization is based are included in comments below.
2191 void CookieMonster::InitializeHistograms() { 2185 void CookieMonster::InitializeHistograms() {
2192 // From UMA_HISTOGRAM_CUSTOM_COUNTS 2186 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2193 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet( 2187 histogram_expiration_duration_minutes_ =
2194 "Cookie.ExpirationDurationMinutes", 2188 base::Histogram::FactoryGet("Cookie.ExpirationDurationMinutes",
2195 1, kMinutesInTenYears, 50, 2189 1,
2196 base::Histogram::kUmaTargetedHistogramFlag); 2190 kMinutesInTenYears,
2197 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet( 2191 50,
2198 "Cookie.BetweenAccessIntervalMinutes", 2192 base::Histogram::kUmaTargetedHistogramFlag);
2199 1, kMinutesInTenYears, 50, 2193 histogram_between_access_interval_minutes_ =
2200 base::Histogram::kUmaTargetedHistogramFlag); 2194 base::Histogram::FactoryGet("Cookie.BetweenAccessIntervalMinutes",
2201 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet( 2195 1,
2202 "Cookie.EvictedLastAccessMinutes", 2196 kMinutesInTenYears,
2203 1, kMinutesInTenYears, 50, 2197 50,
2204 base::Histogram::kUmaTargetedHistogramFlag); 2198 base::Histogram::kUmaTargetedHistogramFlag);
2199 histogram_evicted_last_access_minutes_ =
2200 base::Histogram::FactoryGet("Cookie.EvictedLastAccessMinutes",
2201 1,
2202 kMinutesInTenYears,
2203 50,
2204 base::Histogram::kUmaTargetedHistogramFlag);
2205 histogram_count_ = base::Histogram::FactoryGet( 2205 histogram_count_ = base::Histogram::FactoryGet(
2206 "Cookie.Count", 1, 4000, 50, 2206 "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag);
2207 base::Histogram::kUmaTargetedHistogramFlag); 2207 histogram_domain_count_ =
2208 histogram_domain_count_ = base::Histogram::FactoryGet( 2208 base::Histogram::FactoryGet("Cookie.DomainCount",
2209 "Cookie.DomainCount", 1, 4000, 50, 2209 1,
2210 base::Histogram::kUmaTargetedHistogramFlag); 2210 4000,
2211 histogram_etldp1_count_ = base::Histogram::FactoryGet( 2211 50,
2212 "Cookie.Etldp1Count", 1, 4000, 50, 2212 base::Histogram::kUmaTargetedHistogramFlag);
2213 base::Histogram::kUmaTargetedHistogramFlag); 2213 histogram_etldp1_count_ =
2214 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet( 2214 base::Histogram::FactoryGet("Cookie.Etldp1Count",
2215 "Cookie.DomainPerEtldp1Count", 1, 4000, 50, 2215 1,
2216 base::Histogram::kUmaTargetedHistogramFlag); 2216 4000,
2217 50,
2218 base::Histogram::kUmaTargetedHistogramFlag);
2219 histogram_domain_per_etldp1_count_ =
2220 base::Histogram::FactoryGet("Cookie.DomainPerEtldp1Count",
2221 1,
2222 4000,
2223 50,
2224 base::Histogram::kUmaTargetedHistogramFlag);
2217 2225
2218 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS 2226 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2219 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet( 2227 histogram_number_duplicate_db_cookies_ =
2220 "Net.NumDuplicateCookiesInDb", 1, 10000, 50, 2228 base::Histogram::FactoryGet("Net.NumDuplicateCookiesInDb",
2221 base::Histogram::kUmaTargetedHistogramFlag); 2229 1,
2230 10000,
2231 50,
2232 base::Histogram::kUmaTargetedHistogramFlag);
2222 2233
2223 // From UMA_HISTOGRAM_ENUMERATION 2234 // From UMA_HISTOGRAM_ENUMERATION
2224 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet( 2235 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2225 "Cookie.DeletionCause", 1, 2236 "Cookie.DeletionCause",
2226 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY, 2237 1,
2238 DELETE_COOKIE_LAST_ENTRY - 1,
2239 DELETE_COOKIE_LAST_ENTRY,
2227 base::Histogram::kUmaTargetedHistogramFlag); 2240 base::Histogram::kUmaTargetedHistogramFlag);
2228 2241
2229 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES 2242 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2230 histogram_time_get_ = base::Histogram::FactoryTimeGet("Cookie.TimeGet", 2243 histogram_time_get_ = base::Histogram::FactoryTimeGet(
2231 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), 2244 "Cookie.TimeGet",
2232 50, base::Histogram::kUmaTargetedHistogramFlag); 2245 base::TimeDelta::FromMilliseconds(1),
2246 base::TimeDelta::FromMinutes(1),
2247 50,
2248 base::Histogram::kUmaTargetedHistogramFlag);
2233 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet( 2249 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2234 "Cookie.TimeBlockedOnLoad", 2250 "Cookie.TimeBlockedOnLoad",
2235 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), 2251 base::TimeDelta::FromMilliseconds(1),
2236 50, base::Histogram::kUmaTargetedHistogramFlag); 2252 base::TimeDelta::FromMinutes(1),
2253 50,
2254 base::Histogram::kUmaTargetedHistogramFlag);
2237 } 2255 }
2238 2256
2239
2240 // The system resolution is not high enough, so we can have multiple 2257 // The system resolution is not high enough, so we can have multiple
2241 // set cookies that result in the same system time. When this happens, we 2258 // set cookies that result in the same system time. When this happens, we
2242 // increment by one Time unit. Let's hope computers don't get too fast. 2259 // increment by one Time unit. Let's hope computers don't get too fast.
2243 Time CookieMonster::CurrentTime() { 2260 Time CookieMonster::CurrentTime() {
2244 return std::max(Time::Now(), 2261 return std::max(
2262 Time::Now(),
2245 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); 2263 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));
2246 } 2264 }
2247 2265
2248 } // namespace net 2266 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698