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/DataContainerEvent.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIXPConnect.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: DataContainerEvent::DataContainerEvent(EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: WidgetEvent* aEvent) michael@0: : Event(aOwner, aPresContext, aEvent) michael@0: { michael@0: if (mOwner) { michael@0: if (nsIDocument* doc = mOwner->GetExtantDoc()) { michael@0: doc->WarnOnceAbout(nsIDocument::eDataContainerEvent); michael@0: } michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(DataContainerEvent) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DataContainerEvent, Event) michael@0: tmp->mData.Clear(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DataContainerEvent, Event) michael@0: tmp->mData.EnumerateRead(TraverseEntry, &cb); michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(DataContainerEvent, Event) michael@0: NS_IMPL_RELEASE_INHERITED(DataContainerEvent, Event) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DataContainerEvent) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMDataContainerEvent) michael@0: NS_INTERFACE_MAP_END_INHERITING(Event) michael@0: michael@0: NS_IMETHODIMP michael@0: DataContainerEvent::GetData(const nsAString& aKey, nsIVariant** aData) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aData); michael@0: michael@0: mData.Get(aKey, aData); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: DataContainerEvent::SetData(const nsAString& aKey, nsIVariant* aData) michael@0: { michael@0: NS_ENSURE_ARG(aData); michael@0: michael@0: // Make sure this event isn't already being dispatched. michael@0: NS_ENSURE_STATE(!mEvent->mFlags.mIsBeingDispatched); michael@0: mData.Put(aKey, aData); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: DataContainerEvent::SetData(JSContext* aCx, const nsAString& aKey, michael@0: JS::Handle aVal, michael@0: ErrorResult& aRv) michael@0: { michael@0: if (!nsContentUtils::XPConnect()) { michael@0: aRv = NS_ERROR_FAILURE; michael@0: return; michael@0: } michael@0: nsCOMPtr val; michael@0: nsresult rv = michael@0: nsContentUtils::XPConnect()->JSToVariant(aCx, aVal, getter_AddRefs(val)); michael@0: if (NS_FAILED(rv)) { michael@0: aRv = rv; michael@0: return; michael@0: } michael@0: aRv = SetData(aKey, val); michael@0: } michael@0: michael@0: PLDHashOperator michael@0: DataContainerEvent::TraverseEntry(const nsAString& aKey, michael@0: nsIVariant* aDataItem, michael@0: void* aUserArg) michael@0: { michael@0: nsCycleCollectionTraversalCallback *cb = michael@0: static_cast(aUserArg); michael@0: cb->NoteXPCOMChild(aDataItem); michael@0: michael@0: return PL_DHASH_NEXT; 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_NewDOMDataContainerEvent(nsIDOMEvent** aInstancePtrResult, michael@0: EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: WidgetEvent* aEvent) michael@0: { michael@0: DataContainerEvent* it = new DataContainerEvent(aOwner, aPresContext, aEvent); michael@0: NS_ADDREF(it); michael@0: *aInstancePtrResult = static_cast(it); michael@0: return NS_OK; michael@0: } michael@0: