tor-browser

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

CookieService.h (5848B)


      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_CookieService_h
      7 #define mozilla_net_CookieService_h
      8 
      9 #include "nsICookieService.h"
     10 #include "nsICookieManager.h"
     11 #include "nsIObserver.h"
     12 #include "nsWeakReference.h"
     13 
     14 #include "Cookie.h"
     15 #include "CookieCommons.h"
     16 #include "ThirdPartyCookieBlockingExceptions.h"
     17 
     18 #include "nsString.h"
     19 #include "nsICookieValidation.h"
     20 #include "nsIMemoryReporter.h"
     21 #include "mozilla/MemoryReporting.h"
     22 #include "mozilla/MozPromise.h"
     23 
     24 class nsIConsoleReportCollector;
     25 class nsICookieJarSettings;
     26 class nsIEffectiveTLDService;
     27 class nsIURI;
     28 class nsIChannel;
     29 class mozIThirdPartyUtil;
     30 
     31 namespace mozilla {
     32 namespace net {
     33 
     34 class CookiePersistentStorage;
     35 class CookiePrivateStorage;
     36 class CookieStorage;
     37 
     38 /******************************************************************************
     39 * CookieService:
     40 * class declaration
     41 ******************************************************************************/
     42 
     43 class CookieService final : public nsICookieService,
     44                            public nsICookieManager,
     45                            public nsIObserver,
     46                            public nsSupportsWeakReference,
     47                            public nsIMemoryReporter {
     48 private:
     49  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
     50 
     51 public:
     52  NS_DECL_ISUPPORTS
     53  NS_DECL_NSIOBSERVER
     54  NS_DECL_NSICOOKIESERVICE
     55  NS_DECL_NSICOOKIEMANAGER
     56  NS_DECL_NSIMEMORYREPORTER
     57 
     58  static already_AddRefed<CookieService> GetSingleton();
     59 
     60  CookieService();
     61  static already_AddRefed<nsICookieService> GetXPCOMSingleton();
     62  nsresult Init();
     63 
     64  static void Update3PCBExceptionInfo(nsIChannel* aChannel);
     65 
     66  ThirdPartyCookieBlockingExceptions& ThirdPartyCookieBlockingExceptionsRef() {
     67    return mThirdPartyCookieBlockingExceptions;
     68  }
     69 
     70  /**
     71   * Start watching the observer service for messages indicating that an app has
     72   * been uninstalled.  When an app is uninstalled, we get the cookie service
     73   * (thus instantiating it, if necessary) and clear all the cookies for that
     74   * app.
     75   */
     76 
     77  static CookieStatus CheckPrefs(
     78      nsIConsoleReportCollector* aCRC, nsICookieJarSettings* aCookieJarSettings,
     79      nsIURI* aHostURI, bool aIsForeign, bool aIsThirdPartyTrackingResource,
     80      bool aIsThirdPartySocialTrackingResource,
     81      bool aStorageAccessPermissionGranted, const nsACString& aCookieHeader,
     82      const int aNumOfCookies, const OriginAttributes& aOriginAttrs,
     83      uint32_t* aRejectedReason);
     84 
     85  void GetCookiesForURI(nsIURI* aHostURI, nsIChannel* aChannel, bool aIsForeign,
     86                        bool aIsThirdPartyTrackingResource,
     87                        bool aIsThirdPartySocialTrackingResource,
     88                        bool aStorageAccessPermissionGranted,
     89                        uint32_t aRejectedReason, bool aIsSafeTopLevelNav,
     90                        bool aIsSameSiteForeign, bool aHadCrossSiteRedirects,
     91                        bool aHttpBound,
     92                        bool aAllowSecureCookiesToInsecureOrigin,
     93                        const nsTArray<OriginAttributes>& aOriginAttrsList,
     94                        nsTArray<RefPtr<Cookie>>& aCookieList);
     95 
     96  /**
     97   * This method is a helper that allows calling nsICookieManager::Remove()
     98   * with OriginAttributes parameter.
     99   */
    100  nsresult Remove(const nsACString& aHost, const OriginAttributes& aAttrs,
    101                  const nsACString& aName, const nsACString& aPath,
    102                  bool aFromHttp, const nsID* aOperationID);
    103 
    104  nsICookieValidation::ValidationError SetCookiesFromIPC(
    105      const nsACString& aBaseDomain, const OriginAttributes& aAttrs,
    106      nsIURI* aHostURI, bool aFromHttp, bool aIsThirdParty,
    107      const nsTArray<CookieStruct>& aCookies,
    108      dom::BrowsingContext* aBrowsingContext);
    109 
    110 protected:
    111  virtual ~CookieService();
    112 
    113  bool IsInitialized() const;
    114 
    115  void InitCookieStorages();
    116  void CloseCookieStorages();
    117 
    118  nsresult NormalizeHost(nsCString& aHost);
    119  void NotifyAccepted(nsIChannel* aChannel);
    120 
    121  nsresult GetCookiesWithOriginAttributes(
    122      const OriginAttributesPattern& aPattern, const nsCString& aBaseDomain,
    123      bool aSorted, nsTArray<RefPtr<nsICookie>>& aResult);
    124  nsresult RemoveCookiesWithOriginAttributes(
    125      const OriginAttributesPattern& aPattern, const nsCString& aBaseDomain);
    126 
    127 protected:
    128  CookieStorage* PickStorage(const OriginAttributes& aAttrs);
    129  CookieStorage* PickStorage(const OriginAttributesPattern& aAttrs);
    130 
    131  nsresult RemoveCookiesFromExactHost(const nsACString& aHost,
    132                                      const OriginAttributesPattern& aPattern);
    133 
    134  // cached members.
    135  nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
    136  nsCOMPtr<nsIEffectiveTLDService> mTLDService;
    137 
    138  ThirdPartyCookieBlockingExceptions mThirdPartyCookieBlockingExceptions;
    139 
    140  // we have two separate Cookie Storages: one for normal browsing and one for
    141  // private browsing.
    142  RefPtr<CookieStorage> mPersistentStorage;
    143  RefPtr<CookieStorage> mPrivateStorage;
    144 
    145 private:
    146  nsresult AddInternal(nsIURI* aCookieURI, const nsACString& aHost,
    147                       const nsACString& aPath, const nsACString& aName,
    148                       const nsACString& aValue, bool aIsSecure,
    149                       bool aIsHttpOnly, bool aIsSession, int64_t aExpiry,
    150                       OriginAttributes* aOriginAttributes, int32_t aSameSite,
    151                       nsICookie::schemeType aSchemeMap, bool aIsPartitioned,
    152                       bool aFromHttp, const nsID* aOperationID,
    153                       nsICookieValidation** aValidation);
    154 };
    155 
    156 }  // namespace net
    157 }  // namespace mozilla
    158 
    159 #endif  // mozilla_net_CookieService_h