Back to index

lightning-sunbird  0.9+nobinonly
nsCookie.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Daniel Witte.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Daniel Witte (dwitte@stanford.edu)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsCookie_h__
00040 #define nsCookie_h__
00041 
00042 #include "nsICookie.h"
00043 #include "nsICookie2.h"
00044 #include "nsString.h"
00045 #include "nsInt64.h"
00046 
00053 /******************************************************************************
00054  * nsCookie:
00055  * implementation
00056  ******************************************************************************/
00057 
00058 class nsCookie : public nsICookie2_MOZILLA_1_8_BRANCH
00059 {
00060   // break up the NS_DECL_ISUPPORTS macro, since we use a bitfield refcount member
00061   public:
00062     NS_DECL_ISUPPORTS_INHERITED
00063   protected:
00064     NS_DECL_OWNINGTHREAD
00065 
00066   public:
00067     // nsISupports
00068     NS_DECL_NSICOOKIE
00069     NS_DECL_NSICOOKIE2
00070     NS_DECL_NSICOOKIE2_MOZILLA_1_8_BRANCH
00071 
00072   private:
00073     // for internal use only. see nsCookie::Create().
00074     nsCookie(const char     *aName,
00075              const char     *aValue,
00076              const char     *aHost,
00077              const char     *aPath,
00078              const char     *aEnd,
00079              nsInt64         aExpiry,
00080              nsInt64         aLastAccessed,
00081              PRUint32        aCreationTime,
00082              PRBool          aIsSession,
00083              PRBool          aIsSecure,
00084              PRBool          aIsHttpOnly,
00085              nsCookieStatus  aStatus,
00086              nsCookiePolicy  aPolicy)
00087      : mNext(nsnull)
00088      , mName(aName)
00089      , mValue(aValue)
00090      , mHost(aHost)
00091      , mPath(aPath)
00092      , mEnd(aEnd)
00093      , mExpiry(aExpiry)
00094      , mLastAccessed(aLastAccessed)
00095      , mCreationTime(aCreationTime)
00096      , mRefCnt(0)
00097      , mIsSession(aIsSession != PR_FALSE)
00098      , mIsSecure(aIsSecure != PR_FALSE)
00099      , mIsHttpOnly(aIsHttpOnly != PR_FALSE)
00100      , mStatus(aStatus)
00101      , mPolicy(aPolicy)
00102     {
00103     }
00104 
00105   public:
00106     // public helper to create an nsCookie object. use |operator delete|
00107     // to destroy an object created by this method.
00108     static nsCookie * Create(const nsACString &aName,
00109                              const nsACString &aValue,
00110                              const nsACString &aHost,
00111                              const nsACString &aPath,
00112                              nsInt64           aExpiry,
00113                              nsInt64           aLastAccessed,
00114                              PRBool            aIsSession,
00115                              PRBool            aIsSecure,
00116                              PRBool            aIsHttpOnly,
00117                              nsCookieStatus    aStatus,
00118                              nsCookiePolicy    aPolicy);
00119 
00120     virtual ~nsCookie() {};
00121 
00122     // fast (inline, non-xpcom) getters
00123     inline const nsDependentCString Name()  const { return nsDependentCString(mName, mValue - 1); }
00124     inline const nsDependentCString Value() const { return nsDependentCString(mValue, mHost - 1); }
00125     inline const nsDependentCString Host()  const { return nsDependentCString(mHost, mPath - 1); }
00126     inline const nsDependentCString RawHost() const { return nsDependentCString(IsDomain() ? mHost + 1 : mHost, mPath - 1); }
00127     inline const nsDependentCString Path()  const { return nsDependentCString(mPath, mEnd); }
00128     inline nsInt64 Expiry()                 const { return mExpiry; }
00129     inline nsInt64 LastAccessed()           const { return mLastAccessed; }
00130     inline PRUint32 CreationTime()          const { return mCreationTime; }
00131     inline PRBool IsSession()               const { return mIsSession; }
00132     inline PRBool IsDomain()                const { return *mHost == '.'; }
00133     inline PRBool IsSecure()                const { return mIsSecure; }
00134     inline PRBool IsHttpOnly()              const { return mIsHttpOnly; }
00135     inline nsCookieStatus Status()          const { return mStatus; }
00136     inline nsCookiePolicy Policy()          const { return mPolicy; }
00137 
00138     // setters
00139     inline void SetLastAccessed(nsInt64 aLastAccessed) { mLastAccessed = aLastAccessed; }
00140     inline void SetExpiry(PRInt64 aExpiry)             { mExpiry = aExpiry; }
00141     inline void SetIsSession(PRBool aIsSession)        { mIsSession = aIsSession; }
00142     inline void SetCreationTime(PRUint32 aCT)          { mCreationTime = aCT; }
00143 
00144     // linked list management helper
00145     inline nsCookie*& Next() { return mNext; }
00146 
00147   protected:
00148     // member variables
00149     // we use char* ptrs to store the strings in a contiguous block,
00150     // so we save on the overhead of using nsCStrings. However, we
00151     // store a terminating null for each string, so we can hand them
00152     // out as nsAFlatCStrings.
00153 
00154     nsCookie   *mNext;
00155     const char *mName;
00156     const char *mValue;
00157     const char *mHost;
00158     const char *mPath;
00159     const char *mEnd;
00160     nsInt64     mExpiry;
00161     nsInt64     mLastAccessed;
00162     PRUint32    mCreationTime;
00163     PRUint32    mRefCnt    : 16;
00164     PRUint32    mIsSession : 1;
00165     PRUint32    mIsSecure  : 1;
00166     PRUint32    mIsHttpOnly: 1;
00167     PRUint32    mStatus    : 3;
00168     PRUint32    mPolicy    : 3;
00169 };
00170 
00171 #endif // nsCookie_h__