wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / NFC / JSNdefRecordURI.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include <memory>
20 #include <string>
21 #include <CommonsJavaScript/JSUtils.h>
22
23 #include <JSTizenExceptionFactory.h>
24 #include <JSTizenException.h>
25 #include <SecurityExceptions.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <ArgumentValidator.h>
28 #include <GlobalContextManager.h>
29 #include <JSWebAPIException.h>
30 #include <PlatformException.h>
31
32 #include "NFCFactory.h"
33 #include "JSNdefRecordURI.h"
34 #include <Logger.h>
35
36 namespace DeviceAPI {
37 namespace NFC {
38
39 using namespace DeviceAPI::Common;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42
43 #define TIZEN_NDEFRECORD_URI_URI "uri"
44
45 JSClassDefinition JSNdefRecordURI::m_classInfo =
46 {
47         0,
48         kJSClassAttributeNone,
49         "NDEFRecordURI",
50         JSNdefRecord::getClassRef(),
51         m_property,
52         NULL,
53         initialize,
54         finalize,
55         NULL, //HasProperty,
56         NULL,
57         NULL, //SetProperty,
58         NULL, //DeleteProperty,
59         NULL, //GetPropertyNames,
60         NULL, //CallAsFunction,
61         NULL, //CallAsConstructor,
62         NULL,
63         NULL, //ConvertToType
64 };
65
66 JSStaticValue JSNdefRecordURI::m_property[] =
67 {
68         {TIZEN_NDEFRECORD_URI_URI,  getProperty, NULL, kJSPropertyAttributeReadOnly},
69         { 0, 0, 0, 0 }
70 };
71
72 const JSClassRef JSNdefRecordURI::getClassRef() {
73         if (!m_jsClassRef) {
74                 m_jsClassRef = JSClassCreate(&m_classInfo);
75         }
76         return m_jsClassRef;
77 }
78
79 const JSClassDefinition* JSNdefRecordURI::getClassInfo() {
80         return &m_classInfo;
81 }
82
83 JSClassRef JSNdefRecordURI::m_jsClassRef = JSClassCreate(JSNdefRecordURI::getClassInfo());
84
85 void JSNdefRecordURI::initialize(JSContextRef context, JSObjectRef object) {
86         LoggerD("entered");
87 }
88
89 void JSNdefRecordURI::finalize(JSObjectRef object) {
90         LoggerD("Entered");
91 }
92
93 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
94         return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload));
95 }
96
97 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, const std::string &uri) {
98         return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(uri));
99 }
100
101 JSObjectRef JSNdefRecordURI::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) {
102         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord);
103
104         if (!priv) {
105                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
106         }
107
108         JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
109         return obj;
110 }
111
112 JSObjectRef JSNdefRecordURI::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
113 {
114         LoggerD("entered");
115
116         Try {
117                 ArgumentValidator validator(ctx, argumentCount, arguments);
118                 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx);
119
120                 JSObjectRef result = createJSObject(global_context, validator.toString(0));
121
122                 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
123             JSObjectSetProperty(global_context, result, ctorName, constructor,
124                 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
125             JSStringRelease(ctorName);
126                 return result;
127     } Catch (BasePlatformException) {
128         JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, _rethrown_exception);
129         *exception = error;
130         return error;
131         } Catch(ConversionException) {
132                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
133                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
134                 *exception = error;
135         return error;
136         } Catch (InvalidArgumentException) {
137                 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
138                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
139                 *exception = error;
140         return error;
141         } Catch (PlatformException) {
142                 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
143                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
144                 *exception = error;
145         return error;
146         } Catch (WrtDeviceApis::Commons::UnknownException) {
147                 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
148         } Catch (WrtDeviceApis::Commons::Exception) {
149                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
150         }
151         JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
152         *exception = error;
153         return error;
154 }
155 JSValueRef JSNdefRecordURI::getProperty(JSContextRef context, JSObjectRef object,
156         JSStringRef propertyName, JSValueRef* exception) {
157         LoggerD("Enter");
158
159         Try     {
160                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_URI_URI)) {
161                         NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
162                         if (!priv) {
163                                 ThrowMsg(NullPointerException, "Private object not set.");
164                         }
165                         INdefRecordPtr ndefRecord = priv->getObject();
166                         char * uri = NULL;
167                         if (ndefRecord->getUri(&uri)) {
168                                 Converter convert(context);
169                                 std::string result(uri);
170                                 free(uri);
171                                 LoggerD("uri : " << result);
172                                 return convert.toJSValueRef(result);
173                         }
174                         LoggerD("This record is not URI Type");
175                 }
176         } Catch (ConversionException) {
177                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
178         } Catch (WrtDeviceApis::Commons::UnknownException) {
179                 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
180         } Catch (PlatformException) {
181                 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
182         } Catch (NullPointerException) {
183                 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
184         } Catch (WrtDeviceApis::Commons::Exception) {
185                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
186         }
187         return JSValueMakeUndefined(context);
188 }
189
190
191 } //NFC
192 } //DeviceAPI
193