tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CookiePersistentStorage.h (5133B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozilla_net_CookiePersistentStorage_h
      7 #define mozilla_net_CookiePersistentStorage_h
      8 
      9 #include "CookieStorage.h"
     10 
     11 #include "mozilla/Atomics.h"
     12 #include "mozilla/Monitor.h"
     13 #include "mozilla/net/NeckoChannelParams.h"
     14 #include "mozIStorageBindingParamsArray.h"
     15 #include "mozIStorageCompletionCallback.h"
     16 #include "mozIStorageStatement.h"
     17 #include "mozIStorageStatementCallback.h"
     18 
     19 class mozIStorageAsyncStatement;
     20 class mozIStorageService;
     21 class nsICookieTransactionCallback;
     22 class nsIEffectiveTLDService;
     23 
     24 namespace mozilla {
     25 namespace net {
     26 
     27 class CookiePersistentStorage final : public CookieStorage {
     28 public:
     29  // Result codes for TryInitDB() and Read().
     30  enum OpenDBResult { RESULT_OK, RESULT_RETRY, RESULT_FAILURE };
     31 
     32  static already_AddRefed<CookiePersistentStorage> Create();
     33 
     34  void HandleCorruptDB();
     35 
     36  void RemoveCookiesWithOriginAttributes(
     37      const OriginAttributesPattern& aPattern,
     38      const nsACString& aBaseDomain) override;
     39 
     40  void RemoveCookiesFromExactHost(
     41      const nsACString& aHost, const nsACString& aBaseDomain,
     42      const OriginAttributesPattern& aPattern) override;
     43 
     44  void StaleCookies(const nsTArray<RefPtr<Cookie>>& aCookieList,
     45                    int64_t aCurrentTimeInUsec) override;
     46 
     47  void Close() override;
     48 
     49  void EnsureInitialized() override;
     50 
     51  void CleanupCachedStatements();
     52  void CleanupDBConnection();
     53 
     54  void Activate();
     55 
     56  void RebuildCorruptDB();
     57  void HandleDBClosed();
     58 
     59  nsresult RunInTransaction(nsICookieTransactionCallback* aCallback) override;
     60 
     61  // State of the database connection.
     62  enum CorruptFlag {
     63    OK,                   // normal
     64    CLOSING_FOR_REBUILD,  // corruption detected, connection closing
     65    REBUILDING            // close complete, rebuilding database from memory
     66  };
     67 
     68  CorruptFlag GetCorruptFlag() const { return mCorruptFlag; }
     69 
     70  void SetCorruptFlag(CorruptFlag aFlag) { mCorruptFlag = aFlag; }
     71 
     72 protected:
     73  const char* NotificationTopic() const override { return "cookie-changed"; }
     74 
     75  void NotifyChangedInternal(nsICookieNotification* aNotification,
     76                             bool aOldCookieIsSession) override;
     77 
     78  void RemoveAllInternal() override;
     79 
     80  void RemoveCookieFromDB(const Cookie& aCookie) override;
     81 
     82  void StoreCookie(const nsACString& aBaseDomain,
     83                   const OriginAttributes& aOriginAttributes,
     84                   Cookie* aCookie) override;
     85 
     86 private:
     87  CookiePersistentStorage();
     88 
     89  static void UpdateCookieInList(Cookie* aCookie, int64_t aLastAccessed,
     90                                 mozIStorageBindingParamsArray* aParamsArray);
     91 
     92  void PrepareCookieRemoval(const Cookie& aCookie,
     93                            mozIStorageBindingParamsArray* aParamsArray);
     94 
     95  void InitDBConn();
     96  nsresult InitDBConnInternal();
     97 
     98  OpenDBResult TryInitDB(bool aRecreateDB);
     99  OpenDBResult Read();
    100  void MoveUnpartitionedChipsCookies();
    101 
    102  void RecordValidationTelemetry();
    103 
    104  nsresult CreateTableWorker(const char* aName);
    105  nsresult CreateTable();
    106  nsresult CreateTableForSchemaVersion6();
    107  nsresult CreateTableForSchemaVersion5();
    108 
    109  static UniquePtr<CookieStruct> GetCookieFromRow(mozIStorageStatement* aRow);
    110 
    111  already_AddRefed<nsIArray> PurgeCookies(int64_t aCurrentTimeInUsec,
    112                                          uint16_t aMaxNumberOfCookies,
    113                                          int64_t aCookiePurgeAge) override;
    114 
    115  void CollectCookieJarSizeData() override;
    116 
    117  void DeleteFromDB(mozIStorageBindingParamsArray* aParamsArray);
    118 
    119  void MaybeStoreCookiesToDB(mozIStorageBindingParamsArray* aParamsArray);
    120 
    121  nsCOMPtr<nsIThread> mThread;
    122  nsCOMPtr<mozIStorageService> mStorageService;
    123  nsCOMPtr<nsIEffectiveTLDService> mTLDService;
    124 
    125  // encapsulates a (key, Cookie) tuple for temporary storage purposes.
    126  struct CookieDomainTuple {
    127    CookieKey key;
    128    OriginAttributes originAttributes;
    129    UniquePtr<CookieStruct> cookie;
    130  };
    131 
    132  // thread
    133  TimeStamp mEndInitDBConn;
    134  nsTArray<CookieDomainTuple> mReadArray;
    135 
    136  Monitor mMonitor MOZ_UNANNOTATED;
    137 
    138  Atomic<bool> mInitialized;
    139  Atomic<bool> mInitializedDBConn;
    140 
    141  nsCOMPtr<nsIFile> mCookieFile;
    142  nsCOMPtr<mozIStorageConnection> mDBConn;
    143  nsCOMPtr<mozIStorageAsyncStatement> mStmtInsert;
    144  nsCOMPtr<mozIStorageAsyncStatement> mStmtDelete;
    145  nsCOMPtr<mozIStorageAsyncStatement> mStmtUpdate;
    146 
    147  CorruptFlag mCorruptFlag;
    148 
    149  // Various parts representing asynchronous read state. These are useful
    150  // while the background read is taking place.
    151  nsCOMPtr<mozIStorageConnection> mSyncConn;
    152 
    153  // DB completion handlers.
    154  nsCOMPtr<mozIStorageStatementCallback> mInsertListener;
    155  nsCOMPtr<mozIStorageStatementCallback> mUpdateListener;
    156  nsCOMPtr<mozIStorageStatementCallback> mRemoveListener;
    157  nsCOMPtr<mozIStorageCompletionCallback> mCloseListener;
    158 };
    159 
    160 }  // namespace net
    161 }  // namespace mozilla
    162 
    163 #endif  // mozilla_net_CookiePersistentStorage_h