308c2faab747f202ee98d41b94a54ad0ad5d9066
[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 "JSNdefRecordText.h"
21 #include "JSNdefRecordURI.h"
22 #include "JSNdefRecordMedia.h"
23 #include "JSNdefRecordArray.h"
24 #include "NFCConverter.h"
25
26 #include <dpl/log/log.h>
27
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"
36
37 using namespace TizenApis::Api::NFC;
38 using namespace TizenApis::Commons;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41
42 #define TIZEN10_NDEFMESSAGE_ATTRIBUTENAME "NDEFMessage"
43 #define TIZEN10_NDEFMESSAGE_RECORDCOUNT "recordCount"
44 #define TIZEN10_NDEFMESSAGE_RECORDS "records"
45
46 namespace TizenApis {
47 namespace Tizen1_0 {
48
49  JSClassDefinition JSNdefMessage::m_classInfo =
50 {
51     0,
52     kJSClassAttributeNone,
53     TIZEN10_NDEFMESSAGE_ATTRIBUTENAME,
54     0,
55     m_property,
56     m_function,
57     initialize,
58     finalize,
59     NULL, //hasProperty,
60     NULL,
61     setProperty, //setProperty,
62     NULL, //DeleteProperty,
63     NULL, //GetPropertyNames,
64     NULL, //CallAsFunction,
65     constructor, //CallAsConstructor,
66     hasInstance, //HasInstance,
67     NULL  //ConvertToType
68 };
69
70 JSStaticValue JSNdefMessage::m_property[] =
71 {
72     //NdefMessageProperties
73     { TIZEN10_NDEFMESSAGE_RECORDCOUNT, getProperty,
74             NULL, kJSPropertyAttributeReadOnly},
75      { TIZEN10_NDEFMESSAGE_RECORDS, getProperty,
76             setProperty, kJSPropertyAttributeNone},
77     { 0, 0, 0, 0 }
78 };
79
80 JSStaticFunction JSNdefMessage::m_function[] = {
81     {"toByte", JSNdefMessage::toByte, 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, INdefMessagePtr message) {
116         LogDebug("entered");
117
118         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, message);
119
120         if (!priv) {
121                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
122         }
123
124         return JSObjectMake(context, getClassRef(), priv);
125 }
126
127 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<void *> ndefRcords) {
128         LogDebug("entered");
129
130         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords);
131
132         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
133
134         if (!priv) {
135                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
136         }
137
138         return JSObjectMake(context, getClassRef(), priv);
139 }
140
141 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<unsigned char> rawdata) {
142         LogDebug("entered");
143
144         INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(rawdata);
145
146         NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
147
148         if (!priv) {
149                 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
150         }
151
152         return JSObjectMake(context, getClassRef(), priv);
153 }
154 void JSNdefMessage::initialize(JSContextRef context, JSObjectRef object)
155 {
156         LogDebug("entered");
157
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)) {
163                         delete priv;
164                 }
165         }
166 }
167
168 void JSNdefMessage::finalize(JSObjectRef object)
169 {
170         LogDebug( "entered" );
171         NdefMessagePrivObject *priv = static_cast<NdefMessagePrivObject*>( JSObjectGetPrivate( object ) ) ;
172         JSObjectSetPrivate(object, NULL);
173         LogDebug("Deleting NdefMessage object");
174         delete priv;
175 }
176
177
178 const JSClassRef JSNdefMessage::getClassRef()
179 {
180         if (!m_jsClassRef) {
181                 m_jsClassRef = JSClassCreate(&m_classInfo);
182         }
183         return m_jsClassRef;
184 }
185
186 const JSClassDefinition* JSNdefMessage::getClassInfo()
187 {
188         return &m_classInfo;
189 }
190
191 JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
192 {
193         LogDebug("entered");
194
195         NdefMessagePrivObject* mainPriv = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(constructor));
196
197         Try {
198                 if ((argumentCount == 0) || JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]))
199                         return createJSObject(ctx);
200                 else {
201                         if (!(JSIsArrayValue(ctx, arguments[0]) || JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])))
202                                 ThrowMsg(ConversionException, "Parameter's type is not matched");
203
204                         NFCConverter convert(ctx);
205
206                         if (JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])) {
207                                 return createJSObject(ctx, JSNdefRecordArray::getNdefRecordArray(ctx, arguments[0]));
208                         }
209
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;
218                                         else
219                                                 isByteArray = true;
220                                         if (isRecordArray && isByteArray)
221                                                 break;
222                                 }
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]));
227                                 else
228                                         return createJSObject(ctx, convert.toVectorOfUChars(arguments[0]));
229                         } else
230                                 return createJSObject(ctx);
231                 }
232         } Catch(ConversionException) {
233                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
234                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
235                 return NULL;
236         } Catch (InvalidArgumentException) {
237                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
238                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
239                 return NULL;
240         } Catch (PlatformException) {
241                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
242                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
243                 return NULL;
244         } Catch (UnknownException) {
245                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
246                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
247                 return NULL;
248         } Catch (WrtDeviceApis::Commons::Exception) {
249                 LogError("Exception: " << _rethrown_exception.GetMessage());
250                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
251                 return NULL;
252         }
253         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
254         return NULL;
255 }
256
257 JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object,
258         JSStringRef propertyName, JSValueRef* exception)
259 {
260         LogDebug("Enter");
261
262         Try     {
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");
267                 }
268
269                 INdefMessagePtr NdefMessage(privateObject->getObject());
270                 NFCConverter convert(context);
271
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);
277                 }
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());
288         }
289         return JSValueMakeUndefined(context);
290 }
291
292 bool JSNdefMessage::setProperty(JSContextRef context, JSObjectRef object,
293         JSStringRef propertyName, JSValueRef value,  JSValueRef* exception)
294 {
295         LogDebug("Enter");
296
297         Try     {
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");
303                         }
304
305                         NFCConverter convert(context);
306
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.");
310                         }
311
312                         NdefMessagePrivObject *priv;
313                         if (JSNdefRecordArray::isObjectOfClass(context, value))
314                                 priv = new NdefMessagePrivObject(context, JSNdefRecordArray::getNdefRecordArray(context, value));
315                         else {
316                                 std::vector<void *> records = convert.toVectorOfRecordHandles(value);
317                                 priv = new NdefMessagePrivObject(context, NFCFactory::getInstance().createNDEFMessageObject(records));
318                         }
319                         if (!JSObjectSetPrivate(object, priv)) {
320                                 delete priv;
321                                         ThrowMsg(NullPointerException, "Private object not set");
322                         }
323                         delete privateObject;
324                         return true;
325                  }
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");
344         }
345         return false;
346
347 }
348
349 bool JSNdefMessage::hasInstance(JSContextRef context,
350         JSObjectRef constructor,
351         JSValueRef possibleInstance,
352         JSValueRef* exception)
353 {
354     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
355 }
356
357 JSValueRef JSNdefMessage::toByte(JSContextRef context,
358         JSObjectRef object,
359         JSObjectRef thisObject,
360         size_t argumentCount,
361         const JSValueRef arguments[],
362         JSValueRef* exception)
363 {
364         LogDebug("Entered ");
365         Try {
366                 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_P2P_FUNCS);
367                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
368         
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");
373                 }
374
375                 INdefMessagePtr NdefMessage(privateObject->getObject());
376                 NFCConverter convert(context);
377
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");
391         }
392
393         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
394 }
395
396 }
397 }