Back to index

lightning-sunbird  0.9+nobinonly
nsDOMStorage.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  * Mozilla Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2006
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Neil Deakin <enndeakin@sympatico.ca>
00024  *   Johnny Stenback <jst@mozilla.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsDOMStorage_h___
00041 #define nsDOMStorage_h___
00042 
00043 #include "nscore.h"
00044 #include "nsAutoPtr.h"
00045 #include "nsIDOMStorage.h"
00046 #include "nsIDOMStorageList.h"
00047 #include "nsIDOMStorageItem.h"
00048 #include "nsInterfaceHashtable.h"
00049 #include "nsVoidArray.h"
00050 #include "nsPIDOMStorage.h"
00051 #include "nsIDOMToString.h"
00052 #include "nsDOMEvent.h"
00053 #include "nsIDOMStorageEvent.h"
00054 
00055 #ifdef MOZ_STORAGE
00056 #include "nsDOMStorageDB.h"
00057 #endif
00058 
00059 class nsDOMStorage;
00060 class nsDOMStorageItem;
00061 
00062 class nsDOMStorageEntry : public nsVoidPtrHashKey
00063 {
00064 public:
00065   nsDOMStorageEntry(KeyTypePointer aStr);
00066   nsDOMStorageEntry(const nsDOMStorageEntry& aToCopy);
00067   ~nsDOMStorageEntry();
00068 
00069   // weak reference so that it can be deleted when no longer used
00070   nsDOMStorage* mStorage;
00071 };
00072 
00073 class nsSessionStorageEntry : public nsStringHashKey
00074 {
00075 public:
00076   nsSessionStorageEntry(KeyTypePointer aStr);
00077   nsSessionStorageEntry(const nsSessionStorageEntry& aToCopy);
00078   ~nsSessionStorageEntry();
00079 
00080   nsRefPtr<nsDOMStorageItem> mItem;
00081 };
00082 
00083 class nsDOMStorageManager : public nsIObserver
00084 {
00085 public:
00086   // nsISupports
00087   NS_DECL_ISUPPORTS
00088 
00089   // nsIObserver
00090   NS_DECL_NSIOBSERVER
00091 
00092   void AddToStoragesHash(nsDOMStorage* aStorage);
00093   void RemoveFromStoragesHash(nsDOMStorage* aStorage);
00094 
00095   nsresult ClearAllStorages();
00096 
00097   static nsresult Initialize();
00098   static void Shutdown();
00099 
00100   static nsDOMStorageManager* gStorageManager;
00101 
00102 protected:
00103 
00104   nsTHashtable<nsDOMStorageEntry> mStorages;
00105 };
00106 
00107 class nsDOMStorage : public nsIDOMStorage,
00108                      public nsPIDOMStorage
00109 {
00110 public:
00111   nsDOMStorage();
00112   nsDOMStorage(nsIURI* aURI, const nsAString& aDomain, PRBool aUseDB);
00113   virtual ~nsDOMStorage();
00114 
00115   // nsISupports
00116   NS_DECL_ISUPPORTS
00117 
00118   // nsIDOMStorage
00119   NS_DECL_NSIDOMSTORAGE
00120 
00121   // nsPIDOMStorage
00122   virtual void Init(nsIURI* aURI, const nsAString& aDomain, PRBool aUseDB);
00123   virtual already_AddRefed<nsIDOMStorage> Clone(nsIURI* aURI);
00124   virtual nsTArray<nsString> *GetKeys();
00125 
00126   PRBool UseDB() { return mUseDB && !mSessionOnly; }
00127 
00128   // cache whether storage may be used by aURI, and whether it is session
00129   // only. If aURI is null, the uri associated with this storage (mURI)
00130   // is checked. Returns true if storage may be used.
00131   static PRBool
00132   CanUseStorage(nsIURI* aURI, PRPackedBool* aSessionOnly);
00133 
00134   PRBool
00135   CacheStoragePermissions()
00136   {
00137     return CanUseStorage(mURI, &mSessionOnly);
00138   }
00139 
00140   // retrieve the value and secure state corresponding to a key out of storage.
00141   nsresult
00142   GetDBValue(const nsAString& aKey,
00143              nsAString& aValue,
00144              PRBool* aSecure,
00145              nsAString& aOwner);
00146 
00147   // set the value corresponding to a key in the storage. If
00148   // aSecure is false, then attempts to modify a secure value
00149   // throw NS_ERROR_DOM_INVALID_ACCESS_ERR
00150   nsresult
00151   SetDBValue(const nsAString& aKey,
00152              const nsAString& aValue,
00153              PRBool aSecure);
00154 
00155   // set the value corresponding to a key as secure.
00156   nsresult
00157   SetSecure(const nsAString& aKey, PRBool aSecure);
00158 
00159   // clear all values from the store
00160   void ClearAll();
00161 
00162 protected:
00163 
00164   friend class nsDOMStorageManager;
00165 
00166   static nsresult InitDB();
00167 
00168   // cache the keys from the database for faster lookup
00169   nsresult CacheKeysFromDB();
00170 
00171   void BroadcastChangeNotification();
00172 
00173   // true if the storage database should be used for values
00174   PRPackedBool mUseDB;
00175 
00176   // true if the preferences indicates that this storage should be session only
00177   PRPackedBool mSessionOnly;
00178 
00179   // true if items from the database are cached
00180   PRPackedBool mItemsCached;
00181 
00182   // the URI this store is associated with
00183   nsCOMPtr<nsIURI> mURI;
00184 
00185   // domain this store is associated with
00186   nsAutoString mDomain;
00187 
00188   // the key->value item pairs
00189   nsTHashtable<nsSessionStorageEntry> mItems;
00190 
00191 #ifdef MOZ_STORAGE
00192   static nsDOMStorageDB* gStorageDB;
00193 #endif
00194 };
00195 
00196 class nsDOMStorageList : public nsIDOMStorageList
00197 {
00198 public:
00199   nsDOMStorageList()
00200   {
00201     mStorages.Init();
00202   };
00203 
00204   virtual ~nsDOMStorageList() {};
00205 
00206   // nsISupports
00207   NS_DECL_ISUPPORTS
00208 
00209   // nsIDOMStorageList
00210   NS_DECL_NSIDOMSTORAGELIST
00211 
00215   static PRBool
00216   CanAccessDomain(const nsAString& aRequestedDomain,
00217                   const nsAString& aCurrentDomain);
00218 
00219 protected:
00220 
00231   nsresult
00232   GetStorageForDomain(nsIURI* aURI,
00233                       const nsAString& aRequestedDomain,
00234                       const nsAString& aCurrentDomain,
00235                       PRBool aNoCurrentDomainCheck,
00236                       nsIDOMStorage** aStorage);
00237 
00241   static PRBool
00242   ConvertDomainToArray(const nsAString& aDomain,
00243                        nsStringArray* aArray);
00244 
00245   nsInterfaceHashtable<nsStringHashKey, nsIDOMStorage> mStorages;
00246 };
00247 
00248 class nsDOMStorageItem : public nsIDOMStorageItem,
00249                          public nsIDOMToString
00250 {
00251 public:
00252   nsDOMStorageItem(nsDOMStorage* aStorage,
00253                    const nsAString& aKey,
00254                    const nsAString& aValue,
00255                    PRBool aSecure);
00256   virtual ~nsDOMStorageItem();
00257 
00258   // nsISupports
00259   NS_DECL_ISUPPORTS
00260 
00261   // nsIDOMStorage
00262   NS_DECL_NSIDOMSTORAGEITEM
00263 
00264   // nsIDOMToString
00265   NS_DECL_NSIDOMTOSTRING
00266 
00267   PRBool IsSecure()
00268   {
00269     return mSecure;
00270   }
00271 
00272   void SetSecureInternal(PRBool aSecure)
00273   {
00274     mSecure = aSecure;
00275   }
00276 
00277   const nsAString& GetValueInternal()
00278   {
00279     return mValue;
00280   }
00281 
00282   const void SetValueInternal(const nsAString& aValue)
00283   {
00284     mValue = aValue;
00285   }
00286 
00287   void ClearValue()
00288   {
00289     mValue.Truncate();
00290   }
00291 
00292 protected:
00293 
00294   // true if this value is for secure sites only
00295   PRBool mSecure;
00296 
00297   // key for the item
00298   nsString mKey;
00299 
00300   // value of the item
00301   nsString mValue;
00302 
00303   // If this item came from the db, mStorage points to the storage
00304   // object where this item came from.
00305   nsRefPtr<nsDOMStorage> mStorage;
00306 };
00307 
00308 class nsDOMStorageEvent : public nsDOMEvent,
00309                           public nsIDOMStorageEvent
00310 {
00311 public:
00312   nsDOMStorageEvent(const nsAString& aDomain)
00313     : nsDOMEvent(nsnull, nsnull), mDomain(aDomain)
00314   {
00315     if (aDomain.IsEmpty()) {
00316       // An empty domain means this event is for a session sotrage
00317       // object change. Store #session as the domain.
00318 
00319       mDomain = NS_LITERAL_STRING("#session");
00320     }
00321   }
00322 
00323   virtual ~nsDOMStorageEvent()
00324   {
00325   }
00326 
00327   nsresult Init();
00328 
00329   NS_DECL_ISUPPORTS
00330   NS_DECL_NSIDOMSTORAGEEVENT
00331   NS_FORWARD_NSIDOMEVENT(nsDOMEvent::)
00332 
00333 protected:
00334   nsString mDomain;
00335 };
00336 
00337 NS_IMETHODIMP
00338 NS_NewDOMStorage(nsISupports* aOuter, REFNSIID aIID, void** aResult);
00339 
00340 nsresult
00341 NS_NewDOMStorageList(nsIDOMStorageList** aResult);
00342 
00343 #endif /* nsDOMStorage_h___ */