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 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 // Identifier of an evicted cookie. |
| 46 typedef std::string EvictedCookieKey; |
| 47 |
| 48 // Structure to store sanitized data from CanonicalCookie. |
| 49 struct EvictedCookie { |
| 50 base::Time eviction_time_; |
| 51 base::Time expiry_time_; |
| 52 bool is_google_; |
| 53 |
| 54 EvictedCookie(base::Time eviction_time, |
| 55 base::Time expiry_time, |
| 56 bool is_google) |
| 57 : eviction_time_(eviction_time), |
| 58 expiry_time_(expiry_time), |
| 59 is_google_(is_google) {} |
| 60 |
| 61 bool is_expired(const base::Time& current) { |
| 62 return !expiry_time_.is_null() && current >= expiry_time_; |
| 63 } |
| 64 }; |
| 65 |
| 66 class Delegate { |
| 67 public: |
| 68 virtual ~Delegate() {} |
| 69 |
| 70 // Called when a stored evicted cookie is reinstated. |
| 71 virtual void Report(const EvictedCookie& dc, |
| 72 const base::Time& reinstatement_time) = 0; |
| 73 |
| 74 // Getter of time is placed here to enable mocks. |
| 75 virtual base::Time CurrentTime() = 0; |
| 76 }; |
| 77 |
| 78 // |next_delegate| can be NULL. |
| 79 explicit EvictedDomainCookieCounter( |
| 80 scoped_refptr<net::CookieMonster::Delegate> next_delegate); |
| 81 |
| 82 EvictedDomainCookieCounter( |
| 83 scoped_refptr<net::CookieMonster::Delegate> next_cookie_monster_delegate, |
| 84 scoped_ptr<Delegate> delegate, |
| 85 size_t max_size, |
| 86 size_t purge_count); |
| 87 |
| 88 // Returns the number of evicted cookies stored. |
| 89 size_t GetStorageSize() const; |
| 90 |
| 91 // CookieMonster::Delegate implementation. |
| 92 virtual void OnCookieChanged(const net::CanonicalCookie& cc, |
| 93 bool removed, |
| 94 ChangeCause cause) OVERRIDE; |
| 95 |
| 96 private: |
| 97 // Storage class of evicted cookie. |
| 98 typedef std::map<EvictedCookieKey, EvictedCookie*> EvictedCookieMap; |
| 99 |
| 100 virtual ~EvictedDomainCookieCounter(); |
| 101 |
| 102 // If too many evicted cookies are stored, delete the expired ones, then |
| 103 // delete cookies that were evicted the longest, until size limit reached. |
| 104 void GarbageCollect(const base::Time& current); |
| 105 |
| 106 // Called when a cookie is evicted. Adds the evicted cookie to storage, |
| 107 // possibly replacing an existing equivalent cookie. |
| 108 void StoreEvictedCookie(const EvictedCookieKey& key, |
| 109 const net::CanonicalCookie& cc, |
| 110 const base::Time& current); |
| 111 |
| 112 // Called when a new cookie is added. If reinstatement occurs, then notifies |
| 113 // |delegate_| and then removes the evicted cookie. |
| 114 void ProcessNewCookie(const EvictedCookieKey& key, |
| 115 const net::CanonicalCookie& cc, |
| 116 const base::Time& current); |
| 117 |
| 118 // Another delegate to forward events to. |
| 119 scoped_refptr<net::CookieMonster::Delegate> next_cookie_monster_delegate_; |
| 120 |
| 121 scoped_ptr<Delegate> delegate_; |
| 122 |
| 123 EvictedCookieMap evicted_cookies_; |
| 124 |
| 125 // Capacity of the evicted cookie storage, before garbage collection occurs. |
| 126 const size_t max_size_; |
| 127 |
| 128 // After garbage collection, size reduces to <= |max_size_| - |purge_count_|. |
| 129 const size_t purge_count_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(EvictedDomainCookieCounter); |
| 132 }; |
| 133 |
| 134 } // namespace chrome_browser_net |
| 135 |
| 136 #endif // CHROME_BROWSER_NET_EVICTED_DOMAIN_COOKIE_COUNTER_H_ |
OLD | NEW |