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