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 "nsDocShell.h"
8
#include "nsDSURIContentListener.h"
9
#include "nsIChannel.h"
10
#include "nsServiceManagerUtils.h"
11
#include "nsDocShellCID.h"
12
#include "nsIWebNavigationInfo.h"
13
#include "mozilla/dom/CanonicalBrowsingContext.h"
14
#include "mozilla/dom/Document.h"
15
#include "mozilla/Unused.h"
16
#include "nsError.h"
17
#include "nsContentSecurityManager.h"
18
#include "nsDocShellLoadTypes.h"
19
#include "nsGlobalWindowOuter.h"
20
#include "nsIInterfaceRequestor.h"
21
#include "nsIMultiPartChannel.h"
22
#include "nsWebNavigationInfo.h"
23
24
using namespace mozilla;
25
using namespace mozilla::dom;
26
27
NS_IMPL_ADDREF(MaybeCloseWindowHelper)
28
NS_IMPL_RELEASE(MaybeCloseWindowHelper)
29
30
NS_INTERFACE_MAP_BEGIN(MaybeCloseWindowHelper)
31
NS_INTERFACE_MAP_ENTRY(nsISupports)
32
NS_INTERFACE_MAP_END
33
34
MaybeCloseWindowHelper::MaybeCloseWindowHelper(BrowsingContext* aContentContext)
35
: mBrowsingContext(aContentContext),
36
mTimer(nullptr),
37
mShouldCloseWindow(false) {}
38
39
MaybeCloseWindowHelper::~MaybeCloseWindowHelper() {}
40
41
void MaybeCloseWindowHelper::SetShouldCloseWindow(bool aShouldCloseWindow) {
42
mShouldCloseWindow = aShouldCloseWindow;
43
}
44
45
BrowsingContext* MaybeCloseWindowHelper::MaybeCloseWindow() {
46
if (!mShouldCloseWindow) {
47
return mBrowsingContext;
48
}
49
50
// This method should not be called more than once, but it's better to avoid
51
// closing the current window again.
52
mShouldCloseWindow = false;
53
54
// Reset the window context to the opener window so that the dependent
55
// dialogs have a parent
56
RefPtr<BrowsingContext> newBC = ChooseNewBrowsingContext(mBrowsingContext);
57
58
if (newBC != mBrowsingContext && newBC && !newBC->IsDiscarded()) {
59
mBCToClose = mBrowsingContext;
60
mBrowsingContext = newBC;
61
62
// Now close the old window. Do it on a timer so that we don't run
63
// into issues trying to close the window before it has fully opened.
64
NS_ASSERTION(!mTimer, "mTimer was already initialized once!");
65
NS_NewTimerWithCallback(getter_AddRefs(mTimer), this, 0,
66
nsITimer::TYPE_ONE_SHOT);
67
}
68
69
return mBrowsingContext;
70
}
71
72
already_AddRefed<BrowsingContext>
73
MaybeCloseWindowHelper::ChooseNewBrowsingContext(BrowsingContext* aBC) {
74
RefPtr<BrowsingContext> bc = aBC;
75
76
RefPtr<BrowsingContext> opener = bc->GetOpener();
77
if (opener && !opener->IsDiscarded()) {
78
return opener.forget();
79
}
80
81
if (!XRE_IsParentProcess()) {
82
return bc.forget();
83
}
84
85
CanonicalBrowsingContext* cbc = CanonicalBrowsingContext::Cast(aBC);
86
RefPtr<WindowGlobalParent> wgp = cbc->GetEmbedderWindowGlobal();
87
if (!wgp) {
88
return bc.forget();
89
}
90
91
return do_AddRef(wgp->BrowsingContext());
92
}
93
94
NS_IMETHODIMP
95
MaybeCloseWindowHelper::Notify(nsITimer* timer) {
96
NS_ASSERTION(mBCToClose, "No window to close after timer fired");
97
98
mBCToClose->Close(CallerType::System, IgnoreErrors());
99
mBCToClose = nullptr;
100
mTimer = nullptr;
101
102
return NS_OK;
103
}
104
105
nsDSURIContentListener::nsDSURIContentListener(nsDocShell* aDocShell)
106
: mDocShell(aDocShell),
107
mExistingJPEGRequest(nullptr),
108
mParentContentListener(nullptr) {}
109
110
nsDSURIContentListener::~nsDSURIContentListener() {}
111
112
NS_IMPL_ADDREF(nsDSURIContentListener)
113
NS_IMPL_RELEASE(nsDSURIContentListener)
114
115
NS_INTERFACE_MAP_BEGIN(nsDSURIContentListener)
116
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURIContentListener)
117
NS_INTERFACE_MAP_ENTRY(nsIURIContentListener)
118
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
119
NS_INTERFACE_MAP_END
120
121
NS_IMETHODIMP
122
nsDSURIContentListener::DoContent(const nsACString& aContentType,
123
bool aIsContentPreferred,
124
nsIRequest* aRequest,
125
nsIStreamListener** aContentHandler,
126
bool* aAbortProcess) {
127
nsresult rv;
128
NS_ENSURE_ARG_POINTER(aContentHandler);
129
NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
130
131
*aAbortProcess = false;
132
133
// determine if the channel has just been retargeted to us...
134
nsLoadFlags loadFlags = 0;
135
nsCOMPtr<nsIChannel> aOpenedChannel = do_QueryInterface(aRequest);
136
137
if (aOpenedChannel) {
138
aOpenedChannel->GetLoadFlags(&loadFlags);
139
140
// block top-level data URI navigations if triggered by the web
141
if (!nsContentSecurityManager::AllowTopLevelNavigationToDataURI(
142
aOpenedChannel)) {
143
// logging to console happens within AllowTopLevelNavigationToDataURI
144
aRequest->Cancel(NS_ERROR_DOM_BAD_URI);
145
*aAbortProcess = true;
146
// close the window since the navigation to a data URI was blocked
147
if (mDocShell && mDocShell->GetBrowsingContext()) {
148
RefPtr<MaybeCloseWindowHelper> maybeCloseWindowHelper =
149
new MaybeCloseWindowHelper(mDocShell->GetBrowsingContext());
150
maybeCloseWindowHelper->SetShouldCloseWindow(true);
151
Unused << maybeCloseWindowHelper->MaybeCloseWindow();
152
}
153
return NS_OK;
154
}
155
}
156
157
if (loadFlags & nsIChannel::LOAD_RETARGETED_DOCUMENT_URI) {
158
// XXX: Why does this not stop the content too?
159
mDocShell->Stop(nsIWebNavigation::STOP_NETWORK);
160
161
mDocShell->SetLoadType(aIsContentPreferred ? LOAD_LINK : LOAD_NORMAL);
162
}
163
164
// In case of multipart jpeg request (mjpeg) we don't really want to
165
// create new viewer since the one we already have is capable of
166
// rendering multipart jpeg correctly (see bug 625012)
167
nsCOMPtr<nsIChannel> baseChannel;
168
if (nsCOMPtr<nsIMultiPartChannel> mpchan = do_QueryInterface(aRequest)) {
169
mpchan->GetBaseChannel(getter_AddRefs(baseChannel));
170
}
171
172
bool reuseCV = baseChannel && baseChannel == mExistingJPEGRequest &&
173
aContentType.EqualsLiteral("image/jpeg");
174
175
if (mExistingJPEGStreamListener && reuseCV) {
176
RefPtr<nsIStreamListener> copy(mExistingJPEGStreamListener);
177
copy.forget(aContentHandler);
178
rv = NS_OK;
179
} else {
180
rv =
181
mDocShell->CreateContentViewer(aContentType, aRequest, aContentHandler);
182
if (NS_SUCCEEDED(rv) && reuseCV) {
183
mExistingJPEGStreamListener = *aContentHandler;
184
} else {
185
mExistingJPEGStreamListener = nullptr;
186
}
187
mExistingJPEGRequest = baseChannel;
188
}
189
190
if (rv == NS_ERROR_REMOTE_XUL || rv == NS_ERROR_DOCSHELL_DYING) {
191
aRequest->Cancel(rv);
192
*aAbortProcess = true;
193
return NS_OK;
194
}
195
196
if (NS_FAILED(rv)) {
197
// we don't know how to handle the content
198
*aContentHandler = nullptr;
199
return rv;
200
}
201
202
if (loadFlags & nsIChannel::LOAD_RETARGETED_DOCUMENT_URI) {
203
nsCOMPtr<nsPIDOMWindowOuter> domWindow =
204
mDocShell ? mDocShell->GetWindow() : nullptr;
205
NS_ENSURE_TRUE(domWindow, NS_ERROR_FAILURE);
206
domWindow->Focus(mozilla::dom::CallerType::System);
207
}
208
209
return NS_OK;
210
}
211
212
NS_IMETHODIMP
213
nsDSURIContentListener::IsPreferred(const char* aContentType,
214
char** aDesiredContentType,
215
bool* aCanHandle) {
216
NS_ENSURE_ARG_POINTER(aCanHandle);
217
NS_ENSURE_ARG_POINTER(aDesiredContentType);
218
219
// the docshell has no idea if it is the preferred content provider or not.
220
// It needs to ask its parent if it is the preferred content handler or not...
221
222
nsCOMPtr<nsIURIContentListener> parentListener;
223
GetParentContentListener(getter_AddRefs(parentListener));
224
if (parentListener) {
225
return parentListener->IsPreferred(aContentType, aDesiredContentType,
226
aCanHandle);
227
}
228
// we used to return false here if we didn't have a parent properly registered
229
// at the top of the docshell hierarchy to dictate what content types this
230
// docshell should be a preferred handler for. But this really makes it hard
231
// for developers using iframe or browser tags because then they need to make
232
// sure they implement nsIURIContentListener otherwise all link clicks would
233
// get sent to another window because we said we weren't the preferred handler
234
// type. I'm going to change the default now... if we can handle the content,
235
// and someone didn't EXPLICITLY set a nsIURIContentListener at the top of our
236
// docshell chain, then we'll now always attempt to process the content
237
// ourselves...
238
return CanHandleContent(aContentType, true, aDesiredContentType, aCanHandle);
239
}
240
241
NS_IMETHODIMP
242
nsDSURIContentListener::CanHandleContent(const char* aContentType,
243
bool aIsContentPreferred,
244
char** aDesiredContentType,
245
bool* aCanHandleContent) {
246
MOZ_ASSERT(aCanHandleContent, "Null out param?");
247
NS_ENSURE_ARG_POINTER(aDesiredContentType);
248
249
*aCanHandleContent = false;
250
*aDesiredContentType = nullptr;
251
252
if (aContentType) {
253
uint32_t canHandle = nsWebNavigationInfo::IsTypeSupported(
254
nsDependentCString(aContentType), mDocShell);
255
*aCanHandleContent = (canHandle != nsIWebNavigationInfo::UNSUPPORTED);
256
}
257
258
return NS_OK;
259
}
260
261
NS_IMETHODIMP
262
nsDSURIContentListener::GetLoadCookie(nsISupports** aLoadCookie) {
263
NS_IF_ADDREF(*aLoadCookie = nsDocShell::GetAsSupports(mDocShell));
264
return NS_OK;
265
}
266
267
NS_IMETHODIMP
268
nsDSURIContentListener::SetLoadCookie(nsISupports* aLoadCookie) {
269
#ifdef DEBUG
270
RefPtr<nsDocLoader> cookieAsDocLoader =
271
nsDocLoader::GetAsDocLoader(aLoadCookie);
272
NS_ASSERTION(cookieAsDocLoader && cookieAsDocLoader == mDocShell,
273
"Invalid load cookie being set!");
274
#endif
275
return NS_OK;
276
}
277
278
NS_IMETHODIMP
279
nsDSURIContentListener::GetParentContentListener(
280
nsIURIContentListener** aParentListener) {
281
if (mWeakParentContentListener) {
282
nsCOMPtr<nsIURIContentListener> tempListener =
283
do_QueryReferent(mWeakParentContentListener);
284
*aParentListener = tempListener;
285
NS_IF_ADDREF(*aParentListener);
286
} else {
287
*aParentListener = mParentContentListener;
288
NS_IF_ADDREF(*aParentListener);
289
}
290
return NS_OK;
291
}
292
293
NS_IMETHODIMP
294
nsDSURIContentListener::SetParentContentListener(
295
nsIURIContentListener* aParentListener) {
296
if (aParentListener) {
297
// Store the parent listener as a weak ref. Parents not supporting
298
// nsISupportsWeakReference assert but may still be used.
299
mParentContentListener = nullptr;
300
mWeakParentContentListener = do_GetWeakReference(aParentListener);
301
if (!mWeakParentContentListener) {
302
mParentContentListener = aParentListener;
303
}
304
} else {
305
mWeakParentContentListener = nullptr;
306
mParentContentListener = nullptr;
307
}
308
return NS_OK;
309
}