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 nsresult
Dispatch(TaskCategory aCategory
,
91 already_AddRefed
<nsIRunnable
>&& aRunnable
);
93 nsISerialEventTarget
* EventTargetFor(TaskCategory aCategory
) const;
95 AbstractThread
* AbstractMainThreadFor(TaskCategory aCategory
);
97 // Return a pointer that can be continually checked to see if access to this
98 // DocGroup is valid. This pointer should live at least as long as the
100 bool* GetValidAccessPtr();
102 // Append aSlot to the list of signal slot list, and queue a mutation observer
104 void SignalSlotChange(HTMLSlotElement
& aSlot
);
106 nsTArray
<RefPtr
<HTMLSlotElement
>> MoveSignalSlotList();
108 // List of DocGroups that has non-empty signal slot list.
109 static AutoTArray
<RefPtr
<DocGroup
>, 2>* sPendingDocGroups
;
111 // Returns true if any of its documents are active but not in the bfcache.
112 bool IsActive() const;
114 nsresult
QueueIframePostMessages(already_AddRefed
<nsIRunnable
>&& aRunnable
,
117 void TryFlushIframePostMessages(uint64_t aWindowId
);
119 static bool TryToLoadIframesInBackground();
121 const nsID
& AgentClusterId() const { return mAgentClusterId
; }
123 bool IsEmpty() const { return mDocuments
.IsEmpty(); }
126 DocGroup(BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
130 void FlushIframePostMessageQueue();
132 nsTArray
<Document
*> mDocuments
;
133 RefPtr
<mozilla::dom::CustomElementReactionsStack
> mReactionsStack
;
134 nsTArray
<RefPtr
<HTMLSlotElement
>> mSignalSlotList
;
135 RefPtr
<BrowsingContextGroup
> mBrowsingContextGroup
;
136 RefPtr
<mozilla::ThrottledEventQueue
> mIframePostMessageQueue
;
137 nsTHashSet
<uint64_t> mIframesUsedPostMessageQueue
;
138 nsCOMPtr
<nsISerialEventTarget
> mEventTarget
;
140 // non-null if the JS execution for this docgroup is regulated with regards
141 // to worker threads. This should only be used when we are forcing serialized
143 RefPtr
<JSExecutionManager
> mExecutionManager
;
145 // Each DocGroup has a persisted agent cluster ID.
146 const nsID mAgentClusterId
;
148 RefPtr
<mozilla::dom::DOMArena
> mArena
;
152 } // namespace mozilla
154 #endif // defined(DocGroup_h)