Bug Summary

File:root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp
Warning:line 915, column 5
Value stored to 'rv' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name PrototypeDocumentContentSink.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/dom/prototype -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/dom/prototype -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D MOZ_SUPPORT_LEAKCHECKING -D STATIC_EXPORTABLE_JS_API -I /root/firefox-clang/dom/prototype -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dom/prototype -I /root/firefox-clang/dom/base -I /root/firefox-clang/dom/xul -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c++ /root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp
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 https://mozilla.org/MPL/2.0/. */
6
7#include "nsCOMPtr.h"
8#include "mozilla/dom/PrototypeDocumentContentSink.h"
9#include "nsIParser.h"
10#include "mozilla/dom/Document.h"
11#include "nsIContent.h"
12#include "nsIURI.h"
13#include "nsNetUtil.h"
14#include "nsHTMLParts.h"
15#include "nsCRT.h"
16#include "mozilla/StyleSheetInlines.h"
17#include "mozilla/css/Loader.h"
18#include "nsGkAtoms.h"
19#include "nsContentUtils.h"
20#include "nsDocElementCreatedNotificationRunner.h"
21#include "nsIScriptContext.h"
22#include "nsNameSpaceManager.h"
23#include "nsIScriptError.h"
24#include "prtime.h"
25#include "mozilla/Logging.h"
26#include "nsRect.h"
27#include "nsIScriptElement.h"
28#include "nsReadableUtils.h"
29#include "nsUnicharUtils.h"
30#include "nsIChannel.h"
31#include "nsNodeInfoManager.h"
32#include "nsContentCreatorFunctions.h"
33#include "nsIContentPolicy.h"
34#include "nsContentPolicyUtils.h"
35#include "nsError.h"
36#include "nsIScriptGlobalObject.h"
37#include "mozAutoDocUpdate.h"
38#include "nsMimeTypes.h"
39#include "nsHtml5SVGLoadDispatcher.h"
40#include "nsTextNode.h"
41#include "mozilla/dom/AutoEntryScript.h"
42#include "mozilla/dom/CDATASection.h"
43#include "mozilla/dom/Comment.h"
44#include "mozilla/dom/DocumentType.h"
45#include "mozilla/dom/Element.h"
46#include "mozilla/dom/HTMLTemplateElement.h"
47#include "mozilla/dom/ProcessingInstruction.h"
48#include "mozilla/dom/XMLStylesheetProcessingInstruction.h"
49#include "mozilla/dom/ScriptLoader.h"
50#include "mozilla/dom/nsCSPUtils.h"
51#include "mozilla/LoadInfo.h"
52#include "mozilla/PresShell.h"
53#include "mozilla/ProfilerLabels.h"
54#include "mozilla/RefPtr.h"
55#include "mozilla/Try.h"
56
57#include "nsXULPrototypeCache.h"
58#include "nsXULElement.h"
59#include "mozilla/CycleCollectedJSContext.h"
60#include "js/CompilationAndEvaluation.h"
61#include "js/experimental/JSStencil.h"
62#include "js/Utility.h" // JS::FreePolicy
63
64using namespace mozilla;
65using namespace mozilla::dom;
66
67LazyLogModule PrototypeDocumentContentSink::gLog("PrototypeDocument");
68
69nsresult NS_NewPrototypeDocumentContentSink(nsIContentSink** aResult,
70 Document* aDoc, nsIURI* aURI,
71 nsISupports* aContainer,
72 nsIChannel* aChannel) {
73 MOZ_ASSERT(nullptr != aResult, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(nullptr != aResult)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(nullptr != aResult))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("nullptr != aResult"
" (" "null ptr" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 73); AnnotateMozCrashReason("MOZ_ASSERT" "(" "nullptr != aResult"
") (" "null ptr" ")"); do { MOZ_CrashSequence(__null, 73); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
74 if (nullptr == aResult) {
75 return NS_ERROR_NULL_POINTER;
76 }
77 RefPtr<PrototypeDocumentContentSink> it = new PrototypeDocumentContentSink();
78
79 nsresult rv = it->Init(aDoc, aURI, aContainer, aChannel);
80 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 80); return rv; } } while (false)
;
81
82 it.forget(aResult);
83 return NS_OK;
84}
85
86namespace mozilla::dom {
87
88PrototypeDocumentContentSink::PrototypeDocumentContentSink()
89 : mNextSrcLoadWaiter(nullptr),
90 mCurrentScriptProto(nullptr),
91 mOffThreadCompiling(false),
92 mStillWalking(false),
93 mPendingSheets(0) {}
94
95PrototypeDocumentContentSink::~PrototypeDocumentContentSink() {
96 NS_ASSERTION(do { if (!(mNextSrcLoadWaiter == nullptr)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "unreferenced document still waiting for script source to load?"
, "mNextSrcLoadWaiter == nullptr", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 98); MOZ_PretendNoReturn(); } } while (0)
97 mNextSrcLoadWaiter == nullptr,do { if (!(mNextSrcLoadWaiter == nullptr)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "unreferenced document still waiting for script source to load?"
, "mNextSrcLoadWaiter == nullptr", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 98); MOZ_PretendNoReturn(); } } while (0)
98 "unreferenced document still waiting for script source to load?")do { if (!(mNextSrcLoadWaiter == nullptr)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "unreferenced document still waiting for script source to load?"
, "mNextSrcLoadWaiter == nullptr", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 98); MOZ_PretendNoReturn(); } } while (0)
;
99}
100
101nsresult PrototypeDocumentContentSink::Init(Document* aDoc, nsIURI* aURI,
102 nsISupports* aContainer,
103 nsIChannel* aChannel) {
104 MOZ_ASSERT(aDoc, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aDoc)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(aDoc))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("aDoc" " (" "null ptr" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 104); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aDoc" ") (" "null ptr"
")"); do { MOZ_CrashSequence(__null, 104); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
105 MOZ_ASSERT(aURI, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aURI)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(aURI))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("aURI" " (" "null ptr" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 105); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aURI" ") (" "null ptr"
")"); do { MOZ_CrashSequence(__null, 105); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
106
107 mDocument = aDoc;
108
109 mDocument->SetDelayFrameLoaderInitialization(true);
110 mDocument->SetMayStartLayout(false);
111
112 // Get the URI. this should match the uri used for the OnNewURI call in
113 // nsDocShell::CreateDocumentViewer.
114 nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(mDocumentURI));
115 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 115); return rv; } } while (false)
;
116
117 mScriptLoader = mDocument->ScriptLoader();
118
119 return NS_OK;
120}
121
122NS_IMPL_CYCLE_COLLECTION(PrototypeDocumentContentSink, mParser, mDocumentURI,PrototypeDocumentContentSink::cycleCollection PrototypeDocumentContentSink
::_cycleCollectorGlobal; void PrototypeDocumentContentSink::cycleCollection
::Unlink(void* p) { PrototypeDocumentContentSink* tmp = DowncastCCParticipant
<PrototypeDocumentContentSink>(p); ImplCycleCollectionUnlink
(tmp->mParser); ImplCycleCollectionUnlink(tmp->mDocumentURI
); ImplCycleCollectionUnlink(tmp->mDocument); ImplCycleCollectionUnlink
(tmp->mScriptLoader); ImplCycleCollectionUnlink(tmp->mContextStack
); ImplCycleCollectionUnlink(tmp->mCurrentPrototype); (void
)tmp; } nsresult PrototypeDocumentContentSink::cycleCollection
::TraverseNative( void* p, nsCycleCollectionTraversalCallback
& cb) { PrototypeDocumentContentSink* tmp = DowncastCCParticipant
<PrototypeDocumentContentSink>(p); cb.DescribeRefCountedNode
(tmp->mRefCnt.get(), "PrototypeDocumentContentSink"); ImplCycleCollectionTraverse
(cb, tmp->mParser, "mParser", 0); ImplCycleCollectionTraverse
(cb, tmp->mDocumentURI, "mDocumentURI", 0); ImplCycleCollectionTraverse
(cb, tmp->mDocument, "mDocument", 0); ImplCycleCollectionTraverse
(cb, tmp->mScriptLoader, "mScriptLoader", 0); ImplCycleCollectionTraverse
(cb, tmp->mContextStack, "mContextStack", 0); ImplCycleCollectionTraverse
(cb, tmp->mCurrentPrototype, "mCurrentPrototype", 0); (void
)tmp; return NS_OK; }
123 mDocument, mScriptLoader, mContextStack,PrototypeDocumentContentSink::cycleCollection PrototypeDocumentContentSink
::_cycleCollectorGlobal; void PrototypeDocumentContentSink::cycleCollection
::Unlink(void* p) { PrototypeDocumentContentSink* tmp = DowncastCCParticipant
<PrototypeDocumentContentSink>(p); ImplCycleCollectionUnlink
(tmp->mParser); ImplCycleCollectionUnlink(tmp->mDocumentURI
); ImplCycleCollectionUnlink(tmp->mDocument); ImplCycleCollectionUnlink
(tmp->mScriptLoader); ImplCycleCollectionUnlink(tmp->mContextStack
); ImplCycleCollectionUnlink(tmp->mCurrentPrototype); (void
)tmp; } nsresult PrototypeDocumentContentSink::cycleCollection
::TraverseNative( void* p, nsCycleCollectionTraversalCallback
& cb) { PrototypeDocumentContentSink* tmp = DowncastCCParticipant
<PrototypeDocumentContentSink>(p); cb.DescribeRefCountedNode
(tmp->mRefCnt.get(), "PrototypeDocumentContentSink"); ImplCycleCollectionTraverse
(cb, tmp->mParser, "mParser", 0); ImplCycleCollectionTraverse
(cb, tmp->mDocumentURI, "mDocumentURI", 0); ImplCycleCollectionTraverse
(cb, tmp->mDocument, "mDocument", 0); ImplCycleCollectionTraverse
(cb, tmp->mScriptLoader, "mScriptLoader", 0); ImplCycleCollectionTraverse
(cb, tmp->mContextStack, "mContextStack", 0); ImplCycleCollectionTraverse
(cb, tmp->mCurrentPrototype, "mCurrentPrototype", 0); (void
)tmp; return NS_OK; }
124 mCurrentPrototype)PrototypeDocumentContentSink::cycleCollection PrototypeDocumentContentSink
::_cycleCollectorGlobal; void PrototypeDocumentContentSink::cycleCollection
::Unlink(void* p) { PrototypeDocumentContentSink* tmp = DowncastCCParticipant
<PrototypeDocumentContentSink>(p); ImplCycleCollectionUnlink
(tmp->mParser); ImplCycleCollectionUnlink(tmp->mDocumentURI
); ImplCycleCollectionUnlink(tmp->mDocument); ImplCycleCollectionUnlink
(tmp->mScriptLoader); ImplCycleCollectionUnlink(tmp->mContextStack
); ImplCycleCollectionUnlink(tmp->mCurrentPrototype); (void
)tmp; } nsresult PrototypeDocumentContentSink::cycleCollection
::TraverseNative( void* p, nsCycleCollectionTraversalCallback
& cb) { PrototypeDocumentContentSink* tmp = DowncastCCParticipant
<PrototypeDocumentContentSink>(p); cb.DescribeRefCountedNode
(tmp->mRefCnt.get(), "PrototypeDocumentContentSink"); ImplCycleCollectionTraverse
(cb, tmp->mParser, "mParser", 0); ImplCycleCollectionTraverse
(cb, tmp->mDocumentURI, "mDocumentURI", 0); ImplCycleCollectionTraverse
(cb, tmp->mDocument, "mDocument", 0); ImplCycleCollectionTraverse
(cb, tmp->mScriptLoader, "mScriptLoader", 0); ImplCycleCollectionTraverse
(cb, tmp->mContextStack, "mContextStack", 0); ImplCycleCollectionTraverse
(cb, tmp->mCurrentPrototype, "mCurrentPrototype", 0); (void
)tmp; return NS_OK; }
125
126NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PrototypeDocumentContentSink)nsresult PrototypeDocumentContentSink::QueryInterface(const nsIID
& aIID, void** aInstancePtr) { do { if (!(aInstancePtr)) {
NS_DebugBreak(NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 126); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface
; if (TopThreeWordsEquals( aIID, (nsXPCOMCycleCollectionParticipant
::kIID), (nsCycleCollectionISupports::kIID)) && (LowWordEquals
(aIID, (nsXPCOMCycleCollectionParticipant::kIID)) || LowWordEquals
(aIID, (nsCycleCollectionISupports::kIID)))) { if (LowWordEquals
(aIID, (nsXPCOMCycleCollectionParticipant::kIID))) { *aInstancePtr
= PrototypeDocumentContentSink::cycleCollection::GetParticipant
(); return NS_OK; } if (LowWordEquals(aIID, (nsCycleCollectionISupports
::kIID))) { *aInstancePtr = PrototypeDocumentContentSink::cycleCollection
::Upcast(this); return NS_OK; } foundInterface = nullptr; } else
127 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentSink)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsISupports>)) foundInterface = static_cast
<nsISupports*>(static_cast<nsIContentSink*>(this)
); else
128 NS_INTERFACE_MAP_ENTRY(nsIContentSink)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIContentSink>)) foundInterface =
static_cast<nsIContentSink*>(this); else
129 NS_INTERFACE_MAP_ENTRY(nsIStreamLoaderObserver)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIStreamLoaderObserver>)) foundInterface
= static_cast<nsIStreamLoaderObserver*>(this); else
130 NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsICSSLoaderObserver>)) foundInterface
= static_cast<nsICSSLoaderObserver*>(this); else
131 NS_INTERFACE_MAP_ENTRY(nsIOffThreadScriptReceiver)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIOffThreadScriptReceiver>)) foundInterface
= static_cast<nsIOffThreadScriptReceiver*>(this); else
132NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do
{ static_assert( mozilla::detail::AssertionConditionType<
decltype(!aIID.Equals((nsISupports::kIID)))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::kIID
))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("!aIID.Equals((nsISupports::kIID))", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 132); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::kIID))"
")"); do { MOZ_CrashSequence(__null, 132); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE
; } else { (foundInterface)->AddRef(); status = NS_OK; } *
aInstancePtr = foundInterface; return status; }
133
134NS_IMPL_CYCLE_COLLECTING_ADDREF(PrototypeDocumentContentSink)MozExternalRefCountType PrototypeDocumentContentSink::AddRef(
void) { static_assert(!std::is_destructible_v<PrototypeDocumentContentSink
>, "Reference-counted class " "PrototypeDocumentContentSink"
" should not have a public destructor. " "Make this class's destructor non-public"
); do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0"
" (" "illegal refcnt" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 134); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0"
") (" "illegal refcnt" ")"); do { MOZ_CrashSequence(__null, 134
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); _mOwningThread.AssertOwnership("PrototypeDocumentContentSink"
" not thread-safe"); nsISupports* base = PrototypeDocumentContentSink
::cycleCollection::Upcast(this); nsrefcnt count = mRefCnt.incr
(base); NS_LogAddRef((this), (count), ("PrototypeDocumentContentSink"
), (uint32_t)(sizeof(*this))); return count; }
135NS_IMPL_CYCLE_COLLECTING_RELEASE(PrototypeDocumentContentSink)MozExternalRefCountType PrototypeDocumentContentSink::Release
(void) { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) > 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) > 0))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0"
" (" "dup release" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 135); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0"
") (" "dup release" ")"); do { MOZ_CrashSequence(__null, 135
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); _mOwningThread.AssertOwnership("PrototypeDocumentContentSink"
" not thread-safe"); nsISupports* base = PrototypeDocumentContentSink
::cycleCollection::Upcast(this); nsrefcnt count = mRefCnt.decr
(base); if (count == 0) { NS_CycleCollectableHasRefCntZero();
} NS_LogRelease((this), (count), ("PrototypeDocumentContentSink"
)); return count; } void PrototypeDocumentContentSink::DeleteCycleCollectable
(void) { delete (this); }
136
137//----------------------------------------------------------------------
138//
139// nsIContentSink interface
140//
141
142void PrototypeDocumentContentSink::SetDocumentCharset(
143 NotNull<const Encoding*> aEncoding) {
144 if (mDocument) {
145 mDocument->SetDocumentCharacterSet(aEncoding);
146 }
147}
148
149nsISupports* PrototypeDocumentContentSink::GetTarget() {
150 return ToSupports(mDocument);
151}
152
153bool PrototypeDocumentContentSink::IsScriptExecuting() {
154 return !!mScriptLoader->GetCurrentScript();
155}
156
157NS_IMETHODIMPnsresult
158PrototypeDocumentContentSink::SetParser(nsParserBase* aParser) {
159 MOZ_ASSERT(aParser, "Should have a parser here!")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aParser)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aParser))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aParser" " (" "Should have a parser here!"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 159); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aParser" ") ("
"Should have a parser here!" ")"); do { MOZ_CrashSequence(__null
, 159); __attribute__((nomerge)) ::abort(); } while (false); }
} while (false)
;
160 mParser = aParser;
161 return NS_OK;
162}
163
164nsIParser* PrototypeDocumentContentSink::GetParser() {
165 return static_cast<nsIParser*>(mParser.get());
166}
167
168void PrototypeDocumentContentSink::ContinueInterruptedParsingIfEnabled() {
169 if (mParser && mParser->IsParserEnabled()) {
170 GetParser()->ContinueInterruptedParsing();
171 }
172}
173
174void PrototypeDocumentContentSink::ContinueInterruptedParsingAsync() {
175 nsCOMPtr<nsIRunnable> ev = NewRunnableMethod(
176 "PrototypeDocumentContentSink::ContinueInterruptedParsingIfEnabled", this,
177 &PrototypeDocumentContentSink::ContinueInterruptedParsingIfEnabled);
178 mDocument->Dispatch(ev.forget());
179}
180
181//----------------------------------------------------------------------
182//
183// PrototypeDocumentContentSink::ContextStack
184//
185
186PrototypeDocumentContentSink::ContextStack::ContextStack()
187 : mTop(nullptr), mDepth(0) {}
188
189PrototypeDocumentContentSink::ContextStack::~ContextStack() { Clear(); }
190
191void PrototypeDocumentContentSink::ContextStack::Traverse(
192 nsCycleCollectionTraversalCallback& aCallback, const char* aName,
193 uint32_t aFlags) {
194 aFlags |= CycleCollectionEdgeNameArrayFlag;
195 Entry* current = mTop;
196 while (current) {
197 CycleCollectionNoteChild(aCallback, current->mElement, aName, aFlags);
198 current = current->mNext;
199 }
200}
201
202void PrototypeDocumentContentSink::ContextStack::Clear() {
203 while (mTop) {
204 Entry* doomed = mTop;
205 mTop = mTop->mNext;
206 NS_IF_RELEASE(doomed->mElement)do { if (doomed->mElement) { (doomed->mElement)->Release
(); (doomed->mElement) = 0; } } while (0)
;
207 delete doomed;
208 }
209 mDepth = 0;
210}
211
212nsresult PrototypeDocumentContentSink::ContextStack::Push(
213 nsXULPrototypeElement* aPrototype, nsIContent* aElement) {
214 Entry* entry = new Entry;
215 entry->mPrototype = aPrototype;
216 entry->mElement = aElement;
217 NS_IF_ADDREF(entry->mElement)ns_if_addref(entry->mElement);
218 entry->mIndex = 0;
219
220 entry->mNext = mTop;
221 mTop = entry;
222
223 ++mDepth;
224 return NS_OK;
225}
226
227nsresult PrototypeDocumentContentSink::ContextStack::Pop() {
228 if (mDepth == 0) return NS_ERROR_UNEXPECTED;
229
230 Entry* doomed = mTop;
231 mTop = mTop->mNext;
232 --mDepth;
233
234 NS_IF_RELEASE(doomed->mElement)do { if (doomed->mElement) { (doomed->mElement)->Release
(); (doomed->mElement) = 0; } } while (0)
;
235 delete doomed;
236 return NS_OK;
237}
238
239nsresult PrototypeDocumentContentSink::ContextStack::Peek(
240 nsXULPrototypeElement** aPrototype, nsIContent** aElement,
241 int32_t* aIndex) {
242 if (mDepth == 0) return NS_ERROR_UNEXPECTED;
243
244 *aPrototype = mTop->mPrototype;
245 *aElement = mTop->mElement;
246 NS_IF_ADDREF(*aElement)ns_if_addref(*aElement);
247 *aIndex = mTop->mIndex;
248
249 return NS_OK;
250}
251
252nsresult PrototypeDocumentContentSink::ContextStack::SetTopIndex(
253 int32_t aIndex) {
254 if (mDepth == 0) return NS_ERROR_UNEXPECTED;
255
256 mTop->mIndex = aIndex;
257 return NS_OK;
258}
259
260//----------------------------------------------------------------------
261//
262// Content model walking routines
263//
264
265nsresult PrototypeDocumentContentSink::OnPrototypeLoadDone(
266 nsXULPrototypeDocument* aPrototype) {
267 mCurrentPrototype = aPrototype;
268 mDocument->SetPrototypeDocument(aPrototype);
269
270 nsresult rv = PrepareToWalk();
271 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 271); return rv; } } while (false)
;
272
273 rv = ResumeWalk();
274
275 return rv;
276}
277
278nsresult PrototypeDocumentContentSink::PrepareToWalk() {
279 MOZ_ASSERT(mCurrentPrototype)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mCurrentPrototype)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mCurrentPrototype))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("mCurrentPrototype"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 279); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mCurrentPrototype"
")"); do { MOZ_CrashSequence(__null, 279); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
280 nsresult rv;
281
282 mStillWalking = true;
283
284 // Notify document that the load is beginning
285 mDocument->BeginLoad();
286 MOZ_ASSERT(!mDocument->HasChildren())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mDocument->HasChildren())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mDocument->HasChildren()
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"!mDocument->HasChildren()", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 286); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mDocument->HasChildren()"
")"); do { MOZ_CrashSequence(__null, 286); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
287
288 // Get the prototype's root element and initialize the context
289 // stack for the prototype walk.
290 nsXULPrototypeElement* proto = mCurrentPrototype->GetRootElement();
291
292 if (!proto) {
293 if (MOZ_LOG_TEST(gLog, LogLevel::Error)(__builtin_expect(!!(mozilla::detail::log_test(gLog, LogLevel
::Error)), 0))
) {
294 nsCOMPtr<nsIURI> url = mCurrentPrototype->GetURI();
295
296 nsAutoCString urlspec;
297 rv = url->GetSpec(urlspec);
298 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
299
300 MOZ_LOG(gLog, LogLevel::Error,do { const ::mozilla::LogModule* moz_real_module = gLog; if (
(__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Error)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Error, "prototype: error parsing '%s'", urlspec.get
()); } } while (0)
301 ("prototype: error parsing '%s'", urlspec.get()))do { const ::mozilla::LogModule* moz_real_module = gLog; if (
(__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Error)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Error, "prototype: error parsing '%s'", urlspec.get
()); } } while (0)
;
302 }
303
304 return NS_OK;
305 }
306
307 const nsTArray<RefPtr<nsXULPrototypePI> >& processingInstructions =
308 mCurrentPrototype->GetProcessingInstructions();
309
310 uint32_t total = processingInstructions.Length();
311 for (uint32_t i = 0; i < total; ++i) {
312 rv = CreateAndInsertPI(processingInstructions[i], mDocument,
313 /* aInProlog */ true);
314 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
315 }
316
317 // Do one-time initialization.
318 RefPtr<Element> root;
319
320 // Add the root element
321 rv = CreateElementFromPrototype(proto, getter_AddRefs(root), nullptr);
322 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
323
324 ErrorResult error;
325 mDocument->AppendChildTo(root, false, error);
326 if (error.Failed()) {
327 return error.StealNSResult();
328 }
329
330 // TODO(emilio): Should this really notify? We don't notify of appends anyhow,
331 // and we just appended the root so no styles can possibly depend on it.
332 mDocument->UpdateDocumentStates(DocumentState::RTL_LOCALE, true);
333
334 nsContentUtils::AddScriptRunner(
335 new nsDocElementCreatedNotificationRunner(mDocument));
336
337 // There'd better not be anything on the context stack at this
338 // point! This is the basis case for our "induction" in
339 // ResumeWalk(), below, which'll assume that there's always a
340 // content element on the context stack if we're in the document.
341 NS_ASSERTION(mContextStack.Depth() == 0,do { if (!(mContextStack.Depth() == 0)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "something's on the context stack already", "mContextStack.Depth() == 0"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 342); MOZ_PretendNoReturn(); } } while (0)
342 "something's on the context stack already")do { if (!(mContextStack.Depth() == 0)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "something's on the context stack already", "mContextStack.Depth() == 0"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 342); MOZ_PretendNoReturn(); } } while (0)
;
343 if (mContextStack.Depth() != 0) return NS_ERROR_UNEXPECTED;
344
345 rv = mContextStack.Push(proto, root);
346 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
347
348 return NS_OK;
349}
350
351nsresult PrototypeDocumentContentSink::CreateAndInsertPI(
352 const nsXULPrototypePI* aProtoPI, nsINode* aParent, bool aInProlog) {
353 MOZ_ASSERT(aProtoPI, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aProtoPI)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aProtoPI))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aProtoPI" " (" "null ptr"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 353); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aProtoPI" ") ("
"null ptr" ")"); do { MOZ_CrashSequence(__null, 353); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
354 MOZ_ASSERT(aParent, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aParent)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aParent))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("aParent" " (" "null ptr"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 354); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aParent" ") ("
"null ptr" ")"); do { MOZ_CrashSequence(__null, 354); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
355
356 RefPtr<ProcessingInstruction> node =
357 NS_NewXMLProcessingInstruction(aParent->OwnerDoc()->NodeInfoManager(),
358 aProtoPI->mTarget, aProtoPI->mData);
359
360 nsresult rv;
361 if (aProtoPI->mTarget.EqualsLiteral("xml-stylesheet")) {
362 MOZ_ASSERT(LinkStyle::FromNode(*node),do { static_assert( mozilla::detail::AssertionConditionType<
decltype(LinkStyle::FromNode(*node))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(LinkStyle::FromNode(*node)))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("LinkStyle::FromNode(*node)"
" (" "XML Stylesheet node does not implement LinkStyle!" ")"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 363); AnnotateMozCrashReason("MOZ_ASSERT" "(" "LinkStyle::FromNode(*node)"
") (" "XML Stylesheet node does not implement LinkStyle!" ")"
); do { MOZ_CrashSequence(__null, 363); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
363 "XML Stylesheet node does not implement LinkStyle!")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(LinkStyle::FromNode(*node))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(LinkStyle::FromNode(*node)))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("LinkStyle::FromNode(*node)"
" (" "XML Stylesheet node does not implement LinkStyle!" ")"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 363); AnnotateMozCrashReason("MOZ_ASSERT" "(" "LinkStyle::FromNode(*node)"
") (" "XML Stylesheet node does not implement LinkStyle!" ")"
); do { MOZ_CrashSequence(__null, 363); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
364 auto* pi = static_cast<XMLStylesheetProcessingInstruction*>(node.get());
365 rv = InsertXMLStylesheetPI(aProtoPI, aParent, pi);
366 } else {
367 // Handles the special <?csp ?> PI, which will be handled before
368 // creating any element with potential inline style or scripts.
369 if (aInProlog && aProtoPI->mTarget.EqualsLiteral("csp")) {
370 CSP_ApplyMetaCSPToDoc(*aParent->OwnerDoc(), aProtoPI->mData);
371 }
372
373 // No special processing, just add the PI to the document.
374 ErrorResult error;
375 aParent->AppendChildTo(node->AsContent(), false, error);
376 rv = error.StealNSResult();
377 }
378
379 return rv;
380}
381
382nsresult PrototypeDocumentContentSink::InsertXMLStylesheetPI(
383 const nsXULPrototypePI* aProtoPI, nsINode* aParent,
384 XMLStylesheetProcessingInstruction* aPINode) {
385 // We want to be notified when the style sheet finishes loading, so
386 // disable style sheet loading for now.
387 aPINode->DisableUpdates();
388 aPINode->OverrideBaseURI(mCurrentPrototype->GetURI());
389
390 ErrorResult rv;
391 aParent->AppendChildTo(aPINode, false, rv);
392 if (rv.Failed()) {
393 return rv.StealNSResult();
394 }
395
396 // load the stylesheet if necessary, passing ourselves as
397 // nsICSSObserver
398 auto result = aPINode->EnableUpdatesAndUpdateStyleSheet(this);
399 if (result.isErr()) {
400 // Ignore errors from UpdateStyleSheet; we don't want failure to
401 // do that to break the XUL document load. But do propagate out
402 // NS_ERROR_OUT_OF_MEMORY.
403 if (result.unwrapErr() == NS_ERROR_OUT_OF_MEMORY) {
404 return result.unwrapErr();
405 }
406 return NS_OK;
407 }
408
409 auto update = result.unwrap();
410 if (update.ShouldBlock()) {
411 ++mPendingSheets;
412 }
413
414 return NS_OK;
415}
416
417void PrototypeDocumentContentSink::CloseElement(Element* aElement,
418 bool aHadChildren) {
419 if (nsIContent::RequiresDoneAddingChildren(
420 aElement->NodeInfo()->NamespaceID(),
421 aElement->NodeInfo()->NameAtom())) {
422 aElement->DoneAddingChildren(false);
423 }
424
425 if (auto* linkStyle = LinkStyle::FromNode(*aElement)) {
426 auto result = linkStyle->EnableUpdatesAndUpdateStyleSheet(this);
427 if (result.isOk() && result.unwrap().ShouldBlock()) {
428 ++mPendingSheets;
429 }
430 return;
431 }
432
433 if (!aHadChildren) {
434 return;
435 }
436
437 // See bug 370111 and bug 1495946. We don't cache inline styles nor module
438 // scripts in the prototype cache, and we don't notify on node insertion, so
439 // we need to do this for the stylesheet / script to be properly processed.
440 // This kinda sucks, but notifying was a pretty sizeable perf regression so...
441 if (aElement->IsHTMLElement(nsGkAtoms::script) ||
442 aElement->IsSVGElement(nsGkAtoms::script)) {
443 nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(aElement);
444 MOZ_ASSERT(sele, "Node didn't QI to script.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(sele)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(sele))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("sele" " (" "Node didn't QI to script."
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 444); AnnotateMozCrashReason("MOZ_ASSERT" "(" "sele" ") (" "Node didn't QI to script."
")"); do { MOZ_CrashSequence(__null, 444); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
445 if (sele->GetScriptIsModule()) {
446 DebugOnly<bool> block = sele->AttemptToExecute();
447 MOZ_ASSERT(!block, "<script type=module> shouldn't block the parser")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!block)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(!block))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("!block" " (" "<script type=module> shouldn't block the parser"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 447); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!block" ") ("
"<script type=module> shouldn't block the parser" ")")
; do { MOZ_CrashSequence(__null, 447); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
448 }
449 }
450}
451
452nsresult PrototypeDocumentContentSink::ResumeWalk() {
453 nsresult rv = ResumeWalkInternal();
454 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
455 nsContentUtils::ReportToConsoleNonLocalized(
456 u"Failed to load document from prototype document."_ns,
457 nsIScriptError::errorFlag, "Prototype Document"_ns, mDocument,
458 SourceLocation{mDocumentURI.get()});
459 }
460 return rv;
461}
462
463nsresult PrototypeDocumentContentSink::ResumeWalkInternal() {
464 MOZ_ASSERT(mStillWalking)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mStillWalking)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mStillWalking))), 0))) { do {
} while (false); MOZ_ReportAssertionFailure("mStillWalking",
"/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 464); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mStillWalking"
")"); do { MOZ_CrashSequence(__null, 464); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
465 // Walk the prototype and build the delegate content model. The
466 // walk is performed in a top-down, left-to-right fashion. That
467 // is, a parent is built before any of its children; a node is
468 // only built after all of its siblings to the left are fully
469 // constructed.
470 //
471 // It is interruptable so that transcluded documents (e.g.,
472 // <html:script src="..." />) can be properly re-loaded if the
473 // cached copy of the document becomes stale.
474 nsresult rv;
475 nsCOMPtr<nsIURI> docURI =
476 mCurrentPrototype ? mCurrentPrototype->GetURI() : nullptr;
477
478 while (true) {
479 // Begin (or resume) walking the current prototype.
480
481 while (mContextStack.Depth() > 0) {
482 // Look at the top of the stack to determine what we're
483 // currently working on.
484 // This will always be a node already constructed and
485 // inserted to the actual document.
486 nsXULPrototypeElement* proto;
487 nsCOMPtr<nsIContent> element;
488 nsCOMPtr<nsIContent> nodeToPushTo;
489 int32_t indx; // all children of proto before indx (not
490 // inclusive) have already been constructed
491 rv = mContextStack.Peek(&proto, getter_AddRefs(element), &indx);
492 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
493
494 if (indx >= (int32_t)proto->mChildren.Length()) {
495 if (element) {
496 // We've processed all of the prototype's children.
497 CloseElement(element->AsElement(), /* aHadChildren = */ true);
498 }
499 // Now pop the context stack back up to the parent
500 // element and continue the prototype walk.
501 mContextStack.Pop();
502 continue;
503 }
504
505 nodeToPushTo = element;
506 // For template elements append the content to the template's document
507 // fragment.
508 if (auto* templateElement = HTMLTemplateElement::FromNode(element)) {
509 nodeToPushTo = templateElement->Content();
510 }
511
512 // Grab the next child, and advance the current context stack
513 // to the next sibling to our right.
514 nsXULPrototypeNode* childproto = proto->mChildren[indx];
515 mContextStack.SetTopIndex(++indx);
516
517 switch (childproto->mType) {
518 case nsXULPrototypeNode::eType_Element: {
519 // An 'element', which may contain more content.
520 auto* protoele = static_cast<nsXULPrototypeElement*>(childproto);
521
522 RefPtr<Element> child;
523 MOZ_TRY(CreateElementFromPrototype(protoele, getter_AddRefs(child),__extension__({ auto mozTryVarTempResult = ::mozilla::ToResult
(CreateElementFromPrototype(protoele, getter_AddRefs(child), nodeToPushTo
)); if ((__builtin_expect(!!(mozTryVarTempResult.isErr()), 0)
)) { return mozTryVarTempResult.propagateErr(); } mozTryVarTempResult
.unwrap(); })
524 nodeToPushTo))__extension__({ auto mozTryVarTempResult = ::mozilla::ToResult
(CreateElementFromPrototype(protoele, getter_AddRefs(child), nodeToPushTo
)); if ((__builtin_expect(!!(mozTryVarTempResult.isErr()), 0)
)) { return mozTryVarTempResult.propagateErr(); } mozTryVarTempResult
.unwrap(); })
;
525
526 if (auto* linkStyle = LinkStyle::FromNode(*child)) {
527 linkStyle->DisableUpdates();
528 }
529
530 // ...and append it to the content model.
531 ErrorResult error;
532 nodeToPushTo->AppendChildTo(child, false, error);
533 if (error.Failed()) {
534 return error.StealNSResult();
535 }
536
537 if (nsIContent::RequiresDoneCreatingElement(
538 protoele->mNodeInfo->NamespaceID(),
539 protoele->mNodeInfo->NameAtom())) {
540 child->DoneCreatingElement();
541 }
542
543 // If it has children, push the element onto the context
544 // stack and begin to process them.
545 if (protoele->mChildren.Length() > 0) {
546 rv = mContextStack.Push(protoele, child);
547 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
548 } else {
549 // If there are no children, close the element immediately.
550 CloseElement(child, /* aHadChildren = */ false);
551 }
552 } break;
553
554 case nsXULPrototypeNode::eType_Script: {
555 // A script reference. Execute the script immediately;
556 // this may have side effects in the content model.
557 auto* scriptproto = static_cast<nsXULPrototypeScript*>(childproto);
558 if (scriptproto->mSrcURI) {
559 // A transcluded script reference; this may
560 // "block" our prototype walk if the script isn't
561 // cached, or the cached copy of the script is
562 // stale and must be reloaded.
563 bool blocked;
564 rv = LoadScript(scriptproto, &blocked);
565 // If the script cannot be loaded, just keep going!
566
567 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && blocked) return NS_OK;
568 } else if (scriptproto->HasStencil()) {
569 // An inline script
570 rv = ExecuteScript(scriptproto);
571 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
572 }
573 } break;
574
575 case nsXULPrototypeNode::eType_Text: {
576 nsNodeInfoManager* nim = nodeToPushTo->NodeInfo()->NodeInfoManager();
577 // A simple text node.
578 RefPtr<nsTextNode> text = new (nim) nsTextNode(nim);
579
580 auto* textproto = static_cast<nsXULPrototypeText*>(childproto);
581 text->SetText(textproto->mValue, false);
582
583 ErrorResult error;
584 nodeToPushTo->AppendChildTo(text, false, error);
585 if (error.Failed()) {
586 return error.StealNSResult();
587 }
588 } break;
589
590 case nsXULPrototypeNode::eType_PI: {
591 auto* piProto = static_cast<nsXULPrototypePI*>(childproto);
592
593 // <?xml-stylesheet?> and <?csp?> don't have an effect
594 // outside the prolog, issue a warning.
595
596 if (piProto->mTarget.EqualsLiteral("xml-stylesheet") ||
597 piProto->mTarget.EqualsLiteral("csp")) {
598 AutoTArray<nsString, 1> params = {piProto->mTarget};
599
600 nsContentUtils::ReportToConsole(
601 nsIScriptError::warningFlag, "XUL Document"_ns, nullptr,
602 nsContentUtils::eXUL_PROPERTIES, "PINotInProlog2", params,
603 SourceLocation(docURI.get()));
604 }
605
606 if (nsIContent* parent = element.get()) {
607 // an inline script could have removed the root element
608 rv = CreateAndInsertPI(piProto, parent, /* aInProlog */ false);
609 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 609); return rv; } } while (false)
;
610 }
611 } break;
612
613 default:
614 MOZ_ASSERT_UNREACHABLE("Unexpected nsXULPrototypeNode::Type")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"Unexpected nsXULPrototypeNode::Type" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 614); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "Unexpected nsXULPrototypeNode::Type"
")"); do { MOZ_CrashSequence(__null, 614); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
615 }
616 }
617
618 // Once we get here, the context stack will have been
619 // depleted. That means that the entire prototype has been
620 // walked and content has been constructed.
621 break;
622 }
623
624 mStillWalking = false;
625 return MaybeDoneWalking();
626}
627
628void PrototypeDocumentContentSink::InitialTranslationCompleted() {
629 MaybeDoneWalking();
630}
631
632nsresult PrototypeDocumentContentSink::MaybeDoneWalking() {
633 if (mPendingSheets > 0 || mStillWalking) {
634 return NS_OK;
635 }
636
637 if (mDocument->HasPendingInitialTranslation()) {
638 mDocument->OnParsingCompleted();
639 return NS_OK;
640 }
641
642 return DoneWalking();
643}
644
645nsresult PrototypeDocumentContentSink::DoneWalking() {
646 MOZ_ASSERT(mPendingSheets == 0, "there are sheets to be loaded")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mPendingSheets == 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mPendingSheets == 0))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("mPendingSheets == 0"
" (" "there are sheets to be loaded" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 646); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mPendingSheets == 0"
") (" "there are sheets to be loaded" ")"); do { MOZ_CrashSequence
(__null, 646); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
;
647 MOZ_ASSERT(!mStillWalking, "walk not done")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mStillWalking)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mStillWalking))), 0))) { do
{ } while (false); MOZ_ReportAssertionFailure("!mStillWalking"
" (" "walk not done" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 647); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mStillWalking"
") (" "walk not done" ")"); do { MOZ_CrashSequence(__null, 647
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
;
648 MOZ_ASSERT(!mDocument->HasPendingInitialTranslation(), "translation pending")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mDocument->HasPendingInitialTranslation())>::
isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(!mDocument->HasPendingInitialTranslation()))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("!mDocument->HasPendingInitialTranslation()"
" (" "translation pending" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 648); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mDocument->HasPendingInitialTranslation()"
") (" "translation pending" ")"); do { MOZ_CrashSequence(__null
, 648); __attribute__((nomerge)) ::abort(); } while (false); }
} while (false)
;
649
650 if (mDocument) {
651 MOZ_ASSERT(mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING
)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING"
" (" "Bad readyState" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 652); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING"
") (" "Bad readyState" ")"); do { MOZ_CrashSequence(__null, 652
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
652 "Bad readyState")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING
)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING"
" (" "Bad readyState" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 652); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mDocument->GetReadyStateEnum() == Document::READYSTATE_LOADING"
") (" "Bad readyState" ")"); do { MOZ_CrashSequence(__null, 652
); __attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
;
653 mDocument->SetReadyStateInternal(Document::READYSTATE_INTERACTIVE);
654 mDocument->NotifyPossibleTitleChange(false);
655
656 nsContentUtils::DispatchEventOnlyToChrome(mDocument, mDocument,
657 u"MozBeforeInitialXULLayout"_ns,
658 CanBubble::eYes, Cancelable::eNo);
659 }
660
661 if (mScriptLoader) {
662 mScriptLoader->ParsingComplete(false);
663 mScriptLoader->DeferCheckpointReached();
664 }
665
666 StartLayout();
667
668 if (mDocumentURI->SchemeIs("chrome") &&
669 nsXULPrototypeCache::GetInstance()->IsEnabled()) {
670 bool isCachedOnDisk;
671 nsXULPrototypeCache::GetInstance()->HasPrototype(mDocumentURI,
672 &isCachedOnDisk);
673 if (!isCachedOnDisk) {
674 if (!mDocument->GetDocumentElement() ||
675 (mDocument->GetDocumentElement()->NodeInfo()->Equals(
676 nsGkAtoms::parsererror) &&
677 mDocument->GetDocumentElement()->NodeInfo()->NamespaceEquals(
678 nsDependentAtomString(nsGkAtoms::nsuri_parsererror)))) {
679 nsXULPrototypeCache::GetInstance()->RemovePrototype(mDocumentURI);
680 } else {
681 nsXULPrototypeCache::GetInstance()->WritePrototype(mCurrentPrototype);
682 }
683 }
684 }
685
686 mDocument->SetDelayFrameLoaderInitialization(false);
687 RefPtr<Document> doc = mDocument;
688 doc->MaybeInitializeFinalizeFrameLoaders();
689
690 // If the document we are loading has a reference or it is a
691 // frameset document, disable the scroll bars on the views.
692
693 doc->SetScrollToRef(mDocument->GetDocumentURI());
694
695 doc->EndLoad();
696
697 return NS_OK;
698}
699
700void PrototypeDocumentContentSink::StartLayout() {
701 AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(mozilla::Maybe<nsAutoCString> autoCStr; mozilla::Maybe<
mozilla::AutoProfilerLabel> raiiObjectNsCString; if (profiler_is_active
()) { autoCStr.emplace(mDocumentURI->GetSpecOrDefault()); raiiObjectNsCString
.emplace("PrototypeDocumentContentSink::StartLayout", autoCStr
->get(), JS::ProfilingCategoryPair::LAYOUT); }
702 "PrototypeDocumentContentSink::StartLayout", LAYOUT,mozilla::Maybe<nsAutoCString> autoCStr; mozilla::Maybe<
mozilla::AutoProfilerLabel> raiiObjectNsCString; if (profiler_is_active
()) { autoCStr.emplace(mDocumentURI->GetSpecOrDefault()); raiiObjectNsCString
.emplace("PrototypeDocumentContentSink::StartLayout", autoCStr
->get(), JS::ProfilingCategoryPair::LAYOUT); }
703 mDocumentURI->GetSpecOrDefault())mozilla::Maybe<nsAutoCString> autoCStr; mozilla::Maybe<
mozilla::AutoProfilerLabel> raiiObjectNsCString; if (profiler_is_active
()) { autoCStr.emplace(mDocumentURI->GetSpecOrDefault()); raiiObjectNsCString
.emplace("PrototypeDocumentContentSink::StartLayout", autoCStr
->get(), JS::ProfilingCategoryPair::LAYOUT); }
;
704 mDocument->SetMayStartLayout(true);
705 RefPtr<PresShell> presShell = mDocument->GetPresShell();
706 if (presShell && !presShell->DidInitialize()) {
707 nsresult rv = presShell->Initialize();
708 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
709 return;
710 }
711 }
712}
713
714NS_IMETHODIMPnsresult
715PrototypeDocumentContentSink::StyleSheetLoaded(StyleSheet* aSheet,
716 bool aWasDeferred,
717 nsresult aStatus) {
718 if (!aWasDeferred) {
719 // Don't care about when alternate sheets finish loading
720 MOZ_ASSERT(mPendingSheets > 0, "Unexpected StyleSheetLoaded notification")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(mPendingSheets > 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(mPendingSheets > 0))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("mPendingSheets > 0"
" (" "Unexpected StyleSheetLoaded notification" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 720); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mPendingSheets > 0"
") (" "Unexpected StyleSheetLoaded notification" ")"); do { MOZ_CrashSequence
(__null, 720); __attribute__((nomerge)) ::abort(); } while (false
); } } while (false)
;
721
722 --mPendingSheets;
723
724 return MaybeDoneWalking();
725 }
726
727 return NS_OK;
728}
729
730nsresult PrototypeDocumentContentSink::LoadScript(
731 nsXULPrototypeScript* aScriptProto, bool* aBlock) {
732 // Load a transcluded script
733 nsresult rv;
734
735 bool isChromeDoc = mDocumentURI->SchemeIs("chrome");
736
737 if (isChromeDoc && aScriptProto->HasStencil()) {
738 rv = ExecuteScript(aScriptProto);
739
740 // Ignore return value from execution, and don't block
741 *aBlock = false;
742 return NS_OK;
743 }
744
745 // Try the XUL script cache, in case two XUL documents source the same
746 // .js file (e.g., strres.js from navigator.xul and utilityOverlay.xul).
747 // XXXbe the cache relies on aScriptProto's GC root!
748 bool useXULCache = nsXULPrototypeCache::GetInstance()->IsEnabled();
749
750 if (isChromeDoc && useXULCache) {
751 RefPtr<JS::Stencil> newStencil =
752 nsXULPrototypeCache::GetInstance()->GetStencil(aScriptProto->mSrcURI);
753 if (newStencil) {
754 // The script language for a proto must remain constant - we
755 // can't just change it for this unexpected language.
756 aScriptProto->Set(newStencil);
757 }
758
759 if (aScriptProto->HasStencil()) {
760 rv = ExecuteScript(aScriptProto);
761
762 // Ignore return value from execution, and don't block
763 *aBlock = false;
764 return NS_OK;
765 }
766 }
767
768 // Release stencil from FastLoad since we decided against using them
769 aScriptProto->Set(nullptr);
770
771 // Set the current script prototype so that OnStreamComplete can report
772 // the right file if there are errors in the script.
773 NS_ASSERTION(!mCurrentScriptProto,do { if (!(!mCurrentScriptProto)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "still loading a script when starting another load?", "!mCurrentScriptProto"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 774); MOZ_PretendNoReturn(); } } while (0)
774 "still loading a script when starting another load?")do { if (!(!mCurrentScriptProto)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "still loading a script when starting another load?", "!mCurrentScriptProto"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 774); MOZ_PretendNoReturn(); } } while (0)
;
775 mCurrentScriptProto = aScriptProto;
776
777 if (isChromeDoc && aScriptProto->mSrcLoading) {
778 // Another document load has started, which is still in progress.
779 // Remember to ResumeWalk this document when the load completes.
780 mNextSrcLoadWaiter = aScriptProto->mSrcLoadWaiters;
781 aScriptProto->mSrcLoadWaiters = this;
782 NS_ADDREF_THIS()AddRef();
783 } else {
784 nsCOMPtr<nsILoadGroup> group =
785 mDocument
786 ->GetDocumentLoadGroup(); // found in
787 // mozilla::dom::Document::SetScriptGlobalObject
788
789 // Note: the loader will keep itself alive while it's loading.
790 nsCOMPtr<nsIStreamLoader> loader;
791 rv = NS_NewStreamLoader(
792 getter_AddRefs(loader), aScriptProto->mSrcURI,
793 this, // aObserver
794 mDocument, // aRequestingContext
795 nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT,
796 nsIContentPolicy::TYPE_INTERNAL_SCRIPT, group);
797
798 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
799 mCurrentScriptProto = nullptr;
800 return rv;
801 }
802
803 aScriptProto->mSrcLoading = true;
804 }
805
806 // Block until OnStreamComplete resumes us.
807 *aBlock = true;
808 return NS_OK;
809}
810
811NS_IMETHODIMPnsresult
812PrototypeDocumentContentSink::OnStreamComplete(nsIStreamLoader* aLoader,
813 nsISupports* context,
814 nsresult aStatus,
815 uint32_t stringLen,
816 const uint8_t* string) {
817 nsCOMPtr<nsIRequest> request;
818 aLoader->GetRequest(getter_AddRefs(request));
819 nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
820
821#ifdef DEBUG1
822 // print a load error on bad status
823 if (NS_FAILED(aStatus)((bool)(__builtin_expect(!!(NS_FAILED_impl(aStatus)), 0)))) {
824 if (channel) {
825 nsCOMPtr<nsIURI> uri;
826 channel->GetURI(getter_AddRefs(uri));
827 if (uri) {
828 printf("Failed to load %s\n", uri->GetSpecOrDefault().get());
829 }
830 }
831 }
832#endif
833
834 // This is the completion routine that will be called when a
835 // transcluded script completes. Compile and execute the script
836 // if the load was successful, then continue building content
837 // from the prototype.
838 nsresult rv = aStatus;
839
840 NS_ASSERTION(mCurrentScriptProto && mCurrentScriptProto->mSrcLoading,do { if (!(mCurrentScriptProto && mCurrentScriptProto
->mSrcLoading)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "script source not loading on unichar stream complete?"
, "mCurrentScriptProto && mCurrentScriptProto->mSrcLoading"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 841); MOZ_PretendNoReturn(); } } while (0)
841 "script source not loading on unichar stream complete?")do { if (!(mCurrentScriptProto && mCurrentScriptProto
->mSrcLoading)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "script source not loading on unichar stream complete?"
, "mCurrentScriptProto && mCurrentScriptProto->mSrcLoading"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 841); MOZ_PretendNoReturn(); } } while (0)
;
842 if (!mCurrentScriptProto) {
843 // XXX Wallpaper for bug 270042
844 return NS_OK;
845 }
846
847 if (NS_SUCCEEDED(aStatus)((bool)(__builtin_expect(!!(!NS_FAILED_impl(aStatus)), 1)))) {
848 // If the including document is a FastLoad document, and we're
849 // compiling an out-of-line script (one with src=...), then we must
850 // be writing a new FastLoad file. If we were reading this script
851 // from the FastLoad file, XULContentSinkImpl::OpenScript (over in
852 // nsXULContentSink.cpp) would have already deserialized a non-null
853 // script->mStencil, causing control flow at the top of LoadScript
854 // not to reach here.
855 nsCOMPtr<nsIURI> uri = mCurrentScriptProto->mSrcURI;
856
857 // XXX should also check nsIHttpChannel::requestSucceeded
858
859 MOZ_ASSERT(!mOffThreadCompiling,do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mOffThreadCompiling)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mOffThreadCompiling))), 0))
) { do { } while (false); MOZ_ReportAssertionFailure("!mOffThreadCompiling"
" (" "PrototypeDocument can't load multiple scripts at once"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 860); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mOffThreadCompiling"
") (" "PrototypeDocument can't load multiple scripts at once"
")"); do { MOZ_CrashSequence(__null, 860); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
860 "PrototypeDocument can't load multiple scripts at once")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!mOffThreadCompiling)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!mOffThreadCompiling))), 0))
) { do { } while (false); MOZ_ReportAssertionFailure("!mOffThreadCompiling"
" (" "PrototypeDocument can't load multiple scripts at once"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 860); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mOffThreadCompiling"
") (" "PrototypeDocument can't load multiple scripts at once"
")"); do { MOZ_CrashSequence(__null, 860); __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
861
862 UniquePtr<Utf8Unit[], JS::FreePolicy> units;
863 size_t unitsLength = 0;
864
865 rv = ScriptLoader::ConvertToUTF8(channel, string, stringLen, u""_ns,
866 mDocument, units, unitsLength);
867 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
868 rv = mCurrentScriptProto->CompileMaybeOffThread(
869 std::move(units), unitsLength, uri, 1, mDocument, this);
870 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && !mCurrentScriptProto->HasStencil()) {
871 mOffThreadCompiling = true;
872 mDocument->BlockOnload();
873 return NS_OK;
874 }
875 }
876 }
877
878 return OnScriptCompileComplete(mCurrentScriptProto->GetStencil(), rv);
879}
880
881NS_IMETHODIMPnsresult
882PrototypeDocumentContentSink::OnScriptCompileComplete(JS::Stencil* aStencil,
883 nsresult aStatus) {
884 // The mCurrentScriptProto may have been cleared out by another
885 // PrototypeDocumentContentSink.
886 if (!mCurrentScriptProto) {
887 return NS_OK;
888 }
889
890 // When compiling off thread the script will not have been attached to the
891 // script proto yet.
892 if (aStencil && !mCurrentScriptProto->HasStencil()) {
893 mCurrentScriptProto->Set(aStencil);
894 }
895
896 // Allow load events to be fired once off thread compilation finishes.
897 if (mOffThreadCompiling) {
898 mOffThreadCompiling = false;
899 mDocument->UnblockOnload(false);
900 }
901
902 // Clear mCurrentScriptProto now, but save it first for use below in
903 // the execute code, and in the while loop that resumes walks of other
904 // documents that raced to load this script.
905 nsXULPrototypeScript* scriptProto = mCurrentScriptProto;
906 mCurrentScriptProto = nullptr;
907
908 // Clear the prototype's loading flag before executing the script or
909 // resuming document walks, in case any of those control flows starts a
910 // new script load.
911 scriptProto->mSrcLoading = false;
912
913 nsresult rv = aStatus;
914 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
915 rv = ExecuteScript(scriptProto);
Value stored to 'rv' is never read
916
917 // If the XUL cache is enabled, save the script object there in
918 // case different XUL documents source the same script.
919 //
920 // But don't save the script in the cache unless the master XUL
921 // document URL is a chrome: URL. It is valid for a URL such as
922 // about:config to translate into a master document URL, whose
923 // prototype document nodes -- including prototype scripts that
924 // hold GC roots protecting their mJSObject pointers -- are not
925 // cached in the XUL prototype cache. See StartDocumentLoad,
926 // the fillXULCache logic.
927 //
928 // A document such as about:config is free to load a script via
929 // a URL such as chrome://global/content/config.js, and we must
930 // not cache that script object without a prototype cache entry
931 // containing a companion nsXULPrototypeScript node that owns a
932 // GC root protecting the script object. Otherwise, the script
933 // cache entry will dangle once the uncached prototype document
934 // is released when its owning document is unloaded.
935 //
936 // (See http://bugzilla.mozilla.org/show_bug.cgi?id=98207 for
937 // the true crime story.)
938 bool useXULCache = nsXULPrototypeCache::GetInstance()->IsEnabled();
939
940 if (useXULCache && mDocumentURI->SchemeIs("chrome") &&
941 scriptProto->HasStencil()) {
942 nsXULPrototypeCache::GetInstance()->PutStencil(scriptProto->mSrcURI,
943 scriptProto->GetStencil());
944 }
945 // ignore any evaluation errors
946 }
947
948 rv = ResumeWalk();
949
950 // Load a pointer to the prototype-script's list of documents who
951 // raced to load the same script
952 PrototypeDocumentContentSink** docp = &scriptProto->mSrcLoadWaiters;
953
954 // Resume walking other documents that waited for this one's load, first
955 // executing the script we just compiled, in each doc's script context
956 PrototypeDocumentContentSink* doc;
957 while ((doc = *docp) != nullptr) {
958 NS_ASSERTION(doc->mCurrentScriptProto == scriptProto,do { if (!(doc->mCurrentScriptProto == scriptProto)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "waiting for wrong script to load?", "doc->mCurrentScriptProto == scriptProto"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 959); MOZ_PretendNoReturn(); } } while (0)
959 "waiting for wrong script to load?")do { if (!(doc->mCurrentScriptProto == scriptProto)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "waiting for wrong script to load?", "doc->mCurrentScriptProto == scriptProto"
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 959); MOZ_PretendNoReturn(); } } while (0)
;
960 doc->mCurrentScriptProto = nullptr;
961
962 // Unlink doc from scriptProto's list before executing and resuming
963 *docp = doc->mNextSrcLoadWaiter;
964 doc->mNextSrcLoadWaiter = nullptr;
965
966 if (aStatus == NS_BINDING_ABORTED && !scriptProto->HasStencil()) {
967 // If the previous doc load was aborted, we want to try loading
968 // again for the next doc. Otherwise, one abort would lead to all
969 // subsequent waiting docs to abort as well.
970 bool block = false;
971 doc->LoadScript(scriptProto, &block);
972 NS_RELEASE(doc)do { (doc)->Release(); (doc) = 0; } while (0);
973 return rv;
974 }
975
976 // Execute only if we loaded and compiled successfully, then resume
977 if (NS_SUCCEEDED(aStatus)((bool)(__builtin_expect(!!(!NS_FAILED_impl(aStatus)), 1))) && scriptProto->HasStencil()) {
978 doc->ExecuteScript(scriptProto);
979 }
980 doc->ResumeWalk();
981 NS_RELEASE(doc)do { (doc)->Release(); (doc) = 0; } while (0);
982 }
983
984 return rv;
985}
986
987nsresult PrototypeDocumentContentSink::ExecuteScript(
988 nsXULPrototypeScript* aScript) {
989 MOZ_ASSERT(aScript != nullptr, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aScript != nullptr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aScript != nullptr))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("aScript != nullptr"
" (" "null ptr" ")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 989); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aScript != nullptr"
") (" "null ptr" ")"); do { MOZ_CrashSequence(__null, 989); __attribute__
((nomerge)) ::abort(); } while (false); } } while (false)
;
990 NS_ENSURE_TRUE(aScript, NS_ERROR_NULL_POINTER)do { if ((__builtin_expect(!!(!(aScript)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "aScript" ") failed", nullptr
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 990); return NS_ERROR_NULL_POINTER; } } while (false)
;
991
992 nsIScriptGlobalObject* scriptGlobalObject;
993 bool aHasHadScriptHandlingObject;
994 scriptGlobalObject =
995 mDocument->GetScriptHandlingObject(aHasHadScriptHandlingObject);
996
997 NS_ENSURE_TRUE(scriptGlobalObject, NS_ERROR_NOT_INITIALIZED)do { if ((__builtin_expect(!!(!(scriptGlobalObject)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "scriptGlobalObject" ") failed"
, nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 997); return NS_ERROR_NOT_INITIALIZED; } } while (false)
;
998
999 nsresult rv;
1000 rv = scriptGlobalObject->EnsureScriptEnvironment();
1001 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 1001); return rv; } } while (false)
;
1002
1003 // Execute the precompiled script with the given version
1004 nsAutoMicroTask mt;
1005
1006 // We're about to run script via JS_ExecuteScript, so we need an
1007 // AutoEntryScript. This is Gecko specific and not in any spec.
1008 AutoEntryScript aes(scriptGlobalObject, "precompiled XUL <script> element");
1009 JSContext* cx = aes.cx();
1010
1011 JS::Rooted<JSScript*> scriptObject(cx);
1012 rv = aScript->InstantiateScript(cx, &scriptObject);
1013 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 1013); return rv; } } while (false)
;
1014
1015 JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
1016 NS_ENSURE_TRUE(xpc::Scriptability::Get(global).Allowed(), NS_OK)do { if ((__builtin_expect(!!(!(xpc::Scriptability::Get(global
).Allowed())), 0))) { NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE("
"xpc::Scriptability::Get(global).Allowed()" ") failed", nullptr
, "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 1016); return NS_OK; } } while (false)
;
1017
1018 if (!aScript->mOutOfLine) {
1019 // Check if CSP allows loading of inline scripts.
1020 if (nsCOMPtr<nsIContentSecurityPolicy> csp = mDocument->GetCsp()) {
1021 nsAutoJSString content;
1022 JS::Rooted<JSString*> decompiled(cx,
1023 JS_DecompileScript(cx, scriptObject));
1024 if (NS_WARN_IF(!decompiled || !content.init(cx, decompiled))NS_warn_if_impl(!decompiled || !content.init(cx, decompiled),
"!decompiled || !content.init(cx, decompiled)", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 1024)
) {
1025 JS_ClearPendingException(cx);
1026 }
1027
1028 bool allowInlineScript = false;
1029 rv = csp->GetAllowsInline(
1030 nsIContentSecurityPolicy::SCRIPT_SRC_ELEM_DIRECTIVE,
1031 /* aHasUnsafeHash */ false, /* aNonce */ u""_ns,
1032 /* aParserCreated */ true,
1033 /* aTriggeringElement */ nullptr,
1034 /* nsICSPEventListener */ nullptr,
1035 /* aContentOfPseudoScript */ content, aScript->mLineNo,
1036 /* aColumnNumber */ 0, &allowInlineScript);
1037 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !allowInlineScript) {
1038 return NS_OK;
1039 }
1040 }
1041 }
1042
1043 // On failure, ~AutoScriptEntry will handle exceptions, so
1044 // there is no need to manually check the return value.
1045 JS::Rooted<JS::Value> rval(cx);
1046 Unused << JS_ExecuteScript(cx, scriptObject, &rval);
1047
1048 return NS_OK;
1049}
1050
1051nsresult PrototypeDocumentContentSink::CreateElementFromPrototype(
1052 nsXULPrototypeElement* aPrototype, Element** aResult, nsIContent* aParent) {
1053 // Create a content model element from a prototype element.
1054 MOZ_ASSERT(aPrototype, "null ptr")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aPrototype)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aPrototype))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aPrototype" " (" "null ptr"
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 1054); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPrototype"
") (" "null ptr" ")"); do { MOZ_CrashSequence(__null, 1054);
__attribute__((nomerge)) ::abort(); } while (false); } } while
(false)
;
1055 if (!aPrototype) return NS_ERROR_NULL_POINTER;
1056
1057 *aResult = nullptr;
1058 nsresult rv = NS_OK;
1059
1060 if (MOZ_LOG_TEST(gLog, LogLevel::Debug)(__builtin_expect(!!(mozilla::detail::log_test(gLog, LogLevel
::Debug)), 0))
) {
1061 MOZ_LOG(do { const ::mozilla::LogModule* moz_real_module = gLog; if (
(__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "prototype: creating <%s> from prototype"
, NS_ConvertUTF16toUTF8(aPrototype->mNodeInfo->QualifiedName
()).get()); } } while (0)
1062 gLog, LogLevel::Debug,do { const ::mozilla::LogModule* moz_real_module = gLog; if (
(__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "prototype: creating <%s> from prototype"
, NS_ConvertUTF16toUTF8(aPrototype->mNodeInfo->QualifiedName
()).get()); } } while (0)
1063 ("prototype: creating <%s> from prototype",do { const ::mozilla::LogModule* moz_real_module = gLog; if (
(__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "prototype: creating <%s> from prototype"
, NS_ConvertUTF16toUTF8(aPrototype->mNodeInfo->QualifiedName
()).get()); } } while (0)
1064 NS_ConvertUTF16toUTF8(aPrototype->mNodeInfo->QualifiedName()).get()))do { const ::mozilla::LogModule* moz_real_module = gLog; if (
(__builtin_expect(!!(mozilla::detail::log_test(moz_real_module
, LogLevel::Debug)), 0))) { mozilla::detail::log_print(moz_real_module
, LogLevel::Debug, "prototype: creating <%s> from prototype"
, NS_ConvertUTF16toUTF8(aPrototype->mNodeInfo->QualifiedName
()).get()); } } while (0)
;
1065 }
1066
1067 RefPtr<Element> result;
1068
1069 Document* doc = aParent ? aParent->OwnerDoc() : mDocument.get();
1070 if (aPrototype->mNodeInfo->NamespaceEquals(kNameSpaceID_XUL8)) {
1071 const bool isRoot = !aParent;
1072 // If it's a XUL element, it'll be lightweight until somebody
1073 // monkeys with it.
1074 result = nsXULElement::CreateFromPrototype(aPrototype, doc, isRoot);
1075 if (!result) {
1076 return NS_ERROR_OUT_OF_MEMORY;
1077 }
1078 } else {
1079 // If it's not a XUL element, it's gonna be heavyweight no matter
1080 // what. So we need to copy everything out of the prototype
1081 // into the element. Get a nodeinfo from our nodeinfo manager
1082 // for this node.
1083 RefPtr<NodeInfo> newNodeInfo = doc->NodeInfoManager()->GetNodeInfo(
1084 aPrototype->mNodeInfo->NameAtom(),
1085 aPrototype->mNodeInfo->GetPrefixAtom(),
1086 aPrototype->mNodeInfo->NamespaceID(), nsINode::ELEMENT_NODE);
1087 if (!newNodeInfo) {
1088 return NS_ERROR_OUT_OF_MEMORY;
1089 }
1090 const bool isScript =
1091 newNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_XHTML3) ||
1092 newNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_SVG9);
1093 if (aPrototype->mIsAtom &&
1094 newNodeInfo->NamespaceID() == kNameSpaceID_XHTML3) {
1095 rv = NS_NewHTMLElement(getter_AddRefs(result), newNodeInfo.forget(),
1096 NOT_FROM_PARSER, aPrototype->mIsAtom);
1097 } else {
1098 rv = NS_NewElement(getter_AddRefs(result), newNodeInfo.forget(),
1099 NOT_FROM_PARSER);
1100 }
1101 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
1102
1103 rv = AddAttributes(aPrototype, result);
1104 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
1105
1106 if (isScript) {
1107 nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(result);
1108 MOZ_ASSERT(sele, "Node didn't QI to script.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(sele)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(sele))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("sele" " (" "Node didn't QI to script."
")", "/root/firefox-clang/dom/prototype/PrototypeDocumentContentSink.cpp"
, 1108); AnnotateMozCrashReason("MOZ_ASSERT" "(" "sele" ") ("
"Node didn't QI to script." ")"); do { MOZ_CrashSequence(__null
, 1108); __attribute__((nomerge)) ::abort(); } while (false);
} } while (false)
;
1109
1110 sele->FreezeExecutionAttrs(doc);
1111 // Script loading is handled by the this content sink, so prevent the
1112 // script from loading when it is bound to the document.
1113 //
1114 // NOTE(emilio): This is only done for non-module scripts, because we
1115 // don't support caching modules properly yet, see the comment in
1116 // XULContentSinkImpl::OpenScript. For non-inline scripts, this is enough,
1117 // since we can start the load when the node is inserted. Non-inline
1118 // scripts need another special-case in CloseElement.
1119 if (!sele->GetScriptIsModule()) {
1120 sele->PreventExecution();
1121 }
1122 }
1123 }
1124
1125 // FIXME(bug 1627474): Is this right if this is inside an <html:template>?
1126 if (result->HasAttr(nsGkAtoms::datal10nid)) {
1127 mDocument->mL10nProtoElements.InsertOrUpdate(result, RefPtr{aPrototype});
1128 result->SetElementCreatedFromPrototypeAndHasUnmodifiedL10n();
1129 }
1130 result.forget(aResult);
1131 return NS_OK;
1132}
1133
1134nsresult PrototypeDocumentContentSink::AddAttributes(
1135 nsXULPrototypeElement* aPrototype, Element* aElement) {
1136 nsresult rv;
1137
1138 for (size_t i = 0; i < aPrototype->mAttributes.Length(); ++i) {
1139 nsXULPrototypeAttribute* protoattr = &(aPrototype->mAttributes[i]);
1140 nsAutoString valueStr;
1141 protoattr->mValue.ToString(valueStr);
1142
1143 rv = aElement->SetAttr(protoattr->mName.NamespaceID(),
1144 protoattr->mName.LocalName(),
1145 protoattr->mName.GetPrefix(), valueStr, false);
1146 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
1147 }
1148
1149 return NS_OK;
1150}
1151
1152} // namespace mozilla::dom