1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
23 * Daniel Witte (dwitte@stanford.edu)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 /******************************************************************************
45 ******************************************************************************/
47 // copy aSource strings into contiguous storage provided in aDest1,
48 // providing terminating nulls for each destination string.
50 StrBlockCopy(const nsACString
&aSource1
,
51 const nsACString
&aSource2
,
52 const nsACString
&aSource3
,
53 const nsACString
&aSource4
,
60 char *toBegin
= aDest1
;
61 nsACString::const_iterator fromBegin
, fromEnd
;
63 *copy_string(aSource1
.BeginReading(fromBegin
), aSource1
.EndReading(fromEnd
), toBegin
) = char(0);
65 *copy_string(aSource2
.BeginReading(fromBegin
), aSource2
.EndReading(fromEnd
), toBegin
) = char(0);
67 *copy_string(aSource3
.BeginReading(fromBegin
), aSource3
.EndReading(fromEnd
), toBegin
) = char(0);
69 *copy_string(aSource4
.BeginReading(fromBegin
), aSource4
.EndReading(fromEnd
), toBegin
) = char(0);
73 /******************************************************************************
76 ******************************************************************************/
78 // This is a counter that keeps track of the last used creation time, each time
79 // we create a new nsCookie. This is nominally the time (in microseconds) the
80 // cookie was created, but is guaranteed to be monotonically increasing for
81 // cookies added at runtime after the database has been read in. This is
82 // necessary to enforce ordering among cookies whose creation times would
83 // otherwise overlap, since it's possible two cookies may be created at the same
84 // time, or that the system clock isn't monotonic.
85 static PRInt64 gLastCreationTime
;
88 nsCookie::GenerateUniqueCreationTime(PRInt64 aCreationTime
)
90 // Check if the creation time given to us is greater than the running maximum
91 // (it should always be monotonically increasing).
92 if (aCreationTime
> gLastCreationTime
) {
93 gLastCreationTime
= aCreationTime
;
98 return ++gLastCreationTime
;
102 nsCookie::Create(const nsACString
&aName
,
103 const nsACString
&aValue
,
104 const nsACString
&aHost
,
105 const nsACString
&aPath
,
107 PRInt64 aLastAccessed
,
108 PRInt64 aCreationTime
,
113 // find the required string buffer size, adding 4 for the terminating nulls
114 const PRUint32 stringLength
= aName
.Length() + aValue
.Length() +
115 aHost
.Length() + aPath
.Length() + 4;
117 // allocate contiguous space for the nsCookie and its strings -
118 // we store the strings in-line with the nsCookie to save allocations
119 void *place
= ::operator new(sizeof(nsCookie
) + stringLength
);
123 // assign string members
124 char *name
, *value
, *host
, *path
, *end
;
125 name
= static_cast<char *>(place
) + sizeof(nsCookie
);
126 StrBlockCopy(aName
, aValue
, aHost
, aPath
,
127 name
, value
, host
, path
, end
);
129 // If the creationTime given to us is higher than the running maximum, update
131 if (aCreationTime
> gLastCreationTime
)
132 gLastCreationTime
= aCreationTime
;
134 // construct the cookie. placement new, oh yeah!
135 return new (place
) nsCookie(name
, value
, host
, path
, end
,
136 aExpiry
, aLastAccessed
, aCreationTime
,
137 aIsSession
, aIsSecure
, aIsHttpOnly
);
140 /******************************************************************************
143 ******************************************************************************/
146 NS_IMETHODIMP
nsCookie::GetName(nsACString
&aName
) { aName
= Name(); return NS_OK
; }
147 NS_IMETHODIMP
nsCookie::GetValue(nsACString
&aValue
) { aValue
= Value(); return NS_OK
; }
148 NS_IMETHODIMP
nsCookie::GetHost(nsACString
&aHost
) { aHost
= Host(); return NS_OK
; }
149 NS_IMETHODIMP
nsCookie::GetRawHost(nsACString
&aHost
) { aHost
= RawHost(); return NS_OK
; }
150 NS_IMETHODIMP
nsCookie::GetPath(nsACString
&aPath
) { aPath
= Path(); return NS_OK
; }
151 NS_IMETHODIMP
nsCookie::GetExpiry(PRInt64
*aExpiry
) { *aExpiry
= Expiry(); return NS_OK
; }
152 NS_IMETHODIMP
nsCookie::GetIsSession(PRBool
*aIsSession
) { *aIsSession
= IsSession(); return NS_OK
; }
153 NS_IMETHODIMP
nsCookie::GetIsDomain(PRBool
*aIsDomain
) { *aIsDomain
= IsDomain(); return NS_OK
; }
154 NS_IMETHODIMP
nsCookie::GetIsSecure(PRBool
*aIsSecure
) { *aIsSecure
= IsSecure(); return NS_OK
; }
155 NS_IMETHODIMP
nsCookie::GetIsHttpOnly(PRBool
*aHttpOnly
) { *aHttpOnly
= IsHttpOnly(); return NS_OK
; }
156 NS_IMETHODIMP
nsCookie::GetStatus(nsCookieStatus
*aStatus
) { *aStatus
= 0; return NS_OK
; }
157 NS_IMETHODIMP
nsCookie::GetPolicy(nsCookiePolicy
*aPolicy
) { *aPolicy
= 0; return NS_OK
; }
158 NS_IMETHODIMP
nsCookie::GetCreationTime(PRInt64
*aCreation
){ *aCreation
= CreationTime(); return NS_OK
; }
159 NS_IMETHODIMP
nsCookie::GetLastAccessed(PRInt64
*aTime
) { *aTime
= LastAccessed(); return NS_OK
; }
161 // compatibility method, for use with the legacy nsICookie interface.
162 // here, expires == 0 denotes a session cookie.
164 nsCookie::GetExpires(PRUint64
*aExpires
)
169 *aExpires
= Expiry() > 0 ? Expiry() : 1;
174 NS_IMPL_ISUPPORTS2(nsCookie
, nsICookie2
, nsICookie
)