Quellcode-Bibliothek PostMessageEvent.cpp
Sprache: C
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 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/. */
// TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230, bug 1535398)
MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHODIMP PostMessageEvent::Run() { // Note: We don't init this AutoJSAPI with targetWindow, because we do not // want exceptions during message deserialization to trigger error events on // targetWindow.
AutoJSAPI jsapi;
jsapi.Init();
JSContext* cx = jsapi.cx();
// The document URI is just used for the principal mismatch error message // below. Use a stack variable so mCallerURI is not held onto after // this method finishes, regardless of the method outcome.
nsCOMPtr<nsIURI> callerURI = std::move(mCallerURI);
// If we bailed before this point we're going to leak mMessage, but // that's probably better than crashing.
// If the window's document has suppressed event handling, hand off this event // for running later. We check the top window's document so that when multiple // same-origin windows exist in the same top window, postMessage events will // be delivered in the same order they were posted, regardless of which window // they were posted to. if (nsCOMPtr<nsPIDOMWindowOuter> topWindow =
targetWindow->GetOuterWindow()->GetInProcessTop()) { if (nsCOMPtr<nsPIDOMWindowInner> topInner =
topWindow->GetCurrentInnerWindow()) { if (topInner->GetExtantDoc() &&
topInner->GetExtantDoc()->SuspendPostMessageEvent(this)) { return NS_OK;
}
}
}
JSAutoRealm ar(cx, targetWindow->GetWrapper());
// Ensure that any origin which might have been provided is the origin of this // window's document. Note that we do this *now* instead of when postMessage // is called because the target window might have been navigated to a // different location between then and now. If this check happened when // postMessage was called, it would be fairly easy for a malicious webpage to // intercept messages intended for another site by carefully timing navigation // of the target window so it changed location after postMessage but before // now. if (mProvidedPrincipal) { // Get the target's origin either from its principal or, in the case the // principal doesn't carry a URI (e.g. the system principal), the target's // document.
nsIPrincipal* targetPrin = targetWindow->GetPrincipal(); if (NS_WARN_IF(!targetPrin)) return NS_OK;
// Note: This is contrary to the spec with respect to file: URLs, which // the spec groups into a single origin, but given we intentionally // don't do that in other places it seems better to hold the line for // now. Long-term, we want HTML5 to address this so that we can // be compliant while being safer. if (!targetPrin->Equals(mProvidedPrincipal)) {
OriginAttributes sourceAttrs = mProvidedPrincipal->OriginAttributesRef();
OriginAttributes targetAttrs = targetPrin->OriginAttributesRef();
MOZ_DIAGNOSTIC_ASSERT(
sourceAttrs.mUserContextId == targetAttrs.mUserContextId, "Target and source should have the same userContextId attribute.");
void PostMessageEvent::Dispatch(nsGlobalWindowInner* aTargetWindow,
Event* aEvent) { // We can't simply call dispatchEvent on the window because doing so ends // up flipping the trusted bit on the event, and we don't want that to // happen because then untrusted content can call postMessage on a chrome // window if it can get a reference to it.
nsEventStatus status = nsEventStatus_eIgnore;
EventDispatcher::Dispatch(aTargetWindow, presContext, internalEvent, aEvent,
&status);
}
static nsresult MaybeThrottle(nsGlobalWindowOuter* aTargetWindow,
PostMessageEvent* aEvent) {
BrowsingContext* bc = aTargetWindow->GetBrowsingContext(); if (!bc) { return NS_ERROR_FAILURE;
}
bc = bc->Top(); if (!bc->IsLoading()) { return NS_ERROR_FAILURE;
} if (nsContentUtils::IsPDFJS(aTargetWindow->GetPrincipal())) { // pdf.js is known to block the load event on a worker's postMessage event. // Avoid throttling postMessage for pdf.js to avoid pathological wait times, // see bug 1840762. return NS_ERROR_FAILURE;
} if (!StaticPrefs::dom_separate_event_queue_for_post_message_enabled()) { return NS_ERROR_FAILURE;
} return bc->Group()->QueuePostMessageEvent(aEvent);
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.15Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.