Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / GlobalTeardownObserver.cpp
blob9f9ac39b479ef0701e9e4253e75bb641c060263c
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/. */
7 #include "GlobalTeardownObserver.h"
8 #include "mozilla/Sprintf.h"
9 #include "mozilla/dom/Document.h"
11 namespace mozilla {
13 GlobalTeardownObserver::GlobalTeardownObserver() = default;
14 GlobalTeardownObserver::GlobalTeardownObserver(nsIGlobalObject* aGlobalObject,
15 bool aHasOrHasHadOwnerWindow)
16 : mHasOrHasHadOwnerWindow(aHasOrHasHadOwnerWindow) {
17 BindToOwner(aGlobalObject);
20 GlobalTeardownObserver::~GlobalTeardownObserver() {
21 if (mParentObject) {
22 mParentObject->RemoveGlobalTeardownObserver(this);
26 void GlobalTeardownObserver::BindToOwner(nsIGlobalObject* aOwner) {
27 MOZ_ASSERT(!mParentObject);
29 if (aOwner) {
30 mParentObject = aOwner;
31 aOwner->AddGlobalTeardownObserver(this);
32 // Let's cache the result of this QI for fast access and off main thread
33 // usage
34 mOwnerWindow =
35 nsCOMPtr<nsPIDOMWindowInner>(do_QueryInterface(aOwner)).get();
36 if (mOwnerWindow) {
37 mHasOrHasHadOwnerWindow = true;
42 void GlobalTeardownObserver::DisconnectFromOwner() {
43 if (mParentObject) {
44 mParentObject->RemoveGlobalTeardownObserver(this);
46 mOwnerWindow = nullptr;
47 mParentObject = nullptr;
50 nsresult GlobalTeardownObserver::CheckCurrentGlobalCorrectness() const {
51 NS_ENSURE_STATE(!mHasOrHasHadOwnerWindow || mOwnerWindow);
53 // Main-thread.
54 if (mOwnerWindow && !mOwnerWindow->IsCurrentInnerWindow()) {
55 return NS_ERROR_FAILURE;
58 if (NS_IsMainThread()) {
59 return NS_OK;
62 if (!mParentObject) {
63 return NS_ERROR_FAILURE;
66 if (mParentObject->IsDying()) {
67 return NS_ERROR_FAILURE;
70 return NS_OK;
73 }; // namespace mozilla