tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ClipboardEvent.cpp (3277B)


      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/ClipboardEvent.h"
      8 
      9 #include "mozilla/ContentEvents.h"
     10 #include "mozilla/ErrorResult.h"
     11 #include "mozilla/dom/DataTransfer.h"
     12 #include "nsIClipboard.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 ClipboardEvent::ClipboardEvent(EventTarget* aOwner, nsPresContext* aPresContext,
     17                               InternalClipboardEvent* aEvent)
     18    : Event(aOwner, aPresContext,
     19            aEvent ? aEvent : new InternalClipboardEvent(false, eVoidEvent)) {
     20  if (aEvent) {
     21    mEventIsInternal = false;
     22  } else {
     23    mEventIsInternal = true;
     24  }
     25 }
     26 
     27 void ClipboardEvent::InitClipboardEvent(const nsAString& aType, bool aCanBubble,
     28                                        bool aCancelable,
     29                                        DataTransfer* aClipboardData) {
     30  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
     31 
     32  Event::InitEvent(aType, aCanBubble, aCancelable);
     33  mEvent->AsClipboardEvent()->mClipboardData = aClipboardData;
     34 }
     35 
     36 already_AddRefed<ClipboardEvent> ClipboardEvent::Constructor(
     37    const GlobalObject& aGlobal, const nsAString& aType,
     38    const ClipboardEventInit& aParam, ErrorResult& aRv) {
     39  nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
     40  RefPtr<ClipboardEvent> e = new ClipboardEvent(t, nullptr, nullptr);
     41  bool trusted = e->Init(t);
     42 
     43  RefPtr<DataTransfer> clipboardData;
     44  if (e->mEventIsInternal) {
     45    InternalClipboardEvent* event = e->mEvent->AsClipboardEvent();
     46    if (event) {
     47      // Always create a clipboardData for the copy event. If this is changed to
     48      // support other types of events, make sure that read/write privileges are
     49      // checked properly within DataTransfer.
     50      clipboardData = new DataTransfer(ToSupports(e), eCopy, false, Nothing());
     51      clipboardData->SetData(aParam.mDataType, aParam.mData,
     52                             *aGlobal.GetSubjectPrincipal(), aRv);
     53      NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
     54    }
     55  }
     56 
     57  e->InitClipboardEvent(aType, aParam.mBubbles, aParam.mCancelable,
     58                        clipboardData);
     59  e->SetTrusted(trusted);
     60  e->SetComposed(aParam.mComposed);
     61  return e.forget();
     62 }
     63 
     64 DataTransfer* ClipboardEvent::GetClipboardData() {
     65  InternalClipboardEvent* event = mEvent->AsClipboardEvent();
     66 
     67  if (!event->mClipboardData) {
     68    if (mEventIsInternal) {
     69      event->mClipboardData =
     70          new DataTransfer(ToSupports(this), eCopy, false, Nothing());
     71    } else {
     72      event->mClipboardData = new DataTransfer(
     73          ToSupports(this), event->mMessage, event->mMessage == ePaste,
     74          Some(nsIClipboard::kGlobalClipboard));
     75    }
     76  }
     77 
     78  return event->mClipboardData;
     79 }
     80 
     81 }  // namespace mozilla::dom
     82 
     83 using namespace mozilla;
     84 using namespace mozilla::dom;
     85 
     86 already_AddRefed<ClipboardEvent> NS_NewDOMClipboardEvent(
     87    EventTarget* aOwner, nsPresContext* aPresContext,
     88    InternalClipboardEvent* aEvent) {
     89  RefPtr<ClipboardEvent> it = new ClipboardEvent(aOwner, aPresContext, aEvent);
     90  return it.forget();
     91 }