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
)
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
);
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
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();
86 inline int64_t CreationTime() const {
87 return mData
.creationTime();
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 const OriginAttributes
& OriginAttributesRef() const {
94 return mOriginAttributes
;
96 inline int32_t SameSite() const { return mData
.sameSite(); }
97 inline int32_t RawSameSite() const { return mData
.rawSameSite(); }
98 inline bool IsDefaultSameSite() const {
99 return SameSite() == nsICookie::SAMESITE_LAX
&&
100 RawSameSite() == nsICookie::SAMESITE_NONE
;
102 inline uint8_t SchemeMap() const { return mData
.schemeMap(); }
105 inline void SetExpiry(int64_t aExpiry
) { mData
.expiry() = aExpiry
; }
106 inline void SetLastAccessed(int64_t aTime
) { mData
.lastAccessed() = aTime
; }
107 inline void SetIsSession(bool aIsSession
) { mData
.isSession() = aIsSession
; }
108 inline bool SetIsHttpOnly(bool aIsHttpOnly
) {
109 return mData
.isHttpOnly() = aIsHttpOnly
;
111 // Set the creation time manually, overriding the monotonicity checks in
112 // Create(). Use with caution!
113 inline void SetCreationTime(int64_t aTime
) { mData
.creationTime() = aTime
; }
114 inline void SetSchemeMap(uint8_t aSchemeMap
) {
115 mData
.schemeMap() = aSchemeMap
;
117 inline void SetHost(const nsACString
& aHost
) { mData
.host() = aHost
; }
119 bool IsStale() const;
121 const CookieStruct
& ToIPC() const { return mData
; }
123 already_AddRefed
<Cookie
> Clone() const;
126 virtual ~Cookie() = default;
131 // Please update SizeOfIncludingThis if this strategy changes.
133 OriginAttributes mOriginAttributes
;
134 nsCString mFilePathCache
;
137 // Comparator class for sorting cookies before sending to a server.
138 class CompareCookiesForSending
{
140 bool Equals(const Cookie
* aCookie1
, const Cookie
* aCookie2
) const {
141 return aCookie1
->CreationTime() == aCookie2
->CreationTime() &&
142 aCookie2
->Path().Length() == aCookie1
->Path().Length();
145 bool LessThan(const Cookie
* aCookie1
, const Cookie
* aCookie2
) const {
146 // compare by cookie path length in accordance with RFC2109
147 int32_t result
= aCookie2
->Path().Length() - aCookie1
->Path().Length();
148 if (result
!= 0) return result
< 0;
150 // when path lengths match, older cookies should be listed first. this is
151 // required for backwards compatibility since some websites erroneously
152 // depend on receiving cookies in the order in which they were sent to the
153 // browser! see bug 236772.
154 return aCookie1
->CreationTime() < aCookie2
->CreationTime();
159 } // namespace mozilla
161 #endif // mozilla_net_Cookie_h