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
)) {
20 mEventIsInternal
= false;
22 mEventIsInternal
= true;
23 mEvent
->mTime
= PR_Now();
27 void ClipboardEvent::InitClipboardEvent(const nsAString
& aType
, bool aCanBubble
,
29 DataTransfer
* aClipboardData
) {
30 NS_ENSURE_TRUE_VOID(!mEvent
->mFlags
.mIsBeingDispatched
);
32 Event::InitEvent(aType
, aCanBubble
, aCancelable
);
33 mEvent
->AsClipboardEvent()->mClipboardData
= aClipboardData
;
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
);
43 RefPtr
<DataTransfer
> clipboardData
;
44 if (e
->mEventIsInternal
) {
45 InternalClipboardEvent
* event
= e
->mEvent
->AsClipboardEvent();
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, -1);
51 clipboardData
->SetData(aParam
.mDataType
, aParam
.mData
,
52 *aGlobal
.GetSubjectPrincipal(), aRv
);
53 NS_ENSURE_TRUE(!aRv
.Failed(), nullptr);
57 e
->InitClipboardEvent(aType
, aParam
.mBubbles
, aParam
.mCancelable
,
59 e
->SetTrusted(trusted
);
60 e
->SetComposed(aParam
.mComposed
);
64 DataTransfer
* ClipboardEvent::GetClipboardData() {
65 InternalClipboardEvent
* event
= mEvent
->AsClipboardEvent();
67 if (!event
->mClipboardData
) {
68 if (mEventIsInternal
) {
69 event
->mClipboardData
=
70 new DataTransfer(ToSupports(this), eCopy
, false, -1);
72 event
->mClipboardData
= new DataTransfer(
73 ToSupports(this), event
->mMessage
, event
->mMessage
== ePaste
,
74 nsIClipboard::kGlobalClipboard
);
78 return event
->mClipboardData
;
81 } // namespace mozilla::dom
83 using namespace mozilla
;
84 using namespace mozilla::dom
;
86 already_AddRefed
<ClipboardEvent
> NS_NewDOMClipboardEvent(
87 EventTarget
* aOwner
, nsPresContext
* aPresContext
,
88 InternalClipboardEvent
* aEvent
) {
89 RefPtr
<ClipboardEvent
> it
= new ClipboardEvent(aOwner
, aPresContext
, aEvent
);