Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNdefRecordURI.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 "JSNdefRecordURI.h"
30 #include <CommonsJavaScript/Converter.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_URI_URI "uri"
41
42 JSClassDefinition JSNdefRecordURI::m_classInfo =
43 {
44         0,
45         kJSClassAttributeNone,
46         "NDEFRecordURI",
47         JSNdefRecord::getClassRef(),
48         m_property,
49         NULL,
50         initialize,
51         finalize,
52         NULL, //HasProperty,
53         NULL,
54         NULL, //SetProperty,
55         NULL, //DeleteProperty,
56         NULL, //GetPropertyNames,
57         NULL, //CallAsFunction,
58         constructor, //CallAsConstructor,
59         hasInstance,
60         NULL, //ConvertToType
61 };
62
63 JSStaticValue JSNdefRecordURI::m_property[] =
64 {
65         {TIZEN10_NDEFRECORD_URI_URI,  getProperty, NULL, kJSPropertyAttributeReadOnly},
66         { 0, 0, 0, 0 }
67 };
68
69 const JSClassRef JSNdefRecordURI::getClassRef() {
70         if (!m_jsClassRef) {
71                 m_jsClassRef = JSClassCreate(&m_classInfo);
72         }
73         return m_jsClassRef;
74 }
75
76 const JSClassDefinition* JSNdefRecordURI::getClassInfo() {
77         return &m_classInfo;
78 }
79
80 JSClassRef JSNdefRecordURI::m_jsClassRef = JSClassCreate(JSNdefRecordURI::getClassInfo());
81
82 void JSNdefRecordURI::initialize(JSContextRef context, JSObjectRef object) {
83         LogDebug("entered");
84
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)) {
90                         delete priv;
91                 }
92         }
93 }
94
95 void JSNdefRecordURI::finalize(JSObjectRef object) {
96         LogDebug("Entered");
97         NdefRecordPrivObject* priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
98         JSObjectSetPrivate(object, NULL);
99         LogDebug("Deleting ndefrecord object");
100         delete priv;
101 }
102
103 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
104         INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload);
105
106         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
107
108         if (!priv) {
109                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
110         }
111
112         return JSObjectMake(context, getClassRef(), priv);
113 }
114
115 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const std::string &uri) {
116         INdefRecordPtr NdefRecord = NFCFactory::getInstance().createNDEFRecordObject(uri);
117
118         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, NdefRecord);
119
120         if (!priv) {
121                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
122         }
123
124         return JSObjectMake(context, getClassRef(), priv);
125 }
126
127 JSObjectRef JSNdefRecordURI::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 != 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");
138                 }
139
140                 Converter convert(ctx);
141
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");
146                 return NULL;
147         } Catch (InvalidArgumentException) {
148                 LogError("InvalidArgumentException: " << _rethrown_exception.GetMessage());
149                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
150                 return NULL;
151         } Catch (PlatformException) {
152                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
153                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
154                 return NULL;
155         } Catch (WrtDeviceApis::Commons::Exception) {
156                 LogError("Exception: " << _rethrown_exception.GetMessage());
157                 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
158                 return NULL;
159         }
160         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
161         return NULL;
162
163 }
164 JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object,
165         JSStringRef propertyName, JSValueRef* exception) {
166         LogDebug("Enter");
167
168         Try     {
169                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_NDEFRECORD_URI_URI)) {
170                         NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
171                         if (!priv) {
172                                 ThrowMsg(NullPointerException, "Private object not set.");
173                         }
174                         INdefRecordPtr NdefRecord = priv->getObject();
175                         char * uri = NULL;
176                         if (NdefRecord->getUri(&uri)) {
177                                 Converter convert(context);
178                                 std::string result(uri);
179                                 free(uri);
180                                 LogDebug("uri : " << result);
181                                 return convert.toJSValueRef(result);
182                         }
183                         LogDebug("This record is not URI Type");
184                 }
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());
193         }
194         return JSValueMakeUndefined(context);
195 }
196
197 bool JSNdefRecordURI::hasInstance(JSContextRef context,
198         JSObjectRef constructor,
199         JSValueRef possibleInstance,
200         JSValueRef* exception)
201 {
202     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
203 }
204
205 } //Tizen1_0
206 } //TizenApis
207