Bug 1165217 - Use nsIPrincipal instead of nsIURI/appId/inBrowser for nsIQuotaManager, r=janv

This commit is contained in:
Michael Layzell 2015-07-03 17:36:28 -04:00
parent aa26047103
commit 5dd6c9804e
11 changed files with 76 additions and 132 deletions

View File

@ -188,8 +188,11 @@ function initIndexedDBRow()
var quotaManager = Components.classes["@mozilla.org/dom/quota/manager;1"]
.getService(nsIQuotaManager);
let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(Components.interfaces.nsIScriptSecurityManager)
.createCodebasePrincipal(gPermURI, {});
gUsageRequest =
quotaManager.getUsageForURI(gPermURI, onIndexedDBUsageCallback);
quotaManager.getUsageForPrincipal(principal, onIndexedDBUsageCallback);
var status = document.getElementById("indexedDBStatus");
var button = document.getElementById("indexedDBClear");
@ -201,9 +204,13 @@ function initIndexedDBRow()
function onIndexedDBClear()
{
let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(Components.interfaces.nsIScriptSecurityManager)
.createCodebasePrincipal(gPermURI, {});
Components.classes["@mozilla.org/dom/quota/manager;1"]
.getService(nsIQuotaManager)
.clearStoragesForURI(gPermURI);
.clearStoragesForPrincipal(principal);
Components.classes["@mozilla.org/serviceworkers/manager;1"]
.getService(Components.interfaces.nsIServiceWorkerManager)
@ -213,8 +220,9 @@ function onIndexedDBClear()
initIndexedDBRow();
}
function onIndexedDBUsageCallback(uri, usage, fileUsage)
function onIndexedDBUsageCallback(principal, usage, fileUsage)
{
let uri = principal.URI;
if (!uri.equals(gPermURI)) {
throw new Error("Callback received for bad URI: " + uri);
}

View File

@ -6,13 +6,16 @@ const nsIQuotaManager = Components.interfaces.nsIQuotaManager;
let gURI = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI("http://localhost", null, null);
function onUsageCallback(uri, usage, fileUsage) {}
function onUsageCallback(principal, usage, fileUsage) {}
function onLoad()
{
var quotaManager = Components.classes["@mozilla.org/dom/quota/manager;1"]
.getService(nsIQuotaManager);
var quotaRequest = quotaManager.getUsageForURI(gURI, onUsageCallback);
let principal = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(Components.interfaces.nsIScriptSecurityManager)
.createCodebasePrincipal(gURI, {});
var quotaRequest = quotaManager.getUsageForPrincipal(principal, onUsageCallback);
quotaRequest.cancel();
Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService)

View File

@ -92,7 +92,7 @@ function testSteps()
let usageBeforeMaintenance;
quotaManager.getUsageForURI(uri, (url, usage) => {
quotaManager.getUsageForPrincipal(principal, (principal, usage) => {
ok(usage > 0, "Usage is non-zero");
usageBeforeMaintenance = usage;
continueToNextStep();
@ -118,7 +118,7 @@ function testSteps()
let usageAfterMaintenance;
quotaManager.getUsageForURI(uri, (url, usage) => {
quotaManager.getUsageForPrincipal(principal, (principal, usage) => {
ok(usage > 0, "Usage is non-zero");
usageAfterMaintenance = usage;
continueToNextStep();

View File

@ -259,7 +259,10 @@ function resetOrClearAllDatabases(callback, clear) {
let uri = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService)
.newURI("http://foo.com", null, null);
quotaManager.getUsageForURI(uri, function(usage, fileUsage) {
let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
.getService(Ci.nsIScriptSecurityManager)
.createCodebasePrincipal(uri, {});
quotaManager.getUsageForPrincipal(principal, function(principal, usage, fileUsage) {
callback();
});
}

View File

@ -27,6 +27,7 @@
#include <algorithm>
#include "GeckoProfiler.h"
#include "mozilla/Atomics.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/CondVar.h"
#include "mozilla/dom/PContent.h"
#include "mozilla/dom/asmjscache/AsmJSCache.h"
@ -669,20 +670,16 @@ class GetUsageOp
UsageInfo mUsageInfo;
const nsCString mGroup;
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIPrincipal> mPrincipal;
nsCOMPtr<nsIUsageCallback> mCallback;
const uint32_t mAppId;
const bool mIsApp;
const bool mInMozBrowserOnly;
public:
GetUsageOp(const nsACString& aGroup,
const nsACString& aOrigin,
bool aIsApp,
nsIURI* aURI,
nsIUsageCallback* aCallback,
uint32_t aAppId,
bool aInMozBrowserOnly);
nsIPrincipal* aPrincipal,
nsIUsageCallback* aCallback);
private:
~GetUsageOp()
@ -3404,32 +3401,6 @@ QuotaManager::GetStorageId(PersistenceType aPersistenceType,
aDatabaseId = str;
}
// static
nsresult
QuotaManager::GetInfoFromURI(nsIURI* aURI,
uint32_t aAppId,
bool aInMozBrowser,
nsACString* aGroup,
nsACString* aOrigin,
bool* aIsApp)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aURI);
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
NS_ENSURE_TRUE(secMan, NS_ERROR_FAILURE);
nsCOMPtr<nsIPrincipal> principal;
nsresult rv = secMan->GetAppCodebasePrincipal(aURI, aAppId, aInMozBrowser,
getter_AddRefs(principal));
NS_ENSURE_SUCCESS(rv, rv);
rv = GetInfoFromPrincipal(principal, aGroup, aOrigin, aIsApp);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
static nsresult
TryGetInfoForAboutURI(nsIPrincipal* aPrincipal,
nsACString& aGroup,
@ -3709,36 +3680,27 @@ QuotaManager::GetDirectoryMetadata(nsIFile* aDirectory,
NS_IMPL_ISUPPORTS(QuotaManager, nsIQuotaManager, nsIObserver)
NS_IMETHODIMP
QuotaManager::GetUsageForURI(nsIURI* aURI,
nsIUsageCallback* aCallback,
uint32_t aAppId,
bool aInMozBrowserOnly,
uint8_t aOptionalArgCount,
nsIQuotaRequest** _retval)
QuotaManager::GetUsageForPrincipal(nsIPrincipal* aPrincipal,
nsIUsageCallback* aCallback,
nsIQuotaRequest** _retval)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
NS_ENSURE_ARG_POINTER(aURI);
NS_ENSURE_ARG_POINTER(aPrincipal);
NS_ENSURE_ARG_POINTER(aCallback);
// This only works from the main process.
NS_ENSURE_TRUE(XRE_IsParentProcess(), NS_ERROR_NOT_AVAILABLE);
if (!aOptionalArgCount) {
aAppId = nsIScriptSecurityManager::NO_APP_ID;
}
// Figure out which origin we're dealing with.
nsCString group;
nsCString origin;
bool isApp;
nsresult rv = GetInfoFromURI(aURI, aAppId, aInMozBrowserOnly, &group, &origin,
&isApp);
nsresult rv = GetInfoFromPrincipal(aPrincipal, &group, &origin, &isApp);
NS_ENSURE_SUCCESS(rv, rv);
nsRefPtr<GetUsageOp> op =
new GetUsageOp(group, origin, isApp, aURI, aCallback, aAppId,
aInMozBrowserOnly);
new GetUsageOp(group, origin, isApp, aPrincipal, aCallback);
op->RunImmediately();
@ -3764,15 +3726,12 @@ QuotaManager::Clear()
}
NS_IMETHODIMP
QuotaManager::ClearStoragesForURI(nsIURI* aURI,
uint32_t aAppId,
bool aInMozBrowserOnly,
const nsACString& aPersistenceType,
uint8_t aOptionalArgCount)
QuotaManager::ClearStoragesForPrincipal(nsIPrincipal* aPrincipal,
const nsACString& aPersistenceType)
{
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
NS_ENSURE_ARG_POINTER(aURI);
NS_ENSURE_ARG_POINTER(aPrincipal);
Nullable<PersistenceType> persistenceType;
nsresult rv =
@ -3784,18 +3743,16 @@ QuotaManager::ClearStoragesForURI(nsIURI* aURI,
// This only works from the main process.
NS_ENSURE_TRUE(XRE_IsParentProcess(), NS_ERROR_NOT_AVAILABLE);
if (!aOptionalArgCount) {
aAppId = nsIScriptSecurityManager::NO_APP_ID;
}
// Figure out which origin we're dealing with.
nsCString origin;
rv = GetInfoFromURI(aURI, aAppId, aInMozBrowserOnly, nullptr, &origin,
nullptr);
rv = GetInfoFromPrincipal(aPrincipal, nullptr, &origin, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
const mozilla::OriginAttributes& attrs =
mozilla::BasePrincipal::Cast(aPrincipal)->OriginAttributesRef();
nsAutoCString pattern;
GetOriginPatternString(aAppId, aInMozBrowserOnly, origin, pattern);
GetOriginPatternString(attrs.mAppId, attrs.mInBrowser, origin, pattern);
nsRefPtr<OriginClearOp> op =
new OriginClearOp(persistenceType, OriginScope::FromPattern(pattern));
@ -4625,24 +4582,20 @@ SaveOriginAccessTimeOp::DoDirectoryWork(QuotaManager* aQuotaManager)
GetUsageOp::GetUsageOp(const nsACString& aGroup,
const nsACString& aOrigin,
bool aIsApp,
nsIURI* aURI,
nsIUsageCallback* aCallback,
uint32_t aAppId,
bool aInMozBrowserOnly)
nsIPrincipal* aPrincipal,
nsIUsageCallback* aCallback)
: NormalOriginOperationBase(Nullable<PersistenceType>(),
OriginScope::FromOrigin(aOrigin),
/* aExclusive */ false)
, mGroup(aGroup)
, mURI(aURI)
, mPrincipal(aPrincipal)
, mCallback(aCallback)
, mAppId(aAppId)
, mIsApp(aIsApp)
, mInMozBrowserOnly(aInMozBrowserOnly)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!aGroup.IsEmpty());
MOZ_ASSERT(!aOrigin.IsEmpty());
MOZ_ASSERT(aURI);
MOZ_ASSERT(aPrincipal);
MOZ_ASSERT(aCallback);
}
@ -4777,12 +4730,11 @@ GetUsageOp::SendResults()
mUsageInfo.ResetUsage();
}
mCallback->OnUsageResult(mURI, mUsageInfo.TotalUsage(), mUsageInfo.FileUsage(), mAppId,
mInMozBrowserOnly);
mCallback->OnUsageResult(mPrincipal, mUsageInfo.TotalUsage(), mUsageInfo.FileUsage());
}
// Clean up.
mURI = nullptr;
mPrincipal = nullptr;
mCallback = nullptr;
}

View File

@ -306,14 +306,6 @@ public:
Client::Type aClientType,
nsACString& aDatabaseId);
static nsresult
GetInfoFromURI(nsIURI* aURI,
uint32_t aAppId,
bool aInMozBrowser,
nsACString* aGroup,
nsACString* aOrigin,
bool* aIsApp);
static nsresult
GetInfoFromPrincipal(nsIPrincipal* aPrincipal,
nsACString* aGroup,

View File

@ -7,27 +7,24 @@
#include "nsISupports.idl"
interface nsIQuotaRequest;
interface nsIURI;
interface nsIPrincipal;
interface nsIUsageCallback;
[scriptable, builtinclass, uuid(2968fcd5-1872-4ddc-8c16-62b27e357f31)]
[scriptable, builtinclass, uuid(101cf53c-e7f3-4723-9f43-a23a85c8eda0)]
interface nsIQuotaManager : nsISupports
{
/**
* Schedules an asynchronous callback that will return the total amount of
* disk space being used by storages for the given origin.
*
* @param aURI
* The URI whose usage is being queried.
* @param aPrincipal
* A principal for the origin whose usage is being queried.
* @param aCallback
* The callback that will be called when the usage is available.
*/
[optional_argc]
nsIQuotaRequest
getUsageForURI(in nsIURI aURI,
in nsIUsageCallback aCallback,
[optional] in unsigned long aAppId,
[optional] in boolean aInMozBrowserOnly);
getUsageForPrincipal(in nsIPrincipal aPrincipal,
in nsIUsageCallback aCallback);
/**
* Removes all storages. The files may not be deleted immediately depending
@ -44,15 +41,12 @@ interface nsIQuotaManager : nsISupports
* Removes all storages stored for the given URI. The files may not be
* deleted immediately depending on prohibitive concurrent operations.
*
* @param aURI
* The URI whose storages are to be cleared.
* @param aPrincipal
* A principal for the origin whose storages are to be cleared.
*/
[optional_argc]
void
clearStoragesForURI(in nsIURI aURI,
[optional] in unsigned long aAppId,
[optional] in boolean aInMozBrowserOnly,
[optional] in ACString aPersistenceType);
clearStoragesForPrincipal(in nsIPrincipal aPrincipal,
[optional] in ACString aPersistenceType);
/**
* Resets quota and storage management. This can be used to force

View File

@ -6,15 +6,12 @@
#include "nsISupports.idl"
interface nsIURI;
interface nsIPrincipal;
[scriptable, function, uuid(7b0f9928-0ddc-42c7-b9f2-6b2308b90b18)]
[scriptable, function, uuid(54b9f44f-533f-41ee-8fa8-86cc978125f0)]
interface nsIUsageCallback : nsISupports
{
void
onUsageResult(in nsIURI aURI,
in unsigned long long aUsage,
in unsigned long long aFileUsage,
in unsigned long aAppId,
in boolean aInMozBrowserOnly);
void onUsageResult(in nsIPrincipal aPrincipal,
in unsigned long long aUsage,
in unsigned long long aFileUsage);
};

View File

@ -34,6 +34,7 @@ skip-if = true
[test_ril_worker_voiceprivacy.js]
[test_ril_worker_ecm.js]
[test_ril_worker_stk.js]
requesttimeoutfactor = 2
[test_ril_worker_barring_password.js]
[test_ril_worker_cdma_info_rec.js]
[test_ril_system_messenger.js]

View File

@ -505,24 +505,22 @@ SpecialPowersObserverAPI.prototype = {
throw new SpecialPowersError('Invalid operation for SPQuotaManager');
}
let uri = this._getURI(msg.uri);
let secMan = Services.scriptSecurityManager;
let principal = secMan.createCodebasePrincipal(this._getURI(msg.uri), {
appId: msg.appId,
inBrowser: msg.inBrowser,
});
if (op == 'clear') {
if (('inBrowser' in msg) && msg.inBrowser !== undefined) {
qm.clearStoragesForURI(uri, msg.appId, msg.inBrowser);
} else if (('appId' in msg) && msg.appId !== undefined) {
qm.clearStoragesForURI(uri, msg.appId);
} else {
qm.clearStoragesForURI(uri);
}
qm.clearStoragesForPrincipal(principal);
} else if (op == 'reset') {
qm.reset();
}
// We always use the getUsageForURI callback even if we're clearing
// since we know that clear and getUsageForURI are synchronized by the
// We always use the getUsageForPrincipal callback even if we're clearing
// since we know that clear and getUsageForPrincipal are synchronized by the
// QuotaManager.
let callback = function(uri, usage, fileUsage) {
let callback = function(principal, usage, fileUsage) {
let reply = { id: msg.id };
if (op == 'getUsage') {
reply.usage = usage;
@ -531,13 +529,7 @@ SpecialPowersObserverAPI.prototype = {
mm.sendAsyncMessage(aMessage.name, reply);
};
if (('inBrowser' in msg) && msg.inBrowser !== undefined) {
qm.getUsageForURI(uri, callback, msg.appId, msg.inBrowser);
} else if (('appId' in msg) && msg.appId !== undefined) {
qm.getUsageForURI(uri, callback, msg.appId);
} else {
qm.getUsageForURI(uri, callback);
}
qm.getUsageForPrincipal(principal, callback);
return undefined; // See comment at the beginning of this function.
}

View File

@ -160,8 +160,10 @@ this.ForgetAboutSite = {
caUtils);
let httpURI = caUtils.makeURI("http://" + aDomain);
let httpsURI = caUtils.makeURI("https://" + aDomain);
qm.clearStoragesForURI(httpURI);
qm.clearStoragesForURI(httpsURI);
let httpPrincipal = Services.scriptSecurityManager.createCodebasePrincipal(httpURI, {});
let httpsPrincipal = Services.scriptSecurityManager.createCodebasePrincipal(httpsURI, {});
qm.clearStoragesForPrincipal(httpPrincipal);
qm.clearStoragesForPrincipal(httpsPrincipal);
function onContentPrefsRemovalFinished() {
// Everybody else (including extensions)