Home | History | Annotate | Download | only in cookies
      1 // Copyright (c) 2012 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 // Brought to you by the letter D and the number 2.
      6 
      7 #ifndef NET_COOKIES_COOKIE_MONSTER_H_
      8 #define NET_COOKIES_COOKIE_MONSTER_H_
      9 
     10 #include <deque>
     11 #include <map>
     12 #include <queue>
     13 #include <set>
     14 #include <string>
     15 #include <utility>
     16 #include <vector>
     17 
     18 #include "base/basictypes.h"
     19 #include "base/callback_forward.h"
     20 #include "base/gtest_prod_util.h"
     21 #include "base/memory/ref_counted.h"
     22 #include "base/memory/scoped_ptr.h"
     23 #include "base/synchronization/lock.h"
     24 #include "base/time/time.h"
     25 #include "net/base/net_export.h"
     26 #include "net/cookies/canonical_cookie.h"
     27 #include "net/cookies/cookie_constants.h"
     28 #include "net/cookies/cookie_store.h"
     29 
     30 class GURL;
     31 
     32 namespace base {
     33 class Histogram;
     34 class HistogramBase;
     35 class TimeTicks;
     36 }  // namespace base
     37 
     38 namespace net {
     39 
     40 class ParsedCookie;
     41 
     42 // The cookie monster is the system for storing and retrieving cookies. It has
     43 // an in-memory list of all cookies, and synchronizes non-session cookies to an
     44 // optional permanent storage that implements the PersistentCookieStore
     45 // interface.
     46 //
     47 // This class IS thread-safe. Normally, it is only used on the I/O thread, but
     48 // is also accessed directly through Automation for UI testing.
     49 //
     50 // All cookie tasks are handled asynchronously. Tasks may be deferred if
     51 // all affected cookies are not yet loaded from the backing store. Otherwise,
     52 // the callback may be invoked immediately (prior to return of the asynchronous
     53 // function).
     54 //
     55 // A cookie task is either pending loading of the entire cookie store, or
     56 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the
     57 // cookie task will be queued in tasks_pending_ while PersistentCookieStore
     58 // chain loads the cookie store on DB thread. In the latter case, the cookie
     59 // task will be queued in tasks_pending_for_key_ while PermanentCookieStore
     60 // loads cookies for the specified domain key(eTLD+1) on DB thread.
     61 //
     62 // Callbacks are guaranteed to be invoked on the calling thread.
     63 //
     64 // TODO(deanm) Implement CookieMonster, the cookie database.
     65 //  - Verify that our domain enforcement and non-dotted handling is correct
     66 class NET_EXPORT CookieMonster : public CookieStore {
     67  public:
     68   class Delegate;
     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   typedef std::vector<CookieMap::iterator> CookieItVector;
    110 
    111   // Cookie garbage collection thresholds.  Based off of the Mozilla defaults.
    112   // When the number of cookies gets to k{Domain,}MaxCookies
    113   // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
    114   // It might seem scary to have a high purge value, but really it's not.
    115   // You just make sure that you increase the max to cover the increase
    116   // in purge, and we would have been purging the same number of cookies.
    117   // We're just going through the garbage collection process less often.
    118   // Note that the DOMAIN values are per eTLD+1; see comment for the
    119   // CookieMap typedef.  So, e.g., the maximum number of cookies allowed for
    120   // google.com and all of its subdomains will be 150-180.
    121   //
    122   // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
    123   // be evicted by global garbage collection, even if we have more than
    124   // kMaxCookies.  This does not affect domain garbage collection.
    125   static const size_t kDomainMaxCookies;
    126   static const size_t kDomainPurgeCookies;
    127   static const size_t kMaxCookies;
    128   static const size_t kPurgeCookies;
    129 
    130   // Quota for cookies with {low, medium, high} priorities within a domain.
    131   static const size_t kDomainCookiesQuotaLow;
    132   static const size_t kDomainCookiesQuotaMedium;
    133   static const size_t kDomainCookiesQuotaHigh;
    134 
    135   // The store passed in should not have had Init() called on it yet. This
    136   // class will take care of initializing it. The backing store is NOT owned by
    137   // this class, but it must remain valid for the duration of the cookie
    138   // monster's existence. If |store| is NULL, then no backing store will be
    139   // updated. If |delegate| is non-NULL, it will be notified on
    140   // creation/deletion of cookies.
    141   CookieMonster(PersistentCookieStore* store, Delegate* delegate);
    142 
    143   // Only used during unit testing.
    144   CookieMonster(PersistentCookieStore* store,
    145                 Delegate* delegate,
    146                 int last_access_threshold_milliseconds);
    147 
    148   // Helper function that adds all cookies from |list| into this instance.
    149   bool InitializeFrom(const CookieList& list);
    150 
    151   typedef base::Callback<void(const CookieList& cookies)> GetCookieListCallback;
    152   typedef base::Callback<void(bool success)> DeleteCookieCallback;
    153   typedef base::Callback<void(bool cookies_exist)> HasCookiesForETLDP1Callback;
    154 
    155   // Sets a cookie given explicit user-provided cookie attributes. The cookie
    156   // name, value, domain, etc. are each provided as separate strings. This
    157   // function expects each attribute to be well-formed. It will check for
    158   // disallowed characters (e.g. the ';' character is disallowed within the
    159   // cookie value attribute) and will return false without setting the cookie
    160   // if such characters are found.
    161   void SetCookieWithDetailsAsync(const GURL& url,
    162                                  const std::string& name,
    163                                  const std::string& value,
    164                                  const std::string& domain,
    165                                  const std::string& path,
    166                                  const base::Time& expiration_time,
    167                                  bool secure,
    168                                  bool http_only,
    169                                  CookiePriority priority,
    170                                  const SetCookiesCallback& callback);
    171 
    172 
    173   // Returns all the cookies, for use in management UI, etc. This does not mark
    174   // the cookies as having been accessed.
    175   // The returned cookies are ordered by longest path, then by earliest
    176   // creation date.
    177   void GetAllCookiesAsync(const GetCookieListCallback& callback);
    178 
    179   // Returns all the cookies, for use in management UI, etc. Filters results
    180   // using given url scheme, host / domain and path and options. This does not
    181   // mark the cookies as having been accessed.
    182   // The returned cookies are ordered by longest path, then earliest
    183   // creation date.
    184   void GetAllCookiesForURLWithOptionsAsync(
    185       const GURL& url,
    186       const CookieOptions& options,
    187       const GetCookieListCallback& callback);
    188 
    189   // Invokes GetAllCookiesForURLWithOptions with options set to include HTTP
    190   // only cookies.
    191   void GetAllCookiesForURLAsync(const GURL& url,
    192                                 const GetCookieListCallback& callback);
    193 
    194   // Deletes all of the cookies.
    195   void DeleteAllAsync(const DeleteCallback& callback);
    196 
    197   // Deletes all cookies that match the host of the given URL
    198   // regardless of path.  This includes all http_only and secure cookies,
    199   // but does not include any domain cookies that may apply to this host.
    200   // Returns the number of cookies deleted.
    201   void DeleteAllForHostAsync(const GURL& url,
    202                              const DeleteCallback& callback);
    203 
    204   // Same as DeleteAllForHostAsync, except it deletes cookies between
    205   // [|delete_begin|, |delete_end|).
    206   // Returns the number of cookies deleted.
    207   void DeleteAllCreatedBetweenForHostAsync(const base::Time delete_begin,
    208                                            const base::Time delete_end,
    209                                            const GURL& url,
    210                                            const DeleteCallback& callback);
    211 
    212   // Deletes one specific cookie.
    213   void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
    214                                   const DeleteCookieCallback& callback);
    215 
    216   // Checks whether for a given ETLD+1, there currently exist any cookies.
    217   void HasCookiesForETLDP1Async(const std::string& etldp1,
    218                                 const HasCookiesForETLDP1Callback& callback);
    219 
    220   // Resets the list of cookieable schemes to the supplied schemes.
    221   // If this this method is called, it must be called before first use of
    222   // the instance (i.e. as part of the instance initialization process).
    223   void SetCookieableSchemes(const char* schemes[], size_t num_schemes);
    224 
    225   // Resets the list of cookieable schemes to kDefaultCookieableSchemes with or
    226   // without 'file' being included.
    227   void SetEnableFileScheme(bool accept);
    228 
    229   // Instructs the cookie monster to not delete expired cookies. This is used
    230   // in cases where the cookie monster is used as a data structure to keep
    231   // arbitrary cookies.
    232   void SetKeepExpiredCookies();
    233 
    234   // Protects session cookies from deletion on shutdown.
    235   void SetForceKeepSessionState();
    236 
    237   // There are some unknowns about how to correctly handle file:// cookies,
    238   // and our implementation for this is not robust enough. This allows you
    239   // to enable support, but it should only be used for testing. Bug 1157243.
    240   // Must be called before creating a CookieMonster instance.
    241   static void EnableFileScheme();
    242 
    243   // Flush the backing store (if any) to disk and post the given callback when
    244   // done.
    245   // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE.
    246   // It may be posted to the current thread, or it may run on the thread that
    247   // actually does the flushing. Your Task should generally post a notification
    248   // to the thread you actually want to be notified on.
    249   void FlushStore(const base::Closure& callback);
    250 
    251   // CookieStore implementation.
    252 
    253   // Sets the cookies specified by |cookie_list| returned from |url|
    254   // with options |options| in effect.
    255   virtual void SetCookieWithOptionsAsync(
    256       const GURL& url,
    257       const std::string& cookie_line,
    258       const CookieOptions& options,
    259       const SetCookiesCallback& callback) OVERRIDE;
    260 
    261   // Gets all cookies that apply to |url| given |options|.
    262   // The returned cookies are ordered by longest path, then earliest
    263   // creation date.
    264   virtual void GetCookiesWithOptionsAsync(
    265       const GURL& url,
    266       const CookieOptions& options,
    267       const GetCookiesCallback& callback) OVERRIDE;
    268 
    269   // Deletes all cookies with that might apply to |url| that has |cookie_name|.
    270   virtual void DeleteCookieAsync(
    271       const GURL& url, const std::string& cookie_name,
    272       const base::Closure& callback) OVERRIDE;
    273 
    274   // Deletes all of the cookies that have a creation_date greater than or equal
    275   // to |delete_begin| and less than |delete_end|
    276   // Returns the number of cookies that have been deleted.
    277   virtual void DeleteAllCreatedBetweenAsync(
    278       const base::Time& delete_begin,
    279       const base::Time& delete_end,
    280       const DeleteCallback& callback) OVERRIDE;
    281 
    282   virtual void DeleteSessionCookiesAsync(const DeleteCallback&) OVERRIDE;
    283 
    284   virtual CookieMonster* GetCookieMonster() OVERRIDE;
    285 
    286   // Enables writing session cookies into the cookie database. If this this
    287   // method is called, it must be called before first use of the instance
    288   // (i.e. as part of the instance initialization process).
    289   void SetPersistSessionCookies(bool persist_session_cookies);
    290 
    291   // Enables the new garbage collection algorithm where domain cookie eviction
    292   // uses cookie priorities to decide which cookies to purge and which to keep.
    293   void SetPriorityAwareGarbageCollection(
    294       bool priority_aware_garbage_collection);
    295 
    296   // Debugging method to perform various validation checks on the map.
    297   // Currently just checking that there are no null CanonicalCookie pointers
    298   // in the map.
    299   // Argument |arg| is to allow retaining of arbitrary data if the CHECKs
    300   // in the function trip.  TODO(rdsmith):Remove hack.
    301   void ValidateMap(int arg);
    302 
    303   // Determines if the scheme of the URL is a scheme that cookies will be
    304   // stored for.
    305   bool IsCookieableScheme(const std::string& scheme);
    306 
    307   // The default list of schemes the cookie monster can handle.
    308   static const char* kDefaultCookieableSchemes[];
    309   static const int kDefaultCookieableSchemesCount;
    310 
    311  private:
    312   // For queueing the cookie monster calls.
    313   class CookieMonsterTask;
    314   class DeleteAllCreatedBetweenTask;
    315   class DeleteAllCreatedBetweenForHostTask;
    316   class DeleteAllForHostTask;
    317   class DeleteAllTask;
    318   class DeleteCookieTask;
    319   class DeleteCanonicalCookieTask;
    320   class GetAllCookiesForURLWithOptionsTask;
    321   class GetAllCookiesTask;
    322   class GetCookiesWithOptionsTask;
    323   class SetCookieWithDetailsTask;
    324   class SetCookieWithOptionsTask;
    325   class DeleteSessionCookiesTask;
    326   class HasCookiesForETLDP1Task;
    327 
    328   // Testing support.
    329   // For SetCookieWithCreationTime.
    330   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
    331                            TestCookieDeleteAllCreatedBetweenTimestamps);
    332   // For SetCookieWithCreationTime.
    333   FRIEND_TEST_ALL_PREFIXES(MultiThreadedCookieMonsterTest,
    334                            ThreadCheckDeleteAllCreatedBetweenForHost);
    335 
    336   // For gargage collection constants.
    337   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
    338   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection);
    339   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers);
    340   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
    341 
    342   // For validation of key values.
    343   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
    344   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
    345   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
    346   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
    347 
    348   // For FindCookiesForKey.
    349   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
    350 
    351   // Internal reasons for deletion, used to populate informative histograms
    352   // and to provide a public cause for onCookieChange notifications.
    353   //
    354   // If you add or remove causes from this list, please be sure to also update
    355   // the Delegate::ChangeCause mapping inside ChangeCauseMapping. Moreover,
    356   // these are used as array indexes, so avoid reordering to keep the
    357   // histogram buckets consistent. New items (if necessary) should be added
    358   // at the end of the list, just before DELETE_COOKIE_LAST_ENTRY.
    359   enum DeletionCause {
    360     DELETE_COOKIE_EXPLICIT = 0,
    361     DELETE_COOKIE_OVERWRITE,
    362     DELETE_COOKIE_EXPIRED,
    363     DELETE_COOKIE_EVICTED,
    364     DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE,
    365     DELETE_COOKIE_DONT_RECORD,  // e.g. For final cleanup after flush to store.
    366     DELETE_COOKIE_EVICTED_DOMAIN,
    367     DELETE_COOKIE_EVICTED_GLOBAL,
    368 
    369     // Cookies evicted during domain level garbage collection that
    370     // were accessed longer ago than kSafeFromGlobalPurgeDays
    371     DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
    372 
    373     // Cookies evicted during domain level garbage collection that
    374     // were accessed more recently than kSafeFromGlobalPurgeDays
    375     // (and thus would have been preserved by global garbage collection).
    376     DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
    377 
    378     // A common idiom is to remove a cookie by overwriting it with an
    379     // already-expired expiration date. This captures that case.
    380     DELETE_COOKIE_EXPIRED_OVERWRITE,
    381 
    382     DELETE_COOKIE_LAST_ENTRY
    383   };
    384 
    385   // The number of days since last access that cookies will not be subject
    386   // to global garbage collection.
    387   static const int kSafeFromGlobalPurgeDays;
    388 
    389   // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
    390   static const int kRecordStatisticsIntervalSeconds = 10 * 60;
    391 
    392   virtual ~CookieMonster();
    393 
    394   // The following are synchronous calls to which the asynchronous methods
    395   // delegate either immediately (if the store is loaded) or through a deferred
    396   // task (if the store is not yet loaded).
    397   bool SetCookieWithDetails(const GURL& url,
    398                             const std::string& name,
    399                             const std::string& value,
    400                             const std::string& domain,
    401                             const std::string& path,
    402                             const base::Time& expiration_time,
    403                             bool secure,
    404                             bool http_only,
    405                             CookiePriority priority);
    406 
    407   CookieList GetAllCookies();
    408 
    409   CookieList GetAllCookiesForURLWithOptions(const GURL& url,
    410                                             const CookieOptions& options);
    411 
    412   CookieList GetAllCookiesForURL(const GURL& url);
    413 
    414   int DeleteAll(bool sync_to_store);
    415 
    416   int DeleteAllCreatedBetween(const base::Time& delete_begin,
    417                               const base::Time& delete_end);
    418 
    419   int DeleteAllForHost(const GURL& url);
    420   int DeleteAllCreatedBetweenForHost(const base::Time delete_begin,
    421                                      const base::Time delete_end,
    422                                      const GURL& url);
    423 
    424   bool DeleteCanonicalCookie(const CanonicalCookie& cookie);
    425 
    426   bool SetCookieWithOptions(const GURL& url,
    427                             const std::string& cookie_line,
    428                             const CookieOptions& options);
    429 
    430   std::string GetCookiesWithOptions(const GURL& url,
    431                                     const CookieOptions& options);
    432 
    433   void DeleteCookie(const GURL& url, const std::string& cookie_name);
    434 
    435   bool SetCookieWithCreationTime(const GURL& url,
    436                                  const std::string& cookie_line,
    437                                  const base::Time& creation_time);
    438 
    439   int DeleteSessionCookies();
    440 
    441   bool HasCookiesForETLDP1(const std::string& etldp1);
    442 
    443   // Called by all non-static functions to ensure that the cookies store has
    444   // been initialized. This is not done during creating so it doesn't block
    445   // the window showing.
    446   // Note: this method should always be called with lock_ held.
    447   void InitIfNecessary() {
    448     if (!initialized_) {
    449       if (store_.get()) {
    450         InitStore();
    451       } else {
    452         loaded_ = true;
    453       }
    454       initialized_ = true;
    455     }
    456   }
    457 
    458   // Initializes the backing store and reads existing cookies from it.
    459   // Should only be called by InitIfNecessary().
    460   void InitStore();
    461 
    462   // Stores cookies loaded from the backing store and invokes any deferred
    463   // calls. |beginning_time| should be the moment PersistentCookieStore::Load
    464   // was invoked and is used for reporting histogram_time_blocked_on_load_.
    465   // See PersistentCookieStore::Load for details on the contents of cookies.
    466   void OnLoaded(base::TimeTicks beginning_time,
    467                 const std::vector<CanonicalCookie*>& cookies);
    468 
    469   // Stores cookies loaded from the backing store and invokes the deferred
    470   // task(s) pending loading of cookies associated with the domain key
    471   // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been
    472   // loaded from DB. See PersistentCookieStore::Load for details on the contents
    473   // of cookies.
    474   void OnKeyLoaded(
    475     const std::string& key,
    476     const std::vector<CanonicalCookie*>& cookies);
    477 
    478   // Stores the loaded cookies.
    479   void StoreLoadedCookies(const std::vector<CanonicalCookie*>& cookies);
    480 
    481   // Invokes deferred calls.
    482   void InvokeQueue();
    483 
    484   // Checks that |cookies_| matches our invariants, and tries to repair any
    485   // inconsistencies. (In other words, it does not have duplicate cookies).
    486   void EnsureCookiesMapIsValid();
    487 
    488   // Checks for any duplicate cookies for CookieMap key |key| which lie between
    489   // |begin| and |end|. If any are found, all but the most recent are deleted.
    490   // Returns the number of duplicate cookies that were deleted.
    491   int TrimDuplicateCookiesForKey(const std::string& key,
    492                                  CookieMap::iterator begin,
    493                                  CookieMap::iterator end);
    494 
    495   void SetDefaultCookieableSchemes();
    496 
    497   void FindCookiesForHostAndDomain(const GURL& url,
    498                                    const CookieOptions& options,
    499                                    bool update_access_time,
    500                                    std::vector<CanonicalCookie*>* cookies);
    501 
    502   void FindCookiesForKey(const std::string& key,
    503                          const GURL& url,
    504                          const CookieOptions& options,
    505                          const base::Time& current,
    506                          bool update_access_time,
    507                          std::vector<CanonicalCookie*>* cookies);
    508 
    509   // Delete any cookies that are equivalent to |ecc| (same path, domain, etc).
    510   // If |skip_httponly| is true, httponly cookies will not be deleted.  The
    511   // return value with be true if |skip_httponly| skipped an httponly cookie.
    512   // |key| is the key to find the cookie in cookies_; see the comment before
    513   // the CookieMap typedef for details.
    514   // NOTE: There should never be more than a single matching equivalent cookie.
    515   bool DeleteAnyEquivalentCookie(const std::string& key,
    516                                  const CanonicalCookie& ecc,
    517                                  bool skip_httponly,
    518                                  bool already_expired);
    519 
    520   // Takes ownership of *cc.
    521   void InternalInsertCookie(const std::string& key,
    522                             CanonicalCookie* cc,
    523                             bool sync_to_store);
    524 
    525   // Helper function that sets cookies with more control.
    526   // Not exposed as we don't want callers to have the ability
    527   // to specify (potentially duplicate) creation times.
    528   bool SetCookieWithCreationTimeAndOptions(const GURL& url,
    529                                            const std::string& cookie_line,
    530                                            const base::Time& creation_time,
    531                                            const CookieOptions& options);
    532 
    533   // Helper function that sets a canonical cookie, deleting equivalents and
    534   // performing garbage collection.
    535   bool SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
    536                           const base::Time& creation_time,
    537                           const CookieOptions& options);
    538 
    539   void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
    540                                       const base::Time& current_time);
    541 
    542   // |deletion_cause| argument is used for collecting statistics and choosing
    543   // the correct Delegate::ChangeCause for OnCookieChanged notifications.
    544   void InternalDeleteCookie(CookieMap::iterator it, bool sync_to_store,
    545                             DeletionCause deletion_cause);
    546 
    547   // If the number of cookies for CookieMap key |key|, or globally, are
    548   // over the preset maximums above, garbage collect, first for the host and
    549   // then globally.  See comments above garbage collection threshold
    550   // constants for details.
    551   //
    552   // Returns the number of cookies deleted (useful for debugging).
    553   int GarbageCollect(const base::Time& current, const std::string& key);
    554 
    555   // Helper for GarbageCollect(); can be called directly as well.  Deletes
    556   // all expired cookies in |itpair|.  If |cookie_its| is non-NULL, it is
    557   // populated with all the non-expired cookies from |itpair|.
    558   //
    559   // Returns the number of cookies deleted.
    560   int GarbageCollectExpired(const base::Time& current,
    561                             const CookieMapItPair& itpair,
    562                             std::vector<CookieMap::iterator>* cookie_its);
    563 
    564   // Helper for GarbageCollect(). Deletes all cookies in the range specified by
    565   // [|it_begin|, |it_end|). Returns the number of cookies deleted.
    566   int GarbageCollectDeleteRange(const base::Time& current,
    567                                 DeletionCause cause,
    568                                 CookieItVector::iterator cookie_its_begin,
    569                                 CookieItVector::iterator cookie_its_end);
    570 
    571   // Find the key (for lookup in cookies_) based on the given domain.
    572   // See comment on keys before the CookieMap typedef.
    573   std::string GetKey(const std::string& domain) const;
    574 
    575   bool HasCookieableScheme(const GURL& url);
    576 
    577   // Statistics support
    578 
    579   // This function should be called repeatedly, and will record
    580   // statistics if a sufficient time period has passed.
    581   void RecordPeriodicStats(const base::Time& current_time);
    582 
    583   // Initialize the above variables; should only be called from
    584   // the constructor.
    585   void InitializeHistograms();
    586 
    587   // The resolution of our time isn't enough, so we do something
    588   // ugly and increment when we've seen the same time twice.
    589   base::Time CurrentTime();
    590 
    591   // Runs the task if, or defers the task until, the full cookie database is
    592   // loaded.
    593   void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item);
    594 
    595   // Runs the task if, or defers the task until, the cookies for the given URL
    596   // are loaded.
    597   void DoCookieTaskForURL(const scoped_refptr<CookieMonsterTask>& task_item,
    598     const GURL& url);
    599 
    600   // Histogram variables; see CookieMonster::InitializeHistograms() in
    601   // cookie_monster.cc for details.
    602   base::HistogramBase* histogram_expiration_duration_minutes_;
    603   base::HistogramBase* histogram_between_access_interval_minutes_;
    604   base::HistogramBase* histogram_evicted_last_access_minutes_;
    605   base::HistogramBase* histogram_count_;
    606   base::HistogramBase* histogram_domain_count_;
    607   base::HistogramBase* histogram_etldp1_count_;
    608   base::HistogramBase* histogram_domain_per_etldp1_count_;
    609   base::HistogramBase* histogram_number_duplicate_db_cookies_;
    610   base::HistogramBase* histogram_cookie_deletion_cause_;
    611   base::HistogramBase* histogram_time_get_;
    612   base::HistogramBase* histogram_time_mac_;
    613   base::HistogramBase* histogram_time_blocked_on_load_;
    614 
    615   CookieMap cookies_;
    616 
    617   // Indicates whether the cookie store has been initialized. This happens
    618   // lazily in InitStoreIfNecessary().
    619   bool initialized_;
    620 
    621   // Indicates whether loading from the backend store is completed and
    622   // calls may be immediately processed.
    623   bool loaded_;
    624 
    625   // List of domain keys that have been loaded from the DB.
    626   std::set<std::string> keys_loaded_;
    627 
    628   // Map of domain keys to their associated task queues. These tasks are blocked
    629   // until all cookies for the associated domain key eTLD+1 are loaded from the
    630   // backend store.
    631   std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
    632       tasks_pending_for_key_;
    633 
    634   // Queues tasks that are blocked until all cookies are loaded from the backend
    635   // store.
    636   std::queue<scoped_refptr<CookieMonsterTask> > tasks_pending_;
    637 
    638   scoped_refptr<PersistentCookieStore> store_;
    639 
    640   base::Time last_time_seen_;
    641 
    642   // Minimum delay after updating a cookie's LastAccessDate before we will
    643   // update it again.
    644   const base::TimeDelta last_access_threshold_;
    645 
    646   // Approximate date of access time of least recently accessed cookie
    647   // in |cookies_|.  Note that this is not guaranteed to be accurate, only a)
    648   // to be before or equal to the actual time, and b) to be accurate
    649   // immediately after a garbage collection that scans through all the cookies.
    650   // This value is used to determine whether global garbage collection might
    651   // find cookies to purge.
    652   // Note: The default Time() constructor will create a value that compares
    653   // earlier than any other time value, which is wanted.  Thus this
    654   // value is not initialized.
    655   base::Time earliest_access_time_;
    656 
    657   // During loading, holds the set of all loaded cookie creation times. Used to
    658   // avoid ever letting cookies with duplicate creation times into the store;
    659   // that way we don't have to worry about what sections of code are safe
    660   // to call while it's in that state.
    661   std::set<int64> creation_times_;
    662 
    663   std::vector<std::string> cookieable_schemes_;
    664 
    665   scoped_refptr<Delegate> delegate_;
    666 
    667   // Lock for thread-safety
    668   base::Lock lock_;
    669 
    670   base::Time last_statistic_record_time_;
    671 
    672   bool keep_expired_cookies_;
    673   bool persist_session_cookies_;
    674   bool priority_aware_garbage_collection_;
    675 
    676   // Static setting for whether or not file scheme cookies are allows when
    677   // a new CookieMonster is created, or the accepted schemes on a CookieMonster
    678   // instance are reset back to defaults.
    679   static bool default_enable_file_scheme_;
    680 
    681   DISALLOW_COPY_AND_ASSIGN(CookieMonster);
    682 };
    683 
    684 class NET_EXPORT CookieMonster::Delegate
    685     : public base::RefCountedThreadSafe<CookieMonster::Delegate> {
    686  public:
    687   // The publicly relevant reasons a cookie might be changed.
    688   enum ChangeCause {
    689     // The cookie was changed directly by a consumer's action.
    690     CHANGE_COOKIE_EXPLICIT,
    691     // The cookie was automatically removed due to an insert operation that
    692     // overwrote it.
    693     CHANGE_COOKIE_OVERWRITE,
    694     // The cookie was automatically removed as it expired.
    695     CHANGE_COOKIE_EXPIRED,
    696     // The cookie was automatically evicted during garbage collection.
    697     CHANGE_COOKIE_EVICTED,
    698     // The cookie was overwritten with an already-expired expiration date.
    699     CHANGE_COOKIE_EXPIRED_OVERWRITE
    700   };
    701 
    702   // Will be called when a cookie is added or removed. The function is passed
    703   // the respective |cookie| which was added to or removed from the cookies.
    704   // If |removed| is true, the cookie was deleted, and |cause| will be set
    705   // to the reason for its removal. If |removed| is false, the cookie was
    706   // added, and |cause| will be set to CHANGE_COOKIE_EXPLICIT.
    707   //
    708   // As a special case, note that updating a cookie's properties is implemented
    709   // as a two step process: the cookie to be updated is first removed entirely,
    710   // generating a notification with cause CHANGE_COOKIE_OVERWRITE.  Afterwards,
    711   // a new cookie is written with the updated values, generating a notification
    712   // with cause CHANGE_COOKIE_EXPLICIT.
    713   virtual void OnCookieChanged(const CanonicalCookie& cookie,
    714                                bool removed,
    715                                ChangeCause cause) = 0;
    716  protected:
    717   friend class base::RefCountedThreadSafe<CookieMonster::Delegate>;
    718   virtual ~Delegate() {}
    719 };
    720 
    721 typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
    722     RefcountedPersistentCookieStore;
    723 
    724 class NET_EXPORT CookieMonster::PersistentCookieStore
    725     : public RefcountedPersistentCookieStore {
    726  public:
    727   typedef base::Callback<void(const std::vector<CanonicalCookie*>&)>
    728       LoadedCallback;
    729 
    730   // Initializes the store and retrieves the existing cookies. This will be
    731   // called only once at startup. The callback will return all the cookies
    732   // that are not yet returned to CookieMonster by previous priority loads.
    733   virtual void Load(const LoadedCallback& loaded_callback) = 0;
    734 
    735   // Does a priority load of all cookies for the domain key (eTLD+1). The
    736   // callback will return all the cookies that are not yet returned by previous
    737   // loads, which includes cookies for the requested domain key if they are not
    738   // already returned, plus all cookies that are chain-loaded and not yet
    739   // returned to CookieMonster.
    740   virtual void LoadCookiesForKey(const std::string& key,
    741                                  const LoadedCallback& loaded_callback) = 0;
    742 
    743   virtual void AddCookie(const CanonicalCookie& cc) = 0;
    744   virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
    745   virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
    746 
    747   // Instructs the store to not discard session only cookies on shutdown.
    748   virtual void SetForceKeepSessionState() = 0;
    749 
    750   // Flushes the store and posts |callback| when complete.
    751   virtual void Flush(const base::Closure& callback) = 0;
    752 
    753  protected:
    754   PersistentCookieStore() {}
    755   virtual ~PersistentCookieStore() {}
    756 
    757  private:
    758   friend class base::RefCountedThreadSafe<PersistentCookieStore>;
    759   DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore);
    760 };
    761 
    762 }  // namespace net
    763 
    764 #endif  // NET_COOKIES_COOKIE_MONSTER_H_
    765