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 "NFCConverter.h"
22 #include <dpl/log/log.h>
24 #include <Commons/Exception.h>
25 #include <CommonsJavaScript/PrivateObject.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include <Tizen/Common/SecurityExceptions.h>
30 #include <API/NFC/NFCFactory.h>
31 #include "plugin_config.h"
33 using namespace TizenApis::Api::NFC;
34 using namespace TizenApis::Commons;
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
39 const char* TIZEN10_NDEFMESSAGE_ATTRIBUTENAME = "NDEFMessage";
41 const char* TIZEN10_NDEFMESSAGE_RECORDCOUNT= "recordCount";
47 JSClassDefinition JSNdefMessage::m_classInfo =
50 kJSClassAttributeNone,
51 TIZEN10_NDEFMESSAGE_ATTRIBUTENAME,
60 NULL, //DeleteProperty,
61 NULL, //GetPropertyNames,
62 NULL, //CallAsFunction,
63 NULL, //CallAsConstructor,
68 JSStaticValue JSNdefMessage::m_property[] =
70 //NdefMessageProperties
71 { TIZEN10_NDEFMESSAGE_RECORDCOUNT, getProperty,
72 NULL, kJSPropertyAttributeReadOnly},
76 JSStaticFunction JSNdefMessage::m_function[] = {
77 {"toByte", JSNdefMessage::toByte, kJSPropertyAttributeNone},
78 {"getNDEFRecord", JSNdefMessage::getNDEFRecord, kJSPropertyAttributeNone},
79 {"insertNDEFRecord", JSNdefMessage::insertNDEFRecord, kJSPropertyAttributeNone},
80 {"appendNDEFRecord", JSNdefMessage::appendNDEFRecord, kJSPropertyAttributeNone},
81 {"removeNDEFRecord", JSNdefMessage::removeNDEFRecord, 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, std::vector<void *> ndefRcords) {
118 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords);
120 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
123 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
126 return JSObjectMake(context, getClassRef(), priv);
129 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<unsigned char> rawdata) {
132 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(rawdata);
134 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
137 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
140 return JSObjectMake(context, getClassRef(), priv);
142 void JSNdefMessage::initialize(JSContextRef context, JSObjectRef object)
146 void JSNdefMessage::finalize(JSObjectRef object)
148 LogDebug( "entered" );
149 NdefMessagePrivObject *priv = static_cast<NdefMessagePrivObject*>( JSObjectGetPrivate( object ) ) ;
150 JSObjectSetPrivate(object, NULL);
151 LogDebug("Deleting NdefMessage object");
156 const JSClassRef JSNdefMessage::getClassRef()
159 m_jsClassRef = JSClassCreate(&m_classInfo);
164 const JSClassDefinition* JSNdefMessage::getClassInfo()
169 JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object,
170 JSStringRef propertyName, JSValueRef* exception)
175 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
176 if (!privateObject) {
177 LogError("Private object is not set.");
178 ThrowMsg(NullPointerException, "Private object not initialized");
181 INdefMessagePtr NdefMessage(privateObject->getObject());
182 Converter convert(context);
184 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDCOUNT)) {
185 return convert.toJSValueRefLong(NdefMessage->getRecordCount());
188 } Catch (ConversionException) {
189 LogError("ConversionException: " << _rethrown_exception.GetMessage());
190 } Catch (WrtDeviceApis::Commons::Exception) {
191 LogError("Exception: " << _rethrown_exception.GetMessage());
193 return JSValueMakeUndefined(context);
196 JSValueRef JSNdefMessage::toByte(JSContextRef context,
198 JSObjectRef thisObject,
199 size_t argumentCount,
200 const JSValueRef arguments[],
201 JSValueRef* exception)
203 LogDebug("Entered ");
205 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
206 if (NULL == privateObject) {
207 LogError("private object is null");
208 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
211 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
212 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
214 INdefMessagePtr NdefMessage(privateObject->getObject());
215 NFCConverter convert(context);
217 return convert.toJSValueRef(NdefMessage->toByte());
218 } Catch (PlatformException) {
219 LogError("Exception: " << _rethrown_exception.GetMessage());
220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
221 } Catch(NullPointerException) {
222 LogError("Exception: " << _rethrown_exception.GetMessage());
223 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
224 } Catch (WrtDeviceApis::Commons::Exception) {
225 LogError("Exception: " << _rethrown_exception.GetMessage());
226 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
229 return JSValueMakeNull(context);
232 JSValueRef JSNdefMessage::getNDEFRecord(JSContextRef context,
234 JSObjectRef thisObject,
235 size_t argumentCount,
236 const JSValueRef arguments[],
237 JSValueRef* exception)
239 LogDebug("Entered ");
241 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
242 if (NULL == privateObject) {
243 LogError("private object is null");
244 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
247 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
248 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
250 if (argumentCount < 1) {
251 LogError("Wrong argument count");
252 Throw(InvalidArgumentException);
254 if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) {
255 LogError("Index parameter is JSNull/JSUndefined/Not Number");
256 Throw(InvalidArgumentException);
259 INdefMessagePtr NdefMessage(privateObject->getObject());
260 NFCConverter convert(context);
262 return JSNdefRecord::createJSObject(privateObject->getContext(), NdefMessage->getNDEFRecord(convert.toLong(arguments[0])));
263 } Catch (InvalidArgumentException) {
264 LogError("Exception: " << _rethrown_exception.GetMessage());
265 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Value");
266 } Catch(ConversionException) {
267 LogError("Exception: " << _rethrown_exception.GetMessage());
268 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
269 } Catch (PlatformException) {
270 LogError("Exception: " << _rethrown_exception.GetMessage());
271 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
272 } Catch(NullPointerException) {
273 LogError("Exception: " << _rethrown_exception.GetMessage());
274 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
275 } Catch (WrtDeviceApis::Commons::Exception) {
276 LogError("Exception: " << _rethrown_exception.GetMessage());
277 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
280 return JSValueMakeNull(context);
283 JSValueRef JSNdefMessage::insertNDEFRecord(JSContextRef context,
285 JSObjectRef thisObject,
286 size_t argumentCount,
287 const JSValueRef arguments[],
288 JSValueRef* exception)
290 LogDebug("Entered ");
292 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
293 if (NULL == privateObject) {
294 LogError("private object is null");
295 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
298 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
299 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
301 if (argumentCount < 2) {
302 LogError("Wrong argument count");
303 Throw(InvalidArgumentException);
305 if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) ||
306 (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || (!JSValueIsObjectOfClass(context, arguments[1], JSNdefRecord::getClassRef())))) {
307 LogError("Index parameter is JSNull/JSUndefined/Not Number");
308 Throw(InvalidArgumentException);
311 INdefMessagePtr NdefMessage(privateObject->getObject());
312 NFCConverter convert(context);
314 long index = convert.toLong(arguments[0]);
315 void *record = convert.getRecordHandle(arguments[1]);
317 NdefMessage->insertNDEFRecord(index, record);
318 } Catch (InvalidArgumentException) {
319 LogError("Exception: " << _rethrown_exception.GetMessage());
320 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Value");
321 } Catch(ConversionException) {
322 LogError("Exception: " << _rethrown_exception.GetMessage());
323 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
324 } Catch (PlatformException) {
325 LogError("Exception: " << _rethrown_exception.GetMessage());
326 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
327 } Catch(NullPointerException) {
328 LogError("Exception: " << _rethrown_exception.GetMessage());
329 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
330 } Catch (WrtDeviceApis::Commons::Exception) {
331 LogError("Exception: " << _rethrown_exception.GetMessage());
332 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
335 return JSValueMakeNull(context);
338 JSValueRef JSNdefMessage::appendNDEFRecord(JSContextRef context,
340 JSObjectRef thisObject,
341 size_t argumentCount,
342 const JSValueRef arguments[],
343 JSValueRef* exception)
345 LogDebug("Entered ");
347 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
348 if (NULL == privateObject) {
349 LogError("private object is null");
350 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
353 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
354 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
356 if (argumentCount < 1) {
357 LogError("Wrong argument count");
358 Throw(InvalidArgumentException);
361 INdefMessagePtr NdefMessage(privateObject->getObject());
362 NFCConverter convert(context);
364 void *record = convert.getRecordHandle(arguments[0]);
366 NdefMessage->appendNDEFRecord(record);
367 } Catch (InvalidArgumentException) {
368 LogError("Exception: " << _rethrown_exception.GetMessage());
369 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Value");
370 } Catch(ConversionException) {
371 LogError("Exception: " << _rethrown_exception.GetMessage());
372 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
373 } Catch (PlatformException) {
374 LogError("Exception: " << _rethrown_exception.GetMessage());
375 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
376 } Catch(NullPointerException) {
377 LogError("Exception: " << _rethrown_exception.GetMessage());
378 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
379 } Catch (WrtDeviceApis::Commons::Exception) {
380 LogError("Exception: " << _rethrown_exception.GetMessage());
381 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
383 return JSValueMakeNull(context);
386 JSValueRef JSNdefMessage::removeNDEFRecord(JSContextRef context,
388 JSObjectRef thisObject,
389 size_t argumentCount,
390 const JSValueRef arguments[],
391 JSValueRef* exception)
393 LogDebug("Entered ");
395 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
396 if (NULL == privateObject) {
397 LogError("private object is null");
398 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
401 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
402 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
404 if (argumentCount < 1) {
405 LogError("Wrong argument count");
406 Throw(InvalidArgumentException);
408 if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) {
409 LogError("Index parameter is JSNull/JSUndefined/Not Number");
410 Throw(InvalidArgumentException);
413 INdefMessagePtr NdefMessage(privateObject->getObject());
414 NFCConverter convert(context);
416 NdefMessage->removeNDEFRecord(convert.toLong(arguments[0]));
417 } Catch (InvalidArgumentException) {
418 LogError("Exception: " << _rethrown_exception.GetMessage());
419 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Value");
420 } Catch(ConversionException) {
421 LogError("Exception: " << _rethrown_exception.GetMessage());
422 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
423 } Catch (PlatformException) {
424 LogError("Exception: " << _rethrown_exception.GetMessage());
425 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
426 } Catch(NullPointerException) {
427 LogError("Exception: " << _rethrown_exception.GetMessage());
428 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
429 } Catch (WrtDeviceApis::Commons::Exception) {
430 LogError("Exception: " << _rethrown_exception.GetMessage());
431 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
434 return JSValueMakeNull(context);