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 "nsTHashtable.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/dom/BrowsingContextGroup.h"
16 #include "mozilla/dom/CustomElementRegistry.h"
17 #include "mozilla/dom/HTMLSlotElement.h"
18 #include "mozilla/PerformanceCounter.h"
19 #include "mozilla/PerformanceTypes.h"
25 // Two browsing contexts are considered "related" if they are reachable from one
26 // another through window.opener, window.parent, or window.frames. This is the
27 // spec concept of a browsing context group.
29 // Two browsing contexts are considered "similar-origin" if they can be made to
30 // have the same origin by setting document.domain. This is the spec concept of
31 // a "unit of similar-origin related browsing contexts"
33 // A BrowsingContextGroup is a set of browsing contexts which are all
34 // "related". Within a BrowsingContextGroup, browsing contexts are
35 // broken into "similar-origin" DocGroups. A DocGroup is a member
36 // of exactly one BrowsingContextGroup.
37 class DocGroup final
{
39 typedef nsTArray
<Document
*>::iterator Iterator
;
41 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DocGroup
)
43 static already_AddRefed
<DocGroup
> Create(
44 BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
46 // Returns NS_ERROR_FAILURE and sets |aString| to an empty string if the TLD
47 // service isn't available. Returns NS_OK on success, but may still set
48 // |aString| may still be set to an empty string.
49 static MOZ_MUST_USE nsresult
GetKey(nsIPrincipal
* aPrincipal
,
50 bool aCrossOriginIsolated
,
53 bool MatchesKey(const nsACString
& aKey
) { return aKey
== mKey
; }
55 const nsACString
& GetKey() const { return mKey
; }
57 PerformanceCounter
* GetPerformanceCounter() { return mPerformanceCounter
; }
59 JSExecutionManager
* GetExecutionManager() const { return mExecutionManager
; }
60 void SetExecutionManager(JSExecutionManager
*);
62 RefPtr
<PerformanceInfoPromise
> ReportPerformanceInfo();
64 BrowsingContextGroup
* GetBrowsingContextGroup() const {
65 return mBrowsingContextGroup
;
68 mozilla::dom::DOMArena
* ArenaAllocator() { return mArena
; }
70 mozilla::dom::CustomElementReactionsStack
* CustomElementReactionsStack() {
71 MOZ_ASSERT(NS_IsMainThread());
72 if (!mReactionsStack
) {
73 mReactionsStack
= new mozilla::dom::CustomElementReactionsStack();
76 return mReactionsStack
;
79 // Adding documents to a DocGroup should be done through
80 // BrowsingContextGroup::AddDocument (which in turn calls
81 // DocGroup::AddDocument).
82 void AddDocument(Document
* aDocument
);
84 // Removing documents from a DocGroup should be done through
85 // BrowsingContextGroup::RemoveDocument(which in turn calls
86 // DocGroup::RemoveDocument).
87 void RemoveDocument(Document
* aDocument
);
89 // Iterators for iterating over every document within the DocGroup
91 MOZ_ASSERT(NS_IsMainThread());
92 return mDocuments
.begin();
95 MOZ_ASSERT(NS_IsMainThread());
96 return mDocuments
.end();
99 nsresult
Dispatch(TaskCategory aCategory
,
100 already_AddRefed
<nsIRunnable
>&& aRunnable
);
102 nsISerialEventTarget
* EventTargetFor(TaskCategory aCategory
) const;
104 AbstractThread
* AbstractMainThreadFor(TaskCategory aCategory
);
106 // Return a pointer that can be continually checked to see if access to this
107 // DocGroup is valid. This pointer should live at least as long as the
109 bool* GetValidAccessPtr();
111 // Append aSlot to the list of signal slot list, and queue a mutation observer
113 void SignalSlotChange(HTMLSlotElement
& aSlot
);
115 nsTArray
<RefPtr
<HTMLSlotElement
>> MoveSignalSlotList();
117 // List of DocGroups that has non-empty signal slot list.
118 static AutoTArray
<RefPtr
<DocGroup
>, 2>* sPendingDocGroups
;
120 // Returns true if any of its documents are active but not in the bfcache.
121 bool IsActive() const;
123 nsresult
QueueIframePostMessages(already_AddRefed
<nsIRunnable
>&& aRunnable
,
126 void TryFlushIframePostMessages(uint64_t aWindowId
);
128 static bool TryToLoadIframesInBackground();
130 const nsID
& AgentClusterId() const { return mAgentClusterId
; }
132 bool IsEmpty() const { return mDocuments
.IsEmpty(); }
135 DocGroup(BrowsingContextGroup
* aBrowsingContextGroup
, const nsACString
& aKey
);
139 void FlushIframePostMessageQueue();
141 nsTArray
<Document
*> mDocuments
;
142 RefPtr
<mozilla::dom::CustomElementReactionsStack
> mReactionsStack
;
143 nsTArray
<RefPtr
<HTMLSlotElement
>> mSignalSlotList
;
144 RefPtr
<mozilla::PerformanceCounter
> mPerformanceCounter
;
145 RefPtr
<BrowsingContextGroup
> mBrowsingContextGroup
;
146 RefPtr
<mozilla::ThrottledEventQueue
> mIframePostMessageQueue
;
147 nsTHashtable
<nsUint64HashKey
> mIframesUsedPostMessageQueue
;
148 nsCOMPtr
<nsISerialEventTarget
> mEventTarget
;
150 // non-null if the JS execution for this docgroup is regulated with regards
151 // to worker threads. This should only be used when we are forcing serialized
153 RefPtr
<JSExecutionManager
> mExecutionManager
;
155 // Each DocGroup has a persisted agent cluster ID.
156 const nsID mAgentClusterId
;
158 RefPtr
<mozilla::dom::DOMArena
> mArena
;
162 } // namespace mozilla
164 #endif // defined(DocGroup_h)