michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/ClipboardEvent.h" michael@0: #include "mozilla/ContentEvents.h" michael@0: #include "mozilla/dom/DataTransfer.h" michael@0: #include "nsIClipboard.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: ClipboardEvent::ClipboardEvent(EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: InternalClipboardEvent* aEvent) michael@0: : Event(aOwner, aPresContext, michael@0: aEvent ? aEvent : new InternalClipboardEvent(false, 0)) michael@0: { michael@0: if (aEvent) { michael@0: mEventIsInternal = false; michael@0: } else { michael@0: mEventIsInternal = true; michael@0: mEvent->time = PR_Now(); michael@0: } michael@0: } michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(ClipboardEvent) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMClipboardEvent) michael@0: NS_INTERFACE_MAP_END_INHERITING(Event) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(ClipboardEvent, Event) michael@0: NS_IMPL_RELEASE_INHERITED(ClipboardEvent, Event) michael@0: michael@0: nsresult michael@0: ClipboardEvent::InitClipboardEvent(const nsAString& aType, michael@0: bool aCanBubble, michael@0: bool aCancelable, michael@0: nsIDOMDataTransfer* aClipboardData) michael@0: { michael@0: nsCOMPtr clipboardData = do_QueryInterface(aClipboardData); michael@0: // Null clipboardData is OK michael@0: michael@0: ErrorResult rv; michael@0: InitClipboardEvent(aType, aCanBubble, aCancelable, clipboardData, rv); michael@0: michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: ClipboardEvent::InitClipboardEvent(const nsAString& aType, bool aCanBubble, michael@0: bool aCancelable, michael@0: DataTransfer* aClipboardData, michael@0: ErrorResult& aError) michael@0: { michael@0: aError = Event::InitEvent(aType, aCanBubble, aCancelable); michael@0: if (aError.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: mEvent->AsClipboardEvent()->clipboardData = aClipboardData; michael@0: } michael@0: michael@0: already_AddRefed michael@0: ClipboardEvent::Constructor(const GlobalObject& aGlobal, michael@0: const nsAString& aType, michael@0: const ClipboardEventInit& aParam, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr t = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: nsRefPtr e = new ClipboardEvent(t, nullptr, nullptr); michael@0: bool trusted = e->Init(t); michael@0: michael@0: nsRefPtr clipboardData; michael@0: if (e->mEventIsInternal) { michael@0: InternalClipboardEvent* event = e->mEvent->AsClipboardEvent(); michael@0: if (event) { michael@0: // Always create a clipboardData for the copy event. If this is changed to michael@0: // support other types of events, make sure that read/write privileges are michael@0: // checked properly within DataTransfer. michael@0: clipboardData = new DataTransfer(ToSupports(e), NS_COPY, false, -1); michael@0: clipboardData->SetData(aParam.mDataType, aParam.mData); michael@0: } michael@0: } michael@0: michael@0: e->InitClipboardEvent(aType, aParam.mBubbles, aParam.mCancelable, michael@0: clipboardData, aRv); michael@0: e->SetTrusted(trusted); michael@0: return e.forget(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: ClipboardEvent::GetClipboardData(nsIDOMDataTransfer** aClipboardData) michael@0: { michael@0: NS_IF_ADDREF(*aClipboardData = GetClipboardData()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: DataTransfer* michael@0: ClipboardEvent::GetClipboardData() michael@0: { michael@0: InternalClipboardEvent* event = mEvent->AsClipboardEvent(); michael@0: michael@0: if (!event->clipboardData) { michael@0: if (mEventIsInternal) { michael@0: event->clipboardData = michael@0: new DataTransfer(ToSupports(this), NS_COPY, false, -1); michael@0: } else { michael@0: event->clipboardData = michael@0: new DataTransfer(ToSupports(this), event->message, michael@0: event->message == NS_PASTE, michael@0: nsIClipboard::kGlobalClipboard); michael@0: } michael@0: } michael@0: michael@0: return event->clipboardData; michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: nsresult michael@0: NS_NewDOMClipboardEvent(nsIDOMEvent** aInstancePtrResult, michael@0: EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: InternalClipboardEvent* aEvent) michael@0: { michael@0: ClipboardEvent* it = new ClipboardEvent(aOwner, aPresContext, aEvent); michael@0: NS_ADDREF(it); michael@0: *aInstancePtrResult = static_cast(it); michael@0: return NS_OK; michael@0: }