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
10 #include "nsIMemoryReporter.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
;
24 * The Cookie class is the main cookie storage medium for use within cookie
28 /******************************************************************************
31 ******************************************************************************/
33 class Cookie final
: public nsICookie
{
34 MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf
)
41 static Cookie
* Cast(nsICookie
* aCookie
) {
42 return static_cast<Cookie
*>(aCookie
);
45 static const Cookie
* Cast(const nsICookie
* aCookie
) {
46 return static_cast<const Cookie
*>(aCookie
);
50 // for internal use only. see Cookie::Create().
51 Cookie(const CookieStruct
& aCookieData
,
52 const OriginAttributes
& aOriginAttributes
)
53 : mData(aCookieData
), mOriginAttributes(aOriginAttributes
) {}
55 static already_AddRefed
<Cookie
> FromCookieStruct(
56 const CookieStruct
& aCookieData
,
57 const OriginAttributes
& aOriginAttributes
);
60 // Returns false if rawSameSite has an invalid value, compared to sameSite.
61 static bool ValidateSameSite(const CookieStruct
& aCookieData
);
63 // Generate a unique and monotonically increasing creation time. See comment
65 static int64_t GenerateUniqueCreationTime(int64_t aCreationTime
);
67 // public helper to create an Cookie object.
68 static already_AddRefed
<Cookie
> Create(
69 const CookieStruct
& aCookieData
,
70 const OriginAttributes
& aOriginAttributes
);
72 // Same as Cookie::Create but fixes the lastAccessed and creationDates
73 // if they are set in the future.
74 // Should only get called from CookiePersistentStorage::InitDBConn
75 static already_AddRefed
<Cookie
> CreateValidated(
76 const CookieStruct
& aCookieData
,
77 const OriginAttributes
& aOriginAttributes
);
79 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
81 // fast (inline, non-xpcom) getters
82 inline const nsCString
& Name() const { return mData
.name(); }
83 inline const nsCString
& Value() const { return mData
.value(); }
84 inline const nsCString
& Host() const { return mData
.host(); }
85 inline nsDependentCSubstring
RawHost() const {
86 return nsDependentCSubstring(mData
.host(), IsDomain() ? 1 : 0);
88 inline const nsCString
& Path() const { return mData
.path(); }
89 inline int64_t Expiry() const { return mData
.expiry(); } // in seconds
90 inline int64_t LastAccessed() const {
91 return mData
.lastAccessed();
93 inline int64_t CreationTime() const {
94 return mData
.creationTime();
96 inline bool IsSession() const { return mData
.isSession(); }
97 inline bool IsDomain() const { return *mData
.host().get() == '.'; }
98 inline bool IsSecure() const { return mData
.isSecure(); }
99 inline bool IsHttpOnly() const { return mData
.isHttpOnly(); }
100 inline bool IsPartitioned() const {
101 return !mOriginAttributes
.mPartitionKey
.IsEmpty();
103 inline bool RawIsPartitioned() const { return mData
.isPartitioned(); }
104 inline const OriginAttributes
& OriginAttributesRef() const {
105 return mOriginAttributes
;
107 inline int32_t SameSite() const { return mData
.sameSite(); }
108 inline int32_t RawSameSite() const { return mData
.rawSameSite(); }
109 inline bool IsDefaultSameSite() const {
110 return SameSite() == nsICookie::SAMESITE_LAX
&&
111 RawSameSite() == nsICookie::SAMESITE_NONE
;
113 inline uint8_t SchemeMap() const { return mData
.schemeMap(); }
116 inline void SetExpiry(int64_t aExpiry
) { mData
.expiry() = aExpiry
; }
117 inline void SetLastAccessed(int64_t aTime
) { mData
.lastAccessed() = aTime
; }
118 inline void SetIsSession(bool aIsSession
) { mData
.isSession() = aIsSession
; }
119 inline bool SetIsHttpOnly(bool aIsHttpOnly
) {
120 return mData
.isHttpOnly() = aIsHttpOnly
;
122 // Set the creation time manually, overriding the monotonicity checks in
123 // Create(). Use with caution!
124 inline void SetCreationTime(int64_t aTime
) { mData
.creationTime() = aTime
; }
125 inline void SetSchemeMap(uint8_t aSchemeMap
) {
126 mData
.schemeMap() = aSchemeMap
;
128 inline void SetHost(const nsACString
& aHost
) { mData
.host() = aHost
; }
130 uint32_t NameAndValueBytes() {
131 return mData
.name().Length() + mData
.value().Length();
134 bool IsStale() const;
136 const CookieStruct
& ToIPC() const { return mData
; }
138 already_AddRefed
<Cookie
> Clone() const;
141 virtual ~Cookie() = default;
146 // Please update SizeOfIncludingThis if this strategy changes.
148 OriginAttributes mOriginAttributes
;
151 // Comparator class for sorting cookies before sending to a server.
152 class CompareCookiesForSending
{
154 bool Equals(const nsICookie
* aCookie1
, const nsICookie
* aCookie2
) const {
155 return Cookie::Cast(aCookie1
)->CreationTime() ==
156 Cookie::Cast(aCookie2
)->CreationTime() &&
157 Cookie::Cast(aCookie2
)->Path().Length() ==
158 Cookie::Cast(aCookie1
)->Path().Length();
161 bool LessThan(const nsICookie
* aCookie1
, const nsICookie
* aCookie2
) const {
162 // compare by cookie path length in accordance with RFC2109
163 int32_t result
= Cookie::Cast(aCookie2
)->Path().Length() -
164 Cookie::Cast(aCookie1
)->Path().Length();
165 if (result
!= 0) return result
< 0;
167 // when path lengths match, older cookies should be listed first. this is
168 // required for backwards compatibility since some websites erroneously
169 // depend on receiving cookies in the order in which they were sent to the
170 // browser! see bug 236772.
171 return Cookie::Cast(aCookie1
)->CreationTime() <
172 Cookie::Cast(aCookie2
)->CreationTime();
177 } // namespace mozilla
179 #endif // mozilla_net_Cookie_h