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"
13 GlobalTeardownObserver::GlobalTeardownObserver() = default;
14 GlobalTeardownObserver::GlobalTeardownObserver(nsIGlobalObject
* aGlobalObject
,
15 bool aHasOrHasHadOwnerWindow
)
16 : mHasOrHasHadOwnerWindow(aHasOrHasHadOwnerWindow
) {
17 BindToOwner(aGlobalObject
);
20 GlobalTeardownObserver::~GlobalTeardownObserver() {
22 mParentObject
->RemoveGlobalTeardownObserver(this);
26 void GlobalTeardownObserver::BindToOwner(nsIGlobalObject
* aOwner
) {
27 MOZ_ASSERT(!mParentObject
);
30 mParentObject
= aOwner
;
31 aOwner
->AddGlobalTeardownObserver(this);
32 // Let's cache the result of this QI for fast access and off main thread
35 nsCOMPtr
<nsPIDOMWindowInner
>(do_QueryInterface(aOwner
)).get();
37 mHasOrHasHadOwnerWindow
= true;
42 void GlobalTeardownObserver::DisconnectFromOwner() {
44 mParentObject
->RemoveGlobalTeardownObserver(this);
46 mOwnerWindow
= nullptr;
47 mParentObject
= nullptr;
50 nsresult
GlobalTeardownObserver::CheckCurrentGlobalCorrectness() const {
51 NS_ENSURE_STATE(!mHasOrHasHadOwnerWindow
|| mOwnerWindow
);
54 if (mOwnerWindow
&& !mOwnerWindow
->IsCurrentInnerWindow()) {
55 return NS_ERROR_FAILURE
;
58 if (NS_IsMainThread()) {
63 return NS_ERROR_FAILURE
;
66 if (mParentObject
->IsDying()) {
67 return NS_ERROR_FAILURE
;
73 }; // namespace mozilla