c16697fe966c389d1206fab466cad9cf010adfee
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNdefMessage.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include "JSNdefMessage.h"
19 #include "JSNdefRecord.h"
20 #include "NFCConverter.h"
21
22 #include <dpl/log/log.h>
23
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"
32
33 using namespace TizenApis::Api::NFC;
34 using namespace TizenApis::Commons;
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37
38 namespace {
39     const char* TIZEN10_NDEFMESSAGE_ATTRIBUTENAME     = "NDEFMessage";
40
41     const char* TIZEN10_NDEFMESSAGE_RECORDCOUNT= "recordCount";
42 }
43
44 namespace TizenApis {
45 namespace Tizen1_0 {
46
47  JSClassDefinition JSNdefMessage::m_classInfo =
48 {
49     0,
50     kJSClassAttributeNone,
51     TIZEN10_NDEFMESSAGE_ATTRIBUTENAME,
52     0,
53     m_property,
54     m_function,
55     initialize,
56     finalize,
57     NULL, //hasProperty,
58     NULL,
59     NULL, //setProperty,
60     NULL, //DeleteProperty,
61     NULL, //GetPropertyNames,
62     NULL, //CallAsFunction,
63     NULL, //CallAsConstructor,
64     NULL, //HasInstance,
65     NULL  //ConvertToType
66 };
67
68 JSStaticValue JSNdefMessage::m_property[] =
69 {
70     //NdefMessageProperties
71     { TIZEN10_NDEFMESSAGE_RECORDCOUNT, getProperty,
72             NULL, kJSPropertyAttributeReadOnly},
73     { 0, 0, 0, 0 }
74 };
75
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},
82     { 0, 0, 0}
83 };
84
85 JSClassRef JSNdefMessage::m_jsClassRef = JSClassCreate(JSNdefMessage::getClassInfo());
86
87 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, void *messageHandle) {
88         LogDebug("entered");
89
90         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(messageHandle);
91
92         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
93
94         if (!priv) {
95                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
96         }
97
98         return JSObjectMake(context, getClassRef(), priv);
99 }
100
101 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context) {
102         LogDebug("entered");
103
104         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject();
105
106         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
107
108         if (!priv) {
109                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
110         }
111
112         return JSObjectMake(context, getClassRef(), priv);
113 }
114
115 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<void *> ndefRcords) {
116         LogDebug("entered");
117
118         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords);
119
120         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
121
122         if (!priv) {
123                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
124         }
125
126         return JSObjectMake(context, getClassRef(), priv);
127 }
128
129 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<unsigned char> rawdata) {
130         LogDebug("entered");
131
132         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(rawdata);
133
134         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
135
136         if (!priv) {
137                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
138         }
139
140         return JSObjectMake(context, getClassRef(), priv);
141 }
142 void JSNdefMessage::initialize(JSContextRef context, JSObjectRef object)
143 {
144 }
145
146 void JSNdefMessage::finalize(JSObjectRef object)
147 {
148         LogDebug( "entered" );
149         NdefMessagePrivObject *priv = static_cast<NdefMessagePrivObject*>( JSObjectGetPrivate( object ) ) ;
150         JSObjectSetPrivate(object, NULL);
151         LogDebug("Deleting NdefMessage object");
152         delete priv;
153 }
154
155
156 const JSClassRef JSNdefMessage::getClassRef()
157 {
158         if (!m_jsClassRef) {
159                 m_jsClassRef = JSClassCreate(&m_classInfo);
160         }
161         return m_jsClassRef;
162 }
163
164 const JSClassDefinition* JSNdefMessage::getClassInfo()
165 {
166         return &m_classInfo;
167 }
168
169 JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object,
170         JSStringRef propertyName, JSValueRef* exception)
171 {
172         LogDebug("Enter");
173
174         Try     {
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");
179                 }
180
181                 INdefMessagePtr NdefMessage(privateObject->getObject());
182                 Converter convert(context);
183
184                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDCOUNT)) {
185                         return convert.toJSValueRefLong(NdefMessage->getRecordCount());
186                 }
187
188         } Catch (ConversionException) {
189                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
190         } Catch (WrtDeviceApis::Commons::Exception) {
191                 LogError("Exception: " << _rethrown_exception.GetMessage());
192         }
193         return JSValueMakeUndefined(context);
194 }
195
196 JSValueRef JSNdefMessage::toByte(JSContextRef context,
197         JSObjectRef object,
198         JSObjectRef thisObject,
199         size_t argumentCount,
200         const JSValueRef arguments[],
201         JSValueRef* exception)
202 {
203         LogDebug("Entered ");
204         Try {
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");
209                 }
210
211                 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
212                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
213
214                 INdefMessagePtr NdefMessage(privateObject->getObject());
215                 NFCConverter convert(context);
216
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");
227         }
228
229         return JSValueMakeNull(context);
230 }
231
232 JSValueRef JSNdefMessage::getNDEFRecord(JSContextRef context,
233         JSObjectRef object,
234         JSObjectRef thisObject,
235         size_t argumentCount,
236         const JSValueRef arguments[],
237         JSValueRef* exception)
238 {
239         LogDebug("Entered ");
240         Try {
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");
245                 }
246
247                 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
248                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
249
250                 if (argumentCount < 1) {
251                         LogError("Wrong argument count");
252                         Throw(InvalidArgumentException);
253                 }
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);
257                 }
258
259                 INdefMessagePtr NdefMessage(privateObject->getObject());
260                 NFCConverter convert(context);
261
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");
278         }
279
280         return JSValueMakeNull(context);
281 }
282
283 JSValueRef JSNdefMessage::insertNDEFRecord(JSContextRef context,
284         JSObjectRef object,
285         JSObjectRef thisObject,
286         size_t argumentCount,
287         const JSValueRef arguments[],
288         JSValueRef* exception)
289 {
290         LogDebug("Entered ");
291         Try {
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");
296                 }
297
298                 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
299                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
300
301                 if (argumentCount < 2) {
302                         LogError("Wrong argument count");
303                         Throw(InvalidArgumentException);
304                 }
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);
309                 }
310
311                 INdefMessagePtr NdefMessage(privateObject->getObject());
312                 NFCConverter convert(context);
313
314                 long index = convert.toLong(arguments[0]);
315                 void *record = convert.getRecordHandle(arguments[1]);
316
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");
333         }
334
335         return JSValueMakeNull(context);
336 }
337
338 JSValueRef JSNdefMessage::appendNDEFRecord(JSContextRef context,
339         JSObjectRef object,
340         JSObjectRef thisObject,
341         size_t argumentCount,
342         const JSValueRef arguments[],
343         JSValueRef* exception)
344 {
345         LogDebug("Entered ");
346         Try {
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");
351                 }
352
353                 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
354                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
355
356                 if (argumentCount < 1) {
357                         LogError("Wrong argument count");
358                         Throw(InvalidArgumentException);
359                 }
360
361                 INdefMessagePtr NdefMessage(privateObject->getObject());
362                 NFCConverter convert(context);
363
364                 void *record = convert.getRecordHandle(arguments[0]);
365
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");
382         }
383         return JSValueMakeNull(context);
384 }
385
386 JSValueRef JSNdefMessage::removeNDEFRecord(JSContextRef context,
387         JSObjectRef object,
388         JSObjectRef thisObject,
389         size_t argumentCount,
390         const JSValueRef arguments[],
391         JSValueRef* exception)
392 {
393         LogDebug("Entered ");
394         Try {
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");
399                 }
400
401                 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
402                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
403
404                 if (argumentCount < 1) {
405                         LogError("Wrong argument count");
406                         Throw(InvalidArgumentException);
407                 }
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);
411                 }
412
413                 INdefMessagePtr NdefMessage(privateObject->getObject());
414                 NFCConverter convert(context);
415
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");
432         }
433
434         return JSValueMakeNull(context);
435 }
436
437 }
438 }