2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-03-11 15:49:48 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2014-02-26 05:23:56 +00:00
|
|
|
#include "mozilla/dom/ClipboardEvent.h"
|
|
|
|
#include "mozilla/ContentEvents.h"
|
2013-08-12 14:45:33 +00:00
|
|
|
#include "mozilla/dom/DataTransfer.h"
|
2013-08-07 18:52:38 +00:00
|
|
|
#include "nsIClipboard.h"
|
2013-03-11 15:49:48 +00:00
|
|
|
|
2014-02-26 05:23:56 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-09-27 06:20:55 +00:00
|
|
|
|
2014-02-26 05:23:56 +00:00
|
|
|
ClipboardEvent::ClipboardEvent(EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
InternalClipboardEvent* aEvent)
|
2014-03-05 00:37:43 +00:00
|
|
|
: Event(aOwner, aPresContext,
|
2015-08-28 23:58:26 +00:00
|
|
|
aEvent ? aEvent : new InternalClipboardEvent(false, eVoidEvent))
|
2013-03-11 15:49:48 +00:00
|
|
|
{
|
|
|
|
if (aEvent) {
|
|
|
|
mEventIsInternal = false;
|
|
|
|
} else {
|
|
|
|
mEventIsInternal = true;
|
2016-03-28 04:29:42 +00:00
|
|
|
mEvent->mTime = PR_Now();
|
2013-03-11 15:49:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 05:23:56 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(ClipboardEvent)
|
2013-03-11 15:49:48 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMClipboardEvent)
|
2014-03-05 00:37:43 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(Event)
|
2013-03-11 15:49:48 +00:00
|
|
|
|
2014-03-05 00:37:43 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ClipboardEvent, Event)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ClipboardEvent, Event)
|
2013-03-11 15:49:48 +00:00
|
|
|
|
2013-03-11 15:49:56 +00:00
|
|
|
nsresult
|
2014-02-26 05:23:56 +00:00
|
|
|
ClipboardEvent::InitClipboardEvent(const nsAString& aType,
|
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
|
|
|
nsIDOMDataTransfer* aClipboardData)
|
2013-03-11 15:49:56 +00:00
|
|
|
{
|
2014-02-27 03:23:31 +00:00
|
|
|
nsCOMPtr<DataTransfer> clipboardData = do_QueryInterface(aClipboardData);
|
|
|
|
// Null clipboardData is OK
|
2013-03-11 15:49:56 +00:00
|
|
|
|
2014-02-27 03:23:31 +00:00
|
|
|
ErrorResult rv;
|
2015-11-13 00:09:42 +00:00
|
|
|
InitClipboardEvent(aType, aCanBubble, aCancelable, clipboardData);
|
2013-03-11 15:49:56 +00:00
|
|
|
|
2015-04-27 13:18:51 +00:00
|
|
|
return rv.StealNSResult();
|
2014-02-27 03:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClipboardEvent::InitClipboardEvent(const nsAString& aType, bool aCanBubble,
|
|
|
|
bool aCancelable,
|
2015-11-13 00:09:42 +00:00
|
|
|
DataTransfer* aClipboardData)
|
2014-02-27 03:23:31 +00:00
|
|
|
{
|
2016-10-30 19:30:06 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
|
|
|
|
|
2015-11-13 00:09:42 +00:00
|
|
|
Event::InitEvent(aType, aCanBubble, aCancelable);
|
2016-03-26 08:19:57 +00:00
|
|
|
mEvent->AsClipboardEvent()->mClipboardData = aClipboardData;
|
2013-03-11 15:49:56 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 05:23:56 +00:00
|
|
|
already_AddRefed<ClipboardEvent>
|
|
|
|
ClipboardEvent::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType,
|
|
|
|
const ClipboardEventInit& aParam,
|
|
|
|
ErrorResult& aRv)
|
2013-03-28 14:11:21 +00:00
|
|
|
{
|
2014-02-26 05:23:56 +00:00
|
|
|
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ClipboardEvent> e = new ClipboardEvent(t, nullptr, nullptr);
|
2013-03-28 14:11:21 +00:00
|
|
|
bool trusted = e->Init(t);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataTransfer> clipboardData;
|
2013-03-28 14:11:21 +00:00
|
|
|
if (e->mEventIsInternal) {
|
2013-10-18 06:10:22 +00:00
|
|
|
InternalClipboardEvent* event = e->mEvent->AsClipboardEvent();
|
2013-03-28 14:11:21 +00:00
|
|
|
if (event) {
|
|
|
|
// Always create a clipboardData for the copy event. If this is changed to
|
|
|
|
// support other types of events, make sure that read/write privileges are
|
2013-08-12 14:45:33 +00:00
|
|
|
// checked properly within DataTransfer.
|
2015-09-08 14:33:34 +00:00
|
|
|
clipboardData = new DataTransfer(ToSupports(e), eCopy, false, -1);
|
2016-09-30 17:54:28 +00:00
|
|
|
clipboardData->SetData(aParam.mDataType, aParam.mData,
|
2016-10-11 01:07:48 +00:00
|
|
|
*aGlobal.GetSubjectPrincipal(), aRv);
|
2015-10-22 22:30:04 +00:00
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
2013-03-28 14:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:23:31 +00:00
|
|
|
e->InitClipboardEvent(aType, aParam.mBubbles, aParam.mCancelable,
|
2015-11-13 00:09:42 +00:00
|
|
|
clipboardData);
|
2013-03-28 14:11:21 +00:00
|
|
|
e->SetTrusted(trusted);
|
2016-08-31 03:16:11 +00:00
|
|
|
e->SetComposed(aParam.mComposed);
|
2013-03-28 14:11:21 +00:00
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
2013-03-11 15:49:48 +00:00
|
|
|
NS_IMETHODIMP
|
2014-02-26 05:23:56 +00:00
|
|
|
ClipboardEvent::GetClipboardData(nsIDOMDataTransfer** aClipboardData)
|
2013-03-28 14:11:21 +00:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aClipboardData = GetClipboardData());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:23:31 +00:00
|
|
|
DataTransfer*
|
2014-02-26 05:23:56 +00:00
|
|
|
ClipboardEvent::GetClipboardData()
|
2013-03-11 15:49:48 +00:00
|
|
|
{
|
2013-10-18 06:10:22 +00:00
|
|
|
InternalClipboardEvent* event = mEvent->AsClipboardEvent();
|
2013-03-11 15:49:48 +00:00
|
|
|
|
2016-03-26 08:19:57 +00:00
|
|
|
if (!event->mClipboardData) {
|
2013-03-11 15:49:48 +00:00
|
|
|
if (mEventIsInternal) {
|
2016-03-26 08:19:57 +00:00
|
|
|
event->mClipboardData =
|
2015-09-08 14:33:34 +00:00
|
|
|
new DataTransfer(ToSupports(this), eCopy, false, -1);
|
2013-03-11 15:49:48 +00:00
|
|
|
} else {
|
2016-03-26 08:19:57 +00:00
|
|
|
event->mClipboardData =
|
2015-08-22 01:34:51 +00:00
|
|
|
new DataTransfer(ToSupports(this), event->mMessage,
|
2015-09-08 14:33:35 +00:00
|
|
|
event->mMessage == ePaste,
|
2014-02-27 03:23:31 +00:00
|
|
|
nsIClipboard::kGlobalClipboard);
|
2013-03-11 15:49:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 08:19:57 +00:00
|
|
|
return event->mClipboardData;
|
2013-03-11 15:49:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 05:23:56 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-08-12 11:39:31 +00:00
|
|
|
already_AddRefed<ClipboardEvent>
|
|
|
|
NS_NewDOMClipboardEvent(EventTarget* aOwner,
|
2014-02-26 05:23:56 +00:00
|
|
|
nsPresContext* aPresContext,
|
|
|
|
InternalClipboardEvent* aEvent)
|
2013-03-11 15:49:48 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ClipboardEvent> it =
|
2015-08-12 11:39:31 +00:00
|
|
|
new ClipboardEvent(aOwner, aPresContext, aEvent);
|
|
|
|
return it.forget();
|
2013-03-11 15:49:48 +00:00
|
|
|
}
|