1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "mozilla/dom/MediaError.h"
10 #include <unordered_set>
12 #include "mozilla/dom/Document.h"
13 #include "mozilla/dom/MediaErrorBinding.h"
14 #include "nsContentUtils.h"
15 #include "nsIScriptError.h"
17 #include "js/Warnings.h" // JS::WarnASCII
19 namespace mozilla::dom
{
21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaError
, mParent
)
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaError
)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaError
)
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaError
)
26 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
27 NS_INTERFACE_MAP_ENTRY(nsISupports
)
30 MediaError::MediaError(HTMLMediaElement
* aParent
, uint16_t aCode
,
31 const nsACString
& aMessage
)
32 : mParent(aParent
), mCode(aCode
), mMessage(aMessage
) {}
34 void MediaError::GetMessage(nsAString
& aResult
) const {
35 // When fingerprinting resistance is enabled, only messages in this list
36 // can be returned to content script.
37 // FIXME: An unordered_set seems overkill for this.
38 static const std::unordered_set
<std::string
> whitelist
= {
43 const bool shouldBlank
= whitelist
.find(mMessage
.get()) == whitelist
.end();
46 // Print a warning message to JavaScript console to alert developers of
47 // a non-whitelisted error message.
48 nsAutoCString message
=
50 "This error message will be blank when "
51 "privacy.resistFingerprinting = true."
52 " If it is really necessary, please add it to the whitelist in"
53 " MediaError::GetMessage: ") +
55 Document
* ownerDoc
= mParent
->OwnerDoc();
57 if (api
.Init(ownerDoc
->GetScopeObject())) {
58 // We prefer this API because it can also print to our debug log and
59 // try server's log viewer.
60 JS::WarnASCII(api
.cx(), "%s", message
.get());
62 // If failed to use JS::WarnASCII, fall back to
63 // nsContentUtils::ReportToConsoleNonLocalized, which can only print to
64 // JavaScript console.
65 nsContentUtils::ReportToConsoleNonLocalized(
66 NS_ConvertASCIItoUTF16(message
), nsIScriptError::warningFlag
,
67 "MediaError"_ns
, ownerDoc
);
70 if (!nsContentUtils::IsCallerChrome() &&
71 ownerDoc
->ShouldResistFingerprinting(RFPTarget::MediaError
)) {
77 CopyUTF8toUTF16(mMessage
, aResult
);
80 JSObject
* MediaError::WrapObject(JSContext
* aCx
,
81 JS::Handle
<JSObject
*> aGivenProto
) {
82 return MediaError_Binding::Wrap(aCx
, this, aGivenProto
);
85 } // namespace mozilla::dom