michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCookie.h" michael@0: #include "nsUTF8ConverterService.h" michael@0: #include michael@0: michael@0: /****************************************************************************** michael@0: * nsCookie: michael@0: * string helper impl michael@0: ******************************************************************************/ michael@0: michael@0: // copy aSource strings into contiguous storage provided in aDest1, michael@0: // providing terminating nulls for each destination string. michael@0: static inline void michael@0: StrBlockCopy(const nsACString &aSource1, michael@0: const nsACString &aSource2, michael@0: const nsACString &aSource3, michael@0: const nsACString &aSource4, michael@4: const nsACString &aSource5, michael@0: char *&aDest1, michael@0: char *&aDest2, michael@0: char *&aDest3, michael@0: char *&aDest4, michael@4: char *&aDest5, michael@0: char *&aDestEnd) michael@0: { michael@0: char *toBegin = aDest1; michael@0: nsACString::const_iterator fromBegin, fromEnd; michael@0: michael@0: *copy_string(aSource1.BeginReading(fromBegin), aSource1.EndReading(fromEnd), toBegin) = char(0); michael@0: aDest2 = ++toBegin; michael@0: *copy_string(aSource2.BeginReading(fromBegin), aSource2.EndReading(fromEnd), toBegin) = char(0); michael@0: aDest3 = ++toBegin; michael@0: *copy_string(aSource3.BeginReading(fromBegin), aSource3.EndReading(fromEnd), toBegin) = char(0); michael@0: aDest4 = ++toBegin; michael@0: *copy_string(aSource4.BeginReading(fromBegin), aSource4.EndReading(fromEnd), toBegin) = char(0); michael@4: aDest5 = ++toBegin; michael@4: *copy_string(aSource5.BeginReading(fromBegin), aSource5.EndReading(fromEnd), toBegin) = char(0); michael@0: aDestEnd = toBegin; michael@0: } michael@0: michael@0: /****************************************************************************** michael@0: * nsCookie: michael@0: * creation helper michael@0: ******************************************************************************/ michael@0: michael@0: // This is a counter that keeps track of the last used creation time, each time michael@0: // we create a new nsCookie. This is nominally the time (in microseconds) the michael@0: // cookie was created, but is guaranteed to be monotonically increasing for michael@0: // cookies added at runtime after the database has been read in. This is michael@0: // necessary to enforce ordering among cookies whose creation times would michael@0: // otherwise overlap, since it's possible two cookies may be created at the same michael@0: // time, or that the system clock isn't monotonic. michael@0: static int64_t gLastCreationTime; michael@0: michael@0: int64_t michael@0: nsCookie::GenerateUniqueCreationTime(int64_t aCreationTime) michael@0: { michael@0: // Check if the creation time given to us is greater than the running maximum michael@0: // (it should always be monotonically increasing). michael@0: if (aCreationTime > gLastCreationTime) { michael@0: gLastCreationTime = aCreationTime; michael@0: return aCreationTime; michael@0: } michael@0: michael@0: // Make up our own. michael@0: return ++gLastCreationTime; michael@0: } michael@0: michael@0: nsCookie * michael@0: nsCookie::Create(const nsACString &aName, michael@0: const nsACString &aValue, michael@0: const nsACString &aHost, michael@4: const nsACString &aOrigin, michael@0: const nsACString &aPath, michael@0: int64_t aExpiry, michael@0: int64_t aLastAccessed, michael@0: int64_t aCreationTime, michael@0: bool aIsSession, michael@0: bool aIsSecure, michael@0: bool aIsHttpOnly) michael@0: { michael@0: // Ensure mValue contains a valid UTF-8 sequence. Otherwise XPConnect will michael@0: // truncate the string after the first invalid octet. michael@0: nsUTF8ConverterService converter; michael@0: nsAutoCString aUTF8Value; michael@0: converter.ConvertStringToUTF8(aValue, "UTF-8", false, true, 1, aUTF8Value); michael@0: michael@4: // find the required string buffer size, accommodating terminating nulls michael@0: const uint32_t stringLength = aName.Length() + aUTF8Value.Length() + michael@4: aHost.Length() + aOrigin.Length() + michael@4: aPath.Length() + 5; michael@0: michael@0: // allocate contiguous space for the nsCookie and its strings - michael@0: // we store the strings in-line with the nsCookie to save allocations michael@0: void *place = ::operator new(sizeof(nsCookie) + stringLength); michael@0: if (!place) michael@0: return nullptr; michael@0: michael@0: // assign string members michael@4: char *name, *value, *host, *origin, *path, *end; michael@0: name = static_cast(place) + sizeof(nsCookie); michael@4: StrBlockCopy(aName, aUTF8Value, aHost, aOrigin, aPath, michael@4: name, value, host, origin, path, end); michael@0: michael@0: // If the creationTime given to us is higher than the running maximum, update michael@0: // our maximum. michael@0: if (aCreationTime > gLastCreationTime) michael@0: gLastCreationTime = aCreationTime; michael@0: michael@0: // construct the cookie. placement new, oh yeah! michael@4: return new (place) nsCookie(name, value, host, origin, path, end, michael@0: aExpiry, aLastAccessed, aCreationTime, michael@0: aIsSession, aIsSecure, aIsHttpOnly); michael@0: } michael@0: michael@0: size_t michael@0: nsCookie::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: // There is no need to measure the sizes of the individual string michael@0: // members, since the strings are stored in-line with the nsCookie. michael@0: return aMallocSizeOf(this); michael@0: } michael@0: michael@0: /****************************************************************************** michael@0: * nsCookie: michael@0: * xpcom impl michael@0: ******************************************************************************/ michael@0: michael@0: // xpcom getters michael@0: NS_IMETHODIMP nsCookie::GetName(nsACString &aName) { aName = Name(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetValue(nsACString &aValue) { aValue = Value(); return NS_OK; } michael@4: NS_IMETHODIMP nsCookie::GetOrigin(nsACString &aOrigin) { aOrigin = Origin(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetHost(nsACString &aHost) { aHost = Host(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetRawHost(nsACString &aHost) { aHost = RawHost(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetPath(nsACString &aPath) { aPath = Path(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetExpiry(int64_t *aExpiry) { *aExpiry = Expiry(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetIsSession(bool *aIsSession) { *aIsSession = IsSession(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetIsDomain(bool *aIsDomain) { *aIsDomain = IsDomain(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetIsSecure(bool *aIsSecure) { *aIsSecure = IsSecure(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetIsHttpOnly(bool *aHttpOnly) { *aHttpOnly = IsHttpOnly(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetStatus(nsCookieStatus *aStatus) { *aStatus = 0; return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetPolicy(nsCookiePolicy *aPolicy) { *aPolicy = 0; return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetCreationTime(int64_t *aCreation){ *aCreation = CreationTime(); return NS_OK; } michael@0: NS_IMETHODIMP nsCookie::GetLastAccessed(int64_t *aTime) { *aTime = LastAccessed(); return NS_OK; } michael@0: michael@0: // compatibility method, for use with the legacy nsICookie interface. michael@0: // here, expires == 0 denotes a session cookie. michael@0: NS_IMETHODIMP michael@0: nsCookie::GetExpires(uint64_t *aExpires) michael@0: { michael@0: if (IsSession()) { michael@0: *aExpires = 0; michael@0: } else { michael@0: *aExpires = Expiry() > 0 ? Expiry() : 1; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsCookie, nsICookie2, nsICookie)