2015-05-03 22:32:37 +03: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 19:49:48 +04: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 09:23:56 +04:00
|
|
|
#include "mozilla/dom/ClipboardEvent.h"
|
|
|
|
#include "mozilla/ContentEvents.h"
|
2013-08-12 18:45:33 +04:00
|
|
|
#include "mozilla/dom/DataTransfer.h"
|
2013-08-07 22:52:38 +04:00
|
|
|
#include "nsIClipboard.h"
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2014-02-26 09:23:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-09-27 10:20:55 +04:00
|
|
|
|
2014-02-26 09:23:56 +04:00
|
|
|
ClipboardEvent::ClipboardEvent(EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
InternalClipboardEvent* aEvent)
|
2014-03-05 04:37:43 +04:00
|
|
|
: Event(aOwner, aPresContext,
|
2015-08-29 02:58:26 +03:00
|
|
|
aEvent ? aEvent : new InternalClipboardEvent(false, eVoidEvent))
|
2013-03-11 19:49:48 +04:00
|
|
|
{
|
|
|
|
if (aEvent) {
|
|
|
|
mEventIsInternal = false;
|
|
|
|
} else {
|
|
|
|
mEventIsInternal = true;
|
|
|
|
mEvent->time = PR_Now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:23:56 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN(ClipboardEvent)
|
2013-03-11 19:49:48 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMClipboardEvent)
|
2014-03-05 04:37:43 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(Event)
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2014-03-05 04:37:43 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(ClipboardEvent, Event)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ClipboardEvent, Event)
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2013-03-11 19:49:56 +04:00
|
|
|
nsresult
|
2014-02-26 09:23:56 +04:00
|
|
|
ClipboardEvent::InitClipboardEvent(const nsAString& aType,
|
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
|
|
|
nsIDOMDataTransfer* aClipboardData)
|
2013-03-11 19:49:56 +04:00
|
|
|
{
|
2014-02-27 07:23:31 +04:00
|
|
|
nsCOMPtr<DataTransfer> clipboardData = do_QueryInterface(aClipboardData);
|
|
|
|
// Null clipboardData is OK
|
2013-03-11 19:49:56 +04:00
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
ErrorResult rv;
|
2015-11-13 03:09:42 +03:00
|
|
|
InitClipboardEvent(aType, aCanBubble, aCancelable, clipboardData);
|
2013-03-11 19:49:56 +04:00
|
|
|
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2014-02-27 07:23:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClipboardEvent::InitClipboardEvent(const nsAString& aType, bool aCanBubble,
|
|
|
|
bool aCancelable,
|
2015-11-13 03:09:42 +03:00
|
|
|
DataTransfer* aClipboardData)
|
2014-02-27 07:23:31 +04:00
|
|
|
{
|
2015-11-13 03:09:42 +03:00
|
|
|
Event::InitEvent(aType, aCanBubble, aCancelable);
|
2016-03-26 11:19:57 +03:00
|
|
|
mEvent->AsClipboardEvent()->mClipboardData = aClipboardData;
|
2013-03-11 19:49:56 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 09:23:56 +04:00
|
|
|
already_AddRefed<ClipboardEvent>
|
|
|
|
ClipboardEvent::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType,
|
|
|
|
const ClipboardEventInit& aParam,
|
|
|
|
ErrorResult& aRv)
|
2013-03-28 18:11:21 +04:00
|
|
|
{
|
2014-02-26 09:23:56 +04:00
|
|
|
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ClipboardEvent> e = new ClipboardEvent(t, nullptr, nullptr);
|
2013-03-28 18:11:21 +04:00
|
|
|
bool trusted = e->Init(t);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataTransfer> clipboardData;
|
2013-03-28 18:11:21 +04:00
|
|
|
if (e->mEventIsInternal) {
|
2013-10-18 10:10:22 +04:00
|
|
|
InternalClipboardEvent* event = e->mEvent->AsClipboardEvent();
|
2013-03-28 18:11:21 +04: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 18:45:33 +04:00
|
|
|
// checked properly within DataTransfer.
|
2015-09-08 17:33:34 +03:00
|
|
|
clipboardData = new DataTransfer(ToSupports(e), eCopy, false, -1);
|
2015-10-23 01:30:04 +03:00
|
|
|
clipboardData->SetData(aParam.mDataType, aParam.mData, aRv);
|
|
|
|
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
2013-03-28 18:11:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
e->InitClipboardEvent(aType, aParam.mBubbles, aParam.mCancelable,
|
2015-11-13 03:09:42 +03:00
|
|
|
clipboardData);
|
2013-03-28 18:11:21 +04:00
|
|
|
e->SetTrusted(trusted);
|
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
NS_IMETHODIMP
|
2014-02-26 09:23:56 +04:00
|
|
|
ClipboardEvent::GetClipboardData(nsIDOMDataTransfer** aClipboardData)
|
2013-03-28 18:11:21 +04:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aClipboardData = GetClipboardData());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 07:23:31 +04:00
|
|
|
DataTransfer*
|
2014-02-26 09:23:56 +04:00
|
|
|
ClipboardEvent::GetClipboardData()
|
2013-03-11 19:49:48 +04:00
|
|
|
{
|
2013-10-18 10:10:22 +04:00
|
|
|
InternalClipboardEvent* event = mEvent->AsClipboardEvent();
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2016-03-26 11:19:57 +03:00
|
|
|
if (!event->mClipboardData) {
|
2013-03-11 19:49:48 +04:00
|
|
|
if (mEventIsInternal) {
|
2016-03-26 11:19:57 +03:00
|
|
|
event->mClipboardData =
|
2015-09-08 17:33:34 +03:00
|
|
|
new DataTransfer(ToSupports(this), eCopy, false, -1);
|
2013-03-11 19:49:48 +04:00
|
|
|
} else {
|
2016-03-26 11:19:57 +03:00
|
|
|
event->mClipboardData =
|
2015-08-22 04:34:51 +03:00
|
|
|
new DataTransfer(ToSupports(this), event->mMessage,
|
2015-09-08 17:33:35 +03:00
|
|
|
event->mMessage == ePaste,
|
2014-02-27 07:23:31 +04:00
|
|
|
nsIClipboard::kGlobalClipboard);
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 11:19:57 +03:00
|
|
|
return event->mClipboardData;
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 09:23:56 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-08-12 14:39:31 +03:00
|
|
|
already_AddRefed<ClipboardEvent>
|
|
|
|
NS_NewDOMClipboardEvent(EventTarget* aOwner,
|
2014-02-26 09:23:56 +04:00
|
|
|
nsPresContext* aPresContext,
|
|
|
|
InternalClipboardEvent* aEvent)
|
2013-03-11 19:49:48 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ClipboardEvent> it =
|
2015-08-12 14:39:31 +03:00
|
|
|
new ClipboardEvent(aOwner, aPresContext, aEvent);
|
|
|
|
return it.forget();
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|