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"
18 #include "mozilla/PerformanceCounter.h"
19 #include "mozilla/PerformanceTypes.h"
25 class CustomElementReactionsStack
;
26 class JSExecutionManager
;
28 // Two browsing contexts are considered "related" if they are reachable from one
29 // another through window.opener, window.parent, or window.frames. This is the
30 // spec concept of a browsing context group.
32 // Two browsing contexts are considered "similar-origin" if they can be made to
33 // have the same origin by setting document.domain. This is the spec concept of
34 // a "unit of similar-origin related browsing contexts"
36 // A BrowsingContextGroup is a set of browsing contexts which are all
37 // "related". Within a BrowsingContextGroup, browsing contexts are
38 // broken into "similar-origin" DocGroups. A DocGroup is a member
39 // of exactly one BrowsingContextGroup.
40 class DocGroup final
{
42 typedef nsTArray
<Document
*>::iterator Iterator
;
44 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(DocGroup
)
45 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(DocGroup
)
47 static already_AddRefed
<DocGroup
> Create(
48 BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
50 // Returns NS_ERROR_FAILURE and sets |aString| to an empty string if the TLD
51 // service isn't available. Returns NS_OK on success, but may still set
52 // |aString| may still be set to an empty string.
53 [[nodiscard
]] static nsresult
GetKey(nsIPrincipal
* aPrincipal
,
54 bool aCrossOriginIsolated
,
57 bool MatchesKey(const nsACString
& aKey
) { return aKey
== mKey
; }
59 const nsACString
& GetKey() const { return mKey
; }
61 PerformanceCounter
* GetPerformanceCounter() { return mPerformanceCounter
; }
63 JSExecutionManager
* GetExecutionManager() const { return mExecutionManager
; }
64 void SetExecutionManager(JSExecutionManager
*);
66 RefPtr
<PerformanceInfoPromise
> ReportPerformanceInfo();
68 BrowsingContextGroup
* GetBrowsingContextGroup() const {
69 return mBrowsingContextGroup
;
72 mozilla::dom::DOMArena
* ArenaAllocator() { return mArena
; }
74 mozilla::dom::CustomElementReactionsStack
* CustomElementReactionsStack();
76 // Adding documents to a DocGroup should be done through
77 // BrowsingContextGroup::AddDocument (which in turn calls
78 // DocGroup::AddDocument).
79 void AddDocument(Document
* aDocument
);
81 // Removing documents from a DocGroup should be done through
82 // BrowsingContextGroup::RemoveDocument(which in turn calls
83 // DocGroup::RemoveDocument).
84 void RemoveDocument(Document
* aDocument
);
86 // Iterators for iterating over every document within the DocGroup
88 MOZ_ASSERT(NS_IsMainThread());
89 return mDocuments
.begin();
92 MOZ_ASSERT(NS_IsMainThread());
93 return mDocuments
.end();
96 nsresult
Dispatch(TaskCategory aCategory
,
97 already_AddRefed
<nsIRunnable
>&& aRunnable
);
99 nsISerialEventTarget
* EventTargetFor(TaskCategory aCategory
) const;
101 AbstractThread
* AbstractMainThreadFor(TaskCategory aCategory
);
103 // Return a pointer that can be continually checked to see if access to this
104 // DocGroup is valid. This pointer should live at least as long as the
106 bool* GetValidAccessPtr();
108 // Append aSlot to the list of signal slot list, and queue a mutation observer
110 void SignalSlotChange(HTMLSlotElement
& aSlot
);
112 nsTArray
<RefPtr
<HTMLSlotElement
>> MoveSignalSlotList();
114 // List of DocGroups that has non-empty signal slot list.
115 static AutoTArray
<RefPtr
<DocGroup
>, 2>* sPendingDocGroups
;
117 // Returns true if any of its documents are active but not in the bfcache.
118 bool IsActive() const;
120 nsresult
QueueIframePostMessages(already_AddRefed
<nsIRunnable
>&& aRunnable
,
123 void TryFlushIframePostMessages(uint64_t aWindowId
);
125 static bool TryToLoadIframesInBackground();
127 const nsID
& AgentClusterId() const { return mAgentClusterId
; }
129 bool IsEmpty() const { return mDocuments
.IsEmpty(); }
132 DocGroup(BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
136 void FlushIframePostMessageQueue();
138 nsTArray
<Document
*> mDocuments
;
139 RefPtr
<mozilla::dom::CustomElementReactionsStack
> mReactionsStack
;
140 nsTArray
<RefPtr
<HTMLSlotElement
>> mSignalSlotList
;
141 RefPtr
<mozilla::PerformanceCounter
> mPerformanceCounter
;
142 RefPtr
<BrowsingContextGroup
> mBrowsingContextGroup
;
143 RefPtr
<mozilla::ThrottledEventQueue
> mIframePostMessageQueue
;
144 nsTHashSet
<uint64_t> mIframesUsedPostMessageQueue
;
145 nsCOMPtr
<nsISerialEventTarget
> mEventTarget
;
147 // non-null if the JS execution for this docgroup is regulated with regards
148 // to worker threads. This should only be used when we are forcing serialized
150 RefPtr
<JSExecutionManager
> mExecutionManager
;
152 // Each DocGroup has a persisted agent cluster ID.
153 const nsID mAgentClusterId
;
155 RefPtr
<mozilla::dom::DOMArena
> mArena
;
159 } // namespace mozilla
161 #endif // defined(DocGroup_h)