DocGroup.h (4594B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef DocGroup_h 8 #define DocGroup_h 9 10 #include "mozilla/RefPtr.h" 11 #include "mozilla/dom/BrowsingContextGroup.h" 12 #include "mozilla/dom/HTMLSlotElement.h" 13 #include "nsIPrincipal.h" 14 #include "nsISupportsImpl.h" 15 #include "nsString.h" 16 #include "nsTHashSet.h" 17 #include "nsThreadUtils.h" 18 19 namespace mozilla { 20 class AbstractThread; 21 namespace dom { 22 23 class CustomElementReactionsStack; 24 class JSExecutionManager; 25 26 // DocGroup is the Gecko object for a "Similar-origin Window Agent" (the 27 // window-global component of an "Agent Cluster"). 28 // https://html.spec.whatwg.org/multipage/webappapis.html#similar-origin-window-agent 29 // 30 // A DocGroup is shared between a series of window globals which are reachable 31 // from one-another (e.g. through `window.opener`, `window.parent` or 32 // `window.frames`), and are able to synchronously communicate with one-another, 33 // (either due to being same-origin, or by setting `document.domain`). 34 // 35 // NOTE: Similar to how the principal for a global is stored on a Document, the 36 // DocGroup for a window global is also attached to the corresponding Document 37 // object. This is required for certain features (such as the ArenaAllocator) 38 // which require the DocGroup before the nsGlobalWindowInner has been created. 39 // 40 // NOTE: DocGroup is not the source of truth for synchronous script access. 41 // Non-window globals, such as extension globals and system JS, may have 42 // synchronous access yet not be part of the DocGroup. The DocGroup should, 43 // however, align with web-visible synchronous script access boundaries. 44 class DocGroup final { 45 public: 46 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DocGroup) 47 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(DocGroup) 48 49 static already_AddRefed<DocGroup> Create( 50 BrowsingContextGroup* aBrowsingContextGroup, const DocGroupKey& aKey); 51 52 void AssertMatches(const Document* aDocument) const; 53 54 const DocGroupKey& GetKey() const { return mKey; } 55 56 bool IsOriginKeyed() const { return mKey.mOriginKeyed; } 57 58 JSExecutionManager* GetExecutionManager() const { return mExecutionManager; } 59 void SetExecutionManager(JSExecutionManager*); 60 61 BrowsingContextGroup* GetBrowsingContextGroup() const { 62 return mBrowsingContextGroup; 63 } 64 65 mozilla::dom::DOMArena* ArenaAllocator() { return mArena; } 66 67 mozilla::dom::CustomElementReactionsStack* CustomElementReactionsStack(); 68 69 // Adding documents to a DocGroup should be done through 70 // BrowsingContextGroup::AddDocument (which in turn calls 71 // DocGroup::AddDocument). 72 void AddDocument(Document* aDocument); 73 74 // Removing documents from a DocGroup should be done through 75 // BrowsingContextGroup::RemoveDocument(which in turn calls 76 // DocGroup::RemoveDocument). 77 void RemoveDocument(Document* aDocument); 78 79 // Return a pointer that can be continually checked to see if access to this 80 // DocGroup is valid. This pointer should live at least as long as the 81 // DocGroup. 82 bool* GetValidAccessPtr(); 83 84 // Append aSlot to the list of signal slot list, and queue a mutation observer 85 // microtask. 86 void SignalSlotChange(HTMLSlotElement& aSlot); 87 88 nsTArray<RefPtr<HTMLSlotElement>> MoveSignalSlotList(); 89 90 // List of DocGroups that has non-empty signal slot list. 91 static AutoTArray<RefPtr<DocGroup>, 2>* sPendingDocGroups; 92 93 // Returns true if any of its documents are active but not in the bfcache. 94 bool IsActive() const; 95 96 const nsID& AgentClusterId() const { return mAgentClusterId; } 97 98 bool IsEmpty() const { return mDocuments.IsEmpty(); } 99 100 private: 101 DocGroup(BrowsingContextGroup* aBrowsingContextGroup, 102 const DocGroupKey& aKey); 103 104 ~DocGroup(); 105 106 DocGroupKey mKey; 107 108 nsTArray<Document*> mDocuments; 109 RefPtr<mozilla::dom::CustomElementReactionsStack> mReactionsStack; 110 nsTArray<RefPtr<HTMLSlotElement>> mSignalSlotList; 111 RefPtr<BrowsingContextGroup> mBrowsingContextGroup; 112 113 // non-null if the JS execution for this docgroup is regulated with regards 114 // to worker threads. This should only be used when we are forcing serialized 115 // SAB access. 116 RefPtr<JSExecutionManager> mExecutionManager; 117 118 // Each DocGroup has a persisted agent cluster ID. 119 const nsID mAgentClusterId; 120 121 RefPtr<mozilla::dom::DOMArena> mArena; 122 }; 123 124 } // namespace dom 125 } // namespace mozilla 126 127 #endif // defined(DocGroup_h)