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
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/
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
15 * The Original Code is mozilla.org code.
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.
23 * Michiel van Leeuwen (mvl@exedo.nl)
24 * Daniel Witte (dwitte@stanford.edu)
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.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsPermissionManager_h__
41 #define nsPermissionManager_h__
43 #include "nsIPermissionManager.h"
44 #include "nsIObserver.h"
45 #include "nsIObserverService.h"
46 #include "nsWeakReference.h"
49 #include "nsTHashtable.h"
55 class mozIStorageConnection
;
56 class mozIStorageStatement
;
58 ////////////////////////////////////////////////////////////////////////////////
60 class nsPermissionEntry
63 nsPermissionEntry(PRUint32 aType
, PRUint32 aPermission
, PRInt64 aID
)
65 , mPermission(aPermission
)
73 class nsHostEntry
: public PLDHashEntryHdr
77 typedef const char* KeyType
;
78 typedef const char* KeyTypePointer
;
80 nsHostEntry(const char* aHost
);
81 nsHostEntry(const nsHostEntry
& toCopy
);
87 KeyType
GetKey() const
92 PRBool
KeyEquals(KeyTypePointer aKey
) const
94 return !strcmp(mHost
, aKey
);
97 static KeyTypePointer
KeyToPointer(KeyType aKey
)
102 static PLDHashNumber
HashKey(KeyTypePointer aKey
)
104 // PL_DHashStringKey doesn't use the table parameter, so we can safely
106 return PL_DHashStringKey(nsnull
, aKey
);
109 // force the hashtable to use the copy constructor when shuffling entries
110 // around, otherwise the Auto part of our nsAutoTArray won't be happy!
111 enum { ALLOW_MEMMOVE
= PR_FALSE
};
113 // Permissions methods
114 inline const nsDependentCString
GetHost() const
116 return nsDependentCString(mHost
);
119 inline nsTArray
<nsPermissionEntry
> & GetPermissions()
124 inline PRInt32
GetPermissionIndex(PRUint32 aType
) const
126 for (PRUint32 i
= 0; i
< mPermissions
.Length(); ++i
)
127 if (mPermissions
[i
].mType
== aType
)
133 inline PRUint32
GetPermission(PRUint32 aType
) const
135 for (PRUint32 i
= 0; i
< mPermissions
.Length(); ++i
)
136 if (mPermissions
[i
].mType
== aType
)
137 return mPermissions
[i
].mPermission
;
139 return nsIPermissionManager::UNKNOWN_ACTION
;
144 nsAutoTArray
<nsPermissionEntry
, 1> mPermissions
;
148 class nsPermissionManager
: public nsIPermissionManager
,
150 public nsSupportsWeakReference
156 NS_DECL_NSIPERMISSIONMANAGER
159 nsPermissionManager();
160 virtual ~nsPermissionManager();
165 // enums for AddInternal()
173 enum DBOperationType
{
178 enum NotifyOperationType
{
183 nsresult
AddInternal(const nsAFlatCString
&aHost
,
184 const nsAFlatCString
&aType
,
185 PRUint32 aPermission
,
187 NotifyOperationType aNotifyOperation
,
188 DBOperationType aDBOperation
);
190 PRInt32
GetTypeIndex(const char *aTypeString
,
193 nsHostEntry
*GetHostEntry(const nsAFlatCString
&aHost
,
195 PRBool aExactHostMatch
);
197 nsresult
CommonTestPermission(nsIURI
*aURI
,
199 PRUint32
*aPermission
,
200 PRBool aExactHostMatch
);
203 nsresult
CreateTable();
206 void NotifyObserversWithPermission(const nsACString
&aHost
,
207 const nsCString
&aType
,
208 PRUint32 aPermission
,
209 const PRUnichar
*aData
);
210 void NotifyObservers(nsIPermission
*aPermission
, const PRUnichar
*aData
);
211 nsresult
RemoveAllInternal();
212 nsresult
RemoveAllFromMemory();
213 nsresult
NormalizeToACE(nsCString
&aHost
);
214 nsresult
GetHost(nsIURI
*aURI
, nsACString
&aResult
);
215 static void UpdateDB(OperationType aOp
,
216 mozIStorageStatement
* aStmt
,
218 const nsACString
&aHost
,
219 const nsACString
&aType
,
220 PRUint32 aPermission
);
222 nsCOMPtr
<nsIObserverService
> mObserverService
;
223 nsCOMPtr
<nsIIDNService
> mIDNService
;
225 nsCOMPtr
<mozIStorageConnection
> mDBConn
;
226 nsCOMPtr
<mozIStorageStatement
> mStmtInsert
;
227 nsCOMPtr
<mozIStorageStatement
> mStmtDelete
;
228 nsCOMPtr
<mozIStorageStatement
> mStmtUpdate
;
230 nsTHashtable
<nsHostEntry
> mHostTable
;
231 // a unique, monotonically increasing id used to identify each database entry
234 // An array to store the strings identifying the different types.
235 nsTArray
<nsCString
> mTypeArray
;
238 // {4F6B5E00-0C36-11d5-A535-0010A401EB10}
239 #define NS_PERMISSIONMANAGER_CID \
240 { 0x4f6b5e00, 0xc36, 0x11d5, { 0xa5, 0x35, 0x0, 0x10, 0xa4, 0x1, 0xeb, 0x10 } }
242 #endif /* nsPermissionManager_h__ */