Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNdefRecordText.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 "JSNdefRecordText.h"
30 #include "NFCConverter.h"
31
32 namespace TizenApis {
33 namespace Tizen1_0 {
34
35 using namespace TizenApis::Commons;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace Api::NFC;
39
40 #define TIZEN10_NDEFRECORD_TEXT_TEXT "text"
41 #define TIZEN10_NDEFRECORD_TEXT_LANGCODE "langCode"
42 #define TIZEN10_NDEFRECORD_TEXT_ENCODETYPE "encodeType"
43
44 JSClassDefinition JSNdefRecordText::m_classInfo =
45 {
46         0,
47         kJSClassAttributeNone,
48         "NDEFRecordText",
49         JSNdefRecord::getClassRef(),
50         m_property,
51         NULL,
52         initialize,
53         finalize,
54         NULL, //HasProperty,
55         NULL,
56         NULL, //SetProperty,
57         NULL, //DeleteProperty,
58         NULL, //GetPropertyNames,
59         NULL, //CallAsFunction,
60         constructor, //CallAsConstructor,
61         hasInstance,
62         NULL, //ConvertToType
63 };
64
65 JSStaticValue JSNdefRecordText::m_property[] =
66 {
67         {TIZEN10_NDEFRECORD_TEXT_TEXT,  getProperty, NULL, kJSPropertyAttributeReadOnly},
68         {TIZEN10_NDEFRECORD_TEXT_LANGCODE,  getProperty, NULL, kJSPropertyAttributeReadOnly},
69         {TIZEN10_NDEFRECORD_TEXT_ENCODETYPE,  getProperty, NULL, kJSPropertyAttributeReadOnly},
70         { 0, 0, 0, 0 }
71 };
72
73 const JSClassRef JSNdefRecordText::getClassRef() {
74         if (!m_jsClassRef) {
75                 m_jsClassRef = JSClassCreate(&m_classInfo);
76         }
77         return m_jsClassRef;
78 }
79
80 const JSClassDefinition* JSNdefRecordText::getClassInfo() {
81         return &m_classInfo;
82 }
83
84 JSClassRef JSNdefRecordText::m_jsClassRef = JSClassCreate(JSNdefRecordText::getClassInfo());
85
86 JSObjectRef JSNdefRecordText::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
87         INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload);
88
89         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
90
91         if (!priv) {
92                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
93         }
94
95         return JSObjectMake(context, getClassRef(), priv);
96 }
97
98 JSObjectRef JSNdefRecordText::createJSObject(JSContextRef context, const std::string &text, const std::string &langCode, const short encodeType) {
99         INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(text, langCode, encodeType);
100
101         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
102
103         if (!priv) {
104                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
105         }
106
107         return JSObjectMake(context, getClassRef(), priv);
108 }
109
110 void JSNdefRecordText::initialize(JSContextRef context, JSObjectRef object) {
111         LogDebug("entered");
112
113         if (!JSObjectGetPrivate(object)) {
114                 LogDebug("Private object not set... setting it.");
115                 INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject();
116                 NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
117                 if (!JSObjectSetPrivate(object, priv)) {
118                         delete priv;
119                 }
120         }
121 }
122
123 void JSNdefRecordText::finalize(JSObjectRef object) {
124         LogDebug("Entered");
125         NdefRecordPrivObject* priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
126         JSObjectSetPrivate(object, NULL);
127         LogDebug("Deleting ndefrecord object");
128         delete priv;
129 }
130
131 JSObjectRef JSNdefRecordText::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
132 {
133         LogDebug("entered");
134
135         NdefRecordPrivObject* mainPriv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(constructor));
136         JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
137         Try {
138                 if ((argumentCount < 2) || (argumentCount > 3)) {
139                         ThrowMsg(InvalidArgumentException, "parameter count is wrong.");
140                 } else if ((JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !JSValueIsString(ctx, arguments[0]))
141                         || (JSValueIsNull(ctx, arguments[1]) || JSValueIsUndefined(ctx, arguments[1]) || !JSValueIsString(ctx, arguments[1]))) {
142                         ThrowMsg(ConversionException, "parameter is JSNull/JSUndefined/Not array");
143                 }
144
145                 NFCConverter convert(ctx);
146                 short encodeType = NFC_TEXT_ENCODE_UTF_8;
147
148                 if ((argumentCount == 3) && (!JSValueIsNull(ctx, arguments[2]))) {
149                         if (JSValueIsUndefined(ctx, arguments[2]) || !JSValueIsString(ctx, arguments[2]))
150                                 Throw(ConversionException);
151                         encodeType = convert.toNfcTextEncodeUTF(convert.toString(arguments[2]));
152                 }
153
154                 return createJSObject(global_context, convert.toString(arguments[0]), convert.toString(arguments[1]), encodeType);
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 JSNdefRecordText::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                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TEXT_TEXT)) {
189                         char * text = NULL;
190                         if (NdefRecord->getText(&text)) {
191                                 std::string result(text);
192                                 free(text);
193                                 LogDebug("text : " << result);
194                                 return convert.toJSValueRef(result);
195                         }
196                         LogDebug("This record is not Text Type");
197                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TEXT_LANGCODE)) {
198                         char * landCode = NULL;
199                         if (NdefRecord->getLangCode(&landCode)) {
200                                 std::string result(landCode);
201                                 free(landCode);
202                                 LogDebug("landCode : " << result);
203                                 return convert.toJSValueRef(result);
204                         }
205                         LogDebug("This record is not Text Type");
206                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_TEXT_ENCODETYPE)) {
207                         nfcTextEncodeUTF encodeType = NFC_TEXT_ENCODE_UTF_16;
208                         if (NdefRecord->getEncodeType(&encodeType)) {
209                                 LogDebug("encodeType : " << encodeType);
210                                 return convert.toJSValueRef(convert.toNfcTextEncodeUTFString(encodeType));
211                         }
212                         LogDebug("This record is not Text Type");
213                 }
214
215         } Catch (ConversionException) {
216                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
217         } Catch (PlatformException) {
218                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
219         } Catch (NullPointerException) {
220                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
221         } Catch (WrtDeviceApis::Commons::Exception) {
222                 LogError("Exception: " << _rethrown_exception.GetMessage());
223         }
224         return JSValueMakeUndefined(context);
225 }
226
227 bool JSNdefRecordText::hasInstance(JSContextRef context,
228         JSObjectRef constructor,
229         JSValueRef possibleInstance,
230         JSValueRef* exception)
231 {
232     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
233 }
234
235 } //Tizen1_0
236 } //TizenApis
237