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: #ifndef nsCookie_h__ michael@0: #define nsCookie_h__ michael@0: michael@0: #include "nsICookie.h" michael@0: #include "nsICookie2.h" michael@0: #include "nsString.h" michael@0: michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: /** michael@0: * The nsCookie class is the main cookie storage medium for use within cookie michael@0: * code. It implements nsICookie2, which extends nsICookie, a frozen interface michael@0: * for xpcom access of cookie objects. michael@0: */ michael@0: michael@0: /****************************************************************************** michael@0: * nsCookie: michael@0: * implementation michael@0: ******************************************************************************/ michael@0: michael@0: class nsCookie : public nsICookie2 michael@0: { michael@0: public: michael@0: // nsISupports michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSICOOKIE michael@0: NS_DECL_NSICOOKIE2 michael@0: michael@0: private: michael@0: // for internal use only. see nsCookie::Create(). michael@0: nsCookie(const char *aName, michael@0: const char *aValue, michael@0: const char *aHost, michael@0: const char *aPath, michael@0: const char *aEnd, 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: : mName(aName) michael@0: , mValue(aValue) michael@0: , mHost(aHost) michael@0: , mPath(aPath) michael@0: , mEnd(aEnd) michael@0: , mExpiry(aExpiry) michael@0: , mLastAccessed(aLastAccessed) michael@0: , mCreationTime(aCreationTime) michael@0: , mIsSession(aIsSession != false) michael@0: , mIsSecure(aIsSecure != false) michael@0: , mIsHttpOnly(aIsHttpOnly != false) michael@0: { michael@0: } michael@0: michael@0: public: michael@0: // Generate a unique and monotonically increasing creation time. See comment michael@0: // in nsCookie.cpp. michael@0: static int64_t GenerateUniqueCreationTime(int64_t aCreationTime); michael@0: michael@0: // public helper to create an nsCookie object. use |operator delete| michael@0: // to destroy an object created by this method. michael@0: static nsCookie * Create(const nsACString &aName, michael@0: const nsACString &aValue, michael@0: const nsACString &aHost, 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: virtual ~nsCookie() {} michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: // fast (inline, non-xpcom) getters michael@0: inline const nsDependentCString Name() const { return nsDependentCString(mName, mValue - 1); } michael@0: inline const nsDependentCString Value() const { return nsDependentCString(mValue, mHost - 1); } michael@0: inline const nsDependentCString Host() const { return nsDependentCString(mHost, mPath - 1); } michael@0: inline const nsDependentCString RawHost() const { return nsDependentCString(IsDomain() ? mHost + 1 : mHost, mPath - 1); } michael@0: inline const nsDependentCString Path() const { return nsDependentCString(mPath, mEnd); } michael@0: inline int64_t Expiry() const { return mExpiry; } // in seconds michael@0: inline int64_t LastAccessed() const { return mLastAccessed; } // in microseconds michael@0: inline int64_t CreationTime() const { return mCreationTime; } // in microseconds michael@0: inline bool IsSession() const { return mIsSession; } michael@0: inline bool IsDomain() const { return *mHost == '.'; } michael@0: inline bool IsSecure() const { return mIsSecure; } michael@0: inline bool IsHttpOnly() const { return mIsHttpOnly; } michael@0: michael@0: // setters michael@0: inline void SetExpiry(int64_t aExpiry) { mExpiry = aExpiry; } michael@0: inline void SetLastAccessed(int64_t aTime) { mLastAccessed = aTime; } michael@0: inline void SetIsSession(bool aIsSession) { mIsSession = (bool) aIsSession; } michael@0: // Set the creation time manually, overriding the monotonicity checks in michael@0: // Create(). Use with caution! michael@0: inline void SetCreationTime(int64_t aTime) { mCreationTime = aTime; } michael@0: michael@0: protected: michael@0: // member variables michael@0: // we use char* ptrs to store the strings in a contiguous block, michael@0: // so we save on the overhead of using nsCStrings. However, we michael@0: // store a terminating null for each string, so we can hand them michael@0: // out as nsAFlatCStrings. michael@0: // michael@0: // Please update SizeOfIncludingThis if this strategy changes. michael@0: const char *mName; michael@0: const char *mValue; michael@0: const char *mHost; michael@0: const char *mPath; michael@0: const char *mEnd; michael@0: int64_t mExpiry; michael@0: int64_t mLastAccessed; michael@0: int64_t mCreationTime; michael@0: bool mIsSession; michael@0: bool mIsSecure; michael@0: bool mIsHttpOnly; michael@0: }; michael@0: michael@0: #endif // nsCookie_h__