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/. */
6
7 #include "mozilla/dom/MediaError.h"
8
9 #include <string>
10 #include <unordered_set>
11
12 #include "mozilla/dom/MediaErrorBinding.h"
13 #include "nsContentUtils.h"
14 #include "nsIScriptError.h"
15 #include "jsapi.h"
16 #include "js/Warnings.h" // JS::WarnASCII
17
18 namespace mozilla {
19 namespace dom {
20
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaError,mParent)21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaError, mParent)
22 NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaError)
23 NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaError)
24
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaError)
26 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
27 NS_INTERFACE_MAP_ENTRY(nsISupports)
28 NS_INTERFACE_MAP_END
29
30 MediaError::MediaError(HTMLMediaElement* aParent, uint16_t aCode,
31 const nsACString& aMessage)
32 : mParent(aParent), mCode(aCode), mMessage(aMessage) {}
33
GetMessage(nsAString & aResult) const34 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 static const std::unordered_set<std::string> whitelist = {
38 "404: Not Found"
39 // TODO
40 };
41
42 bool shouldBlank = (whitelist.find(mMessage.get()) == whitelist.end());
43
44 if (shouldBlank) {
45 // Print a warning message to JavaScript console to alert developers of
46 // a non-whitelisted error message.
47 nsAutoCString message =
48 NS_LITERAL_CSTRING(
49 "This error message will be blank when "
50 "privacy.resistFingerprinting = true."
51 " If it is really necessary, please add it to the whitelist in"
52 " MediaError::GetMessage: ") +
53 mMessage;
54 Document* ownerDoc = mParent->OwnerDoc();
55 AutoJSAPI api;
56 if (api.Init(ownerDoc->GetScopeObject())) {
57 // We prefer this API because it can also print to our debug log and
58 // try server's log viewer.
59 JS::WarnASCII(api.cx(), "%s", message.get());
60 } else {
61 // If failed to use JS::WarnASCII, fall back to
62 // nsContentUtils::ReportToConsoleNonLocalized, which can only print to
63 // JavaScript console.
64 nsContentUtils::ReportToConsoleNonLocalized(
65 NS_ConvertASCIItoUTF16(message), nsIScriptError::warningFlag,
66 NS_LITERAL_CSTRING("MediaError"), ownerDoc);
67 }
68 }
69
70 if (!nsContentUtils::IsCallerChrome() &&
71 nsContentUtils::ShouldResistFingerprinting(mParent->OwnerDoc()) &&
72 shouldBlank) {
73 aResult.Truncate();
74 return;
75 }
76
77 CopyUTF8toUTF16(mMessage, aResult);
78 }
79
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)80 JSObject* MediaError::WrapObject(JSContext* aCx,
81 JS::Handle<JSObject*> aGivenProto) {
82 return MediaError_Binding::Wrap(aCx, this, aGivenProto);
83 }
84
85 } // namespace dom
86 } // namespace mozilla
87