Line data Source code
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 : 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 0 : 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 0 : : mName(aName)
53 : , mValue(aValue)
54 : , mHost(aHost)
55 : , mPath(aPath)
56 : , mEnd(aEnd)
57 : , mExpiry(aExpiry)
58 : , mLastAccessed(aLastAccessed)
59 : , mCreationTime(aCreationTime)
60 : , mIsSession(aIsSession)
61 : , mIsSecure(aIsSecure)
62 : , mIsHttpOnly(aIsHttpOnly)
63 0 : , mOriginAttributes(aOriginAttributes)
64 : {
65 0 : }
66 :
67 0 : static int CookieStaleThreshold()
68 : {
69 : static bool initialized = false;
70 : static int value = 60;
71 0 : if (!initialized) {
72 0 : mozilla::Preferences::AddIntVarCache(&value, "network.cookie.staleThreshold", 60);
73 0 : initialized = true;
74 : }
75 0 : return value;
76 : }
77 :
78 : public:
79 : // Generate a unique and monotonically increasing creation time. See comment
80 : // in nsCookie.cpp.
81 : static int64_t GenerateUniqueCreationTime(int64_t aCreationTime);
82 :
83 : // public helper to create an nsCookie object. use |operator delete|
84 : // to destroy an object created by this method.
85 : static nsCookie * Create(const nsACString &aName,
86 : const nsACString &aValue,
87 : const nsACString &aHost,
88 : const nsACString &aPath,
89 : int64_t aExpiry,
90 : int64_t aLastAccessed,
91 : int64_t aCreationTime,
92 : bool aIsSession,
93 : bool aIsSecure,
94 : bool aIsHttpOnly,
95 : const OriginAttributes& aOriginAttributes);
96 :
97 : size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
98 :
99 : // fast (inline, non-xpcom) getters
100 0 : inline const nsDependentCString Name() const { return nsDependentCString(mName, mValue - 1); }
101 0 : inline const nsDependentCString Value() const { return nsDependentCString(mValue, mHost - 1); }
102 0 : inline const nsDependentCString Host() const { return nsDependentCString(mHost, mPath - 1); }
103 0 : inline const nsDependentCString RawHost() const { return nsDependentCString(IsDomain() ? mHost + 1 : mHost, mPath - 1); }
104 0 : inline const nsDependentCString Path() const { return nsDependentCString(mPath, mEnd); }
105 0 : inline int64_t Expiry() const { return mExpiry; } // in seconds
106 0 : inline int64_t LastAccessed() const { return mLastAccessed; } // in microseconds
107 0 : inline int64_t CreationTime() const { return mCreationTime; } // in microseconds
108 0 : inline bool IsSession() const { return mIsSession; }
109 0 : inline bool IsDomain() const { return *mHost == '.'; }
110 0 : inline bool IsSecure() const { return mIsSecure; }
111 0 : inline bool IsHttpOnly() const { return mIsHttpOnly; }
112 0 : inline const OriginAttributes& OriginAttributesRef() const { return mOriginAttributes; }
113 :
114 : // setters
115 0 : inline void SetExpiry(int64_t aExpiry) { mExpiry = aExpiry; }
116 0 : inline void SetLastAccessed(int64_t aTime) { mLastAccessed = aTime; }
117 0 : inline void SetIsSession(bool aIsSession) { mIsSession = aIsSession; }
118 : // Set the creation time manually, overriding the monotonicity checks in
119 : // Create(). Use with caution!
120 0 : inline void SetCreationTime(int64_t aTime) { mCreationTime = aTime; }
121 :
122 : bool IsStale() const;
123 :
124 : protected:
125 0 : virtual ~nsCookie() {}
126 :
127 : private:
128 : // member variables
129 : // we use char* ptrs to store the strings in a contiguous block,
130 : // so we save on the overhead of using nsCStrings. However, we
131 : // store a terminating null for each string, so we can hand them
132 : // out as nsCStrings.
133 : //
134 : // Please update SizeOfIncludingThis if this strategy changes.
135 : const char *mName;
136 : const char *mValue;
137 : const char *mHost;
138 : const char *mPath;
139 : const char *mEnd;
140 : int64_t mExpiry;
141 : int64_t mLastAccessed;
142 : int64_t mCreationTime;
143 : bool mIsSession;
144 : bool mIsSecure;
145 : bool mIsHttpOnly;
146 : mozilla::OriginAttributes mOriginAttributes;
147 : };
148 :
149 : #endif // nsCookie_h__
|