2020-04-24 01:58:23 +00:00
|
|
|
/* -*- 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 "ParentProcessDocumentChannel.h"
|
|
|
|
|
2021-10-14 12:57:59 +00:00
|
|
|
#include "mozilla/extensions/StreamFilterParent.h"
|
2020-06-27 04:09:35 +00:00
|
|
|
#include "mozilla/net/ParentChannelWrapper.h"
|
|
|
|
#include "mozilla/net/UrlClassifierCommon.h"
|
2020-05-28 00:07:39 +00:00
|
|
|
#include "mozilla/StaticPrefs_extensions.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "nsCRT.h"
|
2020-05-28 00:07:39 +00:00
|
|
|
#include "nsDocShell.h"
|
2020-04-24 01:45:55 +00:00
|
|
|
#include "nsIObserverService.h"
|
2020-06-12 21:03:06 +00:00
|
|
|
#include "nsIClassifiedChannel.h"
|
2020-09-28 12:01:12 +00:00
|
|
|
#include "nsIXULRuntime.h"
|
2021-10-14 12:57:59 +00:00
|
|
|
#include "nsHttpHandler.h"
|
|
|
|
#include "nsDocShellLoadState.h"
|
2020-04-24 01:45:55 +00:00
|
|
|
|
2020-04-24 01:58:23 +00:00
|
|
|
extern mozilla::LazyLogModule gDocumentChannelLog;
|
|
|
|
#define LOG(fmt) MOZ_LOG(gDocumentChannelLog, mozilla::LogLevel::Verbose, fmt)
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2020-06-12 05:56:36 +00:00
|
|
|
using RedirectToRealChannelPromise =
|
|
|
|
typename PDocumentChannelParent::RedirectToRealChannelPromise;
|
|
|
|
|
2020-04-24 01:58:23 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(ParentProcessDocumentChannel, DocumentChannel,
|
2020-04-24 01:45:55 +00:00
|
|
|
nsIAsyncVerifyRedirectCallback, nsIObserver)
|
2020-04-24 01:58:23 +00:00
|
|
|
|
|
|
|
ParentProcessDocumentChannel::ParentProcessDocumentChannel(
|
|
|
|
nsDocShellLoadState* aLoadState, class LoadInfo* aLoadInfo,
|
2020-04-27 05:38:15 +00:00
|
|
|
nsLoadFlags aLoadFlags, uint32_t aCacheKey, bool aUriModified,
|
2023-12-19 10:05:12 +00:00
|
|
|
bool aIsEmbeddingBlockedError)
|
2020-04-27 05:38:15 +00:00
|
|
|
: DocumentChannel(aLoadState, aLoadInfo, aLoadFlags, aCacheKey,
|
2023-12-19 10:05:12 +00:00
|
|
|
aUriModified, aIsEmbeddingBlockedError) {
|
2020-04-24 01:58:23 +00:00
|
|
|
LOG(("ParentProcessDocumentChannel ctor [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ParentProcessDocumentChannel::~ParentProcessDocumentChannel() {
|
|
|
|
LOG(("ParentProcessDocumentChannel dtor [this=%p]", this));
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:56:36 +00:00
|
|
|
RefPtr<RedirectToRealChannelPromise>
|
2020-04-24 01:58:23 +00:00
|
|
|
ParentProcessDocumentChannel::RedirectToRealChannel(
|
|
|
|
nsTArray<ipc::Endpoint<extensions::PStreamFilterParent>>&&
|
|
|
|
aStreamFilterEndpoints,
|
2022-12-02 16:15:56 +00:00
|
|
|
uint32_t aRedirectFlags, uint32_t aLoadFlags,
|
|
|
|
const nsTArray<EarlyHintConnectArgs>& aEarlyHints) {
|
2020-04-24 01:58:23 +00:00
|
|
|
LOG(("ParentProcessDocumentChannel RedirectToRealChannel [this=%p]", this));
|
|
|
|
nsCOMPtr<nsIChannel> channel = mDocumentLoadListener->GetChannel();
|
|
|
|
channel->SetLoadFlags(aLoadFlags);
|
|
|
|
channel->SetNotificationCallbacks(mCallbacks);
|
|
|
|
|
|
|
|
if (mLoadGroup) {
|
|
|
|
channel->SetLoadGroup(mLoadGroup);
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:07:39 +00:00
|
|
|
if (XRE_IsE10sParentProcess()) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_GetFinalChannelURI(channel, getter_AddRefs(uri)));
|
|
|
|
if (!nsDocShell::CanLoadInParentProcess(uri)) {
|
|
|
|
nsAutoCString msg;
|
|
|
|
uri->GetSpec(msg);
|
|
|
|
msg.Insert(
|
|
|
|
"Attempt to load a non-authorised load in the parent process: ", 0);
|
|
|
|
NS_ASSERTION(false, msg.get());
|
2020-06-12 05:56:36 +00:00
|
|
|
return RedirectToRealChannelPromise::CreateAndResolve(
|
|
|
|
NS_ERROR_CONTENT_BLOCKED, __func__);
|
2020-05-28 00:07:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 01:58:23 +00:00
|
|
|
mStreamFilterEndpoints = std::move(aStreamFilterEndpoints);
|
|
|
|
|
2020-09-09 00:40:38 +00:00
|
|
|
if (mDocumentLoadListener->IsDocumentLoad() &&
|
2021-01-26 12:41:22 +00:00
|
|
|
mozilla::SessionHistoryInParent() && GetDocShell() &&
|
|
|
|
mDocumentLoadListener->GetLoadingSessionHistoryInfo()) {
|
2020-09-09 00:40:38 +00:00
|
|
|
GetDocShell()->SetLoadingSessionHistoryInfo(
|
|
|
|
*mDocumentLoadListener->GetLoadingSessionHistoryInfo());
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:56:36 +00:00
|
|
|
RefPtr<RedirectToRealChannelPromise> p = mPromise.Ensure(__func__);
|
2020-06-12 05:57:02 +00:00
|
|
|
// We make the promise use direct task dispatch in order to reduce the number
|
|
|
|
// of event loops iterations.
|
|
|
|
mPromise.UseDirectTaskDispatch(__func__);
|
2020-04-24 01:58:23 +00:00
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
gHttpHandler->AsyncOnChannelRedirect(this, channel, aRedirectFlags);
|
|
|
|
if (NS_FAILED(rv)) {
|
2020-06-12 05:56:28 +00:00
|
|
|
LOG(
|
|
|
|
("ParentProcessDocumentChannel RedirectToRealChannel "
|
|
|
|
"AsyncOnChannelRedirect failed [this=%p "
|
|
|
|
"aRv=%d]",
|
|
|
|
this, int(rv)));
|
2020-04-24 01:58:23 +00:00
|
|
|
OnRedirectVerifyCallback(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ParentProcessDocumentChannel::OnRedirectVerifyCallback(nsresult aResult) {
|
|
|
|
LOG(
|
|
|
|
("ParentProcessDocumentChannel OnRedirectVerifyCallback [this=%p "
|
|
|
|
"aResult=%d]",
|
|
|
|
this, int(aResult)));
|
2020-04-28 12:29:52 +00:00
|
|
|
|
2020-06-12 05:56:28 +00:00
|
|
|
MOZ_ASSERT(mDocumentLoadListener);
|
2020-04-24 01:58:23 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(aResult)) {
|
|
|
|
Cancel(aResult);
|
2020-06-12 05:56:28 +00:00
|
|
|
} else if (mCanceled) {
|
2020-05-28 10:22:31 +00:00
|
|
|
aResult = NS_ERROR_ABORT;
|
2020-04-24 01:58:23 +00:00
|
|
|
} else {
|
|
|
|
const nsCOMPtr<nsIChannel> channel = mDocumentLoadListener->GetChannel();
|
|
|
|
mLoadGroup->AddRequest(channel, nullptr);
|
|
|
|
// Adding the channel to the loadgroup could have triggered a status
|
|
|
|
// change with an observer being called destroying the docShell, resulting
|
|
|
|
// in the PPDC to be canceled.
|
2020-06-12 05:56:36 +00:00
|
|
|
if (mCanceled) {
|
|
|
|
aResult = NS_ERROR_ABORT;
|
|
|
|
} else {
|
2020-04-24 01:58:23 +00:00
|
|
|
mLoadGroup->RemoveRequest(this, nullptr, NS_BINDING_REDIRECTED);
|
|
|
|
for (auto& endpoint : mStreamFilterEndpoints) {
|
|
|
|
extensions::StreamFilterParent::Attach(channel, std::move(endpoint));
|
|
|
|
}
|
2020-06-12 21:03:06 +00:00
|
|
|
|
|
|
|
RefPtr<ParentChannelWrapper> wrapper =
|
|
|
|
new ParentChannelWrapper(channel, mListener);
|
|
|
|
|
2020-06-27 04:10:23 +00:00
|
|
|
wrapper->Register(mDocumentLoadListener->GetRedirectChannelId());
|
2020-04-24 01:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 05:56:36 +00:00
|
|
|
mPromise.Resolve(aResult, __func__);
|
2020-04-24 01:58:23 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP ParentProcessDocumentChannel::AsyncOpen(
|
|
|
|
nsIStreamListener* aListener) {
|
|
|
|
LOG(("ParentProcessDocumentChannel AsyncOpen [this=%p]", this));
|
2020-07-21 01:01:05 +00:00
|
|
|
auto docShell = RefPtr<nsDocShell>(GetDocShell());
|
|
|
|
MOZ_ASSERT(docShell);
|
|
|
|
|
|
|
|
bool isDocumentLoad = mLoadInfo->GetExternalContentPolicyType() !=
|
2021-01-15 12:07:16 +00:00
|
|
|
ExtContentPolicy::TYPE_OBJECT;
|
2020-07-21 01:01:05 +00:00
|
|
|
|
|
|
|
mDocumentLoadListener = MakeRefPtr<DocumentLoadListener>(
|
|
|
|
docShell->GetBrowsingContext()->Canonical(), isDocumentLoad);
|
2020-04-24 01:58:23 +00:00
|
|
|
LOG(("Created PPDocumentChannel with listener=%p",
|
|
|
|
mDocumentLoadListener.get()));
|
|
|
|
|
2020-04-24 01:45:55 +00:00
|
|
|
// Add observers.
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(observerService->AddObserver(
|
|
|
|
this, NS_HTTP_ON_MODIFY_REQUEST_TOPIC, false));
|
|
|
|
}
|
|
|
|
|
2020-04-24 01:58:23 +00:00
|
|
|
gHttpHandler->OnOpeningDocumentRequest(this);
|
|
|
|
|
2020-07-21 01:01:05 +00:00
|
|
|
if (isDocumentLoad) {
|
2020-07-31 13:37:00 +00:00
|
|
|
// Return value of setting synced field should be checked. See bug 1656492.
|
|
|
|
Unused << GetDocShell()->GetBrowsingContext()->SetCurrentLoadIdentifier(
|
2020-07-21 01:01:05 +00:00
|
|
|
Some(mLoadState->GetLoadIdentifier()));
|
|
|
|
}
|
2020-06-23 13:01:37 +00:00
|
|
|
|
2020-04-24 01:58:23 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
Maybe<dom::ClientInfo> initialClientInfo = mInitialClientInfo;
|
2020-07-21 01:01:05 +00:00
|
|
|
|
|
|
|
RefPtr<DocumentLoadListener::OpenPromise> promise;
|
|
|
|
if (isDocumentLoad) {
|
|
|
|
promise = mDocumentLoadListener->OpenDocument(
|
2024-10-08 21:28:09 +00:00
|
|
|
mLoadState, mLoadFlags, mCacheKey, Some(mChannelId), TimeStamp::Now(),
|
|
|
|
mTiming, std::move(initialClientInfo), mUriModified,
|
2023-12-19 10:05:12 +00:00
|
|
|
Some(mIsEmbeddingBlockedError), nullptr /* ContentParent */, &rv);
|
2020-07-21 01:01:05 +00:00
|
|
|
} else {
|
|
|
|
promise = mDocumentLoadListener->OpenObject(
|
2021-10-28 00:37:15 +00:00
|
|
|
mLoadState, mCacheKey, Some(mChannelId), TimeStamp::Now(), mTiming,
|
2020-07-21 01:01:05 +00:00
|
|
|
std::move(initialClientInfo), InnerWindowIDForExtantDoc(docShell),
|
|
|
|
mLoadFlags, mLoadInfo->InternalContentPolicyType(),
|
2021-10-14 12:57:59 +00:00
|
|
|
dom::UserActivation::IsHandlingUserInput(), nullptr /* ContentParent */,
|
2020-08-18 16:49:34 +00:00
|
|
|
nullptr /* ObjectUpgradeHandler */, &rv);
|
2020-07-21 01:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 05:56:28 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_ASSERT(!promise);
|
|
|
|
mDocumentLoadListener = nullptr;
|
|
|
|
RemoveObserver();
|
2020-04-24 01:58:23 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mListener = aListener;
|
|
|
|
if (mLoadGroup) {
|
|
|
|
mLoadGroup->AddRequest(this, nullptr);
|
|
|
|
}
|
2020-06-12 05:56:28 +00:00
|
|
|
|
|
|
|
RefPtr<ParentProcessDocumentChannel> self = this;
|
2020-06-12 05:56:36 +00:00
|
|
|
promise->Then(
|
2020-06-23 05:05:36 +00:00
|
|
|
GetCurrentSerialEventTarget(), __func__,
|
2020-06-12 05:56:36 +00:00
|
|
|
[self](DocumentLoadListener::OpenPromiseSucceededType&& aResolveValue) {
|
2023-06-01 09:46:04 +00:00
|
|
|
self->mDocumentLoadListener->CancelEarlyHintPreloads();
|
|
|
|
nsTArray<EarlyHintConnectArgs> earlyHints;
|
|
|
|
|
2020-06-12 05:56:36 +00:00
|
|
|
// The DLL is waiting for us to resolve the
|
|
|
|
// RedirectToRealChannelPromise given as parameter.
|
|
|
|
RefPtr<RedirectToRealChannelPromise> p =
|
|
|
|
self->RedirectToRealChannel(
|
|
|
|
std::move(aResolveValue.mStreamFilterEndpoints),
|
2022-12-02 16:15:56 +00:00
|
|
|
aResolveValue.mRedirectFlags, aResolveValue.mLoadFlags,
|
2023-06-01 09:46:04 +00:00
|
|
|
earlyHints)
|
2020-06-12 05:56:36 +00:00
|
|
|
->Then(
|
2020-06-23 05:05:36 +00:00
|
|
|
GetCurrentSerialEventTarget(), __func__,
|
2020-06-12 05:56:36 +00:00
|
|
|
[self](RedirectToRealChannelPromise::ResolveOrRejectValue&&
|
2020-06-12 05:57:02 +00:00
|
|
|
aValue) -> RefPtr<RedirectToRealChannelPromise> {
|
2020-06-12 05:56:36 +00:00
|
|
|
MOZ_ASSERT(aValue.IsResolve());
|
|
|
|
nsresult rv = aValue.ResolveValue();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
self->DisconnectChildListeners(rv, rv);
|
|
|
|
}
|
|
|
|
self->mLoadGroup = nullptr;
|
|
|
|
self->mListener = nullptr;
|
|
|
|
self->mCallbacks = nullptr;
|
|
|
|
self->RemoveObserver();
|
2020-06-12 05:57:02 +00:00
|
|
|
auto p =
|
|
|
|
MakeRefPtr<RedirectToRealChannelPromise::Private>(
|
|
|
|
__func__);
|
|
|
|
p->UseDirectTaskDispatch(__func__);
|
|
|
|
p->ResolveOrReject(std::move(aValue), __func__);
|
|
|
|
return p;
|
2020-06-12 05:56:36 +00:00
|
|
|
});
|
|
|
|
// We chain the promise the DLL is waiting on to the one returned by
|
|
|
|
// RedirectToRealChannel. As soon as the promise returned is
|
|
|
|
// resolved or rejected, so will the DLL's promise.
|
|
|
|
p->ChainTo(aResolveValue.mPromise.forget(), __func__);
|
|
|
|
},
|
|
|
|
[self](DocumentLoadListener::OpenPromiseFailedType&& aRejectValue) {
|
2020-07-12 22:50:34 +00:00
|
|
|
// If this is a normal failure, then we want to disconnect our listeners
|
|
|
|
// and notify them of the failure. If this is a process switch, then we
|
|
|
|
// can just ignore it silently, and trust that the switch will shut down
|
|
|
|
// our docshell and cancel us when it's ready.
|
2022-04-27 15:22:01 +00:00
|
|
|
if (!aRejectValue.mContinueNavigating) {
|
2020-07-12 22:50:34 +00:00
|
|
|
self->DisconnectChildListeners(aRejectValue.mStatus,
|
|
|
|
aRejectValue.mLoadGroupStatus);
|
|
|
|
}
|
2020-06-12 05:56:36 +00:00
|
|
|
self->RemoveObserver();
|
|
|
|
});
|
2020-04-24 01:58:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP ParentProcessDocumentChannel::Cancel(nsresult aStatus) {
|
2022-09-08 09:11:19 +00:00
|
|
|
return CancelWithReason(aStatus, "ParentProcessDocumentChannel::Cancel"_ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP ParentProcessDocumentChannel::CancelWithReason(
|
|
|
|
nsresult aStatusCode, const nsACString& aReason) {
|
|
|
|
LOG(("ParentProcessDocumentChannel CancelWithReason [this=%p]", this));
|
2020-04-24 01:58:23 +00:00
|
|
|
if (mCanceled) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCanceled = true;
|
2020-06-12 05:56:36 +00:00
|
|
|
// This will force the DocumentListener to abort the promise if there's one
|
|
|
|
// pending.
|
2022-09-08 09:11:19 +00:00
|
|
|
mDocumentLoadListener->Cancel(aStatusCode, aReason);
|
2020-06-12 05:56:36 +00:00
|
|
|
|
2020-06-12 05:56:28 +00:00
|
|
|
return NS_OK;
|
2020-06-09 15:55:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 00:42:09 +00:00
|
|
|
void ParentProcessDocumentChannel::RemoveObserver() {
|
2020-04-24 01:45:55 +00:00
|
|
|
if (nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService()) {
|
|
|
|
observerService->RemoveObserver(this, NS_HTTP_ON_MODIFY_REQUEST_TOPIC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIObserver
|
2020-06-12 05:56:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2020-04-24 01:45:55 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ParentProcessDocumentChannel::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mRequestObserversCalled) {
|
|
|
|
// We have already emitted the event, we don't want to emit it again.
|
|
|
|
// We only care about forwarding the first NS_HTTP_ON_MODIFY_REQUEST_TOPIC
|
|
|
|
// encountered.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aSubject);
|
|
|
|
if (!channel || mDocumentLoadListener->GetChannel() != channel) {
|
|
|
|
// Not a channel we are interested with.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
LOG(("DocumentChannelParent Observe [this=%p aChannel=%p]", this,
|
|
|
|
channel.get()));
|
|
|
|
if (!nsCRT::strcmp(aTopic, NS_HTTP_ON_MODIFY_REQUEST_TOPIC)) {
|
|
|
|
mRequestObserversCalled = true;
|
|
|
|
gHttpHandler->OnModifyDocumentRequest(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-24 01:58:23 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2020-04-26 00:50:52 +00:00
|
|
|
#undef LOG
|