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/. */
10 #include "nsISupportsImpl.h"
11 #include "nsIPrincipal.h"
12 #include "nsThreadUtils.h"
13 #include "nsTHashSet.h"
15 #include "mozilla/RefPtr.h"
16 #include "mozilla/dom/BrowsingContextGroup.h"
17 #include "mozilla/dom/HTMLSlotElement.h"
23 class CustomElementReactionsStack
;
24 class JSExecutionManager
;
26 // Two browsing contexts are considered "related" if they are reachable from one
27 // another through window.opener, window.parent, or window.frames. This is the
28 // spec concept of a browsing context group.
30 // Two browsing contexts are considered "similar-origin" if they can be made to
31 // have the same origin by setting document.domain. This is the spec concept of
32 // a "unit of similar-origin related browsing contexts"
34 // A BrowsingContextGroup is a set of browsing contexts which are all
35 // "related". Within a BrowsingContextGroup, browsing contexts are
36 // broken into "similar-origin" DocGroups. A DocGroup is a member
37 // of exactly one BrowsingContextGroup.
38 class DocGroup final
{
40 typedef nsTArray
<Document
*>::iterator Iterator
;
42 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DocGroup
)
43 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(DocGroup
)
45 static already_AddRefed
<DocGroup
> Create(
46 BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
48 // Returns NS_ERROR_FAILURE and sets |aString| to an empty string if the TLD
49 // service isn't available. Returns NS_OK on success, but may still set
50 // |aString| may still be set to an empty string.
51 [[nodiscard
]] static nsresult
GetKey(nsIPrincipal
* aPrincipal
,
52 bool aCrossOriginIsolated
,
55 bool MatchesKey(const nsACString
& aKey
) { return aKey
== mKey
; }
57 const nsACString
& GetKey() const { return mKey
; }
59 JSExecutionManager
* GetExecutionManager() const { return mExecutionManager
; }
60 void SetExecutionManager(JSExecutionManager
*);
62 BrowsingContextGroup
* GetBrowsingContextGroup() const {
63 return mBrowsingContextGroup
;
66 mozilla::dom::DOMArena
* ArenaAllocator() { return mArena
; }
68 mozilla::dom::CustomElementReactionsStack
* CustomElementReactionsStack();
70 // Adding documents to a DocGroup should be done through
71 // BrowsingContextGroup::AddDocument (which in turn calls
72 // DocGroup::AddDocument).
73 void AddDocument(Document
* aDocument
);
75 // Removing documents from a DocGroup should be done through
76 // BrowsingContextGroup::RemoveDocument(which in turn calls
77 // DocGroup::RemoveDocument).
78 void RemoveDocument(Document
* aDocument
);
80 // Iterators for iterating over every document within the DocGroup
82 MOZ_ASSERT(NS_IsMainThread());
83 return mDocuments
.begin();
86 MOZ_ASSERT(NS_IsMainThread());
87 return mDocuments
.end();
90 // Return a pointer that can be continually checked to see if access to this
91 // DocGroup is valid. This pointer should live at least as long as the
93 bool* GetValidAccessPtr();
95 // Append aSlot to the list of signal slot list, and queue a mutation observer
97 void SignalSlotChange(HTMLSlotElement
& aSlot
);
99 nsTArray
<RefPtr
<HTMLSlotElement
>> MoveSignalSlotList();
101 // List of DocGroups that has non-empty signal slot list.
102 static AutoTArray
<RefPtr
<DocGroup
>, 2>* sPendingDocGroups
;
104 // Returns true if any of its documents are active but not in the bfcache.
105 bool IsActive() const;
107 nsresult
QueueIframePostMessages(already_AddRefed
<nsIRunnable
>&& aRunnable
,
110 void TryFlushIframePostMessages(uint64_t aWindowId
);
112 static bool TryToLoadIframesInBackground();
114 const nsID
& AgentClusterId() const { return mAgentClusterId
; }
116 bool IsEmpty() const { return mDocuments
.IsEmpty(); }
119 DocGroup(BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
123 void FlushIframePostMessageQueue();
125 nsTArray
<Document
*> mDocuments
;
126 RefPtr
<mozilla::dom::CustomElementReactionsStack
> mReactionsStack
;
127 nsTArray
<RefPtr
<HTMLSlotElement
>> mSignalSlotList
;
128 RefPtr
<BrowsingContextGroup
> mBrowsingContextGroup
;
129 RefPtr
<mozilla::ThrottledEventQueue
> mIframePostMessageQueue
;
130 nsTHashSet
<uint64_t> mIframesUsedPostMessageQueue
;
132 // non-null if the JS execution for this docgroup is regulated with regards
133 // to worker threads. This should only be used when we are forcing serialized
135 RefPtr
<JSExecutionManager
> mExecutionManager
;
137 // Each DocGroup has a persisted agent cluster ID.
138 const nsID mAgentClusterId
;
140 RefPtr
<mozilla::dom::DOMArena
> mArena
;
144 } // namespace mozilla
146 #endif // defined(DocGroup_h)