2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Michiel van Leeuwen (mvl@exedo.nl)
|
2007-12-04 22:37:36 -08:00
|
|
|
* Daniel Witte (dwitte@stanford.edu)
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
2010-07-19 11:33:33 -07:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2010-07-15 07:04:25 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPermissionManager.h"
|
|
|
|
#include "nsPermission.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsNetUtil.h"
|
2007-12-04 22:37:36 -08:00
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsArrayEnumerator.h"
|
2009-01-21 20:15:34 -08:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsILineInputStream.h"
|
2007-12-04 22:37:36 -08:00
|
|
|
#include "nsIIDNService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "prprf.h"
|
2007-12-04 22:37:36 -08:00
|
|
|
#include "mozIStorageService.h"
|
|
|
|
#include "mozIStorageStatement.h"
|
|
|
|
#include "mozIStorageConnection.h"
|
|
|
|
#include "mozStorageHelper.h"
|
|
|
|
#include "mozStorageCID.h"
|
2010-07-15 07:04:25 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#ifdef MOZ_IPC
|
2010-07-19 11:33:33 -07:00
|
|
|
using mozilla::dom::ContentChild;
|
|
|
|
|
2010-07-15 07:04:25 -07:00
|
|
|
static PRBool
|
|
|
|
IsChildProcess()
|
|
|
|
{
|
|
|
|
return XRE_GetProcessType() == GeckoProcessType_Content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns The child process object, or if we are not in the child
|
|
|
|
* process, nsnull.
|
|
|
|
*/
|
2010-07-19 11:33:33 -07:00
|
|
|
static ContentChild*
|
2010-07-15 07:04:25 -07:00
|
|
|
ChildProcess()
|
|
|
|
{
|
|
|
|
if (IsChildProcess()) {
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild* cpc = ContentChild::GetSingleton();
|
2010-07-15 07:04:25 -07:00
|
|
|
if (!cpc)
|
|
|
|
NS_RUNTIMEABORT("Content Process is NULL!");
|
|
|
|
return cpc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ENSURE_NOT_CHILD_PROCESS \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (IsChildProcess()) { \
|
|
|
|
NS_ERROR("cannot set permission from content process"); \
|
|
|
|
return NS_ERROR_NOT_AVAILABLE; \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 3
|
|
|
|
#include "plarena.h"
|
|
|
|
|
|
|
|
static PLArenaPool *gHostArena = nsnull;
|
|
|
|
|
|
|
|
// making sHostArena 512b for nice allocation
|
|
|
|
// growing is quite cheap
|
|
|
|
#define HOST_ARENA_SIZE 512
|
|
|
|
|
|
|
|
// equivalent to strdup() - does no error checking,
|
|
|
|
// we're assuming we're only called with a valid pointer
|
|
|
|
static char *
|
|
|
|
ArenaStrDup(const char* str, PLArenaPool* aArena)
|
|
|
|
{
|
|
|
|
void* mem;
|
|
|
|
const PRUint32 size = strlen(str) + 1;
|
|
|
|
PL_ARENA_ALLOCATE(mem, aArena, size);
|
|
|
|
if (mem)
|
|
|
|
memcpy(mem, str, size);
|
2007-07-08 00:08:04 -07:00
|
|
|
return static_cast<char*>(mem);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHostEntry::nsHostEntry(const char* aHost)
|
|
|
|
{
|
|
|
|
mHost = ArenaStrDup(aHost, gHostArena);
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// XXX this can fail on OOM
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHostEntry::nsHostEntry(const nsHostEntry& toCopy)
|
2007-12-04 22:37:36 -08:00
|
|
|
: mHost(toCopy.mHost)
|
|
|
|
, mPermissions(toCopy.mPermissions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2007-12-04 22:37:36 -08:00
|
|
|
// nsPermissionManager Implementation
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
static const char kPermissionsFileName[] = "permissions.sqlite";
|
2009-10-16 14:01:04 -07:00
|
|
|
#define HOSTS_SCHEMA_VERSION 2
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
static const char kHostpermFileName[] = "hostperm.1";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static const char kPermissionChangeNotification[] = PERM_CHANGE_NOTIFICATION;
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS3(nsPermissionManager, nsIPermissionManager, nsIObserver, nsISupportsWeakReference)
|
|
|
|
|
|
|
|
nsPermissionManager::nsPermissionManager()
|
2007-12-04 22:37:36 -08:00
|
|
|
: mLargestID(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPermissionManager::~nsPermissionManager()
|
|
|
|
{
|
|
|
|
RemoveAllFromMemory();
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult
|
|
|
|
nsPermissionManager::Init()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mHostTable.Init()) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
// Child will route messages to parent, so no need for further initialization
|
|
|
|
if (IsChildProcess())
|
|
|
|
return NS_OK;
|
|
|
|
#endif
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// ignore failure here, since it's non-fatal (we can run fine without
|
|
|
|
// persistent storage - e.g. if there's no profile).
|
|
|
|
// XXX should we tell the user about this?
|
2009-04-17 23:42:09 -07:00
|
|
|
InitDB(PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mObserverService = do_GetService("@mozilla.org/observer-service;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mObserverService->AddObserver(this, "profile-before-change", PR_TRUE);
|
|
|
|
mObserverService->AddObserver(this, "profile-do-change", PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult
|
2009-04-17 23:42:09 -07:00
|
|
|
nsPermissionManager::InitDB(PRBool aRemoveFile)
|
2007-12-04 22:37:36 -08:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> permissionsFile;
|
|
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(permissionsFile));
|
|
|
|
if (!permissionsFile)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsresult rv = permissionsFile->AppendNative(NS_LITERAL_CSTRING(kPermissionsFileName));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-04-17 23:42:09 -07:00
|
|
|
if (aRemoveFile) {
|
|
|
|
PRBool exists = PR_FALSE;
|
|
|
|
rv = permissionsFile->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (exists) {
|
|
|
|
rv = permissionsFile->Remove(PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsCOMPtr<mozIStorageService> storage = do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID);
|
|
|
|
if (!storage)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// cache a connection to the hosts database
|
|
|
|
rv = storage->OpenDatabase(permissionsFile, getter_AddRefs(mDBConn));
|
2008-02-28 00:23:12 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool ready;
|
|
|
|
mDBConn->GetConnectionReady(&ready);
|
|
|
|
if (!ready) {
|
2007-12-04 22:37:36 -08:00
|
|
|
// delete and try again
|
|
|
|
rv = permissionsFile->Remove(PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = storage->OpenDatabase(permissionsFile, getter_AddRefs(mDBConn));
|
2008-02-28 00:23:12 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mDBConn->GetConnectionReady(&ready);
|
|
|
|
if (!ready)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-12-04 22:37:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool tableExists = PR_FALSE;
|
|
|
|
mDBConn->TableExists(NS_LITERAL_CSTRING("moz_hosts"), &tableExists);
|
|
|
|
if (!tableExists) {
|
|
|
|
rv = CreateTable();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// table already exists; check the schema version before reading
|
|
|
|
PRInt32 dbSchemaVersion;
|
|
|
|
rv = mDBConn->GetSchemaVersion(&dbSchemaVersion);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
switch (dbSchemaVersion) {
|
|
|
|
// upgrading.
|
|
|
|
// every time you increment the database schema, you need to implement
|
|
|
|
// the upgrading code from the previous version to the new one.
|
|
|
|
// fall through to current version
|
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// previous non-expiry version of database. Upgrade it by adding the
|
|
|
|
// expiration columns
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_hosts ADD expireType INTEGER"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"ALTER TABLE moz_hosts ADD expireTime INTEGER"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->SetSchemaVersion(HOSTS_SCHEMA_VERSION);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fall through to the next upgrade
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// current version.
|
|
|
|
case HOSTS_SCHEMA_VERSION:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
NS_WARNING("couldn't get schema version!");
|
|
|
|
|
|
|
|
// the table may be usable; someone might've just clobbered the schema
|
|
|
|
// version. we can treat this case like a downgrade using the codepath
|
|
|
|
// below, by verifying the columns we care about are all there. for now,
|
|
|
|
// re-set the schema version in the db, in case the checks succeed (if
|
|
|
|
// they don't, we're dropping the table anyway).
|
|
|
|
rv = mDBConn->SetSchemaVersion(HOSTS_SCHEMA_VERSION);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// fall through to downgrade check
|
|
|
|
|
|
|
|
// downgrading.
|
|
|
|
// if columns have been added to the table, we can still use the ones we
|
|
|
|
// understand safely. if columns have been deleted or altered, just
|
|
|
|
// blow away the table and start from scratch! if you change the way
|
|
|
|
// a column is interpreted, make sure you also change its name so this
|
|
|
|
// check will catch it.
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// check if all the expected columns exist
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2009-10-16 14:01:04 -07:00
|
|
|
"SELECT host, type, permission, expireType, expireTime FROM moz_hosts"),
|
|
|
|
getter_AddRefs(stmt));
|
2007-12-04 22:37:36 -08:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// our columns aren't there - drop the table!
|
|
|
|
rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DROP TABLE moz_hosts"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = CreateTable();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make operations on the table asynchronous, for performance
|
|
|
|
mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("PRAGMA synchronous = OFF"));
|
|
|
|
|
|
|
|
// cache frequently used statements (for insertion, deletion, and updating)
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"INSERT INTO moz_hosts "
|
2009-10-16 14:01:04 -07:00
|
|
|
"(id, host, type, permission, expireType, expireTime) "
|
|
|
|
"VALUES (?1, ?2, ?3, ?4, ?5, ?6)"), getter_AddRefs(mStmtInsert));
|
2007-12-04 22:37:36 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"DELETE FROM moz_hosts "
|
|
|
|
"WHERE id = ?1"), getter_AddRefs(mStmtDelete));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
|
|
|
"UPDATE moz_hosts "
|
2009-10-16 14:01:04 -07:00
|
|
|
"SET permission = ?2, expireType= ?3, expireTime = ?4 WHERE id = ?1"),
|
|
|
|
getter_AddRefs(mStmtUpdate));
|
2007-12-04 22:37:36 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// check whether to import or just read in the db
|
|
|
|
if (tableExists)
|
|
|
|
return Read();
|
|
|
|
|
|
|
|
return Import();
|
|
|
|
}
|
|
|
|
|
|
|
|
// sets the schema version and creates the moz_hosts table.
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::CreateTable()
|
|
|
|
{
|
|
|
|
// set the schema version, before creating the table
|
|
|
|
nsresult rv = mDBConn->SetSchemaVersion(HOSTS_SCHEMA_VERSION);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// create the table
|
2010-08-21 09:48:01 -07:00
|
|
|
// SQL also lives in automation.py.in. If you change this SQL change that
|
|
|
|
// one too.
|
2007-12-04 22:37:36 -08:00
|
|
|
return mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"CREATE TABLE moz_hosts ("
|
|
|
|
" id INTEGER PRIMARY KEY"
|
|
|
|
",host TEXT"
|
|
|
|
",type TEXT"
|
|
|
|
",permission INTEGER"
|
2009-10-16 14:01:04 -07:00
|
|
|
",expireType INTEGER"
|
|
|
|
",expireTime INTEGER"
|
2007-12-04 22:37:36 -08:00
|
|
|
")"));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::Add(nsIURI *aURI,
|
|
|
|
const char *aType,
|
2009-10-16 14:01:04 -07:00
|
|
|
PRUint32 aPermission,
|
|
|
|
PRUint32 aExpireType,
|
|
|
|
PRInt64 aExpireTime)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
ENSURE_NOT_CHILD_PROCESS;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
2009-10-16 14:01:04 -07:00
|
|
|
NS_ENSURE_TRUE(aExpireType == nsIPermissionManager::EXPIRE_NEVER ||
|
|
|
|
aExpireType == nsIPermissionManager::EXPIRE_TIME ||
|
|
|
|
aExpireType == nsIPermissionManager::EXPIRE_SESSION,
|
|
|
|
NS_ERROR_INVALID_ARG);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
// Skip addition if the permission is already expired.
|
|
|
|
if (aExpireType == nsIPermissionManager::EXPIRE_TIME &&
|
2009-10-16 16:16:31 -07:00
|
|
|
aExpireTime <= PR_Now() / 1000)
|
2009-10-16 14:01:04 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCAutoString host;
|
|
|
|
rv = GetHost(aURI, host);
|
2007-12-04 22:37:36 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
return AddInternal(host, nsDependentCString(aType), aPermission, 0,
|
|
|
|
aExpireType, aExpireTime, eNotify, eWriteToDB);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::AddInternal(const nsAFlatCString &aHost,
|
2007-12-04 22:37:36 -08:00
|
|
|
const nsAFlatCString &aType,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 aPermission,
|
2007-12-04 22:37:36 -08:00
|
|
|
PRInt64 aID,
|
2009-10-16 14:01:04 -07:00
|
|
|
PRUint32 aExpireType,
|
|
|
|
PRInt64 aExpireTime,
|
2007-12-04 22:37:36 -08:00
|
|
|
NotifyOperationType aNotifyOperation,
|
|
|
|
DBOperationType aDBOperation)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gHostArena) {
|
|
|
|
gHostArena = new PLArenaPool;
|
|
|
|
if (!gHostArena)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
PL_INIT_ARENA_POOL(gHostArena, "PermissionHostArena", HOST_ARENA_SIZE);
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// look up the type index
|
|
|
|
PRInt32 typeIndex = GetTypeIndex(aType.get(), PR_TRUE);
|
|
|
|
NS_ENSURE_TRUE(typeIndex != -1, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// When an entry already exists, PutEntry will return that, instead
|
|
|
|
// of adding a new one
|
|
|
|
nsHostEntry *entry = mHostTable.PutEntry(aHost.get());
|
|
|
|
if (!entry) return NS_ERROR_FAILURE;
|
|
|
|
if (!entry->GetKey()) {
|
|
|
|
mHostTable.RawRemoveEntry(entry);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// figure out the transaction type, and get any existing permission value
|
|
|
|
OperationType op;
|
|
|
|
PRInt32 index = entry->GetPermissionIndex(typeIndex);
|
|
|
|
if (index == -1) {
|
|
|
|
if (aPermission == nsIPermissionManager::UNKNOWN_ACTION)
|
|
|
|
op = eOperationNone;
|
|
|
|
else
|
|
|
|
op = eOperationAdding;
|
|
|
|
|
|
|
|
} else {
|
2009-10-16 14:01:04 -07:00
|
|
|
nsPermissionEntry oldPermissionEntry = entry->GetPermissions()[index];
|
|
|
|
|
|
|
|
// remove the permission if the permission is UNKNOWN, update the
|
|
|
|
// permission if its value or expire type have changed OR if the time has
|
|
|
|
// changed and the expire type is time, otherwise, don't modify. There's
|
|
|
|
// no need to modify a permission that doesn't expire with time when the
|
|
|
|
// only thing changed is the expire time.
|
|
|
|
if (aPermission == oldPermissionEntry.mPermission &&
|
|
|
|
aExpireType == oldPermissionEntry.mExpireType &&
|
|
|
|
(aExpireType != nsIPermissionManager::EXPIRE_TIME ||
|
|
|
|
aExpireTime == oldPermissionEntry.mExpireTime))
|
2007-12-04 22:37:36 -08:00
|
|
|
op = eOperationNone;
|
|
|
|
else if (aPermission == nsIPermissionManager::UNKNOWN_ACTION)
|
|
|
|
op = eOperationRemoving;
|
|
|
|
else
|
|
|
|
op = eOperationChanging;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// do the work for adding, deleting, or changing a permission:
|
|
|
|
// update the in-memory list, write to the db, and notify consumers.
|
|
|
|
PRInt64 id;
|
|
|
|
switch (op) {
|
|
|
|
case eOperationNone:
|
|
|
|
{
|
|
|
|
// nothing to do
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
case eOperationAdding:
|
|
|
|
{
|
|
|
|
if (aDBOperation == eWriteToDB) {
|
|
|
|
// we'll be writing to the database - generate a known unique id
|
|
|
|
id = ++mLargestID;
|
|
|
|
} else {
|
|
|
|
// we're reading from the database - use the id already assigned
|
|
|
|
id = aID;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
entry->GetPermissions().AppendElement(nsPermissionEntry(typeIndex, aPermission, id, aExpireType, aExpireTime));
|
2007-12-04 22:37:36 -08:00
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
if (aDBOperation == eWriteToDB && aExpireType != nsIPermissionManager::EXPIRE_SESSION)
|
|
|
|
UpdateDB(op, mStmtInsert, id, aHost, aType, aPermission, aExpireType, aExpireTime);
|
2007-12-04 22:37:36 -08:00
|
|
|
|
|
|
|
if (aNotifyOperation == eNotify) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NotifyObserversWithPermission(aHost,
|
2007-12-04 22:37:36 -08:00
|
|
|
mTypeArray[typeIndex],
|
2007-03-22 10:30:00 -07:00
|
|
|
aPermission,
|
2009-10-16 14:01:04 -07:00
|
|
|
aExpireType,
|
|
|
|
aExpireTime,
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LITERAL_STRING("added").get());
|
2007-12-04 22:37:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eOperationRemoving:
|
|
|
|
{
|
2009-10-16 14:01:04 -07:00
|
|
|
nsPermissionEntry oldPermissionEntry = entry->GetPermissions()[index];
|
|
|
|
id = oldPermissionEntry.mID;
|
2007-12-04 22:37:36 -08:00
|
|
|
entry->GetPermissions().RemoveElementAt(index);
|
|
|
|
|
|
|
|
// If no more types are present, remove the entry
|
|
|
|
if (entry->GetPermissions().IsEmpty())
|
|
|
|
mHostTable.RawRemoveEntry(entry);
|
|
|
|
|
|
|
|
if (aDBOperation == eWriteToDB)
|
2009-10-16 14:01:04 -07:00
|
|
|
UpdateDB(op, mStmtDelete, id, EmptyCString(), EmptyCString(), 0,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0);
|
2007-12-04 22:37:36 -08:00
|
|
|
|
|
|
|
if (aNotifyOperation == eNotify) {
|
|
|
|
NotifyObserversWithPermission(aHost,
|
|
|
|
mTypeArray[typeIndex],
|
2009-10-16 14:01:04 -07:00
|
|
|
oldPermissionEntry.mPermission,
|
|
|
|
oldPermissionEntry.mExpireType,
|
|
|
|
oldPermissionEntry.mExpireTime,
|
2007-12-04 22:37:36 -08:00
|
|
|
NS_LITERAL_STRING("deleted").get());
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case eOperationChanging:
|
|
|
|
{
|
|
|
|
id = entry->GetPermissions()[index].mID;
|
|
|
|
entry->GetPermissions()[index].mPermission = aPermission;
|
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
if (aDBOperation == eWriteToDB && aExpireType != nsIPermissionManager::EXPIRE_SESSION)
|
|
|
|
UpdateDB(op, mStmtUpdate, id, EmptyCString(), EmptyCString(), aPermission, aExpireType, aExpireTime);
|
2007-12-04 22:37:36 -08:00
|
|
|
|
|
|
|
if (aNotifyOperation == eNotify) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NotifyObserversWithPermission(aHost,
|
2007-12-04 22:37:36 -08:00
|
|
|
mTypeArray[typeIndex],
|
2007-03-22 10:30:00 -07:00
|
|
|
aPermission,
|
2009-10-16 14:01:04 -07:00
|
|
|
aExpireType,
|
|
|
|
aExpireTime,
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LITERAL_STRING("changed").get());
|
2007-12-04 22:37:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::Remove(const nsACString &aHost,
|
|
|
|
const char *aType)
|
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
ENSURE_NOT_CHILD_PROCESS;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// AddInternal() handles removal, just let it do the work
|
|
|
|
return AddInternal(PromiseFlatCString(aHost),
|
|
|
|
nsDependentCString(aType),
|
|
|
|
nsIPermissionManager::UNKNOWN_ACTION,
|
|
|
|
0,
|
2009-10-16 14:01:04 -07:00
|
|
|
nsIPermissionManager::EXPIRE_NEVER,
|
|
|
|
0,
|
2007-12-04 22:37:36 -08:00
|
|
|
eNotify,
|
|
|
|
eWriteToDB);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::RemoveAll()
|
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
ENSURE_NOT_CHILD_PROCESS;
|
|
|
|
#endif
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult rv = RemoveAllInternal();
|
2007-03-22 10:30:00 -07:00
|
|
|
NotifyObservers(nsnull, NS_LITERAL_STRING("cleared").get());
|
2007-12-04 22:37:36 -08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::RemoveAllInternal()
|
|
|
|
{
|
|
|
|
RemoveAllFromMemory();
|
|
|
|
|
|
|
|
// clear the db
|
|
|
|
if (mDBConn) {
|
|
|
|
nsresult rv = mDBConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING("DELETE FROM moz_hosts"));
|
|
|
|
if (NS_FAILED(rv)) {
|
2009-04-17 23:42:09 -07:00
|
|
|
mStmtInsert = nsnull;
|
|
|
|
mStmtDelete = nsnull;
|
|
|
|
mStmtUpdate = nsnull;
|
|
|
|
mDBConn = nsnull;
|
|
|
|
rv = InitDB(PR_TRUE);
|
2007-12-04 22:37:36 -08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::TestExactPermission(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 *aPermission)
|
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild* cpc = ChildProcess();
|
2010-07-15 07:04:25 -07:00
|
|
|
if (cpc) {
|
2010-07-26 12:59:34 -07:00
|
|
|
return cpc->SendTestPermission(aURI, nsDependentCString(aType), PR_TRUE,
|
2010-07-15 07:04:25 -07:00
|
|
|
aPermission) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
return CommonTestPermission(aURI, aType, aPermission, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPermissionManager::TestPermission(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 *aPermission)
|
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
2010-07-19 11:33:33 -07:00
|
|
|
ContentChild* cpc = ChildProcess();
|
2010-07-15 07:04:25 -07:00
|
|
|
if (cpc) {
|
2010-07-26 12:59:34 -07:00
|
|
|
return cpc->SendTestPermission(aURI, nsDependentCString(aType), PR_FALSE,
|
2010-07-15 07:04:25 -07:00
|
|
|
aPermission) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
return CommonTestPermission(aURI, aType, aPermission, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::CommonTestPermission(nsIURI *aURI,
|
|
|
|
const char *aType,
|
|
|
|
PRUint32 *aPermission,
|
|
|
|
PRBool aExactHostMatch)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
|
|
|
|
|
|
|
// set the default
|
|
|
|
*aPermission = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
|
|
|
|
nsCAutoString host;
|
|
|
|
nsresult rv = GetHost(aURI, host);
|
2010-08-19 16:06:07 -07:00
|
|
|
// No host doesn't mean an error. Just return the default. Unless this is
|
|
|
|
// a file uri. In that case use a magic host.
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PRBool isFile;
|
|
|
|
rv = aURI->SchemeIs("file", &isFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (isFile) {
|
|
|
|
host.AssignLiteral("<file>");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRInt32 typeIndex = GetTypeIndex(aType, PR_FALSE);
|
|
|
|
// If type == -1, the type isn't known,
|
|
|
|
// so just return NS_OK
|
|
|
|
if (typeIndex == -1) return NS_OK;
|
|
|
|
|
|
|
|
nsHostEntry *entry = GetHostEntry(host, typeIndex, aExactHostMatch);
|
|
|
|
if (entry)
|
2009-10-16 14:01:04 -07:00
|
|
|
*aPermission = entry->GetPermission(typeIndex).mPermission;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get hostentry for given host string and permission type.
|
|
|
|
// walk up the domain if needed.
|
|
|
|
// return null if nothing found.
|
2010-08-19 16:06:07 -07:00
|
|
|
// Also accepts host on the format "<foo>". This will perform an exact match
|
|
|
|
// lookup as the string doesn't contain any dots.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsHostEntry *
|
|
|
|
nsPermissionManager::GetHostEntry(const nsAFlatCString &aHost,
|
|
|
|
PRUint32 aType,
|
|
|
|
PRBool aExactHostMatch)
|
|
|
|
{
|
|
|
|
PRUint32 offset = 0;
|
|
|
|
nsHostEntry *entry;
|
2009-10-16 14:01:04 -07:00
|
|
|
PRInt64 now = PR_Now() / 1000;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
do {
|
|
|
|
entry = mHostTable.GetEntry(aHost.get() + offset);
|
|
|
|
if (entry) {
|
2009-10-16 14:01:04 -07:00
|
|
|
nsPermissionEntry permEntry = entry->GetPermission(aType);
|
|
|
|
|
|
|
|
// if the entry is expired, remove and keep looking for others.
|
|
|
|
if (permEntry.mExpireType == nsIPermissionManager::EXPIRE_TIME &&
|
2009-10-16 16:16:31 -07:00
|
|
|
permEntry.mExpireTime <= now)
|
2009-10-16 14:01:04 -07:00
|
|
|
Remove(aHost, mTypeArray[aType].get());
|
|
|
|
else if (permEntry.mPermission != nsIPermissionManager::UNKNOWN_ACTION)
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
// reset entry, to be able to return null on failure
|
|
|
|
entry = nsnull;
|
|
|
|
}
|
|
|
|
if (aExactHostMatch)
|
|
|
|
break; // do not try super domains
|
|
|
|
|
|
|
|
offset = aHost.FindChar('.', offset) + 1;
|
|
|
|
|
|
|
|
// walk up the domaintree (we stop as soon as we find a match,
|
|
|
|
// which will be the most specific domain we have an entry for).
|
|
|
|
} while (offset > 0);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// helper struct for passing arguments into hash enumeration callback.
|
|
|
|
struct nsGetEnumeratorData
|
|
|
|
{
|
|
|
|
nsGetEnumeratorData(nsCOMArray<nsIPermission> *aArray, const nsTArray<nsCString> *aTypes)
|
|
|
|
: array(aArray)
|
|
|
|
, types(aTypes) {}
|
|
|
|
|
|
|
|
nsCOMArray<nsIPermission> *array;
|
|
|
|
const nsTArray<nsCString> *types;
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static PLDHashOperator
|
2007-12-04 22:37:36 -08:00
|
|
|
AddPermissionsToList(nsHostEntry *entry, void *arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-12-04 22:37:36 -08:00
|
|
|
nsGetEnumeratorData *data = static_cast<nsGetEnumeratorData *>(arg);
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < entry->GetPermissions().Length(); ++i) {
|
|
|
|
nsPermissionEntry &permEntry = entry->GetPermissions()[i];
|
|
|
|
|
|
|
|
nsPermission *perm = new nsPermission(entry->GetHost(),
|
|
|
|
data->types->ElementAt(permEntry.mType),
|
2009-10-16 14:01:04 -07:00
|
|
|
permEntry.mPermission,
|
|
|
|
permEntry.mExpireType,
|
|
|
|
permEntry.mExpireTime);
|
2007-12-04 22:37:36 -08:00
|
|
|
|
|
|
|
data->array->AppendObject(perm);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPermissionManager::GetEnumerator(nsISimpleEnumerator **aEnum)
|
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
ENSURE_NOT_CHILD_PROCESS;
|
|
|
|
#endif
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// roll an nsCOMArray of all our permissions, then hand out an enumerator
|
|
|
|
nsCOMArray<nsIPermission> array;
|
|
|
|
nsGetEnumeratorData data(&array, &mTypeArray);
|
|
|
|
|
|
|
|
mHostTable.EnumerateEntries(AddPermissionsToList, &data);
|
|
|
|
|
|
|
|
return NS_NewArrayEnumerator(aEnum, array);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPermissionManager::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
|
|
|
|
{
|
2010-07-15 07:04:25 -07:00
|
|
|
#ifdef MOZ_IPC
|
|
|
|
ENSURE_NOT_CHILD_PROCESS;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
2007-12-04 22:37:36 -08:00
|
|
|
// The profile is about to change,
|
|
|
|
// or is going away because the application is shutting down.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nsCRT::strcmp(someData, NS_LITERAL_STRING("shutdown-cleanse").get())) {
|
2007-12-04 22:37:36 -08:00
|
|
|
// clear the permissions file
|
|
|
|
RemoveAllInternal();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2007-12-04 22:37:36 -08:00
|
|
|
RemoveAllFromMemory();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
|
2007-12-04 22:37:36 -08:00
|
|
|
// the profile has already changed; init the db from the new location
|
2009-04-17 23:42:09 -07:00
|
|
|
InitDB(PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//*****************************************************************************
|
|
|
|
//*** nsPermissionManager private methods
|
|
|
|
//*****************************************************************************
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::RemoveAllFromMemory()
|
|
|
|
{
|
2007-12-04 22:37:36 -08:00
|
|
|
mLargestID = 0;
|
|
|
|
mTypeArray.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
mHostTable.Clear();
|
|
|
|
if (gHostArena) {
|
|
|
|
PL_FinishArenaPool(gHostArena);
|
|
|
|
delete gHostArena;
|
|
|
|
}
|
|
|
|
gHostArena = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns -1 on failure
|
|
|
|
PRInt32
|
|
|
|
nsPermissionManager::GetTypeIndex(const char *aType,
|
|
|
|
PRBool aAdd)
|
|
|
|
{
|
2007-12-04 22:37:36 -08:00
|
|
|
for (PRUint32 i = 0; i < mTypeArray.Length(); ++i)
|
|
|
|
if (mTypeArray[i].Equals(aType))
|
2007-03-22 10:30:00 -07:00
|
|
|
return i;
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
if (!aAdd) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Not found, but that is ok - we were just looking.
|
|
|
|
return -1;
|
2007-12-04 22:37:36 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This type was not registered before.
|
2007-12-04 22:37:36 -08:00
|
|
|
// append it to the array, without copy-constructing the string
|
|
|
|
nsCString *elem = mTypeArray.AppendElement();
|
|
|
|
if (!elem)
|
2007-03-22 10:30:00 -07:00
|
|
|
return -1;
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
elem->Assign(aType);
|
|
|
|
return mTypeArray.Length() - 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
// wrapper function for mangling (host,type,perm,expireType,expireTime)
|
|
|
|
// set into an nsIPermission.
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsPermissionManager::NotifyObserversWithPermission(const nsACString &aHost,
|
2007-12-04 22:37:36 -08:00
|
|
|
const nsCString &aType,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 aPermission,
|
2009-10-16 14:01:04 -07:00
|
|
|
PRUint32 aExpireType,
|
|
|
|
PRInt64 aExpireTime,
|
2007-03-22 10:30:00 -07:00
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPermission> permission =
|
2009-10-16 14:01:04 -07:00
|
|
|
new nsPermission(aHost, aType, aPermission, aExpireType, aExpireTime);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (permission)
|
|
|
|
NotifyObservers(permission, aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify observers that the permission list changed. there are four possible
|
|
|
|
// values for aData:
|
|
|
|
// "deleted" means a permission was deleted. aPermission is the deleted permission.
|
|
|
|
// "added" means a permission was added. aPermission is the added permission.
|
|
|
|
// "changed" means a permission was altered. aPermission is the new permission.
|
|
|
|
// "cleared" means the entire permission list was cleared. aPermission is null.
|
|
|
|
void
|
|
|
|
nsPermissionManager::NotifyObservers(nsIPermission *aPermission,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
if (mObserverService)
|
|
|
|
mObserverService->NotifyObservers(aPermission,
|
|
|
|
kPermissionChangeNotification,
|
|
|
|
aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::Read()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2007-09-20 22:09:54 -07:00
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
// delete expired permissions before we read in the db
|
|
|
|
{
|
|
|
|
// this deletion has its own scope so the write lock is released when done.
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmtDeleteExpired;
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2009-10-16 16:16:31 -07:00
|
|
|
"DELETE FROM moz_hosts WHERE expireType = ?1 AND expireTime <= ?2"),
|
2009-10-16 14:01:04 -07:00
|
|
|
getter_AddRefs(stmtDeleteExpired));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmtDeleteExpired->BindInt32Parameter(0, nsIPermissionManager::EXPIRE_TIME);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = stmtDeleteExpired->BindInt64Parameter(1, PR_Now() / 1000);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = stmtDeleteExpired->ExecuteStep(&hasResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsCOMPtr<mozIStorageStatement> stmt;
|
|
|
|
rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
|
2009-10-16 14:01:04 -07:00
|
|
|
"SELECT id, host, type, permission, expireType, expireTime "
|
2007-12-04 22:37:36 -08:00
|
|
|
"FROM moz_hosts"), getter_AddRefs(stmt));
|
2007-09-20 22:09:54 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
PRInt64 id;
|
|
|
|
nsCAutoString host, type;
|
|
|
|
PRUint32 permission;
|
2009-10-16 14:01:04 -07:00
|
|
|
PRUint32 expireType;
|
|
|
|
PRInt64 expireTime;
|
2007-12-04 22:37:36 -08:00
|
|
|
PRBool hasResult;
|
|
|
|
while (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
|
|
|
|
// explicitly set our entry id counter for use in AddInternal(),
|
|
|
|
// and keep track of the largest id so we know where to pick up.
|
|
|
|
id = stmt->AsInt64(0);
|
|
|
|
if (id > mLargestID)
|
|
|
|
mLargestID = id;
|
|
|
|
|
|
|
|
rv = stmt->GetUTF8String(1, host);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
rv = stmt->GetUTF8String(2, type);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
permission = stmt->AsInt32(3);
|
2009-10-16 14:01:04 -07:00
|
|
|
expireType = stmt->AsInt32(4);
|
2007-12-04 22:37:36 -08:00
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
// convert into PRInt64 value (milliseconds)
|
|
|
|
expireTime = stmt->AsInt64(5);
|
|
|
|
|
|
|
|
rv = AddInternal(host, type, permission, id, expireType, expireTime,
|
|
|
|
eDontNotify, eNoDBOperation);
|
2007-12-04 22:37:36 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char kMatchTypeHost[] = "host";
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPermissionManager::Import()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> permissionsFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(permissionsFile));
|
2008-01-11 16:38:38 -08:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-12-04 22:37:36 -08:00
|
|
|
|
|
|
|
rv = permissionsFile->AppendNative(NS_LITERAL_CSTRING(kHostpermFileName));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> fileInputStream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
|
|
|
|
permissionsFile);
|
2008-01-11 16:38:38 -08:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsILineInputStream> lineInputStream = do_QueryInterface(fileInputStream, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// start a transaction on the storage db, to optimize insertions.
|
|
|
|
// transaction will automically commit on completion
|
|
|
|
mozStorageTransaction transaction(mDBConn, PR_TRUE);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* format is:
|
|
|
|
* matchtype \t type \t permission \t host
|
|
|
|
* Only "host" is supported for matchtype
|
|
|
|
* type is a string that identifies the type of permission (e.g. "cookie")
|
|
|
|
* permission is an integer between 1 and 15
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsCAutoString buffer;
|
|
|
|
PRBool isMore = PR_TRUE;
|
|
|
|
while (isMore && NS_SUCCEEDED(lineInputStream->ReadLine(buffer, &isMore))) {
|
|
|
|
if (buffer.IsEmpty() || buffer.First() == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> lineArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// Split the line at tabs
|
2009-01-21 20:15:34 -08:00
|
|
|
ParseString(buffer, '\t', lineArray);
|
2007-12-04 22:37:36 -08:00
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
if (lineArray[0].EqualsLiteral(kMatchTypeHost) &&
|
|
|
|
lineArray.Length() == 4) {
|
2007-12-04 22:37:36 -08:00
|
|
|
|
|
|
|
PRInt32 error;
|
2009-01-21 20:15:34 -08:00
|
|
|
PRUint32 permission = lineArray[2].ToInteger(&error);
|
2007-12-04 22:37:36 -08:00
|
|
|
if (error)
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// hosts might be encoded in UTF8; switch them to ACE to be consistent
|
2009-01-21 20:15:34 -08:00
|
|
|
if (!IsASCII(lineArray[3])) {
|
|
|
|
rv = NormalizeToACE(lineArray[3]);
|
2007-12-04 22:37:36 -08:00
|
|
|
if (NS_FAILED(rv))
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-10-16 14:01:04 -07:00
|
|
|
rv = AddInternal(lineArray[3], lineArray[1], permission, 0,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0, eDontNotify, eWriteToDB);
|
2007-12-04 22:37:36 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// we're done importing - delete the old file
|
|
|
|
permissionsFile->Remove(PR_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult
|
|
|
|
nsPermissionManager::NormalizeToACE(nsCString &aHost)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-12-04 22:37:36 -08:00
|
|
|
// lazily init the IDN service
|
|
|
|
if (!mIDNService) {
|
|
|
|
nsresult rv;
|
|
|
|
mIDNService = do_GetService(NS_IDNSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
return mIDNService->ConvertUTF8toACE(aHost, aHost);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-12-04 22:37:36 -08:00
|
|
|
nsPermissionManager::GetHost(nsIURI *aURI, nsACString &aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-12-04 22:37:36 -08:00
|
|
|
nsCOMPtr<nsIURI> innerURI = NS_GetInnermostURI(aURI);
|
|
|
|
if (!innerURI) return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
nsresult rv = innerURI->GetAsciiHost(aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
if (NS_FAILED(rv) || aResult.IsEmpty())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
void
|
|
|
|
nsPermissionManager::UpdateDB(OperationType aOp,
|
|
|
|
mozIStorageStatement* aStmt,
|
|
|
|
PRInt64 aID,
|
|
|
|
const nsACString &aHost,
|
|
|
|
const nsACString &aType,
|
2009-10-16 14:01:04 -07:00
|
|
|
PRUint32 aPermission,
|
|
|
|
PRUint32 aExpireType,
|
|
|
|
PRInt64 aExpireTime)
|
2007-12-04 22:37:36 -08:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
// no statement is ok - just means we don't have a profile
|
|
|
|
if (!aStmt)
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
switch (aOp) {
|
|
|
|
case eOperationAdding:
|
|
|
|
{
|
|
|
|
rv = aStmt->BindInt64Parameter(0, aID);
|
|
|
|
if (NS_FAILED(rv)) break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
rv = aStmt->BindUTF8StringParameter(1, aHost);
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
rv = aStmt->BindUTF8StringParameter(2, aType);
|
|
|
|
if (NS_FAILED(rv)) break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
rv = aStmt->BindInt32Parameter(3, aPermission);
|
2009-10-16 14:01:04 -07:00
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
rv = aStmt->BindInt32Parameter(4, aExpireType);
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
rv = aStmt->BindInt64Parameter(5, aExpireTime);
|
2007-12-04 22:37:36 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
case eOperationRemoving:
|
|
|
|
{
|
|
|
|
rv = aStmt->BindInt64Parameter(0, aID);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
case eOperationChanging:
|
|
|
|
{
|
|
|
|
rv = aStmt->BindInt64Parameter(0, aID);
|
|
|
|
if (NS_FAILED(rv)) break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
rv = aStmt->BindInt32Parameter(1, aPermission);
|
2009-10-16 14:01:04 -07:00
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
rv = aStmt->BindInt32Parameter(2, aExpireType);
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
rv = aStmt->BindInt64Parameter(3, aExpireTime);
|
2007-12-04 22:37:36 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("need a valid operation in UpdateDB()!");
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRBool hasResult;
|
|
|
|
rv = aStmt->ExecuteStep(&hasResult);
|
|
|
|
aStmt->Reset();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-04 22:37:36 -08:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("db change failed!");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-12-04 22:37:36 -08:00
|
|
|
|