Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNFCTarget.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 "JSNFCTarget.h"
19 #include "JSNFCManager.h"
20 #include "NFCConverter.h"
21 #include "ResponseDispatcher.h"
22 #include <dpl/log/log.h>
23
24 #include <CommonsJavaScript/Validator.h>
25 #include <Commons/Exception.h>
26 #include <CommonsJavaScript/PrivateObject.h>
27 #include <CommonsJavaScript/JSUtils.h>
28 #include <CommonsJavaScript/JSCallbackManager.h>
29 #include <CommonsJavaScript/Utils.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/SecurityExceptions.h>
33 #include <API/NFC/NFCFactory.h>
34 #include <API/NFC/EventTargetAction.h>
35
36 #include "plugin_config.h"
37
38
39 using namespace TizenApis::Api::NFC;
40 using namespace TizenApis::Commons;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace DPL;
44
45 #define TIZEN10_NFCTARGET_ATTRIBUTENAME "NFCTarget"
46
47 #define TIZEN10_NFCTARGET_ISCONNECTED "isConnected"
48
49 namespace TizenApis {
50 namespace Tizen1_0 {
51
52  JSClassDefinition JSNFCTarget::m_classInfo =
53 {
54     0,
55     kJSClassAttributeNone,
56     TIZEN10_NFCTARGET_ATTRIBUTENAME,
57     0,
58     m_property,
59     m_function,
60     initialize,
61     finalize,
62     NULL, //hasProperty,
63     NULL,
64     NULL, //setProperty,
65     NULL, //DeleteProperty,
66     NULL, //GetPropertyNames,
67     NULL, //CallAsFunction,
68     NULL, //CallAsConstructor,
69     hasInstance, //HasInstance,
70     NULL  //ConvertToType
71 };
72
73 JSStaticValue JSNFCTarget::m_property[] =
74 {
75     //NFCTargetProperties
76     { TIZEN10_NFCTARGET_ISCONNECTED, getProperty,
77             NULL, kJSPropertyAttributeReadOnly},
78     { 0, 0, 0, 0 }
79 };
80
81 JSStaticFunction JSNFCTarget::m_function[] = {
82     {"setReceiveNDEFListener", JSNFCTarget::setReceiveNDEFListener, kJSPropertyAttributeNone},
83     {"unsetReceiveNDEFListener", JSNFCTarget::unsetReceiveNDEFListener, kJSPropertyAttributeNone},
84     {"sendNDEF", JSNFCTarget::sendNDEF, kJSPropertyAttributeNone},
85     { 0, 0, 0}
86 };
87
88 JSClassRef JSNFCTarget::m_jsClassRef = JSClassCreate(JSNFCTarget::getClassInfo());
89
90 JSObjectRef JSNFCTarget::createJSObject(JSContextRef context, void *tagHandle, void* privManager) {
91         LogDebug("entered");
92
93         INFCTargetPtr NFCTarget = NFCFactory::getInstance().createNFCTargetObject(tagHandle);
94         NFCTarget->setPrivateNFCManagerPtr(privManager);
95
96         NFCTargetPrivObject *priv = new NFCTargetPrivObject(context, NFCTarget);
97
98         if (!priv) {
99                 ThrowMsg(NullPointerException, "Can not new a NFCTarget object");
100         }
101
102         return JSObjectMake(context, getClassRef(), priv);
103 }
104
105 void JSNFCTarget::initialize(JSContextRef context, JSObjectRef object)
106 {
107 }
108
109 void JSNFCTarget::finalize(JSObjectRef object)
110 {
111         LogDebug( "entered" );
112         NFCTargetPrivObject *priv = static_cast<NFCTargetPrivObject*>( JSObjectGetPrivate( object ) ) ;
113         JSObjectSetPrivate(object, NULL);
114         LogDebug("Deleting timezone object");
115         delete priv;
116 }
117
118
119 const JSClassRef JSNFCTarget::getClassRef()
120 {
121         if (!m_jsClassRef) {
122                 m_jsClassRef = JSClassCreate(&m_classInfo);
123         }
124         return m_jsClassRef;
125 }
126
127 const JSClassDefinition* JSNFCTarget::getClassInfo()
128 {
129         return &m_classInfo;
130 }
131
132 JSValueRef JSNFCTarget::getProperty(JSContextRef context, JSObjectRef object,
133         JSStringRef propertyName, JSValueRef* exception)
134 {
135         LogDebug("Enter");
136
137         Try     {
138                 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(object));
139                 if (!privateObject) {
140                         LogError("Private object is not set.");
141                         ThrowMsg(NullPointerException, "Private object not initialized");
142                 }
143
144                 INFCTargetPtr NFCTarget(privateObject->getObject());
145                 NFCConverter convert(context);
146
147                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NFCTARGET_ISCONNECTED)) {
148                         NFCManagerPrivObject* privateNFCManager = static_cast<NFCManagerPrivObject*>(NFCTarget->getPrivateNFCManagerPtr());
149                         if (!privateNFCManager) {
150                                 LogError("NFC Manager Private object is not set.");
151                                 ThrowMsg(NullPointerException, "Private object not initialized");
152                         }
153                         INFCManagerPtr NFCManager(privateNFCManager->getObject());
154                         return convert.toJSValueRef(NFCManager->isValidHandle(NFCTarget->getHandle()));
155                 }
156         } Catch (ConversionException) {
157                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
158         } Catch (NullPointerException) {
159                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
160         } Catch (WrtDeviceApis::Commons::Exception) {
161                 LogError("Exception: " << _rethrown_exception.GetMessage());
162         }
163         return JSValueMakeUndefined(context);
164 }
165
166 bool JSNFCTarget::hasInstance(JSContextRef context,
167         JSObjectRef constructor,
168         JSValueRef possibleInstance,
169         JSValueRef* exception)
170 {
171     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
172 }
173
174 JSValueRef JSNFCTarget::setReceiveNDEFListener(JSContextRef context,
175         JSObjectRef object,
176         JSObjectRef thisObject,
177         size_t argumentCount,
178         const JSValueRef arguments[],
179         JSValueRef* exception)
180 {
181         LogDebug("Entered ");
182
183         NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(thisObject));
184         if (NULL == privateObject) {
185                 LogError("private object is null");
186                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
187         }
188
189         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_P2P_FUNCS);
190         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
191
192         NFCConverter convert(context);
193         Validator validator(context, exception);
194
195         if ((argumentCount < 1) || (argumentCount > 2)) {
196                 LogError("JSNFCTarget::setReceiveNDEFListener argumentCount error");
197                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
198         }
199         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !validator.isCallback(arguments[0])) {
200                 /* 1st argument is mandatory. And 1st argument must be Callback. */
201                 LogError("JSNFCTarget::setReceiveNDEFListener NDEFMessageReadCB TypeMismatchException!");
202                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
203         }
204         if ((argumentCount > 1)  && !JSValueIsNull(context, arguments[1])){
205                 if (JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1])) {
206                         /* 2nd argument must be Callback. */
207                         LogError("JSNFCTarget::setReceiveNDEFListener errorCallback TypeMismatchException!");
208                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
209                 }
210         }
211
212         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
213         if (validator.isCallback(arguments[0])) {
214                 onSuccessForCbm = arguments[0];
215         }
216         if (argumentCount > 1) {
217                 if (validator.isCallback(arguments[1])) {
218                         onErrorForCbm = arguments[1];
219                 }
220         }
221
222         INFCTargetPtr NFCTarget(privateObject->getObject());
223         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
224         Try {
225
226                 EventTargetActionReceiveEmitterPtr emitter(new EventTargetActionReceiveEmitter);
227                 emitter->setListener(&NFCResponseDispatcher::getInstance());
228                 emitter->setEventPrivateData(StaticPointerCast<EventTargetActionReceive::PrivateDataType>(callbackManager));
229                 NFCTarget->setReceiveNDEFListener(emitter);
230                 return JSValueMakeNull(context);
231         } Catch (ConversionException) {
232                 LogError("JSNFCManager::setReceiveNDEFListener : ConversionException");
233                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
234         } Catch (InvalidArgumentException) {
235                 LogError("JSNFCManager::setReceiveNDEFListener InvalidArgumentException");
236                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
237         } Catch (PlatformException) {
238                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
239                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
240                 return JSValueMakeNull(context);
241         } Catch (WrtDeviceApis::Commons::Exception) {
242                 LogError("Exception: " << _rethrown_exception.GetMessage());
243                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
244                 return JSValueMakeNull(context);
245         }
246
247         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
248         return JSValueMakeNull(context);
249 }
250
251 JSValueRef JSNFCTarget::unsetReceiveNDEFListener(JSContextRef context,
252         JSObjectRef object,
253         JSObjectRef thisObject,
254         size_t argumentCount,
255         const JSValueRef arguments[],
256         JSValueRef* exception)
257 {
258         LogDebug("Entered ");
259         Try {
260                 if (argumentCount != 0) {
261                         LogError("JSNFCTarget::unsetReceiveNDEFListener argumentCount error");
262                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
263                 }
264
265                 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(thisObject));
266                 if (NULL == privateObject) {
267                         LogError("private object is null");
268                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
269                 }
270
271                 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_P2P_FUNCS);
272                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
273
274                 INFCTargetPtr NFCTarget(privateObject->getObject());
275                 NFCTarget->unsetReceiveNDEFListener();
276
277                 return JSValueMakeNull(context);
278         } Catch (PlatformException) {
279                 LogError("Exception: " << _rethrown_exception.GetMessage());
280                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
281         } Catch(NullPointerException) {
282                 LogError("Exception: " << _rethrown_exception.GetMessage());
283                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
284         } Catch (WrtDeviceApis::Commons::Exception) {
285                 LogError("Exception: " << _rethrown_exception.GetMessage());
286                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
287         }
288
289         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
290 }
291
292 JSValueRef JSNFCTarget::sendNDEF(JSContextRef context,
293         JSObjectRef object,
294         JSObjectRef thisObject,
295         size_t argumentCount,
296         const JSValueRef arguments[],
297         JSValueRef* exception)
298 {
299         LogDebug("Entered ");
300
301         NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(thisObject));
302         if (NULL == privateObject) {
303                 LogError("private object is null");
304                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
305         }
306
307         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_P2P_FUNCS);
308         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
309
310
311         NFCConverter convert(context);
312         Validator validator(context, exception);
313
314         if ((argumentCount < 1) || (argumentCount > 3)) {
315                 LogError("JSNFCTarget::sendNDEF argumentCount error");
316                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
317         }
318         if ((argumentCount > 1 ) && !JSValueIsNull(context, arguments[1]) && (JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1]))) {
319                 /* 1st argument is mandatory. And 1st argument must be Callback. */
320                 LogError("JSNFCTarget::sendNDEF successCallback TypeMismatchException!");
321                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
322         }
323         if ((argumentCount > 2 ) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
324                 /* 2nd argument must be Callback. */
325                 LogError("JSNFCTarget::sendNDEF errorCallbackType MismatchException!");
326                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
327         }
328
329         void *messageHandle = convert.getMessageHandle(arguments[0]);
330
331         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
332         if (validator.isCallback(arguments[1])) {
333                 onSuccessForCbm = arguments[1];
334         }
335
336         if ((argumentCount > 2 ) && (validator.isCallback(arguments[2]))) {
337                 onErrorForCbm = arguments[2];
338         }
339
340         INFCTargetPtr NFCTarget(privateObject->getObject());
341         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
342         Try {
343                 EventTargetActionSendPtr event(new EventTargetActionSend(messageHandle));
344                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
345                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
346                 NFCTarget->sendNDEF(event);
347                 return JSValueMakeNull(context);
348         } Catch (ConversionException) {
349                 LogError("JSNFCManager::sendNDEF : ConversionException");
350                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
351         } Catch (InvalidArgumentException) {
352                 LogError("JSNFCManager::sendNDEF InvalidArgumentException");
353                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
354         } Catch (PlatformException) {
355                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
356                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
357                 return JSValueMakeNull(context);
358         } Catch (WrtDeviceApis::Commons::Exception) {
359                 LogError("Exception: " << _rethrown_exception.GetMessage());
360                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
361                 return JSValueMakeNull(context);
362         }
363
364         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
365         return JSValueMakeNull(context);
366 }
367
368 }
369 }