forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie_monster.cc
2159 lines (1820 loc) · 75.4 KB
/
cookie_monster.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Portions of this code based on Mozilla:
// (netwerk/cookie/src/nsCookieService.cpp)
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2003
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Daniel Witte (dwitte@stanford.edu)
* Michiel van Leeuwen (mvl@exedo.nl)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "net/cookies/cookie_monster.h"
#include <algorithm>
#include <functional>
#include <set>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "googleurl/src/gurl.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_util.h"
#include "net/cookies/parsed_cookie.h"
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
// In steady state, most cookie requests can be satisfied by the in memory
// cookie monster store. However, if a request comes in during the initial
// cookie load, it must be delayed until that load completes. That is done by
// queueing it on CookieMonster::tasks_pending_ and running it when notification
// of cookie load completion is received via CookieMonster::OnLoaded. This
// callback is passed to the persistent store from CookieMonster::InitStore(),
// which is called on the first operation invoked on the CookieMonster.
//
// On the browser critical paths (e.g. for loading initial web pages in a
// session restore) it may take too long to wait for the full load. If a cookie
// request is for a specific URL, DoCookieTaskForURL is called, which triggers a
// priority load if the key is not loaded yet by calling PersistentCookieStore
// :: LoadCookiesForKey. The request is queued in
// CookieMonster::tasks_pending_for_key_ and executed upon receiving
// notification of key load completion via CookieMonster::OnKeyLoaded(). If
// multiple requests for the same eTLD+1 are received before key load
// completion, only the first request calls
// PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
// in CookieMonster::tasks_pending_for_key_ and executed upon receiving
// notification of key load completion triggered by the first request for the
// same eTLD+1.
static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
namespace net {
// See comments at declaration of these variables in cookie_monster.h
// for details.
const size_t CookieMonster::kDomainMaxCookies = 180;
const size_t CookieMonster::kDomainPurgeCookies = 30;
const size_t CookieMonster::kMaxCookies = 3300;
const size_t CookieMonster::kPurgeCookies = 300;
const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
const size_t CookieMonster::kDomainCookiesQuotaHigh =
CookieMonster::kDomainMaxCookies - CookieMonster::kDomainPurgeCookies
- CookieMonster::kDomainCookiesQuotaLow
- CookieMonster::kDomainCookiesQuotaMedium;
const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
namespace {
typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
// Default minimum delay after updating a cookie's LastAccessDate before we
// will update it again.
const int kDefaultAccessUpdateThresholdSeconds = 60;
// Comparator to sort cookies from highest creation date to lowest
// creation date.
struct OrderByCreationTimeDesc {
bool operator()(const CookieMonster::CookieMap::iterator& a,
const CookieMonster::CookieMap::iterator& b) const {
return a->second->CreationDate() > b->second->CreationDate();
}
};
// Constants for use in VLOG
const int kVlogPerCookieMonster = 1;
const int kVlogPeriodic = 3;
const int kVlogGarbageCollection = 5;
const int kVlogSetCookies = 7;
const int kVlogGetCookies = 9;
// Mozilla sorts on the path length (longest first), and then it
// sorts by creation time (oldest first).
// The RFC says the sort order for the domain attribute is undefined.
bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
if (cc1->Path().length() == cc2->Path().length())
return cc1->CreationDate() < cc2->CreationDate();
return cc1->Path().length() > cc2->Path().length();
}
bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
const CookieMonster::CookieMap::iterator& it2) {
// Cookies accessed less recently should be deleted first.
if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
return it1->second->LastAccessDate() < it2->second->LastAccessDate();
// In rare cases we might have two cookies with identical last access times.
// To preserve the stability of the sort, in these cases prefer to delete
// older cookies over newer ones. CreationDate() is guaranteed to be unique.
return it1->second->CreationDate() < it2->second->CreationDate();
}
// Our strategy to find duplicates is:
// (1) Build a map from (cookiename, cookiepath) to
// {list of cookies with this signature, sorted by creation time}.
// (2) For each list with more than 1 entry, keep the cookie having the
// most recent creation time, and delete the others.
//
// Two cookies are considered equivalent if they have the same domain,
// name, and path.
struct CookieSignature {
public:
CookieSignature(const std::string& name,
const std::string& domain,
const std::string& path)
: name(name), domain(domain), path(path) {
}
// To be a key for a map this class needs to be assignable, copyable,
// and have an operator<. The default assignment operator
// and copy constructor are exactly what we want.
bool operator<(const CookieSignature& cs) const {
// Name compare dominates, then domain, then path.
int diff = name.compare(cs.name);
if (diff != 0)
return diff < 0;
diff = domain.compare(cs.domain);
if (diff != 0)
return diff < 0;
return path.compare(cs.path) < 0;
}
std::string name;
std::string domain;
std::string path;
};
// Determine the cookie domain to use for setting the specified cookie.
bool GetCookieDomain(const GURL& url,
const ParsedCookie& pc,
std::string* result) {
std::string domain_string;
if (pc.HasDomain())
domain_string = pc.Domain();
return cookie_util::GetCookieDomainWithString(url, domain_string, result);
}
// For a CookieItVector iterator range [|it_begin|, |it_end|),
// sorts the first |num_sort| + 1 elements by LastAccessDate().
// The + 1 element exists so for any interval of length <= |num_sort| starting
// from |cookies_its_begin|, a LastAccessDate() bound can be found.
void SortLeastRecentlyAccessed(
CookieMonster::CookieItVector::iterator it_begin,
CookieMonster::CookieItVector::iterator it_end,
size_t num_sort) {
DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
}
// Predicate to support PartitionCookieByPriority().
struct CookiePriorityEqualsTo
: std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
CookiePriorityEqualsTo(CookiePriority priority)
: priority_(priority) {}
bool operator()(const CookieMonster::CookieMap::iterator it) const {
return it->second->Priority() == priority_;
}
const CookiePriority priority_;
};
// For a CookieItVector iterator range [|it_begin|, |it_end|),
// moves all cookies with a given |priority| to the beginning of the list.
// Returns: An iterator in [it_begin, it_end) to the first element with
// priority != |priority|, or |it_end| if all have priority == |priority|.
CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
CookieMonster::CookieItVector::iterator it_begin,
CookieMonster::CookieItVector::iterator it_end,
CookiePriority priority) {
return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
}
bool LowerBoundAccessDateComparator(
const CookieMonster::CookieMap::iterator it, const Time& access_date) {
return it->second->LastAccessDate() < access_date;
}
// For a CookieItVector iterator range [|it_begin|, |it_end|)
// from a CookieItVector sorted by LastAccessDate(), returns the
// first iterator with access date >= |access_date|, or cookie_its_end if this
// holds for all.
CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
const CookieMonster::CookieItVector::iterator its_begin,
const CookieMonster::CookieItVector::iterator its_end,
const Time& access_date) {
return std::lower_bound(its_begin, its_end, access_date,
LowerBoundAccessDateComparator);
}
// Mapping between DeletionCause and Delegate::ChangeCause; the mapping also
// provides a boolean that specifies whether or not an OnCookieChanged
// notification ought to be generated.
typedef struct ChangeCausePair_struct {
CookieMonster::Delegate::ChangeCause cause;
bool notify;
} ChangeCausePair;
ChangeCausePair ChangeCauseMapping[] = {
// DELETE_COOKIE_EXPLICIT
{ CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, true },
// DELETE_COOKIE_OVERWRITE
{ CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE, true },
// DELETE_COOKIE_EXPIRED
{ CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED, true },
// DELETE_COOKIE_EVICTED
{ CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
// DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
{ CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false },
// DELETE_COOKIE_DONT_RECORD
{ CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false },
// DELETE_COOKIE_EVICTED_DOMAIN
{ CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
// DELETE_COOKIE_EVICTED_GLOBAL
{ CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
// DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
{ CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
// DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
{ CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
// DELETE_COOKIE_EXPIRED_OVERWRITE
{ CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true },
// DELETE_COOKIE_LAST_ENTRY
{ CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false }
};
std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
std::string cookie_line;
for (CanonicalCookieVector::const_iterator it = cookies.begin();
it != cookies.end(); ++it) {
if (it != cookies.begin())
cookie_line += "; ";
// In Mozilla if you set a cookie like AAAA, it will have an empty token
// and a value of AAAA. When it sends the cookie back, it will send AAAA,
// so we need to avoid sending =AAAA for a blank token value.
if (!(*it)->Name().empty())
cookie_line += (*it)->Name() + "=";
cookie_line += (*it)->Value();
}
return cookie_line;
}
} // namespace
// static
bool CookieMonster::default_enable_file_scheme_ = false;
CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate)
: initialized_(false),
loaded_(false),
store_(store),
last_access_threshold_(
TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
delegate_(delegate),
last_statistic_record_time_(Time::Now()),
keep_expired_cookies_(false),
persist_session_cookies_(false),
priority_aware_garbage_collection_(false) {
InitializeHistograms();
SetDefaultCookieableSchemes();
}
CookieMonster::CookieMonster(PersistentCookieStore* store,
Delegate* delegate,
int last_access_threshold_milliseconds)
: initialized_(false),
loaded_(false),
store_(store),
last_access_threshold_(base::TimeDelta::FromMilliseconds(
last_access_threshold_milliseconds)),
delegate_(delegate),
last_statistic_record_time_(base::Time::Now()),
keep_expired_cookies_(false),
persist_session_cookies_(false),
priority_aware_garbage_collection_(false) {
InitializeHistograms();
SetDefaultCookieableSchemes();
}
// Task classes for queueing the coming request.
class CookieMonster::CookieMonsterTask
: public base::RefCountedThreadSafe<CookieMonsterTask> {
public:
// Runs the task and invokes the client callback on the thread that
// originally constructed the task.
virtual void Run() = 0;
protected:
explicit CookieMonsterTask(CookieMonster* cookie_monster);
virtual ~CookieMonsterTask();
// Invokes the callback immediately, if the current thread is the one
// that originated the task, or queues the callback for execution on the
// appropriate thread. Maintains a reference to this CookieMonsterTask
// instance until the callback completes.
void InvokeCallback(base::Closure callback);
CookieMonster* cookie_monster() {
return cookie_monster_;
}
private:
friend class base::RefCountedThreadSafe<CookieMonsterTask>;
CookieMonster* cookie_monster_;
scoped_refptr<base::MessageLoopProxy> thread_;
DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
};
CookieMonster::CookieMonsterTask::CookieMonsterTask(
CookieMonster* cookie_monster)
: cookie_monster_(cookie_monster),
thread_(base::MessageLoopProxy::current()) {
}
CookieMonster::CookieMonsterTask::~CookieMonsterTask() {}
// Unfortunately, one cannot re-bind a Callback with parameters into a closure.
// Therefore, the closure passed to InvokeCallback is a clumsy binding of
// Callback::Run on a wrapped Callback instance. Since Callback is not
// reference counted, we bind to an instance that is a member of the
// CookieMonsterTask subclass. Then, we cannot simply post the callback to a
// message loop because the underlying instance may be destroyed (along with the
// CookieMonsterTask instance) in the interim. Therefore, we post a callback
// bound to the CookieMonsterTask, which *is* reference counted (thus preventing
// destruction of the original callback), and which invokes the closure (which
// invokes the original callback with the returned data).
void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
if (thread_->BelongsToCurrentThread()) {
callback.Run();
} else {
thread_->PostTask(FROM_HERE, base::Bind(
&CookieMonster::CookieMonsterTask::InvokeCallback, this, callback));
}
}
// Task class for SetCookieWithDetails call.
class CookieMonster::SetCookieWithDetailsTask
: public CookieMonster::CookieMonsterTask {
public:
SetCookieWithDetailsTask(CookieMonster* cookie_monster,
const GURL& url,
const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
const base::Time& expiration_time,
bool secure,
bool http_only,
CookiePriority priority,
const CookieMonster::SetCookiesCallback& callback)
: CookieMonsterTask(cookie_monster),
url_(url),
name_(name),
value_(value),
domain_(domain),
path_(path),
expiration_time_(expiration_time),
secure_(secure),
http_only_(http_only),
priority_(priority),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~SetCookieWithDetailsTask() {}
private:
GURL url_;
std::string name_;
std::string value_;
std::string domain_;
std::string path_;
base::Time expiration_time_;
bool secure_;
bool http_only_;
CookiePriority priority_;
CookieMonster::SetCookiesCallback callback_;
DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
};
void CookieMonster::SetCookieWithDetailsTask::Run() {
bool success = this->cookie_monster()->
SetCookieWithDetails(url_, name_, value_, domain_, path_,
expiration_time_, secure_, http_only_, priority_);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::SetCookiesCallback::Run,
base::Unretained(&callback_), success));
}
}
// Task class for GetAllCookies call.
class CookieMonster::GetAllCookiesTask
: public CookieMonster::CookieMonsterTask {
public:
GetAllCookiesTask(CookieMonster* cookie_monster,
const CookieMonster::GetCookieListCallback& callback)
: CookieMonsterTask(cookie_monster),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask
virtual void Run() OVERRIDE;
protected:
virtual ~GetAllCookiesTask() {}
private:
CookieMonster::GetCookieListCallback callback_;
DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
};
void CookieMonster::GetAllCookiesTask::Run() {
if (!callback_.is_null()) {
CookieList cookies = this->cookie_monster()->GetAllCookies();
this->InvokeCallback(base::Bind(&CookieMonster::GetCookieListCallback::Run,
base::Unretained(&callback_), cookies));
}
}
// Task class for GetAllCookiesForURLWithOptions call.
class CookieMonster::GetAllCookiesForURLWithOptionsTask
: public CookieMonster::CookieMonsterTask {
public:
GetAllCookiesForURLWithOptionsTask(
CookieMonster* cookie_monster,
const GURL& url,
const CookieOptions& options,
const CookieMonster::GetCookieListCallback& callback)
: CookieMonsterTask(cookie_monster),
url_(url),
options_(options),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~GetAllCookiesForURLWithOptionsTask() {}
private:
GURL url_;
CookieOptions options_;
CookieMonster::GetCookieListCallback callback_;
DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
};
void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
if (!callback_.is_null()) {
CookieList cookies = this->cookie_monster()->
GetAllCookiesForURLWithOptions(url_, options_);
this->InvokeCallback(base::Bind(&CookieMonster::GetCookieListCallback::Run,
base::Unretained(&callback_), cookies));
}
}
// Task class for DeleteAll call.
class CookieMonster::DeleteAllTask : public CookieMonster::CookieMonsterTask {
public:
DeleteAllTask(CookieMonster* cookie_monster,
const CookieMonster::DeleteCallback& callback)
: CookieMonsterTask(cookie_monster),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~DeleteAllTask() {}
private:
CookieMonster::DeleteCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
};
void CookieMonster::DeleteAllTask::Run() {
int num_deleted = this->cookie_monster()->DeleteAll(true);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run,
base::Unretained(&callback_), num_deleted));
}
}
// Task class for DeleteAllCreatedBetween call.
class CookieMonster::DeleteAllCreatedBetweenTask
: public CookieMonster::CookieMonsterTask {
public:
DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
const Time& delete_begin,
const Time& delete_end,
const CookieMonster::DeleteCallback& callback)
: CookieMonsterTask(cookie_monster),
delete_begin_(delete_begin),
delete_end_(delete_end),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~DeleteAllCreatedBetweenTask() {}
private:
Time delete_begin_;
Time delete_end_;
CookieMonster::DeleteCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
};
void CookieMonster::DeleteAllCreatedBetweenTask::Run() {
int num_deleted = this->cookie_monster()->
DeleteAllCreatedBetween(delete_begin_, delete_end_);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run,
base::Unretained(&callback_), num_deleted));
}
}
// Task class for DeleteAllForHost call.
class CookieMonster::DeleteAllForHostTask
: public CookieMonster::CookieMonsterTask {
public:
DeleteAllForHostTask(CookieMonster* cookie_monster,
const GURL& url,
const CookieMonster::DeleteCallback& callback)
: CookieMonsterTask(cookie_monster),
url_(url),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~DeleteAllForHostTask() {}
private:
GURL url_;
CookieMonster::DeleteCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
};
void CookieMonster::DeleteAllForHostTask::Run() {
int num_deleted = this->cookie_monster()->DeleteAllForHost(url_);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run,
base::Unretained(&callback_), num_deleted));
}
}
// Task class for DeleteCanonicalCookie call.
class CookieMonster::DeleteCanonicalCookieTask
: public CookieMonster::CookieMonsterTask {
public:
DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
const CanonicalCookie& cookie,
const CookieMonster::DeleteCookieCallback& callback)
: CookieMonsterTask(cookie_monster),
cookie_(cookie),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~DeleteCanonicalCookieTask() {}
private:
CanonicalCookie cookie_;
CookieMonster::DeleteCookieCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
};
void CookieMonster::DeleteCanonicalCookieTask::Run() {
bool result = this->cookie_monster()->DeleteCanonicalCookie(cookie_);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::DeleteCookieCallback::Run,
base::Unretained(&callback_), result));
}
}
// Task class for SetCookieWithOptions call.
class CookieMonster::SetCookieWithOptionsTask
: public CookieMonster::CookieMonsterTask {
public:
SetCookieWithOptionsTask(CookieMonster* cookie_monster,
const GURL& url,
const std::string& cookie_line,
const CookieOptions& options,
const CookieMonster::SetCookiesCallback& callback)
: CookieMonsterTask(cookie_monster),
url_(url),
cookie_line_(cookie_line),
options_(options),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~SetCookieWithOptionsTask() {}
private:
GURL url_;
std::string cookie_line_;
CookieOptions options_;
CookieMonster::SetCookiesCallback callback_;
DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
};
void CookieMonster::SetCookieWithOptionsTask::Run() {
bool result = this->cookie_monster()->
SetCookieWithOptions(url_, cookie_line_, options_);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::SetCookiesCallback::Run,
base::Unretained(&callback_), result));
}
}
// Task class for GetCookiesWithOptions call.
class CookieMonster::GetCookiesWithOptionsTask
: public CookieMonster::CookieMonsterTask {
public:
GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
const GURL& url,
const CookieOptions& options,
const CookieMonster::GetCookiesCallback& callback)
: CookieMonsterTask(cookie_monster),
url_(url),
options_(options),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~GetCookiesWithOptionsTask() {}
private:
GURL url_;
CookieOptions options_;
CookieMonster::GetCookiesCallback callback_;
DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
};
void CookieMonster::GetCookiesWithOptionsTask::Run() {
std::string cookie = this->cookie_monster()->
GetCookiesWithOptions(url_, options_);
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::GetCookiesCallback::Run,
base::Unretained(&callback_), cookie));
}
}
// Task class for DeleteCookie call.
class CookieMonster::DeleteCookieTask
: public CookieMonster::CookieMonsterTask {
public:
DeleteCookieTask(CookieMonster* cookie_monster,
const GURL& url,
const std::string& cookie_name,
const base::Closure& callback)
: CookieMonsterTask(cookie_monster),
url_(url),
cookie_name_(cookie_name),
callback_(callback) { }
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~DeleteCookieTask() {}
private:
GURL url_;
std::string cookie_name_;
base::Closure callback_;
DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
};
void CookieMonster::DeleteCookieTask::Run() {
this->cookie_monster()->DeleteCookie(url_, cookie_name_);
if (!callback_.is_null()) {
this->InvokeCallback(callback_);
}
}
// Task class for DeleteSessionCookies call.
class CookieMonster::DeleteSessionCookiesTask
: public CookieMonster::CookieMonsterTask {
public:
DeleteSessionCookiesTask(CookieMonster* cookie_monster,
const CookieMonster::DeleteCallback& callback)
: CookieMonsterTask(cookie_monster), callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~DeleteSessionCookiesTask() {}
private:
CookieMonster::DeleteCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
};
void CookieMonster::DeleteSessionCookiesTask::Run() {
int num_deleted = this->cookie_monster()->DeleteSessionCookies();
if (!callback_.is_null()) {
this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run,
base::Unretained(&callback_), num_deleted));
}
}
// Task class for HasCookiesForETLDP1Task call.
class CookieMonster::HasCookiesForETLDP1Task
: public CookieMonster::CookieMonsterTask {
public:
HasCookiesForETLDP1Task(
CookieMonster* cookie_monster,
const std::string& etldp1,
const CookieMonster::HasCookiesForETLDP1Callback& callback)
: CookieMonsterTask(cookie_monster),
etldp1_(etldp1),
callback_(callback) {
}
// CookieMonster::CookieMonsterTask:
virtual void Run() OVERRIDE;
protected:
virtual ~HasCookiesForETLDP1Task() {}
private:
std::string etldp1_;
CookieMonster::HasCookiesForETLDP1Callback callback_;
DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
};
void CookieMonster::HasCookiesForETLDP1Task::Run() {
bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
if (!callback_.is_null()) {
this->InvokeCallback(
base::Bind(&CookieMonster::HasCookiesForETLDP1Callback::Run,
base::Unretained(&callback_), result));
}
}
// Asynchronous CookieMonster API
void CookieMonster::SetCookieWithDetailsAsync(
const GURL& url,
const std::string& name,
const std::string& value,
const std::string& domain,
const std::string& path,
const base::Time& expiration_time,
bool secure,
bool http_only,
CookiePriority priority,
const SetCookiesCallback& callback) {
scoped_refptr<SetCookieWithDetailsTask> task =
new SetCookieWithDetailsTask(this, url, name, value, domain, path,
expiration_time, secure, http_only, priority,
callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
scoped_refptr<GetAllCookiesTask> task =
new GetAllCookiesTask(this, callback);
DoCookieTask(task);
}
void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
const GURL& url,
const CookieOptions& options,
const GetCookieListCallback& callback) {
scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::GetAllCookiesForURLAsync(
const GURL& url, const GetCookieListCallback& callback) {
CookieOptions options;
options.set_include_httponly();
scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::HasCookiesForETLDP1Async(
const std::string& etldp1,
const HasCookiesForETLDP1Callback& callback) {
scoped_refptr<HasCookiesForETLDP1Task> task =
new HasCookiesForETLDP1Task(this, etldp1, callback);
DoCookieTaskForURL(task, GURL("http://" + etldp1));
}
void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
scoped_refptr<DeleteAllTask> task =
new DeleteAllTask(this, callback);
DoCookieTask(task);
}
void CookieMonster::DeleteAllCreatedBetweenAsync(
const Time& delete_begin, const Time& delete_end,
const DeleteCallback& callback) {
scoped_refptr<DeleteAllCreatedBetweenTask> task =
new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
callback);
DoCookieTask(task);
}
void CookieMonster::DeleteAllForHostAsync(
const GURL& url, const DeleteCallback& callback) {
scoped_refptr<DeleteAllForHostTask> task =
new DeleteAllForHostTask(this, url, callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::DeleteCanonicalCookieAsync(
const CanonicalCookie& cookie,
const DeleteCookieCallback& callback) {
scoped_refptr<DeleteCanonicalCookieTask> task =
new DeleteCanonicalCookieTask(this, cookie, callback);
DoCookieTask(task);
}
void CookieMonster::SetCookieWithOptionsAsync(
const GURL& url,
const std::string& cookie_line,
const CookieOptions& options,
const SetCookiesCallback& callback) {
scoped_refptr<SetCookieWithOptionsTask> task =
new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::GetCookiesWithOptionsAsync(
const GURL& url,
const CookieOptions& options,
const GetCookiesCallback& callback) {
scoped_refptr<GetCookiesWithOptionsTask> task =
new GetCookiesWithOptionsTask(this, url, options, callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::DeleteCookieAsync(const GURL& url,
const std::string& cookie_name,
const base::Closure& callback) {
scoped_refptr<DeleteCookieTask> task =
new DeleteCookieTask(this, url, cookie_name, callback);
DoCookieTaskForURL(task, url);
}
void CookieMonster::DeleteSessionCookiesAsync(
const CookieStore::DeleteCallback& callback) {
scoped_refptr<DeleteSessionCookiesTask> task =
new DeleteSessionCookiesTask(this, callback);
DoCookieTask(task);
}
void CookieMonster::DoCookieTask(
const scoped_refptr<CookieMonsterTask>& task_item) {
{
base::AutoLock autolock(lock_);
InitIfNecessary();
if (!loaded_) {
tasks_pending_.push(task_item);
return;
}
}
task_item->Run();
}
void CookieMonster::DoCookieTaskForURL(
const scoped_refptr<CookieMonsterTask>& task_item,
const GURL& url) {
{
base::AutoLock autolock(lock_);
InitIfNecessary();
// If cookies for the requested domain key (eTLD+1) have been loaded from DB
// then run the task, otherwise load from DB.
if (!loaded_) {
// Checks if the domain key has been loaded.
std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
url.host()));
if (keys_loaded_.find(key) == keys_loaded_.end()) {
std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
::iterator it = tasks_pending_for_key_.find(key);
if (it == tasks_pending_for_key_.end()) {
store_->LoadCookiesForKey(key,