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 "JSNdefRecordURI.h"
30 #include <CommonsJavaScript/Converter.h>
35 using namespace TizenApis::Commons;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace Api::NFC;
40 #define TIZEN10_NDEFRECORD_URI_URI "uri"
42 JSClassDefinition JSNdefRecordURI::m_classInfo =
45 kJSClassAttributeNone,
47 JSNdefRecord::getClassRef(),
55 NULL, //DeleteProperty,
56 NULL, //GetPropertyNames,
57 NULL, //CallAsFunction,
58 constructor, //CallAsConstructor,
63 JSStaticValue JSNdefRecordURI::m_property[] =
65 {TIZEN10_NDEFRECORD_URI_URI, getProperty, NULL, kJSPropertyAttributeReadOnly},
69 const JSClassRef JSNdefRecordURI::getClassRef() {
71 m_jsClassRef = JSClassCreate(&m_classInfo);
76 const JSClassDefinition* JSNdefRecordURI::getClassInfo() {
80 JSClassRef JSNdefRecordURI::m_jsClassRef = JSClassCreate(JSNdefRecordURI::getClassInfo());
82 void JSNdefRecordURI::initialize(JSContextRef context, JSObjectRef object) {
85 if (!JSObjectGetPrivate(object)) {
86 LogDebug("Private object not set... setting it.");
87 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject();
88 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
89 if (!JSObjectSetPrivate(object, priv)) {
95 void JSNdefRecordURI::finalize(JSObjectRef object) {
97 NdefRecordPrivObject* priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
98 JSObjectSetPrivate(object, NULL);
99 LogDebug("Deleting ndefrecord object");
103 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
104 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload);
106 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
109 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
112 return JSObjectMake(context, getClassRef(), priv);
115 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const std::string &uri) {
116 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(uri);
118 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
121 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
124 return JSObjectMake(context, getClassRef(), priv);
127 JSObjectRef JSNdefRecordURI::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
131 NdefRecordPrivObject* mainPriv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(constructor));
132 JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
134 if (argumentCount != 1) {
135 ThrowMsg(InvalidArgumentException, "parameter count is wrong.");
136 } else if (JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !JSValueIsString(ctx, arguments[0])) {
137 ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not array");
140 Converter convert(ctx);
142 return createJSObject(global_context, convert.toString(arguments[0]));
143 } Catch(ConversionException) {
144 LogError("ConversionException: " << _rethrown_exception.GetMessage());
145 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
147 } Catch (InvalidArgumentException) {
148 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
149 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
151 } Catch (PlatformException) {
152 LogError("PlatformException: " << _rethrown_exception.GetMessage());
153 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
155 } Catch (WrtDeviceApis::Commons::Exception) {
156 LogError("Exception: " << _rethrown_exception.GetMessage());
157 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
160 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
164 JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object,
165 JSStringRef propertyName, JSValueRef* exception) {
169 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_URI_URI)) {
170 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
172 ThrowMsg(NullPointerException, "Private object not set.");
174 INdefRecordPtr NdefRecord = priv->getObject();
176 if (NdefRecord->getUri(&uri)) {
177 Converter convert(context);
178 std::string result(uri);
180 LogDebug("uri : " << result);
181 return convert.toJSValueRef(result);
183 LogDebug("This record is not URI Type");
185 } Catch (ConversionException) {
186 LogError("ConversionException: " << _rethrown_exception.GetMessage());
187 } Catch (PlatformException) {
188 LogError("PlatformException: " << _rethrown_exception.GetMessage());
189 } Catch (NullPointerException) {
190 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
191 } Catch (WrtDeviceApis::Commons::Exception) {
192 LogError("Exception: " << _rethrown_exception.GetMessage());
194 return JSValueMakeUndefined(context);
197 bool JSNdefRecordURI::hasInstance(JSContextRef context,
198 JSObjectRef constructor,
199 JSValueRef possibleInstance,
200 JSValueRef* exception)
202 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());