netwerk/cookie/nsCookie.cpp

changeset 0
6474c204b198
child 4
fc2d59ddac77
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cookie/nsCookie.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsCookie.h"
    1.10 +#include "nsUTF8ConverterService.h"
    1.11 +#include <stdlib.h>
    1.12 +
    1.13 +/******************************************************************************
    1.14 + * nsCookie:
    1.15 + * string helper impl
    1.16 + ******************************************************************************/
    1.17 +
    1.18 +// copy aSource strings into contiguous storage provided in aDest1,
    1.19 +// providing terminating nulls for each destination string.
    1.20 +static inline void
    1.21 +StrBlockCopy(const nsACString &aSource1,
    1.22 +             const nsACString &aSource2,
    1.23 +             const nsACString &aSource3,
    1.24 +             const nsACString &aSource4,
    1.25 +             char             *&aDest1,
    1.26 +             char             *&aDest2,
    1.27 +             char             *&aDest3,
    1.28 +             char             *&aDest4,
    1.29 +             char             *&aDestEnd)
    1.30 +{
    1.31 +  char *toBegin = aDest1;
    1.32 +  nsACString::const_iterator fromBegin, fromEnd;
    1.33 +
    1.34 +  *copy_string(aSource1.BeginReading(fromBegin), aSource1.EndReading(fromEnd), toBegin) = char(0);
    1.35 +  aDest2 = ++toBegin;
    1.36 +  *copy_string(aSource2.BeginReading(fromBegin), aSource2.EndReading(fromEnd), toBegin) = char(0);
    1.37 +  aDest3 = ++toBegin;
    1.38 +  *copy_string(aSource3.BeginReading(fromBegin), aSource3.EndReading(fromEnd), toBegin) = char(0);
    1.39 +  aDest4 = ++toBegin;
    1.40 +  *copy_string(aSource4.BeginReading(fromBegin), aSource4.EndReading(fromEnd), toBegin) = char(0);
    1.41 +  aDestEnd = toBegin;
    1.42 +}
    1.43 +
    1.44 +/******************************************************************************
    1.45 + * nsCookie:
    1.46 + * creation helper
    1.47 + ******************************************************************************/
    1.48 +
    1.49 +// This is a counter that keeps track of the last used creation time, each time
    1.50 +// we create a new nsCookie. This is nominally the time (in microseconds) the
    1.51 +// cookie was created, but is guaranteed to be monotonically increasing for
    1.52 +// cookies added at runtime after the database has been read in. This is
    1.53 +// necessary to enforce ordering among cookies whose creation times would
    1.54 +// otherwise overlap, since it's possible two cookies may be created at the same
    1.55 +// time, or that the system clock isn't monotonic.
    1.56 +static int64_t gLastCreationTime;
    1.57 +
    1.58 +int64_t
    1.59 +nsCookie::GenerateUniqueCreationTime(int64_t aCreationTime)
    1.60 +{
    1.61 +  // Check if the creation time given to us is greater than the running maximum
    1.62 +  // (it should always be monotonically increasing).
    1.63 +  if (aCreationTime > gLastCreationTime) {
    1.64 +    gLastCreationTime = aCreationTime;
    1.65 +    return aCreationTime;
    1.66 +  }
    1.67 +
    1.68 +  // Make up our own.
    1.69 +  return ++gLastCreationTime;
    1.70 +}
    1.71 +
    1.72 +nsCookie *
    1.73 +nsCookie::Create(const nsACString &aName,
    1.74 +                 const nsACString &aValue,
    1.75 +                 const nsACString &aHost,
    1.76 +                 const nsACString &aPath,
    1.77 +                 int64_t           aExpiry,
    1.78 +                 int64_t           aLastAccessed,
    1.79 +                 int64_t           aCreationTime,
    1.80 +                 bool              aIsSession,
    1.81 +                 bool              aIsSecure,
    1.82 +                 bool              aIsHttpOnly)
    1.83 +{
    1.84 +  // Ensure mValue contains a valid UTF-8 sequence. Otherwise XPConnect will
    1.85 +  // truncate the string after the first invalid octet.
    1.86 +  nsUTF8ConverterService converter;
    1.87 +  nsAutoCString aUTF8Value;
    1.88 +  converter.ConvertStringToUTF8(aValue, "UTF-8", false, true, 1, aUTF8Value);
    1.89 +
    1.90 +  // find the required string buffer size, adding 4 for the terminating nulls
    1.91 +  const uint32_t stringLength = aName.Length() + aUTF8Value.Length() +
    1.92 +                                aHost.Length() + aPath.Length() + 4;
    1.93 +
    1.94 +  // allocate contiguous space for the nsCookie and its strings -
    1.95 +  // we store the strings in-line with the nsCookie to save allocations
    1.96 +  void *place = ::operator new(sizeof(nsCookie) + stringLength);
    1.97 +  if (!place)
    1.98 +    return nullptr;
    1.99 +
   1.100 +  // assign string members
   1.101 +  char *name, *value, *host, *path, *end;
   1.102 +  name = static_cast<char *>(place) + sizeof(nsCookie);
   1.103 +  StrBlockCopy(aName, aUTF8Value, aHost, aPath,
   1.104 +               name, value, host, path, end);
   1.105 +
   1.106 +  // If the creationTime given to us is higher than the running maximum, update
   1.107 +  // our maximum.
   1.108 +  if (aCreationTime > gLastCreationTime)
   1.109 +    gLastCreationTime = aCreationTime;
   1.110 +
   1.111 +  // construct the cookie. placement new, oh yeah!
   1.112 +  return new (place) nsCookie(name, value, host, path, end,
   1.113 +                              aExpiry, aLastAccessed, aCreationTime,
   1.114 +                              aIsSession, aIsSecure, aIsHttpOnly);
   1.115 +}
   1.116 +
   1.117 +size_t
   1.118 +nsCookie::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
   1.119 +{
   1.120 +    // There is no need to measure the sizes of the individual string
   1.121 +    // members, since the strings are stored in-line with the nsCookie.
   1.122 +    return aMallocSizeOf(this);
   1.123 +}
   1.124 +
   1.125 +/******************************************************************************
   1.126 + * nsCookie:
   1.127 + * xpcom impl
   1.128 + ******************************************************************************/
   1.129 +
   1.130 +// xpcom getters
   1.131 +NS_IMETHODIMP nsCookie::GetName(nsACString &aName)         { aName = Name();            return NS_OK; }
   1.132 +NS_IMETHODIMP nsCookie::GetValue(nsACString &aValue)       { aValue = Value();          return NS_OK; }
   1.133 +NS_IMETHODIMP nsCookie::GetHost(nsACString &aHost)         { aHost = Host();            return NS_OK; }
   1.134 +NS_IMETHODIMP nsCookie::GetRawHost(nsACString &aHost)      { aHost = RawHost();         return NS_OK; }
   1.135 +NS_IMETHODIMP nsCookie::GetPath(nsACString &aPath)         { aPath = Path();            return NS_OK; }
   1.136 +NS_IMETHODIMP nsCookie::GetExpiry(int64_t *aExpiry)        { *aExpiry = Expiry();       return NS_OK; }
   1.137 +NS_IMETHODIMP nsCookie::GetIsSession(bool *aIsSession)   { *aIsSession = IsSession(); return NS_OK; }
   1.138 +NS_IMETHODIMP nsCookie::GetIsDomain(bool *aIsDomain)     { *aIsDomain = IsDomain();   return NS_OK; }
   1.139 +NS_IMETHODIMP nsCookie::GetIsSecure(bool *aIsSecure)     { *aIsSecure = IsSecure();   return NS_OK; }
   1.140 +NS_IMETHODIMP nsCookie::GetIsHttpOnly(bool *aHttpOnly)   { *aHttpOnly = IsHttpOnly(); return NS_OK; }
   1.141 +NS_IMETHODIMP nsCookie::GetStatus(nsCookieStatus *aStatus) { *aStatus = 0;              return NS_OK; }
   1.142 +NS_IMETHODIMP nsCookie::GetPolicy(nsCookiePolicy *aPolicy) { *aPolicy = 0;              return NS_OK; }
   1.143 +NS_IMETHODIMP nsCookie::GetCreationTime(int64_t *aCreation){ *aCreation = CreationTime(); return NS_OK; }
   1.144 +NS_IMETHODIMP nsCookie::GetLastAccessed(int64_t *aTime)    { *aTime = LastAccessed();   return NS_OK; }
   1.145 +
   1.146 +// compatibility method, for use with the legacy nsICookie interface.
   1.147 +// here, expires == 0 denotes a session cookie.
   1.148 +NS_IMETHODIMP
   1.149 +nsCookie::GetExpires(uint64_t *aExpires)
   1.150 +{
   1.151 +  if (IsSession()) {
   1.152 +    *aExpires = 0;
   1.153 +  } else {
   1.154 +    *aExpires = Expiry() > 0 ? Expiry() : 1;
   1.155 +  }
   1.156 +  return NS_OK;
   1.157 +}
   1.158 +
   1.159 +NS_IMPL_ISUPPORTS(nsCookie, nsICookie2, nsICookie)

mercurial