OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Brought to you by the letter D and the number 2. | 5 // Provides a temporary redirection while clients are updated to use the new |
| 6 // path. |
6 | 7 |
7 #ifndef NET_BASE_COOKIE_MONSTER_H_ | 8 #ifndef NET_BASE_COOKIE_MONSTER_H_ |
8 #define NET_BASE_COOKIE_MONSTER_H_ | 9 #define NET_BASE_COOKIE_MONSTER_H_ |
9 #pragma once | 10 #pragma once |
10 | 11 |
11 #include <deque> | 12 #include "net/cookies/cookie_monster.h" |
12 #include <map> | |
13 #include <queue> | |
14 #include <set> | |
15 #include <string> | |
16 #include <utility> | |
17 #include <vector> | |
18 | |
19 #include "base/basictypes.h" | |
20 #include "base/callback_forward.h" | |
21 #include "base/gtest_prod_util.h" | |
22 #include "base/memory/ref_counted.h" | |
23 #include "base/memory/scoped_ptr.h" | |
24 #include "base/synchronization/lock.h" | |
25 #include "base/time.h" | |
26 #include "net/base/cookie_store.h" | |
27 #include "net/base/net_export.h" | |
28 | |
29 class GURL; | |
30 | |
31 namespace base { | |
32 class Histogram; | |
33 class TimeTicks; | |
34 } // namespace base | |
35 | |
36 namespace net { | |
37 | |
38 class CookieList; | |
39 | |
40 // The cookie monster is the system for storing and retrieving cookies. It has | |
41 // an in-memory list of all cookies, and synchronizes non-session cookies to an | |
42 // optional permanent storage that implements the PersistentCookieStore | |
43 // interface. | |
44 // | |
45 // This class IS thread-safe. Normally, it is only used on the I/O thread, but | |
46 // is also accessed directly through Automation for UI testing. | |
47 // | |
48 // All cookie tasks are handled asynchronously. Tasks may be deferred if | |
49 // all affected cookies are not yet loaded from the backing store. Otherwise, | |
50 // the callback may be invoked immediately (prior to return of the asynchronous | |
51 // function). | |
52 // | |
53 // A cookie task is either pending loading of the entire cookie store, or | |
54 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the | |
55 // cookie task will be queued in queue_ while PersistentCookieStore chain loads | |
56 // the cookie store on DB thread. In the latter case, the cookie task will be | |
57 // queued in tasks_queued_ while PermanentCookieStore loads cookies for the | |
58 // specified domain key(eTLD+1) on DB thread. | |
59 // | |
60 // Callbacks are guaranteed to be invoked on the calling thread. | |
61 // | |
62 // TODO(deanm) Implement CookieMonster, the cookie database. | |
63 // - Verify that our domain enforcement and non-dotted handling is correct | |
64 class NET_EXPORT CookieMonster : public CookieStore { | |
65 public: | |
66 class CanonicalCookie; | |
67 class Delegate; | |
68 class ParsedCookie; | |
69 class PersistentCookieStore; | |
70 | |
71 // Terminology: | |
72 // * The 'top level domain' (TLD) of an internet domain name is | |
73 // the terminal "." free substring (e.g. "com" for google.com | |
74 // or world.std.com). | |
75 // * The 'effective top level domain' (eTLD) is the longest | |
76 // "." initiated terminal substring of an internet domain name | |
77 // that is controlled by a general domain registrar. | |
78 // (e.g. "co.uk" for news.bbc.co.uk). | |
79 // * The 'effective top level domain plus one' (eTLD+1) is the | |
80 // shortest "." delimited terminal substring of an internet | |
81 // domain name that is not controlled by a general domain | |
82 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or | |
83 // "google.com" for news.google.com). The general assumption | |
84 // is that all hosts and domains under an eTLD+1 share some | |
85 // administrative control. | |
86 | |
87 // CookieMap is the central data structure of the CookieMonster. It | |
88 // is a map whose values are pointers to CanonicalCookie data | |
89 // structures (the data structures are owned by the CookieMonster | |
90 // and must be destroyed when removed from the map). The key is based on the | |
91 // effective domain of the cookies. If the domain of the cookie has an | |
92 // eTLD+1, that is the key for the map. If the domain of the cookie does not | |
93 // have an eTLD+1, the key of the map is the host the cookie applies to (it is | |
94 // not legal to have domain cookies without an eTLD+1). This rule | |
95 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork". | |
96 // This behavior is the same as the behavior in Firefox v 3.6.10. | |
97 | |
98 // NOTE(deanm): | |
99 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy | |
100 // so it would seem like hashing would help. However they were very | |
101 // close, with multimap being a tiny bit faster. I think this is because | |
102 // our map is at max around 1000 entries, and the additional complexity | |
103 // for the hashing might not overcome the O(log(1000)) for querying | |
104 // a multimap. Also, multimap is standard, another reason to use it. | |
105 // TODO(rdsmith): This benchmark should be re-done now that we're allowing | |
106 // subtantially more entries in the map. | |
107 typedef std::multimap<std::string, CanonicalCookie*> CookieMap; | |
108 typedef std::pair<CookieMap::iterator, CookieMap::iterator> CookieMapItPair; | |
109 | |
110 // The store passed in should not have had Init() called on it yet. This | |
111 // class will take care of initializing it. The backing store is NOT owned by | |
112 // this class, but it must remain valid for the duration of the cookie | |
113 // monster's existence. If |store| is NULL, then no backing store will be | |
114 // updated. If |delegate| is non-NULL, it will be notified on | |
115 // creation/deletion of cookies. | |
116 CookieMonster(PersistentCookieStore* store, Delegate* delegate); | |
117 | |
118 // Only used during unit testing. | |
119 CookieMonster(PersistentCookieStore* store, | |
120 Delegate* delegate, | |
121 int last_access_threshold_milliseconds); | |
122 | |
123 // Parses the string with the cookie time (very forgivingly). | |
124 static base::Time ParseCookieTime(const std::string& time_string); | |
125 | |
126 // Helper function that adds all cookies from |list| into this instance. | |
127 bool InitializeFrom(const CookieList& list); | |
128 | |
129 typedef base::Callback<void(const CookieList& cookies)> GetCookieListCallback; | |
130 typedef base::Callback<void(bool success)> DeleteCookieCallback; | |
131 | |
132 // Sets a cookie given explicit user-provided cookie attributes. The cookie | |
133 // name, value, domain, etc. are each provided as separate strings. This | |
134 // function expects each attribute to be well-formed. It will check for | |
135 // disallowed characters (e.g. the ';' character is disallowed within the | |
136 // cookie value attribute) and will return false without setting the cookie | |
137 // if such characters are found. | |
138 void SetCookieWithDetailsAsync(const GURL& url, | |
139 const std::string& name, | |
140 const std::string& value, | |
141 const std::string& domain, | |
142 const std::string& path, | |
143 const base::Time& expiration_time, | |
144 bool secure, bool http_only, | |
145 const SetCookiesCallback& callback); | |
146 | |
147 | |
148 // Returns all the cookies, for use in management UI, etc. This does not mark | |
149 // the cookies as having been accessed. | |
150 // The returned cookies are ordered by longest path, then by earliest | |
151 // creation date. | |
152 void GetAllCookiesAsync(const GetCookieListCallback& callback); | |
153 | |
154 // Returns all the cookies, for use in management UI, etc. Filters results | |
155 // using given url scheme, host / domain and path and options. This does not | |
156 // mark the cookies as having been accessed. | |
157 // The returned cookies are ordered by longest path, then earliest | |
158 // creation date. | |
159 void GetAllCookiesForURLWithOptionsAsync( | |
160 const GURL& url, | |
161 const CookieOptions& options, | |
162 const GetCookieListCallback& callback); | |
163 | |
164 // Invokes GetAllCookiesForURLWithOptions with options set to include HTTP | |
165 // only cookies. | |
166 void GetAllCookiesForURLAsync(const GURL& url, | |
167 const GetCookieListCallback& callback); | |
168 | |
169 // Deletes all of the cookies. | |
170 void DeleteAllAsync(const DeleteCallback& callback); | |
171 | |
172 // Deletes all cookies that match the host of the given URL | |
173 // regardless of path. This includes all http_only and secure cookies, | |
174 // but does not include any domain cookies that may apply to this host. | |
175 // Returns the number of cookies deleted. | |
176 void DeleteAllForHostAsync(const GURL& url, | |
177 const DeleteCallback& callback); | |
178 | |
179 // Deletes one specific cookie. | |
180 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, | |
181 const DeleteCookieCallback& callback); | |
182 | |
183 // Override the default list of schemes that are allowed to be set in | |
184 // this cookie store. Calling his overrides the value of | |
185 // "enable_file_scheme_". | |
186 // If this this method is called, it must be called before first use of | |
187 // the instance (i.e. as part of the instance initialization process). | |
188 void SetCookieableSchemes(const char* schemes[], size_t num_schemes); | |
189 | |
190 // Instructs the cookie monster to not delete expired cookies. This is used | |
191 // in cases where the cookie monster is used as a data structure to keep | |
192 // arbitrary cookies. | |
193 void SetKeepExpiredCookies(); | |
194 | |
195 // Delegates the call to set the |clear_local_store_on_exit_| flag of the | |
196 // PersistentStore if it exists. | |
197 void SetClearPersistentStoreOnExit(bool clear_local_store); | |
198 | |
199 // There are some unknowns about how to correctly handle file:// cookies, | |
200 // and our implementation for this is not robust enough. This allows you | |
201 // to enable support, but it should only be used for testing. Bug 1157243. | |
202 // Must be called before creating a CookieMonster instance. | |
203 static void EnableFileScheme(); | |
204 | |
205 // Flush the backing store (if any) to disk and post the given callback when | |
206 // done. | |
207 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE. | |
208 // It may be posted to the current thread, or it may run on the thread that | |
209 // actually does the flushing. Your Task should generally post a notification | |
210 // to the thread you actually want to be notified on. | |
211 void FlushStore(const base::Closure& callback); | |
212 | |
213 // CookieStore implementation. | |
214 | |
215 // Sets the cookies specified by |cookie_list| returned from |url| | |
216 // with options |options| in effect. | |
217 virtual void SetCookieWithOptionsAsync( | |
218 const GURL& url, | |
219 const std::string& cookie_line, | |
220 const CookieOptions& options, | |
221 const SetCookiesCallback& callback) OVERRIDE; | |
222 | |
223 // Gets all cookies that apply to |url| given |options|. | |
224 // The returned cookies are ordered by longest path, then earliest | |
225 // creation date. | |
226 virtual void GetCookiesWithOptionsAsync( | |
227 const GURL& url, | |
228 const CookieOptions& options, | |
229 const GetCookiesCallback& callback) OVERRIDE; | |
230 | |
231 virtual void GetCookiesWithInfoAsync( | |
232 const GURL& url, | |
233 const CookieOptions& options, | |
234 const GetCookieInfoCallback& callback) OVERRIDE; | |
235 | |
236 // Deletes all cookies with that might apply to |url| that has |cookie_name|. | |
237 virtual void DeleteCookieAsync( | |
238 const GURL& url, const std::string& cookie_name, | |
239 const base::Closure& callback) OVERRIDE; | |
240 | |
241 // Deletes all of the cookies that have a creation_date greater than or equal | |
242 // to |delete_begin| and less than |delete_end| | |
243 // Returns the number of cookies that have been deleted. | |
244 virtual void DeleteAllCreatedBetweenAsync( | |
245 const base::Time& delete_begin, | |
246 const base::Time& delete_end, | |
247 const DeleteCallback& callback) OVERRIDE; | |
248 | |
249 virtual CookieMonster* GetCookieMonster() OVERRIDE; | |
250 | |
251 // Enables writing session cookies into the cookie database. If this this | |
252 // method is called, it must be called before first use of the instance | |
253 // (i.e. as part of the instance initialization process). | |
254 void SetPersistSessionCookies(bool persist_session_cookies); | |
255 | |
256 // Protects session cookies from deletion on shutdown. | |
257 void SaveSessionCookies(); | |
258 | |
259 // Debugging method to perform various validation checks on the map. | |
260 // Currently just checking that there are no null CanonicalCookie pointers | |
261 // in the map. | |
262 // Argument |arg| is to allow retaining of arbitrary data if the CHECKs | |
263 // in the function trip. TODO(rdsmith):Remove hack. | |
264 void ValidateMap(int arg); | |
265 | |
266 // The default list of schemes the cookie monster can handle. | |
267 static const char* kDefaultCookieableSchemes[]; | |
268 static const int kDefaultCookieableSchemesCount; | |
269 | |
270 private: | |
271 // For queueing the cookie monster calls. | |
272 class CookieMonsterTask; | |
273 class DeleteAllCreatedBetweenTask; | |
274 class DeleteAllForHostTask; | |
275 class DeleteAllTask; | |
276 class DeleteCookieTask; | |
277 class DeleteCanonicalCookieTask; | |
278 class GetAllCookiesForURLWithOptionsTask; | |
279 class GetAllCookiesTask; | |
280 class GetCookiesWithOptionsTask; | |
281 class GetCookiesWithInfoTask; | |
282 class SetCookieWithDetailsTask; | |
283 class SetCookieWithOptionsTask; | |
284 | |
285 // Testing support. | |
286 // For SetCookieWithCreationTime. | |
287 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, | |
288 TestCookieDeleteAllCreatedBetweenTimestamps); | |
289 | |
290 // For gargage collection constants. | |
291 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); | |
292 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection); | |
293 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); | |
294 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); | |
295 | |
296 // For validation of key values. | |
297 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); | |
298 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); | |
299 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey); | |
300 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey); | |
301 | |
302 // For FindCookiesForKey. | |
303 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies); | |
304 | |
305 // Internal reasons for deletion, used to populate informative histograms | |
306 // and to provide a public cause for onCookieChange notifications. | |
307 // | |
308 // If you add or remove causes from this list, please be sure to also update | |
309 // the Delegate::ChangeCause mapping inside ChangeCauseMapping. Moreover, | |
310 // these are used as array indexes, so avoid reordering to keep the | |
311 // histogram buckets consistent. New items (if necessary) should be added | |
312 // at the end of the list, just before DELETE_COOKIE_LAST_ENTRY. | |
313 enum DeletionCause { | |
314 DELETE_COOKIE_EXPLICIT = 0, | |
315 DELETE_COOKIE_OVERWRITE, | |
316 DELETE_COOKIE_EXPIRED, | |
317 DELETE_COOKIE_EVICTED, | |
318 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE, | |
319 DELETE_COOKIE_DONT_RECORD, // e.g. For final cleanup after flush to store. | |
320 DELETE_COOKIE_EVICTED_DOMAIN, | |
321 DELETE_COOKIE_EVICTED_GLOBAL, | |
322 | |
323 // Cookies evicted during domain level garbage collection that | |
324 // were accessed longer ago than kSafeFromGlobalPurgeDays | |
325 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, | |
326 | |
327 // Cookies evicted during domain level garbage collection that | |
328 // were accessed more recently than kSafeFromGlobalPurgeDays | |
329 // (and thus would have been preserved by global garbage collection). | |
330 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, | |
331 | |
332 // A common idiom is to remove a cookie by overwriting it with an | |
333 // already-expired expiration date. This captures that case. | |
334 DELETE_COOKIE_EXPIRED_OVERWRITE, | |
335 | |
336 DELETE_COOKIE_LAST_ENTRY | |
337 }; | |
338 | |
339 // Cookie garbage collection thresholds. Based off of the Mozilla defaults. | |
340 // When the number of cookies gets to k{Domain,}MaxCookies | |
341 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies. | |
342 // It might seem scary to have a high purge value, but really it's not. | |
343 // You just make sure that you increase the max to cover the increase | |
344 // in purge, and we would have been purging the same amount of cookies. | |
345 // We're just going through the garbage collection process less often. | |
346 // Note that the DOMAIN values are per eTLD+1; see comment for the | |
347 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for | |
348 // google.com and all of its subdomains will be 150-180. | |
349 // | |
350 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not | |
351 // be evicted by global garbage collection, even if we have more than | |
352 // kMaxCookies. This does not affect domain garbage collection. | |
353 // | |
354 // Present in .h file to make accessible to tests through FRIEND_TEST. | |
355 // Actual definitions are in cookie_monster.cc. | |
356 static const size_t kDomainMaxCookies; | |
357 static const size_t kDomainPurgeCookies; | |
358 static const size_t kMaxCookies; | |
359 static const size_t kPurgeCookies; | |
360 | |
361 // The number of days since last access that cookies will not be subject | |
362 // to global garbage collection. | |
363 static const int kSafeFromGlobalPurgeDays; | |
364 | |
365 // Record statistics every kRecordStatisticsIntervalSeconds of uptime. | |
366 static const int kRecordStatisticsIntervalSeconds = 10 * 60; | |
367 | |
368 virtual ~CookieMonster(); | |
369 | |
370 // The following are synchronous calls to which the asynchronous methods | |
371 // delegate either immediately (if the store is loaded) or through a deferred | |
372 // task (if the store is not yet loaded). | |
373 bool SetCookieWithDetails(const GURL& url, | |
374 const std::string& name, | |
375 const std::string& value, | |
376 const std::string& domain, | |
377 const std::string& path, | |
378 const base::Time& expiration_time, | |
379 bool secure, bool http_only); | |
380 | |
381 CookieList GetAllCookies(); | |
382 | |
383 CookieList GetAllCookiesForURLWithOptions(const GURL& url, | |
384 const CookieOptions& options); | |
385 | |
386 CookieList GetAllCookiesForURL(const GURL& url); | |
387 | |
388 int DeleteAll(bool sync_to_store); | |
389 | |
390 int DeleteAllCreatedBetween(const base::Time& delete_begin, | |
391 const base::Time& delete_end); | |
392 | |
393 int DeleteAllForHost(const GURL& url); | |
394 | |
395 bool DeleteCanonicalCookie(const CanonicalCookie& cookie); | |
396 | |
397 bool SetCookieWithOptions(const GURL& url, | |
398 const std::string& cookie_line, | |
399 const CookieOptions& options); | |
400 | |
401 std::string GetCookiesWithOptions(const GURL& url, | |
402 const CookieOptions& options); | |
403 | |
404 void GetCookiesWithInfo(const GURL& url, | |
405 const CookieOptions& options, | |
406 std::string* cookie_line, | |
407 std::vector<CookieInfo>* cookie_infos); | |
408 | |
409 void DeleteCookie(const GURL& url, const std::string& cookie_name); | |
410 | |
411 bool SetCookieWithCreationTime(const GURL& url, | |
412 const std::string& cookie_line, | |
413 const base::Time& creation_time); | |
414 | |
415 // Called by all non-static functions to ensure that the cookies store has | |
416 // been initialized. This is not done during creating so it doesn't block | |
417 // the window showing. | |
418 // Note: this method should always be called with lock_ held. | |
419 void InitIfNecessary() { | |
420 if (!initialized_) { | |
421 if (store_) { | |
422 InitStore(); | |
423 } else { | |
424 loaded_ = true; | |
425 } | |
426 initialized_ = true; | |
427 } | |
428 } | |
429 | |
430 // Initializes the backing store and reads existing cookies from it. | |
431 // Should only be called by InitIfNecessary(). | |
432 void InitStore(); | |
433 | |
434 // Stores cookies loaded from the backing store and invokes any deferred | |
435 // calls. |beginning_time| should be the moment PersistentCookieStore::Load | |
436 // was invoked and is used for reporting histogram_time_blocked_on_load_. | |
437 // See PersistentCookieStore::Load for details on the contents of cookies. | |
438 void OnLoaded(base::TimeTicks beginning_time, | |
439 const std::vector<CanonicalCookie*>& cookies); | |
440 | |
441 // Stores cookies loaded from the backing store and invokes the deferred | |
442 // task(s) pending loading of cookies associated with the domain key | |
443 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been | |
444 // loaded from DB. See PersistentCookieStore::Load for details on the contents | |
445 // of cookies. | |
446 void OnKeyLoaded( | |
447 const std::string& key, | |
448 const std::vector<CanonicalCookie*>& cookies); | |
449 | |
450 // Stores the loaded cookies. | |
451 void StoreLoadedCookies(const std::vector<CanonicalCookie*>& cookies); | |
452 | |
453 // Invokes deferred calls. | |
454 void InvokeQueue(); | |
455 | |
456 // Checks that |cookies_| matches our invariants, and tries to repair any | |
457 // inconsistencies. (In other words, it does not have duplicate cookies). | |
458 void EnsureCookiesMapIsValid(); | |
459 | |
460 // Checks for any duplicate cookies for CookieMap key |key| which lie between | |
461 // |begin| and |end|. If any are found, all but the most recent are deleted. | |
462 // Returns the number of duplicate cookies that were deleted. | |
463 int TrimDuplicateCookiesForKey(const std::string& key, | |
464 CookieMap::iterator begin, | |
465 CookieMap::iterator end); | |
466 | |
467 void SetDefaultCookieableSchemes(); | |
468 | |
469 void FindCookiesForHostAndDomain(const GURL& url, | |
470 const CookieOptions& options, | |
471 bool update_access_time, | |
472 std::vector<CanonicalCookie*>* cookies); | |
473 | |
474 void FindCookiesForKey(const std::string& key, | |
475 const GURL& url, | |
476 const CookieOptions& options, | |
477 const base::Time& current, | |
478 bool update_access_time, | |
479 std::vector<CanonicalCookie*>* cookies); | |
480 | |
481 // Delete any cookies that are equivalent to |ecc| (same path, domain, etc). | |
482 // If |skip_httponly| is true, httponly cookies will not be deleted. The | |
483 // return value with be true if |skip_httponly| skipped an httponly cookie. | |
484 // |key| is the key to find the cookie in cookies_; see the comment before | |
485 // the CookieMap typedef for details. | |
486 // NOTE: There should never be more than a single matching equivalent cookie. | |
487 bool DeleteAnyEquivalentCookie(const std::string& key, | |
488 const CanonicalCookie& ecc, | |
489 bool skip_httponly, | |
490 bool already_expired); | |
491 | |
492 // Takes ownership of *cc. | |
493 void InternalInsertCookie(const std::string& key, | |
494 CanonicalCookie* cc, | |
495 bool sync_to_store); | |
496 | |
497 // Helper function that sets cookies with more control. | |
498 // Not exposed as we don't want callers to have the ability | |
499 // to specify (potentially duplicate) creation times. | |
500 bool SetCookieWithCreationTimeAndOptions(const GURL& url, | |
501 const std::string& cookie_line, | |
502 const base::Time& creation_time, | |
503 const CookieOptions& options); | |
504 | |
505 // Helper function that sets a canonical cookie, deleting equivalents and | |
506 // performing garbage collection. | |
507 bool SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc, | |
508 const base::Time& creation_time, | |
509 const CookieOptions& options); | |
510 | |
511 void InternalUpdateCookieAccessTime(CanonicalCookie* cc, | |
512 const base::Time& current_time); | |
513 | |
514 // |deletion_cause| argument is used for collecting statistics and choosing | |
515 // the correct Delegate::ChangeCause for OnCookieChanged notifications. | |
516 void InternalDeleteCookie(CookieMap::iterator it, bool sync_to_store, | |
517 DeletionCause deletion_cause); | |
518 | |
519 // If the number of cookies for CookieMap key |key|, or globally, are | |
520 // over the preset maximums above, garbage collect, first for the host and | |
521 // then globally. See comments above garbage collection threshold | |
522 // constants for details. | |
523 // | |
524 // Returns the number of cookies deleted (useful for debugging). | |
525 int GarbageCollect(const base::Time& current, const std::string& key); | |
526 | |
527 // Helper for GarbageCollect(); can be called directly as well. Deletes | |
528 // all expired cookies in |itpair|. If |cookie_its| is non-NULL, it is | |
529 // populated with all the non-expired cookies from |itpair|. | |
530 // | |
531 // Returns the number of cookies deleted. | |
532 int GarbageCollectExpired(const base::Time& current, | |
533 const CookieMapItPair& itpair, | |
534 std::vector<CookieMap::iterator>* cookie_its); | |
535 | |
536 // Helper for GarbageCollect(). Deletes all cookies in the list | |
537 // that were accessed before |keep_accessed_after|, using DeletionCause | |
538 // |cause|. If |keep_accessed_after| is null, deletes all cookies in the | |
539 // list. Returns the number of cookies deleted. | |
540 int GarbageCollectDeleteList(const base::Time& current, | |
541 const base::Time& keep_accessed_after, | |
542 DeletionCause cause, | |
543 std::vector<CookieMap::iterator>& cookie_its); | |
544 | |
545 // Find the key (for lookup in cookies_) based on the given domain. | |
546 // See comment on keys before the CookieMap typedef. | |
547 std::string GetKey(const std::string& domain) const; | |
548 | |
549 bool HasCookieableScheme(const GURL& url); | |
550 | |
551 // Statistics support | |
552 | |
553 // This function should be called repeatedly, and will record | |
554 // statistics if a sufficient time period has passed. | |
555 void RecordPeriodicStats(const base::Time& current_time); | |
556 | |
557 // Initialize the above variables; should only be called from | |
558 // the constructor. | |
559 void InitializeHistograms(); | |
560 | |
561 // The resolution of our time isn't enough, so we do something | |
562 // ugly and increment when we've seen the same time twice. | |
563 base::Time CurrentTime(); | |
564 | |
565 // Runs the task if, or defers the task until, the full cookie database is | |
566 // loaded. | |
567 void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item); | |
568 | |
569 // Runs the task if, or defers the task until, the cookies for the given URL | |
570 // are loaded. | |
571 void DoCookieTaskForURL(const scoped_refptr<CookieMonsterTask>& task_item, | |
572 const GURL& url); | |
573 | |
574 // Histogram variables; see CookieMonster::InitializeHistograms() in | |
575 // cookie_monster.cc for details. | |
576 base::Histogram* histogram_expiration_duration_minutes_; | |
577 base::Histogram* histogram_between_access_interval_minutes_; | |
578 base::Histogram* histogram_evicted_last_access_minutes_; | |
579 base::Histogram* histogram_count_; | |
580 base::Histogram* histogram_domain_count_; | |
581 base::Histogram* histogram_etldp1_count_; | |
582 base::Histogram* histogram_domain_per_etldp1_count_; | |
583 base::Histogram* histogram_number_duplicate_db_cookies_; | |
584 base::Histogram* histogram_cookie_deletion_cause_; | |
585 base::Histogram* histogram_time_get_; | |
586 base::Histogram* histogram_time_mac_; | |
587 base::Histogram* histogram_time_blocked_on_load_; | |
588 | |
589 CookieMap cookies_; | |
590 | |
591 // Indicates whether the cookie store has been initialized. This happens | |
592 // lazily in InitStoreIfNecessary(). | |
593 bool initialized_; | |
594 | |
595 // Indicates whether loading from the backend store is completed and | |
596 // calls may be immediately processed. | |
597 bool loaded_; | |
598 | |
599 // List of domain keys that have been loaded from the DB. | |
600 std::set<std::string> keys_loaded_; | |
601 | |
602 // Map of domain keys to their associated task queues. These tasks are blocked | |
603 // until all cookies for the associated domain key eTLD+1 are loaded from the | |
604 // backend store. | |
605 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > | |
606 tasks_queued_; | |
607 | |
608 // Queues tasks that are blocked until all cookies are loaded from the backend | |
609 // store. | |
610 std::queue<scoped_refptr<CookieMonsterTask> > queue_; | |
611 | |
612 scoped_refptr<PersistentCookieStore> store_; | |
613 | |
614 base::Time last_time_seen_; | |
615 | |
616 // Minimum delay after updating a cookie's LastAccessDate before we will | |
617 // update it again. | |
618 const base::TimeDelta last_access_threshold_; | |
619 | |
620 // Approximate date of access time of least recently accessed cookie | |
621 // in |cookies_|. Note that this is not guaranteed to be accurate, only a) | |
622 // to be before or equal to the actual time, and b) to be accurate | |
623 // immediately after a garbage collection that scans through all the cookies. | |
624 // This value is used to determine whether global garbage collection might | |
625 // find cookies to purge. | |
626 // Note: The default Time() constructor will create a value that compares | |
627 // earlier than any other time value, which is wanted. Thus this | |
628 // value is not initialized. | |
629 base::Time earliest_access_time_; | |
630 | |
631 // During loading, holds the set of all loaded cookie creation times. Used to | |
632 // avoid ever letting cookies with duplicate creation times into the store; | |
633 // that way we don't have to worry about what sections of code are safe | |
634 // to call while it's in that state. | |
635 std::set<int64> creation_times_; | |
636 | |
637 std::vector<std::string> cookieable_schemes_; | |
638 | |
639 scoped_refptr<Delegate> delegate_; | |
640 | |
641 // Lock for thread-safety | |
642 base::Lock lock_; | |
643 | |
644 base::Time last_statistic_record_time_; | |
645 | |
646 bool keep_expired_cookies_; | |
647 bool persist_session_cookies_; | |
648 | |
649 static bool enable_file_scheme_; | |
650 | |
651 DISALLOW_COPY_AND_ASSIGN(CookieMonster); | |
652 }; | |
653 | |
654 class NET_EXPORT CookieMonster::CanonicalCookie { | |
655 public: | |
656 | |
657 // These constructors do no validation or canonicalization of their inputs; | |
658 // the resulting CanonicalCookies should not be relied on to be canonical | |
659 // unless the caller has done appropriate validation and canonicalization | |
660 // themselves. | |
661 CanonicalCookie(); | |
662 CanonicalCookie(const GURL& url, | |
663 const std::string& name, | |
664 const std::string& value, | |
665 const std::string& domain, | |
666 const std::string& path, | |
667 const std::string& mac_key, | |
668 const std::string& mac_algorithm, | |
669 const base::Time& creation, | |
670 const base::Time& expiration, | |
671 const base::Time& last_access, | |
672 bool secure, | |
673 bool httponly, | |
674 bool has_expires, | |
675 bool is_persistent); | |
676 | |
677 // This constructor does canonicalization but not validation. | |
678 // The result of this constructor should not be relied on in contexts | |
679 // in which pre-validation of the ParsedCookie has not been done. | |
680 CanonicalCookie(const GURL& url, const ParsedCookie& pc); | |
681 | |
682 ~CanonicalCookie(); | |
683 | |
684 // Supports the default copy constructor. | |
685 | |
686 // Creates a canonical cookie from parsed cookie. | |
687 // Canonicalizes and validates inputs. May return NULL if an attribute | |
688 // value is invalid. | |
689 static CanonicalCookie* Create(const GURL& url, | |
690 const ParsedCookie& pc); | |
691 | |
692 // Creates a canonical cookie from unparsed attribute values. | |
693 // Canonicalizes and validates inputs. May return NULL if an attribute | |
694 // value is invalid. | |
695 static CanonicalCookie* Create(const GURL& url, | |
696 const std::string& name, | |
697 const std::string& value, | |
698 const std::string& domain, | |
699 const std::string& path, | |
700 const std::string& mac_key, | |
701 const std::string& mac_algorithm, | |
702 const base::Time& creation, | |
703 const base::Time& expiration, | |
704 bool secure, | |
705 bool http_only, | |
706 bool is_persistent); | |
707 | |
708 const std::string& Source() const { return source_; } | |
709 const std::string& Name() const { return name_; } | |
710 const std::string& Value() const { return value_; } | |
711 const std::string& Domain() const { return domain_; } | |
712 const std::string& Path() const { return path_; } | |
713 const std::string& MACKey() const { return mac_key_; } | |
714 const std::string& MACAlgorithm() const { return mac_algorithm_; } | |
715 const base::Time& CreationDate() const { return creation_date_; } | |
716 const base::Time& LastAccessDate() const { return last_access_date_; } | |
717 bool DoesExpire() const { return has_expires_; } | |
718 bool IsPersistent() const { return is_persistent_; } | |
719 const base::Time& ExpiryDate() const { return expiry_date_; } | |
720 bool IsSecure() const { return secure_; } | |
721 bool IsHttpOnly() const { return httponly_; } | |
722 bool IsDomainCookie() const { | |
723 return !domain_.empty() && domain_[0] == '.'; } | |
724 bool IsHostCookie() const { return !IsDomainCookie(); } | |
725 | |
726 bool IsExpired(const base::Time& current) { | |
727 return has_expires_ && current >= expiry_date_; | |
728 } | |
729 | |
730 // Are the cookies considered equivalent in the eyes of RFC 2965. | |
731 // The RFC says that name must match (case-sensitive), domain must | |
732 // match (case insensitive), and path must match (case sensitive). | |
733 // For the case insensitive domain compare, we rely on the domain | |
734 // having been canonicalized (in | |
735 // GetCookieDomainWithString->CanonicalizeHost). | |
736 bool IsEquivalent(const CanonicalCookie& ecc) const { | |
737 // It seems like it would make sense to take secure and httponly into | |
738 // account, but the RFC doesn't specify this. | |
739 // NOTE: Keep this logic in-sync with TrimDuplicateCookiesForHost(). | |
740 return (name_ == ecc.Name() && domain_ == ecc.Domain() | |
741 && path_ == ecc.Path()); | |
742 } | |
743 | |
744 void SetLastAccessDate(const base::Time& date) { | |
745 last_access_date_ = date; | |
746 } | |
747 | |
748 bool IsOnPath(const std::string& url_path) const; | |
749 bool IsDomainMatch(const std::string& scheme, const std::string& host) const; | |
750 | |
751 std::string DebugString() const; | |
752 | |
753 // Returns the cookie source when cookies are set for |url|. This function | |
754 // is public for unit test purposes only. | |
755 static std::string GetCookieSourceFromURL(const GURL& url); | |
756 | |
757 private: | |
758 // Gives the session cookie an expiration time if needed | |
759 void SetSessionCookieExpiryTime(); | |
760 | |
761 // The source member of a canonical cookie is the origin of the URL that tried | |
762 // to set this cookie, minus the port number if any. This field is not | |
763 // persistent though; its only used in the in-tab cookies dialog to show the | |
764 // user the source URL. This is used for both allowed and blocked cookies. | |
765 // When a CanonicalCookie is constructed from the backing store (common case) | |
766 // this field will be null. CanonicalCookie consumers should not rely on | |
767 // this field unless they guarantee that the creator of those | |
768 // CanonicalCookies properly initialized the field. | |
769 // TODO(abarth): We might need to make this field persistent for MAC cookies. | |
770 std::string source_; | |
771 std::string name_; | |
772 std::string value_; | |
773 std::string domain_; | |
774 std::string path_; | |
775 std::string mac_key_; // TODO(abarth): Persist to disk. | |
776 std::string mac_algorithm_; // TODO(abarth): Persist to disk. | |
777 base::Time creation_date_; | |
778 base::Time expiry_date_; | |
779 base::Time last_access_date_; | |
780 bool secure_; | |
781 bool httponly_; | |
782 bool has_expires_; | |
783 bool is_persistent_; | |
784 }; | |
785 | |
786 class CookieMonster::Delegate | |
787 : public base::RefCountedThreadSafe<CookieMonster::Delegate> { | |
788 public: | |
789 // The publicly relevant reasons a cookie might be changed. | |
790 enum ChangeCause { | |
791 // The cookie was changed directly by a consumer's action. | |
792 CHANGE_COOKIE_EXPLICIT, | |
793 // The cookie was automatically removed due to an insert operation that | |
794 // overwrote it. | |
795 CHANGE_COOKIE_OVERWRITE, | |
796 // The cookie was automatically removed as it expired. | |
797 CHANGE_COOKIE_EXPIRED, | |
798 // The cookie was automatically evicted during garbage collection. | |
799 CHANGE_COOKIE_EVICTED, | |
800 // The cookie was overwritten with an already-expired expiration date. | |
801 CHANGE_COOKIE_EXPIRED_OVERWRITE | |
802 }; | |
803 | |
804 // Will be called when a cookie is added or removed. The function is passed | |
805 // the respective |cookie| which was added to or removed from the cookies. | |
806 // If |removed| is true, the cookie was deleted, and |cause| will be set | |
807 // to the reason for it's removal. If |removed| is false, the cookie was | |
808 // added, and |cause| will be set to CHANGE_COOKIE_EXPLICIT. | |
809 // | |
810 // As a special case, note that updating a cookie's properties is implemented | |
811 // as a two step process: the cookie to be updated is first removed entirely, | |
812 // generating a notification with cause CHANGE_COOKIE_OVERWRITE. Afterwards, | |
813 // a new cookie is written with the updated values, generating a notification | |
814 // with cause CHANGE_COOKIE_EXPLICIT. | |
815 virtual void OnCookieChanged(const CookieMonster::CanonicalCookie& cookie, | |
816 bool removed, | |
817 ChangeCause cause) = 0; | |
818 protected: | |
819 friend class base::RefCountedThreadSafe<CookieMonster::Delegate>; | |
820 virtual ~Delegate() {} | |
821 }; | |
822 | |
823 class NET_EXPORT CookieMonster::ParsedCookie { | |
824 public: | |
825 typedef std::pair<std::string, std::string> TokenValuePair; | |
826 typedef std::vector<TokenValuePair> PairList; | |
827 | |
828 // The maximum length of a cookie string we will try to parse | |
829 static const size_t kMaxCookieSize = 4096; | |
830 // The maximum number of Token/Value pairs. Shouldn't have more than 8. | |
831 static const int kMaxPairs = 16; | |
832 | |
833 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com" | |
834 ParsedCookie(const std::string& cookie_line); | |
835 ~ParsedCookie(); | |
836 | |
837 // You should not call any other methods on the class if !IsValid | |
838 bool IsValid() const { return is_valid_; } | |
839 | |
840 const std::string& Name() const { return pairs_[0].first; } | |
841 const std::string& Token() const { return Name(); } | |
842 const std::string& Value() const { return pairs_[0].second; } | |
843 | |
844 bool HasPath() const { return path_index_ != 0; } | |
845 const std::string& Path() const { return pairs_[path_index_].second; } | |
846 bool HasDomain() const { return domain_index_ != 0; } | |
847 const std::string& Domain() const { return pairs_[domain_index_].second; } | |
848 bool HasMACKey() const { return mac_key_index_ != 0; } | |
849 const std::string& MACKey() const { return pairs_[mac_key_index_].second; } | |
850 bool HasMACAlgorithm() const { return mac_algorithm_index_ != 0; } | |
851 const std::string& MACAlgorithm() const { | |
852 return pairs_[mac_algorithm_index_].second; | |
853 } | |
854 bool HasExpires() const { return expires_index_ != 0; } | |
855 const std::string& Expires() const { return pairs_[expires_index_].second; } | |
856 bool HasMaxAge() const { return maxage_index_ != 0; } | |
857 const std::string& MaxAge() const { return pairs_[maxage_index_].second; } | |
858 bool IsSecure() const { return secure_index_ != 0; } | |
859 bool IsHttpOnly() const { return httponly_index_ != 0; } | |
860 | |
861 // Returns the number of attributes, for example, returning 2 for: | |
862 // "BLAH=hah; path=/; domain=.google.com" | |
863 size_t NumberOfAttributes() const { return pairs_.size() - 1; } | |
864 | |
865 // For debugging only! | |
866 std::string DebugString() const; | |
867 | |
868 // Returns an iterator pointing to the first terminator character found in | |
869 // the given string. | |
870 static std::string::const_iterator FindFirstTerminator(const std::string& s); | |
871 | |
872 // Given iterators pointing to the beginning and end of a string segment, | |
873 // returns as output arguments token_start and token_end to the start and end | |
874 // positions of a cookie attribute token name parsed from the segment, and | |
875 // updates the segment iterator to point to the next segment to be parsed. | |
876 // If no token is found, the function returns false. | |
877 static bool ParseToken(std::string::const_iterator* it, | |
878 const std::string::const_iterator& end, | |
879 std::string::const_iterator* token_start, | |
880 std::string::const_iterator* token_end); | |
881 | |
882 // Given iterators pointing to the beginning and end of a string segment, | |
883 // returns as output arguments value_start and value_end to the start and end | |
884 // positions of a cookie attribute value parsed from the segment, and updates | |
885 // the segment iterator to point to the next segment to be parsed. | |
886 static void ParseValue(std::string::const_iterator* it, | |
887 const std::string::const_iterator& end, | |
888 std::string::const_iterator* value_start, | |
889 std::string::const_iterator* value_end); | |
890 | |
891 // Same as the above functions, except the input is assumed to contain the | |
892 // desired token/value and nothing else. | |
893 static std::string ParseTokenString(const std::string& token); | |
894 static std::string ParseValueString(const std::string& value); | |
895 | |
896 private: | |
897 static const char kTerminator[]; | |
898 static const int kTerminatorLen; | |
899 static const char kWhitespace[]; | |
900 static const char kValueSeparator[]; | |
901 static const char kTokenSeparator[]; | |
902 | |
903 void ParseTokenValuePairs(const std::string& cookie_line); | |
904 void SetupAttributes(); | |
905 | |
906 PairList pairs_; | |
907 bool is_valid_; | |
908 // These will default to 0, but that should never be valid since the | |
909 // 0th index is the user supplied token/value, not an attribute. | |
910 // We're really never going to have more than like 8 attributes, so we | |
911 // could fit these into 3 bits each if we're worried about size... | |
912 size_t path_index_; | |
913 size_t domain_index_; | |
914 size_t mac_key_index_; | |
915 size_t mac_algorithm_index_; | |
916 size_t expires_index_; | |
917 size_t maxage_index_; | |
918 size_t secure_index_; | |
919 size_t httponly_index_; | |
920 | |
921 DISALLOW_COPY_AND_ASSIGN(ParsedCookie); | |
922 }; | |
923 | |
924 typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore> | |
925 RefcountedPersistentCookieStore; | |
926 | |
927 class CookieMonster::PersistentCookieStore | |
928 : public RefcountedPersistentCookieStore { | |
929 public: | |
930 virtual ~PersistentCookieStore() {} | |
931 | |
932 typedef base::Callback<void(const std::vector< | |
933 CookieMonster::CanonicalCookie*>&)> LoadedCallback; | |
934 | |
935 // Initializes the store and retrieves the existing cookies. This will be | |
936 // called only once at startup. The callback will return all the cookies | |
937 // that are not yet returned to CookieMonster by previous priority loads. | |
938 virtual void Load(const LoadedCallback& loaded_callback) = 0; | |
939 | |
940 // Does a priority load of all cookies for the domain key (eTLD+1). The | |
941 // callback will return all the cookies that are not yet returned by previous | |
942 // loads, which includes cookies for the requested domain key if they are not | |
943 // already returned, plus all cookies that are chain-loaded and not yet | |
944 // returned to CookieMonster. | |
945 virtual void LoadCookiesForKey(const std::string& key, | |
946 const LoadedCallback& loaded_callback) = 0; | |
947 | |
948 virtual void AddCookie(const CanonicalCookie& cc) = 0; | |
949 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; | |
950 virtual void DeleteCookie(const CanonicalCookie& cc) = 0; | |
951 | |
952 // Sets the value of the user preference whether the persistent storage | |
953 // must be deleted upon destruction. | |
954 virtual void SetClearLocalStateOnExit(bool clear_local_state) = 0; | |
955 | |
956 // Flushes the store and posts |callback| when complete. | |
957 virtual void Flush(const base::Closure& callback) = 0; | |
958 | |
959 protected: | |
960 PersistentCookieStore() {} | |
961 | |
962 private: | |
963 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); | |
964 }; | |
965 | |
966 class CookieList : public std::vector<CookieMonster::CanonicalCookie> { | |
967 }; | |
968 | |
969 } // namespace net | |
970 | 13 |
971 #endif // NET_BASE_COOKIE_MONSTER_H_ | 14 #endif // NET_BASE_COOKIE_MONSTER_H_ |
OLD | NEW |