1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "MediaStreamError.h"
8 #include "mozilla/dom/MediaStreamErrorBinding.h"
9 #include "mozilla/dom/Promise.h"
10 #include "nsContentUtils.h"
14 BaseMediaMgrError::BaseMediaMgrError(Name aName
, const nsACString
& aMessage
,
15 const nsAString
& aConstraint
)
16 : mMessage(aMessage
), mConstraint(aConstraint
), mName(aName
) {
17 #define MAP_MEDIAERR(name, msg) \
18 { Name::name, #name, msg }
22 const char* mNameString
;
25 MAP_MEDIAERR(AbortError
, "The operation was aborted."),
26 MAP_MEDIAERR(InvalidStateError
, "The object is in an invalid state."),
27 MAP_MEDIAERR(NotAllowedError
,
28 "The request is not allowed by the user agent "
29 "or the platform in the current context."),
30 MAP_MEDIAERR(NotFoundError
, "The object can not be found here."),
31 MAP_MEDIAERR(NotReadableError
, "The I/O read operation failed."),
32 MAP_MEDIAERR(OverconstrainedError
, "Constraints could be not satisfied."),
33 MAP_MEDIAERR(SecurityError
, "The operation is insecure."),
34 MAP_MEDIAERR(TypeError
, ""),
36 for (auto& entry
: map
) {
37 if (entry
.mName
== mName
) {
38 mNameString
.AssignASCII(entry
.mNameString
);
39 if (mMessage
.IsEmpty()) {
40 mMessage
.AssignASCII(entry
.mMessage
);
45 MOZ_ASSERT_UNREACHABLE("Unknown error type");
48 NS_IMPL_ISUPPORTS0(MediaMgrError
)
50 void MediaMgrError::Reject(dom::Promise
* aPromise
) const {
52 case Name::AbortError
:
53 aPromise
->MaybeRejectWithAbortError(mMessage
);
55 case Name::InvalidStateError
:
56 aPromise
->MaybeRejectWithInvalidStateError(mMessage
);
58 case Name::NotAllowedError
:
59 aPromise
->MaybeRejectWithNotAllowedError(mMessage
);
61 case Name::NotFoundError
:
62 aPromise
->MaybeRejectWithNotFoundError(mMessage
);
64 case Name::NotReadableError
:
65 aPromise
->MaybeRejectWithNotReadableError(mMessage
);
67 case Name::OverconstrainedError
: {
68 // TODO: Add OverconstrainedError type.
69 // https://bugzilla.mozilla.org/show_bug.cgi?id=1453013
70 nsCOMPtr
<nsPIDOMWindowInner
> window
=
71 do_QueryInterface(aPromise
->GetGlobalObject());
72 aPromise
->MaybeReject(MakeRefPtr
<dom::MediaStreamError
>(window
, *this));
75 case Name::SecurityError
:
76 aPromise
->MaybeRejectWithSecurityError(mMessage
);
79 aPromise
->MaybeRejectWithTypeError(mMessage
);
81 // -Wswitch ensures all cases are covered so don't add default:.
87 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaStreamError
, mParent
)
88 NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaStreamError
)
89 NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaStreamError
)
90 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaStreamError
)
91 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
92 NS_INTERFACE_MAP_ENTRY(nsISupports
)
93 NS_INTERFACE_MAP_ENTRY(MediaStreamError
)
96 JSObject
* MediaStreamError::WrapObject(JSContext
* aCx
,
97 JS::Handle
<JSObject
*> aGivenProto
) {
98 return MediaStreamError_Binding::Wrap(aCx
, this, aGivenProto
);
101 void MediaStreamError::GetName(nsAString
& aName
) const { aName
= mNameString
; }
103 void MediaStreamError::GetMessage(nsAString
& aMessage
) const {
104 CopyUTF8toUTF16(mMessage
, aMessage
);
107 void MediaStreamError::GetConstraint(nsAString
& aConstraint
) const {
108 aConstraint
= mConstraint
;
112 } // namespace mozilla