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/. */
6 #ifndef mozilla_net_CookieService_h
7 #define mozilla_net_CookieService_h
9 #include "nsICookieService.h"
10 #include "nsICookieManager.h"
11 #include "nsIObserver.h"
12 #include "nsWeakReference.h"
15 #include "CookieCommons.h"
18 #include "nsIMemoryReporter.h"
19 #include "mozilla/MemoryReporting.h"
21 class nsIConsoleReportCollector
;
22 class nsICookieJarSettings
;
23 class nsIEffectiveTLDService
;
27 class mozIThirdPartyUtil
;
32 class CookiePersistentStorage
;
33 class CookiePrivateStorage
;
36 /******************************************************************************
39 ******************************************************************************/
41 class CookieService final
: public nsICookieService
,
42 public nsICookieManager
,
44 public nsSupportsWeakReference
,
45 public nsIMemoryReporter
{
47 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const;
52 NS_DECL_NSICOOKIESERVICE
53 NS_DECL_NSICOOKIEMANAGER
54 NS_DECL_NSIMEMORYREPORTER
56 static already_AddRefed
<CookieService
> GetSingleton();
59 static already_AddRefed
<nsICookieService
> GetXPCOMSingleton();
63 * Start watching the observer service for messages indicating that an app has
64 * been uninstalled. When an app is uninstalled, we get the cookie service
65 * (thus instantiating it, if necessary) and clear all the cookies for that
69 static bool CanSetCookie(nsIURI
* aHostURI
, const nsACString
& aBaseDomain
,
70 CookieStruct
& aCookieData
, bool aRequireHostMatch
,
71 CookieStatus aStatus
, nsCString
& aCookieHeader
,
72 bool aFromHttp
, bool aIsForeignAndNotAddon
,
73 bool aPartitionedOnly
,
74 nsIConsoleReportCollector
* aCRC
, bool& aSetCookie
);
75 static CookieStatus
CheckPrefs(
76 nsIConsoleReportCollector
* aCRC
, nsICookieJarSettings
* aCookieJarSettings
,
77 nsIURI
* aHostURI
, bool aIsForeign
, bool aIsThirdPartyTrackingResource
,
78 bool aIsThirdPartySocialTrackingResource
,
79 bool aStorageAccessPermissionGranted
, const nsACString
& aCookieHeader
,
80 const int aNumOfCookies
, const OriginAttributes
& aOriginAttrs
,
81 uint32_t* aRejectedReason
);
83 void GetCookiesForURI(nsIURI
* aHostURI
, nsIChannel
* aChannel
, bool aIsForeign
,
84 bool aIsThirdPartyTrackingResource
,
85 bool aIsThirdPartySocialTrackingResource
,
86 bool aStorageAccessPermissionGranted
,
87 uint32_t aRejectedReason
, bool aIsSafeTopLevelNav
,
88 bool aIsSameSiteForeign
, bool aHadCrossSiteRedirects
,
90 bool aAllowSecureCookiesToInsecureOrigin
,
91 const OriginAttributes
& aOriginAttrs
,
92 nsTArray
<Cookie
*>& aCookieList
);
95 * This method is a helper that allows calling nsICookieManager::Remove()
96 * with OriginAttributes parameter.
98 nsresult
Remove(const nsACString
& aHost
, const OriginAttributes
& aAttrs
,
99 const nsACString
& aName
, const nsACString
& aPath
);
101 bool SetCookiesFromIPC(const nsACString
& aBaseDomain
,
102 const OriginAttributes
& aAttrs
, nsIURI
* aHostURI
,
103 bool aFromHttp
, const nsTArray
<CookieStruct
>& aCookies
,
104 dom::BrowsingContext
* aBrowsingContext
);
107 virtual ~CookieService();
109 bool IsInitialized() const;
111 void InitCookieStorages();
112 void CloseCookieStorages();
114 nsresult
NormalizeHost(nsCString
& aHost
);
115 static bool GetTokenValue(nsACString::const_char_iterator
& aIter
,
116 nsACString::const_char_iterator
& aEndIter
,
117 nsDependentCSubstring
& aTokenString
,
118 nsDependentCSubstring
& aTokenValue
,
120 static bool ParseAttributes(nsIConsoleReportCollector
* aCRC
, nsIURI
* aHostURI
,
121 nsCString
& aCookieHeader
,
122 CookieStruct
& aCookieData
, nsACString
& aExpires
,
123 nsACString
& aMaxage
, bool& aAcceptedByParser
);
124 static bool CheckDomain(CookieStruct
& aCookieData
, nsIURI
* aHostURI
,
125 const nsACString
& aBaseDomain
,
126 bool aRequireHostMatch
);
127 static bool CheckHiddenPrefix(CookieStruct
& aCookieData
);
128 static bool CheckPath(CookieStruct
& aCookieData
,
129 nsIConsoleReportCollector
* aCRC
, nsIURI
* aHostURI
);
130 static bool CheckPrefixes(CookieStruct
& aCookieData
, bool aSecureRequest
);
131 static bool GetExpiry(CookieStruct
& aCookieData
, const nsACString
& aExpires
,
132 const nsACString
& aMaxage
, int64_t aCurrentTime
,
134 void NotifyAccepted(nsIChannel
* aChannel
);
136 nsresult
GetCookiesWithOriginAttributes(
137 const OriginAttributesPattern
& aPattern
, const nsCString
& aBaseDomain
,
138 nsTArray
<RefPtr
<nsICookie
>>& aResult
);
139 nsresult
RemoveCookiesWithOriginAttributes(
140 const OriginAttributesPattern
& aPattern
, const nsCString
& aBaseDomain
);
143 CookieStorage
* PickStorage(const OriginAttributes
& aAttrs
);
144 CookieStorage
* PickStorage(const OriginAttributesPattern
& aAttrs
);
146 nsresult
RemoveCookiesFromExactHost(const nsACString
& aHost
,
147 const OriginAttributesPattern
& aPattern
);
150 nsCOMPtr
<mozIThirdPartyUtil
> mThirdPartyUtil
;
151 nsCOMPtr
<nsIEffectiveTLDService
> mTLDService
;
152 nsCOMPtr
<nsIIDNService
> mIDNService
;
154 // we have two separate Cookie Storages: one for normal browsing and one for
156 RefPtr
<CookieStorage
> mPersistentStorage
;
157 RefPtr
<CookieStorage
> mPrivateStorage
;
161 } // namespace mozilla
163 #endif // mozilla_net_CookieService_h