2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include <Tizen/Common/SecurityExceptions.h>
27 #include <API/NFC/NFCFactory.h>
29 #include "JSNdefRecord.h"
30 #include "plugin_config.h"
31 #include "NFCConverter.h"
36 using namespace TizenApis::Commons;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace Api::NFC;
41 #define TIZEN10_NDEFRECORD_TNF "tnf"
42 #define TIZEN10_NDEFRECORD_TYPENAME "typeName"
43 #define TIZEN10_NDEFRECORD_ID "id"
44 #define TIZEN10_NDEFRECORD_PAYLOAD "payload"
46 JSClassDefinition JSNdefRecord::m_classInfo =
49 kJSClassAttributeNone,
59 NULL, //DeleteProperty,
60 NULL, //GetPropertyNames,
61 NULL, //CallAsFunction,
62 constructor, //CallAsConstructor,
67 JSStaticValue JSNdefRecord::m_property[] =
69 {TIZEN10_NDEFRECORD_TNF, getProperty, NULL, kJSPropertyAttributeReadOnly},
70 {TIZEN10_NDEFRECORD_TYPENAME, getProperty, NULL, kJSPropertyAttributeReadOnly},
71 {TIZEN10_NDEFRECORD_ID, getProperty, NULL, kJSPropertyAttributeReadOnly},
72 {TIZEN10_NDEFRECORD_PAYLOAD, getProperty, NULL, kJSPropertyAttributeReadOnly},
76 const JSClassRef JSNdefRecord::getClassRef() {
78 m_jsClassRef = JSClassCreate(&m_classInfo);
83 const JSClassDefinition* JSNdefRecord::getClassInfo() {
87 JSClassRef JSNdefRecord::m_jsClassRef = JSClassCreate(JSNdefRecord::getClassInfo());
89 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordData &ndefRecordData) {
91 return createJSObject(context, ndefRecordData.properties, ndefRecordData.payload);
94 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
95 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload);
97 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
100 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
103 return JSObjectMake(context, getClassRef(), priv);
106 void JSNdefRecord::initialize(JSContextRef context, JSObjectRef object) {
109 if (!JSObjectGetPrivate(object)) {
110 LogDebug("Private object not set... setting it.");
111 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject();
112 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
113 if (!JSObjectSetPrivate(object, priv)) {
119 void JSNdefRecord::finalize(JSObjectRef object) {
121 NdefRecordPrivObject* priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
122 JSObjectSetPrivate(object, NULL);
123 LogDebug("Deleting ndefrecord object");
127 JSObjectRef JSNdefRecord::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
131 NdefRecordPrivObject* mainPriv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(constructor));
132 JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
134 if ((argumentCount < 3) || (argumentCount > 4)) {
135 ThrowMsg(InvalidArgumentException, "parameter count is wrong.");
136 } else if ((JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !JSValueIsNumber(ctx, arguments[0]))
137 || (JSValueIsNull(ctx, arguments[1]) || JSValueIsUndefined(ctx, arguments[1]) || !JSIsArrayValue(ctx, arguments[1]))
138 || (JSValueIsNull(ctx, arguments[2]) || JSValueIsUndefined(ctx, arguments[2]) || !JSIsArrayValue(ctx, arguments[2]))) {
139 ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not array");
141 NFCConverter convert(ctx);
143 NdefRecordProperties prop;
144 prop.tnf = static_cast<nfcTNF>(convert.toLong(arguments[0]));
146 prop.typeName = convert.toVectorOfUChars(arguments[1]);
147 if ((argumentCount == 4) && !JSValueIsNull(ctx, arguments[3])) {
148 if ( JSValueIsUndefined(ctx, arguments[3]) || !JSIsArrayValue(ctx, arguments[3]))
149 ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not array");
150 prop.id = convert.toVectorOfUChars(arguments[3]);
153 return createJSObject(global_context, prop, convert.toVectorOfUChars(arguments[2]));
155 } Catch(ConversionException) {
156 LogError("ConversionException: " << _rethrown_exception.GetMessage());
157 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
159 } Catch (InvalidArgumentException) {
160 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
161 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
163 } Catch (PlatformException) {
164 LogError("PlatformException: " << _rethrown_exception.GetMessage());
165 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
167 } Catch (WrtDeviceApis::Commons::Exception) {
168 LogError("Exception: " << _rethrown_exception.GetMessage());
169 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
172 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
176 JSValueRef JSNdefRecord::getProperty(JSContextRef context, JSObjectRef object,
177 JSStringRef propertyName, JSValueRef* exception) {
181 NFCConverter convert(context);
182 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
184 ThrowMsg(NullPointerException, "Private object not set.");
186 INdefRecordPtr NdefRecord = priv->getObject();
188 LogDebug("propertyName : " << convert.toString(propertyName));
189 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TNF))
190 return convert.toJSValueRef(NdefRecord->getTNF());
191 else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TYPENAME))
192 return convert.toJSValueRef(NdefRecord->getTypeName());
193 else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_ID))
194 return convert.toJSValueRef(NdefRecord->getID());
195 else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_PAYLOAD))
196 return convert.toJSValueRef(NdefRecord->getPayload());
197 } Catch (ConversionException) {
198 LogError("ConversionException: " << _rethrown_exception.GetMessage());
199 } Catch (PlatformException) {
200 LogError("PlatformException: " << _rethrown_exception.GetMessage());
201 } Catch (NullPointerException) {
202 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
203 } Catch (WrtDeviceApis::Commons::Exception) {
204 LogError("Exception: " << _rethrown_exception.GetMessage());
206 return JSValueMakeUndefined(context);
209 bool JSNdefRecord::hasInstance(JSContextRef context,
210 JSObjectRef constructor,
211 JSValueRef possibleInstance,
212 JSValueRef* exception)
214 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());