michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ 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: /* Copyright © 2013 Deutsche Telekom, Inc. */ michael@0: michael@0: #include "MozNDEFRecord.h" michael@0: #include "mozilla/dom/MozNDEFRecordBinding.h" michael@0: #include "mozilla/HoldDropJSObjects.h" michael@0: #include "nsContentUtils.h" michael@0: michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(MozNDEFRecord) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MozNDEFRecord) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER michael@0: tmp->DropData(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MozNDEFRecord) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(MozNDEFRecord) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mType) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mId) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mPayload) michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(MozNDEFRecord) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(MozNDEFRecord) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MozNDEFRecord) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: void michael@0: MozNDEFRecord::HoldData() michael@0: { michael@0: mozilla::HoldJSObjects(this); michael@0: } michael@0: michael@0: void michael@0: MozNDEFRecord::DropData() michael@0: { michael@0: if (mType) { michael@0: mType = nullptr; michael@0: } michael@0: if (mId) { michael@0: mId = nullptr; michael@0: } michael@0: if (mPayload) { michael@0: mPayload = nullptr; michael@0: } michael@0: mozilla::DropJSObjects(this); michael@0: } michael@0: michael@0: /* static */ michael@0: already_AddRefed michael@0: MozNDEFRecord::Constructor(const GlobalObject& aGlobal, michael@0: uint8_t aTnf, michael@0: const Optional& aType, michael@0: const Optional& aId, michael@0: const Optional& aPayload, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr win = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: if (!win) { michael@0: aRv.Throw(NS_ERROR_FAILURE); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsRefPtr ndefrecord = new MozNDEFRecord(aGlobal.GetContext(), michael@0: win, aTnf, aType, aId, michael@0: aPayload); michael@0: if (!ndefrecord) { michael@0: aRv.Throw(NS_ERROR_FAILURE); michael@0: return nullptr; michael@0: } michael@0: return ndefrecord.forget(); michael@0: } michael@0: michael@0: MozNDEFRecord::MozNDEFRecord(JSContext* aCx, nsPIDOMWindow* aWindow, michael@0: uint8_t aTnf, michael@0: const Optional& aType, michael@0: const Optional& aId, michael@0: const Optional& aPayload) michael@0: : mTnf(aTnf) michael@0: { michael@0: mWindow = aWindow; // For GetParentObject() michael@0: michael@0: if (aType.WasPassed()) { michael@0: mType = Uint8Array::Create(aCx, this, aType.Value().Length(), aType.Value().Data()); michael@0: } michael@0: michael@0: if (aId.WasPassed()) { michael@0: mId = Uint8Array::Create(aCx, this, aId.Value().Length(), aId.Value().Data()); michael@0: } michael@0: michael@0: if (aPayload.WasPassed()) { michael@0: mPayload = Uint8Array::Create(aCx, this, aPayload.Value().Length(), aPayload.Value().Data()); michael@0: } michael@0: michael@0: SetIsDOMBinding(); michael@0: HoldData(); michael@0: } michael@0: michael@0: MozNDEFRecord::~MozNDEFRecord() michael@0: { michael@0: DropData(); michael@0: } michael@0: michael@0: JSObject* michael@0: MozNDEFRecord::WrapObject(JSContext* aCx) michael@0: { michael@0: return MozNDEFRecordBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla