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