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 "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"
26 #include <dpl/log/log.h>
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"
37 using namespace TizenApis::Api::NFC;
38 using namespace TizenApis::Commons;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
42 #define TIZEN10_NDEFMESSAGE_ATTRIBUTENAME "NDEFMessage"
43 #define TIZEN10_NDEFMESSAGE_RECORDCOUNT "recordCount"
44 #define TIZEN10_NDEFMESSAGE_RECORDS "records"
49 JSClassDefinition JSNdefMessage::m_classInfo =
52 kJSClassAttributeNone,
53 TIZEN10_NDEFMESSAGE_ATTRIBUTENAME,
61 setProperty, //setProperty,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 constructor, //CallAsConstructor,
66 hasInstance, //HasInstance,
70 JSStaticValue JSNdefMessage::m_property[] =
72 //NdefMessageProperties
73 { TIZEN10_NDEFMESSAGE_RECORDCOUNT, getProperty,
74 NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN10_NDEFMESSAGE_RECORDS, getProperty,
76 setProperty, kJSPropertyAttributeNone},
80 JSStaticFunction JSNdefMessage::m_function[] = {
81 {"toByte", JSNdefMessage::toByte, kJSPropertyAttributeNone},
85 JSClassRef JSNdefMessage::m_jsClassRef = JSClassCreate(JSNdefMessage::getClassInfo());
87 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, void *messageHandle) {
90 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(messageHandle);
92 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
95 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
98 return JSObjectMake(context, getClassRef(), priv);
101 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context) {
104 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject();
106 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
109 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
112 return JSObjectMake(context, getClassRef(), priv);
115 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, INdefMessagePtr message) {
118 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, message);
121 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
124 return JSObjectMake(context, getClassRef(), priv);
127 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<void *> ndefRcords) {
130 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(ndefRcords);
132 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
135 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
138 return JSObjectMake(context, getClassRef(), priv);
141 JSObjectRef JSNdefMessage::createJSObject(JSContextRef context, std::vector<unsigned char> rawdata) {
144 INdefMessagePtr NdefMessage = NFCFactory::getInstance().createNDEFMessageObject(rawdata);
146 NdefMessagePrivObject *priv = new NdefMessagePrivObject(context, NdefMessage);
149 ThrowMsg(NullPointerException, "Can not new a NdefMessage object");
152 return JSObjectMake(context, getClassRef(), priv);
154 void JSNdefMessage::initialize(JSContextRef context, JSObjectRef object)
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)) {
168 void JSNdefMessage::finalize(JSObjectRef object)
170 LogDebug( "entered" );
171 NdefMessagePrivObject *priv = static_cast<NdefMessagePrivObject*>( JSObjectGetPrivate( object ) ) ;
172 JSObjectSetPrivate(object, NULL);
173 LogDebug("Deleting NdefMessage object");
178 const JSClassRef JSNdefMessage::getClassRef()
181 m_jsClassRef = JSClassCreate(&m_classInfo);
186 const JSClassDefinition* JSNdefMessage::getClassInfo()
191 JSObjectRef JSNdefMessage::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
195 NdefMessagePrivObject* mainPriv = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(constructor));
196 JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
198 if (argumentCount == 0)
199 return createJSObject(global_context);
200 else if (argumentCount == 1) {
201 if (JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !(JSIsArrayValue(ctx, arguments[0]) || JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])))
202 ThrowMsg(ConversionException, "Parameter's type is not matched");
204 NFCConverter convert(ctx);
206 if (JSNdefRecordArray::isObjectOfClass(ctx, arguments[0])) {
207 return createJSObject(global_context, JSNdefRecordArray::getNdefRecordArray(ctx, arguments[0]));
210 JSObjectRef objArg = convert.toJSObjectRef(arguments[0]);
211 if (JSGetArrayLength(ctx, objArg) > 0) {
213 for (std::size_t i = 0; i < JSGetArrayLength(ctx, objArg); ++i) {
214 element = JSGetArrayElement(ctx, objArg, i);
215 if (!JSValueIsNull(ctx, element))
218 if (JSValueIsNull(ctx, element))
219 return createJSObject(global_context);
220 else if (convert.isNdefRecord(element))
221 return createJSObject(global_context, convert.toVectorOfRecordHandles(arguments[0]));
223 return createJSObject(global_context, convert.toVectorOfUChars(arguments[0]));
225 return createJSObject(global_context);
228 ThrowMsg(InvalidArgumentException, "ArgumentCount is not correct");
230 } Catch(ConversionException) {
231 LogError("ConversionException: " << _rethrown_exception.GetMessage());
232 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
234 } Catch (InvalidArgumentException) {
235 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
236 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
238 } Catch (PlatformException) {
239 LogError("PlatformException: " << _rethrown_exception.GetMessage());
240 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
242 } Catch (WrtDeviceApis::Commons::Exception) {
243 LogError("Exception: " << _rethrown_exception.GetMessage());
244 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
247 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
251 JSValueRef JSNdefMessage::getProperty(JSContextRef context, JSObjectRef object,
252 JSStringRef propertyName, JSValueRef* exception)
257 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
258 if (!privateObject) {
259 LogError("Private object is not set.");
260 ThrowMsg(NullPointerException, "Private object not initialized");
263 INdefMessagePtr NdefMessage(privateObject->getObject());
264 NFCConverter convert(context);
266 LogDebug("propertyName : " << convert.toString(propertyName));
267 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDCOUNT)) {
268 return convert.toJSValueRefLong(NdefMessage->getRecordCount());
269 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDS)) {
270 return JSNdefRecordArray::createArray(privateObject->getContext(), NdefMessage);
272 } Catch (ConversionException) {
273 LogError("ConversionException: " << _rethrown_exception.GetMessage());
274 } Catch (NullPointerException) {
275 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
276 } Catch (WrtDeviceApis::Commons::Exception) {
277 LogError("Exception: " << _rethrown_exception.GetMessage());
279 return JSValueMakeUndefined(context);
282 bool JSNdefMessage::setProperty(JSContextRef context, JSObjectRef object,
283 JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
288 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFMESSAGE_RECORDS)) {
289 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(object));
290 if (!privateObject) {
291 LogError("Private object is not set.");
292 ThrowMsg(NullPointerException, "Private object not initialized");
295 INdefMessagePtr NdefMessage(privateObject->getObject());
296 NFCConverter convert(context);
298 if (JSValueIsNull(context, value) || JSValueIsUndefined(context, value) || !(JSIsArrayValue(context, value) || JSNdefRecordArray::isObjectOfClass(context, value))) {
299 LogError("value is invald.");
300 ThrowMsg(ConversionException, "value is invald.");
303 NdefMessagePrivObject *priv;
304 if (JSNdefRecordArray::isObjectOfClass(context, value))
305 priv = new NdefMessagePrivObject(context, JSNdefRecordArray::getNdefRecordArray(context, value));
307 std::vector<void *> records = convert.toVectorOfRecordHandles(value);
308 priv = new NdefMessagePrivObject(context, NFCFactory::getInstance().createNDEFMessageObject(records));
310 if (!JSObjectSetPrivate(object, priv)) {
312 ThrowMsg(NullPointerException, "Private object not set");
314 delete privateObject;
317 } Catch (NullPointerException) {
318 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
319 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
320 } Catch (ConversionException) {
321 LogError("ConversionException: " << _rethrown_exception.GetMessage());
322 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
323 } Catch (InvalidArgumentException) {
324 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
325 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
326 } Catch (WrtDeviceApis::Commons::Exception) {
327 LogError("Exception: " << _rethrown_exception.GetMessage());
328 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
334 bool JSNdefMessage::hasInstance(JSContextRef context,
335 JSObjectRef constructor,
336 JSValueRef possibleInstance,
337 JSValueRef* exception)
339 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
342 JSValueRef JSNdefMessage::toByte(JSContextRef context,
344 JSObjectRef thisObject,
345 size_t argumentCount,
346 const JSValueRef arguments[],
347 JSValueRef* exception)
349 LogDebug("Entered ");
351 if (argumentCount != 0) {
352 LogError("JSNdefMessage::toByte TypeMismatchException");
353 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
356 NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(thisObject));
357 if (NULL == privateObject) {
358 LogError("private object is null");
359 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
362 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
363 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
365 INdefMessagePtr NdefMessage(privateObject->getObject());
366 NFCConverter convert(context);
368 return convert.toJSValueRef(NdefMessage->toByte());
369 } Catch (PlatformException) {
370 LogError("Exception: " << _rethrown_exception.GetMessage());
371 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
372 } Catch(NullPointerException) {
373 LogError("Exception: " << _rethrown_exception.GetMessage());
374 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
375 } Catch (WrtDeviceApis::Commons::Exception) {
376 LogError("Exception: " << _rethrown_exception.GetMessage());
377 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
380 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");