1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/cookie/nsPermissionManager.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,327 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsPermissionManager_h__ 1.10 +#define nsPermissionManager_h__ 1.11 + 1.12 +#include "nsIPermissionManager.h" 1.13 +#include "nsIObserver.h" 1.14 +#include "nsIObserverService.h" 1.15 +#include "nsWeakReference.h" 1.16 +#include "nsCOMPtr.h" 1.17 +#include "nsIFile.h" 1.18 +#include "nsTHashtable.h" 1.19 +#include "nsTArray.h" 1.20 +#include "nsString.h" 1.21 +#include "nsPermission.h" 1.22 +#include "nsHashKeys.h" 1.23 +#include "nsAutoPtr.h" 1.24 +#include "nsCOMArray.h" 1.25 +#include "nsDataHashtable.h" 1.26 + 1.27 +class nsIPermission; 1.28 +class nsIIDNService; 1.29 +class mozIStorageConnection; 1.30 +class mozIStorageAsyncStatement; 1.31 + 1.32 +//////////////////////////////////////////////////////////////////////////////// 1.33 + 1.34 +class nsPermissionManager : public nsIPermissionManager, 1.35 + public nsIObserver, 1.36 + public nsSupportsWeakReference 1.37 +{ 1.38 +public: 1.39 + class PermissionEntry 1.40 + { 1.41 + public: 1.42 + PermissionEntry(int64_t aID, uint32_t aType, uint32_t aPermission, 1.43 + uint32_t aExpireType, int64_t aExpireTime) 1.44 + : mID(aID) 1.45 + , mType(aType) 1.46 + , mPermission(aPermission) 1.47 + , mExpireType(aExpireType) 1.48 + , mExpireTime(aExpireTime) 1.49 + , mNonSessionPermission(aPermission) 1.50 + , mNonSessionExpireType(aExpireType) 1.51 + , mNonSessionExpireTime(aExpireTime) 1.52 + {} 1.53 + 1.54 + int64_t mID; 1.55 + uint32_t mType; 1.56 + uint32_t mPermission; 1.57 + uint32_t mExpireType; 1.58 + int64_t mExpireTime; 1.59 + uint32_t mNonSessionPermission; 1.60 + uint32_t mNonSessionExpireType; 1.61 + uint32_t mNonSessionExpireTime; 1.62 + }; 1.63 + 1.64 + /** 1.65 + * PermissionKey is the key used by PermissionHashKey hash table. 1.66 + * 1.67 + * NOTE: It could be implementing nsIHashable but there is no reason to worry 1.68 + * with XPCOM interfaces while we don't need to. 1.69 + */ 1.70 + class PermissionKey 1.71 + { 1.72 + public: 1.73 + PermissionKey(nsIPrincipal* aPrincipal); 1.74 + PermissionKey(const nsACString& aHost, 1.75 + uint32_t aAppId, 1.76 + bool aIsInBrowserElement) 1.77 + : mHost(aHost) 1.78 + , mAppId(aAppId) 1.79 + , mIsInBrowserElement(aIsInBrowserElement) 1.80 + { 1.81 + } 1.82 + 1.83 + bool operator==(const PermissionKey& aKey) const { 1.84 + return mHost.Equals(aKey.mHost) && 1.85 + mAppId == aKey.mAppId && 1.86 + mIsInBrowserElement == aKey.mIsInBrowserElement; 1.87 + } 1.88 + 1.89 + PLDHashNumber GetHashCode() const { 1.90 + nsAutoCString str; 1.91 + str.Assign(mHost); 1.92 + str.AppendInt(mAppId); 1.93 + str.AppendInt(static_cast<int32_t>(mIsInBrowserElement)); 1.94 + 1.95 + return mozilla::HashString(str); 1.96 + } 1.97 + 1.98 + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PermissionKey) 1.99 + 1.100 + nsCString mHost; 1.101 + uint32_t mAppId; 1.102 + bool mIsInBrowserElement; 1.103 + 1.104 + private: 1.105 + // Default ctor shouldn't be used. 1.106 + PermissionKey() MOZ_DELETE; 1.107 + 1.108 + // Dtor shouldn't be used outside of the class. 1.109 + ~PermissionKey() {}; 1.110 + }; 1.111 + 1.112 + class PermissionHashKey : public nsRefPtrHashKey<PermissionKey> 1.113 + { 1.114 + public: 1.115 + PermissionHashKey(const PermissionKey* aPermissionKey) 1.116 + : nsRefPtrHashKey<PermissionKey>(aPermissionKey) 1.117 + {} 1.118 + 1.119 + PermissionHashKey(const PermissionHashKey& toCopy) 1.120 + : nsRefPtrHashKey<PermissionKey>(toCopy) 1.121 + , mPermissions(toCopy.mPermissions) 1.122 + {} 1.123 + 1.124 + bool KeyEquals(const PermissionKey* aKey) const 1.125 + { 1.126 + return *aKey == *GetKey(); 1.127 + } 1.128 + 1.129 + static PLDHashNumber HashKey(const PermissionKey* aKey) 1.130 + { 1.131 + return aKey->GetHashCode(); 1.132 + } 1.133 + 1.134 + // Force the hashtable to use the copy constructor when shuffling entries 1.135 + // around, otherwise the Auto part of our nsAutoTArray won't be happy! 1.136 + enum { ALLOW_MEMMOVE = false }; 1.137 + 1.138 + inline nsTArray<PermissionEntry> & GetPermissions() 1.139 + { 1.140 + return mPermissions; 1.141 + } 1.142 + 1.143 + inline int32_t GetPermissionIndex(uint32_t aType) const 1.144 + { 1.145 + for (uint32_t i = 0; i < mPermissions.Length(); ++i) 1.146 + if (mPermissions[i].mType == aType) 1.147 + return i; 1.148 + 1.149 + return -1; 1.150 + } 1.151 + 1.152 + inline PermissionEntry GetPermission(uint32_t aType) const 1.153 + { 1.154 + for (uint32_t i = 0; i < mPermissions.Length(); ++i) 1.155 + if (mPermissions[i].mType == aType) 1.156 + return mPermissions[i]; 1.157 + 1.158 + // unknown permission... return relevant data 1.159 + return PermissionEntry(-1, aType, nsIPermissionManager::UNKNOWN_ACTION, 1.160 + nsIPermissionManager::EXPIRE_NEVER, 0); 1.161 + } 1.162 + 1.163 + private: 1.164 + nsAutoTArray<PermissionEntry, 1> mPermissions; 1.165 + }; 1.166 + 1.167 + // nsISupports 1.168 + NS_DECL_ISUPPORTS 1.169 + NS_DECL_NSIPERMISSIONMANAGER 1.170 + NS_DECL_NSIOBSERVER 1.171 + 1.172 + nsPermissionManager(); 1.173 + virtual ~nsPermissionManager(); 1.174 + static nsIPermissionManager* GetXPCOMSingleton(); 1.175 + nsresult Init(); 1.176 + 1.177 + // enums for AddInternal() 1.178 + enum OperationType { 1.179 + eOperationNone, 1.180 + eOperationAdding, 1.181 + eOperationRemoving, 1.182 + eOperationChanging 1.183 + }; 1.184 + 1.185 + enum DBOperationType { 1.186 + eNoDBOperation, 1.187 + eWriteToDB 1.188 + }; 1.189 + 1.190 + enum NotifyOperationType { 1.191 + eDontNotify, 1.192 + eNotify 1.193 + }; 1.194 + 1.195 + nsresult AddInternal(nsIPrincipal* aPrincipal, 1.196 + const nsAFlatCString &aType, 1.197 + uint32_t aPermission, 1.198 + int64_t aID, 1.199 + uint32_t aExpireType, 1.200 + int64_t aExpireTime, 1.201 + NotifyOperationType aNotifyOperation, 1.202 + DBOperationType aDBOperation); 1.203 + 1.204 + /** 1.205 + * Initialize the "webapp-uninstall" observing. 1.206 + * Will create a nsPermissionManager instance if needed. 1.207 + * That way, we can prevent have nsPermissionManager created at startup just 1.208 + * to be able to clear data when an application is uninstalled. 1.209 + */ 1.210 + static void AppClearDataObserverInit(); 1.211 + 1.212 +private: 1.213 + int32_t GetTypeIndex(const char *aTypeString, 1.214 + bool aAdd); 1.215 + 1.216 + PermissionHashKey* GetPermissionHashKey(const nsACString& aHost, 1.217 + uint32_t aAppId, 1.218 + bool aIsInBrowserElement, 1.219 + uint32_t aType, 1.220 + bool aExactHostMatch); 1.221 + 1.222 + nsresult CommonTestPermission(nsIPrincipal* aPrincipal, 1.223 + const char *aType, 1.224 + uint32_t *aPermission, 1.225 + bool aExactHostMatch, 1.226 + bool aIncludingSession); 1.227 + 1.228 + nsresult InitDB(bool aRemoveFile); 1.229 + nsresult CreateTable(); 1.230 + nsresult Import(); 1.231 + nsresult Read(); 1.232 + void NotifyObserversWithPermission(const nsACString &aHost, 1.233 + uint32_t aAppId, 1.234 + bool aIsInBrowserElement, 1.235 + const nsCString &aType, 1.236 + uint32_t aPermission, 1.237 + uint32_t aExpireType, 1.238 + int64_t aExpireTime, 1.239 + const char16_t *aData); 1.240 + void NotifyObservers(nsIPermission *aPermission, const char16_t *aData); 1.241 + 1.242 + // Finalize all statements, close the DB and null it. 1.243 + // if aRebuildOnSuccess, reinitialize database 1.244 + void CloseDB(bool aRebuildOnSuccess = false); 1.245 + 1.246 + nsresult RemoveAllInternal(bool aNotifyObservers); 1.247 + nsresult RemoveAllFromMemory(); 1.248 + nsresult NormalizeToACE(nsCString &aHost); 1.249 + static void UpdateDB(OperationType aOp, 1.250 + mozIStorageAsyncStatement* aStmt, 1.251 + int64_t aID, 1.252 + const nsACString& aHost, 1.253 + const nsACString& aType, 1.254 + uint32_t aPermission, 1.255 + uint32_t aExpireType, 1.256 + int64_t aExpireTime, 1.257 + uint32_t aAppId, 1.258 + bool aIsInBrowserElement); 1.259 + 1.260 + nsresult RemoveExpiredPermissionsForApp(uint32_t aAppId); 1.261 + 1.262 + /** 1.263 + * This struct has to be passed as an argument to GetPermissionsForApp. 1.264 + * |appId| and |browserOnly| have to be defined. 1.265 + * |permissions| will be filed with permissions that are related to the app. 1.266 + * If |browserOnly| is true, only permissions related to a browserElement will 1.267 + * be in |permissions|. 1.268 + */ 1.269 + struct GetPermissionsForAppStruct { 1.270 + uint32_t appId; 1.271 + bool browserOnly; 1.272 + nsCOMArray<nsIPermission> permissions; 1.273 + 1.274 + GetPermissionsForAppStruct() MOZ_DELETE; 1.275 + GetPermissionsForAppStruct(uint32_t aAppId, bool aBrowserOnly) 1.276 + : appId(aAppId) 1.277 + , browserOnly(aBrowserOnly) 1.278 + {} 1.279 + }; 1.280 + 1.281 + /** 1.282 + * This method will return the list of all permissions that are related to a 1.283 + * specific app. 1.284 + * @param arg has to be an instance of GetPermissionsForAppStruct. 1.285 + */ 1.286 + static PLDHashOperator 1.287 + GetPermissionsForApp(PermissionHashKey* entry, void* arg); 1.288 + 1.289 + /** 1.290 + * This method restores an app's permissions when its session ends. 1.291 + */ 1.292 + static PLDHashOperator 1.293 + RemoveExpiredPermissionsForAppEnumerator(PermissionHashKey* entry, 1.294 + void* nonused); 1.295 + 1.296 + nsCOMPtr<nsIObserverService> mObserverService; 1.297 + nsCOMPtr<nsIIDNService> mIDNService; 1.298 + 1.299 + nsCOMPtr<mozIStorageConnection> mDBConn; 1.300 + nsCOMPtr<mozIStorageAsyncStatement> mStmtInsert; 1.301 + nsCOMPtr<mozIStorageAsyncStatement> mStmtDelete; 1.302 + nsCOMPtr<mozIStorageAsyncStatement> mStmtUpdate; 1.303 + 1.304 + nsTHashtable<PermissionHashKey> mPermissionTable; 1.305 + // a unique, monotonically increasing id used to identify each database entry 1.306 + int64_t mLargestID; 1.307 + 1.308 + // An array to store the strings identifying the different types. 1.309 + nsTArray<nsCString> mTypeArray; 1.310 + 1.311 + // A list of struct for counting applications 1.312 + struct ApplicationCounter { 1.313 + uint32_t mAppId; 1.314 + uint32_t mCounter; 1.315 + }; 1.316 + nsTArray<ApplicationCounter> mAppIdRefcounts; 1.317 + 1.318 + // Initially, |false|. Set to |true| once shutdown has started, to avoid 1.319 + // reopening the database. 1.320 + bool mIsShuttingDown; 1.321 + 1.322 + friend class DeleteFromMozHostListener; 1.323 + friend class CloseDatabaseListener; 1.324 +}; 1.325 + 1.326 +// {4F6B5E00-0C36-11d5-A535-0010A401EB10} 1.327 +#define NS_PERMISSIONMANAGER_CID \ 1.328 +{ 0x4f6b5e00, 0xc36, 0x11d5, { 0xa5, 0x35, 0x0, 0x10, 0xa4, 0x1, 0xeb, 0x10 } } 1.329 + 1.330 +#endif /* nsPermissionManager_h__ */