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 #include "net/cookies/evicted_domain_cookie_counter.h" | |
6 | |
7 #include <queue> | |
8 #include <vector> | |
9 | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/string_util.h" | |
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
14 #include "net/cookies/canonical_cookie.h" | |
15 | |
16 namespace net { | |
17 | |
18 using base::Time; | |
19 using base::TimeDelta; | |
20 | |
21 namespace { | |
22 | |
23 const size_t kMaxEvictedDomainCookies = 500; | |
24 const size_t kPurgeEvictedDomainCookies = 100; | |
25 | |
26 class DelegateImpl : public EvictedDomainCookieCounter::Delegate { | |
27 public: | |
28 DelegateImpl(); | |
29 | |
30 // EvictedDomainCookieCounter::Delegate implementation. | |
31 virtual void Report(const EvictedDomainCookieCounter::EvictedCookie& ec, | |
32 const Time& reinstatement_time) OVERRIDE; | |
33 virtual Time CurrentTime() OVERRIDE; | |
34 }; | |
35 | |
36 DelegateImpl::DelegateImpl() {} | |
37 | |
38 void DelegateImpl::Report(const EvictedDomainCookieCounter::EvictedCookie& ec, | |
39 const Time& reinstatement_time) { | |
40 TimeDelta reinstatement_delay(reinstatement_time - ec.eviction_time_); | |
41 // Need to duplicate HISTOGRAM_CUSTOM_TIMES(), since it is a macro that | |
42 // defines a static variable. | |
43 if (ec.is_google_) { | |
44 HISTOGRAM_CUSTOM_TIMES("Cookie.ReinstatedCookiesGoogle", | |
45 reinstatement_delay, | |
46 TimeDelta::FromSeconds(1), | |
47 TimeDelta::FromDays(7), | |
48 50); | |
49 } else { | |
50 HISTOGRAM_CUSTOM_TIMES("Cookie.ReinstatedCookiesOther", | |
51 reinstatement_delay, | |
52 TimeDelta::FromSeconds(1), | |
53 TimeDelta::FromDays(7), | |
54 50); | |
55 } | |
56 } | |
57 | |
58 Time DelegateImpl::CurrentTime() { | |
59 return Time::Now(); | |
60 } | |
61 | |
62 } // namespace | |
63 | |
64 EvictedDomainCookieCounter::EvictedDomainCookieCounter( | |
65 scoped_refptr<CookieMonster::Delegate> next_cookie_monster_delegate) | |
66 : next_cookie_monster_delegate_(next_cookie_monster_delegate), | |
67 delegate_(new DelegateImpl), | |
68 max_size_(kMaxEvictedDomainCookies), | |
69 purge_count_(kPurgeEvictedDomainCookies) { | |
70 } | |
71 | |
72 EvictedDomainCookieCounter::EvictedDomainCookieCounter( | |
73 scoped_refptr<CookieMonster::Delegate> next_cookie_monster_delegate, | |
74 scoped_ptr<Delegate> delegate, | |
75 size_t max_size, | |
76 size_t purge_count) | |
77 : next_cookie_monster_delegate_(next_cookie_monster_delegate), | |
78 delegate_(delegate.Pass()), | |
79 max_size_(max_size), | |
80 purge_count_(purge_count) { | |
81 DCHECK(delegate_); | |
82 DCHECK(purge_count < max_size_); | |
83 } | |
84 | |
85 EvictedDomainCookieCounter::~EvictedDomainCookieCounter() { | |
86 STLDeleteContainerPairSecondPointers(evicted_cookies_.begin(), | |
87 evicted_cookies_.end()); | |
88 } | |
89 | |
90 // static | |
91 // Simplified from google_util.cc: IsGoogleHostname(). | |
92 bool EvictedDomainCookieCounter::UnsafeIsGoogleCookie( | |
93 const CanonicalCookie& cc) { | |
94 const std::string& host(cc.Domain()); | |
95 size_t tld_length = | |
96 RegistryControlledDomainService::GetRegistryLength(host, false); | |
97 if ((tld_length == 0) || (tld_length == std::string::npos)) | |
98 return false; | |
99 | |
100 std::string host_minus_tld(host, 0, host.length() - tld_length); | |
101 return LowerCaseEqualsASCII(host_minus_tld, "google.") || | |
102 EndsWith(host_minus_tld, ".google.", false); | |
103 } | |
104 | |
105 size_t EvictedDomainCookieCounter::GetStorageSize() const { | |
106 return evicted_cookies_.size(); | |
107 } | |
108 | |
109 void EvictedDomainCookieCounter::OnCookieChanged(const CanonicalCookie& cc, | |
110 bool removed, | |
111 ChangeCause cause) { | |
112 EvictedDomainCookieCounter::EvictedCookieKey key(GetKey(cc)); | |
113 Time current(delegate_->CurrentTime()); | |
114 if (removed) { | |
115 if (cause == CookieMonster::Delegate::CHANGE_COOKIE_EVICTED) | |
116 StoreEvictedCookie(key, cc, current); | |
117 else // Explicit deletion, so stop tracking. | |
118 FreeEvictedCookie(key); | |
erikwright (departed)
2013/03/26 16:38:13
On the surface, it would seem like this should nev
huangs
2013/03/27 00:40:26
Removed. I was being paranoid by assuming some sem
| |
119 } else { // Includes adds or updates. | |
120 ProcessNewCookie(key, cc, current); | |
121 } | |
122 | |
123 if (next_cookie_monster_delegate_) | |
124 next_cookie_monster_delegate_->OnCookieChanged(cc, removed, cause); | |
125 } | |
126 | |
127 // static | |
128 EvictedDomainCookieCounter::EvictedCookieKey | |
129 EvictedDomainCookieCounter::GetKey(const CanonicalCookie& cc) { | |
130 return cc.Domain() + ";" + cc.Path() + ";" + cc.Name(); | |
131 } | |
132 | |
133 void EvictedDomainCookieCounter::FreeEvictedCookie( | |
134 const EvictedCookieKey& key) { | |
135 EvictedCookieMap::iterator it = evicted_cookies_.find(key); | |
136 if (it != evicted_cookies_.end()) { | |
137 delete it->second; | |
138 evicted_cookies_.erase(it); | |
139 } | |
140 } | |
141 | |
142 // |current| is passed by value, in case it came from a to-be-deleted element. | |
erikwright (departed)
2013/03/26 16:38:13
Since this is a private method and you know it doe
huangs
2013/03/27 00:40:26
Makes sense. Changed to const base::Time. Also,
| |
143 void EvictedDomainCookieCounter::FreeExpiredEvictedCookies(Time current) { | |
144 EvictedCookieMap::iterator it = evicted_cookies_.begin(); | |
145 while (it != evicted_cookies_.end()) { | |
146 if (it->second->is_expired(current)) { | |
147 delete it->second; | |
148 evicted_cookies_.erase(it++); // Post-increment idiom. | |
149 } else { | |
150 ++it; | |
151 } | |
152 } | |
153 } | |
154 | |
155 void EvictedDomainCookieCounter::GarbageCollect(const Time& current) { | |
156 if (evicted_cookies_.size() <= max_size_) | |
157 return; | |
158 | |
159 // From |evicted_cookies_|, removed all expired cookies, and remove cookies | |
160 // with the oldest |eviction_time_| until |size_goal| is attained. | |
161 size_t size_goal = max_size_ - purge_count_; | |
162 // Bound on number of non-expired cookies to remove. | |
163 size_t remove_quota = evicted_cookies_.size() - size_goal; | |
164 DCHECK(remove_quota > 0); | |
165 | |
166 typedef EvictedDomainCookieCounter::EvictedCookie* EvictedCookiePtr; | |
167 // Comparator for a priority_queue<> whose top() element specifies the most | |
168 // recently evicted cookie. | |
169 struct EvictedCookieComparator { | |
170 bool operator() (EvictedCookiePtr ec1, EvictedCookiePtr ec2) { | |
171 return ec1->eviction_time_ < ec2->eviction_time_; | |
172 } | |
173 }; | |
174 | |
175 // Use priority queue to store oldest cookies, resize as necessary. | |
176 std::priority_queue<EvictedCookiePtr, std::vector<EvictedCookiePtr>, | |
177 EvictedCookieComparator> oldest_set; | |
178 for (EvictedCookieMap::iterator it = evicted_cookies_.begin(); | |
179 it != evicted_cookies_.end(); ++it) { | |
180 if (it->second->is_expired(current)) // Will be removed, so decrease quota. | |
181 remove_quota = remove_quota ? remove_quota - 1 : 0; // Cap to 0 (size_t). | |
182 else | |
183 oldest_set.push(it->second); | |
184 | |
185 if (oldest_set.size() > remove_quota) | |
186 oldest_set.pop(); // Spare the youngest element from deletion. | |
erikwright (departed)
2013/03/26 16:38:13
Alternatively, I suppose you could reverse the com
huangs
2013/03/27 00:40:26
The old method saves more memory, but it turns out
huangs
2013/03/27 00:40:26
Simulation:
import math
def lg(n):
return math.l
huangs
2013/03/27 00:40:26
Instead of using priority_queue<>, may as well use
| |
187 } | |
188 DCHECK(oldest_set.size() <= remove_quota); | |
189 | |
190 // Mark oldest non-expired cookies as expired, for removal. | |
191 for (; !oldest_set.empty(); oldest_set.pop()) | |
192 oldest_set.top()->set_expired(); | |
erikwright (departed)
2013/03/26 16:38:13
Here (combined with FreeExpiredEvictedCookies) we
huangs
2013/03/27 00:40:26
Cool. Doing this, except I'm now using vector<T>!
| |
193 | |
194 FreeExpiredEvictedCookies(current); | |
195 // Apply stricter check if non-expired cookies were deleted. | |
196 DCHECK(remove_quota ? evicted_cookies_.size() == size_goal : | |
197 evicted_cookies_.size() <= size_goal); | |
198 } | |
199 | |
200 void EvictedDomainCookieCounter::StoreEvictedCookie(const EvictedCookieKey& key, | |
201 const CanonicalCookie& cc, | |
202 const Time& current) { | |
203 if (!cc.IsExpired(current)) { | |
erikwright (departed)
2013/03/26 16:38:13
This should also only happen if the cause is CHANG
huangs
2013/03/27 00:40:26
Removed.
| |
204 FreeEvictedCookie(key); // Deallocate explicitly. | |
erikwright (departed)
2013/03/26 16:38:13
std::map::insert returns a pair of iterator/bool.
huangs
2013/03/27 00:40:26
Ah, neat. :) Done.
| |
205 | |
206 EvictedCookie* ec = | |
207 new EvictedCookie(current, cc.ExpiryDate(), UnsafeIsGoogleCookie(cc)); | |
208 evicted_cookies_.insert(EvictedCookieMap::value_type(key, ec)); | |
209 | |
210 GarbageCollect(current); | |
211 } | |
212 } | |
213 | |
214 void EvictedDomainCookieCounter::ProcessNewCookie(const EvictedCookieKey& key, | |
215 const CanonicalCookie& cc, | |
216 const Time& current) { | |
217 EvictedCookieMap::iterator it = evicted_cookies_.find(key); | |
218 if (it != evicted_cookies_.end()) { | |
219 if (!it->second->is_expired(current)) | |
220 delegate_->Report(*it->second, current); // Reinstatement. | |
221 delete it->second; | |
222 evicted_cookies_.erase(it); | |
223 } | |
224 } | |
225 | |
226 } // namespace net | |
OLD | NEW |