2013-11-06 21:53:19 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/* Copyright © 2013 Deutsche Telekom, Inc. */
|
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
#include "MozNDEFRecord.h"
|
|
|
|
#include "mozilla/dom/MozNDEFRecordBinding.h"
|
2013-11-09 03:10:23 +04:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2013-11-06 21:53:19 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
2013-11-09 03:10:23 +04:00
|
|
|
|
2013-11-06 21:53:19 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-11-09 03:10:23 +04:00
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MozNDEFRecord)
|
2013-11-09 03:10:23 +04:00
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MozNDEFRecord)
|
2013-11-09 03:10:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2014-02-12 13:21:09 +04:00
|
|
|
tmp->DropData();
|
2013-11-09 03:10:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MozNDEFRecord)
|
2013-11-09 03:10:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(MozNDEFRecord)
|
2013-11-09 03:10:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mType)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mId)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mPayload)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MozNDEFRecord)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MozNDEFRecord)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MozNDEFRecord)
|
2013-11-06 21:53:19 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-11-09 03:10:23 +04:00
|
|
|
void
|
2014-01-29 04:20:17 +04:00
|
|
|
MozNDEFRecord::HoldData()
|
2013-11-09 03:10:23 +04:00
|
|
|
{
|
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-29 04:20:17 +04:00
|
|
|
MozNDEFRecord::DropData()
|
2013-11-09 03:10:23 +04:00
|
|
|
{
|
|
|
|
if (mType) {
|
|
|
|
mType = nullptr;
|
|
|
|
}
|
|
|
|
if (mId) {
|
|
|
|
mId = nullptr;
|
|
|
|
}
|
|
|
|
if (mPayload) {
|
|
|
|
mPayload = nullptr;
|
|
|
|
}
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
2014-08-28 12:44:48 +04:00
|
|
|
/**
|
|
|
|
* Validate TNF.
|
|
|
|
* See section 3.3 THE NDEF Specification Test Requirements,
|
|
|
|
* NDEF specification 1.0
|
|
|
|
*/
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
MozNDEFRecord::ValidateTNF(const MozNDEFRecordOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// * The TNF field MUST have a value between 0x00 and 0x06.
|
|
|
|
// * The TNF value MUST NOT be 0x07.
|
|
|
|
// These two requirements are already handled by WebIDL bindings.
|
|
|
|
|
|
|
|
// If the TNF value is 0x00 (Empty), the TYPE, ID, and PAYLOAD fields MUST be
|
|
|
|
// omitted from the record.
|
|
|
|
if ((aOptions.mTnf == TNF::Empty) &&
|
|
|
|
(aOptions.mType.WasPassed() || aOptions.mId.WasPassed() ||
|
|
|
|
aOptions.mPayload.WasPassed())) {
|
|
|
|
NS_WARNING("tnf is empty but type/id/payload is not null.");
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the TNF value is 0x05 (Unknown) or 0x06(Unchanged), the TYPE field MUST
|
|
|
|
// be omitted from the NDEF record.
|
|
|
|
if ((aOptions.mTnf == TNF::Unknown || aOptions.mTnf == TNF::Unchanged) &&
|
|
|
|
aOptions.mType.WasPassed()) {
|
|
|
|
NS_WARNING("tnf is unknown/unchanged but type is not null.");
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-06 21:53:19 +04:00
|
|
|
/* static */
|
2014-01-29 04:20:17 +04:00
|
|
|
already_AddRefed<MozNDEFRecord>
|
|
|
|
MozNDEFRecord::Constructor(const GlobalObject& aGlobal,
|
2014-08-18 10:56:12 +04:00
|
|
|
const MozNDEFRecordOptions& aOptions,
|
2013-11-06 21:53:19 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-09 03:10:23 +04:00
|
|
|
|
2014-08-28 12:44:48 +04:00
|
|
|
if (!ValidateTNF(aOptions, aRv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-16 22:08:00 +04:00
|
|
|
nsRefPtr<MozNDEFRecord> ndefrecord = new MozNDEFRecord(aGlobal.Context(),
|
2014-08-18 10:56:12 +04:00
|
|
|
win, aOptions);
|
2013-11-09 03:10:23 +04:00
|
|
|
if (!ndefrecord) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-11-06 21:53:19 +04:00
|
|
|
return ndefrecord.forget();
|
|
|
|
}
|
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
MozNDEFRecord::MozNDEFRecord(JSContext* aCx, nsPIDOMWindow* aWindow,
|
2014-08-18 10:56:12 +04:00
|
|
|
const MozNDEFRecordOptions& aOptions)
|
2013-11-06 21:53:19 +04:00
|
|
|
{
|
2013-11-09 03:10:23 +04:00
|
|
|
mWindow = aWindow; // For GetParentObject()
|
|
|
|
|
2014-08-18 10:56:12 +04:00
|
|
|
mTnf = aOptions.mTnf;
|
|
|
|
|
|
|
|
if (aOptions.mType.WasPassed()) {
|
|
|
|
const Uint8Array& type = aOptions.mType.Value();
|
|
|
|
type.ComputeLengthAndData();
|
|
|
|
mType = Uint8Array::Create(aCx, this, type.Length(), type.Data());
|
2014-02-20 11:08:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 10:56:12 +04:00
|
|
|
if (aOptions.mId.WasPassed()) {
|
|
|
|
const Uint8Array& id = aOptions.mId.Value();
|
|
|
|
id.ComputeLengthAndData();
|
|
|
|
mId = Uint8Array::Create(aCx, this, id.Length(), id.Data());
|
2014-02-20 11:08:46 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 10:56:12 +04:00
|
|
|
if (aOptions.mPayload.WasPassed()) {
|
|
|
|
const Uint8Array& payload = aOptions.mPayload.Value();
|
|
|
|
payload.ComputeLengthAndData();
|
|
|
|
mPayload = Uint8Array::Create(aCx, this, payload.Length(), payload.Data());
|
2014-02-20 11:08:46 +04:00
|
|
|
}
|
2013-11-09 03:10:23 +04:00
|
|
|
|
2013-11-06 21:53:19 +04:00
|
|
|
SetIsDOMBinding();
|
2013-11-09 03:10:23 +04:00
|
|
|
HoldData();
|
2013-11-06 21:53:19 +04:00
|
|
|
}
|
|
|
|
|
2014-01-29 04:20:17 +04:00
|
|
|
MozNDEFRecord::~MozNDEFRecord()
|
2013-11-06 21:53:19 +04:00
|
|
|
{
|
2013-11-09 03:10:23 +04:00
|
|
|
DropData();
|
2013-11-06 21:53:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
MozNDEFRecord::WrapObject(JSContext* aCx)
|
2013-11-06 21:53:19 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return MozNDEFRecordBinding::Wrap(aCx, this);
|
2013-11-06 21:53:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|