Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/WindowDestroyedEvent.cpp
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
#include "WindowDestroyedEvent.h"
8
9
#include "nsJSUtils.h"
10
#include "jsapi.h"
11
#include "js/Wrapper.h"
12
#include "nsIPrincipal.h"
13
#include "nsISupportsPrimitives.h"
14
#include "nsIAppStartup.h"
15
#include "nsToolkitCompsCID.h"
16
#include "nsCOMPtr.h"
17
#include "nsContentUtils.h"
18
#include "xpcpublic.h"
19
20
namespace mozilla {
21
22
struct BrowserCompartmentMatcher : public js::CompartmentFilter {
23
  bool match(JS::Compartment* aC) const override
24
0
  {
25
0
    return !xpc::MightBeWebContentCompartment(aC);
26
0
  }
27
};
28
29
WindowDestroyedEvent::WindowDestroyedEvent(nsGlobalWindowInner* aWindow,
30
                                           uint64_t aID, const char* aTopic)
31
  : mozilla::Runnable("WindowDestroyedEvent")
32
  , mID(aID)
33
  , mPhase(Phase::Destroying)
34
  , mTopic(aTopic)
35
  , mIsInnerWindow(true)
36
0
{
37
0
  mWindow = do_GetWeakReference(aWindow);
38
0
}
39
40
WindowDestroyedEvent::WindowDestroyedEvent(nsGlobalWindowOuter* aWindow,
41
                                           uint64_t aID, const char* aTopic)
42
  : mozilla::Runnable("WindowDestroyedEvent")
43
  , mID(aID)
44
  , mPhase(Phase::Destroying)
45
  , mTopic(aTopic)
46
  , mIsInnerWindow(false)
47
0
{
48
0
  mWindow = do_GetWeakReference(aWindow);
49
0
}
50
51
NS_IMETHODIMP
52
WindowDestroyedEvent::Run()
53
0
{
54
0
  AUTO_PROFILER_LABEL("WindowDestroyedEvent::Run", OTHER);
55
0
56
0
  nsCOMPtr<nsIObserverService> observerService =
57
0
    services::GetObserverService();
58
0
  if (!observerService) {
59
0
    return NS_OK;
60
0
  }
61
0
62
0
  nsCOMPtr<nsISupportsPRUint64> wrapper =
63
0
    do_CreateInstance(NS_SUPPORTS_PRUINT64_CONTRACTID);
64
0
  if (wrapper) {
65
0
    wrapper->SetData(mID);
66
0
    observerService->NotifyObservers(wrapper, mTopic.get(), nullptr);
67
0
  }
68
0
69
0
  switch (mPhase) {
70
0
    case Phase::Destroying:
71
0
    {
72
0
      bool skipNukeCrossCompartment = false;
73
0
#ifndef DEBUG
74
0
      nsCOMPtr<nsIAppStartup> appStartup =
75
0
        do_GetService(NS_APPSTARTUP_CONTRACTID);
76
0
77
0
      if (appStartup) {
78
0
        appStartup->GetShuttingDown(&skipNukeCrossCompartment);
79
0
      }
80
0
#endif
81
0
82
0
      if (!skipNukeCrossCompartment) {
83
0
        // The compartment nuking phase might be too expensive, so do that
84
0
        // part off of idle dispatch.
85
0
86
0
        // For the compartment nuking phase, we dispatch either an
87
0
        // inner-window-nuked or an outer-window-nuked notification.
88
0
        // This will allow tests to wait for compartment nuking to happen.
89
0
        if (mTopic.EqualsLiteral("inner-window-destroyed")) {
90
0
          mTopic.AssignLiteral("inner-window-nuked");
91
0
        } else if (mTopic.EqualsLiteral("outer-window-destroyed")) {
92
0
          mTopic.AssignLiteral("outer-window-nuked");
93
0
        }
94
0
        mPhase = Phase::Nuking;
95
0
96
0
        nsCOMPtr<nsIRunnable> copy(this);
97
0
        NS_IdleDispatchToCurrentThread(copy.forget(), 1000);
98
0
      }
99
0
    }
100
0
    break;
101
0
102
0
    case Phase::Nuking:
103
0
    {
104
0
      nsCOMPtr<nsISupports> window = do_QueryReferent(mWindow);
105
0
      if (window) {
106
0
        nsGlobalWindowInner* currentInner;
107
0
        if (mIsInnerWindow) {
108
0
          currentInner = nsGlobalWindowInner::FromSupports(window);
109
0
        } else {
110
0
          nsGlobalWindowOuter* outer = nsGlobalWindowOuter::FromSupports(window);
111
0
          currentInner = outer->GetCurrentInnerWindowInternal();
112
0
        }
113
0
        NS_ENSURE_TRUE(currentInner, NS_OK);
114
0
115
0
        AutoSafeJSContext cx;
116
0
        JS::Rooted<JSObject*> obj(cx, currentInner->FastGetGlobalJSObject());
117
0
        if (obj && !js::IsSystemRealm(js::GetNonCCWObjectRealm(obj))) {
118
0
          JS::Realm* realm = js::GetNonCCWObjectRealm(obj);
119
0
          JS::Compartment* cpt = JS::GetCompartmentForRealm(realm);
120
0
121
0
          nsCOMPtr<nsIPrincipal> pc =
122
0
            nsJSPrincipals::get(JS::GetRealmPrincipals(realm));
123
0
124
0
          if (BasePrincipal::Cast(pc)->AddonPolicy()) {
125
0
            // We want to nuke all references to the add-on compartment.
126
0
            xpc::NukeAllWrappersForCompartment(cx, cpt,
127
0
                                               mIsInnerWindow ? js::DontNukeWindowReferences
128
0
                                                              : js::NukeWindowReferences);
129
0
          } else {
130
0
            // We only want to nuke wrappers for the chrome->content case
131
0
            js::NukeCrossCompartmentWrappers(cx, BrowserCompartmentMatcher(), cpt,
132
0
                                             mIsInnerWindow ? js::DontNukeWindowReferences
133
0
                                                            : js::NukeWindowReferences,
134
0
                                             js::NukeIncomingReferences);
135
0
          }
136
0
        }
137
0
      }
138
0
    }
139
0
    break;
140
0
  }
141
0
142
0
  return NS_OK;
143
0
}
144
145
} // namespace mozilla