Bug 1728955: part 3) Add logging to `nsBaseClipboard`. r=masayuki
[gecko.git] / dom / base / BindContext.h
blob97908eec4e8119ef065c74869bdaeb838d624244
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* State that is passed down to BindToTree. */
9 #ifndef mozilla_dom_BindContext_h__
10 #define mozilla_dom_BindContext_h__
12 #include "mozilla/Attributes.h"
13 #include "mozilla/dom/Element.h"
14 #include "mozilla/dom/ShadowRoot.h"
15 #include "nsINode.h"
17 namespace mozilla {
18 namespace dom {
20 class Document;
22 struct MOZ_STACK_CLASS BindContext final {
23 // The document that owns the tree we're getting bound to.
25 // This is mostly an optimization to avoid silly pointer-chases to get the
26 // OwnerDoc().
27 Document& OwnerDoc() const { return mDoc; }
29 // Whether we're getting connected.
31 // https://dom.spec.whatwg.org/#connected
32 bool InComposedDoc() const { return mInComposedDoc; }
34 // Whether we're getting bound to the document tree.
36 // https://dom.spec.whatwg.org/#in-a-document-tree
37 bool InUncomposedDoc() const { return mInUncomposedDoc; }
39 Document* GetComposedDoc() const { return mInComposedDoc ? &mDoc : nullptr; }
41 Document* GetUncomposedDoc() const {
42 return mInUncomposedDoc ? &mDoc : nullptr;
45 // Whether our subtree root is changing as a result of this operation.
46 bool SubtreeRootChanges() const { return mSubtreeRootChanges; }
48 // Autofocus is allowed only if the is in the same origin as the top level
49 // document.
50 // https://html.spec.whatwg.org/multipage/interaction.html#the-autofocus-attribute:same-origin
51 // In addition, the document should not be already loaded and the
52 // "browser.autofocus" preference should be 'true'.
53 bool AllowsAutoFocus() const;
55 // This constructor should be used for regular appends to content.
56 explicit BindContext(nsINode& aParent)
57 : mDoc(*aParent.OwnerDoc()),
58 mInComposedDoc(aParent.IsInComposedDoc()),
59 mInUncomposedDoc(aParent.IsInUncomposedDoc()),
60 mSubtreeRootChanges(true) {}
62 // When re-binding a shadow host into a tree, we re-bind all the shadow tree
63 // from the root. In that case, the shadow tree contents remain within the
64 // same subtree root. So children should avoid doing silly things like adding
65 // themselves to the ShadowRoot's id table twice or what not.
67 // This constructor is only meant to be used in that situation.
68 explicit BindContext(ShadowRoot& aShadowRoot)
69 : mDoc(*aShadowRoot.OwnerDoc()),
70 mInComposedDoc(aShadowRoot.IsInComposedDoc()),
71 mInUncomposedDoc(false),
72 mSubtreeRootChanges(false) {}
74 // This constructor is meant to be used when inserting native-anonymous
75 // children into a subtree.
76 enum ForNativeAnonymous { ForNativeAnonymous };
77 BindContext(Element& aParentElement, enum ForNativeAnonymous)
78 : mDoc(*aParentElement.OwnerDoc()),
79 mInComposedDoc(aParentElement.IsInComposedDoc()),
80 mInUncomposedDoc(aParentElement.IsInUncomposedDoc()),
81 mSubtreeRootChanges(true) {
82 MOZ_ASSERT(mInComposedDoc, "Binding NAC in a disconnected subtree?");
85 private:
86 // Returns true iff the document is in the same origin as the top level
87 // document.
88 bool IsSameOriginAsTop() const;
90 Document& mDoc;
92 const bool mInComposedDoc;
93 const bool mInUncomposedDoc;
95 // Whether the bind operation will change the subtree root of the content
96 // we're binding.
97 const bool mSubtreeRootChanges;
100 } // namespace dom
101 } // namespace mozilla
103 #endif