Bug 1888878 - Extract CreatePromise to PromiseUtils.h; r=dom-storage-reviewers,asuth

Differential Revision: https://phabricator.services.mozilla.com/D206230
This commit is contained in:
Jan Varga 2024-06-20 11:15:46 +00:00
parent e284e670c3
commit 984836f827
4 changed files with 71 additions and 23 deletions

View File

@ -25,6 +25,7 @@
#include "mozilla/dom/PBackgroundLSSharedTypes.h"
#include "mozilla/dom/PBackgroundLSSimpleRequest.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/quota/PromiseUtils.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/BackgroundUtils.h"
@ -154,26 +155,6 @@ class SimpleRequestResolver final : public LSSimpleRequestChildCallback {
void OnResponse(const LSSimpleRequestResponse& aResponse) override;
};
nsresult CreatePromise(JSContext* aContext, Promise** aPromise) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aContext);
nsIGlobalObject* global =
xpc::NativeGlobal(JS::CurrentGlobalOrNull(aContext));
if (NS_WARN_IF(!global)) {
return NS_ERROR_FAILURE;
}
ErrorResult result;
RefPtr<Promise> promise = Promise::Create(global, result);
if (result.Failed()) {
return result.StealNSResult();
}
promise.forget(aPromise);
return NS_OK;
}
nsresult CheckedPrincipalToPrincipalInfo(
nsIPrincipal* aPrincipal, mozilla::ipc::PrincipalInfo& aPrincipalInfo) {
MOZ_ASSERT(NS_IsMainThread());
@ -320,7 +301,7 @@ LocalStorageManager2::Preload(nsIPrincipal* aPrincipal, JSContext* aContext,
RefPtr<Promise> promise;
if (aContext) {
rv = CreatePromise(aContext, getter_AddRefs(promise));
rv = quota::CreatePromise(aContext, getter_AddRefs(promise));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -364,7 +345,7 @@ LocalStorageManager2::IsPreloaded(nsIPrincipal* aPrincipal, JSContext* aContext,
MOZ_ASSERT(_retval);
RefPtr<Promise> promise;
nsresult rv = CreatePromise(aContext, getter_AddRefs(promise));
nsresult rv = quota::CreatePromise(aContext, getter_AddRefs(promise));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
@ -395,7 +376,7 @@ LocalStorageManager2::GetState(nsIPrincipal* aPrincipal, JSContext* aContext,
MOZ_ASSERT(_retval);
RefPtr<Promise> promise;
nsresult rv = CreatePromise(aContext, getter_AddRefs(promise));
nsresult rv = quota::CreatePromise(aContext, getter_AddRefs(promise));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

View File

@ -0,0 +1,40 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 "mozilla/dom/quota/PromiseUtils.h"
#include "jsapi.h"
#include "MainThreadUtils.h"
#include "nsDebug.h"
#include "xpcpublic.h"
#include "mozilla/Assertions.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/Promise.h"
namespace mozilla::dom::quota {
nsresult CreatePromise(JSContext* aContext, Promise** aPromise) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aContext);
nsIGlobalObject* global =
xpc::NativeGlobal(JS::CurrentGlobalOrNull(aContext));
if (NS_WARN_IF(!global)) {
return NS_ERROR_FAILURE;
}
ErrorResult result;
RefPtr<Promise> promise = Promise::Create(global, result);
if (result.Failed()) {
return result.StealNSResult();
}
promise.forget(aPromise);
return NS_OK;
}
} // namespace mozilla::dom::quota

25
dom/quota/PromiseUtils.h Normal file
View File

@ -0,0 +1,25 @@
/* -*- 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/. */
#ifndef DOM_QUOTA_PROMISEUTILS_H_
#define DOM_QUOTA_PROMISEUTILS_H_
#include "ErrorList.h"
#include "jstypes.h"
struct JS_PUBLIC_API JSContext;
namespace mozilla::dom {
class Promise;
}
namespace mozilla::dom::quota {
nsresult CreatePromise(JSContext* aContext, Promise** aPromise);
} // namespace mozilla::dom::quota
#endif // DOM_QUOTA_PROMISEUTILS_H_

View File

@ -60,6 +60,7 @@ EXPORTS.mozilla.dom.quota += [
"NSSCipherStrategy.h",
"OriginScope.h",
"PersistenceType.h",
"PromiseUtils.h",
"QuotaCommon.h",
"QuotaManager.h",
"QuotaManagerImpl.h",
@ -108,6 +109,7 @@ UNIFIED_SOURCES += [
"OriginOperations.cpp",
"OriginParser.cpp",
"PersistenceType.cpp",
"PromiseUtils.cpp",
"QMResult.cpp",
"QuotaCommon.cpp",
"QuotaManagerService.cpp",