2015-05-03 22:32:37 +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: */
|
2013-06-12 05:41:21 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#ifndef mozilla_dom_Promise_h
|
|
|
|
#define mozilla_dom_Promise_h
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2020-11-23 19:10:47 +03:00
|
|
|
#include <functional>
|
2020-03-28 16:35:31 +03:00
|
|
|
#include <type_traits>
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include <utility>
|
2020-11-23 19:10:47 +03:00
|
|
|
#include "ErrorList.h"
|
|
|
|
#include "js/RootingAPI.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "js/TypeDecls.h"
|
2020-11-23 19:10:47 +03:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2020-11-23 19:10:47 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/Result.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2021-03-05 18:29:49 +03:00
|
|
|
#include "mozilla/dom/AutoEntryScript.h"
|
2020-11-23 19:10:47 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-04-10 22:57:07 +04:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
Bug 1609996 - Reorder some includes affected by the previous patches. r=froydnj
This was done by:
This was done by applying:
```
diff --git a/python/mozbuild/mozbuild/code-analysis/mach_commands.py b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
index 789affde7bbf..fe33c4c7d4d1 100644
--- a/python/mozbuild/mozbuild/code-analysis/mach_commands.py
+++ b/python/mozbuild/mozbuild/code-analysis/mach_commands.py
@@ -2007,7 +2007,7 @@ class StaticAnalysis(MachCommandBase):
from subprocess import Popen, PIPE, check_output, CalledProcessError
diff_process = Popen(self._get_clang_format_diff_command(commit), stdout=PIPE)
- args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format]
+ args = [sys.executable, clang_format_diff, "-p1", "-binary=%s" % clang_format, '-sort-includes']
if not output_file:
args.append("-i")
```
Then running `./mach clang-format -c <commit-hash>`
Then undoing that patch.
Then running check_spidermonkey_style.py --fixup
Then running `./mach clang-format`
I had to fix four things:
* I needed to move <utility> back down in GuardObjects.h because I was hitting
obscure problems with our system include wrappers like this:
0:03.94 /usr/include/stdlib.h:550:14: error: exception specification in declaration does not match previous declaration
0:03.94 extern void *realloc (void *__ptr, size_t __size)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/malloc_decls.h:53:1: note: previous declaration is here
0:03.94 MALLOC_DECL(realloc, void*, void*, size_t)
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozilla/mozalloc.h:22:32: note: expanded from macro 'MALLOC_DECL'
0:03.94 MOZ_MEMORY_API return_type name##_impl(__VA_ARGS__);
0:03.94 ^
0:03.94 <scratch space>:178:1: note: expanded from here
0:03.94 realloc_impl
0:03.94 ^
0:03.94 /home/emilio/src/moz/gecko-2/obj-debug/dist/include/mozmemory_wrap.h:142:41: note: expanded from macro 'realloc_impl'
0:03.94 #define realloc_impl mozmem_malloc_impl(realloc)
Which I really didn't feel like digging into.
* I had to restore the order of TrustOverrideUtils.h and related files in nss
because the .inc files depend on TrustOverrideUtils.h being included earlier.
* I had to add a missing include to RollingNumber.h
* Also had to partially restore include order in JsepSessionImpl.cpp to avoid
some -WError issues due to some static inline functions being defined in a
header but not used in the rest of the compilation unit.
Differential Revision: https://phabricator.services.mozilla.com/D60327
--HG--
extra : moz-landing-system : lando
2020-01-20 19:19:48 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2020-11-23 19:10:47 +03:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsString.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2020-11-23 19:10:47 +03:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
2014-02-26 01:34:55 +04:00
|
|
|
class nsIGlobalObject;
|
|
|
|
|
2020-11-23 19:10:47 +03:00
|
|
|
namespace JS {
|
|
|
|
class Value;
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
namespace mozilla {
|
2018-08-18 19:43:22 +03:00
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class AnyCallback;
|
2014-09-20 10:20:41 +04:00
|
|
|
class MediaStreamError;
|
2013-11-19 22:43:51 +04:00
|
|
|
class PromiseInit;
|
|
|
|
class PromiseNativeHandler;
|
2014-07-02 08:19:48 +04:00
|
|
|
class PromiseDebugging;
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2020-07-23 17:51:46 +03:00
|
|
|
class Promise : public SupportsWeakPtr {
|
2013-11-24 23:26:07 +04:00
|
|
|
friend class PromiseTask;
|
2014-02-24 17:56:54 +04:00
|
|
|
friend class PromiseWorkerProxy;
|
|
|
|
friend class PromiseWorkerProxyRunnable;
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
public:
|
2020-07-08 01:20:01 +03:00
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(Promise)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
enum PropagateUserInteraction {
|
|
|
|
eDontPropagateUserInteraction,
|
|
|
|
ePropagateUserInteraction
|
|
|
|
};
|
|
|
|
|
2014-07-19 05:31:11 +04:00
|
|
|
// Promise creation tries to create a JS reflector for the Promise, so is
|
|
|
|
// fallible. Furthermore, we don't want to do JS-wrapping on a 0-refcount
|
|
|
|
// object, so we addref before doing that and return the addrefed pointer
|
|
|
|
// here.
|
2018-10-10 00:42:22 +03:00
|
|
|
// Pass ePropagateUserInteraction for aPropagateUserInteraction if you want
|
|
|
|
// the promise resolve handler to be called as if we were handling user
|
|
|
|
// input events in case we are currently handling user input events.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise> Create(
|
2018-10-10 00:42:22 +03:00
|
|
|
nsIGlobalObject* aGlobal, ErrorResult& aRv,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction =
|
|
|
|
eDontPropagateUserInteraction);
|
2016-03-22 18:22:24 +03:00
|
|
|
|
|
|
|
// Reports a rejected Promise by sending an error report.
|
|
|
|
static void ReportRejectedPromise(JSContext* aCx, JS::HandleObject aPromise);
|
2014-02-26 01:34:55 +04:00
|
|
|
|
|
|
|
typedef void (Promise::*MaybeFunc)(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
// Helpers for using Promise from C++.
|
2014-04-16 23:13:41 +04:00
|
|
|
// Most DOM objects are handled already. To add a new type T, add a
|
|
|
|
// ToJSValue overload in ToJSValue.h.
|
|
|
|
// aArg is a const reference so we can pass rvalues like integer constants
|
2014-02-26 01:34:55 +04:00
|
|
|
template <typename T>
|
2018-06-02 14:26:06 +03:00
|
|
|
void MaybeResolve(T&& aArg) {
|
|
|
|
MaybeSomething(std::forward<T>(aArg), &Promise::MaybeResolve);
|
2014-02-26 01:34:55 +04:00
|
|
|
}
|
|
|
|
|
2016-08-10 05:07:42 +03:00
|
|
|
void MaybeResolveWithUndefined();
|
|
|
|
|
2019-07-29 17:43:54 +03:00
|
|
|
void MaybeReject(JS::Handle<JS::Value> aValue) {
|
|
|
|
MaybeSomething(aValue, &Promise::MaybeReject);
|
|
|
|
}
|
2019-11-15 01:51:21 +03:00
|
|
|
|
|
|
|
// This method is deprecated. Consumers should MaybeRejectWithDOMException if
|
|
|
|
// they are rejecting with a DOMException, or use one of the other
|
|
|
|
// MaybeReject* methods otherwise. If they have a random nsresult which may
|
|
|
|
// or may not correspond to a DOMException type, they should consider using an
|
|
|
|
// appropriate DOMException-type nsresult with an informative message and
|
|
|
|
// calling MaybeRejectWithDOMException.
|
2014-06-03 19:38:38 +04:00
|
|
|
inline void MaybeReject(nsresult aArg) {
|
|
|
|
MOZ_ASSERT(NS_FAILED(aArg));
|
2014-02-26 01:34:55 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
2014-09-20 10:20:41 +04:00
|
|
|
|
2020-02-14 18:42:30 +03:00
|
|
|
inline void MaybeReject(ErrorResult&& aArg) {
|
2014-10-30 18:12:09 +03:00
|
|
|
MOZ_ASSERT(aArg.Failed());
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeSomething(std::move(aArg), &Promise::MaybeReject);
|
|
|
|
// That should have consumed aArg.
|
|
|
|
MOZ_ASSERT(!aArg.Failed());
|
2014-10-30 18:12:09 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void MaybeReject(const RefPtr<MediaStreamError>& aArg);
|
2014-09-20 10:20:41 +04:00
|
|
|
|
2016-06-09 22:26:47 +03:00
|
|
|
void MaybeRejectWithUndefined();
|
|
|
|
|
2019-03-26 22:34:29 +03:00
|
|
|
void MaybeResolveWithClone(JSContext* aCx, JS::Handle<JS::Value> aValue);
|
|
|
|
void MaybeRejectWithClone(JSContext* aCx, JS::Handle<JS::Value> aValue);
|
|
|
|
|
2019-09-19 16:02:39 +03:00
|
|
|
// Facilities for rejecting with various spec-defined exception values.
|
2020-02-03 23:37:32 +03:00
|
|
|
#define DOMEXCEPTION(name, err) \
|
|
|
|
inline void MaybeRejectWith##name(const nsACString& aMessage) { \
|
|
|
|
ErrorResult res; \
|
|
|
|
res.Throw##name(aMessage); \
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeReject(std::move(res)); \
|
2020-02-03 23:37:32 +03:00
|
|
|
} \
|
|
|
|
template <int N> \
|
|
|
|
void MaybeRejectWith##name(const char(&aMessage)[N]) { \
|
|
|
|
MaybeRejectWith##name(nsLiteralCString(aMessage)); \
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:37:32 +03:00
|
|
|
#include "mozilla/dom/DOMExceptionNames.h"
|
|
|
|
|
|
|
|
#undef DOMEXCEPTION
|
|
|
|
|
2019-09-19 16:02:39 +03:00
|
|
|
template <ErrNum errorNumber, typename... Ts>
|
|
|
|
void MaybeRejectWithTypeError(Ts&&... aMessageArgs) {
|
|
|
|
ErrorResult res;
|
|
|
|
res.ThrowTypeError<errorNumber>(std::forward<Ts>(aMessageArgs)...);
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeReject(std::move(res));
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
2020-03-07 00:05:48 +03:00
|
|
|
inline void MaybeRejectWithTypeError(const nsACString& aMessage) {
|
2019-09-19 16:02:39 +03:00
|
|
|
ErrorResult res;
|
|
|
|
res.ThrowTypeError(aMessage);
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeReject(std::move(res));
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <int N>
|
2020-03-07 00:05:48 +03:00
|
|
|
void MaybeRejectWithTypeError(const char (&aMessage)[N]) {
|
|
|
|
MaybeRejectWithTypeError(nsLiteralCString(aMessage));
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <ErrNum errorNumber, typename... Ts>
|
|
|
|
void MaybeRejectWithRangeError(Ts&&... aMessageArgs) {
|
|
|
|
ErrorResult res;
|
|
|
|
res.ThrowRangeError<errorNumber>(std::forward<Ts>(aMessageArgs)...);
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeReject(std::move(res));
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
2020-03-07 00:05:48 +03:00
|
|
|
inline void MaybeRejectWithRangeError(const nsACString& aMessage) {
|
2019-09-19 16:02:39 +03:00
|
|
|
ErrorResult res;
|
|
|
|
res.ThrowRangeError(aMessage);
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeReject(std::move(res));
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <int N>
|
2020-03-07 00:05:48 +03:00
|
|
|
void MaybeRejectWithRangeError(const char (&aMessage)[N]) {
|
|
|
|
MaybeRejectWithRangeError(nsLiteralCString(aMessage));
|
2019-09-19 16:02:39 +03:00
|
|
|
}
|
|
|
|
|
2014-06-03 19:38:38 +04:00
|
|
|
// DO NOT USE MaybeRejectBrokenly with in new code. Promises should be
|
|
|
|
// rejected with Error instances.
|
2017-08-06 20:47:00 +03:00
|
|
|
// Note: MaybeRejectBrokenly is a template so we can use it with DOMException
|
|
|
|
// without instantiating the DOMException specialization of MaybeSomething in
|
2014-06-03 19:38:38 +04:00
|
|
|
// every translation unit that includes this header, because that would
|
2017-08-06 20:47:00 +03:00
|
|
|
// require use to include DOMException.h either here or in all those
|
|
|
|
// translation units.
|
2014-06-03 19:38:38 +04:00
|
|
|
template <typename T>
|
|
|
|
void MaybeRejectBrokenly(const T& aArg); // Not implemented by default; see
|
|
|
|
// specializations in the .cpp for
|
|
|
|
// the T values we support.
|
2014-02-26 01:34:55 +04:00
|
|
|
|
2020-03-18 05:22:00 +03:00
|
|
|
// Mark a settled promise as already handled so that rejections will not
|
|
|
|
// be reported as unhandled.
|
2020-11-23 19:10:47 +03:00
|
|
|
void SetSettledPromiseIsHandled();
|
2020-03-18 05:22:00 +03:00
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
// WebIDL
|
|
|
|
|
2019-05-02 21:38:04 +03:00
|
|
|
nsIGlobalObject* GetParentObject() const { return GetGlobalObject(); }
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
// Do the equivalent of Promise.resolve in the compartment of aGlobal. The
|
|
|
|
// compartment of aCx is ignored. Errors are reported on the ErrorResult; if
|
|
|
|
// aRv comes back !Failed(), this function MUST return a non-null value.
|
2018-10-10 00:42:22 +03:00
|
|
|
// Pass ePropagateUserInteraction for aPropagateUserInteraction if you want
|
|
|
|
// the promise resolve handler to be called as if we were handling user
|
|
|
|
// input events in case we are currently handling user input events.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise> Resolve(
|
|
|
|
nsIGlobalObject* aGlobal, JSContext* aCx, JS::Handle<JS::Value> aValue,
|
2018-10-10 00:42:22 +03:00
|
|
|
ErrorResult& aRv,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction =
|
|
|
|
eDontPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
// Do the equivalent of Promise.reject in the compartment of aGlobal. The
|
|
|
|
// compartment of aCx is ignored. Errors are reported on the ErrorResult; if
|
|
|
|
// aRv comes back !Failed(), this function MUST return a non-null value.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise> Reject(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
// Do the equivalent of Promise.all in the current compartment of aCx. Errors
|
|
|
|
// are reported on the ErrorResult; if aRv comes back !Failed(), this function
|
|
|
|
// MUST return a non-null value.
|
2018-10-10 00:42:22 +03:00
|
|
|
// Pass ePropagateUserInteraction for aPropagateUserInteraction if you want
|
|
|
|
// the promise resolve handler to be called as if we were handling user
|
|
|
|
// input events in case we are currently handling user input events.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise> All(
|
2018-04-14 02:31:42 +03:00
|
|
|
JSContext* aCx, const nsTArray<RefPtr<Promise>>& aPromiseList,
|
2018-10-10 00:42:22 +03:00
|
|
|
ErrorResult& aRv,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction =
|
|
|
|
eDontPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
|
|
|
|
void Then(JSContext* aCx,
|
|
|
|
// aCalleeGlobal may not be in the compartment of aCx, when called
|
|
|
|
// over Xrays.
|
|
|
|
JS::Handle<JSObject*> aCalleeGlobal, AnyCallback* aResolveCallback,
|
|
|
|
AnyCallback* aRejectCallback, JS::MutableHandle<JS::Value> aRetval,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2018-08-18 19:43:22 +03:00
|
|
|
template <typename Callback, typename... Args>
|
|
|
|
using IsHandlerCallback =
|
2020-03-28 16:57:21 +03:00
|
|
|
std::is_same<already_AddRefed<Promise>,
|
|
|
|
decltype(std::declval<Callback>()(
|
|
|
|
(JSContext*)(nullptr),
|
|
|
|
std::declval<JS::Handle<JS::Value>>(),
|
|
|
|
std::declval<Args>()...))>;
|
2018-08-18 19:43:22 +03:00
|
|
|
|
2018-08-27 23:10:23 +03:00
|
|
|
template <typename Callback, typename... Args>
|
|
|
|
using ThenResult =
|
2020-03-28 16:35:31 +03:00
|
|
|
std::enable_if_t<IsHandlerCallback<Callback, Args...>::value,
|
|
|
|
Result<RefPtr<Promise>, nsresult>>;
|
2018-08-27 23:10:23 +03:00
|
|
|
|
2018-08-18 19:43:22 +03:00
|
|
|
// Similar to the JavaScript Then() function. Accepts a single lambda function
|
|
|
|
// argument, which it attaches as a native resolution handler, and returns a
|
|
|
|
// new promise which resolves with that handler's return value, or propagates
|
|
|
|
// any rejections from this promise.
|
|
|
|
//
|
|
|
|
// Any additional arguments passed after the callback function are stored and
|
|
|
|
// passed as additional arguments to the function when it is called. These
|
|
|
|
// values will participate in cycle collection for the promise handler, and
|
|
|
|
// therefore may safely form reference cycles with the promise chain.
|
|
|
|
//
|
|
|
|
// Any strong references required by the callback should be passed in this
|
|
|
|
// manner, rather than using lambda capture, lambda captures do not support
|
|
|
|
// cycle collection, and can easily lead to leaks.
|
|
|
|
//
|
|
|
|
// Does not currently support rejection handlers.
|
|
|
|
template <typename Callback, typename... Args>
|
2018-08-27 23:10:23 +03:00
|
|
|
ThenResult<Callback, Args...> ThenWithCycleCollectedArgs(
|
2018-08-18 19:43:22 +03:00
|
|
|
Callback&& aOnResolve, Args&&... aArgs);
|
|
|
|
|
|
|
|
Result<RefPtr<Promise>, nsresult> ThenWithoutCycleCollection(
|
|
|
|
const std::function<
|
|
|
|
already_AddRefed<Promise>(JSContext*, JS::HandleValue)>& aCallback);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JSObject* PromiseObj() const { return mPromiseObj; }
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
void AppendNativeHandler(PromiseNativeHandler* aRunnable);
|
|
|
|
|
2019-05-02 21:38:04 +03:00
|
|
|
nsIGlobalObject* GetGlobalObject() const { return mGlobal; }
|
2015-01-16 01:39:02 +03:00
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// Create a dom::Promise from a given SpiderMonkey Promise object.
|
|
|
|
// aPromiseObj MUST be in the compartment of aGlobal's global JS object.
|
2018-10-10 00:42:22 +03:00
|
|
|
// Pass ePropagateUserInteraction for aPropagateUserInteraction if you want
|
|
|
|
// the promise resolve handler to be called as if we were handling user
|
|
|
|
// input events in case we are currently handling user input events.
|
2016-02-10 01:40:31 +03:00
|
|
|
static already_AddRefed<Promise> CreateFromExisting(
|
2018-10-10 00:42:22 +03:00
|
|
|
nsIGlobalObject* aGlobal, JS::Handle<JSObject*> aPromiseObj,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction =
|
|
|
|
eDontPropagateUserInteraction);
|
2015-11-25 23:48:09 +03:00
|
|
|
|
2016-09-07 05:51:41 +03:00
|
|
|
enum class PromiseState { Pending, Resolved, Rejected };
|
|
|
|
|
|
|
|
PromiseState State() const;
|
|
|
|
|
2021-11-16 01:01:38 +03:00
|
|
|
static already_AddRefed<Promise> CreateResolvedWithUndefined(
|
|
|
|
nsIGlobalObject* aGlobal, ErrorResult& aRv);
|
|
|
|
|
2014-09-12 06:18:49 +04:00
|
|
|
protected:
|
2020-02-03 23:37:32 +03:00
|
|
|
// Legacy method for throwing DOMExceptions. Only used by media code at this
|
|
|
|
// point, via DetailedPromise. Do NOT add new uses! When this is removed,
|
|
|
|
// remove the friend declaration in ErrorResult.h.
|
|
|
|
inline void MaybeRejectWithDOMException(nsresult rv,
|
|
|
|
const nsACString& aMessage) {
|
|
|
|
ErrorResult res;
|
|
|
|
res.ThrowDOMException(rv, aMessage);
|
2020-02-14 18:42:30 +03:00
|
|
|
MaybeReject(std::move(res));
|
2020-02-03 23:37:32 +03:00
|
|
|
}
|
|
|
|
|
2015-11-25 23:48:08 +03:00
|
|
|
struct PromiseCapability;
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// Do NOT call this unless you're Promise::Create or
|
|
|
|
// Promise::CreateFromExisting. I wish we could enforce that from inside this
|
|
|
|
// class too, somehow.
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit Promise(nsIGlobalObject* aGlobal);
|
2014-07-19 05:31:11 +04:00
|
|
|
|
2014-09-12 06:18:49 +04:00
|
|
|
virtual ~Promise();
|
|
|
|
|
2019-10-26 11:14:05 +03:00
|
|
|
// Do JS-wrapping after Promise creation.
|
2018-10-10 00:42:22 +03:00
|
|
|
// Pass ePropagateUserInteraction for aPropagateUserInteraction if you want
|
|
|
|
// the promise resolve handler to be called as if we were handling user
|
|
|
|
// input events in case we are currently handling user input events.
|
2019-10-26 11:14:05 +03:00
|
|
|
void CreateWrapper(ErrorResult& aRv,
|
2018-10-10 00:42:22 +03:00
|
|
|
PropagateUserInteraction aPropagateUserInteraction =
|
|
|
|
eDontPropagateUserInteraction);
|
2014-09-12 06:18:49 +04:00
|
|
|
|
|
|
|
private:
|
2019-07-29 17:43:54 +03:00
|
|
|
void MaybeResolve(JSContext* aCx, JS::Handle<JS::Value> aValue);
|
|
|
|
void MaybeReject(JSContext* aCx, JS::Handle<JS::Value> aValue);
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
template <typename T>
|
2018-06-02 14:26:06 +03:00
|
|
|
void MaybeSomething(T&& aArgument, MaybeFunc aFunc) {
|
2016-03-23 18:02:57 +03:00
|
|
|
MOZ_ASSERT(PromiseObj()); // It was preserved!
|
|
|
|
|
2020-09-28 09:29:44 +03:00
|
|
|
AutoAllowLegacyScriptExecution exemption;
|
2016-07-08 03:08:25 +03:00
|
|
|
AutoEntryScript aes(mGlobal, "Promise resolution or rejection");
|
|
|
|
JSContext* cx = aes.cx();
|
2014-02-26 01:34:55 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2018-06-02 14:26:06 +03:00
|
|
|
if (!ToJSValue(cx, std::forward<T>(aArgument), &val)) {
|
2014-02-26 01:34:55 +04:00
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(this->*aFunc)(cx, val);
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
void HandleException(JSContext* aCx);
|
2014-10-20 06:27:12 +04:00
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
bool MaybePropagateUserInputEventHandling();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIGlobalObject> mGlobal;
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Heap<JSObject*> mPromiseObj;
|
2013-06-12 05:41:21 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2020-12-12 05:55:49 +03:00
|
|
|
extern "C" {
|
|
|
|
// These functions are used in the implementation of ffi bindings for
|
|
|
|
// dom::Promise from Rust in xpcom crate.
|
|
|
|
void DomPromise_AddRef(mozilla::dom::Promise* aPromise);
|
|
|
|
void DomPromise_Release(mozilla::dom::Promise* aPromise);
|
|
|
|
void DomPromise_RejectWithVariant(mozilla::dom::Promise* aPromise,
|
|
|
|
nsIVariant* aVariant);
|
|
|
|
void DomPromise_ResolveWithVariant(mozilla::dom::Promise* aPromise,
|
|
|
|
nsIVariant* aVariant);
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#endif // mozilla_dom_Promise_h
|