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.
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include <Tizen/Common/SecurityExceptions.h>
27 #include <API/NFC/NFCFactory.h>
29 #include "JSNdefRecord.h"
30 #include "plugin_config.h"
31 #include "NFCConverter.h"
36 using namespace TizenApis::Commons;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace Api::NFC;
41 #define TIZEN10_NDEFRECORD_PROPERTIES "properties"
43 #define TIZEN10_NDEFRECORD_TEXT_TEXT "text"
44 #define TIZEN10_NDEFRECORD_TEXT_LANGCODE "langCode"
45 #define TIZEN10_NDEFRECORD_TEXT_ENCODETYPE "encodeType"
47 #define TIZEN10_NDEFRECORD_URI_URI "uri"
49 #define TIZEN10_NDEFRECORD_MEDIA_MIMETYPE "mimeType"
51 JSClassDefinition JSNdefRecord::m_classInfo =
54 kJSClassAttributeNone,
64 NULL, //DeleteProperty,
65 NULL, //GetPropertyNames,
66 NULL, //CallAsFunction,
67 NULL, //CallAsConstructor,
72 JSStaticValue JSNdefRecord::m_property[] =
74 {TIZEN10_NDEFRECORD_PROPERTIES, getProperty, NULL, kJSPropertyAttributeReadOnly},
75 {TIZEN10_NDEFRECORD_TEXT_TEXT, getProperty, NULL, kJSPropertyAttributeReadOnly},
76 {TIZEN10_NDEFRECORD_TEXT_LANGCODE, getProperty, NULL, kJSPropertyAttributeReadOnly},
77 {TIZEN10_NDEFRECORD_TEXT_ENCODETYPE, getProperty, NULL, kJSPropertyAttributeReadOnly},
78 {TIZEN10_NDEFRECORD_URI_URI, getProperty, NULL, kJSPropertyAttributeReadOnly},
79 {TIZEN10_NDEFRECORD_MEDIA_MIMETYPE, getProperty, NULL, kJSPropertyAttributeReadOnly},
83 JSStaticFunction JSNdefRecord::m_function[] = {
84 {"getPayload", JSNdefRecord::getPayload, kJSPropertyAttributeNone},
88 const JSClassRef JSNdefRecord::getClassRef() {
90 m_jsClassRef = JSClassCreate(&m_classInfo);
95 const JSClassDefinition* JSNdefRecord::getClassInfo() {
99 JSClassRef JSNdefRecord::m_jsClassRef = JSClassCreate(JSNdefRecord::getClassInfo());
101 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordData &ndefRecordData) {
103 return createJSObject(context, ndefRecordData.properties, ndefRecordData.payload);
106 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
107 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload);
109 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
112 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
115 return JSObjectMake(context, getClassRef(), priv);
118 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const std::string &text, const std::string &langCode, const short encodeType) {
119 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(text, langCode, encodeType);
121 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
124 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
127 return JSObjectMake(context, getClassRef(), priv);
130 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const std::string &uri) {
131 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(uri);
133 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
136 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
139 return JSObjectMake(context, getClassRef(), priv);
142 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const std::string &mimeType, std::vector<unsigned char> data) {
143 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(mimeType, data);
145 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
148 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
151 return JSObjectMake(context, getClassRef(), priv);
154 void JSNdefRecord::initialize(JSContextRef context, JSObjectRef object) {
155 LogDebug("Entered. Nothing to do.");
158 void JSNdefRecord::finalize(JSObjectRef object) {
160 NdefRecordPrivObject* priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
161 JSObjectSetPrivate(object, NULL);
162 LogDebug("Deleting ndefrecord object");
166 JSValueRef JSNdefRecord::getProperty(JSContextRef context, JSObjectRef object,
167 JSStringRef propertyName, JSValueRef* exception) {
171 NFCConverter convert(context);
172 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_PROPERTIES)) {
173 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
175 ThrowMsg(NullPointerException, "Private object not set.");
177 INdefRecordPtr NdefRecord = priv->getObject();
178 NdefRecordProperties props = NdefRecord->getNDEFRecordProperties();
180 return convert.toJSValueRef(props);
181 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TEXT_TEXT)) {
182 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
184 ThrowMsg(NullPointerException, "Private object not set.");
186 INdefRecordPtr NdefRecord = priv->getObject();
188 if (NdefRecord->getText(&text)) {
189 std::string result(text);
191 LogDebug("text : " << result);
192 return convert.toJSValueRef(result);
194 LogDebug("This record is not Text Type");
195 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TEXT_LANGCODE)) {
196 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
198 ThrowMsg(NullPointerException, "Private object not set.");
200 INdefRecordPtr NdefRecord = priv->getObject();
201 char * landCode = NULL;
202 if (NdefRecord->getLangCode(&landCode)) {
203 std::string result(landCode);
205 LogDebug("landCode : " << result);
206 return convert.toJSValueRef(result);
208 LogDebug("This record is not Text Type");
209 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TEXT_ENCODETYPE)) {
210 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
212 ThrowMsg(NullPointerException, "Private object not set.");
214 INdefRecordPtr NdefRecord = priv->getObject();
215 nfcTextEncodeUTF encodeType = NFC_TEXT_ENCODE_UTF_16;
216 if (NdefRecord->getEncodeType(&encodeType)) {
217 LogDebug("encodeType : " << encodeType);
218 return convert.toJSValueRef(convert.toNfcTextEncodeUTFString(encodeType));
220 LogDebug("This record is not Text Type");
221 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_URI_URI)) {
222 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
224 ThrowMsg(NullPointerException, "Private object not set.");
226 INdefRecordPtr NdefRecord = priv->getObject();
228 if (NdefRecord->getUri(&uri)) {
229 std::string result(uri);
231 LogDebug("uri : " << result);
232 return convert.toJSValueRef(result);
234 LogDebug("This record is not URI Type");
235 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_MEDIA_MIMETYPE)) {
236 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
238 ThrowMsg(NullPointerException, "Private object not set.");
240 INdefRecordPtr NdefRecord = priv->getObject();
241 char * mimeType = NULL;
242 if (NdefRecord->getMimeType(&mimeType)) {
243 std::string result(mimeType);
245 LogDebug("mimeType : " << result);
246 return convert.toJSValueRef(result);
248 LogDebug("This record is not MEDIA Type");
251 } Catch (ConversionException) {
252 LogError("ConversionException: " << _rethrown_exception.GetMessage());
253 } Catch (NullPointerException) {
254 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
255 } Catch (WrtDeviceApis::Commons::Exception) {
256 LogError("Exception: " << _rethrown_exception.GetMessage());
258 return JSValueMakeUndefined(context);
262 JSValueRef JSNdefRecord::getPayload(JSContextRef context,
264 JSObjectRef thisObject,
265 size_t argumentCount,
266 const JSValueRef arguments[],
267 JSValueRef* exception)
269 LogDebug("Entered ");
271 NdefRecordPrivObject* privateObject = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(thisObject));
272 if (NULL == privateObject) {
273 LogError("private object is null");
274 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
277 AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_P2P_FUNCS);
278 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
280 INdefRecordPtr NdefRecord(privateObject->getObject());
281 NFCConverter convert(context);
283 return convert.toJSValueRef(NdefRecord->getPayload());
284 } Catch (PlatformException) {
285 LogError("Exception: " << _rethrown_exception.GetMessage());
286 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
287 } Catch(NullPointerException) {
288 LogError("Exception: " << _rethrown_exception.GetMessage());
289 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
290 } Catch (WrtDeviceApis::Commons::Exception) {
291 LogError("Exception: " << _rethrown_exception.GetMessage());
292 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
295 return JSValueMakeNull(context);