wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / NFC / JSNdefRecordMedia.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 #include <memory>
19 #include <string>
20 #include <CommonsJavaScript/JSUtils.h>
21
22 #include <JSTizenExceptionFactory.h>
23 #include <JSTizenException.h>
24 #include <SecurityExceptions.h>
25 #include <ArgumentValidator.h>
26 #include <GlobalContextManager.h>
27 #include <JSWebAPIException.h>
28 #include <PlatformException.h>
29
30 #include "NFCFactory.h"
31 #include "JSNdefRecordMedia.h"
32 #include "NFCConverter.h"
33 #include <Logger.h>
34
35 namespace DeviceAPI {
36 namespace NFC {
37
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41
42 #define TIZEN_NDEFRECORD_MEDIA_MIMETYPE "mimeType"
43
44 JSClassDefinition JSNdefRecordMedia::m_classInfo =
45 {
46         0,
47         kJSClassAttributeNone,
48         "NDEFRecordMedia",
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         NULL, //CallAsConstructor,
61         NULL,
62         NULL, //ConvertToType
63 };
64
65 JSStaticValue JSNdefRecordMedia::m_property[] =
66 {
67         {TIZEN_NDEFRECORD_MEDIA_MIMETYPE,  getProperty, NULL, kJSPropertyAttributeReadOnly},
68         { 0, 0, 0, 0 }
69 };
70
71 const JSClassRef JSNdefRecordMedia::getClassRef() {
72         if (!m_jsClassRef) {
73                 m_jsClassRef = JSClassCreate(&m_classInfo);
74         }
75         return m_jsClassRef;
76 }
77
78 const JSClassDefinition* JSNdefRecordMedia::getClassInfo() {
79         return &m_classInfo;
80 }
81
82 JSClassRef JSNdefRecordMedia::m_jsClassRef = JSClassCreate(JSNdefRecordMedia::getClassInfo());
83
84 JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const NdefRecordProperties &ndefRecordProperties, std::vector<unsigned char> payload) {
85         return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(ndefRecordProperties, payload));
86 }
87
88 JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, const std::string &mimeType, std::vector<unsigned char> data) {
89         return createJSObject(context, NFCFactory::getInstance().createNDEFRecordObject(mimeType, data));
90 }
91
92 JSObjectRef JSNdefRecordMedia::createJSObject(JSContextRef context, INdefRecordPtr ndefRecord) {
93         NdefRecordPrivObject *priv = new NdefRecordPrivObject(context, ndefRecord);
94
95         if (!priv) {
96                 ThrowMsg(NullPointerException, "Can not new a NdefRecord object");
97         }
98
99         JSObjectRef obj = JSObjectMake(context, getClassRef(), priv);
100         return obj;
101 }
102
103 void JSNdefRecordMedia::initialize(JSContextRef context, JSObjectRef object) {
104         LoggerD("Entered");
105 }
106
107 void JSNdefRecordMedia::finalize(JSObjectRef object) {
108         LoggerD("Entered");
109 }
110
111 JSObjectRef JSNdefRecordMedia::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
112 {
113         LoggerD("entered");
114
115         Try {
116                 ArgumentValidator validator(ctx, argumentCount, arguments);
117                 std::string mimeType = validator.toString(0);
118                 std::vector<unsigned char> data;
119                 JSContextRef global_context = GlobalContextManager::getInstance()->getGlobalContext(ctx);
120
121                 Converter convert(ctx);
122                 if (validator.toArrayObject(1))
123                         data = convert.toVectorOfUChars(arguments[1]);
124
125                 JSObjectRef result = createJSObject(global_context, mimeType, data);
126
127                 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
128             JSObjectSetProperty(global_context, result, ctorName, constructor,
129                 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
130             JSStringRelease(ctorName);
131                 return result;
132     } Catch (BasePlatformException) {
133         JSObjectRef error = JSWebAPIException::makeJSWebAPIException(ctx, _rethrown_exception);
134         *exception = error;
135         return error;
136         } Catch(ConversionException) {
137                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
138                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
139                 *exception = error;
140         return error;
141         } Catch (InvalidArgumentException) {
142                 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
143                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
144                 *exception = error;
145         return error;
146         } Catch (PlatformException) {
147                 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
148                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
149                 *exception = error;
150         return error;
151         } Catch (WrtDeviceApis::Commons::UnknownException) {
152                 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
153         } Catch (WrtDeviceApis::Commons::Exception) {
154                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
155         }
156         JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
157         *exception = error;
158         return error;
159 }
160
161 JSValueRef JSNdefRecordMedia::getProperty(JSContextRef context, JSObjectRef object,
162         JSStringRef propertyName, JSValueRef* exception) {
163         LoggerD("Enter");
164
165         Try     {
166                 NFCConverter convert(context);
167                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NDEFRECORD_MEDIA_MIMETYPE)) {
168                         NdefRecordPrivObject *priv = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(object));
169                         if (!priv) {
170                                 ThrowMsg(NullPointerException, "Private object not set.");
171                         }
172                         INdefRecordPtr ndefRecord = priv->getObject();
173                         char * mimeType = NULL;
174                         if (ndefRecord->getMimeType(&mimeType)) {
175                                 std::string result(mimeType);
176                                 free(mimeType);
177                                 LoggerD("mimeType : " << result);
178                                 return convert.toJSValueRef(result);
179                         }
180                         LoggerD("This record is not MEDIA Type");
181                 }
182
183         } Catch (ConversionException) {
184                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
185         } Catch (WrtDeviceApis::Commons::UnknownException) {
186                 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
187         } Catch (PlatformException) {
188                 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
189         } Catch (NullPointerException) {
190                 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
191         } Catch (WrtDeviceApis::Commons::Exception) {
192                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
193         }
194         return JSValueMakeUndefined(context);
195 }
196
197
198 } //NFC
199 } //DeviceAPI
200