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 "JSNFCTarget.h"
19 #include "JSNFCManager.h"
20 #include "NFCConverter.h"
21 #include "ResponseDispatcher.h"
22 #include <dpl/log/log.h>
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>
36 #include "plugin_config.h"
39 using namespace TizenApis::Api::NFC;
40 using namespace TizenApis::Commons;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
45 #define TIZEN10_NFCTARGET_ATTRIBUTENAME "NFCTarget"
47 #define TIZEN10_NFCTARGET_ISCONNECTED "isConnected"
52 JSClassDefinition JSNFCTarget::m_classInfo =
55 kJSClassAttributeNone,
56 TIZEN10_NFCTARGET_ATTRIBUTENAME,
65 NULL, //DeleteProperty,
66 NULL, //GetPropertyNames,
67 NULL, //CallAsFunction,
68 NULL, //CallAsConstructor,
69 hasInstance, //HasInstance,
73 JSStaticValue JSNFCTarget::m_property[] =
76 { TIZEN10_NFCTARGET_ISCONNECTED, getProperty,
77 NULL, kJSPropertyAttributeReadOnly},
81 JSStaticFunction JSNFCTarget::m_function[] = {
82 {"setReceiveNDEFListener", JSNFCTarget::setReceiveNDEFListener, kJSPropertyAttributeNone},
83 {"unsetReceiveNDEFListener", JSNFCTarget::unsetReceiveNDEFListener, kJSPropertyAttributeNone},
84 {"sendNDEF", JSNFCTarget::sendNDEF, kJSPropertyAttributeNone},
88 JSClassRef JSNFCTarget::m_jsClassRef = JSClassCreate(JSNFCTarget::getClassInfo());
90 JSObjectRef JSNFCTarget::createJSObject(JSContextRef context, void *tagHandle, void* privManager) {
93 INFCTargetPtr NFCTarget = NFCFactory::getInstance().createNFCTargetObject(tagHandle);
94 NFCTarget->setPrivateNFCManagerPtr(privManager);
96 NFCTargetPrivObject *priv = new NFCTargetPrivObject(context, NFCTarget);
99 ThrowMsg(NullPointerException, "Can not new a NFCTarget object");
102 return JSObjectMake(context, getClassRef(), priv);
105 void JSNFCTarget::initialize(JSContextRef context, JSObjectRef object)
109 void JSNFCTarget::finalize(JSObjectRef object)
111 LogDebug( "entered" );
112 NFCTargetPrivObject *priv = static_cast<NFCTargetPrivObject*>( JSObjectGetPrivate( object ) ) ;
113 JSObjectSetPrivate(object, NULL);
114 LogDebug("Deleting timezone object");
119 const JSClassRef JSNFCTarget::getClassRef()
122 m_jsClassRef = JSClassCreate(&m_classInfo);
127 const JSClassDefinition* JSNFCTarget::getClassInfo()
132 JSValueRef JSNFCTarget::getProperty(JSContextRef context, JSObjectRef object,
133 JSStringRef propertyName, JSValueRef* exception)
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");
144 INFCTargetPtr NFCTarget(privateObject->getObject());
145 NFCConverter convert(context);
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");
153 INFCManagerPtr NFCManager(privateNFCManager->getObject());
154 return convert.toJSValueRef(NFCManager->isValidHandle(NFCTarget->getHandle()));
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());
163 return JSValueMakeUndefined(context);
166 bool JSNFCTarget::hasInstance(JSContextRef context,
167 JSObjectRef constructor,
168 JSValueRef possibleInstance,
169 JSValueRef* exception)
171 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
174 JSValueRef JSNFCTarget::setReceiveNDEFListener(JSContextRef context,
176 JSObjectRef thisObject,
177 size_t argumentCount,
178 const JSValueRef arguments[],
179 JSValueRef* exception)
181 LogDebug("Entered ");
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");
189 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_P2P_FUNCS);
190 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
192 NFCConverter convert(context);
193 Validator validator(context, exception);
195 if ((argumentCount < 1) || (argumentCount > 2)) {
196 LogError("JSNFCTarget::setReceiveNDEFListener argumentCount error");
197 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
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");
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");
212 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
213 if (validator.isCallback(arguments[0])) {
214 onSuccessForCbm = arguments[0];
216 if (argumentCount > 1) {
217 if (validator.isCallback(arguments[1])) {
218 onErrorForCbm = arguments[1];
222 INFCTargetPtr NFCTarget(privateObject->getObject());
223 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
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);
247 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
248 return JSValueMakeNull(context);
251 JSValueRef JSNFCTarget::unsetReceiveNDEFListener(JSContextRef context,
253 JSObjectRef thisObject,
254 size_t argumentCount,
255 const JSValueRef arguments[],
256 JSValueRef* exception)
258 LogDebug("Entered ");
260 if (argumentCount != 0) {
261 LogError("JSNFCTarget::unsetReceiveNDEFListener argumentCount error");
262 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
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");
271 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_P2P_FUNCS);
272 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
274 INFCTargetPtr NFCTarget(privateObject->getObject());
275 NFCTarget->unsetReceiveNDEFListener();
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");
289 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
292 JSValueRef JSNFCTarget::sendNDEF(JSContextRef context,
294 JSObjectRef thisObject,
295 size_t argumentCount,
296 const JSValueRef arguments[],
297 JSValueRef* exception)
299 LogDebug("Entered ");
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");
307 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_P2P_FUNCS);
308 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
311 NFCConverter convert(context);
312 Validator validator(context, exception);
314 if ((argumentCount < 1) || (argumentCount > 3)) {
315 LogError("JSNFCTarget::sendNDEF argumentCount error");
316 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
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");
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");
329 void *messageHandle = convert.getMessageHandle(arguments[0]);
331 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
332 if (validator.isCallback(arguments[1])) {
333 onSuccessForCbm = arguments[1];
336 if ((argumentCount > 2 ) && (validator.isCallback(arguments[2]))) {
337 onErrorForCbm = arguments[2];
340 INFCTargetPtr NFCTarget(privateObject->getObject());
341 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
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);
364 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
365 return JSValueMakeNull(context);