DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (3dc70a33491f)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsIGlobalObject_h__
#define nsIGlobalObject_h__

#include "mozilla/LinkedList.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/ClientInfo.h"
#include "mozilla/dom/DispatcherTrait.h"
#include "mozilla/dom/ServiceWorkerDescriptor.h"
#include "nsHashKeys.h"
#include "nsISupports.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
#include "js/TypeDecls.h"

// Must be kept in sync with xpcom/rust/xpcom/src/interfaces/nonidl.rs
#define NS_IGLOBALOBJECT_IID                         \
  {                                                  \
    0x11afa8be, 0xd997, 0x4e07, {                    \
      0xa6, 0xa3, 0x6f, 0x87, 0x2e, 0xc3, 0xee, 0x7f \
    }                                                \
  }

class nsCycleCollectionTraversalCallback;
class nsIPrincipal;
class nsPIDOMWindowInner;

namespace mozilla {
class DOMEventTargetHelper;
namespace dom {
class VoidFunction;
class DebuggerNotificationManager;
class ServiceWorker;
class ServiceWorkerRegistration;
class ServiceWorkerRegistrationDescriptor;
}  // namespace dom
}  // namespace mozilla

class nsIGlobalObject : public nsISupports,
                        public mozilla::dom::DispatcherTrait {
  nsTArray<nsCString> mHostObjectURIs;

  // Raw pointers to bound DETH objects.  These are added by
  // AddEventTargetObject().
  mozilla::LinkedList<mozilla::DOMEventTargetHelper> mEventTargetObjects;

  bool mIsDying;

 protected:
  bool mIsInnerWindow;

  nsIGlobalObject() : mIsDying(false), mIsInnerWindow(false) {}

 public:
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)

  /**
   * This check is added to deal with Promise microtask queues. On the main
   * thread, we do not impose restrictions about when script stops running or
   * when runnables can no longer be dispatched to the main thread. This means
   * it is possible for a Promise chain to keep resolving an infinite chain of
   * promises, preventing the browser from shutting down. See Bug 1058695. To
   * prevent this, the nsGlobalWindow subclass sets this flag when it is
   * closed. The Promise implementation checks this and prohibits new runnables
   * from being dispatched.
   *
   * We pair this with checks during processing the promise microtask queue
   * that pops up the slow script dialog when the Promise queue is preventing
   * a window from going away.
   */
  bool IsDying() const { return mIsDying; }

  /**
   * Return the JSObject for this global, if it still has one.  Otherwise return
   * null.
   *
   * If non-null is returned, then the returned object will have been already
   * exposed to active JS, so callers do not need to do it.
   */
  virtual JSObject* GetGlobalJSObject() = 0;

  /**
   * Return the JSObject for this global _without_ exposing it to active JS.
   * This may return a gray object.
   *
   * This method is appropriate to use in assertions (so there is less of a
   * difference in GC/CC marking between debug and optimized builds) and in
   * situations where we are sure no CC activity can happen while the return
   * value is used and the return value does not end up escaping to the heap in
   * any way.  In all other cases, and in particular in cases where the return
   * value is held in a JS::Rooted or passed to the JSAutoRealm constructor, use
   * GetGlobalJSObject.
   */
  virtual JSObject* GetGlobalJSObjectPreserveColor() const = 0;

  /**
   * Check whether this nsIGlobalObject still has a JSObject associated with it,
   * or whether it's torn-down enough that the JSObject is gone.
   */
  bool HasJSGlobal() const { return GetGlobalJSObjectPreserveColor(); }

  // This method is not meant to be overridden.
  nsIPrincipal* PrincipalOrNull();

  void RegisterHostObjectURI(const nsACString& aURI);

  void UnregisterHostObjectURI(const nsACString& aURI);

  // Any CC class inheriting nsIGlobalObject should call these 2 methods if it
  // exposes the URL API.
  void UnlinkHostObjectURIs();
  void TraverseHostObjectURIs(nsCycleCollectionTraversalCallback& aCb);

  // DETH objects must register themselves on the global when they
  // bind to it in order to get the DisconnectFromOwner() method
  // called correctly.  RemoveEventTargetObject() must be called
  // before the DETH object is destroyed.
  void AddEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
  void RemoveEventTargetObject(mozilla::DOMEventTargetHelper* aObject);

  // Iterate the registered DETH objects and call the given function
  // for each one.
  void ForEachEventTargetObject(
      const std::function<void(mozilla::DOMEventTargetHelper*, bool* aDoneOut)>&
          aFunc) const;

  virtual bool IsInSyncOperation() { return false; }

  virtual mozilla::dom::DebuggerNotificationManager*
  GetOrCreateDebuggerNotificationManager() {
    return nullptr;
  }

  virtual mozilla::dom::DebuggerNotificationManager*
  GetExistingDebuggerNotificationManager() {
    return nullptr;
  }

  virtual mozilla::Maybe<mozilla::dom::ClientInfo> GetClientInfo() const;

  virtual mozilla::Maybe<mozilla::dom::ServiceWorkerDescriptor> GetController()
      const;

  // Get the DOM object for the given descriptor or attempt to create one.
  // Creation can still fail and return nullptr during shutdown, etc.
  virtual RefPtr<mozilla::dom::ServiceWorker> GetOrCreateServiceWorker(
      const mozilla::dom::ServiceWorkerDescriptor& aDescriptor);

  // Get the DOM object for the given descriptor or return nullptr if it does
  // not exist.
  virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
  GetServiceWorkerRegistration(
      const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor)
      const;

  // Get the DOM object for the given descriptor or attempt to create one.
  // Creation can still fail and return nullptr during shutdown, etc.
  virtual RefPtr<mozilla::dom::ServiceWorkerRegistration>
  GetOrCreateServiceWorkerRegistration(
      const mozilla::dom::ServiceWorkerRegistrationDescriptor& aDescriptor);

  // Returns a pointer to this object as an inner window if this is one or
  // nullptr otherwise.
  nsPIDOMWindowInner* AsInnerWindow();

  void QueueMicrotask(mozilla::dom::VoidFunction& aCallback);

 protected:
  virtual ~nsIGlobalObject();

  void StartDying() { mIsDying = true; }

  void DisconnectEventTargetObjects();

  size_t ShallowSizeOfExcludingThis(mozilla::MallocSizeOf aSizeOf) const;
};

NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject, NS_IGLOBALOBJECT_IID)

#endif  // nsIGlobalObject_h__