Bug 448909 - Need more controls WHATWG Video tag. r=mconnor, r=bz
[wine-gecko.git] / extensions / cookie / nsPermissionManager.h
blob57b3a66abeb69ea2838c658a7605849ed73250a0
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
13 * License.
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.
22 * Contributor(s):
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"
47 #include "nsCOMPtr.h"
48 #include "nsIFile.h"
49 #include "nsTHashtable.h"
50 #include "nsTArray.h"
51 #include "nsString.h"
53 class nsIPermission;
54 class nsIIDNService;
55 class mozIStorageConnection;
56 class mozIStorageStatement;
58 ////////////////////////////////////////////////////////////////////////////////
60 class nsPermissionEntry
62 public:
63 nsPermissionEntry(PRUint32 aType, PRUint32 aPermission, PRInt64 aID)
64 : mType(aType)
65 , mPermission(aPermission)
66 , mID(aID) {}
68 PRUint32 mType;
69 PRUint32 mPermission;
70 PRInt64 mID;
73 class nsHostEntry : public PLDHashEntryHdr
75 public:
76 // Hash methods
77 typedef const char* KeyType;
78 typedef const char* KeyTypePointer;
80 nsHostEntry(const char* aHost);
81 nsHostEntry(const nsHostEntry& toCopy);
83 ~nsHostEntry()
87 KeyType GetKey() const
89 return mHost;
92 PRBool KeyEquals(KeyTypePointer aKey) const
94 return !strcmp(mHost, aKey);
97 static KeyTypePointer KeyToPointer(KeyType aKey)
99 return aKey;
102 static PLDHashNumber HashKey(KeyTypePointer aKey)
104 // PL_DHashStringKey doesn't use the table parameter, so we can safely
105 // pass nsnull
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()
121 return mPermissions;
124 inline PRInt32 GetPermissionIndex(PRUint32 aType) const
126 for (PRUint32 i = 0; i < mPermissions.Length(); ++i)
127 if (mPermissions[i].mType == aType)
128 return i;
130 return -1;
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;
142 private:
143 const char *mHost;
144 nsAutoTArray<nsPermissionEntry, 1> mPermissions;
148 class nsPermissionManager : public nsIPermissionManager,
149 public nsIObserver,
150 public nsSupportsWeakReference
152 public:
154 // nsISupports
155 NS_DECL_ISUPPORTS
156 NS_DECL_NSIPERMISSIONMANAGER
157 NS_DECL_NSIOBSERVER
159 nsPermissionManager();
160 virtual ~nsPermissionManager();
161 nsresult Init();
163 private:
165 // enums for AddInternal()
166 enum OperationType {
167 eOperationNone,
168 eOperationAdding,
169 eOperationRemoving,
170 eOperationChanging
173 enum DBOperationType {
174 eNoDBOperation,
175 eWriteToDB
178 enum NotifyOperationType {
179 eDontNotify,
180 eNotify
183 nsresult AddInternal(const nsAFlatCString &aHost,
184 const nsAFlatCString &aType,
185 PRUint32 aPermission,
186 PRInt64 aID,
187 NotifyOperationType aNotifyOperation,
188 DBOperationType aDBOperation);
190 PRInt32 GetTypeIndex(const char *aTypeString,
191 PRBool aAdd);
193 nsHostEntry *GetHostEntry(const nsAFlatCString &aHost,
194 PRUint32 aType,
195 PRBool aExactHostMatch);
197 nsresult CommonTestPermission(nsIURI *aURI,
198 const char *aType,
199 PRUint32 *aPermission,
200 PRBool aExactHostMatch);
202 nsresult InitDB();
203 nsresult CreateTable();
204 nsresult Import();
205 nsresult Read();
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,
217 PRInt64 aID,
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
232 PRInt64 mLargestID;
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__ */