Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNdefRecord.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <memory>
19 #include <string>
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/JSUtils.h>
22
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include <Tizen/Common/SecurityExceptions.h>
26
27 #include <API/NFC/NFCFactory.h>
28
29 #include "JSNdefRecord.h"
30 #include "plugin_config.h"
31 #include "NFCConverter.h"
32
33 namespace TizenApis {
34 namespace Tizen1_0 {
35
36 using namespace TizenApis::Commons;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace Api::NFC;
40
41 #define TIZEN10_NDEFRECORD_TNF "tnf"
42 #define TIZEN10_NDEFRECORD_TYPENAME "typeName"
43 #define TIZEN10_NDEFRECORD_ID "id"
44 #define TIZEN10_NDEFRECORD_PAYLOAD "payload"
45
46 JSClassDefinition JSNdefRecord::m_classInfo =
47 {
48         0,
49         kJSClassAttributeNone,
50         "NDEFRecord",
51         0,
52         m_property,
53         NULL,
54         initialize,
55         finalize,
56         NULL, //HasProperty,
57         NULL,
58         NULL, //SetProperty,
59         NULL, //DeleteProperty,
60         NULL, //GetPropertyNames,
61         NULL, //CallAsFunction,
62         constructor, //CallAsConstructor,
63         hasInstance,
64         NULL, //ConvertToType
65 };
66
67 JSStaticValue JSNdefRecord::m_property[] =
68 {
69         {TIZEN10_NDEFRECORD_TNF,  getProperty, NULL, kJSPropertyAttributeReadOnly},
70         {TIZEN10_NDEFRECORD_TYPENAME,  getProperty, NULL, kJSPropertyAttributeReadOnly},
71         {TIZEN10_NDEFRECORD_ID,  getProperty, NULL, kJSPropertyAttributeReadOnly},
72         {TIZEN10_NDEFRECORD_PAYLOAD,  getProperty, NULL, kJSPropertyAttributeReadOnly},
73         { 0, 0, 0, 0 }
74 };
75
76 const JSClassRef JSNdefRecord::getClassRef() {
77         if (!m_jsClassRef) {
78                 m_jsClassRef = JSClassCreate(&m_classInfo);
79         }
80         return m_jsClassRef;
81 }
82
83 const JSClassDefinition* JSNdefRecord::getClassInfo() {
84         return &m_classInfo;
85 }
86
87 JSClassRef JSNdefRecord::m_jsClassRef = JSClassCreate(JSNdefRecord::getClassInfo());
88
89 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordData &ndefRecordData) {
90         LogDebug("Entered");
91         return createJSObject(context, ndefRecordData.properties, ndefRecordData.payload);
92 }
93
94 JSObjectRef JSNdefRecord::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
95         INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload);
96
97         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
98
99         if (!priv) {
100                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
101         }
102
103         return JSObjectMake(context, getClassRef(), priv);
104 }
105
106 void JSNdefRecord::initialize(JSContextRef context, JSObjectRef object) {
107         LogDebug("entered");
108
109         if (!JSObjectGetPrivate(object)) {
110                 LogDebug("Private object not set... setting it.");
111                 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject();
112                 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
113                 if (!JSObjectSetPrivate(object, priv)) {
114                         delete priv;
115                 }
116         }
117 }
118
119 void JSNdefRecord::finalize(JSObjectRef object) {
120         LogDebug("Entered");
121         NdefRecordPrivObject* priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
122         JSObjectSetPrivate(object, NULL);
123         LogDebug("Deleting ndefrecord object");
124         delete priv;
125 }
126
127 JSObjectRef JSNdefRecord::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
128 {
129         LogDebug("entered");
130
131         NdefRecordPrivObject* mainPriv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(constructor));
132         JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
133         Try {
134                 if ((argumentCount < 3) || (argumentCount > 4)) {
135                         ThrowMsg(InvalidArgumentException, "parameter count is wrong.");
136                 } else if ((JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !JSValueIsNumber(ctx, arguments[0]))
137                         || (JSValueIsNull(ctx, arguments[1]) || JSValueIsUndefined(ctx, arguments[1]) || !JSIsArrayValue(ctx, arguments[1]))
138                         || (JSValueIsNull(ctx, arguments[2]) || JSValueIsUndefined(ctx, arguments[2]) || !JSIsArrayValue(ctx, arguments[2]))) {
139                         ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not array");
140                 }
141                 NFCConverter convert(ctx);
142
143                 NdefRecordProperties prop;
144                 prop.tnf = static_cast<nfcTNF>(convert.toLong(arguments[0]));
145
146                 prop.typeName = convert.toVectorOfUChars(arguments[1]);
147                 if ((argumentCount == 4) && !JSValueIsNull(ctx, arguments[3])) {
148                         if ( JSValueIsUndefined(ctx, arguments[3]) || !JSIsArrayValue(ctx, arguments[3]))
149                                 ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not array");
150                         prop.id = convert.toVectorOfUChars(arguments[3]);
151                 }
152
153                 return createJSObject(global_context, prop, convert.toVectorOfUChars(arguments[2]));
154
155         } Catch(ConversionException) {
156                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
157                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
158                 return NULL;
159         } Catch (InvalidArgumentException) {
160                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
161                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
162                 return NULL;
163         } Catch (PlatformException) {
164                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
165                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
166                 return NULL;
167         } Catch (WrtDeviceApis::Commons::Exception) {
168                 LogError("Exception: " << _rethrown_exception.GetMessage());
169                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
170                 return NULL;
171         }
172         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
173         return NULL;
174 }
175
176 JSValueRef JSNdefRecord::getProperty(JSContextRef context, JSObjectRef object,
177         JSStringRef propertyName, JSValueRef* exception) {
178         LogDebug("Enter");
179
180         Try     {
181                 NFCConverter convert(context);
182                 NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
183                 if (!priv) {
184                         ThrowMsg(NullPointerException, "Private object not set.");
185                 }
186                 INdefRecordPtr NdefRecord = priv->getObject();
187
188                 LogDebug("propertyName : " << convert.toString(propertyName));
189                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TNF))
190                         return convert.toJSValueRef(NdefRecord->getTNF());
191                 else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TYPENAME))
192                         return convert.toJSValueRef(NdefRecord->getTypeName());
193                 else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_ID))
194                         return convert.toJSValueRef(NdefRecord->getID());
195                 else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_PAYLOAD))
196                         return convert.toJSValueRef(NdefRecord->getPayload());
197         } Catch (ConversionException) {
198                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
199         } Catch (PlatformException) {
200                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
201         } Catch (NullPointerException) {
202                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
203         } Catch (WrtDeviceApis::Commons::Exception) {
204                 LogError("Exception: " << _rethrown_exception.GetMessage());
205         }
206         return JSValueMakeUndefined(context);
207 }
208
209 bool JSNdefRecord::hasInstance(JSContextRef context,
210         JSObjectRef constructor,
211         JSValueRef possibleInstance,
212         JSValueRef* exception)
213 {
214     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
215 }
216
217 } //Tizen1_0
218 } //TizenApis
219