netwerk/cookie/nsCookie.h

changeset 0
6474c204b198
child 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:48441bd5886e
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
15 /**
16 * The nsCookie class is the main cookie storage medium for use within cookie
17 * code. It implements nsICookie2, which extends nsICookie, a frozen interface
18 * for xpcom access of cookie objects.
19 */
20
21 /******************************************************************************
22 * nsCookie:
23 * implementation
24 ******************************************************************************/
25
26 class nsCookie : public nsICookie2
27 {
28 public:
29 // nsISupports
30 NS_DECL_ISUPPORTS
31 NS_DECL_NSICOOKIE
32 NS_DECL_NSICOOKIE2
33
34 private:
35 // for internal use only. see nsCookie::Create().
36 nsCookie(const char *aName,
37 const char *aValue,
38 const char *aHost,
39 const char *aPath,
40 const char *aEnd,
41 int64_t aExpiry,
42 int64_t aLastAccessed,
43 int64_t aCreationTime,
44 bool aIsSession,
45 bool aIsSecure,
46 bool aIsHttpOnly)
47 : mName(aName)
48 , mValue(aValue)
49 , mHost(aHost)
50 , mPath(aPath)
51 , mEnd(aEnd)
52 , mExpiry(aExpiry)
53 , mLastAccessed(aLastAccessed)
54 , mCreationTime(aCreationTime)
55 , mIsSession(aIsSession != false)
56 , mIsSecure(aIsSecure != false)
57 , mIsHttpOnly(aIsHttpOnly != false)
58 {
59 }
60
61 public:
62 // Generate a unique and monotonically increasing creation time. See comment
63 // in nsCookie.cpp.
64 static int64_t GenerateUniqueCreationTime(int64_t aCreationTime);
65
66 // public helper to create an nsCookie object. use |operator delete|
67 // to destroy an object created by this method.
68 static nsCookie * Create(const nsACString &aName,
69 const nsACString &aValue,
70 const nsACString &aHost,
71 const nsACString &aPath,
72 int64_t aExpiry,
73 int64_t aLastAccessed,
74 int64_t aCreationTime,
75 bool aIsSession,
76 bool aIsSecure,
77 bool aIsHttpOnly);
78
79 virtual ~nsCookie() {}
80
81 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
82
83 // fast (inline, non-xpcom) getters
84 inline const nsDependentCString Name() const { return nsDependentCString(mName, mValue - 1); }
85 inline const nsDependentCString Value() const { return nsDependentCString(mValue, mHost - 1); }
86 inline const nsDependentCString Host() const { return nsDependentCString(mHost, mPath - 1); }
87 inline const nsDependentCString RawHost() const { return nsDependentCString(IsDomain() ? mHost + 1 : mHost, mPath - 1); }
88 inline const nsDependentCString Path() const { return nsDependentCString(mPath, mEnd); }
89 inline int64_t Expiry() const { return mExpiry; } // in seconds
90 inline int64_t LastAccessed() const { return mLastAccessed; } // in microseconds
91 inline int64_t CreationTime() const { return mCreationTime; } // in microseconds
92 inline bool IsSession() const { return mIsSession; }
93 inline bool IsDomain() const { return *mHost == '.'; }
94 inline bool IsSecure() const { return mIsSecure; }
95 inline bool IsHttpOnly() const { return mIsHttpOnly; }
96
97 // setters
98 inline void SetExpiry(int64_t aExpiry) { mExpiry = aExpiry; }
99 inline void SetLastAccessed(int64_t aTime) { mLastAccessed = aTime; }
100 inline void SetIsSession(bool aIsSession) { mIsSession = (bool) aIsSession; }
101 // Set the creation time manually, overriding the monotonicity checks in
102 // Create(). Use with caution!
103 inline void SetCreationTime(int64_t aTime) { mCreationTime = aTime; }
104
105 protected:
106 // member variables
107 // we use char* ptrs to store the strings in a contiguous block,
108 // so we save on the overhead of using nsCStrings. However, we
109 // store a terminating null for each string, so we can hand them
110 // out as nsAFlatCStrings.
111 //
112 // Please update SizeOfIncludingThis if this strategy changes.
113 const char *mName;
114 const char *mValue;
115 const char *mHost;
116 const char *mPath;
117 const char *mEnd;
118 int64_t mExpiry;
119 int64_t mLastAccessed;
120 int64_t mCreationTime;
121 bool mIsSession;
122 bool mIsSecure;
123 bool mIsHttpOnly;
124 };
125
126 #endif // nsCookie_h__

mercurial