2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <CommonsJavaScript/Validator.h>
19 #include <Commons/Exception.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <JSWebAPIErrorFactory.h>
25 #include <SecurityExceptions.h>
26 #include <ArgumentValidator.h>
27 #include <GlobalContextManager.h>
28 #include <PlatformException.h>
30 #include "JSNFCTarget.h"
31 #include "JSNFCManager.h"
32 #include "JSNdefMessage.h"
33 #include "NFCConverter.h"
34 #include "ResponseDispatcher.h"
35 #include "NFCAsyncCallbackManager.h"
36 #include "NFCListenerManager.h"
37 #include "NFCFactory.h"
38 #include "EventTargetAction.h"
39 #include "plugin_config.h"
43 using namespace DeviceAPI::Common;
44 using namespace WrtDeviceApis::Commons;
45 using namespace WrtDeviceApis::CommonsJavaScript;
48 #define TIZEN_NFCTARGET_ATTRIBUTENAME "NFCTarget"
50 #define TIZEN_NFCTARGET_ISCONNECTED "isConnected"
55 JSClassDefinition JSNFCTarget::m_classInfo =
58 kJSClassAttributeNone,
59 TIZEN_NFCTARGET_ATTRIBUTENAME,
68 NULL, //DeleteProperty,
69 NULL, //GetPropertyNames,
70 NULL, //CallAsFunction,
71 NULL, //CallAsConstructor,
72 hasInstance, //HasInstance,
76 JSStaticValue JSNFCTarget::m_property[] =
79 { TIZEN_NFCTARGET_ISCONNECTED, getProperty,
80 NULL, kJSPropertyAttributeReadOnly},
84 JSStaticFunction JSNFCTarget::m_function[] = {
85 {"setReceiveNDEFListener", JSNFCTarget::setReceiveNDEFListener, kJSPropertyAttributeNone},
86 {"unsetReceiveNDEFListener", JSNFCTarget::unsetReceiveNDEFListener, kJSPropertyAttributeNone},
87 {"sendNDEF", JSNFCTarget::sendNDEF, kJSPropertyAttributeNone},
91 JSClassRef JSNFCTarget::m_jsClassRef = JSClassCreate(JSNFCTarget::getClassInfo());
93 JSObjectRef JSNFCTarget::createJSObject(JSContextRef context, void *tagHandle) {
96 INFCTargetPtr nfcTarget = NFCFactory::getInstance().createNFCTargetObject(tagHandle);
98 NFCTargetPrivObject *priv = new NFCTargetPrivObject(context, nfcTarget);
101 ThrowMsg(NullPointerException, "Can not new a NFCTarget object");
104 return JSObjectMake(context, getClassRef(), priv);
107 void JSNFCTarget::initialize(JSContextRef context, JSObjectRef object)
111 void JSNFCTarget::finalize(JSObjectRef object)
113 LoggerD( "entered" );
114 NFCTargetPrivObject *priv = static_cast<NFCTargetPrivObject*>( JSObjectGetPrivate( object ) ) ;
115 JSObjectSetPrivate(object, NULL);
116 LoggerD("Deleting timezone object");
121 const JSClassRef JSNFCTarget::getClassRef()
124 m_jsClassRef = JSClassCreate(&m_classInfo);
129 const JSClassDefinition* JSNFCTarget::getClassInfo()
134 JSValueRef JSNFCTarget::getProperty(JSContextRef context, JSObjectRef object,
135 JSStringRef propertyName, JSValueRef* exception)
140 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCTARGET_ISCONNECTED)) {
141 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(object));
142 if (!privateObject) {
143 LoggerE("Private object is not set.");
144 ThrowMsg(NullPointerException, "Private object not initialized");
147 INFCTargetPtr nfcTarget(privateObject->getObject());
148 NFCConverter convert(context);
149 return convert.toJSValueRef(nfcTarget->isConnected());
151 } Catch (ConversionException) {
152 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
153 } Catch (NullPointerException) {
154 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
155 } Catch (WrtDeviceApis::Commons::UnknownException) {
156 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
157 } Catch (PlatformException) {
158 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
159 } Catch (WrtDeviceApis::Commons::Exception) {
160 LoggerE("Exception: " << _rethrown_exception.GetMessage());
162 return JSValueMakeUndefined(context);
165 bool JSNFCTarget::hasInstance(JSContextRef context,
166 JSObjectRef constructor,
167 JSValueRef possibleInstance,
168 JSValueRef* exception)
170 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
173 JSValueRef JSNFCTarget::setReceiveNDEFListener(JSContextRef context,
175 JSObjectRef thisObject,
176 size_t argumentCount,
177 const JSValueRef arguments[],
178 JSValueRef* exception)
182 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
183 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
185 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
187 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(thisObject));
188 if (NULL == privateObject) {
189 LoggerE("private object is null");
190 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
193 INFCTargetPtr nfcTarget(privateObject->getObject());
196 ArgumentValidator validator(context, argumentCount, arguments);
197 JSValueRef onSuccessForCbm = NULL;
198 // NDEFMessageReadCallback
199 if (validator.toFunction(0))
200 onSuccessForCbm = arguments[0];
202 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, NULL, true, true);
204 EventTargetActionReceiveEmitterPtr emitter(new EventTargetActionReceiveEmitter);
205 emitter->setListener(&NFCResponseDispatcher::getInstance());
206 emitter->setEventPrivateData(DPL::StaticPointerCast<EventTargetActionReceive::PrivateDataType>(callbackManager));
207 nfcTarget->setReceiveNDEFListener(emitter);
209 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCPEER_RECEIVENDEF_LISTENER)));
210 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
211 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
213 return JSValueMakeUndefined(context);
214 } Catch (BasePlatformException) {
215 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
216 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
217 } Catch (ConversionException) {
218 LoggerE("setReceiveNDEFListener : ConversionException");
219 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
220 } Catch (PlatformException) {
221 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
222 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
223 } Catch (WrtDeviceApis::Commons::UnknownException) {
224 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
225 } Catch (WrtDeviceApis::Commons::Exception) {
226 LoggerE("Exception: " << _rethrown_exception.GetMessage());
229 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
232 JSValueRef JSNFCTarget::unsetReceiveNDEFListener(JSContextRef context,
234 JSObjectRef thisObject,
235 size_t argumentCount,
236 const JSValueRef arguments[],
237 JSValueRef* exception)
241 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
242 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
244 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
246 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(thisObject));
247 if (NULL == privateObject) {
248 LoggerE("private object is null");
249 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
252 INFCTargetPtr nfcTarget(privateObject->getObject());
253 nfcTarget->unsetReceiveNDEFListener();
255 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCPEER_RECEIVENDEF_LISTENER)));
256 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
257 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
259 return JSValueMakeUndefined(context);
260 } Catch (WrtDeviceApis::Commons::UnknownException) {
261 LoggerE("Exception: " << _rethrown_exception.GetMessage());
262 } Catch(NullPointerException) {
263 LoggerE("Exception: " << _rethrown_exception.GetMessage());
264 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
265 } Catch (PlatformException) {
266 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
267 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
268 } Catch (WrtDeviceApis::Commons::Exception) {
269 LoggerE("Exception: " << _rethrown_exception.GetMessage());
272 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
275 JSValueRef JSNFCTarget::sendNDEF(JSContextRef context,
277 JSObjectRef thisObject,
278 size_t argumentCount,
279 const JSValueRef arguments[],
280 JSValueRef* exception)
284 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
285 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
287 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(context);
289 NFCConverter convert(context);
291 NFCTargetPrivObject* privateObject = static_cast<NFCTargetPrivObject*>(JSObjectGetPrivate(thisObject));
292 if (NULL == privateObject) {
293 LoggerE("private object is null");
294 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
297 INFCTargetPtr nfcTarget(privateObject->getObject());
298 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, NULL, NULL, true, true);
300 ArgumentValidator validator(context, argumentCount, arguments);
303 JSObjectRef ndefMessageObj = validator.toObject(0, JSNdefMessage::getClassRef());
305 if (validator.toFunction(1, true))
306 callbackManager->setOnSuccess(arguments[1]);
308 if (validator.toFunction(2, true))
309 callbackManager->setOnError(arguments[2]);
311 void *messageHandle = convert.copiedMessage(ndefMessageObj);
312 EventTargetActionSendPtr event(new EventTargetActionSend(messageHandle));
313 event->setPrivateData( DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
314 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
315 nfcTarget->sendNDEF(event);
316 NFCAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, global_context);
317 return JSValueMakeUndefined(context);
318 } Catch (BasePlatformException) {
319 LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
320 return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
321 } Catch (ConversionException) {
322 LoggerE("sendNDEF : ConversionException");
323 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
324 } Catch (InvalidArgumentException) {
325 LoggerE("sendNDEF InvalidArgumentException");
326 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values"));
327 return JSValueMakeUndefined(context);
328 } Catch (PlatformException) {
329 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
330 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available"));
331 return JSValueMakeUndefined(context);
332 } Catch (WrtDeviceApis::Commons::UnknownException) {
333 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
334 } Catch (WrtDeviceApis::Commons::Exception) {
335 LoggerE("Exception: " << _rethrown_exception.GetMessage());
338 callbackManager->callOnError(JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR,"Unknown Error"));
339 return JSValueMakeUndefined(context);