LCOV - code coverage report
Current view: directory - extensions/cookie - nsPermissionManager.h (source / functions) Found Hit Coverage
Test: app.info Lines: 29 27 93.1 %
Date: 2012-06-02 Functions: 11 11 100.0 %

       1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Michiel van Leeuwen (mvl@exedo.nl)
      24                 :  *   Daniel Witte (dwitte@stanford.edu)
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef nsPermissionManager_h__
      41                 : #define nsPermissionManager_h__
      42                 : 
      43                 : #include "nsIPermissionManager.h"
      44                 : #include "nsIObserver.h"
      45                 : #include "nsIObserverService.h"
      46                 : #include "nsWeakReference.h"
      47                 : #include "nsCOMPtr.h"
      48                 : #include "nsIFile.h"
      49                 : #include "nsTHashtable.h"
      50                 : #include "nsTArray.h"
      51                 : #include "nsString.h"
      52                 : #include "nsPermission.h"
      53                 : 
      54                 : class nsIPermission;
      55                 : class nsIIDNService;
      56                 : class mozIStorageConnection;
      57                 : class mozIStorageStatement;
      58                 : 
      59                 : ////////////////////////////////////////////////////////////////////////////////
      60                 : 
      61                 : class nsPermissionEntry
      62             450 : {
      63                 : public:
      64             450 :   nsPermissionEntry(PRUint32 aType, PRUint32 aPermission, PRInt64 aID, 
      65                 :                     PRUint32 aExpireType, PRInt64 aExpireTime)
      66                 :    : mType(aType)
      67                 :    , mPermission(aPermission)
      68                 :    , mID(aID)
      69                 :    , mExpireType(aExpireType)
      70             450 :    , mExpireTime(aExpireTime) {}
      71                 : 
      72                 :   PRUint32 mType;
      73                 :   PRUint32 mPermission;
      74                 :   PRInt64  mID;
      75                 :   PRUint32 mExpireType;
      76                 :   PRInt64  mExpireTime;
      77                 : };
      78                 : 
      79                 : class nsHostEntry : public PLDHashEntryHdr
      80                 : {
      81                 : public:
      82                 :   // Hash methods
      83                 :   typedef const char* KeyType;
      84                 :   typedef const char* KeyTypePointer;
      85                 : 
      86                 :   nsHostEntry(const char* aHost);
      87                 :   nsHostEntry(const nsHostEntry& toCopy);
      88                 : 
      89             445 :   ~nsHostEntry()
      90             445 :   {
      91             445 :   }
      92                 : 
      93             513 :   KeyType GetKey() const
      94                 :   {
      95             513 :     return mHost;
      96                 :   }
      97                 : 
      98             108 :   bool KeyEquals(KeyTypePointer aKey) const
      99                 :   {
     100             108 :     return !strcmp(mHost, aKey);
     101                 :   }
     102                 : 
     103            1189 :   static KeyTypePointer KeyToPointer(KeyType aKey)
     104                 :   {
     105            1189 :     return aKey;
     106                 :   }
     107                 : 
     108            1189 :   static PLDHashNumber HashKey(KeyTypePointer aKey)
     109                 :   {
     110                 :     // PL_DHashStringKey doesn't use the table parameter, so we can safely
     111                 :     // pass nsnull
     112            1189 :     return PL_DHashStringKey(nsnull, aKey);
     113                 :   }
     114                 : 
     115                 :   // force the hashtable to use the copy constructor when shuffling entries
     116                 :   // around, otherwise the Auto part of our nsAutoTArray won't be happy!
     117                 :   enum { ALLOW_MEMMOVE = false };
     118                 : 
     119                 :   // Permissions methods
     120              13 :   inline const nsDependentCString GetHost() const
     121                 :   {
     122              13 :     return nsDependentCString(mHost);
     123                 :   }
     124                 : 
     125             638 :   inline nsTArray<nsPermissionEntry> & GetPermissions()
     126                 :   {
     127             638 :     return mPermissions;
     128                 :   }
     129                 : 
     130             513 :   inline PRInt32 GetPermissionIndex(PRUint32 aType) const
     131                 :   {
     132             547 :     for (PRUint32 i = 0; i < mPermissions.Length(); ++i)
     133              80 :       if (mPermissions[i].mType == aType)
     134              46 :         return i;
     135                 : 
     136             467 :     return -1;
     137                 :   }
     138                 : 
     139              79 :   inline nsPermissionEntry GetPermission(PRUint32 aType) const
     140                 :   {
     141              97 :     for (PRUint32 i = 0; i < mPermissions.Length(); ++i)
     142              97 :       if (mPermissions[i].mType == aType)
     143              79 :         return mPermissions[i];
     144                 : 
     145                 :     // unknown permission... return relevant data 
     146                 :     nsPermissionEntry unk = nsPermissionEntry(aType, nsIPermissionManager::UNKNOWN_ACTION,
     147               0 :                                               -1, nsIPermissionManager::EXPIRE_NEVER, 0);
     148               0 :     return unk;
     149                 :   }
     150                 : 
     151                 : private:
     152                 :   const char *mHost;
     153                 :   nsAutoTArray<nsPermissionEntry, 1> mPermissions;
     154                 : };
     155                 : 
     156                 : 
     157                 : class nsPermissionManager : public nsIPermissionManager,
     158                 :                             public nsIObserver,
     159                 :                             public nsSupportsWeakReference
     160                 : {
     161                 : public:
     162                 : 
     163                 :   // nsISupports
     164                 :   NS_DECL_ISUPPORTS
     165                 :   NS_DECL_NSIPERMISSIONMANAGER
     166                 :   NS_DECL_NSIOBSERVER
     167                 : 
     168                 :   nsPermissionManager();
     169                 :   virtual ~nsPermissionManager();
     170                 :   static nsIPermissionManager* GetXPCOMSingleton();
     171                 :   nsresult Init();
     172                 : 
     173                 :   // enums for AddInternal()
     174                 :   enum OperationType {
     175                 :     eOperationNone,
     176                 :     eOperationAdding,
     177                 :     eOperationRemoving,
     178                 :     eOperationChanging
     179                 :   };
     180                 : 
     181                 :   enum DBOperationType {
     182                 :     eNoDBOperation,
     183                 :     eWriteToDB
     184                 :   };
     185                 : 
     186                 :   enum NotifyOperationType {
     187                 :     eDontNotify,
     188                 :     eNotify
     189                 :   };
     190                 : 
     191                 :   nsresult AddInternal(const nsAFlatCString &aHost,
     192                 :                        const nsAFlatCString &aType,
     193                 :                        PRUint32 aPermission,
     194                 :                        PRInt64 aID,
     195                 :                        PRUint32 aExpireType,
     196                 :                        PRInt64  aExpireTime,
     197                 :                        NotifyOperationType aNotifyOperation,
     198                 :                        DBOperationType aDBOperation);
     199                 : 
     200                 : private:
     201                 : 
     202                 :   PRInt32 GetTypeIndex(const char *aTypeString,
     203                 :                        bool        aAdd);
     204                 : 
     205                 :   nsHostEntry *GetHostEntry(const nsAFlatCString &aHost,
     206                 :                             PRUint32              aType,
     207                 :                             bool                  aExactHostMatch);
     208                 : 
     209                 :   nsresult CommonTestPermission(nsIURI     *aURI,
     210                 :                                 const char *aType,
     211                 :                                 PRUint32   *aPermission,
     212                 :                                 bool        aExactHostMatch);
     213                 : 
     214                 :   nsresult InitDB(bool aRemoveFile);
     215                 :   nsresult CreateTable();
     216                 :   nsresult Import();
     217                 :   nsresult Read();
     218                 :   void     NotifyObserversWithPermission(const nsACString &aHost,
     219                 :                                          const nsCString  &aType,
     220                 :                                          PRUint32          aPermission,
     221                 :                                          PRUint32          aExpireType,
     222                 :                                          PRInt64           aExpireTime,
     223                 :                                          const PRUnichar  *aData);
     224                 :   void     NotifyObservers(nsIPermission *aPermission, const PRUnichar *aData);
     225                 : 
     226                 :   // Finalize all statements, close the DB and null it.
     227                 :   void     CloseDB();
     228                 : 
     229                 :   nsresult RemoveAllInternal();
     230                 :   nsresult RemoveAllFromMemory();
     231                 :   nsresult NormalizeToACE(nsCString &aHost);
     232                 :   nsresult GetHost(nsIURI *aURI, nsACString &aResult);
     233                 :   static void UpdateDB(OperationType         aOp,
     234                 :                        mozIStorageStatement* aStmt,
     235                 :                        PRInt64               aID,
     236                 :                        const nsACString     &aHost,
     237                 :                        const nsACString     &aType,
     238                 :                        PRUint32              aPermission,
     239                 :                        PRUint32              aExpireType,
     240                 :                        PRInt64               aExpireTime);
     241                 : 
     242                 :   nsCOMPtr<nsIObserverService> mObserverService;
     243                 :   nsCOMPtr<nsIIDNService>      mIDNService;
     244                 : 
     245                 :   nsCOMPtr<mozIStorageConnection> mDBConn;
     246                 :   nsCOMPtr<mozIStorageStatement> mStmtInsert;
     247                 :   nsCOMPtr<mozIStorageStatement> mStmtDelete;
     248                 :   nsCOMPtr<mozIStorageStatement> mStmtUpdate;
     249                 : 
     250                 :   nsTHashtable<nsHostEntry>    mHostTable;
     251                 :   // a unique, monotonically increasing id used to identify each database entry
     252                 :   PRInt64                      mLargestID;
     253                 : 
     254                 :   // An array to store the strings identifying the different types.
     255                 :   nsTArray<nsCString>          mTypeArray;
     256                 : };
     257                 : 
     258                 : // {4F6B5E00-0C36-11d5-A535-0010A401EB10}
     259                 : #define NS_PERMISSIONMANAGER_CID \
     260                 : { 0x4f6b5e00, 0xc36, 0x11d5, { 0xa5, 0x35, 0x0, 0x10, 0xa4, 0x1, 0xeb, 0x10 } }
     261                 : 
     262                 : #endif /* nsPermissionManager_h__ */

Generated by: LCOV version 1.7