2013-06-06 12:59:32 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsPermissionManager_h__
|
|
|
|
#define nsPermissionManager_h__
|
|
|
|
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2014-09-11 20:24:10 -07:00
|
|
|
#include "nsIInputStream.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsTHashtable.h"
|
2007-12-04 22:37:36 -08:00
|
|
|
#include "nsTArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
2010-10-09 11:07:38 -07:00
|
|
|
#include "nsPermission.h"
|
2012-08-23 11:47:55 -07:00
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2012-08-31 07:34:28 -07:00
|
|
|
#include "nsCOMArray.h"
|
2013-01-05 14:02:29 -08:00
|
|
|
#include "nsDataHashtable.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsIPermission;
|
2007-12-04 22:37:36 -08:00
|
|
|
class nsIIDNService;
|
|
|
|
class mozIStorageConnection;
|
2012-09-08 06:48:20 -07:00
|
|
|
class mozIStorageAsyncStatement;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-08-27 11:26:48 -07:00
|
|
|
class nsPermissionManager MOZ_FINAL : public nsIPermissionManager,
|
|
|
|
public nsIObserver,
|
|
|
|
public nsSupportsWeakReference
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-08-23 11:47:55 -07:00
|
|
|
class PermissionEntry
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-23 11:47:55 -07:00
|
|
|
public:
|
|
|
|
PermissionEntry(int64_t aID, uint32_t aType, uint32_t aPermission,
|
2014-09-14 18:33:12 -07:00
|
|
|
uint32_t aExpireType, int64_t aExpireTime,
|
|
|
|
int64_t aModificationTime)
|
2012-08-23 11:47:55 -07:00
|
|
|
: mID(aID)
|
|
|
|
, mType(aType)
|
|
|
|
, mPermission(aPermission)
|
|
|
|
, mExpireType(aExpireType)
|
|
|
|
, mExpireTime(aExpireTime)
|
2014-09-14 18:33:12 -07:00
|
|
|
, mModificationTime(aModificationTime)
|
2013-01-05 14:02:29 -08:00
|
|
|
, mNonSessionPermission(aPermission)
|
2013-01-07 18:29:22 -08:00
|
|
|
, mNonSessionExpireType(aExpireType)
|
2013-06-06 12:59:32 -07:00
|
|
|
, mNonSessionExpireTime(aExpireTime)
|
2012-08-23 11:47:55 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
int64_t mID;
|
|
|
|
uint32_t mType;
|
|
|
|
uint32_t mPermission;
|
|
|
|
uint32_t mExpireType;
|
|
|
|
int64_t mExpireTime;
|
2014-09-14 18:33:12 -07:00
|
|
|
int64_t mModificationTime;
|
2013-01-05 14:02:29 -08:00
|
|
|
uint32_t mNonSessionPermission;
|
2013-01-07 18:29:22 -08:00
|
|
|
uint32_t mNonSessionExpireType;
|
2013-06-06 12:59:32 -07:00
|
|
|
uint32_t mNonSessionExpireTime;
|
2012-08-23 11:47:55 -07:00
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-23 11:47:55 -07:00
|
|
|
/**
|
|
|
|
* PermissionKey is the key used by PermissionHashKey hash table.
|
|
|
|
*
|
|
|
|
* NOTE: It could be implementing nsIHashable but there is no reason to worry
|
|
|
|
* with XPCOM interfaces while we don't need to.
|
|
|
|
*/
|
|
|
|
class PermissionKey
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-23 11:47:55 -07:00
|
|
|
public:
|
2014-09-02 15:20:38 -07:00
|
|
|
explicit PermissionKey(nsIPrincipal* aPrincipal);
|
2012-08-23 11:47:55 -07:00
|
|
|
PermissionKey(const nsACString& aHost,
|
|
|
|
uint32_t aAppId,
|
|
|
|
bool aIsInBrowserElement)
|
|
|
|
: mHost(aHost)
|
|
|
|
, mAppId(aAppId)
|
|
|
|
, mIsInBrowserElement(aIsInBrowserElement)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const PermissionKey& aKey) const {
|
|
|
|
return mHost.Equals(aKey.mHost) &&
|
|
|
|
mAppId == aKey.mAppId &&
|
|
|
|
mIsInBrowserElement == aKey.mIsInBrowserElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
PLDHashNumber GetHashCode() const {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString str;
|
2012-08-23 11:47:55 -07:00
|
|
|
str.Assign(mHost);
|
|
|
|
str.AppendInt(mAppId);
|
|
|
|
str.AppendInt(static_cast<int32_t>(mIsInBrowserElement));
|
|
|
|
|
|
|
|
return mozilla::HashString(str);
|
|
|
|
}
|
|
|
|
|
2013-01-11 02:14:04 -08:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PermissionKey)
|
2012-08-23 11:47:55 -07:00
|
|
|
|
|
|
|
nsCString mHost;
|
|
|
|
uint32_t mAppId;
|
|
|
|
bool mIsInBrowserElement;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Default ctor shouldn't be used.
|
|
|
|
PermissionKey() MOZ_DELETE;
|
|
|
|
|
|
|
|
// Dtor shouldn't be used outside of the class.
|
|
|
|
~PermissionKey() {};
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-23 11:47:55 -07:00
|
|
|
class PermissionHashKey : public nsRefPtrHashKey<PermissionKey>
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-23 11:47:55 -07:00
|
|
|
public:
|
2014-09-02 15:20:38 -07:00
|
|
|
explicit PermissionHashKey(const PermissionKey* aPermissionKey)
|
2012-08-23 11:47:55 -07:00
|
|
|
: nsRefPtrHashKey<PermissionKey>(aPermissionKey)
|
|
|
|
{}
|
|
|
|
|
|
|
|
PermissionHashKey(const PermissionHashKey& toCopy)
|
|
|
|
: nsRefPtrHashKey<PermissionKey>(toCopy)
|
|
|
|
, mPermissions(toCopy.mPermissions)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool KeyEquals(const PermissionKey* aKey) const
|
|
|
|
{
|
|
|
|
return *aKey == *GetKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(const PermissionKey* aKey)
|
|
|
|
{
|
|
|
|
return aKey->GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the hashtable to use the copy constructor when shuffling entries
|
|
|
|
// around, otherwise the Auto part of our nsAutoTArray won't be happy!
|
|
|
|
enum { ALLOW_MEMMOVE = false };
|
|
|
|
|
|
|
|
inline nsTArray<PermissionEntry> & GetPermissions()
|
|
|
|
{
|
|
|
|
return mPermissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int32_t GetPermissionIndex(uint32_t aType) const
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mPermissions.Length(); ++i)
|
|
|
|
if (mPermissions[i].mType == aType)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline PermissionEntry GetPermission(uint32_t aType) const
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mPermissions.Length(); ++i)
|
|
|
|
if (mPermissions[i].mType == aType)
|
|
|
|
return mPermissions[i];
|
|
|
|
|
|
|
|
// unknown permission... return relevant data
|
|
|
|
return PermissionEntry(-1, aType, nsIPermissionManager::UNKNOWN_ACTION,
|
2014-09-14 18:33:12 -07:00
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0, 0);
|
2012-08-23 11:47:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsAutoTArray<PermissionEntry, 1> mPermissions;
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIPERMISSIONMANAGER
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
nsPermissionManager();
|
2010-10-09 11:07:38 -07:00
|
|
|
static nsIPermissionManager* GetXPCOMSingleton();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult Init();
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// enums for AddInternal()
|
|
|
|
enum OperationType {
|
|
|
|
eOperationNone,
|
|
|
|
eOperationAdding,
|
|
|
|
eOperationRemoving,
|
2014-09-11 20:24:10 -07:00
|
|
|
eOperationChanging,
|
|
|
|
eOperationReplacingDefault
|
2007-12-04 22:37:36 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum DBOperationType {
|
|
|
|
eNoDBOperation,
|
|
|
|
eWriteToDB
|
|
|
|
};
|
|
|
|
|
|
|
|
enum NotifyOperationType {
|
|
|
|
eDontNotify,
|
|
|
|
eNotify
|
|
|
|
};
|
|
|
|
|
2014-09-11 20:24:10 -07:00
|
|
|
// A special value for a permission ID that indicates the ID was loaded as
|
|
|
|
// a default value. These will never be written to the database, but may
|
|
|
|
// be overridden with an explicit permission (including UNKNOWN_ACTION)
|
|
|
|
static const int64_t cIDPermissionIsDefault = -1;
|
|
|
|
|
2012-08-25 09:53:48 -07:00
|
|
|
nsresult AddInternal(nsIPrincipal* aPrincipal,
|
2007-12-04 22:37:36 -08:00
|
|
|
const nsAFlatCString &aType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aPermission,
|
|
|
|
int64_t aID,
|
|
|
|
uint32_t aExpireType,
|
|
|
|
int64_t aExpireTime,
|
2014-09-14 18:33:12 -07:00
|
|
|
int64_t aModificationTime,
|
2007-12-04 22:37:36 -08:00
|
|
|
NotifyOperationType aNotifyOperation,
|
|
|
|
DBOperationType aDBOperation);
|
|
|
|
|
2012-08-31 07:34:28 -07:00
|
|
|
/**
|
|
|
|
* Initialize the "webapp-uninstall" observing.
|
|
|
|
* Will create a nsPermissionManager instance if needed.
|
|
|
|
* That way, we can prevent have nsPermissionManager created at startup just
|
|
|
|
* to be able to clear data when an application is uninstalled.
|
|
|
|
*/
|
2013-01-07 09:37:01 -08:00
|
|
|
static void AppClearDataObserverInit();
|
2012-08-31 07:34:28 -07:00
|
|
|
|
2010-10-09 11:07:38 -07:00
|
|
|
private:
|
2014-06-23 15:40:02 -07:00
|
|
|
virtual ~nsPermissionManager();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t GetTypeIndex(const char *aTypeString,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aAdd);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-23 11:47:55 -07:00
|
|
|
PermissionHashKey* GetPermissionHashKey(const nsACString& aHost,
|
|
|
|
uint32_t aAppId,
|
|
|
|
bool aIsInBrowserElement,
|
|
|
|
uint32_t aType,
|
|
|
|
bool aExactHostMatch);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-25 09:53:48 -07:00
|
|
|
nsresult CommonTestPermission(nsIPrincipal* aPrincipal,
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *aType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *aPermission,
|
2013-01-10 12:23:39 -08:00
|
|
|
bool aExactHostMatch,
|
|
|
|
bool aIncludingSession);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult InitDB(bool aRemoveFile);
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult CreateTable();
|
|
|
|
nsresult Import();
|
2014-09-11 20:24:10 -07:00
|
|
|
nsresult ImportDefaults();
|
|
|
|
nsresult _DoImport(nsIInputStream *inputStream, mozIStorageConnection *aConn);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult Read();
|
|
|
|
void NotifyObserversWithPermission(const nsACString &aHost,
|
2012-08-23 11:37:31 -07:00
|
|
|
uint32_t aAppId,
|
|
|
|
bool aIsInBrowserElement,
|
2007-12-04 22:37:36 -08:00
|
|
|
const nsCString &aType,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aPermission,
|
|
|
|
uint32_t aExpireType,
|
|
|
|
int64_t aExpireTime,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *aData);
|
|
|
|
void NotifyObservers(nsIPermission *aPermission, const char16_t *aData);
|
2011-12-21 14:04:49 -08:00
|
|
|
|
|
|
|
// Finalize all statements, close the DB and null it.
|
2012-04-30 09:21:50 -07:00
|
|
|
// if aRebuildOnSuccess, reinitialize database
|
|
|
|
void CloseDB(bool aRebuildOnSuccess = false);
|
2011-12-21 14:04:49 -08:00
|
|
|
|
2012-04-30 09:21:50 -07:00
|
|
|
nsresult RemoveAllInternal(bool aNotifyObservers);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult RemoveAllFromMemory();
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult NormalizeToACE(nsCString &aHost);
|
2012-09-08 06:48:20 -07:00
|
|
|
static void UpdateDB(OperationType aOp,
|
|
|
|
mozIStorageAsyncStatement* aStmt,
|
|
|
|
int64_t aID,
|
|
|
|
const nsACString& aHost,
|
|
|
|
const nsACString& aType,
|
|
|
|
uint32_t aPermission,
|
|
|
|
uint32_t aExpireType,
|
|
|
|
int64_t aExpireTime,
|
2014-09-14 18:33:12 -07:00
|
|
|
int64_t aModificationTime,
|
2012-09-08 06:48:20 -07:00
|
|
|
uint32_t aAppId,
|
|
|
|
bool aIsInBrowserElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-01-05 14:02:29 -08:00
|
|
|
nsresult RemoveExpiredPermissionsForApp(uint32_t aAppId);
|
|
|
|
|
2012-08-31 07:34:28 -07:00
|
|
|
/**
|
|
|
|
* This struct has to be passed as an argument to GetPermissionsForApp.
|
2013-01-18 16:15:55 -08:00
|
|
|
* |appId| and |browserOnly| have to be defined.
|
2012-08-31 07:34:28 -07:00
|
|
|
* |permissions| will be filed with permissions that are related to the app.
|
2013-01-18 16:15:55 -08:00
|
|
|
* If |browserOnly| is true, only permissions related to a browserElement will
|
|
|
|
* be in |permissions|.
|
2012-08-31 07:34:28 -07:00
|
|
|
*/
|
|
|
|
struct GetPermissionsForAppStruct {
|
|
|
|
uint32_t appId;
|
2013-01-18 16:15:55 -08:00
|
|
|
bool browserOnly;
|
2012-08-31 07:34:28 -07:00
|
|
|
nsCOMArray<nsIPermission> permissions;
|
|
|
|
|
|
|
|
GetPermissionsForAppStruct() MOZ_DELETE;
|
2013-01-18 16:15:55 -08:00
|
|
|
GetPermissionsForAppStruct(uint32_t aAppId, bool aBrowserOnly)
|
2012-08-31 07:34:28 -07:00
|
|
|
: appId(aAppId)
|
2013-01-18 16:15:55 -08:00
|
|
|
, browserOnly(aBrowserOnly)
|
2012-08-31 07:34:28 -07:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method will return the list of all permissions that are related to a
|
|
|
|
* specific app.
|
|
|
|
* @param arg has to be an instance of GetPermissionsForAppStruct.
|
|
|
|
*/
|
2013-01-05 14:02:29 -08:00
|
|
|
static PLDHashOperator
|
|
|
|
GetPermissionsForApp(PermissionHashKey* entry, void* arg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method restores an app's permissions when its session ends.
|
|
|
|
*/
|
|
|
|
static PLDHashOperator
|
|
|
|
RemoveExpiredPermissionsForAppEnumerator(PermissionHashKey* entry,
|
|
|
|
void* nonused);
|
2012-08-31 07:34:28 -07:00
|
|
|
|
2014-09-14 18:33:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method removes all permissions modified after the specified time.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
RemoveAllModifiedSince(int64_t aModificationTime);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
2007-12-04 22:37:36 -08:00
|
|
|
nsCOMPtr<nsIIDNService> mIDNService;
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageConnection> mDBConn;
|
2012-09-08 06:48:20 -07:00
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> mStmtInsert;
|
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> mStmtDelete;
|
|
|
|
nsCOMPtr<mozIStorageAsyncStatement> mStmtUpdate;
|
2007-12-04 22:37:36 -08:00
|
|
|
|
2012-08-23 11:47:55 -07:00
|
|
|
nsTHashtable<PermissionHashKey> mPermissionTable;
|
2007-12-04 22:37:36 -08:00
|
|
|
// a unique, monotonically increasing id used to identify each database entry
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mLargestID;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// An array to store the strings identifying the different types.
|
2007-12-04 22:37:36 -08:00
|
|
|
nsTArray<nsCString> mTypeArray;
|
2012-04-30 09:21:50 -07:00
|
|
|
|
2013-01-05 14:02:29 -08:00
|
|
|
// A list of struct for counting applications
|
|
|
|
struct ApplicationCounter {
|
|
|
|
uint32_t mAppId;
|
|
|
|
uint32_t mCounter;
|
|
|
|
};
|
|
|
|
nsTArray<ApplicationCounter> mAppIdRefcounts;
|
|
|
|
|
2012-04-30 09:21:50 -07:00
|
|
|
// Initially, |false|. Set to |true| once shutdown has started, to avoid
|
|
|
|
// reopening the database.
|
|
|
|
bool mIsShuttingDown;
|
|
|
|
|
|
|
|
friend class DeleteFromMozHostListener;
|
|
|
|
friend class CloseDatabaseListener;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// {4F6B5E00-0C36-11d5-A535-0010A401EB10}
|
|
|
|
#define NS_PERMISSIONMANAGER_CID \
|
|
|
|
{ 0x4f6b5e00, 0xc36, 0x11d5, { 0xa5, 0x35, 0x0, 0x10, 0xa4, 0x1, 0xeb, 0x10 } }
|
|
|
|
|
|
|
|
#endif /* nsPermissionManager_h__ */
|