Files
palemoon27/dom/events/UIEvent.cpp
T
roytam1 fb9b106168 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1154563 - Drop the unused argument of WorkerPrivate::Close(); r=baku (366f353f9f)
- Bug 1178721 - Implement SuspendWorkersForWindow;r=khuey (a49bc9a306)
- Bug 1207490 - Part 6: Remove use of expression closure from browser/devtools/. r=vporof (1c4d153319)
- Bug 1198982: Don't fail the SW load for an importScripts failure. r=bkelly (948f5dca94)
- Bug 1160890 - Part 1: Remove unneeded code from ImportScripts(). r=smaug (e31f7c82eb)
- Bug 1160890 - Part 2: ImportScripts() should return muted errors with 3rd party scripts. r=smaug (347e54a605)
- Bug 1188141: Make Worker error events not bubble. r=baku (509fd46933)
- Bug 1160890, r=smaug (0ce3ee09d6)
- Bug 1208687: Only discard events from the outermost queue. r=ehsan (bce722c16b)
- Bug 949376 - MessageEvent::initMessageEvent, r=smaug (8878e51c91)
- Bug 1214772 - Part 2: Make FetchEvent inherit from ExtendableEvent; r=bzbarsky (edff91f7fb)
- Bug 1218131 - Mark FetchEvent.request as SameObject; r=bzbarsky (1f79b94838)
- Bug 1188545 - Add tests for service workers' lifetime management. r=nsm (9bac3b9f2d)
- Bug 1218135 - Remove FetchEvent.client; r=bzbarsky (e95e4e5326)
- Bug 1218151 - Make FetchEventInit.isReload default to false; r=bzbarsky (795d597a3b)
- Bug 1218621 - Keep the service worker alive while the promise passed to FetchEvent.respondWith() settles; r=catalinb (653633c136)
- Bug 1212636 - Add a better error message for the content corrupcted error caused by the Promise being passed to FetchEvent.respondWith; r=bkelly (d6ebabc2f2)
- Bug 1215140 P4 Make service worker respondWith() use channel ConsoleReportCollector. r=bz (0e07f364d4)
- Bug 1218499 - Make FetchEvent.request nullable; r=bzbarsky (850630ea6d)
- Bug 1179397 - Disallow FetchEvent.respondWith() when the dispatch flag is unset; r=jdm (2e57abf1c6)
- Bug 1215140 P5 Report the line number where respondWith() was called. r=bz (484e385ce3)
- Bug 1161239 - Emit a warning if the respondWith handler is resolved with a non-Object value; r=baku (cf2779f827)
- Bug 1181054 - Part 1: Move FormFillIterator and FormDataParser to FetchUtil.cpp; r=bkelly (9f62174f72)
- Bug 1181054 - Part 2: Refactor the code to extract an HTTP header from a buffer from FormDataParser; r=bkelly (7f57f76ce1)
- Bug 1181054 - Part 3: Correctly handle upload channels that have embedded body headers when dispatching a FetchEvent; r=bkelly (ebbfac4419)
-  ug 1181054 - Part 4: Make fetch-event.https.html pass; r=bkelly (b2d88a3f5b)
- Bug 1215140 P6 Update service worker interception error strings to include detailed parameters. r=bz (7918278f09)
- Bug 1219852 P1 Extract common JS values for rejected respondWith() promises. r=bz (12a6beed6c)
- Bug 1219852 P2 Report non-response values passed to FetchEvent.respondWith(). r=bz (69fdad9d5f)
- Bug 1207068 - Implement ExtendableMessageEvent interface. r=baku (29fd7c1c59)
- Bug 1224061: Make Event::InitEvent infallible. r=smaug Bug 1224061 followup to fix bustage. r=me on a CLOSED TREE IGNORE IDL (b4fc91b14b)
- Bug 1205109 - Make pushsubscriptionchange extendable. r=mt (abb45ac864)
- Bug 1207491 - Part 8: Remove use of expression closure from browser/omponents/nsBrowserContentHandler.js. r=Gijs (98dcb2cbee)
- Bug 1182571: Followup bustage fix from merge fail. CLOSED TREE (5062c88996)
- Bug 1213646: Allow URI_IS_UI_RESOURCE and safe about: URIs when SEC_ALLOW_CHROME is set. r=bz (6ca4e2322f)
- Bug 1191645 - Use channel->asycnOpen2 in dom/base/nsSyncLoadService.cpp. r=sicking (3fbd471f6b)
- Bug 1194526 - Use channel->asycnOpen2 in dom/base/nsScriptLoader.cpp (r=sicking) (7207efa45b)
- Bug 1084009 - Part 1/3 - Parse sync scripts off the main thread. r=smaug (72f4d5c749)
- Bug 1084009 - Part 2/3 - Only parse scripts off-main-thread on multicore systems. r=luke (ffb7e2270e)
- Bug 1209193 - Cache PR_GetNumberOfProcessors when checking to do off-main-thread script compilation. r=luke (c514373ad7)
- Bug 663570 - MetaCSP Part 7: CSP preload validation (r=bz) (5398116f85)
- Bug 1207863 - Fix ScopeIter iterating a strict eval frame that errored out before its CallObject was allocated. (r=jorendorff) (ae38882b7c)
- Bug 1223006 - Fix some typo in spidermonkey's comments. r=nbp (7d49536a0f)
- Bug 1223490 - Use stable hashing for InnerViewTable; r=jonco (12b4329982)
2022-11-30 11:46:18 +08:00

