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