Bug 1826566 [wpt PR 39395] - Only merge table columns that have no cell edges., a...
[gecko.git] / netwerk / cookie / Cookie.h
blobd80b0852d1d70a107003ea86509d51ca54393934
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 public:
48 // Returns false if rawSameSite has an invalid value, compared to sameSite.
49 static bool ValidateSameSite(const CookieStruct& aCookieData);
51 // Generate a unique and monotonically increasing creation time. See comment
52 // in Cookie.cpp.
53 static int64_t GenerateUniqueCreationTime(int64_t aCreationTime);
55 // public helper to create an Cookie object.
56 static already_AddRefed<Cookie> Create(
57 const CookieStruct& aCookieData,
58 const OriginAttributes& aOriginAttributes);
60 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
62 // fast (inline, non-xpcom) getters
63 inline const nsCString& Name() const { return mData.name(); }
64 inline const nsCString& Value() const { return mData.value(); }
65 inline const nsCString& Host() const { return mData.host(); }
66 inline nsDependentCSubstring RawHost() const {
67 return nsDependentCSubstring(mData.host(), IsDomain() ? 1 : 0);
69 inline const nsCString& Path() const { return mData.path(); }
70 const nsCString& GetFilePath();
71 inline int64_t Expiry() const { return mData.expiry(); } // in seconds
72 inline int64_t LastAccessed() const {
73 return mData.lastAccessed();
74 } // in microseconds
75 inline int64_t CreationTime() const {
76 return mData.creationTime();
77 } // in microseconds
78 inline bool IsSession() const { return mData.isSession(); }
79 inline bool IsDomain() const { return *mData.host().get() == '.'; }
80 inline bool IsSecure() const { return mData.isSecure(); }
81 inline bool IsHttpOnly() const { return mData.isHttpOnly(); }
82 inline const OriginAttributes& OriginAttributesRef() const {
83 return mOriginAttributes;
85 inline int32_t SameSite() const { return mData.sameSite(); }
86 inline int32_t RawSameSite() const { return mData.rawSameSite(); }
87 inline bool IsDefaultSameSite() const {
88 return SameSite() == nsICookie::SAMESITE_LAX &&
89 RawSameSite() == nsICookie::SAMESITE_NONE;
91 inline uint8_t SchemeMap() const { return mData.schemeMap(); }
93 // setters
94 inline void SetExpiry(int64_t aExpiry) { mData.expiry() = aExpiry; }
95 inline void SetLastAccessed(int64_t aTime) { mData.lastAccessed() = aTime; }
96 inline void SetIsSession(bool aIsSession) { mData.isSession() = aIsSession; }
97 inline bool SetIsHttpOnly(bool aIsHttpOnly) {
98 return mData.isHttpOnly() = aIsHttpOnly;
100 // Set the creation time manually, overriding the monotonicity checks in
101 // Create(). Use with caution!
102 inline void SetCreationTime(int64_t aTime) { mData.creationTime() = aTime; }
103 inline void SetSchemeMap(uint8_t aSchemeMap) {
104 mData.schemeMap() = aSchemeMap;
106 inline void SetHost(const nsACString& aHost) { mData.host() = aHost; }
108 bool IsStale() const;
110 const CookieStruct& ToIPC() const { return mData; }
112 already_AddRefed<Cookie> Clone() const;
114 protected:
115 virtual ~Cookie() = default;
117 private:
118 // member variables
120 // Please update SizeOfIncludingThis if this strategy changes.
121 CookieStruct mData;
122 OriginAttributes mOriginAttributes;
123 nsCString mFilePathCache;
126 // Comparator class for sorting cookies before sending to a server.
127 class CompareCookiesForSending {
128 public:
129 bool Equals(const Cookie* aCookie1, const Cookie* aCookie2) const {
130 return aCookie1->CreationTime() == aCookie2->CreationTime() &&
131 aCookie2->Path().Length() == aCookie1->Path().Length();
134 bool LessThan(const Cookie* aCookie1, const Cookie* aCookie2) const {
135 // compare by cookie path length in accordance with RFC2109
136 int32_t result = aCookie2->Path().Length() - aCookie1->Path().Length();
137 if (result != 0) return result < 0;
139 // when path lengths match, older cookies should be listed first. this is
140 // required for backwards compatibility since some websites erroneously
141 // depend on receiving cookies in the order in which they were sent to the
142 // browser! see bug 236772.
143 return aCookie1->CreationTime() < aCookie2->CreationTime();
147 } // namespace net
148 } // namespace mozilla
150 #endif // mozilla_net_Cookie_h