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.
18 #include "JSNdefMessage.h"
19 #include "JSNdefRecord.h"
20 #include "JSNdefRecordText.h"
21 #include "JSNdefRecordURI.h"
22 #include "JSNdefRecordMedia.h"
23 #include "JSNdefRecordArray.h"
24 #include "NFCConverter.h"
26 #include <dpl/log/log.h>
28 #include <Commons/Exception.h>
29 #include <CommonsJavaScript/PrivateObject.h>
30 #include <CommonsJavaScript/JSUtils.h>
31 #include <Tizen/Common/JSTizenExceptionFactory.h>
32 #include <Tizen/Common/JSTizenException.h>
33 #include <Tizen/Common/SecurityExceptions.h>
34 #include <API/NFC/NFCFactory.h>
35 #include "plugin_config.h"
37 using namespace TizenApis::Api::NFC;
38 using namespace TizenApis::Commons;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
42 #define TIZEN10_NDEFMESSAGE_ATTRIBUTENAME "NDEFMessage"
43 #define TIZEN10_NDEFMESSAGE_RECORDCOUNT "recordCount"
44 #define TIZEN10_NDEFMESSAGE_RECORDS "records"
49 JSClassDefinition JSNdefMessage::m_classInfo =
52 kJSClassAttributeNone,
53 TIZEN10_NDEFMESSAGE_ATTRIBUTENAME,
61 setProperty, //setProperty,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 constructor, //CallAsConstructor,
66 hasInstance, //HasInstance,
70 JSStaticValue JSNdefMessage::m_property[] =
72 //NdefMessageProperties
73 { TIZEN10_NDEFMESSAGE_RECORDCOUNT, getProperty,
74 NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN10_NDEFMESSAGE_RECORDS, getProperty,
76 setProperty, kJSPropertyAttributeNone},
80 JSStaticFunction JSNdefMessage::m_function[] = {
81 {"toByte", JSNdefMessage::toByte, kJSPropertyAttributeNone},
85 JSClassRef JSNdefMessage::m_jsClassRef = JSClassCreate(JSNdefMessage::getClassInfo());
87 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, void *messageHandle) {
90 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(messageHandle);
92 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
95 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
98 return JSObjectMake(context, getClassRef(), priv);
101 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context) {
104 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject();
106 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
109 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
112 return JSObjectMake(context, getClassRef(), priv);
115 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, INdefMessagePtr message) {
118 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, message);
121 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
124 return JSObjectMake(context, getClassRef(), priv);
127 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<void *> ndefRcords) {
130 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords);
132 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
135 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
138 return JSObjectMake(context, getClassRef(), priv);
141 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<unsigned char> rawdata) {
144 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(rawdata);
146 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
149 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
152 return JSObjectMake(context, getClassRef(), priv);
154 void JSNdefMessage::initialize(JSContextRef context, JSObjectRef object)
158 if (!JSObjectGetPrivate(object)) {
159 LogDebug("Private object not set... setting it.");
160 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject();
161 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
162 if (!JSObjectSetPrivate(object, priv)) {
168 void JSNdefMessage::finalize(JSObjectRef object)
170 LogDebug( "entered" );
171 NdefMessagePrivObject *priv = static_cast<NdefMessagePrivObject*>( JSObjectGetPrivate( object ) ) ;
172 JSObjectSetPrivate(object, NULL);
173 LogDebug("Deleting NdefMessage object");
178 const JSClassRef JSNdefMessage::getClassRef()
181 m_jsClassRef = JSClassCreate(&m_classInfo);
186 const JSClassDefinition* JSNdefMessage::getClassInfo()
191 JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
195 NdefMessagePrivObject* mainPriv = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(constructor));
198 if ((argumentCount == 0) || JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]))
199 return createJSObject(ctx);
201 if (!(JSIsArrayValue(ctx, arguments[0]) || JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])))
202 ThrowMsg(ConversionException, "Parameter's type is not matched");
204 NFCConverter convert(ctx);
206 if (JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])) {
207 return createJSObject(ctx, JSNdefRecordArray::getNdefRecordArray(ctx, arguments[0]));
210 JSObjectRef objArg = convert.toJSObjectRef(arguments[0]);
211 if (JSGetArrayLength(ctx, objArg) > 0) {
212 bool isRecordArray = false;
213 bool isByteArray = false;
214 for (std::size_t i = 0; i < JSGetArrayLength(ctx, objArg); ++i) {
215 JSValueRef element = JSGetArrayElement(ctx, objArg, i);
216 if (convert.isNdefRecord(element))
217 isRecordArray = true;
220 if (isRecordArray && isByteArray)
223 if (isRecordArray && isByteArray)
224 ThrowMsg(ConversionException, "Parameter's type is not matched");
225 else if (isRecordArray)
226 return createJSObject(ctx, convert.toVectorOfRecordHandles(arguments[0]));
228 return createJSObject(ctx, convert.toVectorOfUChars(arguments[0]));
230 return createJSObject(ctx);
232 } Catch(ConversionException) {
233 LogError("ConversionException: " << _rethrown_exception.GetMessage());
234 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
236 } Catch (InvalidArgumentException) {
237 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
238 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
240 } Catch (PlatformException) {
241 LogError("PlatformException: " << _rethrown_exception.GetMessage());
242 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
244 } Catch (UnknownException) {
245 LogError("PlatformException: " << _rethrown_exception.GetMessage());
246 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
248 } Catch (WrtDeviceApis::Commons::Exception) {
249 LogError("Exception: " << _rethrown_exception.GetMessage());
250 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
253 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
257 JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object,
258 JSStringRef propertyName, JSValueRef* exception)
263 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
264 if (!privateObject) {
265 LogError("Private object is not set.");
266 ThrowMsg(NullPointerException, "Private object not initialized");
269 INdefMessagePtr NdefMessage(privateObject->getObject());
270 NFCConverter convert(context);
272 LogDebug("propertyName : " << convert.toString(propertyName));
273 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDCOUNT)) {
274 return convert.toJSValueRefLong(NdefMessage->getRecordCount());
275 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDS)) {
276 return JSNdefRecordArray::createArray(context, NdefMessage);
278 } Catch (ConversionException) {
279 LogError("ConversionException: " << _rethrown_exception.GetMessage());
280 } Catch (NullPointerException) {
281 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
282 } Catch (UnknownException) {
283 LogError("UnknownExceptionException: " << _rethrown_exception.GetMessage());
284 } Catch (PlatformException) {
285 LogError("PlatformExceptionException: " << _rethrown_exception.GetMessage());
286 } Catch (WrtDeviceApis::Commons::Exception) {
287 LogError("Exception: " << _rethrown_exception.GetMessage());
289 return JSValueMakeUndefined(context);
292 bool JSNdefMessage::setProperty(JSContextRef context, JSObjectRef object,
293 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
298 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDS)) {
299 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
300 if (!privateObject) {
301 LogError("Private object is not set.");
302 ThrowMsg(NullPointerException, "Private object not initialized");
305 NFCConverter convert(context);
307 if (JSValueIsNull(context, value) || JSValueIsUndefined(context, value) || !(JSIsArrayValue(context, value) || JSNdefRecordArray::isObjectOfClass(context, value))) {
308 LogError("value is invald.");
309 ThrowMsg(ConversionException, "value is invald.");
312 NdefMessagePrivObject *priv;
313 if (JSNdefRecordArray::isObjectOfClass(context, value))
314 priv = new NdefMessagePrivObject(context, JSNdefRecordArray::getNdefRecordArray(context, value));
316 std::vector<void *> records = convert.toVectorOfRecordHandles(value);
317 priv = new NdefMessagePrivObject(context, NFCFactory::getInstance().createNDEFMessageObject(records));
319 if (!JSObjectSetPrivate(object, priv)) {
321 ThrowMsg(NullPointerException, "Private object not set");
323 delete privateObject;
326 } Catch (NullPointerException) {
327 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
328 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
329 } Catch (ConversionException) {
330 LogError("ConversionException: " << _rethrown_exception.GetMessage());
331 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
332 } Catch (InvalidArgumentException) {
333 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
334 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
335 } Catch (PlatformException) {
336 LogError("PlatformException: " << _rethrown_exception.GetMessage());
337 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
338 } Catch (UnknownException) {
339 LogError("PlatformException: " << _rethrown_exception.GetMessage());
340 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
341 } Catch (WrtDeviceApis::Commons::Exception) {
342 LogError("Exception: " << _rethrown_exception.GetMessage());
343 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
349 bool JSNdefMessage::hasInstance(JSContextRef context,
350 JSObjectRef constructor,
351 JSValueRef possibleInstance,
352 JSValueRef* exception)
354 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
357 JSValueRef JSNdefMessage::toByte(JSContextRef context,
359 JSObjectRef thisObject,
360 size_t argumentCount,
361 const JSValueRef arguments[],
362 JSValueRef* exception)
364 LogDebug("Entered ");
366 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_P2P_FUNCS);
367 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
369 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
370 if (NULL == privateObject) {
371 LogError("private object is null");
372 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
375 INdefMessagePtr NdefMessage(privateObject->getObject());
376 NFCConverter convert(context);
378 return convert.toJSValueRef(NdefMessage->toByte());
379 } Catch (UnknownException) {
380 LogError("Exception: " << _rethrown_exception.GetMessage());
381 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
382 } Catch (PlatformException) {
383 LogError("Exception: " << _rethrown_exception.GetMessage());
384 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
385 } Catch(NullPointerException) {
386 LogError("Exception: " << _rethrown_exception.GetMessage());
387 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
388 } Catch (WrtDeviceApis::Commons::Exception) {
389 LogError("Exception: " << _rethrown_exception.GetMessage());
390 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
393 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");