1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsXULElement.h"
10 #include "AttrArray.h"
11 #include "MainThreadUtils.h"
12 #include "ReferrerInfo.h"
14 #include "XULFrameElement.h"
15 #include "XULMenuElement.h"
16 #include "XULPopupElement.h"
17 #include "XULTextElement.h"
18 #include "XULTooltipElement.h"
19 #include "XULTreeElement.h"
20 #include "js/CompilationAndEvaluation.h"
21 #include "js/CompileOptions.h"
22 #include "js/experimental/JSStencil.h"
23 #include "js/OffThreadScriptCompilation.h"
24 #include "js/SourceText.h"
25 #include "js/Transcoding.h"
26 #include "js/Utility.h"
28 #include "mozilla/Assertions.h"
29 #include "mozilla/ArrayIterator.h"
30 #include "mozilla/ClearOnShutdown.h"
31 #include "mozilla/DeclarationBlock.h"
32 #include "mozilla/EventDispatcher.h"
33 #include "mozilla/EventListenerManager.h"
34 #include "mozilla/EventStateManager.h"
35 #include "mozilla/FlushType.h"
36 #include "mozilla/GlobalKeyListener.h"
37 #include "mozilla/HoldDropJSObjects.h"
38 #include "mozilla/MacroForEach.h"
39 #include "mozilla/Maybe.h"
40 #include "mozilla/MouseEvents.h"
41 #include "mozilla/OwningNonNull.h"
42 #include "mozilla/PresShell.h"
43 #include "mozilla/RefPtr.h"
44 #include "mozilla/ScopeExit.h"
45 #include "mozilla/StaticAnalysisFunctions.h"
46 #include "mozilla/StaticPtr.h"
47 #include "mozilla/URLExtraData.h"
48 #include "mozilla/dom/BindContext.h"
49 #include "mozilla/dom/BorrowedAttrInfo.h"
50 #include "mozilla/dom/CSSRuleBinding.h"
51 #include "mozilla/dom/Document.h"
52 #include "mozilla/dom/DocumentInlines.h"
53 #include "mozilla/dom/Element.h"
54 #include "mozilla/dom/Event.h"
55 #include "mozilla/dom/EventTarget.h"
56 #include "mozilla/dom/FragmentOrElement.h"
57 #include "mozilla/dom/FromParser.h"
58 #include "mozilla/dom/MouseEventBinding.h"
59 #include "mozilla/dom/MutationEventBinding.h"
60 #include "mozilla/dom/NodeInfo.h"
61 #include "mozilla/dom/ReferrerPolicyBinding.h"
62 #include "mozilla/dom/ScriptSettings.h"
63 #include "mozilla/dom/XULBroadcastManager.h"
64 #include "mozilla/dom/XULCommandEvent.h"
65 #include "mozilla/dom/XULElementBinding.h"
66 #include "mozilla/dom/nsCSPUtils.h"
67 #include "mozilla/fallible.h"
69 #include "nsAttrValueInlines.h"
70 #include "nsAttrValueOrString.h"
71 #include "nsCaseTreatment.h"
72 #include "nsChangeHint.h"
74 #include "nsCompatibility.h"
75 #include "nsContentCreatorFunctions.h"
76 #include "nsContentUtils.h"
77 #include "nsCycleCollectionNoteChild.h"
78 #include "nsCycleCollectionTraversalCallback.h"
81 #include "nsFocusManager.h"
82 #include "nsGkAtoms.h"
83 #include "nsIContent.h"
84 #include "nsIContentSecurityPolicy.h"
85 #include "nsIControllers.h"
87 #include "nsIDOMEventListener.h"
88 #include "nsIDOMXULControlElement.h"
89 #include "nsIDOMXULSelectCntrlItemEl.h"
90 #include "nsIDocShell.h"
91 #include "nsIFocusManager.h"
93 #include "nsIObjectInputStream.h"
94 #include "nsIObjectOutputStream.h"
95 #include "nsIRunnable.h"
96 #include "nsIScriptContext.h"
97 #include "nsISupportsUtils.h"
99 #include "nsIXPConnect.h"
100 #include "nsMenuFrame.h"
101 #include "nsMenuPopupFrame.h"
102 #include "nsNodeInfoManager.h"
103 #include "nsPIDOMWindow.h"
104 #include "nsPIDOMWindowInlines.h"
105 #include "nsPresContext.h"
106 #include "nsQueryFrame.h"
107 #include "nsString.h"
108 #include "nsStyledElement.h"
109 #include "nsThreadUtils.h"
110 #include "nsXULControllers.h"
111 #include "nsXULPopupListener.h"
112 #include "nsXULPopupManager.h"
113 #include "nsXULPrototypeCache.h"
114 #include "nsXULTooltipListener.h"
115 #include "xpcpublic.h"
117 using namespace mozilla
;
118 using namespace mozilla::dom
;
120 #ifdef XUL_PROTOTYPE_ATTRIBUTE_METERING
121 uint32_t nsXULPrototypeAttribute::gNumElements
;
122 uint32_t nsXULPrototypeAttribute::gNumAttributes
;
123 uint32_t nsXULPrototypeAttribute::gNumCacheTests
;
124 uint32_t nsXULPrototypeAttribute::gNumCacheHits
;
125 uint32_t nsXULPrototypeAttribute::gNumCacheSets
;
126 uint32_t nsXULPrototypeAttribute::gNumCacheFills
;
129 #define NS_DISPATCH_XUL_COMMAND (1 << 0)
131 //----------------------------------------------------------------------
135 nsXULElement::nsXULElement(already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
)
136 : nsStyledElement(std::move(aNodeInfo
)) {
137 XUL_PROTOTYPE_ATTRIBUTE_METER(gNumElements
);
140 nsXULElement::~nsXULElement() = default;
143 nsXULElement
* NS_NewBasicXULElement(
144 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
) {
145 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo(std::move(aNodeInfo
));
146 auto* nim
= nodeInfo
->NodeInfoManager();
147 return new (nim
) nsXULElement(nodeInfo
.forget());
151 nsXULElement
* nsXULElement::Construct(
152 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
) {
153 // NOTE: If you add elements here, you probably also want to change
154 // mozilla::dom::binding_detail::HTMLConstructor in BindingUtils.cpp to take
155 // them into account, otherwise you'll start getting "Illegal constructor"
156 // exceptions in chrome code.
157 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo
= aNodeInfo
;
158 if (nodeInfo
->Equals(nsGkAtoms::label
) ||
159 nodeInfo
->Equals(nsGkAtoms::description
)) {
160 auto* nim
= nodeInfo
->NodeInfoManager();
161 return new (nim
) XULTextElement(nodeInfo
.forget());
164 if (nodeInfo
->Equals(nsGkAtoms::menupopup
) ||
165 nodeInfo
->Equals(nsGkAtoms::popup
) ||
166 nodeInfo
->Equals(nsGkAtoms::panel
)) {
167 return NS_NewXULPopupElement(nodeInfo
.forget());
170 if (nodeInfo
->Equals(nsGkAtoms::tooltip
)) {
171 return NS_NewXULTooltipElement(nodeInfo
.forget());
174 if (nodeInfo
->Equals(nsGkAtoms::iframe
) ||
175 nodeInfo
->Equals(nsGkAtoms::browser
) ||
176 nodeInfo
->Equals(nsGkAtoms::editor
)) {
177 auto* nim
= nodeInfo
->NodeInfoManager();
178 return new (nim
) XULFrameElement(nodeInfo
.forget());
181 if (nodeInfo
->Equals(nsGkAtoms::menu
) ||
182 nodeInfo
->Equals(nsGkAtoms::menulist
)) {
183 auto* nim
= nodeInfo
->NodeInfoManager();
184 return new (nim
) XULMenuElement(nodeInfo
.forget());
187 if (nodeInfo
->Equals(nsGkAtoms::tree
)) {
188 auto* nim
= nodeInfo
->NodeInfoManager();
189 return new (nim
) XULTreeElement(nodeInfo
.forget());
192 return NS_NewBasicXULElement(nodeInfo
.forget());
196 already_AddRefed
<nsXULElement
> nsXULElement::CreateFromPrototype(
197 nsXULPrototypeElement
* aPrototype
, mozilla::dom::NodeInfo
* aNodeInfo
,
198 bool aIsScriptable
, bool aIsRoot
) {
199 RefPtr
<mozilla::dom::NodeInfo
> ni
= aNodeInfo
;
200 nsCOMPtr
<Element
> baseElement
;
201 NS_NewXULElement(getter_AddRefs(baseElement
), ni
.forget(),
202 dom::FROM_PARSER_NETWORK
, aPrototype
->mIsAtom
);
205 nsXULElement
* element
= FromNode(baseElement
);
207 if (aPrototype
->mHasIdAttribute
) {
210 if (aPrototype
->mHasClassAttribute
) {
211 element
->SetMayHaveClass();
213 if (aPrototype
->mHasStyleAttribute
) {
214 element
->SetMayHaveStyle();
217 element
->MakeHeavyweight(aPrototype
);
219 // Check each attribute on the prototype to see if we need to do
220 // any additional processing and hookup that would otherwise be
221 // done 'automagically' by SetAttr().
222 for (const auto& attribute
: aPrototype
->mAttributes
) {
223 element
->AddListenerForAttributeIfNeeded(attribute
.mName
);
227 return baseElement
.forget().downcast
<nsXULElement
>();
233 nsresult
nsXULElement::CreateFromPrototype(nsXULPrototypeElement
* aPrototype
,
235 bool aIsScriptable
, bool aIsRoot
,
237 // Create an nsXULElement from a prototype
238 MOZ_ASSERT(aPrototype
!= nullptr, "null ptr");
239 if (!aPrototype
) return NS_ERROR_NULL_POINTER
;
241 MOZ_ASSERT(aResult
!= nullptr, "null ptr");
242 if (!aResult
) return NS_ERROR_NULL_POINTER
;
244 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo
;
246 mozilla::dom::NodeInfo
* ni
= aPrototype
->mNodeInfo
;
247 nodeInfo
= aDocument
->NodeInfoManager()->GetNodeInfo(
248 ni
->NameAtom(), ni
->GetPrefixAtom(), ni
->NamespaceID(), ELEMENT_NODE
);
250 nodeInfo
= aPrototype
->mNodeInfo
;
253 RefPtr
<nsXULElement
> element
=
254 CreateFromPrototype(aPrototype
, nodeInfo
, aIsScriptable
, aIsRoot
);
255 element
.forget(aResult
);
260 nsresult
NS_NewXULElement(Element
** aResult
,
261 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
,
262 FromParser aFromParser
, nsAtom
* aIsAtom
,
263 mozilla::dom::CustomElementDefinition
* aDefinition
) {
264 RefPtr
<mozilla::dom::NodeInfo
> nodeInfo
= aNodeInfo
;
266 MOZ_ASSERT(nodeInfo
, "need nodeinfo for non-proto Create");
269 nodeInfo
->NamespaceEquals(kNameSpaceID_XUL
),
270 "Trying to create XUL elements that don't have the XUL namespace");
272 Document
* doc
= nodeInfo
->GetDocument();
273 if (doc
&& !doc
->AllowXULXBL()) {
274 return NS_ERROR_NOT_AVAILABLE
;
277 return nsContentUtils::NewXULOrHTMLElement(aResult
, nodeInfo
, aFromParser
,
278 aIsAtom
, aDefinition
);
281 void NS_TrustedNewXULElement(
282 Element
** aResult
, already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
) {
283 RefPtr
<mozilla::dom::NodeInfo
> ni
= aNodeInfo
;
284 MOZ_ASSERT(ni
, "need nodeinfo for non-proto Create");
286 // Create an nsXULElement with the specified namespace and tag.
287 NS_ADDREF(*aResult
= nsXULElement::Construct(ni
.forget()));
290 //----------------------------------------------------------------------
291 // nsISupports interface
293 NS_IMPL_CYCLE_COLLECTION_INHERITED(nsXULElement
, nsStyledElement
)
295 NS_IMPL_ADDREF_INHERITED(nsXULElement
, nsStyledElement
)
296 NS_IMPL_RELEASE_INHERITED(nsXULElement
, nsStyledElement
)
298 NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsXULElement
)
299 NS_ELEMENT_INTERFACE_TABLE_TO_MAP_SEGUE
300 NS_INTERFACE_MAP_END_INHERITING(nsStyledElement
)
302 //----------------------------------------------------------------------
305 nsresult
nsXULElement::Clone(mozilla::dom::NodeInfo
* aNodeInfo
,
306 nsINode
** aResult
) const {
309 RefPtr
<mozilla::dom::NodeInfo
> ni
= aNodeInfo
;
310 RefPtr
<nsXULElement
> element
= Construct(ni
.forget());
312 nsresult rv
= const_cast<nsXULElement
*>(this)->CopyInnerTo(
313 element
, ReparseAttributes::No
);
314 NS_ENSURE_SUCCESS(rv
, rv
);
316 // Note that we're _not_ copying mControllers.
318 element
.forget(aResult
);
322 //----------------------------------------------------------------------
324 EventListenerManager
* nsXULElement::GetEventListenerManagerForAttr(
325 nsAtom
* aAttrName
, bool* aDefer
) {
326 // XXXbz sXBL/XBL2 issue: should we instead use GetComposedDoc()
327 // here, override BindToTree for those classes and munge event
329 Document
* doc
= OwnerDoc();
331 nsPIDOMWindowInner
* window
;
332 Element
* root
= doc
->GetRootElement();
333 if ((!root
|| root
== this) && (window
= doc
->GetInnerWindow())) {
334 nsCOMPtr
<EventTarget
> piTarget
= do_QueryInterface(window
);
337 return piTarget
->GetOrCreateListenerManager();
340 return nsStyledElement::GetEventListenerManagerForAttr(aAttrName
, aDefer
);
343 // returns true if the element is not a list
344 static bool IsNonList(mozilla::dom::NodeInfo
* aNodeInfo
) {
345 return !aNodeInfo
->Equals(nsGkAtoms::tree
) &&
346 !aNodeInfo
->Equals(nsGkAtoms::richlistbox
);
349 bool nsXULElement::IsFocusableInternal(int32_t* aTabIndex
, bool aWithMouse
) {
351 * Returns true if an element may be focused, and false otherwise. The inout
352 * argument aTabIndex will be set to the tab order index to be used; -1 for
353 * elements that should not be part of the tab order and a greater value to
354 * indicate its tab order.
356 * Confusingly, the supplied value for the aTabIndex argument may indicate
357 * whether the element may be focused as a result of the -moz-user-focus
358 * property, where -1 means no and 0 means yes.
360 * For controls, the element cannot be focused and is not part of the tab
361 * order if it is disabled.
363 * -moz-user-focus is overridden if a tabindex (even -1) is specified.
365 * Specifically, the behaviour for all XUL elements is as follows:
366 * *aTabIndex = -1 no tabindex Not focusable or tabbable
367 * *aTabIndex = -1 tabindex="-1" Focusable but not tabbable
368 * *aTabIndex = -1 tabindex=">=0" Focusable and tabbable
369 * *aTabIndex >= 0 no tabindex Focusable and tabbable
370 * *aTabIndex >= 0 tabindex="-1" Focusable but not tabbable
371 * *aTabIndex >= 0 tabindex=">=0" Focusable and tabbable
373 * If aTabIndex is null, then the tabindex is not computed, and
374 * true is returned for non-disabled controls and false otherwise.
377 // elements are not focusable by default
378 bool shouldFocus
= false;
381 // on Mac, mouse interactions only focus the element if it's a list,
382 // or if it's a remote target, since the remote target must handle
384 if (aWithMouse
&& IsNonList(mNodeInfo
) &&
385 !EventStateManager::IsTopLevelRemoteTarget(this)) {
390 nsCOMPtr
<nsIDOMXULControlElement
> xulControl
= AsXULControl();
392 // a disabled element cannot be focused and is not part of the tab order
394 xulControl
->GetDisabled(&disabled
);
396 if (aTabIndex
) *aTabIndex
= -1;
403 Maybe
<int32_t> attrVal
= GetTabIndexAttrValue();
404 if (attrVal
.isSome()) {
405 // The tabindex attribute was specified, so the element becomes
408 *aTabIndex
= attrVal
.value();
410 // otherwise, if there is no tabindex attribute, just use the value of
411 // *aTabIndex to indicate focusability. Reset any supplied tabindex to 0.
412 shouldFocus
= *aTabIndex
>= 0;
418 if (xulControl
&& shouldFocus
&& sTabFocusModelAppliesToXUL
&&
419 !(sTabFocusModel
& eTabFocus_formElementsMask
)) {
420 // By default, the tab focus model doesn't apply to xul element on any
421 // system but OS X. on OS X we're following it for UI elements (XUL) as
422 // sTabFocusModel is based on "Full Keyboard Access" system setting (see
423 // mac/nsILookAndFeel). both textboxes and list elements (i.e. trees and
424 // list) should always be focusable (textboxes are handled as html:input)
425 // For compatibility, we only do this for controls, otherwise elements
426 // like <browser> cannot take this focus.
427 if (IsNonList(mNodeInfo
)) {
436 bool nsXULElement::HasMenu() {
437 nsMenuFrame
* menu
= do_QueryFrame(GetPrimaryFrame(FlushType::Frames
));
441 void nsXULElement::OpenMenu(bool aOpenFlag
) {
442 // Flush frames first. It's not clear why this is needed, see bug 1704670.
443 if (Document
* doc
= GetComposedDoc()) {
444 doc
->FlushPendingNotifications(FlushType::Frames
);
447 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
450 // Nothing will happen if this element isn't a menu.
451 pm
->ShowMenu(this, false);
453 // Nothing will happen if this element isn't a menu.
459 Result
<bool, nsresult
> nsXULElement::PerformAccesskey(bool aKeyCausesActivation
,
460 bool aIsTrustedEvent
) {
461 if (IsXULElement(nsGkAtoms::label
)) {
462 nsAutoString control
;
463 GetAttr(kNameSpaceID_None
, nsGkAtoms::control
, control
);
464 if (control
.IsEmpty()) {
465 return Err(NS_ERROR_UNEXPECTED
);
468 // XXXsmaug Should we use ShadowRoot::GetElementById in case
469 // element is in Shadow DOM?
470 RefPtr
<Document
> document
= GetUncomposedDoc();
472 return Err(NS_ERROR_UNEXPECTED
);
475 RefPtr
<Element
> element
= document
->GetElementById(control
);
477 return Err(NS_ERROR_UNEXPECTED
);
480 // XXXedgar, This is mainly for HTMLElement which doesn't do visible
481 // check in PerformAccesskey. We probably should always do visible
482 // check on HTMLElement even if the PerformAccesskey is not redirected from
483 // label XULelement per spec.
484 nsIFrame
* frame
= element
->GetPrimaryFrame();
485 if (!frame
|| !frame
->IsVisibleConsideringAncestors()) {
486 return Err(NS_ERROR_UNEXPECTED
);
489 return element
->PerformAccesskey(aKeyCausesActivation
, aIsTrustedEvent
);
492 nsIFrame
* frame
= GetPrimaryFrame();
493 if (!frame
|| !frame
->IsVisibleConsideringAncestors()) {
494 return Err(NS_ERROR_UNEXPECTED
);
497 bool focused
= false;
498 // Define behavior for each type of XUL element.
499 if (!IsXULElement(nsGkAtoms::toolbarbutton
)) {
500 if (RefPtr
<nsFocusManager
> fm
= nsFocusManager::GetFocusManager()) {
501 RefPtr
<Element
> elementToFocus
= this;
502 // for radio buttons, focus the radiogroup instead
503 if (IsXULElement(nsGkAtoms::radio
)) {
504 if (nsCOMPtr
<nsIDOMXULSelectControlItemElement
> controlItem
=
505 AsXULSelectControlItem()) {
507 controlItem
->GetDisabled(&disabled
);
509 controlItem
->GetControl(getter_AddRefs(elementToFocus
));
514 if (elementToFocus
) {
515 fm
->SetFocus(elementToFocus
, nsIFocusManager::FLAG_BYKEY
);
517 // Return true if the element became focused.
518 nsPIDOMWindowOuter
* window
= OwnerDoc()->GetWindow();
519 focused
= (window
&& window
->GetFocusedElement() == elementToFocus
);
524 if (aKeyCausesActivation
&& !IsXULElement(nsGkAtoms::menulist
)) {
525 ClickWithInputSource(MouseEvent_Binding::MOZ_SOURCE_KEYBOARD
,
530 // If the accesskey won't cause the activation and the focus isn't changed,
531 // either. Return error so EventStateManager would try to find next element
532 // to handle the accesskey.
533 return focused
? Result
<bool, nsresult
>{focused
} : Err(NS_ERROR_ABORT
);
536 //----------------------------------------------------------------------
538 void nsXULElement::AddListenerForAttributeIfNeeded(nsAtom
* aLocalName
) {
539 // If appropriate, add a popup listener and/or compile the event
540 // handler. Called when we change the element's document, create a
541 // new element, change an attribute's value, etc.
542 // Eventlistenener-attributes are always in the null namespace.
543 if (aLocalName
== nsGkAtoms::menu
|| aLocalName
== nsGkAtoms::contextmenu
||
544 // XXXdwh popup and context are deprecated
545 aLocalName
== nsGkAtoms::popup
|| aLocalName
== nsGkAtoms::context
) {
546 AddPopupListener(aLocalName
);
548 if (nsContentUtils::IsEventAttributeName(aLocalName
, EventNameType_XUL
)) {
550 GetAttr(kNameSpaceID_None
, aLocalName
, value
);
551 SetEventHandler(aLocalName
, value
, true);
555 void nsXULElement::AddListenerForAttributeIfNeeded(const nsAttrName
& aName
) {
556 if (aName
.IsAtom()) {
557 AddListenerForAttributeIfNeeded(aName
.Atom());
561 //----------------------------------------------------------------------
563 // nsIContent interface
565 void nsXULElement::UpdateEditableState(bool aNotify
) {
566 // Don't call through to Element here because the things
567 // it does don't work for cases when we're an editable control.
568 nsIContent
* parent
= GetParent();
570 SetEditableFlag(parent
&& parent
->HasFlag(NODE_IS_EDITABLE
));
571 UpdateState(aNotify
);
574 class XULInContentErrorReporter
: public Runnable
{
576 explicit XULInContentErrorReporter(Document
& aDocument
)
577 : mozilla::Runnable("XULInContentErrorReporter"), mDocument(aDocument
) {}
579 NS_IMETHOD
Run() override
{
580 mDocument
->WarnOnceAbout(DeprecatedOperations::eImportXULIntoContent
,
586 OwningNonNull
<Document
> mDocument
;
589 static bool NeedTooltipSupport(const nsXULElement
& aXULElement
) {
590 if (aXULElement
.NodeInfo()->Equals(nsGkAtoms::treechildren
)) {
591 // treechildren always get tooltip support, since cropped tree cells show
592 // their full text in a tooltip.
596 return aXULElement
.GetBoolAttr(nsGkAtoms::tooltip
) ||
597 aXULElement
.GetBoolAttr(nsGkAtoms::tooltiptext
);
600 nsresult
nsXULElement::BindToTree(BindContext
& aContext
, nsINode
& aParent
) {
601 nsresult rv
= nsStyledElement::BindToTree(aContext
, aParent
);
602 NS_ENSURE_SUCCESS(rv
, rv
);
604 if (!IsInComposedDoc()) {
608 Document
& doc
= aContext
.OwnerDoc();
609 if (!IsInNativeAnonymousSubtree() && !doc
.AllowXULXBL() &&
610 !doc
.HasWarnedAbout(DeprecatedOperations::eImportXULIntoContent
)) {
611 nsContentUtils::AddScriptRunner(new XULInContentErrorReporter(doc
));
615 if (!doc
.AllowXULXBL() && !doc
.IsUnstyledDocument()) {
616 // To save CPU cycles and memory, non-XUL documents only load the user
617 // agent style sheet rules for a minimal set of XUL elements such as
618 // 'scrollbar' that may be created implicitly for their content (those
619 // rules being in minimal-xul.css).
621 // This assertion makes sure no other XUL element is used in a non-XUL
623 nsAtom
* tag
= NodeInfo()->NameAtom();
626 tag
== nsGkAtoms::scrollbar
|| tag
== nsGkAtoms::scrollbarbutton
||
627 tag
== nsGkAtoms::scrollcorner
|| tag
== nsGkAtoms::slider
||
628 tag
== nsGkAtoms::thumb
||
630 tag
== nsGkAtoms::resizer
|| tag
== nsGkAtoms::label
,
631 "Unexpected XUL element in non-XUL doc");
635 // Within Bug 1492063 and its dependencies we started to apply a
636 // CSP to system privileged about pages. Since some about: pages
637 // are implemented in *.xul files we added this workaround to
638 // apply a CSP to them. To do so, we check the introduced custom
639 // attribute 'csp' on the root element.
640 if (doc
.GetRootElement() == this) {
641 nsAutoString cspPolicyStr
;
642 GetAttr(kNameSpaceID_None
, nsGkAtoms::csp
, cspPolicyStr
);
646 nsCOMPtr
<nsIContentSecurityPolicy
> docCSP
= doc
.GetCsp();
647 uint32_t policyCount
= 0;
649 docCSP
->GetPolicyCount(&policyCount
);
651 MOZ_ASSERT(policyCount
== 0, "how come we already have a policy?");
655 CSP_ApplyMetaCSPToDoc(doc
, cspPolicyStr
);
658 if (NodeInfo()->Equals(nsGkAtoms::keyset
, kNameSpaceID_XUL
)) {
659 // Create our XUL key listener and hook it up.
660 XULKeySetGlobalKeyListener::AttachKeyHandler(this);
663 RegUnRegAccessKey(true);
665 if (NeedTooltipSupport(*this)) {
669 if (XULBroadcastManager::MayNeedListener(*this)) {
670 if (!doc
.HasXULBroadcastManager()) {
671 doc
.InitializeXULBroadcastManager();
673 XULBroadcastManager
* broadcastManager
= doc
.GetXULBroadcastManager();
674 broadcastManager
->AddListener(this);
679 void nsXULElement::UnbindFromTree(bool aNullParent
) {
680 if (NodeInfo()->Equals(nsGkAtoms::keyset
, kNameSpaceID_XUL
)) {
681 XULKeySetGlobalKeyListener::DetachKeyHandler(this);
684 RegUnRegAccessKey(false);
686 if (NeedTooltipSupport(*this)) {
687 RemoveTooltipSupport();
690 Document
* doc
= GetComposedDoc();
691 if (doc
&& doc
->HasXULBroadcastManager() &&
692 XULBroadcastManager::MayNeedListener(*this)) {
693 RefPtr
<XULBroadcastManager
> broadcastManager
=
694 doc
->GetXULBroadcastManager();
695 broadcastManager
->RemoveListener(this);
698 // mControllers can own objects that are implemented
699 // in JavaScript (such as some implementations of
700 // nsIControllers. These objects prevent their global
701 // object's script object from being garbage collected,
702 // which means JS continues to hold an owning reference
703 // to the nsGlobalWindow, which owns the document,
704 // which owns this content. That's a cycle, so we break
705 // it here. (It might be better to break this by releasing
706 // mDocument in nsGlobalWindow::SetDocShell, but I'm not
707 // sure whether that would fix all possible cycles through
709 nsExtendedDOMSlots
* slots
= GetExistingExtendedDOMSlots();
711 slots
->mControllers
= nullptr;
714 nsStyledElement::UnbindFromTree(aNullParent
);
717 void nsXULElement::DoneAddingChildren(bool aHaveNotified
) {
718 if (IsXULElement(nsGkAtoms::linkset
)) {
719 Document
* doc
= GetComposedDoc();
721 doc
->OnL10nResourceContainerParsed();
726 void nsXULElement::RegUnRegAccessKey(bool aDoReg
) {
727 // Don't try to register for unsupported elements
728 if (!SupportsAccessKey()) {
732 nsStyledElement::RegUnRegAccessKey(aDoReg
);
735 bool nsXULElement::SupportsAccessKey() const {
736 if (NodeInfo()->Equals(nsGkAtoms::label
) && HasAttr(nsGkAtoms::control
)) {
740 // XXX(ntim): check if description[value] or description[accesskey] are
741 // actually used, remove `value` from {Before/After}SetAttr if not the case
742 if (NodeInfo()->Equals(nsGkAtoms::description
) && HasAttr(nsGkAtoms::value
) &&
743 HasAttr(nsGkAtoms::control
)) {
747 return IsAnyOfXULElements(nsGkAtoms::button
, nsGkAtoms::toolbarbutton
,
748 nsGkAtoms::checkbox
, nsGkAtoms::tab
,
752 nsresult
nsXULElement::BeforeSetAttr(int32_t aNamespaceID
, nsAtom
* aName
,
753 const nsAttrValueOrString
* aValue
,
755 if (aNamespaceID
== kNameSpaceID_None
&&
756 (aName
== nsGkAtoms::accesskey
|| aName
== nsGkAtoms::control
||
757 aName
== nsGkAtoms::value
)) {
758 RegUnRegAccessKey(false);
759 } else if (aNamespaceID
== kNameSpaceID_None
&&
760 (aName
== nsGkAtoms::command
|| aName
== nsGkAtoms::observes
) &&
761 IsInUncomposedDoc()) {
762 // XXX sXBL/XBL2 issue! Owner or current document?
763 // XXX Why does this not also remove broadcast listeners if the
764 // "element" attribute was changed on an <observer>?
765 nsAutoString oldValue
;
766 GetAttr(kNameSpaceID_None
, nsGkAtoms::observes
, oldValue
);
767 if (oldValue
.IsEmpty()) {
768 GetAttr(kNameSpaceID_None
, nsGkAtoms::command
, oldValue
);
771 Document
* doc
= GetUncomposedDoc();
772 if (!oldValue
.IsEmpty() && doc
->HasXULBroadcastManager()) {
773 RefPtr
<XULBroadcastManager
> broadcastManager
=
774 doc
->GetXULBroadcastManager();
775 broadcastManager
->RemoveListener(this);
777 } else if (aNamespaceID
== kNameSpaceID_None
&& aValue
&&
778 mNodeInfo
->Equals(nsGkAtoms::window
) &&
779 aName
== nsGkAtoms::chromemargin
) {
780 nsAttrValue attrValue
;
781 // Make sure the margin format is valid first
782 if (!attrValue
.ParseIntMarginValue(aValue
->String())) {
783 return NS_ERROR_INVALID_ARG
;
785 } else if (aNamespaceID
== kNameSpaceID_None
&&
786 aName
== nsGkAtoms::usercontextid
) {
787 nsAutoString oldValue
;
789 GetAttr(kNameSpaceID_None
, nsGkAtoms::usercontextid
, oldValue
);
790 if (hasAttribute
&& (!aValue
|| !aValue
->String().Equals(oldValue
))) {
791 MOZ_ASSERT(false, "Changing usercontextid is not allowed.");
792 return NS_ERROR_INVALID_ARG
;
796 return nsStyledElement::BeforeSetAttr(aNamespaceID
, aName
, aValue
, aNotify
);
799 nsresult
nsXULElement::AfterSetAttr(int32_t aNamespaceID
, nsAtom
* aName
,
800 const nsAttrValue
* aValue
,
801 const nsAttrValue
* aOldValue
,
802 nsIPrincipal
* aSubjectPrincipal
,
804 if (aNamespaceID
== kNameSpaceID_None
) {
806 AddListenerForAttributeIfNeeded(aName
);
809 if (aName
== nsGkAtoms::accesskey
|| aName
== nsGkAtoms::control
||
810 aName
== nsGkAtoms::value
) {
811 RegUnRegAccessKey(true);
812 } else if (aName
== nsGkAtoms::tooltip
|| aName
== nsGkAtoms::tooltiptext
) {
813 if (!!aValue
!= !!aOldValue
&& IsInComposedDoc() &&
814 !NodeInfo()->Equals(nsGkAtoms::treechildren
)) {
818 RemoveTooltipSupport();
822 Document
* doc
= GetComposedDoc();
823 if (doc
&& doc
->HasXULBroadcastManager()) {
824 RefPtr
<XULBroadcastManager
> broadcastManager
=
825 doc
->GetXULBroadcastManager();
826 broadcastManager
->AttributeChanged(this, aNamespaceID
, aName
);
828 if (doc
&& XULBroadcastManager::MayNeedListener(*this)) {
829 if (!doc
->HasXULBroadcastManager()) {
830 doc
->InitializeXULBroadcastManager();
832 XULBroadcastManager
* broadcastManager
= doc
->GetXULBroadcastManager();
833 broadcastManager
->AddListener(this);
836 // XXX need to check if they're changing an event handler: if
837 // so, then we need to unhook the old one. Or something.
840 return nsStyledElement::AfterSetAttr(aNamespaceID
, aName
, aValue
, aOldValue
,
841 aSubjectPrincipal
, aNotify
);
844 void nsXULElement::AddTooltipSupport() {
845 nsXULTooltipListener
* listener
= nsXULTooltipListener::GetInstance();
850 listener
->AddTooltipSupport(this);
853 void nsXULElement::RemoveTooltipSupport() {
854 nsXULTooltipListener
* listener
= nsXULTooltipListener::GetInstance();
859 listener
->RemoveTooltipSupport(this);
862 bool nsXULElement::ParseAttribute(int32_t aNamespaceID
, nsAtom
* aAttribute
,
863 const nsAString
& aValue
,
864 nsIPrincipal
* aMaybeScriptedPrincipal
,
865 nsAttrValue
& aResult
) {
866 if (aNamespaceID
== kNameSpaceID_None
&& aAttribute
== nsGkAtoms::tabindex
) {
867 return aResult
.ParseIntValue(aValue
);
870 // Parse into a nsAttrValue
871 if (!nsStyledElement::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
872 aMaybeScriptedPrincipal
, aResult
)) {
873 // Fall back to parsing as atom for short values
874 aResult
.ParseStringOrAtom(aValue
);
880 void nsXULElement::DestroyContent() {
881 nsExtendedDOMSlots
* slots
= GetExistingExtendedDOMSlots();
883 slots
->mControllers
= nullptr;
886 nsStyledElement::DestroyContent();
890 void nsXULElement::List(FILE* out
, int32_t aIndent
) const {
891 nsCString
prefix("XUL");
897 nsStyledElement::List(out
, aIndent
, prefix
);
901 bool nsXULElement::IsEventStoppedFromAnonymousScrollbar(EventMessage aMessage
) {
902 return (IsRootOfNativeAnonymousSubtree() &&
903 IsAnyOfXULElements(nsGkAtoms::scrollbar
, nsGkAtoms::scrollcorner
) &&
904 (aMessage
== eMouseClick
|| aMessage
== eMouseDoubleClick
||
905 aMessage
== eXULCommand
|| aMessage
== eContextMenu
||
906 aMessage
== eDragStart
|| aMessage
== eMouseAuxClick
));
909 nsresult
nsXULElement::DispatchXULCommand(const EventChainVisitor
& aVisitor
,
910 nsAutoString
& aCommand
) {
911 // XXX sXBL/XBL2 issue! Owner or current document?
912 nsCOMPtr
<Document
> doc
= GetUncomposedDoc();
913 NS_ENSURE_STATE(doc
);
914 RefPtr
<Element
> commandElt
= doc
->GetElementById(aCommand
);
916 // Create a new command event to dispatch to the element
917 // pointed to by the command attribute. The new event's
918 // sourceEvent will be the original command event that we're
920 RefPtr
<Event
> event
= aVisitor
.mDOMEvent
;
921 uint16_t inputSource
= MouseEvent_Binding::MOZ_SOURCE_UNKNOWN
;
924 NS_ENSURE_STATE(event
->GetOriginalTarget() != commandElt
);
925 RefPtr
<XULCommandEvent
> commandEvent
= event
->AsXULCommandEvent();
927 event
= commandEvent
->GetSourceEvent();
928 inputSource
= commandEvent
->InputSource();
929 button
= commandEvent
->Button();
934 WidgetInputEvent
* orig
= aVisitor
.mEvent
->AsInputEvent();
935 nsContentUtils::DispatchXULCommand(
936 commandElt
, orig
->IsTrusted(), MOZ_KnownLive(aVisitor
.mDOMEvent
),
937 nullptr, orig
->IsControl(), orig
->IsAlt(), orig
->IsShift(),
938 orig
->IsMeta(), inputSource
, button
);
940 NS_WARNING("A XUL element is attached to a command that doesn't exist!\n");
945 void nsXULElement::GetEventTargetParent(EventChainPreVisitor
& aVisitor
) {
946 aVisitor
.mForceContentDispatch
= true; // FIXME! Bug 329119
947 if (IsEventStoppedFromAnonymousScrollbar(aVisitor
.mEvent
->mMessage
)) {
948 // Don't propagate these events from native anonymous scrollbar.
949 aVisitor
.mCanHandle
= true;
950 aVisitor
.SetParentTarget(nullptr, false);
953 if (aVisitor
.mEvent
->mMessage
== eXULCommand
&&
954 aVisitor
.mEvent
->mClass
== eInputEventClass
&&
955 aVisitor
.mEvent
->mOriginalTarget
== static_cast<nsIContent
*>(this) &&
956 !IsXULElement(nsGkAtoms::command
)) {
957 // Check that we really have an xul command event. That will be handled
959 // See if we have a command elt. If so, we execute on the command
960 // instead of on our content element.
961 if (aVisitor
.mDOMEvent
&& aVisitor
.mDOMEvent
->AsXULCommandEvent() &&
962 HasNonEmptyAttr(nsGkAtoms::command
)) {
963 // Stop building the event target chain for the original event.
964 // We don't want it to propagate to any DOM nodes.
965 aVisitor
.mCanHandle
= false;
966 aVisitor
.mAutomaticChromeDispatch
= false;
967 // Dispatch XUL command in PreHandleEvent to prevent it breaks event
968 // target chain creation
969 aVisitor
.mWantsPreHandleEvent
= true;
970 aVisitor
.mItemFlags
|= NS_DISPATCH_XUL_COMMAND
;
975 nsStyledElement::GetEventTargetParent(aVisitor
);
978 nsresult
nsXULElement::PreHandleEvent(EventChainVisitor
& aVisitor
) {
979 if (aVisitor
.mItemFlags
& NS_DISPATCH_XUL_COMMAND
) {
980 nsAutoString command
;
981 GetAttr(kNameSpaceID_None
, nsGkAtoms::command
, command
);
982 MOZ_ASSERT(!command
.IsEmpty());
983 return DispatchXULCommand(aVisitor
, command
);
985 return nsStyledElement::PreHandleEvent(aVisitor
);
988 //----------------------------------------------------------------------
989 // Implementation methods
991 nsChangeHint
nsXULElement::GetAttributeChangeHint(const nsAtom
* aAttribute
,
992 int32_t aModType
) const {
993 if (aAttribute
== nsGkAtoms::value
&&
994 (aModType
== MutationEvent_Binding::REMOVAL
||
995 aModType
== MutationEvent_Binding::ADDITION
) &&
996 IsAnyOfXULElements(nsGkAtoms::label
, nsGkAtoms::description
)) {
997 // Label and description dynamically morph between a normal
998 // block and a cropping single-line XUL text frame. If the
999 // value attribute is being added or removed, then we need to
1000 // return a hint of frame change. (See bugzilla bug 95475 for
1002 return nsChangeHint_ReconstructFrame
;
1005 if (aAttribute
== nsGkAtoms::type
&&
1006 IsAnyOfXULElements(nsGkAtoms::toolbarbutton
, nsGkAtoms::button
)) {
1007 // type=menu switches from a button frame to a menu frame.
1008 return nsChangeHint_ReconstructFrame
;
1011 return nsChangeHint(0);
1014 NS_IMETHODIMP_(bool)
1015 nsXULElement::IsAttributeMapped(const nsAtom
* aAttribute
) const {
1019 nsIControllers
* nsXULElement::GetControllers(ErrorResult
& rv
) {
1020 if (!Controllers()) {
1021 nsExtendedDOMSlots
* slots
= ExtendedDOMSlots();
1023 slots
->mControllers
= new nsXULControllers();
1026 return Controllers();
1029 void nsXULElement::Click(CallerType aCallerType
) {
1030 ClickWithInputSource(MouseEvent_Binding::MOZ_SOURCE_UNKNOWN
,
1031 aCallerType
== CallerType::System
);
1034 void nsXULElement::ClickWithInputSource(uint16_t aInputSource
,
1035 bool aIsTrustedEvent
) {
1036 if (BoolAttrIsTrue(nsGkAtoms::disabled
)) return;
1038 nsCOMPtr
<Document
> doc
= GetComposedDoc(); // Strong just in case
1040 RefPtr
<nsPresContext
> context
= doc
->GetPresContext();
1042 // strong ref to PresContext so events don't destroy it
1044 WidgetMouseEvent
eventDown(aIsTrustedEvent
, eMouseDown
, nullptr,
1045 WidgetMouseEvent::eReal
);
1046 WidgetMouseEvent
eventUp(aIsTrustedEvent
, eMouseUp
, nullptr,
1047 WidgetMouseEvent::eReal
);
1048 WidgetMouseEvent
eventClick(aIsTrustedEvent
, eMouseClick
, nullptr,
1049 WidgetMouseEvent::eReal
);
1050 eventDown
.mInputSource
= eventUp
.mInputSource
= eventClick
.mInputSource
=
1054 nsEventStatus status
= nsEventStatus_eIgnore
;
1055 EventDispatcher::Dispatch(static_cast<nsIContent
*>(this), context
,
1056 &eventDown
, nullptr, &status
);
1059 status
= nsEventStatus_eIgnore
; // reset status
1060 EventDispatcher::Dispatch(static_cast<nsIContent
*>(this), context
,
1061 &eventUp
, nullptr, &status
);
1064 status
= nsEventStatus_eIgnore
; // reset status
1065 EventDispatcher::Dispatch(static_cast<nsIContent
*>(this), context
,
1066 &eventClick
, nullptr, &status
);
1068 // If the click has been prevented, lets skip the command call
1069 // this is how a physical click works
1070 if (status
== nsEventStatus_eConsumeNoDefault
) {
1076 // oncommand is fired when an element is clicked...
1080 void nsXULElement::DoCommand() {
1081 nsCOMPtr
<Document
> doc
= GetComposedDoc(); // strong just in case
1083 RefPtr
<nsXULElement
> self
= this;
1084 nsContentUtils::DispatchXULCommand(self
, true);
1088 bool nsXULElement::IsNodeOfType(uint32_t aFlags
) const { return false; }
1090 nsresult
nsXULElement::AddPopupListener(nsAtom
* aName
) {
1091 // Add a popup listener to the element
1093 (aName
== nsGkAtoms::context
|| aName
== nsGkAtoms::contextmenu
);
1094 uint32_t listenerFlag
= isContext
? XUL_ELEMENT_HAS_CONTENTMENU_LISTENER
1095 : XUL_ELEMENT_HAS_POPUP_LISTENER
;
1097 if (HasFlag(listenerFlag
)) {
1101 nsCOMPtr
<nsIDOMEventListener
> listener
=
1102 new nsXULPopupListener(this, isContext
);
1104 // Add the popup as a listener on this element.
1105 EventListenerManager
* manager
= GetOrCreateListenerManager();
1106 SetFlags(listenerFlag
);
1109 manager
->AddEventListenerByType(listener
, u
"contextmenu"_ns
,
1110 TrustedEventsAtSystemGroupBubble());
1112 manager
->AddEventListenerByType(listener
, u
"mousedown"_ns
,
1113 TrustedEventsAtSystemGroupBubble());
1118 //----------------------------------------------------------------------
1120 nsresult
nsXULElement::MakeHeavyweight(nsXULPrototypeElement
* aPrototype
) {
1127 for (i
= 0; i
< aPrototype
->mAttributes
.Length(); ++i
) {
1128 nsXULPrototypeAttribute
* protoattr
= &aPrototype
->mAttributes
[i
];
1129 nsAttrValue attrValue
;
1131 // Style rules need to be cloned.
1132 if (protoattr
->mValue
.Type() == nsAttrValue::eCSSDeclaration
) {
1133 DeclarationBlock
* decl
= protoattr
->mValue
.GetCSSDeclarationValue();
1134 RefPtr
<DeclarationBlock
> declClone
= decl
->Clone();
1136 nsString stringValue
;
1137 protoattr
->mValue
.ToString(stringValue
);
1139 attrValue
.SetTo(declClone
.forget(), &stringValue
);
1141 attrValue
.SetTo(protoattr
->mValue
);
1145 // XXX we might wanna have a SetAndTakeAttr that takes an nsAttrName
1146 if (protoattr
->mName
.IsAtom()) {
1147 rv
= mAttrs
.SetAndSwapAttr(protoattr
->mName
.Atom(), attrValue
,
1150 rv
= mAttrs
.SetAndSwapAttr(protoattr
->mName
.NodeInfo(), attrValue
,
1153 NS_ENSURE_SUCCESS(rv
, rv
);
1158 bool nsXULElement::BoolAttrIsTrue(nsAtom
* aName
) const {
1159 const nsAttrValue
* attr
= GetAttrInfo(kNameSpaceID_None
, aName
).mValue
;
1161 return attr
&& attr
->Type() == nsAttrValue::eAtom
&&
1162 attr
->GetAtomValue() == nsGkAtoms::_true
;
1165 bool nsXULElement::IsEventAttributeNameInternal(nsAtom
* aName
) {
1166 return nsContentUtils::IsEventAttributeName(aName
, EventNameType_XUL
);
1169 JSObject
* nsXULElement::WrapNode(JSContext
* aCx
,
1170 JS::Handle
<JSObject
*> aGivenProto
) {
1171 return dom::XULElement_Binding::Wrap(aCx
, this, aGivenProto
);
1174 bool nsXULElement::IsInteractiveHTMLContent() const {
1175 return IsXULElement(nsGkAtoms::menupopup
) ||
1176 Element::IsInteractiveHTMLContent();
1179 NS_IMPL_CYCLE_COLLECTION_CLASS(nsXULPrototypeNode
)
1181 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsXULPrototypeNode
)
1182 if (tmp
->mType
== nsXULPrototypeNode::eType_Element
) {
1183 static_cast<nsXULPrototypeElement
*>(tmp
)->Unlink();
1185 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
1186 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsXULPrototypeNode
)
1187 if (tmp
->mType
== nsXULPrototypeNode::eType_Element
) {
1188 nsXULPrototypeElement
* elem
= static_cast<nsXULPrototypeElement
*>(tmp
);
1189 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb
, "mNodeInfo");
1190 cb
.NoteNativeChild(elem
->mNodeInfo
,
1191 NS_CYCLE_COLLECTION_PARTICIPANT(NodeInfo
));
1193 for (i
= 0; i
< elem
->mAttributes
.Length(); ++i
) {
1194 const nsAttrName
& name
= elem
->mAttributes
[i
].mName
;
1195 if (!name
.IsAtom()) {
1196 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb
,
1197 "mAttributes[i].mName.NodeInfo()");
1198 cb
.NoteNativeChild(name
.NodeInfo(),
1199 NS_CYCLE_COLLECTION_PARTICIPANT(NodeInfo
));
1202 ImplCycleCollectionTraverse(cb
, elem
->mChildren
, "mChildren");
1204 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
1205 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsXULPrototypeNode
)
1206 NS_IMPL_CYCLE_COLLECTION_TRACE_END
1208 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsXULPrototypeNode
, AddRef
)
1209 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsXULPrototypeNode
, Release
)
1211 //----------------------------------------------------------------------
1213 // nsXULPrototypeAttribute
1216 nsXULPrototypeAttribute::~nsXULPrototypeAttribute() {
1217 MOZ_COUNT_DTOR(nsXULPrototypeAttribute
);
1220 //----------------------------------------------------------------------
1222 // nsXULPrototypeElement
1225 nsresult
nsXULPrototypeElement::Serialize(
1226 nsIObjectOutputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1227 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1230 // Write basic prototype data
1231 rv
= aStream
->Write32(mType
);
1234 int32_t index
= aNodeInfos
->IndexOf(mNodeInfo
);
1235 NS_ASSERTION(index
>= 0, "unknown mozilla::dom::NodeInfo index");
1236 nsresult tmp
= aStream
->Write32(index
);
1237 if (NS_FAILED(tmp
)) {
1242 tmp
= aStream
->Write32(mAttributes
.Length());
1243 if (NS_FAILED(tmp
)) {
1247 nsAutoString attributeValue
;
1249 for (i
= 0; i
< mAttributes
.Length(); ++i
) {
1250 RefPtr
<mozilla::dom::NodeInfo
> ni
;
1251 if (mAttributes
[i
].mName
.IsAtom()) {
1252 ni
= mNodeInfo
->NodeInfoManager()->GetNodeInfo(
1253 mAttributes
[i
].mName
.Atom(), nullptr, kNameSpaceID_None
,
1254 nsINode::ATTRIBUTE_NODE
);
1255 NS_ASSERTION(ni
, "the nodeinfo should already exist");
1257 ni
= mAttributes
[i
].mName
.NodeInfo();
1260 index
= aNodeInfos
->IndexOf(ni
);
1261 NS_ASSERTION(index
>= 0, "unknown mozilla::dom::NodeInfo index");
1262 tmp
= aStream
->Write32(index
);
1263 if (NS_FAILED(tmp
)) {
1267 mAttributes
[i
].mValue
.ToString(attributeValue
);
1268 tmp
= aStream
->WriteWStringZ(attributeValue
.get());
1269 if (NS_FAILED(tmp
)) {
1274 // Now write children
1275 tmp
= aStream
->Write32(uint32_t(mChildren
.Length()));
1276 if (NS_FAILED(tmp
)) {
1279 for (i
= 0; i
< mChildren
.Length(); i
++) {
1280 nsXULPrototypeNode
* child
= mChildren
[i
].get();
1281 switch (child
->mType
) {
1285 tmp
= child
->Serialize(aStream
, aProtoDoc
, aNodeInfos
);
1286 if (NS_FAILED(tmp
)) {
1291 tmp
= aStream
->Write32(child
->mType
);
1292 if (NS_FAILED(tmp
)) {
1295 nsXULPrototypeScript
* script
=
1296 static_cast<nsXULPrototypeScript
*>(child
);
1298 tmp
= aStream
->Write8(script
->mOutOfLine
);
1299 if (NS_FAILED(tmp
)) {
1302 if (!script
->mOutOfLine
) {
1303 tmp
= script
->Serialize(aStream
, aProtoDoc
, aNodeInfos
);
1304 if (NS_FAILED(tmp
)) {
1308 tmp
= aStream
->WriteCompoundObject(script
->mSrcURI
,
1309 NS_GET_IID(nsIURI
), true);
1310 if (NS_FAILED(tmp
)) {
1314 if (script
->HasStencil()) {
1315 // This may return NS_OK without muxing script->mSrcURI's
1316 // data into the cache file, in the case where that
1317 // muxed document is already there (written by a prior
1318 // session, or by an earlier cache episode during this
1320 tmp
= script
->SerializeOutOfLine(aStream
, aProtoDoc
);
1321 if (NS_FAILED(tmp
)) {
1333 nsresult
nsXULPrototypeElement::Deserialize(
1334 nsIObjectInputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1335 nsIURI
* aDocumentURI
,
1336 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1337 MOZ_ASSERT(aNodeInfos
, "missing nodeinfo array");
1340 uint32_t number
= 0;
1341 nsresult rv
= aStream
->Read32(&number
);
1342 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1343 mNodeInfo
= aNodeInfos
->SafeElementAt(number
, nullptr);
1345 return NS_ERROR_UNEXPECTED
;
1349 rv
= aStream
->Read32(&number
);
1350 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1351 int32_t attributes
= int32_t(number
);
1353 if (attributes
> 0) {
1354 mAttributes
.AppendElements(attributes
);
1356 nsAutoString attributeValue
;
1357 for (size_t i
= 0; i
< mAttributes
.Length(); ++i
) {
1358 rv
= aStream
->Read32(&number
);
1359 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1360 mozilla::dom::NodeInfo
* ni
= aNodeInfos
->SafeElementAt(number
, nullptr);
1362 return NS_ERROR_UNEXPECTED
;
1365 mAttributes
[i
].mName
.SetTo(ni
);
1367 rv
= aStream
->ReadString(attributeValue
);
1368 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1369 rv
= SetAttrAt(i
, attributeValue
, aDocumentURI
);
1370 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1374 rv
= aStream
->Read32(&number
);
1375 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1376 uint32_t numChildren
= int32_t(number
);
1378 if (numChildren
> 0) {
1379 if (!mChildren
.SetCapacity(numChildren
, fallible
)) {
1380 return NS_ERROR_OUT_OF_MEMORY
;
1383 for (uint32_t i
= 0; i
< numChildren
; i
++) {
1384 rv
= aStream
->Read32(&number
);
1385 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1386 Type childType
= (Type
)number
;
1388 RefPtr
<nsXULPrototypeNode
> child
;
1390 switch (childType
) {
1392 child
= new nsXULPrototypeElement();
1393 rv
= child
->Deserialize(aStream
, aProtoDoc
, aDocumentURI
, aNodeInfos
);
1394 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1397 child
= new nsXULPrototypeText();
1398 rv
= child
->Deserialize(aStream
, aProtoDoc
, aDocumentURI
, aNodeInfos
);
1399 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1402 child
= new nsXULPrototypePI();
1403 rv
= child
->Deserialize(aStream
, aProtoDoc
, aDocumentURI
, aNodeInfos
);
1404 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1406 case eType_Script
: {
1407 // language version/options obtained during deserialization.
1408 RefPtr
<nsXULPrototypeScript
> script
= new nsXULPrototypeScript(0);
1410 rv
= aStream
->ReadBoolean(&script
->mOutOfLine
);
1411 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1412 if (!script
->mOutOfLine
) {
1413 rv
= script
->Deserialize(aStream
, aProtoDoc
, aDocumentURI
,
1415 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1417 nsCOMPtr
<nsISupports
> supports
;
1418 rv
= aStream
->ReadObject(true, getter_AddRefs(supports
));
1419 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1420 script
->mSrcURI
= do_QueryInterface(supports
);
1422 rv
= script
->DeserializeOutOfLine(aStream
, aProtoDoc
);
1423 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1426 child
= std::move(script
);
1430 MOZ_ASSERT(false, "Unexpected child type!");
1431 return NS_ERROR_UNEXPECTED
;
1434 MOZ_ASSERT(child
, "Don't append null to mChildren");
1435 MOZ_ASSERT(child
->mType
== childType
);
1436 mChildren
.AppendElement(child
);
1438 // Oh dear. Something failed during the deserialization.
1439 // We don't know what. But likely consequences of failed
1440 // deserializations included calls to |AbortCaching| which
1441 // shuts down the cache and closes our streams.
1442 // If that happens, next time through this loop, we die a messy
1443 // death. So, let's just fail now, and propagate that failure
1444 // upward so that the ChromeProtocolHandler knows it can't use
1445 // a cached chrome channel for this.
1446 if (NS_WARN_IF(NS_FAILED(rv
))) return rv
;
1453 nsresult
nsXULPrototypeElement::SetAttrAt(uint32_t aPos
,
1454 const nsAString
& aValue
,
1455 nsIURI
* aDocumentURI
) {
1456 MOZ_ASSERT(aPos
< mAttributes
.Length(), "out-of-bounds");
1459 // This code is largely duplicated in nsXULElement::SetAttr.
1460 // Any changes should be made to both functions.
1462 if (!mNodeInfo
->NamespaceEquals(kNameSpaceID_XUL
)) {
1463 if (mNodeInfo
->NamespaceEquals(kNameSpaceID_XHTML
) &&
1464 mAttributes
[aPos
].mName
.Equals(nsGkAtoms::is
)) {
1465 // We still care about the is attribute set on HTML elements.
1466 mAttributes
[aPos
].mValue
.ParseAtom(aValue
);
1467 mIsAtom
= mAttributes
[aPos
].mValue
.GetAtomValue();
1472 mAttributes
[aPos
].mValue
.ParseStringOrAtom(aValue
);
1477 if (mAttributes
[aPos
].mName
.Equals(nsGkAtoms::id
) && !aValue
.IsEmpty()) {
1478 mHasIdAttribute
= true;
1479 // Store id as atom.
1480 // id="" means that the element has no id. Not that it has
1481 // emptystring as id.
1482 mAttributes
[aPos
].mValue
.ParseAtom(aValue
);
1485 } else if (mAttributes
[aPos
].mName
.Equals(nsGkAtoms::is
)) {
1486 // Store is as atom.
1487 mAttributes
[aPos
].mValue
.ParseAtom(aValue
);
1488 mIsAtom
= mAttributes
[aPos
].mValue
.GetAtomValue();
1491 } else if (mAttributes
[aPos
].mName
.Equals(nsGkAtoms::_class
)) {
1492 mHasClassAttribute
= true;
1493 // Compute the element's class list
1494 mAttributes
[aPos
].mValue
.ParseAtomArray(aValue
);
1497 } else if (mAttributes
[aPos
].mName
.Equals(nsGkAtoms::style
)) {
1498 mHasStyleAttribute
= true;
1499 // Parse the element's 'style' attribute
1501 // This is basically duplicating what nsINode::NodePrincipal() does
1502 nsIPrincipal
* principal
= mNodeInfo
->NodeInfoManager()->DocumentPrincipal();
1503 // XXX Get correct Base URI (need GetBaseURI on *prototype* element)
1504 // TODO: If we implement Content Security Policy for chrome documents
1505 // as has been discussed, the CSP should be checked here to see if
1506 // inline styles are allowed to be applied.
1507 // XXX No specific specs talk about xul and referrer policy, pass Unset
1509 MakeRefPtr
<ReferrerInfo
>(aDocumentURI
, ReferrerPolicy::_empty
);
1510 auto data
= MakeRefPtr
<URLExtraData
>(aDocumentURI
, referrerInfo
, principal
);
1511 RefPtr
<DeclarationBlock
> declaration
= DeclarationBlock::FromCssText(
1512 aValue
, data
, eCompatibility_FullStandards
, nullptr,
1513 StyleCssRuleType::Style
);
1515 mAttributes
[aPos
].mValue
.SetTo(declaration
.forget(), &aValue
);
1519 // Don't abort if parsing failed, it could just be malformed css.
1520 } else if (mAttributes
[aPos
].mName
.Equals(nsGkAtoms::tabindex
)) {
1521 mAttributes
[aPos
].mValue
.ParseIntValue(aValue
);
1526 mAttributes
[aPos
].mValue
.ParseStringOrAtom(aValue
);
1531 void nsXULPrototypeElement::Unlink() {
1532 mAttributes
.Clear();
1536 //----------------------------------------------------------------------
1538 // nsXULPrototypeScript
1541 nsXULPrototypeScript::nsXULPrototypeScript(uint32_t aLineNo
)
1542 : nsXULPrototypeNode(eType_Script
),
1546 mSrcLoadWaiters(nullptr),
1547 mStencil(nullptr) {}
1549 static nsresult
WriteStencil(nsIObjectOutputStream
* aStream
, JSContext
* aCx
,
1550 JS::Stencil
* aStencil
) {
1551 JS::TranscodeBuffer buffer
;
1552 JS::TranscodeResult code
;
1553 code
= JS::EncodeStencil(aCx
, aStencil
, buffer
);
1555 if (code
!= JS::TranscodeResult::Ok
) {
1556 if (code
== JS::TranscodeResult::Throw
) {
1557 JS_ClearPendingException(aCx
);
1558 return NS_ERROR_OUT_OF_MEMORY
;
1561 MOZ_ASSERT(IsTranscodeFailureResult(code
));
1562 return NS_ERROR_FAILURE
;
1565 size_t size
= buffer
.length();
1566 if (size
> UINT32_MAX
) {
1567 return NS_ERROR_FAILURE
;
1569 nsresult rv
= aStream
->Write32(size
);
1570 if (NS_SUCCEEDED(rv
)) {
1571 // Ideally we could just pass "buffer" here. See bug 1566574.
1572 rv
= aStream
->WriteBytes(Span(buffer
.begin(), size
));
1578 static nsresult
ReadStencil(nsIObjectInputStream
* aStream
, JSContext
* aCx
,
1579 const JS::DecodeOptions
& aOptions
,
1580 JS::Stencil
** aStencilOut
) {
1581 // We don't serialize mutedError-ness of scripts, which is fine as long as
1582 // we only serialize system and XUL-y things. We can detect this by checking
1583 // where the caller wants us to deserialize.
1585 // CompilationScope() could theoretically GC, so get that out of the way
1586 // before comparing to the cx global.
1587 JSObject
* loaderGlobal
= xpc::CompilationScope();
1588 MOZ_RELEASE_ASSERT(nsContentUtils::IsSystemCaller(aCx
) ||
1589 JS::CurrentGlobalOrNull(aCx
) == loaderGlobal
);
1592 nsresult rv
= aStream
->Read32(&size
);
1593 if (NS_FAILED(rv
)) {
1598 rv
= aStream
->ReadBytes(size
, &data
);
1599 if (NS_FAILED(rv
)) {
1603 // The decoded stencil shouldn't borrow from the XDR buffer.
1604 MOZ_ASSERT(!aOptions
.borrowBuffer
);
1605 auto cleanupData
= MakeScopeExit([&]() { free(data
); });
1607 JS::TranscodeRange
range(reinterpret_cast<uint8_t*>(data
), size
);
1610 JS::TranscodeResult code
;
1611 RefPtr
<JS::Stencil
> stencil
;
1612 code
= JS::DecodeStencil(aCx
, aOptions
, range
, getter_AddRefs(stencil
));
1613 if (code
!= JS::TranscodeResult::Ok
) {
1614 if (code
== JS::TranscodeResult::Throw
) {
1615 JS_ClearPendingException(aCx
);
1616 return NS_ERROR_OUT_OF_MEMORY
;
1619 MOZ_ASSERT(IsTranscodeFailureResult(code
));
1620 return NS_ERROR_FAILURE
;
1623 stencil
.forget(aStencilOut
);
1629 void nsXULPrototypeScript::FillCompileOptions(JS::CompileOptions
& options
) {
1630 // If the script was inline, tell the JS parser to save source for
1631 // Function.prototype.toSource(). If it's out of line, we retrieve the
1632 // source from the files on demand.
1633 options
.setSourceIsLazy(mOutOfLine
);
1636 nsresult
nsXULPrototypeScript::Serialize(
1637 nsIObjectOutputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1638 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1639 NS_ENSURE_TRUE(aProtoDoc
, NS_ERROR_UNEXPECTED
);
1642 if (!jsapi
.Init(xpc::CompilationScope())) {
1643 return NS_ERROR_UNEXPECTED
;
1646 NS_ASSERTION(!mSrcLoading
|| mSrcLoadWaiters
!= nullptr || !mStencil
,
1647 "script source still loading when serializing?!");
1648 if (!mStencil
) return NS_ERROR_FAILURE
;
1650 // Write basic prototype data
1652 rv
= aStream
->Write32(mLineNo
);
1653 if (NS_FAILED(rv
)) return rv
;
1655 JSContext
* cx
= jsapi
.cx();
1656 MOZ_ASSERT(xpc::CompilationScope() == JS::CurrentGlobalOrNull(cx
));
1658 return WriteStencil(aStream
, cx
, mStencil
);
1661 nsresult
nsXULPrototypeScript::SerializeOutOfLine(
1662 nsIObjectOutputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
) {
1663 if (!mSrcURI
->SchemeIs("chrome"))
1664 // Don't cache scripts that don't come from chrome uris.
1665 return NS_ERROR_NOT_IMPLEMENTED
;
1667 nsXULPrototypeCache
* cache
= nsXULPrototypeCache::GetInstance();
1668 if (!cache
) return NS_ERROR_OUT_OF_MEMORY
;
1670 NS_ASSERTION(cache
->IsEnabled(),
1671 "writing to the cache file, but the XUL cache is off?");
1673 cache
->HasScript(mSrcURI
, &exists
);
1675 /* return will be NS_OK from GetAsciiSpec.
1676 * that makes no sense.
1677 * nor does returning NS_OK from HasMuxedDocument.
1678 * XXX return something meaningful.
1680 if (exists
) return NS_OK
;
1682 nsCOMPtr
<nsIObjectOutputStream
> oos
;
1683 nsresult rv
= cache
->GetScriptOutputStream(mSrcURI
, getter_AddRefs(oos
));
1684 NS_ENSURE_SUCCESS(rv
, rv
);
1686 nsresult tmp
= Serialize(oos
, aProtoDoc
, nullptr);
1687 if (NS_FAILED(tmp
)) {
1690 tmp
= cache
->FinishScriptOutputStream(mSrcURI
);
1691 if (NS_FAILED(tmp
)) {
1695 if (NS_FAILED(rv
)) cache
->AbortCaching();
1699 nsresult
nsXULPrototypeScript::Deserialize(
1700 nsIObjectInputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1701 nsIURI
* aDocumentURI
,
1702 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1704 NS_ASSERTION(!mSrcLoading
|| mSrcLoadWaiters
!= nullptr || !mStencil
,
1705 "prototype script not well-initialized when deserializing?!");
1707 // Read basic prototype data
1708 rv
= aStream
->Read32(&mLineNo
);
1709 if (NS_FAILED(rv
)) return rv
;
1712 if (!jsapi
.Init(xpc::CompilationScope())) {
1713 return NS_ERROR_UNEXPECTED
;
1715 JSContext
* cx
= jsapi
.cx();
1717 JS::DecodeOptions options
;
1718 RefPtr
<JS::Stencil
> newStencil
;
1719 rv
= ReadStencil(aStream
, cx
, options
, getter_AddRefs(newStencil
));
1720 NS_ENSURE_SUCCESS(rv
, rv
);
1725 nsresult
nsXULPrototypeScript::DeserializeOutOfLine(
1726 nsIObjectInputStream
* aInput
, nsXULPrototypeDocument
* aProtoDoc
) {
1727 // Keep track of failure via rv, so we can
1728 // AbortCaching if things look bad.
1729 nsresult rv
= NS_OK
;
1730 nsXULPrototypeCache
* cache
= nsXULPrototypeCache::GetInstance();
1732 nsCOMPtr
<nsIObjectInputStream
> objectInput
= aInput
;
1734 bool useXULCache
= true;
1736 // NB: we must check the XUL script cache early, to avoid
1737 // multiple deserialization attempts for a given script.
1738 // Note that PrototypeDocumentContentSink::LoadScript
1739 // checks the XUL script cache too, in order to handle the
1740 // serialization case.
1742 // We need do this only for <script src='strres.js'> and the
1743 // like, i.e., out-of-line scripts that are included by several
1744 // different XUL documents stored in the cache file.
1745 useXULCache
= cache
->IsEnabled();
1748 RefPtr
<JS::Stencil
> newStencil
= cache
->GetStencil(mSrcURI
);
1757 rv
= cache
->GetScriptInputStream(mSrcURI
, getter_AddRefs(objectInput
));
1759 // If !mSrcURI, we have an inline script. We shouldn't have
1760 // to do anything else in that case, I think.
1762 // We do reflect errors into rv, but our caller may want to
1763 // ignore our return value, because mStencil will be null
1764 // after any error, and that suffices to cause the script to
1765 // be reloaded (from the src= URI, if any) and recompiled.
1766 // We're better off slow-loading than bailing out due to a
1768 if (NS_SUCCEEDED(rv
))
1769 rv
= Deserialize(objectInput
, aProtoDoc
, nullptr, nullptr);
1771 if (NS_SUCCEEDED(rv
)) {
1772 if (useXULCache
&& mSrcURI
&& mSrcURI
->SchemeIs("chrome")) {
1773 cache
->PutStencil(mSrcURI
, GetStencil());
1775 cache
->FinishScriptInputStream(mSrcURI
);
1777 // If mSrcURI is not in the cache,
1778 // rv will be NS_ERROR_NOT_AVAILABLE and we'll try to
1779 // update the cache file to hold a serialization of
1780 // this script, once it has finished loading.
1781 if (rv
!= NS_ERROR_NOT_AVAILABLE
) cache
->AbortCaching();
1788 class NotifyOffThreadScriptCompletedRunnable
: public Runnable
{
1789 // An array of all outstanding script receivers. All reference counting of
1790 // these objects happens on the main thread. When we return to the main
1791 // thread from script compilation we make sure our receiver is still in
1792 // this array (still alive) before proceeding. This array is cleared during
1793 // shutdown, potentially before all outstanding script compilations have
1794 // finished. We do not need to worry about pointer replay here, because
1795 // a) we should not be starting script compilation after clearing this
1796 // array and b) in all other cases the receiver will still be alive.
1797 static StaticAutoPtr
<nsTArray
<nsCOMPtr
<nsIOffThreadScriptReceiver
>>>
1799 static bool sSetupClearOnShutdown
;
1801 nsIOffThreadScriptReceiver
* mReceiver
;
1802 JS::OffThreadToken
* mToken
;
1805 NotifyOffThreadScriptCompletedRunnable(nsIOffThreadScriptReceiver
* aReceiver
,
1806 JS::OffThreadToken
* aToken
)
1807 : mozilla::Runnable("NotifyOffThreadScriptCompletedRunnable"),
1808 mReceiver(aReceiver
),
1811 static void NoteReceiver(nsIOffThreadScriptReceiver
* aReceiver
) {
1812 if (!sSetupClearOnShutdown
) {
1813 ClearOnShutdown(&sReceivers
);
1814 sSetupClearOnShutdown
= true;
1815 sReceivers
= new nsTArray
<nsCOMPtr
<nsIOffThreadScriptReceiver
>>();
1818 // If we ever crash here, it's because we tried to lazy compile script
1819 // too late in shutdown.
1820 sReceivers
->AppendElement(aReceiver
);
1826 StaticAutoPtr
<nsTArray
<nsCOMPtr
<nsIOffThreadScriptReceiver
>>>
1827 NotifyOffThreadScriptCompletedRunnable::sReceivers
;
1828 bool NotifyOffThreadScriptCompletedRunnable::sSetupClearOnShutdown
= false;
1831 NotifyOffThreadScriptCompletedRunnable::Run() {
1832 MOZ_ASSERT(NS_IsMainThread());
1834 RefPtr
<JS::Stencil
> stencil
;
1837 if (!jsapi
.Init(xpc::CompilationScope())) {
1838 // Now what? I guess we just leak... this should probably never
1840 return NS_ERROR_UNEXPECTED
;
1842 JSContext
* cx
= jsapi
.cx();
1843 stencil
= JS::FinishCompileToStencilOffThread(cx
, mToken
);
1847 // We've already shut down.
1851 auto index
= sReceivers
->IndexOf(mReceiver
);
1852 MOZ_RELEASE_ASSERT(index
!= sReceivers
->NoIndex
);
1853 nsCOMPtr
<nsIOffThreadScriptReceiver
> receiver
=
1854 std::move((*sReceivers
)[index
]);
1855 sReceivers
->RemoveElementAt(index
);
1857 return receiver
->OnScriptCompileComplete(stencil
,
1858 stencil
? NS_OK
: NS_ERROR_FAILURE
);
1861 static void OffThreadScriptReceiverCallback(JS::OffThreadToken
* aToken
,
1862 void* aCallbackData
) {
1863 // Be careful not to adjust the refcount on the receiver, as this callback
1864 // may be invoked off the main thread.
1865 nsIOffThreadScriptReceiver
* aReceiver
=
1866 static_cast<nsIOffThreadScriptReceiver
*>(aCallbackData
);
1867 RefPtr
<NotifyOffThreadScriptCompletedRunnable
> notify
=
1868 new NotifyOffThreadScriptCompletedRunnable(aReceiver
, aToken
);
1869 NS_DispatchToMainThread(notify
);
1872 nsresult
nsXULPrototypeScript::Compile(
1873 const char16_t
* aText
, size_t aTextLength
, JS::SourceOwnership aOwnership
,
1874 nsIURI
* aURI
, uint32_t aLineNo
, Document
* aDocument
,
1875 nsIOffThreadScriptReceiver
* aOffThreadReceiver
/* = nullptr */) {
1876 // We'll compile the script in the compilation scope.
1878 if (!jsapi
.Init(xpc::CompilationScope())) {
1879 if (aOwnership
== JS::SourceOwnership::TakeOwnership
) {
1880 // In this early-exit case -- before the |srcBuf.init| call will
1881 // own |aText| -- we must relinquish ownership manually.
1882 js_free(const_cast<char16_t
*>(aText
));
1885 return NS_ERROR_UNEXPECTED
;
1887 JSContext
* cx
= jsapi
.cx();
1889 JS::SourceText
<char16_t
> srcBuf
;
1890 if (NS_WARN_IF(!srcBuf
.init(cx
, aText
, aTextLength
, aOwnership
))) {
1891 return NS_ERROR_FAILURE
;
1894 nsAutoCString urlspec
;
1895 nsresult rv
= aURI
->GetSpec(urlspec
);
1896 if (NS_WARN_IF(NS_FAILED(rv
))) {
1900 // Ok, compile it to create a prototype script object!
1901 JS::CompileOptions
options(cx
);
1902 FillCompileOptions(options
);
1903 options
.setIntroductionType(mOutOfLine
? "srcScript" : "inlineScript")
1904 .setFileAndLine(urlspec
.get(), mOutOfLine
? 1 : aLineNo
);
1906 JS::Rooted
<JSObject
*> scope(cx
, JS::CurrentGlobalOrNull(cx
));
1908 if (aOffThreadReceiver
&& JS::CanCompileOffThread(cx
, options
, aTextLength
)) {
1909 if (!JS::CompileToStencilOffThread(
1910 cx
, options
, srcBuf
, OffThreadScriptReceiverCallback
,
1911 static_cast<void*>(aOffThreadReceiver
))) {
1912 JS_ClearPendingException(cx
);
1913 return NS_ERROR_OUT_OF_MEMORY
;
1915 NotifyOffThreadScriptCompletedRunnable::NoteReceiver(aOffThreadReceiver
);
1917 RefPtr
<JS::Stencil
> stencil
=
1918 JS::CompileGlobalScriptToStencil(cx
, options
, srcBuf
);
1920 return NS_ERROR_OUT_OF_MEMORY
;
1927 nsresult
nsXULPrototypeScript::InstantiateScript(
1928 JSContext
* aCx
, JS::MutableHandleScript aScript
) {
1929 MOZ_ASSERT(mStencil
);
1931 JS::CompileOptions
options(aCx
);
1932 FillCompileOptions(options
);
1933 JS::InstantiateOptions
instantiateOptions(options
);
1934 aScript
.set(JS::InstantiateGlobalStencil(aCx
, instantiateOptions
, mStencil
));
1936 JS_ClearPendingException(aCx
);
1937 return NS_ERROR_OUT_OF_MEMORY
;
1943 void nsXULPrototypeScript::Set(JS::Stencil
* aStencil
) { mStencil
= aStencil
; }
1945 //----------------------------------------------------------------------
1947 // nsXULPrototypeText
1950 nsresult
nsXULPrototypeText::Serialize(
1951 nsIObjectOutputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1952 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1955 // Write basic prototype data
1956 rv
= aStream
->Write32(mType
);
1958 nsresult tmp
= aStream
->WriteWStringZ(mValue
.get());
1959 if (NS_FAILED(tmp
)) {
1966 nsresult
nsXULPrototypeText::Deserialize(
1967 nsIObjectInputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1968 nsIURI
* aDocumentURI
,
1969 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1970 nsresult rv
= aStream
->ReadString(mValue
);
1971 if (NS_WARN_IF(NS_FAILED(rv
))) {
1977 //----------------------------------------------------------------------
1982 nsresult
nsXULPrototypePI::Serialize(
1983 nsIObjectOutputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
1984 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
1987 // Write basic prototype data
1988 rv
= aStream
->Write32(mType
);
1990 nsresult tmp
= aStream
->WriteWStringZ(mTarget
.get());
1991 if (NS_FAILED(tmp
)) {
1994 tmp
= aStream
->WriteWStringZ(mData
.get());
1995 if (NS_FAILED(tmp
)) {
2002 nsresult
nsXULPrototypePI::Deserialize(
2003 nsIObjectInputStream
* aStream
, nsXULPrototypeDocument
* aProtoDoc
,
2004 nsIURI
* aDocumentURI
,
2005 const nsTArray
<RefPtr
<mozilla::dom::NodeInfo
>>* aNodeInfos
) {
2008 rv
= aStream
->ReadString(mTarget
);
2009 if (NS_FAILED(rv
)) return rv
;
2010 rv
= aStream
->ReadString(mData
);
2011 if (NS_FAILED(rv
)) return rv
;