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

Side by Side Diff: chrome/browser/net/evicted_domain_cookie_counter.h

Issue 12805010: Implementing EvictedDomainCookieCounter to keep user metrics on cookie eviction and reinstatement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync and cleanup. Created 7 years, 8 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
(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 CHROME_BROWSER_NET_EVICTED_DOMAIN_COOKIE_COUNTER_H_
6 #define CHROME_BROWSER_NET_EVICTED_DOMAIN_COOKIE_COUNTER_H_
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 class CanonicalCookie;
20 } // namespace net
21
22 namespace chrome_browser_net {
23
24 // The Evicted Domain Cookie Counter generates statistics on "wrongly evicted"
25 // cookies, i.e., cookies that were "evicted" (on reaching domain cookie limit)
26 // but are then "reinstated" later by a website because they were important.
27 // Here is a specific scenario: a long-lived login session cookie gets evicted
28 // due to its age, thereby forcing the user to lose session, and is reinstated
29 // when the annoyed user reauthenticates.
30 //
31 // A solution to the above problem is the Cookie Priority Field, which gives
32 // server a way to protect important cookies, thereby decreasing the chance
33 // that these cookies are wrongly evicted. To measure the effectiveness of this
34 // solution, we record eviction statistics before vs. after the fix.
35 //
36 // Specifically, we wish to record statistics on "reinstatement delay", i.e.,
37 // the duration between eviction and reinstatement of cookie. We expect that
38 // after the fix, average reinstantement delay will increase, since low priority
39 // cookies are less likely to be reinstated after eviction.
40 //
41 // Statistics for Google domains are tracked separately.
42 //
43 class EvictedDomainCookieCounter : public net::CookieMonster::Delegate {
44 public:
45 // Structure to store sanitized data from CanonicalCookie.
46 struct EvictedCookie {
47 base::Time eviction_time_;
48 base::Time expiry_time_;
49 bool is_google_;
mmenke 2013/03/29 15:22:16 Per Google style guide, members should go at end o
mmenke 2013/03/29 15:22:16 Per Google style guide,, struct members should not
mmenke 2013/03/29 15:23:04 Underscores, rather
huangs 2013/04/02 19:21:52 Done.
huangs 2013/04/02 19:21:52 Done.
50
51 EvictedCookie(base::Time eviction_time,
52 base::Time expiry_time,
53 bool is_google)
54 : eviction_time_(eviction_time),
55 expiry_time_(expiry_time),
56 is_google_(is_google) {}
57
58 bool is_expired(const base::Time& current) {
mmenke 2013/03/29 15:22:16 nit: const
huangs 2013/04/02 19:21:52 Done.
59 return !expiry_time_.is_null() && current >= expiry_time_;
60 }
61 };
62
63 class Delegate {
64 public:
65 virtual ~Delegate() {}
66
67 // Called when a stored evicted cookie is reinstated.
68 virtual void Report(const EvictedCookie& dc,
69 const base::Time& reinstatement_time) = 0;
70
71 // Getter of time is placed here to enable mocks.
72 virtual base::Time CurrentTime() = 0;
mmenke 2013/03/29 15:22:16 const?
huangs 2013/04/02 19:21:52 Okay. Done.
73 };
74
75 // |next_delegate| can be NULL.
76 explicit EvictedDomainCookieCounter(
77 scoped_refptr<net::CookieMonster::Delegate> next_delegate);
mmenke 2013/03/29 15:22:16 nit: Fix indent.
huangs 2013/04/02 19:21:52 Done.
78
79 EvictedDomainCookieCounter(
mmenke 2013/03/29 15:22:16 Think it's worth commenting that this constructor
huangs 2013/04/02 19:21:52 Added comment. We opted to expose extra constructo
80 scoped_refptr<net::CookieMonster::Delegate> next_cookie_monster_delegate,
81 scoped_ptr<Delegate> delegate,
mmenke 2013/03/29 15:22:16 I think having two different classes named simple
huangs 2013/04/02 19:21:52 Renamed by the variable, but not the class. The co
82 size_t max_size,
83 size_t purge_count);
84
85 // Returns the number of evicted cookies stored.
86 size_t GetStorageSize() const;
87
88 // CookieMonster::Delegate implementation.
89 virtual void OnCookieChanged(const net::CanonicalCookie& cc,
90 bool removed,
91 ChangeCause cause) OVERRIDE;
92
93 private:
94 // Identifier of an evicted cookie.
95 typedef std::string EvictedCookieKey;
96
97 // Storage class of evicted cookie.
98 typedef std::map<EvictedCookieKey, EvictedCookie*> EvictedCookieMap;
99
100 virtual ~EvictedDomainCookieCounter();
101
102 // Computes a key for |cc| compatible with CanonicalCookie::IsEquivalent(),
103 // i.e., IsEquivalent(a, b) ==> GetKey(a) == GetKey(b).
104 static EvictedCookieKey GetKey(const net::CanonicalCookie& cc);
105
106 // If too many evicted cookies are stored, delete the expired ones, then
107 // delete cookies that were evicted the longest, until size limit reached.
108 void GarbageCollect(const base::Time& current);
109
110 // Called when a cookie is evicted. Adds the evicted cookie to storage,
111 // possibly replacing an existing equivalent cookie.
112 void StoreEvictedCookie(const EvictedCookieKey& key,
113 const net::CanonicalCookie& cc,
mmenke 2013/03/29 15:22:16 Google style discourages non-standard abbreviation
huangs 2013/04/02 19:21:52 cc was used in CookieMonster. Compromising by rena
114 const base::Time& current);
mmenke 2013/03/29 15:22:16 "current_time" is a much clearer variable name tha
huangs 2013/04/02 19:21:52 Done.
115
116 // Called when a new cookie is added. If reinstatement occurs, then notifies
117 // |delegate_| and then removes the evicted cookie.
118 void ProcessNewCookie(const EvictedCookieKey& key,
119 const net::CanonicalCookie& cc,
mmenke 2013/03/29 15:22:16 canonical_cookie
huangs 2013/04/02 19:21:52 Done.
120 const base::Time& current);
mmenke 2013/03/29 15:22:16 "current_time"
huangs 2013/04/02 19:21:52 Done.
121
122 // Another delegate to forward events to.
123 scoped_refptr<net::CookieMonster::Delegate> next_cookie_monster_delegate_;
124
125 scoped_ptr<Delegate> delegate_;
mmenke 2013/03/29 15:22:16 suggest "cookie_counter_delegate" per earlier comm
huangs 2013/04/02 19:21:52 Done.
126
127 EvictedCookieMap evicted_cookies_;
128
129 // Capacity of the evicted cookie storage, before garbage collection occurs.
130 const size_t max_size_;
131
132 // After garbage collection, size reduces to <= |max_size_| - |purge_count_|.
133 const size_t purge_count_;
134
135 DISALLOW_COPY_AND_ASSIGN(EvictedDomainCookieCounter);
136 };
137
138 } // namespace chrome_browser_net
139
140 #endif // CHROME_BROWSER_NET_EVICTED_DOMAIN_COOKIE_COUNTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698