2016-06-14 15:57:36 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "StorageManager.h"
|
|
|
|
|
2020-10-21 16:16:19 +03:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <utility>
|
|
|
|
#include "ErrorList.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "js/CallArgs.h"
|
|
|
|
#include "js/TypeDecls.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/MacroForEach.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/TelemetryScalarEnums.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2016-06-14 15:57:36 +03:00
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
|
|
|
#include "mozilla/dom/StorageManagerBinding.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
2016-06-14 15:57:36 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
|
|
|
#include "mozilla/dom/WorkerStatus.h"
|
|
|
|
#include "mozilla/dom/quota/QuotaManagerService.h"
|
2017-04-17 10:06:27 +03:00
|
|
|
#include "nsContentPermissionHelper.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIGlobalObject.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2016-06-14 15:57:36 +03:00
|
|
|
#include "nsIQuotaCallbacks.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "nsIQuotaManagerService.h"
|
2016-06-14 15:57:36 +03:00
|
|
|
#include "nsIQuotaRequests.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "nsIQuotaResults.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsLiteralString.h"
|
2016-06-14 15:57:36 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2020-10-21 16:16:19 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFlags.h"
|
|
|
|
#include "nsTLiteralString.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
|
|
|
|
class JSObject;
|
|
|
|
struct JSContext;
|
|
|
|
struct nsID;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
class Runnable;
|
|
|
|
}
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2018-08-20 15:32:33 +03:00
|
|
|
using namespace mozilla::dom::quota;
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2016-06-14 15:57:36 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
// This class is used to get quota usage, request persist and check persisted
|
|
|
|
// status callbacks.
|
2019-08-22 23:51:09 +03:00
|
|
|
class RequestResolver final : public nsIQuotaCallback {
|
2017-04-17 10:06:27 +03:00
|
|
|
public:
|
|
|
|
enum Type { Estimate, Persist, Persisted };
|
|
|
|
|
|
|
|
private:
|
2016-06-14 15:57:36 +03:00
|
|
|
class FinishWorkerRunnable;
|
|
|
|
|
|
|
|
// If this resolver was created for a window then mPromise must be non-null.
|
|
|
|
// Otherwise mProxy must be non-null.
|
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
|
|
|
|
|
|
nsresult mResultCode;
|
|
|
|
StorageEstimate mStorageEstimate;
|
2017-04-17 10:06:27 +03:00
|
|
|
const Type mType;
|
|
|
|
bool mPersisted;
|
2016-06-14 15:57:36 +03:00
|
|
|
|
|
|
|
public:
|
2017-04-17 10:06:27 +03:00
|
|
|
RequestResolver(Type aType, Promise* aPromise)
|
2016-06-14 15:57:36 +03:00
|
|
|
: mPromise(aPromise),
|
|
|
|
mResultCode(NS_OK),
|
2017-04-17 10:06:27 +03:00
|
|
|
mType(aType),
|
|
|
|
mPersisted(false) {
|
2016-06-14 15:57:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
RequestResolver(Type aType, PromiseWorkerProxy* aProxy)
|
2016-06-14 15:57:36 +03:00
|
|
|
: mProxy(aProxy), mResultCode(NS_OK), mType(aType), mPersisted(false) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
void ResolveOrReject();
|
2016-06-14 15:57:36 +03:00
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2017-04-17 10:06:27 +03:00
|
|
|
NS_DECL_NSIQUOTACALLBACK
|
2016-06-14 15:57:36 +03:00
|
|
|
|
|
|
|
private:
|
2020-02-12 13:25:22 +03:00
|
|
|
~RequestResolver() = default;
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
nsresult GetStorageEstimate(nsIVariant* aResult);
|
|
|
|
|
|
|
|
nsresult GetPersisted(nsIVariant* aResult);
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
nsresult OnCompleteInternal(nsIQuotaRequest* aRequest);
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
nsresult Finish();
|
2016-06-14 15:57:36 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class is used to return promise on worker thread.
|
2017-04-17 10:06:27 +03:00
|
|
|
class RequestResolver::FinishWorkerRunnable final : public WorkerRunnable {
|
|
|
|
RefPtr<RequestResolver> mResolver;
|
2016-06-14 15:57:36 +03:00
|
|
|
|
|
|
|
public:
|
2017-04-17 10:06:27 +03:00
|
|
|
explicit FinishWorkerRunnable(RequestResolver* aResolver)
|
2016-06-14 15:57:36 +03:00
|
|
|
: WorkerRunnable(aResolver->mProxy->GetWorkerPrivate()),
|
|
|
|
mResolver(aResolver) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aResolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
|
|
|
};
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
class EstimateWorkerMainThreadRunnable final : public WorkerMainThreadRunnable {
|
2016-06-14 15:57:36 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
EstimateWorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseWorkerProxy* aProxy)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
"StorageManager :: Estimate"_ns),
|
|
|
|
mProxy(aProxy) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() override;
|
|
|
|
};
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
class PersistedWorkerMainThreadRunnable final
|
|
|
|
: public WorkerMainThreadRunnable {
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PersistedWorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseWorkerProxy* aProxy)
|
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
"StorageManager :: Persisted"_ns),
|
|
|
|
mProxy(aProxy) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainThreadRun() override;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* PersistentStoragePermissionRequest
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
class PersistentStoragePermissionRequest final
|
2018-11-14 08:33:25 +03:00
|
|
|
: public ContentPermissionRequestBase {
|
2017-04-17 10:06:27 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PersistentStoragePermissionRequest(nsIPrincipal* aPrincipal,
|
|
|
|
nsPIDOMWindowInner* aWindow,
|
|
|
|
Promise* aPromise)
|
2019-02-04 22:56:48 +03:00
|
|
|
: ContentPermissionRequestBase(aPrincipal, aWindow,
|
2018-11-14 08:33:25 +03:00
|
|
|
"dom.storageManager"_ns,
|
|
|
|
"persistent-storage"_ns),
|
2017-04-17 10:06:27 +03:00
|
|
|
mPromise(aPromise) {
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Start();
|
|
|
|
|
2018-11-14 08:33:25 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PersistentStoragePermissionRequest,
|
|
|
|
ContentPermissionRequestBase)
|
|
|
|
|
|
|
|
// nsIContentPermissionRequest
|
|
|
|
NS_IMETHOD Cancel(void) override;
|
|
|
|
NS_IMETHOD Allow(JS::HandleValue choices) override;
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
private:
|
2018-11-14 08:33:25 +03:00
|
|
|
~PersistentStoragePermissionRequest() = default;
|
2017-04-17 10:06:27 +03:00
|
|
|
};
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
nsresult Estimate(nsIPrincipal* aPrincipal, nsIQuotaCallback* aCallback,
|
|
|
|
nsIQuotaRequest** aRequest) {
|
2016-06-14 15:57:36 +03:00
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
// Firefox and Quota Manager have always used the schemeless origin group
|
|
|
|
// (https://storage.spec.whatwg.org/#schemeless-origin-group) for quota limit
|
|
|
|
// purposes. This has been to prevent a site/eTLD+1 from claiming more than
|
|
|
|
// its fair share of storage through the use of sub-domains. Because the limit
|
|
|
|
// is at the group level and the usage needs to make sense in the context of
|
|
|
|
// that limit, we also expose the group usage. Bug 1374970 reflects this
|
|
|
|
// reality and bug 1305665 tracks our plan to eliminate our use of groups for
|
|
|
|
// this.
|
|
|
|
|
2016-06-14 15:57:36 +03:00
|
|
|
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
|
|
|
|
if (NS_WARN_IF(!qms)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
nsCOMPtr<nsIQuotaRequest> request;
|
|
|
|
nsresult rv = qms->Estimate(aPrincipal, getter_AddRefs(request));
|
2016-06-14 15:57:36 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(request->SetCallback(aCallback));
|
|
|
|
|
|
|
|
request.forget(aRequest);
|
2016-06-14 15:57:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
nsresult Persisted(nsIPrincipal* aPrincipal, nsIQuotaCallback* aCallback,
|
|
|
|
nsIQuotaRequest** aRequest) {
|
|
|
|
MOZ_ASSERT(aPrincipal);
|
|
|
|
MOZ_ASSERT(aCallback);
|
2016-06-14 15:57:36 +03:00
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
|
|
|
|
if (NS_WARN_IF(!qms)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIQuotaRequest> request;
|
|
|
|
nsresult rv = qms->Persisted(aPrincipal, getter_AddRefs(request));
|
2016-06-14 15:57:36 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
// All the methods in nsIQuotaManagerService shouldn't synchronously fire
|
|
|
|
// any callbacks when they are being executed. Even when a result is ready,
|
|
|
|
// a new runnable should be dispatched to current thread to fire the callback
|
|
|
|
// asynchronously. It's safe to set the callback after we call Persisted().
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(request->SetCallback(aCallback));
|
|
|
|
|
|
|
|
request.forget(aRequest);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<Promise> ExecuteOpOnMainOrWorkerThread(
|
|
|
|
nsIGlobalObject* aGlobal, RequestResolver::Type aType, ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
MOZ_ASSERT_IF(aType == RequestResolver::Type::Persist, NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
|
|
|
|
if (NS_WARN_IF(!promise)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
|
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = window->GetExtantDoc();
|
2017-04-17 10:06:27 +03:00
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
|
2017-04-17 10:07:15 +03:00
|
|
|
// Storage Standard 7. API
|
|
|
|
// If origin is an opaque origin, then reject promise with a TypeError.
|
|
|
|
if (principal->GetIsNullPrincipal()) {
|
2019-09-19 21:18:28 +03:00
|
|
|
switch (aType) {
|
|
|
|
case RequestResolver::Type::Persisted:
|
|
|
|
promise->MaybeRejectWithTypeError(
|
2020-03-07 00:05:48 +03:00
|
|
|
"persisted() called for opaque origin");
|
2019-09-19 21:18:28 +03:00
|
|
|
break;
|
|
|
|
case RequestResolver::Type::Persist:
|
|
|
|
promise->MaybeRejectWithTypeError(
|
2020-03-07 00:05:48 +03:00
|
|
|
"persist() called for opaque origin");
|
2019-09-19 21:18:28 +03:00
|
|
|
break;
|
|
|
|
case RequestResolver::Type::Estimate:
|
|
|
|
promise->MaybeRejectWithTypeError(
|
2020-03-07 00:05:48 +03:00
|
|
|
"estimate() called for opaque origin");
|
2019-09-19 21:18:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:07:15 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
switch (aType) {
|
|
|
|
case RequestResolver::Type::Persisted: {
|
|
|
|
RefPtr<RequestResolver> resolver =
|
|
|
|
new RequestResolver(RequestResolver::Type::Persisted, promise);
|
|
|
|
|
|
|
|
RefPtr<nsIQuotaRequest> request;
|
|
|
|
aRv = Persisted(principal, resolver, getter_AddRefs(request));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RequestResolver::Type::Persist: {
|
|
|
|
RefPtr<PersistentStoragePermissionRequest> request =
|
2019-02-04 22:56:48 +03:00
|
|
|
new PersistentStoragePermissionRequest(principal, window, promise);
|
2017-04-17 10:06:27 +03:00
|
|
|
|
2017-04-17 10:07:02 +03:00
|
|
|
// In private browsing mode, no permission prompt.
|
|
|
|
if (nsContentUtils::IsInPrivateBrowsing(doc)) {
|
|
|
|
aRv = request->Cancel();
|
2019-11-07 17:29:02 +03:00
|
|
|
} else if (!request->CheckPermissionDelegate()) {
|
|
|
|
aRv = request->Cancel();
|
2017-04-17 10:07:02 +03:00
|
|
|
} else {
|
|
|
|
aRv = request->Start();
|
|
|
|
}
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RequestResolver::Type::Estimate: {
|
|
|
|
RefPtr<RequestResolver> resolver =
|
|
|
|
new RequestResolver(RequestResolver::Type::Estimate, promise);
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
RefPtr<nsIQuotaRequest> request;
|
|
|
|
aRv = Estimate(principal, resolver, getter_AddRefs(request));
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Invalid aRequest type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
RefPtr<PromiseWorkerProxy> promiseProxy =
|
|
|
|
PromiseWorkerProxy::Create(workerPrivate, promise);
|
|
|
|
if (NS_WARN_IF(!promiseProxy)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case RequestResolver::Type::Estimate: {
|
|
|
|
RefPtr<EstimateWorkerMainThreadRunnable> runnnable =
|
|
|
|
new EstimateWorkerMainThreadRunnable(promiseProxy->GetWorkerPrivate(),
|
|
|
|
promiseProxy);
|
2018-07-12 20:33:41 +03:00
|
|
|
runnnable->Dispatch(Canceling, aRv);
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RequestResolver::Type::Persisted: {
|
|
|
|
RefPtr<PersistedWorkerMainThreadRunnable> runnnable =
|
|
|
|
new PersistedWorkerMainThreadRunnable(
|
|
|
|
promiseProxy->GetWorkerPrivate(), promiseProxy);
|
2018-07-12 20:33:41 +03:00
|
|
|
runnnable->Dispatch(Canceling, aRv);
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Invalid aRequest type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Local class implementations
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
void RequestResolver::ResolveOrReject() {
|
|
|
|
class MOZ_STACK_CLASS AutoCleanup final {
|
|
|
|
RefPtr<PromiseWorkerProxy> mProxy;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit AutoCleanup(PromiseWorkerProxy* aProxy) : mProxy(aProxy) {
|
|
|
|
MOZ_ASSERT(aProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCleanup() {
|
|
|
|
MOZ_ASSERT(mProxy);
|
|
|
|
|
|
|
|
mProxy->CleanUp();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RefPtr<Promise> promise;
|
|
|
|
Maybe<AutoCleanup> autoCleanup;
|
|
|
|
|
|
|
|
if (mPromise) {
|
|
|
|
promise = mPromise;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mProxy);
|
|
|
|
|
|
|
|
promise = mProxy->WorkerPromise();
|
|
|
|
|
|
|
|
// Only clean up for worker case.
|
|
|
|
autoCleanup.emplace(mProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(promise);
|
|
|
|
|
|
|
|
if (mType == Type::Estimate) {
|
|
|
|
if (NS_SUCCEEDED(mResultCode)) {
|
|
|
|
promise->MaybeResolve(mStorageEstimate);
|
|
|
|
} else {
|
2019-09-19 21:18:28 +03:00
|
|
|
promise->MaybeRejectWithTypeError(
|
2020-03-07 00:05:48 +03:00
|
|
|
"Internal error while estimating storage usage");
|
2017-04-17 10:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mType == Type::Persist || mType == Type::Persisted);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mResultCode)) {
|
|
|
|
promise->MaybeResolve(mPersisted);
|
|
|
|
} else {
|
|
|
|
promise->MaybeResolve(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
NS_IMPL_ISUPPORTS(RequestResolver, nsIQuotaCallback)
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
nsresult RequestResolver::GetStorageEstimate(nsIVariant* aResult) {
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
MOZ_ASSERT(mType == Type::Estimate);
|
|
|
|
|
2018-11-20 04:20:05 +03:00
|
|
|
MOZ_ASSERT(aResult->GetDataType() == nsIDataType::VTYPE_INTERFACE_IS);
|
2017-04-17 10:06:27 +03:00
|
|
|
|
2017-03-22 14:13:38 +03:00
|
|
|
nsID* iid;
|
|
|
|
nsCOMPtr<nsISupports> supports;
|
2017-04-17 10:06:27 +03:00
|
|
|
nsresult rv = aResult->GetAsInterface(&iid, getter_AddRefs(supports));
|
2016-06-14 15:57:36 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-22 14:13:38 +03:00
|
|
|
free(iid);
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
nsCOMPtr<nsIQuotaEstimateResult> estimateResult = do_QueryInterface(supports);
|
|
|
|
MOZ_ASSERT(estimateResult);
|
2017-03-22 14:13:38 +03:00
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
2019-08-22 23:51:09 +03:00
|
|
|
estimateResult->GetUsage(&mStorageEstimate.mUsage.Construct()));
|
2017-03-22 14:13:38 +03:00
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
2019-08-22 23:51:09 +03:00
|
|
|
estimateResult->GetLimit(&mStorageEstimate.mQuota.Construct()));
|
2017-03-22 14:13:38 +03:00
|
|
|
|
2016-06-14 15:57:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
nsresult RequestResolver::GetPersisted(nsIVariant* aResult) {
|
|
|
|
MOZ_ASSERT(aResult);
|
|
|
|
MOZ_ASSERT(mType == Type::Persist || mType == Type::Persisted);
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
#ifdef DEBUG
|
2018-11-20 04:20:05 +03:00
|
|
|
uint16_t dataType = aResult->GetDataType();
|
2017-04-17 10:06:27 +03:00
|
|
|
#endif
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
if (mType == Type::Persist) {
|
|
|
|
MOZ_ASSERT(dataType == nsIDataType::VTYPE_VOID);
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
mPersisted = true;
|
|
|
|
return NS_OK;
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
MOZ_ASSERT(dataType == nsIDataType::VTYPE_BOOL);
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
bool persisted;
|
|
|
|
nsresult rv = aResult->GetAsBool(&persisted);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPersisted = persisted;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
nsresult RequestResolver::OnCompleteInternal(nsIQuotaRequest* aRequest) {
|
2016-06-14 15:57:36 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
nsresult resultCode;
|
|
|
|
nsresult rv = aRequest->GetResultCode(&resultCode);
|
2016-06-14 15:57:36 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-04-17 10:06:27 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(resultCode)) {
|
|
|
|
return resultCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIVariant> result;
|
|
|
|
rv = aRequest->GetResult(getter_AddRefs(result));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mType == Type::Estimate) {
|
|
|
|
rv = GetStorageEstimate(result);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mType == Type::Persist || mType == Type::Persisted);
|
|
|
|
|
|
|
|
rv = GetPersisted(result);
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult RequestResolver::Finish() {
|
2016-06-14 15:57:36 +03:00
|
|
|
// In a main thread request.
|
|
|
|
if (!mProxy) {
|
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
ResolveOrReject();
|
2016-06-14 15:57:36 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
{
|
|
|
|
// In a worker thread request.
|
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
if (NS_WARN_IF(mProxy->CleanedUp())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<FinishWorkerRunnable> runnable = new FinishWorkerRunnable(this);
|
|
|
|
if (NS_WARN_IF(!runnable->Dispatch())) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
RequestResolver::OnComplete(nsIQuotaRequest* aRequest) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
mResultCode = OnCompleteInternal(aRequest);
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
nsresult rv = Finish();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RequestResolver::FinishWorkerRunnable::WorkerRun(
|
|
|
|
JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
|
2017-04-17 10:06:27 +03:00
|
|
|
MOZ_ASSERT(aCx);
|
2016-06-14 15:57:36 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
MOZ_ASSERT(mResolver);
|
|
|
|
mResolver->ResolveOrReject();
|
2016-06-14 15:57:36 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EstimateWorkerMainThreadRunnable::MainThreadRun() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
|
|
if (mProxy->CleanedUp()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
principal = mProxy->GetWorkerPrivate()->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
RefPtr<RequestResolver> resolver =
|
|
|
|
new RequestResolver(RequestResolver::Type::Estimate, mProxy);
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2019-08-22 23:51:09 +03:00
|
|
|
RefPtr<nsIQuotaRequest> request;
|
|
|
|
nsresult rv = Estimate(principal, resolver, getter_AddRefs(request));
|
2016-06-14 15:57:36 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
bool PersistedWorkerMainThreadRunnable::MainThreadRun() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mProxy->Lock());
|
|
|
|
if (mProxy->CleanedUp()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
principal = mProxy->GetWorkerPrivate()->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
|
|
|
|
RefPtr<RequestResolver> resolver =
|
|
|
|
new RequestResolver(RequestResolver::Type::Persisted, mProxy);
|
|
|
|
|
|
|
|
RefPtr<nsIQuotaRequest> request;
|
|
|
|
nsresult rv = Persisted(principal, resolver, getter_AddRefs(request));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult PersistentStoragePermissionRequest::Start() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-11-14 08:33:25 +03:00
|
|
|
PromptResult pr;
|
2019-04-25 19:20:48 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
// on Android calling `ShowPrompt` here calls
|
|
|
|
// `nsContentPermissionUtils::AskPermission` once, and a response of
|
|
|
|
// `PromptResult::Pending` calls it again. This results in multiple requests
|
|
|
|
// for storage access, so we check the prompt prefs only to ensure we only
|
|
|
|
// request it once.
|
|
|
|
pr = CheckPromptPrefs();
|
|
|
|
#else
|
2018-11-14 08:33:25 +03:00
|
|
|
nsresult rv = ShowPrompt(pr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2019-04-25 19:20:48 +03:00
|
|
|
#endif
|
2018-11-14 08:33:25 +03:00
|
|
|
if (pr == PromptResult::Granted) {
|
|
|
|
return Allow(JS::UndefinedHandleValue);
|
|
|
|
}
|
|
|
|
if (pr == PromptResult::Denied) {
|
2017-04-17 10:06:27 +03:00
|
|
|
return Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsContentPermissionUtils::AskPermission(this, mWindow);
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2018-11-14 08:33:25 +03:00
|
|
|
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
|
|
|
|
PersistentStoragePermissionRequest, ContentPermissionRequestBase)
|
2017-04-24 16:41:01 +03:00
|
|
|
|
2018-11-14 08:33:25 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(PersistentStoragePermissionRequest,
|
|
|
|
ContentPermissionRequestBase, mPromise)
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PersistentStoragePermissionRequest::Cancel() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mPromise);
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
RefPtr<RequestResolver> resolver =
|
|
|
|
new RequestResolver(RequestResolver::Type::Persisted, mPromise);
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
RefPtr<nsIQuotaRequest> request;
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
return Persisted(mPrincipal, resolver, getter_AddRefs(request));
|
|
|
|
}
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PersistentStoragePermissionRequest::Allow(JS::HandleValue aChoices) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<RequestResolver> resolver =
|
|
|
|
new RequestResolver(RequestResolver::Type::Persist, mPromise);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
|
|
|
|
if (NS_WARN_IF(!qms)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
RefPtr<nsIQuotaRequest> request;
|
2016-06-14 15:57:36 +03:00
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
nsresult rv = qms->Persist(mPrincipal, getter_AddRefs(request));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:06:27 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(request->SetCallback(resolver));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* StorageManager
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
StorageManager::StorageManager(nsIGlobalObject* aGlobal) : mOwner(aGlobal) {
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:25:22 +03:00
|
|
|
StorageManager::~StorageManager() = default;
|
2017-04-17 10:06:27 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise> StorageManager::Persisted(ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(mOwner);
|
|
|
|
|
|
|
|
return ExecuteOpOnMainOrWorkerThread(mOwner, RequestResolver::Type::Persisted,
|
|
|
|
aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> StorageManager::Persist(ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(mOwner);
|
|
|
|
|
2017-04-27 14:06:54 +03:00
|
|
|
Telemetry::ScalarAdd(Telemetry::ScalarID::NAVIGATOR_STORAGE_PERSIST_COUNT, 1);
|
2017-04-17 10:06:27 +03:00
|
|
|
return ExecuteOpOnMainOrWorkerThread(mOwner, RequestResolver::Type::Persist,
|
|
|
|
aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> StorageManager::Estimate(ErrorResult& aRv) {
|
|
|
|
MOZ_ASSERT(mOwner);
|
|
|
|
|
2017-04-27 14:06:54 +03:00
|
|
|
Telemetry::ScalarAdd(Telemetry::ScalarID::NAVIGATOR_STORAGE_ESTIMATE_COUNT,
|
|
|
|
1);
|
2017-04-17 10:06:27 +03:00
|
|
|
return ExecuteOpOnMainOrWorkerThread(mOwner, RequestResolver::Type::Estimate,
|
|
|
|
aRv);
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StorageManager, mOwner)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(StorageManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(StorageManager)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StorageManager)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
JSObject* StorageManager::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return StorageManager_Binding::Wrap(aCx, this, aGivenProto);
|
2016-06-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|