Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / netwerk / cookie / Cookie.h
blob5fa34792686906080f125d9c58c7b8b92be7e943
1 /* -*- Mode: C++; tab-width: 4; 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_Cookie_h
7 #define mozilla_net_Cookie_h
9 #include "nsICookie.h"
10 #include "nsIMemoryReporter.h"
11 #include "nsString.h"
13 #include "mozilla/MemoryReporting.h"
14 #include "mozilla/BasePrincipal.h"
15 #include "mozilla/net/NeckoChannelParams.h"
16 #include "nsIMemoryReporter.h"
18 using mozilla::OriginAttributes;
20 namespace mozilla {
21 namespace net {
23 /**
24 * The Cookie class is the main cookie storage medium for use within cookie
25 * code.
28 /******************************************************************************
29 * Cookie:
30 * implementation
31 ******************************************************************************/
33 class Cookie final : public nsICookie {
34 MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
36 public:
37 // nsISupports
38 NS_DECL_ISUPPORTS
39 NS_DECL_NSICOOKIE
41 private:
42 // for internal use only. see Cookie::Create().
43 Cookie(const CookieStruct& aCookieData,
44 const OriginAttributes& aOriginAttributes)
45 : mData(aCookieData), mOriginAttributes(aOriginAttributes) {}
47 static already_AddRefed<Cookie> FromCookieStruct(
48 const CookieStruct& aCookieData,
49 const OriginAttributes& aOriginAttributes);
51 public:
52 // Returns false if rawSameSite has an invalid value, compared to sameSite.
53 static bool ValidateSameSite(const CookieStruct& aCookieData);
55 // Generate a unique and monotonically increasing creation time. See comment
56 // in Cookie.cpp.
57 static int64_t GenerateUniqueCreationTime(int64_t aCreationTime);
59 // public helper to create an Cookie object.
60 static already_AddRefed<Cookie> Create(
61 const CookieStruct& aCookieData,
62 const OriginAttributes& aOriginAttributes);
64 // Same as Cookie::Create but fixes the lastAccessed and creationDates
65 // if they are set in the future.
66 // Should only get called from CookiePersistentStorage::InitDBConn
67 static already_AddRefed<Cookie> CreateValidated(
68 const CookieStruct& aCookieData,
69 const OriginAttributes& aOriginAttributes);
71 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
73 // fast (inline, non-xpcom) getters
74 inline const nsCString& Name() const { return mData.name(); }
75 inline const nsCString& Value() const { return mData.value(); }
76 inline const nsCString& Host() const { return mData.host(); }
77 inline nsDependentCSubstring RawHost() const {
78 return nsDependentCSubstring(mData.host(), IsDomain() ? 1 : 0);
80 inline const nsCString& Path() const { return mData.path(); }
81 const nsCString& GetFilePath();
82 inline int64_t Expiry() const { return mData.expiry(); } // in seconds
83 inline int64_t LastAccessed() const {
84 return mData.lastAccessed();
85 } // in microseconds
86 inline int64_t CreationTime() const {
87 return mData.creationTime();
88 } // in microseconds
89 inline bool IsSession() const { return mData.isSession(); }
90 inline bool IsDomain() const { return *mData.host().get() == '.'; }
91 inline bool IsSecure() const { return mData.isSecure(); }
92 inline bool IsHttpOnly() const { return mData.isHttpOnly(); }
93 inline bool IsPartitioned() const {
94 return !mOriginAttributes.mPartitionKey.IsEmpty();
96 inline bool RawIsPartitioned() const { return mData.isPartitioned(); }
97 inline const OriginAttributes& OriginAttributesRef() const {
98 return mOriginAttributes;
100 inline int32_t SameSite() const { return mData.sameSite(); }
101 inline int32_t RawSameSite() const { return mData.rawSameSite(); }
102 inline bool IsDefaultSameSite() const {
103 return SameSite() == nsICookie::SAMESITE_LAX &&
104 RawSameSite() == nsICookie::SAMESITE_NONE;
106 inline uint8_t SchemeMap() const { return mData.schemeMap(); }
108 // setters
109 inline void SetExpiry(int64_t aExpiry) { mData.expiry() = aExpiry; }
110 inline void SetLastAccessed(int64_t aTime) { mData.lastAccessed() = aTime; }
111 inline void SetIsSession(bool aIsSession) { mData.isSession() = aIsSession; }
112 inline bool SetIsHttpOnly(bool aIsHttpOnly) {
113 return mData.isHttpOnly() = aIsHttpOnly;
115 // Set the creation time manually, overriding the monotonicity checks in
116 // Create(). Use with caution!
117 inline void SetCreationTime(int64_t aTime) { mData.creationTime() = aTime; }
118 inline void SetSchemeMap(uint8_t aSchemeMap) {
119 mData.schemeMap() = aSchemeMap;
121 inline void SetHost(const nsACString& aHost) { mData.host() = aHost; }
123 bool IsStale() const;
125 const CookieStruct& ToIPC() const { return mData; }
127 already_AddRefed<Cookie> Clone() const;
129 protected:
130 virtual ~Cookie() = default;
132 private:
133 // member variables
135 // Please update SizeOfIncludingThis if this strategy changes.
136 CookieStruct mData;
137 OriginAttributes mOriginAttributes;
138 nsCString mFilePathCache;
141 // Comparator class for sorting cookies before sending to a server.
142 class CompareCookiesForSending {
143 public:
144 bool Equals(const Cookie* aCookie1, const Cookie* aCookie2) const {
145 return aCookie1->CreationTime() == aCookie2->CreationTime() &&
146 aCookie2->Path().Length() == aCookie1->Path().Length();
149 bool LessThan(const Cookie* aCookie1, const Cookie* aCookie2) const {
150 // compare by cookie path length in accordance with RFC2109
151 int32_t result = aCookie2->Path().Length() - aCookie1->Path().Length();
152 if (result != 0) return result < 0;
154 // when path lengths match, older cookies should be listed first. this is
155 // required for backwards compatibility since some websites erroneously
156 // depend on receiving cookies in the order in which they were sent to the
157 // browser! see bug 236772.
158 return aCookie1->CreationTime() < aCookie2->CreationTime();
162 } // namespace net
163 } // namespace mozilla
165 #endif // mozilla_net_Cookie_h