493 lines
13 KiB
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/. */
#include "base/basictypes.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/dom/UIEvent.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ContentEvents.h"
#include "mozilla/EventStateManager.h"
#include "mozilla/TextEvents.h"
#include "nsCOMPtr.h"
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDocShell.h"
#include "nsIDOMWindow.h"
#include "nsIDOMNode.h"
#include "nsIFrame.h"
#include "prtime.h"
namespace mozilla {
namespace dom {
UIEvent::UIEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetGUIEvent* aEvent)
: Event(aOwner, aPresContext,
aEvent ? aEvent : new InternalUIEvent(false, eVoidEvent, nullptr))
, mClientPoint(0, 0)
, mLayerPoint(0, 0)
, mPagePoint(0, 0)
, mMovementPoint(0, 0)
, mIsPointerLocked(EventStateManager::sIsPointerLocked)
, mLastClientPoint(EventStateManager::sLastClientPoint)
{
if (aEvent) {
mEventIsInternal = false;
}
else {
mEventIsInternal = true;
mEvent->time = PR_Now();
}
// Fill mDetail and mView according to the mEvent (widget-generated
// event) we've got
switch(mEvent->mClass) {
case eUIEventClass:
{
mDetail = mEvent->AsUIEvent()->detail;
break;
}
case eScrollPortEventClass:
{
InternalScrollPortEvent* scrollEvent = mEvent->AsScrollPortEvent();
mDetail = (int32_t)scrollEvent->orient;
break;
}
default:
mDetail = 0;
break;
}
mView = nullptr;
if (mPresContext)
{
nsIDocShell* docShell = mPresContext->GetDocShell();
if (docShell)
{
mView = docShell->GetWindow();
}
}
}
// static
already_AddRefed<UIEvent>
UIEvent::Constructor(const GlobalObject& aGlobal,
const nsAString& aType,
const UIEventInit& aParam,
ErrorResult& aRv)
{
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
RefPtr<UIEvent> e = new UIEvent(t, nullptr, nullptr);
bool trusted = e->Init(t);
aRv = e->InitUIEvent(aType, aParam.mBubbles, aParam.mCancelable, aParam.mView,
aParam.mDetail);
e->SetTrusted(trusted);
return e.forget();
}
NS_IMPL_CYCLE_COLLECTION_INHERITED(UIEvent, Event,
mView)
NS_IMPL_ADDREF_INHERITED(UIEvent, Event)
NS_IMPL_RELEASE_INHERITED(UIEvent, Event)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(UIEvent)
NS_INTERFACE_MAP_ENTRY(nsIDOMUIEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)
static nsIntPoint
DevPixelsToCSSPixels(const LayoutDeviceIntPoint& aPoint,
nsPresContext* aContext)
{
return nsIntPoint(aContext->DevPixelsToIntCSSPixels(aPoint.x),
aContext->DevPixelsToIntCSSPixels(aPoint.y));
}
nsIntPoint
UIEvent::GetMovementPoint()
{
if (mPrivateDataDuplicated || mEventIsInternal) {
return mMovementPoint;
}
if (!mEvent ||
(mEvent->mClass != eMouseEventClass &&
mEvent->mClass != eMouseScrollEventClass &&
mEvent->mClass != eWheelEventClass &&
mEvent->mClass != eDragEventClass &&
mEvent->mClass != ePointerEventClass &&
mEvent->mClass != eSimpleGestureEventClass) ||
!mEvent->AsGUIEvent()->widget) {
return nsIntPoint(0, 0);
}
// Calculate the delta between the last screen point and the current one.
nsIntPoint current = DevPixelsToCSSPixels(mEvent->refPoint, mPresContext);
nsIntPoint last = DevPixelsToCSSPixels(mEvent->lastRefPoint, mPresContext);
return current - last;
}
NS_IMETHODIMP
UIEvent::GetView(nsIDOMWindow** aView)
{
*aView = mView;
NS_IF_ADDREF(*aView);
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetDetail(int32_t* aDetail)
{
*aDetail = mDetail;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::InitUIEvent(const nsAString& typeArg,
bool canBubbleArg,
bool cancelableArg,
nsIDOMWindow* viewArg,
int32_t detailArg)
{
if (viewArg) {
nsCOMPtr<nsPIDOMWindow> view = do_QueryInterface(viewArg);
NS_ENSURE_TRUE(view, NS_ERROR_INVALID_ARG);
}
Event::InitEvent(typeArg, canBubbleArg, cancelableArg);
mDetail = detailArg;
mView = viewArg;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetPageX(int32_t* aPageX)
{
NS_ENSURE_ARG_POINTER(aPageX);
*aPageX = PageX();
return NS_OK;
}
int32_t
UIEvent::PageX() const
{
if (mPrivateDataDuplicated) {
return mPagePoint.x;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->refPoint,
mClientPoint).x;
}
NS_IMETHODIMP
UIEvent::GetPageY(int32_t* aPageY)
{
NS_ENSURE_ARG_POINTER(aPageY);
*aPageY = PageY();
return NS_OK;
}
int32_t
UIEvent::PageY() const
{
if (mPrivateDataDuplicated) {
return mPagePoint.y;
}
return Event::GetPageCoords(mPresContext, mEvent, mEvent->refPoint,
mClientPoint).y;
}
NS_IMETHODIMP
UIEvent::GetWhich(uint32_t* aWhich)
{
NS_ENSURE_ARG_POINTER(aWhich);
*aWhich = Which();
return NS_OK;
}
already_AddRefed<nsINode>
UIEvent::GetRangeParent()
{
nsIFrame* targetFrame = nullptr;
if (mPresContext) {
targetFrame = mPresContext->EventStateManager()->GetEventTarget();
}
if (targetFrame) {
nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent,
targetFrame);
nsCOMPtr<nsIContent> parent = targetFrame->GetContentOffsetsFromPoint(pt).content;
if (parent) {
if (parent->ChromeOnlyAccess() &&
!nsContentUtils::CanAccessNativeAnon()) {
return nullptr;
}
return parent.forget();
}
}
return nullptr;
}
NS_IMETHODIMP
UIEvent::GetRangeParent(nsIDOMNode** aRangeParent)
{
NS_ENSURE_ARG_POINTER(aRangeParent);
*aRangeParent = nullptr;
nsCOMPtr<nsINode> n = GetRangeParent();
if (n) {
CallQueryInterface(n, aRangeParent);
}
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetRangeOffset(int32_t* aRangeOffset)
{
NS_ENSURE_ARG_POINTER(aRangeOffset);
*aRangeOffset = RangeOffset();
return NS_OK;
}
int32_t
UIEvent::RangeOffset() const
{
if (!mPresContext) {
return 0;
}
nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget();
if (!targetFrame) {
return 0;
}
nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent,
targetFrame);
return targetFrame->GetContentOffsetsFromPoint(pt).offset;
}
nsIntPoint
UIEvent::GetLayerPoint() const
{
if (!mEvent ||
(mEvent->mClass != eMouseEventClass &&
mEvent->mClass != eMouseScrollEventClass &&
mEvent->mClass != eWheelEventClass &&
mEvent->mClass != ePointerEventClass &&
mEvent->mClass != eTouchEventClass &&
mEvent->mClass != eDragEventClass &&
mEvent->mClass != eSimpleGestureEventClass) ||
!mPresContext ||
mEventIsInternal) {
return mLayerPoint;
}
// XXX I'm not really sure this is correct; it's my best shot, though
nsIFrame* targetFrame = mPresContext->EventStateManager()->GetEventTarget();
if (!targetFrame)
return mLayerPoint;
nsIFrame* layer = nsLayoutUtils::GetClosestLayer(targetFrame);
nsPoint pt(nsLayoutUtils::GetEventCoordinatesRelativeTo(mEvent, layer));
return nsIntPoint(nsPresContext::AppUnitsToIntCSSPixels(pt.x),
nsPresContext::AppUnitsToIntCSSPixels(pt.y));
}
NS_IMETHODIMP
UIEvent::GetLayerX(int32_t* aLayerX)
{
NS_ENSURE_ARG_POINTER(aLayerX);
*aLayerX = GetLayerPoint().x;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetLayerY(int32_t* aLayerY)
{
NS_ENSURE_ARG_POINTER(aLayerY);
*aLayerY = GetLayerPoint().y;
return NS_OK;
}
NS_IMETHODIMP
UIEvent::GetIsChar(bool* aIsChar)
{
*aIsChar = IsChar();
return NS_OK;
}
bool
UIEvent::IsChar() const
{
WidgetKeyboardEvent* keyEvent = mEvent->AsKeyboardEvent();
return keyEvent ? keyEvent->isChar : false;
}
NS_IMETHODIMP
UIEvent::DuplicatePrivateData()
{
mClientPoint =
Event::GetClientCoords(mPresContext, mEvent, mEvent->refPoint,
mClientPoint);
mMovementPoint = GetMovementPoint();
mLayerPoint = GetLayerPoint();
mPagePoint =
Event::GetPageCoords(mPresContext, mEvent, mEvent->refPoint, mClientPoint);
// GetScreenPoint converts mEvent->refPoint to right coordinates.
LayoutDeviceIntPoint screenPoint =
Event::GetScreenCoords(mPresContext, mEvent, mEvent->refPoint);
nsresult rv = Event::DuplicatePrivateData();
if (NS_SUCCEEDED(rv)) {
mEvent->refPoint = screenPoint;
}
return rv;
}
NS_IMETHODIMP_(void)
UIEvent::Serialize(IPC::Message* aMsg, bool aSerializeInterfaceType)
{
if (aSerializeInterfaceType) {
IPC::WriteParam(aMsg, NS_LITERAL_STRING("uievent"));
}
Event::Serialize(aMsg, false);
int32_t detail = 0;
GetDetail(&detail);
IPC::WriteParam(aMsg, detail);
}
NS_IMETHODIMP_(bool)
UIEvent::Deserialize(const IPC::Message* aMsg, void** aIter)
{
NS_ENSURE_TRUE(Event::Deserialize(aMsg, aIter), false);
NS_ENSURE_TRUE(IPC::ReadParam(aMsg, aIter, &mDetail), false);
return true;
}
// XXX Following struct and array are used only in
// UIEvent::ComputeModifierState(), but if we define them in it,
// we fail to build on Mac at calling mozilla::ArrayLength().
struct ModifierPair
{
Modifier modifier;
const char* name;
};
static const ModifierPair kPairs[] = {
{ MODIFIER_ALT, NS_DOM_KEYNAME_ALT },
{ MODIFIER_ALTGRAPH, NS_DOM_KEYNAME_ALTGRAPH },
{ MODIFIER_CAPSLOCK, NS_DOM_KEYNAME_CAPSLOCK },
{ MODIFIER_CONTROL, NS_DOM_KEYNAME_CONTROL },
{ MODIFIER_FN, NS_DOM_KEYNAME_FN },
{ MODIFIER_FNLOCK, NS_DOM_KEYNAME_FNLOCK },
{ MODIFIER_META, NS_DOM_KEYNAME_META },
{ MODIFIER_NUMLOCK, NS_DOM_KEYNAME_NUMLOCK },
{ MODIFIER_SCROLLLOCK, NS_DOM_KEYNAME_SCROLLLOCK },
{ MODIFIER_SHIFT, NS_DOM_KEYNAME_SHIFT },
{ MODIFIER_SYMBOL, NS_DOM_KEYNAME_SYMBOL },
{ MODIFIER_SYMBOLLOCK, NS_DOM_KEYNAME_SYMBOLLOCK },
{ MODIFIER_OS, NS_DOM_KEYNAME_OS }
};
// static
Modifiers
UIEvent::ComputeModifierState(const nsAString& aModifiersList)
{
if (aModifiersList.IsEmpty()) {
return 0;
}
// Be careful about the performance. If aModifiersList is too long,
// parsing it needs too long time.
// XXX Should we abort if aModifiersList is too long?
Modifiers modifiers = 0;
nsAString::const_iterator listStart, listEnd;
aModifiersList.BeginReading(listStart);
aModifiersList.EndReading(listEnd);
for (uint32_t i = 0; i < ArrayLength(kPairs); i++) {
nsAString::const_iterator start(listStart), end(listEnd);
if (!FindInReadable(NS_ConvertASCIItoUTF16(kPairs[i].name), start, end)) {
continue;
}
if ((start != listStart && !NS_IsAsciiWhitespace(*(--start))) ||
(end != listEnd && !NS_IsAsciiWhitespace(*(end)))) {
continue;
}
modifiers |= kPairs[i].modifier;
}
return modifiers;
}
bool
UIEvent::GetModifierStateInternal(const nsAString& aKey)
{
WidgetInputEvent* inputEvent = mEvent->AsInputEvent();
MOZ_ASSERT(inputEvent, "mEvent must be WidgetInputEvent or derived class");
return ((inputEvent->modifiers & WidgetInputEvent::GetModifier(aKey)) != 0);
}
void
UIEvent::InitModifiers(const EventModifierInit& aParam)
{
if (NS_WARN_IF(!mEvent)) {
return;
}
WidgetInputEvent* inputEvent = mEvent->AsInputEvent();
MOZ_ASSERT(inputEvent,
"This method shouldn't be called if it doesn't have modifiers");
if (NS_WARN_IF(!inputEvent)) {
return;
}
inputEvent->modifiers = MODIFIER_NONE;
#define SET_MODIFIER(aName, aValue) \
if (aParam.m##aName) { \
inputEvent->modifiers |= aValue; \
} \
SET_MODIFIER(CtrlKey, MODIFIER_CONTROL)
SET_MODIFIER(ShiftKey, MODIFIER_SHIFT)
SET_MODIFIER(AltKey, MODIFIER_ALT)
SET_MODIFIER(MetaKey, MODIFIER_META)
SET_MODIFIER(ModifierAltGraph, MODIFIER_ALTGRAPH)
SET_MODIFIER(ModifierCapsLock, MODIFIER_CAPSLOCK)
SET_MODIFIER(ModifierFn, MODIFIER_FN)
SET_MODIFIER(ModifierFnLock, MODIFIER_FNLOCK)
SET_MODIFIER(ModifierNumLock, MODIFIER_NUMLOCK)
SET_MODIFIER(ModifierOS, MODIFIER_OS)
SET_MODIFIER(ModifierScrollLock, MODIFIER_SCROLLLOCK)
SET_MODIFIER(ModifierSymbol, MODIFIER_SYMBOL)
SET_MODIFIER(ModifierSymbolLock, MODIFIER_SYMBOLLOCK)
#undef SET_MODIFIER
}
} // namespace dom
} // namespace mozilla
using namespace mozilla;
using namespace mozilla::dom;
already_AddRefed<UIEvent>
NS_NewDOMUIEvent(EventTarget* aOwner,
nsPresContext* aPresContext,
WidgetGUIEvent* aEvent)
{
RefPtr<UIEvent> it = new UIEvent(aOwner, aPresContext, aEvent);
return it.forget();
}