Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsIGlobalObject.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
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef nsIGlobalObject_h__
8
#define nsIGlobalObject_h__
9
10
#include "mozilla/LinkedList.h"
11
#include "mozilla/Maybe.h"
12
#include "mozilla/dom/ClientInfo.h"
13
#include "mozilla/dom/DispatcherTrait.h"
14
#include "mozilla/dom/ServiceWorkerDescriptor.h"
15
#include "nsHashKeys.h"
16
#include "nsISupports.h"
17
#include "nsStringFwd.h"
18
#include "nsTArray.h"
19
#include "nsTHashtable.h"
20
#include "js/TypeDecls.h"
21
22
// Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
23
#define NS_IGLOBALOBJECT_IID \
24
{ 0x11afa8be, 0xd997, 0x4e07, \
25
{ 0xa6, 0xa3, 0x6f, 0x87, 0x2e, 0xc3, 0xee, 0x7f } }
26
27
class nsCycleCollectionTraversalCallback;
28
class nsIPrincipal;
29
class nsPIDOMWindowInner;
30
31
namespace mozilla {
32
class DOMEventTargetHelper;
33
namespace dom {
34
class ServiceWorker;
35
class ServiceWorkerRegistration;
36
class ServiceWorkerRegistrationDescriptor;
37
} // namespace dom
38
} // namespace mozilla
39
40
class nsIGlobalObject : public nsISupports,
41
                        public mozilla::dom::DispatcherTrait
42
{
43
  nsTArray<nsCString> mHostObjectURIs;
44
45
  // Raw pointers to bound DETH objects.  These are added by
46
  // AddEventTargetObject().
47
  mozilla::LinkedList<mozilla::DOMEventTargetHelper> mEventTargetObjects;
48
49
  bool mIsDying;
50
51
protected:
52
53
  bool mIsInnerWindow;
54
55
  nsIGlobalObject()
56
   : mIsDying(false)
57
   , mIsInnerWindow(false)
58
6
  {}
59
60
public:
61
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)
62
63
  /**
64
   * This check is added to deal with Promise microtask queues. On the main
65
   * thread, we do not impose restrictions about when script stops running or
66
   * when runnables can no longer be dispatched to the main thread. This means
67
   * it is possible for a Promise chain to keep resolving an infinite chain of
68
   * promises, preventing the browser from shutting down. See Bug 1058695. To
69
   * prevent this, the nsGlobalWindow subclass sets this flag when it is
70
   * closed. The Promise implementation checks this and prohibits new runnables
71
   * from being dispatched.
72
   *
73
   * We pair this with checks during processing the promise microtask queue
74
   * that pops up the slow script dialog when the Promise queue is preventing
75
   * a window from going away.
76
   */
77
  bool
78
  IsDying() const
79
0
  {
80
0
    return mIsDying;
81
0
  }
82
83
  // GetGlobalJSObject may return a gray object.  If this ever changes so that
84
  // it stops doing that, please simplify the code in FindAssociatedGlobal in
85
  // BindingUtils.h that does JS::ExposeObjectToActiveJS on the return value of
86
  // GetGlobalJSObject.  Also, in that case the JS::ExposeObjectToActiveJS in
87
  // AutoJSAPI::InitInternal can probably be removed.  And also the similar
88
  // calls in XrayWrapper and nsGlobalWindow.
89
  virtual JSObject* GetGlobalJSObject() = 0;
90
91
  // This method is not meant to be overridden.
92
  nsIPrincipal* PrincipalOrNull();
93
94
  void RegisterHostObjectURI(const nsACString& aURI);
95
96
  void UnregisterHostObjectURI(const nsACString& aURI);
97
98
  // Any CC class inheriting nsIGlobalObject should call these 2 methods if it
99
  // exposes the URL API.
100
  void UnlinkHostObjectURIs();
101
  void TraverseHostObjectURIs(nsCycleCollectionTraversalCallback &aCb);
102
103
  // DETH objects must register themselves on the global when they
104
  // bind to it in order to get the DisconnectFromOwner() method
105
  // called correctly.  RemoveEventTargetObject() must be called
106
  // before the DETH object is destroyed.
107
  void AddEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
108
  void RemoveEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
109
110
  // Iterate the registered DETH objects and call the given function
111
  // for each one.
112
  void
113
  ForEachEventTargetObject(const std::function<void(mozilla::DOMEventTargetHelper*, bool* aDoneOut)>& aFunc) const;
114
115
0
  virtual bool IsInSyncOperation() { return false; }
116
117
  virtual mozilla::Maybe<mozilla::dom::ClientInfo>
118
  GetClientInfo() const;
119
120
  virtual mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor>
121
  GetController() const;
122
123
  // Get the DOM object for the given descriptor or attempt to create one.
124
  // Creation can still fail and return nullptr during shutdown, etc.
125
  virtual RefPtr<mozilla::dom::ServiceWorker>
126
  GetOrCreateServiceWorker(const mozilla::dom::ServiceWorkerDescriptor& aDescriptor);
127
128
  // Get the DOM object for the given descriptor or return nullptr if it does
129
  // not exist.
130
  virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
131
  GetServiceWorkerRegistration(const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor) const;
132
133
  // Get the DOM object for the given descriptor or attempt to create one.
134
  // Creation can still fail and return nullptr during shutdown, etc.
135
  virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
136
  GetOrCreateServiceWorkerRegistration(const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor);
137
138
  // Returns a pointer to this object as an inner window if this is one or nullptr otherwise.
139
  nsPIDOMWindowInner* AsInnerWindow();
140
protected:
141
  virtual ~nsIGlobalObject();
142
143
  void
144
  StartDying()
145
0
  {
146
0
    mIsDying = true;
147
0
  }
148
149
  void
150
  DisconnectEventTargetObjects();
151
152
  size_t
153
  ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aSizeOf) const;
154
};
155
156
NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject,
157
                              NS_IGLOBALOBJECT_IID)
158
159
#endif // nsIGlobalObject_h__