/src/mozilla-central/netwerk/cookie/nsCookie.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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/. */ |
5 | | |
6 | | #ifndef nsCookie_h__ |
7 | | #define nsCookie_h__ |
8 | | |
9 | | #include "nsICookie.h" |
10 | | #include "nsICookie2.h" |
11 | | #include "nsString.h" |
12 | | |
13 | | #include "mozilla/MemoryReporting.h" |
14 | | #include "mozilla/BasePrincipal.h" |
15 | | #include "mozilla/Preferences.h" |
16 | | |
17 | | using mozilla::OriginAttributes; |
18 | | |
19 | | /** |
20 | | * The nsCookie class is the main cookie storage medium for use within cookie |
21 | | * code. It implements nsICookie2, which extends nsICookie, a frozen interface |
22 | | * for xpcom access of cookie objects. |
23 | | */ |
24 | | |
25 | | /****************************************************************************** |
26 | | * nsCookie: |
27 | | * implementation |
28 | | ******************************************************************************/ |
29 | | |
30 | | class nsCookie final : public nsICookie2 |
31 | | { |
32 | | public: |
33 | | // nsISupports |
34 | | NS_DECL_ISUPPORTS |
35 | | NS_DECL_NSICOOKIE |
36 | | NS_DECL_NSICOOKIE2 |
37 | | |
38 | | private: |
39 | | // for internal use only. see nsCookie::Create(). |
40 | | nsCookie(const char *aName, |
41 | | const char *aValue, |
42 | | const char *aHost, |
43 | | const char *aPath, |
44 | | const char *aEnd, |
45 | | int64_t aExpiry, |
46 | | int64_t aLastAccessed, |
47 | | int64_t aCreationTime, |
48 | | bool aIsSession, |
49 | | bool aIsSecure, |
50 | | bool aIsHttpOnly, |
51 | | const OriginAttributes& aOriginAttributes, |
52 | | int32_t aSameSite) |
53 | | : mName(aName) |
54 | | , mValue(aValue) |
55 | | , mHost(aHost) |
56 | | , mPath(aPath) |
57 | | , mEnd(aEnd) |
58 | | , mExpiry(aExpiry) |
59 | | , mLastAccessed(aLastAccessed) |
60 | | , mCreationTime(aCreationTime) |
61 | | , mIsSession(aIsSession) |
62 | | , mIsSecure(aIsSecure) |
63 | | , mIsHttpOnly(aIsHttpOnly) |
64 | | , mOriginAttributes(aOriginAttributes) |
65 | | , mSameSite(aSameSite) |
66 | 0 | { |
67 | 0 | } |
68 | | |
69 | | static int CookieStaleThreshold() |
70 | 0 | { |
71 | 0 | static bool initialized = false; |
72 | 0 | static int value = 60; |
73 | 0 | if (!initialized) { |
74 | 0 | mozilla::Preferences::AddIntVarCache(&value, "network.cookie.staleThreshold", 60); |
75 | 0 | initialized = true; |
76 | 0 | } |
77 | 0 | return value; |
78 | 0 | } |
79 | | |
80 | | public: |
81 | | // Generate a unique and monotonically increasing creation time. See comment |
82 | | // in nsCookie.cpp. |
83 | | static int64_t GenerateUniqueCreationTime(int64_t aCreationTime); |
84 | | |
85 | | // public helper to create an nsCookie object. use |operator delete| |
86 | | // to destroy an object created by this method. |
87 | | static nsCookie * Create(const nsACString &aName, |
88 | | const nsACString &aValue, |
89 | | const nsACString &aHost, |
90 | | const nsACString &aPath, |
91 | | int64_t aExpiry, |
92 | | int64_t aLastAccessed, |
93 | | int64_t aCreationTime, |
94 | | bool aIsSession, |
95 | | bool aIsSecure, |
96 | | bool aIsHttpOnly, |
97 | | const OriginAttributes& aOriginAttributes, |
98 | | int32_t aSameSite); |
99 | | |
100 | | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
101 | | |
102 | | // fast (inline, non-xpcom) getters |
103 | 0 | inline const nsDependentCString Name() const { return nsDependentCString(mName, mValue - 1); } |
104 | 0 | inline const nsDependentCString Value() const { return nsDependentCString(mValue, mHost - 1); } |
105 | 0 | inline const nsDependentCString Host() const { return nsDependentCString(mHost, mPath - 1); } |
106 | 0 | inline const nsDependentCString RawHost() const { return nsDependentCString(IsDomain() ? mHost + 1 : mHost, mPath - 1); } |
107 | 0 | inline const nsDependentCString Path() const { return nsDependentCString(mPath, mEnd); } |
108 | 0 | inline int64_t Expiry() const { return mExpiry; } // in seconds |
109 | 0 | inline int64_t LastAccessed() const { return mLastAccessed; } // in microseconds |
110 | 0 | inline int64_t CreationTime() const { return mCreationTime; } // in microseconds |
111 | 0 | inline bool IsSession() const { return mIsSession; } |
112 | 0 | inline bool IsDomain() const { return *mHost == '.'; } |
113 | 0 | inline bool IsSecure() const { return mIsSecure; } |
114 | 0 | inline bool IsHttpOnly() const { return mIsHttpOnly; } |
115 | 0 | inline const OriginAttributes& OriginAttributesRef() const { return mOriginAttributes; } |
116 | 0 | inline int32_t SameSite() const { return mSameSite; } |
117 | | |
118 | | // setters |
119 | 0 | inline void SetExpiry(int64_t aExpiry) { mExpiry = aExpiry; } |
120 | 0 | inline void SetLastAccessed(int64_t aTime) { mLastAccessed = aTime; } |
121 | 0 | inline void SetIsSession(bool aIsSession) { mIsSession = aIsSession; } |
122 | | // Set the creation time manually, overriding the monotonicity checks in |
123 | | // Create(). Use with caution! |
124 | 0 | inline void SetCreationTime(int64_t aTime) { mCreationTime = aTime; } |
125 | | |
126 | | bool IsStale() const; |
127 | | |
128 | | protected: |
129 | 0 | virtual ~nsCookie() = default; |
130 | | |
131 | | private: |
132 | | // member variables |
133 | | // we use char* ptrs to store the strings in a contiguous block, |
134 | | // so we save on the overhead of using nsCStrings. However, we |
135 | | // store a terminating null for each string, so we can hand them |
136 | | // out as nsCStrings. |
137 | | // |
138 | | // Please update SizeOfIncludingThis if this strategy changes. |
139 | | const char *mName; |
140 | | const char *mValue; |
141 | | const char *mHost; |
142 | | const char *mPath; |
143 | | const char *mEnd; |
144 | | int64_t mExpiry; |
145 | | int64_t mLastAccessed; |
146 | | int64_t mCreationTime; |
147 | | bool mIsSession; |
148 | | bool mIsSecure; |
149 | | bool mIsHttpOnly; |
150 | | mozilla::OriginAttributes mOriginAttributes; |
151 | | int32_t mSameSite; |
152 | | }; |
153 | | |
154 | | // Comparator class for sorting cookies before sending to a server. |
155 | | class CompareCookiesForSending |
156 | | { |
157 | | public: |
158 | | bool Equals(const nsCookie* aCookie1, const nsCookie* aCookie2) const |
159 | 0 | { |
160 | 0 | return aCookie1->CreationTime() == aCookie2->CreationTime() && |
161 | 0 | aCookie2->Path().Length() == aCookie1->Path().Length(); |
162 | 0 | } |
163 | | |
164 | | bool LessThan(const nsCookie* aCookie1, const nsCookie* aCookie2) const |
165 | 0 | { |
166 | 0 | // compare by cookie path length in accordance with RFC2109 |
167 | 0 | int32_t result = aCookie2->Path().Length() - aCookie1->Path().Length(); |
168 | 0 | if (result != 0) |
169 | 0 | return result < 0; |
170 | 0 | |
171 | 0 | // when path lengths match, older cookies should be listed first. this is |
172 | 0 | // required for backwards compatibility since some websites erroneously |
173 | 0 | // depend on receiving cookies in the order in which they were sent to the |
174 | 0 | // browser! see bug 236772. |
175 | 0 | return aCookie1->CreationTime() < aCookie2->CreationTime(); |
176 | 0 | } |
177 | | }; |
178 | | #endif // nsCookie_h__ |