1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/net/DocumentChannel.h"
#include <inttypes.h>
#include "mozIDOMWindow.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/Logging.h"
#include "mozilla/RefPtr.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/Document.h"
#include "mozilla/net/DocumentChannelChild.h"
#include "mozilla/net/ParentProcessDocumentChannel.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsDocShell.h"
#include "nsDocShellLoadState.h"
#include "nsHttpHandler.h"
#include "nsIContentPolicy.h"
#include "nsIInterfaceRequestor.h"
#include "nsILoadContext.h"
#include "nsILoadGroup.h"
#include "nsILoadInfo.h"
#include "nsIStreamListener.h"
#include "nsIURI.h"
#include "nsLoadGroup.h"
#include "nsMimeTypes.h"
#include "nsNetUtil.h"
#include "nsPIDOMWindow.h"
#include "nsPIDOMWindowInlines.h"
#include "nsStringFwd.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "nscore.h"
using namespace mozilla::dom;
using namespace mozilla::ipc;
extern mozilla::LazyLogModule gDocumentChannelLog;
#define LOG(fmt) MOZ_LOG(gDocumentChannelLog, mozilla::LogLevel::Verbose, fmt)
namespace mozilla {
namespace net {
//-----------------------------------------------------------------------------
// DocumentChannel::nsISupports
NS_IMPL_ADDREF(DocumentChannel)
NS_IMPL_RELEASE(DocumentChannel)
NS_INTERFACE_MAP_BEGIN(DocumentChannel)
NS_INTERFACE_MAP_ENTRY(nsIRequest)
NS_INTERFACE_MAP_ENTRY(nsIChannel)
NS_INTERFACE_MAP_ENTRY(nsIIdentChannel)
NS_INTERFACE_MAP_ENTRY_CONCRETE(DocumentChannel)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIRequest)
NS_INTERFACE_MAP_END
DocumentChannel::DocumentChannel(nsDocShellLoadState* aLoadState,
net::LoadInfo* aLoadInfo,
nsLoadFlags aLoadFlags, uint32_t aCacheKey,
bool aUriModified,
bool aIsEmbeddingBlockedError)
: mLoadState(aLoadState),
mCacheKey(aCacheKey),
mLoadFlags(aLoadFlags),
mURI(aLoadState->URI()),
mLoadInfo(aLoadInfo),
mUriModified(aUriModified),
mIsEmbeddingBlockedError(aIsEmbeddingBlockedError) {
LOG(("DocumentChannel ctor [this=%p, uri=%s]", this,
aLoadState->URI()->GetSpecOrDefault().get()));
RefPtr<nsHttpHandler> handler = nsHttpHandler::GetInstance();
mChannelId = handler->NewChannelId();
}
NS_IMETHODIMP
DocumentChannel::AsyncOpen(nsIStreamListener* aListener) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
void DocumentChannel::ShutdownListeners(nsresult aStatusCode) {
LOG(("DocumentChannel ShutdownListeners [this=%p, status=%" PRIx32 "]", this,
static_cast<uint32_t>(aStatusCode)));
mStatus = aStatusCode;
nsCOMPtr<nsIStreamListener> listener = mListener;
if (listener) {
listener->OnStartRequest(this);
}
mIsPending = false;
listener = mListener; // it might have changed!
nsCOMPtr<nsILoadGroup> loadGroup = mLoadGroup;
mListener = nullptr;
mLoadGroup = nullptr;
mCallbacks = nullptr;
NS_DispatchToMainThread(NS_NewRunnableFunction(
"DocumentChannel::ShutdownListeners", [=, self = RefPtr{this}] {
if (listener) {
listener->OnStopRequest(self, aStatusCode);
}
if (loadGroup) {
loadGroup->RemoveRequest(self, nullptr, aStatusCode);
}
}));
DeleteIPDL();
}
void DocumentChannel::DisconnectChildListeners(
const nsresult& aStatus, const nsresult& aLoadGroupStatus) {
MOZ_ASSERT(NS_FAILED(aStatus));
// In the case where the channel was redirected to be downloaded by the
// nsExternalHelperAppService in the parent process, we'll be called with a
// different aLoadGroupStatus and aStatus.
//
// Before DocumentChannel was implemented, the channel would have been removed
// from the load group by the nsExternalHelperAppService. This simulates that
// behaviour by removing the load group when the channel is no longer in use.
//
// We cannot unconditionally remove the channel from the load group here, as
// that may unblock load events too early if new navigations will be started
// by channel listeners. See bug 1961008.
if (aStatus != aLoadGroupStatus) {
MOZ_ASSERT(aStatus == NS_BINDING_RETARGETED);
MOZ_ASSERT(NS_SUCCEEDED(aLoadGroupStatus));
mStatus = aLoadGroupStatus;
if (mLoadGroup) {
mLoadGroup->RemoveRequest(this, nullptr, aStatus);
mLoadGroup = nullptr;
}
}
ShutdownListeners(aStatus);
}
nsDocShell* DocumentChannel::GetDocShell() {
nsCOMPtr<nsILoadContext> loadContext;
NS_QueryNotificationCallbacks(this, loadContext);
if (!loadContext) {
return nullptr;
}
nsCOMPtr<mozIDOMWindowProxy> domWindow;
loadContext->GetAssociatedWindow(getter_AddRefs(domWindow));
if (!domWindow) {
return nullptr;
}
auto* pDomWindow = nsPIDOMWindowOuter::From(domWindow);
nsIDocShell* docshell = pDomWindow->GetDocShell();
return nsDocShell::Cast(docshell);
}
static bool URIUsesDocChannel(nsIURI* aURI) {
if (aURI->SchemeIs("javascript")) {
return false;
}
nsCString spec = aURI->GetSpecOrDefault();
return
#ifdef MOZ_WIDGET_ANDROID
!spec.EqualsLiteral("about:crashcontentjava") &&
#endif
!spec.EqualsLiteral("about:crashcontent");
}
bool DocumentChannel::CanUseDocumentChannel(nsIURI* aURI) {
// We want to use DocumentChannel if we're using a supported scheme.
return URIUsesDocChannel(aURI);
}
/* static */
already_AddRefed<DocumentChannel> DocumentChannel::CreateForDocument(
nsDocShellLoadState* aLoadState, class LoadInfo* aLoadInfo,
nsLoadFlags aLoadFlags, nsIInterfaceRequestor* aNotificationCallbacks,
uint32_t aCacheKey, bool aUriModified, bool aIsEmbeddingBlockedError) {
RefPtr<DocumentChannel> channel;
if (XRE_IsContentProcess()) {
channel =
new DocumentChannelChild(aLoadState, aLoadInfo, aLoadFlags, aCacheKey,
aUriModified, aIsEmbeddingBlockedError);
} else {
channel = new ParentProcessDocumentChannel(
aLoadState, aLoadInfo, aLoadFlags, aCacheKey, aUriModified,
aIsEmbeddingBlockedError);
}
channel->SetNotificationCallbacks(aNotificationCallbacks);
return channel.forget();
}
/* static */
already_AddRefed<DocumentChannel> DocumentChannel::CreateForObject(
nsDocShellLoadState* aLoadState, class LoadInfo* aLoadInfo,
nsLoadFlags aLoadFlags, nsIInterfaceRequestor* aNotificationCallbacks) {
return CreateForDocument(aLoadState, aLoadInfo, aLoadFlags,
aNotificationCallbacks, 0, false, false);
}
NS_IMETHODIMP DocumentChannel::SetCanceledReason(const nsACString& aReason) {
return SetCanceledReasonImpl(aReason);
}
NS_IMETHODIMP DocumentChannel::GetCanceledReason(nsACString& aReason) {
return GetCanceledReasonImpl(aReason);
}
NS_IMETHODIMP DocumentChannel::CancelWithReason(nsresult aStatus,
const nsACString& aReason) {
return CancelWithReasonImpl(aStatus, aReason);
}
NS_IMETHODIMP
DocumentChannel::Cancel(nsresult aStatusCode) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
DocumentChannel::Suspend() {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
DocumentChannel::Resume() {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
//-----------------------------------------------------------------------------
// Remainder of nsIRequest/nsIChannel.
//-----------------------------------------------------------------------------
NS_IMETHODIMP DocumentChannel::GetNotificationCallbacks(
nsIInterfaceRequestor** aCallbacks) {
nsCOMPtr<nsIInterfaceRequestor> callbacks(mCallbacks);
callbacks.forget(aCallbacks);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::SetNotificationCallbacks(
nsIInterfaceRequestor* aNotificationCallbacks) {
mCallbacks = aNotificationCallbacks;
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetLoadGroup(nsILoadGroup** aLoadGroup) {
nsCOMPtr<nsILoadGroup> loadGroup(mLoadGroup);
loadGroup.forget(aLoadGroup);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::SetLoadGroup(nsILoadGroup* aLoadGroup) {
mLoadGroup = aLoadGroup;
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetStatus(nsresult* aStatus) {
*aStatus = mStatus;
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetName(nsACString& aResult) {
if (!mURI) {
aResult.Truncate();
return NS_OK;
}
nsCString spec;
nsresult rv = mURI->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
aResult.AssignLiteral("documentchannel:");
aResult.Append(spec);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::IsPending(bool* aResult) {
*aResult = mIsPending;
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetLoadFlags(nsLoadFlags* aLoadFlags) {
*aLoadFlags = mLoadFlags;
return NS_OK;
}
NS_IMETHODIMP
DocumentChannel::GetTRRMode(nsIRequest::TRRMode* aTRRMode) {
return GetTRRModeImpl(aTRRMode);
}
NS_IMETHODIMP
DocumentChannel::SetTRRMode(nsIRequest::TRRMode aTRRMode) {
return SetTRRModeImpl(aTRRMode);
}
NS_IMETHODIMP DocumentChannel::SetLoadFlags(nsLoadFlags aLoadFlags) {
nsLoadFlags mayChange = 0;
if (mLoadInfo->GetExternalContentPolicyType() ==
ExtContentPolicy::TYPE_OBJECT) {
// Setting load flags for TYPE_OBJECT is OK, so long as the channel to
// parent isn't opened yet, or we're only setting the `LOAD_DOCUMENT_URI`
// flag.
mayChange = mWasOpened ? LOAD_DOCUMENT_URI : ~0u;
} else if (!mWasOpened) {
// If we haven't been opened yet, allow the LoadGroup to
// set cache control flags inherited from the default channel.
mayChange = nsLoadGroup::kInheritedLoadFlags;
}
// Check if we're allowed to adjust these flags.
if ((mLoadFlags & ~mayChange) == (aLoadFlags & ~mayChange)) {
mLoadFlags = aLoadFlags;
return NS_OK;
}
MOZ_CRASH_UNSAFE_PRINTF(
"DocumentChannel::SetLoadFlags: Don't set flags after creation "
"(differing flags %x != %x)",
(mLoadFlags ^ aLoadFlags) & mLoadFlags,
(mLoadFlags ^ aLoadFlags) & aLoadFlags);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetOriginalURI(nsIURI** aOriginalURI) {
nsCOMPtr<nsIURI> originalURI =
mLoadState->OriginalURI() ? mLoadState->OriginalURI() : mLoadState->URI();
originalURI.forget(aOriginalURI);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::SetOriginalURI(nsIURI* aOriginalURI) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetURI(nsIURI** aURI) {
nsCOMPtr<nsIURI> uri(mURI);
uri.forget(aURI);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetOwner(nsISupports** aOwner) {
nsCOMPtr<nsISupports> owner(mOwner);
owner.forget(aOwner);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::SetOwner(nsISupports* aOwner) {
mOwner = aOwner;
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetSecurityInfo(
nsITransportSecurityInfo** aSecurityInfo) {
*aSecurityInfo = nullptr;
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::GetContentType(nsACString& aContentType) {
// We may be trying to load HTML object data, and have determined that we're
// going to be performing a document load. In that case, fake the "text/html"
// content type for nsObjectLoadingContent.
if ((mLoadFlags & nsIRequest::LOAD_HTML_OBJECT_DATA) &&
(mLoadFlags & nsIChannel::LOAD_DOCUMENT_URI)) {
aContentType = TEXT_HTML;
return NS_OK;
}
NS_ERROR("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::SetContentType(const nsACString& aContentType) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetContentCharset(nsACString& aContentCharset) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::SetContentCharset(
const nsACString& aContentCharset) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetContentLength(int64_t* aContentLength) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::SetContentLength(int64_t aContentLength) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::Open(nsIInputStream** aStream) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetContentDisposition(
uint32_t* aContentDisposition) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::SetContentDisposition(
uint32_t aContentDisposition) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetContentDispositionFilename(
nsAString& aContentDispositionFilename) {
MOZ_CRASH("If we get here, something will be broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::SetContentDispositionFilename(
const nsAString& aContentDispositionFilename) {
MOZ_CRASH("If we get here, something will be broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetContentDispositionHeader(
nsACString& aContentDispositionHeader) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetLoadInfo(nsILoadInfo** aLoadInfo) {
nsCOMPtr<nsILoadInfo> loadInfo(mLoadInfo);
loadInfo.forget(aLoadInfo);
return NS_OK;
}
NS_IMETHODIMP DocumentChannel::SetLoadInfo(nsILoadInfo* aLoadInfo) {
MOZ_CRASH("If we get here, something is broken");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP DocumentChannel::GetIsDocument(bool* aIsDocument) {
return NS_GetIsDocumentChannel(this, aIsDocument);
}
NS_IMETHODIMP DocumentChannel::GetCanceled(bool* aCanceled) {
*aCanceled = mCanceled;
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsIIdentChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
DocumentChannel::GetChannelId(uint64_t* aChannelId) {
*aChannelId = mChannelId;
return NS_OK;
}
NS_IMETHODIMP
DocumentChannel::SetChannelId(uint64_t aChannelId) {
mChannelId = aChannelId;
return NS_OK;
}
//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
uint64_t InnerWindowIDForExtantDoc(nsDocShell* docShell) {
if (!docShell) {
return 0;
}
Document* doc = docShell->GetExtantDocument();
if (!doc) {
return 0;
}
return doc->InnerWindowID();
}
} // namespace net
} // namespace mozilla
#undef LOG
|