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 id, each time we
79 // create a new nsCookie. The creation id is nominally the time (in microseconds)
80 // the cookie was created. This id also corresponds to the row id used in the
81 // sqlite database, which must be unique. However, since it's possible two cookies
82 // may be created at the same time, or the system clock isn't monotonic, we must
83 // check each id to enforce monotonicity.
84 static PRInt64 gLastCreationID
;
87 nsCookie::Create(const nsACString
&aName
,
88 const nsACString
&aValue
,
89 const nsACString
&aHost
,
90 const nsACString
&aPath
,
92 PRInt64 aLastAccessed
,
98 // find the required string buffer size, adding 4 for the terminating nulls
99 const PRUint32 stringLength
= aName
.Length() + aValue
.Length() +
100 aHost
.Length() + aPath
.Length() + 4;
102 // allocate contiguous space for the nsCookie and its strings -
103 // we store the strings in-line with the nsCookie to save allocations
104 void *place
= ::operator new(sizeof(nsCookie
) + stringLength
);
108 // assign string members
109 char *name
, *value
, *host
, *path
, *end
;
110 name
= static_cast<char *>(place
) + sizeof(nsCookie
);
111 StrBlockCopy(aName
, aValue
, aHost
, aPath
,
112 name
, value
, host
, path
, end
);
114 // check if the creation id given to us is greater than the running maximum
115 // (it should always be monotonically increasing). if it's not, make up our own.
116 if (aCreationID
> gLastCreationID
)
117 gLastCreationID
= aCreationID
;
119 aCreationID
= ++gLastCreationID
;
121 // construct the cookie. placement new, oh yeah!
122 return new (place
) nsCookie(name
, value
, host
, path
, end
,
123 aExpiry
, aLastAccessed
, aCreationID
,
124 aIsSession
, aIsSecure
, aIsHttpOnly
);
127 /******************************************************************************
130 ******************************************************************************/
133 NS_IMETHODIMP
nsCookie::GetName(nsACString
&aName
) { aName
= Name(); return NS_OK
; }
134 NS_IMETHODIMP
nsCookie::GetValue(nsACString
&aValue
) { aValue
= Value(); return NS_OK
; }
135 NS_IMETHODIMP
nsCookie::GetHost(nsACString
&aHost
) { aHost
= Host(); return NS_OK
; }
136 NS_IMETHODIMP
nsCookie::GetRawHost(nsACString
&aHost
) { aHost
= RawHost(); return NS_OK
; }
137 NS_IMETHODIMP
nsCookie::GetPath(nsACString
&aPath
) { aPath
= Path(); return NS_OK
; }
138 NS_IMETHODIMP
nsCookie::GetExpiry(PRInt64
*aExpiry
) { *aExpiry
= Expiry(); return NS_OK
; }
139 NS_IMETHODIMP
nsCookie::GetIsSession(PRBool
*aIsSession
) { *aIsSession
= IsSession(); return NS_OK
; }
140 NS_IMETHODIMP
nsCookie::GetIsDomain(PRBool
*aIsDomain
) { *aIsDomain
= IsDomain(); return NS_OK
; }
141 NS_IMETHODIMP
nsCookie::GetIsSecure(PRBool
*aIsSecure
) { *aIsSecure
= IsSecure(); return NS_OK
; }
142 NS_IMETHODIMP
nsCookie::GetIsHttpOnly(PRBool
*aHttpOnly
) { *aHttpOnly
= IsHttpOnly(); return NS_OK
; }
143 NS_IMETHODIMP
nsCookie::GetStatus(nsCookieStatus
*aStatus
) { *aStatus
= 0; return NS_OK
; }
144 NS_IMETHODIMP
nsCookie::GetPolicy(nsCookiePolicy
*aPolicy
) { *aPolicy
= 0; return NS_OK
; }
146 // compatibility method, for use with the legacy nsICookie interface.
147 // here, expires == 0 denotes a session cookie.
149 nsCookie::GetExpires(PRUint64
*aExpires
)
154 *aExpires
= Expiry() > 0 ? Expiry() : 1;
159 NS_IMPL_ISUPPORTS2(nsCookie
, nsICookie2
, nsICookie
)