Source code

Revision control

Other Tools

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
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_BrowsingContextGroup_h
8
#define mozilla_dom_BrowsingContextGroup_h
9
10
#include "mozilla/dom/BrowsingContext.h"
11
#include "nsHashKeys.h"
12
#include "nsTArray.h"
13
#include "nsTHashtable.h"
14
#include "nsWrapperCache.h"
15
#include "nsXULAppAPI.h"
16
17
namespace mozilla {
18
class ThrottledEventQueue;
19
20
namespace dom {
21
22
class BrowsingContext;
23
class ContentParent;
24
25
// A BrowsingContextGroup represents the Unit of Related Browsing Contexts in
26
// the standard. This object currently serves roughly the same purpose as the
27
// TabGroup class which already exists, and at some point will likely merge with
28
// it.
29
//
30
// A BrowsingContext may not hold references to other BrowsingContext objects
31
// which are not in the same BrowsingContextGroup.
32
class BrowsingContextGroup final : public nsWrapperCache {
33
public:
34
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(BrowsingContextGroup)
35
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(BrowsingContextGroup)
36
37
typedef nsTHashtable<nsRefPtrHashKey<ContentParent>> ContentParents;
38
39
// Interact with the list of BrowsingContexts.
40
bool Contains(BrowsingContext* aContext);
41
void Register(BrowsingContext* aContext);
42
void Unregister(BrowsingContext* aContext);
43
44
// Interact with the list of ContentParents
45
void Subscribe(ContentParent* aOriginProcess);
46
void Unsubscribe(ContentParent* aOriginProcess);
47
48
// Force the given ContentParent to subscribe to our BrowsingContextGroup.
49
void EnsureSubscribed(ContentParent* aProcess);
50
51
// Methods interacting with cached contexts.
52
bool IsContextCached(BrowsingContext* aContext) const;
53
void CacheContext(BrowsingContext* aContext);
54
void CacheContexts(const BrowsingContext::Children& aContexts);
55
bool EvictCachedContext(BrowsingContext* aContext);
56
57
// Get a reference to the list of toplevel contexts in this
58
// BrowsingContextGroup.
59
BrowsingContext::Children& Toplevels() { return mToplevels; }
60
void GetToplevels(BrowsingContext::Children& aToplevels) {
61
aToplevels.AppendElements(mToplevels);
62
}
63
64
nsISupports* GetParentObject() const;
65
JSObject* WrapObject(JSContext* aCx,
66
JS::Handle<JSObject*> aGivenProto) override;
67
68
BrowsingContextGroup();
69
70
static already_AddRefed<BrowsingContextGroup> Select(
71
BrowsingContext* aParent, BrowsingContext* aOpener) {
72
if (aParent) {
73
return do_AddRef(aParent->Group());
74
}
75
if (aOpener) {
76
return do_AddRef(aOpener->Group());
77
}
78
return MakeAndAddRef<BrowsingContextGroup>();
79
}
80
81
static already_AddRefed<BrowsingContextGroup> Select(uint64_t aParentId,
82
uint64_t aOpenerId) {
83
RefPtr<BrowsingContext> parent = BrowsingContext::Get(aParentId);
84
MOZ_RELEASE_ASSERT(parent || aParentId == 0);
85
86
RefPtr<BrowsingContext> opener = BrowsingContext::Get(aOpenerId);
87
MOZ_RELEASE_ASSERT(opener || aOpenerId == 0);
88
89
return Select(parent, opener);
90
}
91
92
// For each 'ContentParent', except for 'aExcludedParent',
93
// associated with this group call 'aCallback'.
94
template <typename Func>
95
void EachOtherParent(ContentParent* aExcludedParent, Func&& aCallback) {
96
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
97
for (auto iter = mSubscribers.Iter(); !iter.Done(); iter.Next()) {
98
if (iter.Get()->GetKey() != aExcludedParent) {
99
aCallback(iter.Get()->GetKey());
100
}
101
}
102
}
103
104
// For each 'ContentParent' associated with
105
// this group call 'aCallback'.
106
template <typename Func>
107
void EachParent(Func&& aCallback) {
108
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
109
for (auto iter = mSubscribers.Iter(); !iter.Done(); iter.Next()) {
110
aCallback(iter.Get()->GetKey());
111
}
112
}
113
114
nsresult QueuePostMessageEvent(already_AddRefed<nsIRunnable>&& aRunnable);
115
116
void FlushPostMessageEvents();
117
118
static BrowsingContextGroup* GetChromeGroup();
119
120
private:
121
friend class CanonicalBrowsingContext;
122
123
~BrowsingContextGroup();
124
125
void UnsubscribeAllContentParents();
126
127
// A BrowsingContextGroup contains a series of BrowsingContext objects. They
128
// are addressed using a hashtable to avoid linear lookup when adding or
129
// removing elements from the set.
130
nsTHashtable<nsRefPtrHashKey<BrowsingContext>> mContexts;
131
132
// The set of toplevel browsing contexts in the current BrowsingContextGroup.
133
BrowsingContext::Children mToplevels;
134
135
ContentParents mSubscribers;
136
137
// Map of cached contexts that need to stay alive due to bfcache.
138
nsTHashtable<nsRefPtrHashKey<BrowsingContext>> mCachedContexts;
139
140
// A queue to store postMessage events during page load, the queue will be
141
// flushed once the page is loaded
142
RefPtr<mozilla::ThrottledEventQueue> mPostMessageEventQueue;
143
};
144
145
} // namespace dom
146
} // namespace mozilla
147
148
#endif // !defined(mozilla_dom_BrowsingContextGroup_h)