Back to index

lightning-sunbird  0.9+nobinonly
nsPermissionManager.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  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsPermissionManager_h__
00039 #define nsPermissionManager_h__
00040 
00041 #include "nsIPermissionManager.h"
00042 #include "nsIObserver.h"
00043 #include "nsIObserverService.h"
00044 #include "nsWeakReference.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsIFile.h"
00047 #include "nsTHashtable.h"
00048 #include "nsString.h"
00049 #include "nsITimer.h"
00050 
00051 class nsIPermission;
00052 
00054 
00055 // This allow us 8 types of permissions, with 256 values for each
00056 // permission (some types might want to prompt, block 3rd party etc).
00057 // Note that nsIPermissionManager.idl only allows 16 values for permission,
00058 // and that nsPermissionManager::Write() can only deal with 26 values of
00059 // permission safely. (We allow space for 256 here, since it's faster to
00060 // deal with bytes than with bits).
00061 // Note: When changing NUMBER_OF_TYPES, also update PermissionsAreEmpty()
00062 // and the constructors.
00063 // This should be a multiple of 4, to make PermissionsAreEmpty() fast
00064 #define NUMBER_OF_TYPES       (8)
00065 #define NUMBER_OF_PERMISSIONS (16)
00066 
00067 class nsHostEntry : public PLDHashEntryHdr
00068 {
00069 public:
00070   // Hash methods
00071   typedef const char* KeyType;
00072   typedef const char* KeyTypePointer;
00073 
00074   nsHostEntry(const char* aHost);
00075   nsHostEntry(const nsHostEntry& toCopy);
00076 
00077   ~nsHostEntry()
00078   {
00079   }
00080 
00081   KeyType GetKey() const
00082   {
00083     return mHost;
00084   }
00085 
00086   KeyTypePointer GetKeyPointer() const
00087   {
00088     return mHost;
00089   }
00090 
00091   PRBool KeyEquals(KeyTypePointer aKey) const
00092   {
00093     return !strcmp(mHost, aKey);
00094   }
00095 
00096   static KeyTypePointer KeyToPointer(KeyType aKey)
00097   {
00098     return aKey;
00099   }
00100 
00101   static PLDHashNumber HashKey(KeyTypePointer aKey)
00102   {
00103     // PL_DHashStringKey doesn't use the table parameter, so we can safely
00104     // pass nsnull
00105     return PL_DHashStringKey(nsnull, aKey);
00106   }
00107 
00108   enum { ALLOW_MEMMOVE = PR_TRUE };
00109 
00110   // Permissions methods
00111   inline const nsDependentCString GetHost() const
00112   {
00113     return nsDependentCString(mHost);
00114   }
00115 
00116   // Callers must do boundary checks
00117   void SetPermission(PRInt32 aTypeIndex, PRUint32 aPermission)
00118   {
00119     mPermissions[aTypeIndex] = (PRUint8)aPermission;
00120   }
00121 
00122   PRUint32 GetPermission(PRInt32 aTypeIndex) const
00123   {
00124     return (PRUint32)mPermissions[aTypeIndex];
00125   }
00126 
00127   PRBool PermissionsAreEmpty() const
00128   {
00129     // Cast to PRUint32, to make this faster. Only 2 checks instead of 8
00130     return (*NS_REINTERPRET_CAST(const PRUint32*, &mPermissions[0])==0 && 
00131             *NS_REINTERPRET_CAST(const PRUint32*, &mPermissions[4])==0 );
00132   }
00133 
00134 private:
00135   const char *mHost;
00136 
00137   // This will contain the permissions for different types, in a bitmasked
00138   // form. The type itself defines the position.
00139   // One byte per permission. This is a small loss of memory (as 255 types
00140   // of action per type is a lot, 16 would be enough), but will improve speed
00141   // Bytemasking is around twice as fast (on one arch at least) than bitmasking
00142   PRUint8 mPermissions[NUMBER_OF_TYPES];
00143 };
00144 
00145 
00146 class nsPermissionManager : public nsIPermissionManager,
00147                             public nsIObserver,
00148                             public nsSupportsWeakReference
00149 {
00150 public:
00151 
00152   // nsISupports
00153   NS_DECL_ISUPPORTS
00154   NS_DECL_NSIPERMISSIONMANAGER
00155   NS_DECL_NSIOBSERVER
00156 
00157   nsPermissionManager();
00158   virtual ~nsPermissionManager();
00159   nsresult Init();
00160 
00161 private:
00162 
00163   nsresult AddInternal(const nsAFlatCString &aHost,
00164                        PRInt32  aTypeIndex,
00165                        PRUint32 aPermission,
00166                        PRBool   aNotify);
00167   PRInt32 GetTypeIndex(const char *aTypeString,
00168                        PRBool      aAdd);
00169 
00170   nsHostEntry *GetHostEntry(const nsAFlatCString &aHost,
00171                             PRUint32              aType);
00172 
00173   // Use LazyWrite to save the permissions file on a timer. It will write
00174   // the file only once if repeatedly hammered quickly.
00175   void        LazyWrite();
00176   static void DoLazyWrite(nsITimer *aTimer, void *aClosure);
00177   nsresult    Write();
00178 
00179   nsresult Read();
00180   void     NotifyObserversWithPermission(const nsACString &aHost,
00181                                          const char       *aType,
00182                                          PRUint32          aPermission,
00183                                          const PRUnichar  *aData);
00184   void     NotifyObservers(nsIPermission *aPermission, const PRUnichar *aData);
00185   nsresult RemoveAllFromMemory();
00186   nsresult GetHost(nsIURI *aURI, nsACString &aResult);
00187   void     RemoveTypeStrings();
00188 
00189   nsCOMPtr<nsIObserverService> mObserverService;
00190   nsCOMPtr<nsIFile>            mPermissionsFile;
00191   nsCOMPtr<nsITimer>           mWriteTimer;
00192   nsTHashtable<nsHostEntry>    mHostTable;
00193   PRUint32                     mHostCount;
00194   PRPackedBool                 mChangedList;
00195   PRPackedBool                 mHasUnknownTypes;
00196 
00197   // An array to store the strings identifying the different types.
00198   char                        *mTypeArray[NUMBER_OF_TYPES];
00199 };
00200 
00201 // {4F6B5E00-0C36-11d5-A535-0010A401EB10}
00202 #define NS_PERMISSIONMANAGER_CID \
00203 { 0x4f6b5e00, 0xc36, 0x11d5, { 0xa5, 0x35, 0x0, 0x10, 0xa4, 0x1, 0xeb, 0x10 } }
00204 
00205 #endif /* nsPermissionManager_h__ */