1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 template<class T>
6 nsPIDOMWindowInner*
AsInner()7 nsPIDOMWindow<T>::AsInner()
8 {
9   MOZ_ASSERT(IsInnerWindow());
10   return reinterpret_cast<nsPIDOMWindowInner*>(this);
11 }
12 
13 template<class T>
14 const nsPIDOMWindowInner*
AsInner()15 nsPIDOMWindow<T>::AsInner() const
16 {
17   MOZ_ASSERT(IsInnerWindow());
18   return reinterpret_cast<const nsPIDOMWindowInner*>(this);
19 }
20 
21 template<class T>
22 nsPIDOMWindowOuter*
AsOuter()23 nsPIDOMWindow<T>::AsOuter()
24 {
25   MOZ_ASSERT(IsOuterWindow());
26   return reinterpret_cast<nsPIDOMWindowOuter*>(this);
27 }
28 
29 template<class T>
30 const nsPIDOMWindowOuter*
AsOuter()31 nsPIDOMWindow<T>::AsOuter() const
32 {
33   MOZ_ASSERT(IsOuterWindow());
34   return reinterpret_cast<const nsPIDOMWindowOuter*>(this);
35 }
36 
37 template <class T>
38 bool
IsLoadingOrRunningTimeout()39 nsPIDOMWindow<T>::IsLoadingOrRunningTimeout() const
40 {
41   if (IsOuterWindow()) {
42     return AsOuter()->GetCurrentInnerWindow()->IsLoadingOrRunningTimeout();
43   }
44   return !mIsDocumentLoaded || mRunningTimeout;
45 }
46 
47 template <class T>
48 bool
IsLoading()49 nsPIDOMWindow<T>::IsLoading() const
50 {
51   if (IsOuterWindow()) {
52     auto* win = AsOuter()->GetCurrentInnerWindow();
53 
54     if (!win) {
55       NS_ERROR("No current inner window available!");
56 
57       return false;
58     }
59 
60     return win->IsLoading();
61   }
62 
63   if (!mOuterWindow) {
64     NS_ERROR("IsLoading() called on orphan inner window!");
65 
66     return false;
67   }
68 
69   return !mIsDocumentLoaded;
70 }
71 
72 template <class T>
73 bool
IsHandlingResizeEvent()74 nsPIDOMWindow<T>::IsHandlingResizeEvent() const
75 {
76   if (IsOuterWindow()) {
77     auto* win = AsOuter()->GetCurrentInnerWindow();
78 
79     if (!win) {
80       NS_ERROR("No current inner window available!");
81 
82       return false;
83     }
84 
85     return win->IsHandlingResizeEvent();
86   }
87 
88   if (!mOuterWindow) {
89     NS_ERROR("IsHandlingResizeEvent() called on orphan inner window!");
90 
91     return false;
92   }
93 
94   return mIsHandlingResizeEvent;
95 }
96 
97 bool
IsCurrentInnerWindow()98 nsPIDOMWindowInner::IsCurrentInnerWindow() const
99 {
100   return mOuterWindow && mOuterWindow->GetCurrentInnerWindow() == AsInner();
101 }
102 
103 bool
HasActiveDocument()104 nsPIDOMWindowInner::HasActiveDocument()
105 {
106   return IsCurrentInnerWindow() ||
107     (mOuterWindow &&
108      mOuterWindow->GetCurrentInnerWindow() &&
109      mOuterWindow->GetCurrentInnerWindow()->GetDoc() == mDoc);
110 }
111 
112 template <class T>
113 nsIDocShell*
GetDocShell()114 nsPIDOMWindow<T>::GetDocShell() const
115 {
116   if (mOuterWindow) {
117     return mOuterWindow->GetDocShell();
118   }
119 
120   return mDocShell;
121 }
122 
123 template <class T>
124 nsIContent*
GetFocusedNode()125 nsPIDOMWindow<T>::GetFocusedNode() const
126 {
127   if (IsOuterWindow()) {
128     return mInnerWindow ? mInnerWindow->GetFocusedNode() : nullptr;
129   }
130 
131   return mFocusedNode;
132 }
133