Source code

Revision control

Other Tools

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 "nsCOMPtr.h"
8
#include "nsIContent.h"
9
#include "nsFrameLoader.h"
10
#include "mozilla/AsyncEventDispatcher.h"
11
#include "mozilla/dom/HTMLIFrameElement.h"
12
#include "mozilla/dom/WindowProxyHolder.h"
13
#include "mozilla/dom/XULFrameElement.h"
14
#include "mozilla/dom/XULFrameElementBinding.h"
15
16
namespace mozilla {
17
namespace dom {
18
19
NS_IMPL_CYCLE_COLLECTION_CLASS(XULFrameElement)
20
21
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(XULFrameElement, nsXULElement)
22
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFrameLoader);
23
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOpener);
24
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
25
26
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(XULFrameElement, nsXULElement)
27
if (tmp->mFrameLoader) {
28
tmp->mFrameLoader->Destroy();
29
}
30
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFrameLoader, mOpener)
31
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
32
33
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(XULFrameElement, nsXULElement,
34
nsFrameLoaderOwner)
35
36
JSObject* XULFrameElement::WrapNode(JSContext* aCx,
37
JS::Handle<JSObject*> aGivenProto) {
38
return XULFrameElement_Binding::Wrap(aCx, this, aGivenProto);
39
}
40
41
nsDocShell* XULFrameElement::GetDocShell() {
42
RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
43
return frameLoader ? frameLoader->GetDocShell(IgnoreErrors()) : nullptr;
44
}
45
46
already_AddRefed<nsIWebNavigation> XULFrameElement::GetWebNavigation() {
47
nsCOMPtr<nsIDocShell> docShell = GetDocShell();
48
nsCOMPtr<nsIWebNavigation> webnav = do_QueryInterface(docShell);
49
return webnav.forget();
50
}
51
52
Nullable<WindowProxyHolder> XULFrameElement::GetContentWindow() {
53
RefPtr<nsDocShell> docShell = GetDocShell();
54
if (docShell) {
55
return docShell->GetWindowProxy();
56
}
57
58
return nullptr;
59
}
60
61
Document* XULFrameElement::GetContentDocument() {
62
nsCOMPtr<nsIDocShell> docShell = GetDocShell();
63
if (docShell) {
64
nsCOMPtr<nsPIDOMWindowOuter> win = docShell->GetWindow();
65
if (win) {
66
return win->GetDoc();
67
}
68
}
69
return nullptr;
70
}
71
72
void XULFrameElement::LoadSrc() {
73
if (!IsInUncomposedDoc() || !OwnerDoc()->GetRootElement()) {
74
return;
75
}
76
RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
77
if (!frameLoader) {
78
// Check if we have an opener we need to be setting
79
RefPtr<BrowsingContext> opener = mOpener;
80
if (!opener) {
81
// If we are a primary xul-browser, we want to take the opener property!
82
nsCOMPtr<nsPIDOMWindowOuter> window = OwnerDoc()->GetWindow();
83
if (AttrValueIs(kNameSpaceID_None, nsGkAtoms::primary, nsGkAtoms::_true,
84
eIgnoreCase) &&
85
window) {
86
opener = window->TakeOpenerForInitialContentBrowser();
87
}
88
}
89
mOpener = nullptr;
90
91
// false as the last parameter so that xul:iframe/browser/editor
92
// session history handling works like dynamic html:iframes.
93
// Usually xul elements are used in chrome, which doesn't have
94
// session history at all.
95
mFrameLoader = nsFrameLoader::Create(this, opener, false);
96
if (NS_WARN_IF(!mFrameLoader)) {
97
return;
98
}
99
100
(new AsyncEventDispatcher(this, NS_LITERAL_STRING("XULFrameLoaderCreated"),
101
CanBubble::eYes))
102
->RunDOMEventWhenSafe();
103
}
104
105
mFrameLoader->LoadFrame(false);
106
}
107
108
void XULFrameElement::SwapFrameLoaders(HTMLIFrameElement& aOtherLoaderOwner,
109
ErrorResult& rv) {
110
aOtherLoaderOwner.SwapFrameLoaders(this, rv);
111
}
112
113
void XULFrameElement::SwapFrameLoaders(XULFrameElement& aOtherLoaderOwner,
114
ErrorResult& rv) {
115
if (&aOtherLoaderOwner == this) {
116
// nothing to do
117
return;
118
}
119
120
aOtherLoaderOwner.SwapFrameLoaders(this, rv);
121
}
122
123
void XULFrameElement::SwapFrameLoaders(nsFrameLoaderOwner* aOtherLoaderOwner,
124
mozilla::ErrorResult& rv) {
125
if (RefPtr<Document> doc = GetComposedDoc()) {
126
// SwapWithOtherLoader relies on frames being up-to-date.
127
doc->FlushPendingNotifications(FlushType::Frames);
128
}
129
130
RefPtr<nsFrameLoader> loader = GetFrameLoader();
131
RefPtr<nsFrameLoader> otherLoader = aOtherLoaderOwner->GetFrameLoader();
132
if (!loader || !otherLoader) {
133
rv.Throw(NS_ERROR_NOT_IMPLEMENTED);
134
return;
135
}
136
137
rv = loader->SwapWithOtherLoader(otherLoader, this, aOtherLoaderOwner);
138
}
139
140
nsresult XULFrameElement::BindToTree(BindContext& aContext, nsINode& aParent) {
141
nsresult rv = nsXULElement::BindToTree(aContext, aParent);
142
NS_ENSURE_SUCCESS(rv, rv);
143
144
if (IsInUncomposedDoc()) {
145
NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(),
146
"Missing a script blocker!");
147
// We're in a document now. Kick off the frame load.
148
LoadSrc();
149
}
150
151
return NS_OK;
152
}
153
154
void XULFrameElement::UnbindFromTree(bool aNullParent) {
155
RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
156
if (frameLoader) {
157
frameLoader->Destroy();
158
}
159
mFrameLoader = nullptr;
160
161
nsXULElement::UnbindFromTree(aNullParent);
162
}
163
164
void XULFrameElement::DestroyContent() {
165
RefPtr<nsFrameLoader> frameLoader = GetFrameLoader();
166
if (frameLoader) {
167
frameLoader->Destroy();
168
}
169
mFrameLoader = nullptr;
170
171
nsXULElement::DestroyContent();
172
}
173
174
nsresult XULFrameElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
175
const nsAttrValue* aValue,
176
const nsAttrValue* aOldValue,
177
nsIPrincipal* aSubjectPrincipal,
178
bool aNotify) {
179
if (aNamespaceID == kNameSpaceID_None && aName == nsGkAtoms::src && aValue) {
180
LoadSrc();
181
}
182
183
return nsXULElement::AfterSetAttr(aNamespaceID, aName, aValue, aOldValue,
184
aSubjectPrincipal, aNotify);
185
}
186
187
} // namespace dom
188
} // namespace mozilla