Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/TabGroup.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef TabGroup_h
8
#define TabGroup_h
9
10
#include "nsHashKeys.h"
11
#include "nsISupportsImpl.h"
12
#include "nsIPrincipal.h"
13
#include "nsTHashtable.h"
14
#include "nsString.h"
15
16
#include "mozilla/Atomics.h"
17
#include "mozilla/SchedulerGroup.h"
18
#include "mozilla/RefPtr.h"
19
20
class mozIDOMWindowProxy;
21
class nsIDocShellTreeItem;
22
class nsIDocument;
23
class nsPIDOMWindowOuter;
24
25
namespace mozilla {
26
class AbstractThread;
27
class ThrottledEventQueue;
28
namespace dom {
29
class TabChild;
30
31
// Two browsing contexts are considered "related" if they are reachable from one
32
// another through window.opener, window.parent, or window.frames. This is the
33
// spec concept of a "unit of related browsing contexts"
34
//
35
// Two browsing contexts are considered "similar-origin" if they can be made to
36
// have the same origin by setting document.domain. This is the spec concept of
37
// a "unit of similar-origin related browsing contexts"
38
//
39
// A TabGroup is a set of browsing contexts which are all "related". Within a
40
// TabGroup, browsing contexts are broken into "similar-origin" DocGroups. In
41
// more detail, a DocGroup is actually a collection of documents, and a
42
// TabGroup is a collection of DocGroups. A TabGroup typically will contain
43
// (through its DocGroups) the documents from one or more tabs related by
44
// window.opener. A DocGroup is a member of exactly one TabGroup.
45
46
class DocGroup;
47
class TabChild;
48
49
class TabGroup final : public SchedulerGroup,
50
                       public LinkedListElement<TabGroup>
51
{
52
private:
53
  class HashEntry : public nsCStringHashKey
54
  {
55
  public:
56
    // NOTE: Weak reference. The DocGroup destructor removes itself from its
57
    // owning TabGroup.
58
    DocGroup* mDocGroup;
59
    explicit HashEntry(const nsACString* aKey);
60
  };
61
62
  typedef nsTHashtable<HashEntry> DocGroupMap;
63
64
public:
65
  typedef DocGroupMap::Iterator Iterator;
66
67
  friend class DocGroup;
68
69
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TabGroup, override)
70
71
  static TabGroup*
72
  GetChromeTabGroup();
73
74
  // Checks if the TabChild already has a TabGroup assigned to it in
75
  // IPDL. Returns this TabGroup if it does. This could happen if the parent
76
  // process created the PBrowser and we needed to assign a TabGroup immediately
77
  // upon receiving the IPDL message. This method is main thread only.
78
  static TabGroup* GetFromActor(TabChild* aTabChild);
79
80
  static TabGroup* GetFromWindow(mozIDOMWindowProxy* aWindow);
81
82
  explicit TabGroup(bool aIsChrome = false);
83
84
  // Get the docgroup for the corresponding doc group key.
85
  // Returns null if the given key hasn't been seen yet.
86
  already_AddRefed<DocGroup>
87
  GetDocGroup(const nsACString& aKey);
88
89
  already_AddRefed<DocGroup>
90
  AddDocument(const nsACString& aKey, nsIDocument* aDocument);
91
92
  // Join the specified TabGroup, returning a reference to it. If aTabGroup is
93
  // nullptr, create a new tabgroup to join.
94
  static already_AddRefed<TabGroup>
95
  Join(nsPIDOMWindowOuter* aWindow, TabGroup* aTabGroup);
96
97
  void Leave(nsPIDOMWindowOuter* aWindow);
98
99
  Iterator Iter()
100
0
  {
101
0
    return mDocGroups.Iter();
102
0
  }
103
104
  // Returns the size of the set of "similar-origin" DocGroups. To
105
  // only consider DocGroups with at least one active document, call
106
  // Count with 'aActiveOnly' = true
107
  uint32_t Count(bool aActiveOnly = false) const;
108
109
  // Returns the nsIDocShellTreeItem with the given name, searching each of the
110
  // docShell trees which are within this TabGroup. It will pass itself as
111
  // aRequestor to each docShellTreeItem which it asks to search for the name,
112
  // and will not search the docShellTreeItem which is passed as aRequestor.
113
  //
114
  // This method is used in order to correctly namespace named windows based on
115
  // their unit of related browsing contexts.
116
  //
117
  // It is illegal to pass in the special case-insensitive names "_blank",
118
  // "_self", "_parent" or "_top", as those should be handled elsewhere.
119
  nsresult
120
  FindItemWithName(const nsAString& aName,
121
                   nsIDocShellTreeItem* aRequestor,
122
                   nsIDocShellTreeItem* aOriginalRequestor,
123
                   nsIDocShellTreeItem** aFoundItem);
124
125
  nsTArray<nsPIDOMWindowOuter*> GetTopLevelWindows() const;
126
0
  const nsTArray<nsPIDOMWindowOuter*>& GetWindows() { return mWindows; }
127
128
  // This method is always safe to call off the main thread. The nsIEventTarget
129
  // can always be used off the main thread.
130
  nsISerialEventTarget* EventTargetFor(TaskCategory aCategory) const override;
131
132
  void WindowChangedBackgroundStatus(bool aIsNowBackground);
133
134
  // Returns true if all of the TabGroup's top-level windows are in
135
  // the background.
136
  bool IsBackground() const override;
137
138
  // Increase/Decrease the number of IndexedDB transactions/databases for the
139
  // decision making of the preemption in the scheduler.
140
  Atomic<uint32_t>& IndexedDBTransactionCounter()
141
0
  {
142
0
    return mNumOfIndexedDBTransactions;
143
0
  }
144
145
  Atomic<uint32_t>& IndexedDBDatabaseCounter()
146
0
  {
147
0
    return mNumOfIndexedDBDatabases;
148
0
  }
149
150
  static LinkedList<TabGroup>* GetTabGroupList()
151
0
  {
152
0
    return sTabGroups;
153
0
  }
154
155
  // This returns true if all the window objects in all the TabGroups are
156
  // either inactive (for example in bfcache) or are in background tabs which
157
  // can be throttled.
158
  static bool HasOnlyThrottableTabs();
159
160
private:
161
  virtual AbstractThread*
162
  AbstractMainThreadForImpl(TaskCategory aCategory) override;
163
164
0
  TabGroup* AsTabGroup() override { return this; }
165
166
  void EnsureThrottledEventQueues();
167
168
  ~TabGroup();
169
170
  // Thread-safe members
171
  Atomic<bool> mLastWindowLeft;
172
  Atomic<bool> mThrottledQueuesInitialized;
173
  Atomic<uint32_t> mNumOfIndexedDBTransactions;
174
  Atomic<uint32_t> mNumOfIndexedDBDatabases;
175
  const bool mIsChrome;
176
177
  // Main thread only
178
  DocGroupMap mDocGroups;
179
  nsTArray<nsPIDOMWindowOuter*> mWindows;
180
  uint32_t mForegroundCount;
181
182
  static LinkedList<TabGroup>* sTabGroups;
183
};
184
185
} // namespace dom
186
} // namespace mozilla
187
188
#endif // defined(TabGroup_h)