Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsPermissionManager_h__ |
michael@0 | 7 | #define nsPermissionManager_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIPermissionManager.h" |
michael@0 | 10 | #include "nsIObserver.h" |
michael@0 | 11 | #include "nsIObserverService.h" |
michael@0 | 12 | #include "nsWeakReference.h" |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | #include "nsIFile.h" |
michael@0 | 15 | #include "nsTHashtable.h" |
michael@0 | 16 | #include "nsTArray.h" |
michael@0 | 17 | #include "nsString.h" |
michael@0 | 18 | #include "nsPermission.h" |
michael@0 | 19 | #include "nsHashKeys.h" |
michael@0 | 20 | #include "nsAutoPtr.h" |
michael@0 | 21 | #include "nsCOMArray.h" |
michael@0 | 22 | #include "nsDataHashtable.h" |
michael@0 | 23 | |
michael@0 | 24 | class nsIPermission; |
michael@0 | 25 | class nsIIDNService; |
michael@0 | 26 | class mozIStorageConnection; |
michael@0 | 27 | class mozIStorageAsyncStatement; |
michael@0 | 28 | |
michael@0 | 29 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 30 | |
michael@0 | 31 | class nsPermissionManager : public nsIPermissionManager, |
michael@0 | 32 | public nsIObserver, |
michael@0 | 33 | public nsSupportsWeakReference |
michael@0 | 34 | { |
michael@0 | 35 | public: |
michael@0 | 36 | class PermissionEntry |
michael@0 | 37 | { |
michael@0 | 38 | public: |
michael@0 | 39 | PermissionEntry(int64_t aID, uint32_t aType, uint32_t aPermission, |
michael@0 | 40 | uint32_t aExpireType, int64_t aExpireTime) |
michael@0 | 41 | : mID(aID) |
michael@0 | 42 | , mType(aType) |
michael@0 | 43 | , mPermission(aPermission) |
michael@0 | 44 | , mExpireType(aExpireType) |
michael@0 | 45 | , mExpireTime(aExpireTime) |
michael@0 | 46 | , mNonSessionPermission(aPermission) |
michael@0 | 47 | , mNonSessionExpireType(aExpireType) |
michael@0 | 48 | , mNonSessionExpireTime(aExpireTime) |
michael@0 | 49 | {} |
michael@0 | 50 | |
michael@0 | 51 | int64_t mID; |
michael@0 | 52 | uint32_t mType; |
michael@0 | 53 | uint32_t mPermission; |
michael@0 | 54 | uint32_t mExpireType; |
michael@0 | 55 | int64_t mExpireTime; |
michael@0 | 56 | uint32_t mNonSessionPermission; |
michael@0 | 57 | uint32_t mNonSessionExpireType; |
michael@0 | 58 | uint32_t mNonSessionExpireTime; |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * PermissionKey is the key used by PermissionHashKey hash table. |
michael@0 | 63 | * |
michael@0 | 64 | * NOTE: It could be implementing nsIHashable but there is no reason to worry |
michael@0 | 65 | * with XPCOM interfaces while we don't need to. |
michael@0 | 66 | */ |
michael@0 | 67 | class PermissionKey |
michael@0 | 68 | { |
michael@0 | 69 | public: |
michael@0 | 70 | PermissionKey(nsIPrincipal* aPrincipal); |
michael@0 | 71 | PermissionKey(const nsACString& aHost, |
michael@0 | 72 | uint32_t aAppId, |
michael@0 | 73 | bool aIsInBrowserElement) |
michael@0 | 74 | : mHost(aHost) |
michael@0 | 75 | , mAppId(aAppId) |
michael@0 | 76 | , mIsInBrowserElement(aIsInBrowserElement) |
michael@0 | 77 | { |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool operator==(const PermissionKey& aKey) const { |
michael@0 | 81 | return mHost.Equals(aKey.mHost) && |
michael@0 | 82 | mAppId == aKey.mAppId && |
michael@0 | 83 | mIsInBrowserElement == aKey.mIsInBrowserElement; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | PLDHashNumber GetHashCode() const { |
michael@0 | 87 | nsAutoCString str; |
michael@0 | 88 | str.Assign(mHost); |
michael@0 | 89 | str.AppendInt(mAppId); |
michael@0 | 90 | str.AppendInt(static_cast<int32_t>(mIsInBrowserElement)); |
michael@0 | 91 | |
michael@0 | 92 | return mozilla::HashString(str); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PermissionKey) |
michael@0 | 96 | |
michael@0 | 97 | nsCString mHost; |
michael@0 | 98 | uint32_t mAppId; |
michael@0 | 99 | bool mIsInBrowserElement; |
michael@0 | 100 | |
michael@0 | 101 | private: |
michael@0 | 102 | // Default ctor shouldn't be used. |
michael@0 | 103 | PermissionKey() MOZ_DELETE; |
michael@0 | 104 | |
michael@0 | 105 | // Dtor shouldn't be used outside of the class. |
michael@0 | 106 | ~PermissionKey() {}; |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | class PermissionHashKey : public nsRefPtrHashKey<PermissionKey> |
michael@0 | 110 | { |
michael@0 | 111 | public: |
michael@0 | 112 | PermissionHashKey(const PermissionKey* aPermissionKey) |
michael@0 | 113 | : nsRefPtrHashKey<PermissionKey>(aPermissionKey) |
michael@0 | 114 | {} |
michael@0 | 115 | |
michael@0 | 116 | PermissionHashKey(const PermissionHashKey& toCopy) |
michael@0 | 117 | : nsRefPtrHashKey<PermissionKey>(toCopy) |
michael@0 | 118 | , mPermissions(toCopy.mPermissions) |
michael@0 | 119 | {} |
michael@0 | 120 | |
michael@0 | 121 | bool KeyEquals(const PermissionKey* aKey) const |
michael@0 | 122 | { |
michael@0 | 123 | return *aKey == *GetKey(); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | static PLDHashNumber HashKey(const PermissionKey* aKey) |
michael@0 | 127 | { |
michael@0 | 128 | return aKey->GetHashCode(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | // Force the hashtable to use the copy constructor when shuffling entries |
michael@0 | 132 | // around, otherwise the Auto part of our nsAutoTArray won't be happy! |
michael@0 | 133 | enum { ALLOW_MEMMOVE = false }; |
michael@0 | 134 | |
michael@0 | 135 | inline nsTArray<PermissionEntry> & GetPermissions() |
michael@0 | 136 | { |
michael@0 | 137 | return mPermissions; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | inline int32_t GetPermissionIndex(uint32_t aType) const |
michael@0 | 141 | { |
michael@0 | 142 | for (uint32_t i = 0; i < mPermissions.Length(); ++i) |
michael@0 | 143 | if (mPermissions[i].mType == aType) |
michael@0 | 144 | return i; |
michael@0 | 145 | |
michael@0 | 146 | return -1; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | inline PermissionEntry GetPermission(uint32_t aType) const |
michael@0 | 150 | { |
michael@0 | 151 | for (uint32_t i = 0; i < mPermissions.Length(); ++i) |
michael@0 | 152 | if (mPermissions[i].mType == aType) |
michael@0 | 153 | return mPermissions[i]; |
michael@0 | 154 | |
michael@0 | 155 | // unknown permission... return relevant data |
michael@0 | 156 | return PermissionEntry(-1, aType, nsIPermissionManager::UNKNOWN_ACTION, |
michael@0 | 157 | nsIPermissionManager::EXPIRE_NEVER, 0); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | private: |
michael@0 | 161 | nsAutoTArray<PermissionEntry, 1> mPermissions; |
michael@0 | 162 | }; |
michael@0 | 163 | |
michael@0 | 164 | // nsISupports |
michael@0 | 165 | NS_DECL_ISUPPORTS |
michael@0 | 166 | NS_DECL_NSIPERMISSIONMANAGER |
michael@0 | 167 | NS_DECL_NSIOBSERVER |
michael@0 | 168 | |
michael@0 | 169 | nsPermissionManager(); |
michael@0 | 170 | virtual ~nsPermissionManager(); |
michael@0 | 171 | static nsIPermissionManager* GetXPCOMSingleton(); |
michael@0 | 172 | nsresult Init(); |
michael@0 | 173 | |
michael@0 | 174 | // enums for AddInternal() |
michael@0 | 175 | enum OperationType { |
michael@0 | 176 | eOperationNone, |
michael@0 | 177 | eOperationAdding, |
michael@0 | 178 | eOperationRemoving, |
michael@0 | 179 | eOperationChanging |
michael@0 | 180 | }; |
michael@0 | 181 | |
michael@0 | 182 | enum DBOperationType { |
michael@0 | 183 | eNoDBOperation, |
michael@0 | 184 | eWriteToDB |
michael@0 | 185 | }; |
michael@0 | 186 | |
michael@0 | 187 | enum NotifyOperationType { |
michael@0 | 188 | eDontNotify, |
michael@0 | 189 | eNotify |
michael@0 | 190 | }; |
michael@0 | 191 | |
michael@0 | 192 | nsresult AddInternal(nsIPrincipal* aPrincipal, |
michael@0 | 193 | const nsAFlatCString &aType, |
michael@0 | 194 | uint32_t aPermission, |
michael@0 | 195 | int64_t aID, |
michael@0 | 196 | uint32_t aExpireType, |
michael@0 | 197 | int64_t aExpireTime, |
michael@0 | 198 | NotifyOperationType aNotifyOperation, |
michael@0 | 199 | DBOperationType aDBOperation); |
michael@0 | 200 | |
michael@0 | 201 | /** |
michael@0 | 202 | * Initialize the "webapp-uninstall" observing. |
michael@0 | 203 | * Will create a nsPermissionManager instance if needed. |
michael@0 | 204 | * That way, we can prevent have nsPermissionManager created at startup just |
michael@0 | 205 | * to be able to clear data when an application is uninstalled. |
michael@0 | 206 | */ |
michael@0 | 207 | static void AppClearDataObserverInit(); |
michael@0 | 208 | |
michael@0 | 209 | private: |
michael@0 | 210 | int32_t GetTypeIndex(const char *aTypeString, |
michael@0 | 211 | bool aAdd); |
michael@0 | 212 | |
michael@0 | 213 | PermissionHashKey* GetPermissionHashKey(const nsACString& aHost, |
michael@0 | 214 | uint32_t aAppId, |
michael@0 | 215 | bool aIsInBrowserElement, |
michael@0 | 216 | uint32_t aType, |
michael@0 | 217 | bool aExactHostMatch); |
michael@0 | 218 | |
michael@0 | 219 | nsresult CommonTestPermission(nsIPrincipal* aPrincipal, |
michael@0 | 220 | const char *aType, |
michael@0 | 221 | uint32_t *aPermission, |
michael@0 | 222 | bool aExactHostMatch, |
michael@0 | 223 | bool aIncludingSession); |
michael@0 | 224 | |
michael@0 | 225 | nsresult InitDB(bool aRemoveFile); |
michael@0 | 226 | nsresult CreateTable(); |
michael@0 | 227 | nsresult Import(); |
michael@0 | 228 | nsresult Read(); |
michael@0 | 229 | void NotifyObserversWithPermission(const nsACString &aHost, |
michael@0 | 230 | uint32_t aAppId, |
michael@0 | 231 | bool aIsInBrowserElement, |
michael@0 | 232 | const nsCString &aType, |
michael@0 | 233 | uint32_t aPermission, |
michael@0 | 234 | uint32_t aExpireType, |
michael@0 | 235 | int64_t aExpireTime, |
michael@0 | 236 | const char16_t *aData); |
michael@0 | 237 | void NotifyObservers(nsIPermission *aPermission, const char16_t *aData); |
michael@0 | 238 | |
michael@0 | 239 | // Finalize all statements, close the DB and null it. |
michael@0 | 240 | // if aRebuildOnSuccess, reinitialize database |
michael@0 | 241 | void CloseDB(bool aRebuildOnSuccess = false); |
michael@0 | 242 | |
michael@0 | 243 | nsresult RemoveAllInternal(bool aNotifyObservers); |
michael@0 | 244 | nsresult RemoveAllFromMemory(); |
michael@0 | 245 | nsresult NormalizeToACE(nsCString &aHost); |
michael@0 | 246 | static void UpdateDB(OperationType aOp, |
michael@0 | 247 | mozIStorageAsyncStatement* aStmt, |
michael@0 | 248 | int64_t aID, |
michael@0 | 249 | const nsACString& aHost, |
michael@0 | 250 | const nsACString& aType, |
michael@0 | 251 | uint32_t aPermission, |
michael@0 | 252 | uint32_t aExpireType, |
michael@0 | 253 | int64_t aExpireTime, |
michael@0 | 254 | uint32_t aAppId, |
michael@0 | 255 | bool aIsInBrowserElement); |
michael@0 | 256 | |
michael@0 | 257 | nsresult RemoveExpiredPermissionsForApp(uint32_t aAppId); |
michael@0 | 258 | |
michael@0 | 259 | /** |
michael@0 | 260 | * This struct has to be passed as an argument to GetPermissionsForApp. |
michael@0 | 261 | * |appId| and |browserOnly| have to be defined. |
michael@0 | 262 | * |permissions| will be filed with permissions that are related to the app. |
michael@0 | 263 | * If |browserOnly| is true, only permissions related to a browserElement will |
michael@0 | 264 | * be in |permissions|. |
michael@0 | 265 | */ |
michael@0 | 266 | struct GetPermissionsForAppStruct { |
michael@0 | 267 | uint32_t appId; |
michael@0 | 268 | bool browserOnly; |
michael@0 | 269 | nsCOMArray<nsIPermission> permissions; |
michael@0 | 270 | |
michael@0 | 271 | GetPermissionsForAppStruct() MOZ_DELETE; |
michael@0 | 272 | GetPermissionsForAppStruct(uint32_t aAppId, bool aBrowserOnly) |
michael@0 | 273 | : appId(aAppId) |
michael@0 | 274 | , browserOnly(aBrowserOnly) |
michael@0 | 275 | {} |
michael@0 | 276 | }; |
michael@0 | 277 | |
michael@0 | 278 | /** |
michael@0 | 279 | * This method will return the list of all permissions that are related to a |
michael@0 | 280 | * specific app. |
michael@0 | 281 | * @param arg has to be an instance of GetPermissionsForAppStruct. |
michael@0 | 282 | */ |
michael@0 | 283 | static PLDHashOperator |
michael@0 | 284 | GetPermissionsForApp(PermissionHashKey* entry, void* arg); |
michael@0 | 285 | |
michael@0 | 286 | /** |
michael@0 | 287 | * This method restores an app's permissions when its session ends. |
michael@0 | 288 | */ |
michael@0 | 289 | static PLDHashOperator |
michael@0 | 290 | RemoveExpiredPermissionsForAppEnumerator(PermissionHashKey* entry, |
michael@0 | 291 | void* nonused); |
michael@0 | 292 | |
michael@0 | 293 | nsCOMPtr<nsIObserverService> mObserverService; |
michael@0 | 294 | nsCOMPtr<nsIIDNService> mIDNService; |
michael@0 | 295 | |
michael@0 | 296 | nsCOMPtr<mozIStorageConnection> mDBConn; |
michael@0 | 297 | nsCOMPtr<mozIStorageAsyncStatement> mStmtInsert; |
michael@0 | 298 | nsCOMPtr<mozIStorageAsyncStatement> mStmtDelete; |
michael@0 | 299 | nsCOMPtr<mozIStorageAsyncStatement> mStmtUpdate; |
michael@0 | 300 | |
michael@0 | 301 | nsTHashtable<PermissionHashKey> mPermissionTable; |
michael@0 | 302 | // a unique, monotonically increasing id used to identify each database entry |
michael@0 | 303 | int64_t mLargestID; |
michael@0 | 304 | |
michael@0 | 305 | // An array to store the strings identifying the different types. |
michael@0 | 306 | nsTArray<nsCString> mTypeArray; |
michael@0 | 307 | |
michael@0 | 308 | // A list of struct for counting applications |
michael@0 | 309 | struct ApplicationCounter { |
michael@0 | 310 | uint32_t mAppId; |
michael@0 | 311 | uint32_t mCounter; |
michael@0 | 312 | }; |
michael@0 | 313 | nsTArray<ApplicationCounter> mAppIdRefcounts; |
michael@0 | 314 | |
michael@0 | 315 | // Initially, |false|. Set to |true| once shutdown has started, to avoid |
michael@0 | 316 | // reopening the database. |
michael@0 | 317 | bool mIsShuttingDown; |
michael@0 | 318 | |
michael@0 | 319 | friend class DeleteFromMozHostListener; |
michael@0 | 320 | friend class CloseDatabaseListener; |
michael@0 | 321 | }; |
michael@0 | 322 | |
michael@0 | 323 | // {4F6B5E00-0C36-11d5-A535-0010A401EB10} |
michael@0 | 324 | #define NS_PERMISSIONMANAGER_CID \ |
michael@0 | 325 | { 0x4f6b5e00, 0xc36, 0x11d5, { 0xa5, 0x35, 0x0, 0x10, 0xa4, 0x1, 0xeb, 0x10 } } |
michael@0 | 326 | |
michael@0 | 327 | #endif /* nsPermissionManager_h__ */ |