TextEvent.cpp (2461B)
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 "mozilla/dom/TextEvent.h" 8 9 #include "mozilla/BasePrincipal.h" // for nsIPrincipal::IsSystemPrincipal() 10 #include "mozilla/EventForwards.h" 11 #include "mozilla/TextEvents.h" 12 #include "mozilla/dom/DataTransfer.h" 13 #include "nsGlobalWindowInner.h" 14 #include "nsIPrincipal.h" 15 #include "nsPresContext.h" 16 17 namespace mozilla::dom { 18 19 TextEvent::TextEvent(EventTarget* aOwner, nsPresContext* aPresContext, 20 InternalLegacyTextEvent* aEvent) 21 : UIEvent(aOwner, aPresContext, 22 aEvent 23 ? aEvent 24 : new InternalLegacyTextEvent(false, eVoidEvent, nullptr)) { 25 NS_ASSERTION(mEvent->mClass == eLegacyTextEventClass, "event type mismatch"); 26 mEventIsInternal = !aEvent; 27 } 28 29 void TextEvent::InitTextEvent(const nsAString& typeArg, bool canBubbleArg, 30 bool cancelableArg, nsGlobalWindowInner* viewArg, 31 const nsAString& dataArg) { 32 if (NS_WARN_IF(mEvent->mFlags.mIsBeingDispatched)) { 33 return; 34 } 35 36 UIEvent::InitUIEvent(typeArg, canBubbleArg, cancelableArg, viewArg, 0); 37 38 static_cast<InternalLegacyTextEvent*>(mEvent)->mData = dataArg; 39 } 40 41 void TextEvent::GetData(nsAString& aData, 42 nsIPrincipal& aSubjectPrincipal) const { 43 InternalLegacyTextEvent* textEvent = mEvent->AsLegacyTextEvent(); 44 MOZ_ASSERT(textEvent); 45 if (mEvent->IsTrusted() && !aSubjectPrincipal.IsSystemPrincipal() && 46 !StaticPrefs::dom_event_clipboardevents_enabled() && 47 ExposesClipboardDataOrDataTransfer(textEvent->mInputType)) { 48 aData.Truncate(); 49 return; 50 } 51 if (!textEvent->mDataTransfer) { 52 aData = textEvent->mData; 53 return; 54 } 55 textEvent->mDataTransfer->GetData(u"text/plain"_ns, aData, aSubjectPrincipal, 56 IgnoreErrors()); 57 } 58 59 } // namespace mozilla::dom 60 61 using namespace mozilla; 62 using namespace mozilla::dom; 63 64 already_AddRefed<TextEvent> NS_NewDOMTextEvent( 65 EventTarget* aOwner, nsPresContext* aPresContext, 66 InternalLegacyTextEvent* aEvent) { 67 RefPtr<TextEvent> it = new TextEvent(aOwner, aPresContext, aEvent); 68 return it.forget(); 69 }