OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_COOKIES_EVICTED_DOMAIN_COOKIE_COUNTER_ | |
6 #define NET_COOKIES_EVICTED_DOMAIN_COOKIE_COUNTER_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/time.h" | |
14 #include "net/cookies/canonical_cookie.h" | |
erikwright (departed)
2013/03/21 19:22:19
forward-decl
huangs
2013/03/21 22:08:09
Done.
| |
15 #include "net/cookies/cookie_monster.h" | |
16 | |
17 namespace net { | |
18 | |
19 // The Evicted Domain Cookie Counter generates statistics on "wrongly evicted" | |
20 // cookies, i.e., cookies that were "evicted" (on reacking domain cookie limit) | |
erikwright (departed)
2013/03/21 19:22:19
typo
huangs
2013/03/21 22:08:09
Done.
| |
21 // but are then "reinstated" later by a website because they were important. | |
22 // Here is a specific scenario: a long-lived login session cookie gets evicted | |
23 // due to its age, thereby forcing the user to lose session, and is reinstated | |
24 // when the annoyed user reauthenticates. | |
25 // | |
26 // A solution to the above problem is the Cookie Priority Field, which gives | |
27 // server a way to protect important cookies, thereby decreasing the chance | |
28 // that these cookies are wrongly evicted. To measure the effictive of this | |
29 // solution, we record eviction statistics before vs. after the fix. | |
30 // | |
31 // Specifically, we wish to record statistics on "reinstatement delay", i.e., | |
32 // the duration between eviction and reinstatement of cookie. We expect that | |
33 // after the fix, average reinstantement delay will increase, since low priority | |
34 // cookies are less likely to be reinstated after eviction. | |
35 // | |
36 // Statistics for Google domains are tracked separately. | |
37 // | |
38 class NET_EXPORT EvictedDomainCookieCounter : public CookieMonster::Delegate { | |
39 public: | |
40 // Structure to store sanitized (no cookie value) data from CanonicalCookie. | |
erikwright (departed)
2013/03/21 19:22:19
parenthetical is unnecessary
huangs
2013/03/21 22:08:09
Done.
| |
41 struct EvictedCookie { | |
42 base::Time eviction_time_; | |
43 base::Time expiry_time_; | |
44 bool is_google_; | |
45 | |
46 EvictedCookie(base::Time eviction_time, | |
47 base::Time expiry_time, | |
48 bool is_google) | |
49 : eviction_time_(eviction_time), | |
50 expiry_time_(expiry_time), | |
51 is_google_(is_google) {} | |
52 | |
53 void set_expired() { | |
54 expiry_time_ = base::Time::UnixEpoch(); | |
55 } | |
56 bool is_expired(const base::Time& current) { | |
57 return !expiry_time_.is_null() && current >= expiry_time_; | |
58 } | |
59 }; | |
60 | |
61 class Delegate { | |
62 public: | |
erikwright (departed)
2013/03/21 19:22:19
Needs a virtual destructor.
huangs
2013/03/21 22:08:09
Done. Making it public instead of protected (else
| |
63 // Called when a stored evicted cookie is reinstated. | |
64 virtual void Report(const EvictedCookie& dc, | |
65 const base::Time& reinstatement_time) = 0; | |
66 | |
67 // Getter of time is placed here to enable mocks. | |
68 virtual base::Time CurrentTime() = 0; | |
69 }; | |
70 | |
71 // Identifier of an evicted cookie. Can be obtained from GetKey(). | |
erikwright (departed)
2013/03/21 19:22:19
These typedefs can be private, right?
huangs
2013/03/21 22:08:09
Done.
| |
72 typedef std::string EvictedCookieKey; | |
73 | |
74 // Storage class of evicted cookie. | |
75 typedef std::map<EvictedCookieKey, EvictedCookie*> EvictedCookieMap; | |
76 | |
77 explicit EvictedDomainCookieCounter(CookieMonster::Delegate* next_delegate); | |
erikwright (departed)
2013/03/21 19:22:19
make scoped_refptr<CookieMonster::Delegate>
huangs
2013/03/21 22:08:09
Done.
| |
78 | |
79 EvictedDomainCookieCounter( | |
80 scoped_refptr<CookieMonster::Delegate> next_delegate, | |
erikwright (departed)
2013/03/21 19:22:19
ref_counted.h
huangs
2013/03/21 22:08:09
Done.
| |
81 scoped_ptr<Delegate> reporter, | |
erikwright (departed)
2013/03/21 19:22:19
scoped_ptr.h
huangs
2013/03/21 22:08:09
Done.
| |
82 size_t max_size, | |
83 size_t purge_count); | |
84 | |
85 // Returns whether or not |cc| is a cookie from Google. | |
86 static bool IsGoogleCookie(const CanonicalCookie& cc); | |
87 | |
88 // Returns the number of evicted cookies stored. | |
89 size_t GetStorageSize() const; | |
90 | |
91 // CookieMonster::Delegate implementation. | |
92 virtual void OnCookieChanged(const CanonicalCookie& cc, | |
93 bool removed, | |
94 ChangeCause cause) OVERRIDE; | |
erikwright (departed)
2013/03/21 19:22:19
compiler_specific.h for OVERRIDE
huangs
2013/03/21 22:08:09
Done.
| |
95 | |
96 private: | |
97 ~EvictedDomainCookieCounter(); | |
98 | |
99 // Computes a key for |cc| compatible with CanonicalCookie::IsEquivalent(), | |
100 // i.e., IsEquivalent(a, b) ==> GetKey(a) == GetKey(b). | |
101 static EvictedCookieKey GetKey(const CanonicalCookie& cc); | |
102 | |
103 // Deallocates and removes an evicted cookie with given |key| if it is stored. | |
104 void FreeEvictedCookie(const EvictedCookieKey& key); | |
105 | |
106 // Deallocates and removes all expired evicted cookies. | |
107 void FreeExpiredEvictedCookies(base::Time time_bound); | |
108 | |
109 // If too many evicted cookies are stored, delete the expired oens, then | |
110 // delete cookies that were evicted the longest, until size limit reached. | |
111 void GarbageCollect(const base::Time& current); | |
112 | |
113 // Called when a cookie is evicted. Adds the evicted cookie to storage, | |
114 // possibly replacing an existing evicted cookie. | |
115 void StoreEvictedCookie(const EvictedCookieKey& key, | |
116 const CanonicalCookie& cc, | |
117 const base::Time& current); | |
118 | |
119 // Called when a new cookie is added, i.e., reinstatement may occur. | |
120 // Returns true iff the reinstatement takes place. | |
121 bool ProcessCookieReinstatement(const EvictedCookieKey& key, | |
122 const CanonicalCookie& cc, | |
123 const base::Time& current); | |
124 | |
125 // For chaining the OnCookieChanged() event. | |
126 scoped_refptr<CookieMonster::Delegate> next_delegate_; | |
127 | |
128 // On reinstatement, data are passed here to report reinstatement delay. | |
129 scoped_ptr<Delegate> reporter_; | |
130 | |
131 // In-memory-only buffer to store sanitized (no cookie value) evicted cookies. | |
132 EvictedCookieMap evicted_cookies_; | |
133 | |
134 // Capacity of the evicted cookie storage. | |
135 const size_t max_size_; | |
136 | |
137 // Minimum amount to decrease evicted cookie storage, on garbage collection. | |
138 const size_t purge_count_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(EvictedDomainCookieCounter); | |
erikwright (departed)
2013/03/21 19:22:19
basictypes.h
huangs
2013/03/21 22:08:09
Done.
| |
141 }; | |
142 | |
143 } // namespace net | |
144 | |
145 #endif // NET_COOKIES_EVICTED_DOMAIN_COOKIE_COUNTER_ | |
OLD | NEW |