Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / NFC / JSNFCManager.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 <Commons/Exception.h>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <JSWebAPIErrorFactory.h>
21 #include <SecurityExceptions.h>
22 #include <GlobalContextManager.h>
23 #include <ArgumentValidator.h>
24 #include <PlatformException.h>
25
26 #include "JSNFCManager.h"
27 #include "JSNFCAdapter.h"
28 #include "NFCConverter.h"
29 #include "NFCFactory.h"
30 #include "plugin_config_impl.h"
31 #include "NFCDefaultAdapter.h"
32 #include <Logger.h>
33
34 using namespace std;
35
36 using namespace DeviceAPI::Common;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39
40 #define TIZEN_NFCMANAGER_ATTRIBUTENAME "NFCManager"
41
42 #define TIZEN_NFCMANAGER_TNF_EMPTY "NFC_RECORD_TNF_EMPTY"
43 #define TIZEN_NFCMANAGER_TNF_WELL_KNOWN "NFC_RECORD_TNF_WELL_KNOWN"
44 #define TIZEN_NFCMANAGER_TNF_MIME_MEDIA "NFC_RECORD_TNF_MIME_MEDIA"
45 #define TIZEN_NFCMANAGER_TNF_URI "NFC_RECORD_TNF_URI"
46 #define TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD  "NFC_RECORD_TNF_EXTERNAL_RTD"
47 #define TIZEN_NFCMANAGER_TNF_UNKNOWN  "NFC_RECORD_TNF_UNKNOWN"
48 #define TIZEN_NFCMANAGER_TNF_UNCHANGED "NFC_RECORD_TNF_UNCHANGED"
49
50 namespace DeviceAPI {
51 namespace NFC {
52
53  JSClassDefinition JSNFCManager::m_classInfo =
54 {
55         0,
56         kJSClassAttributeNone,
57         TIZEN_NFCMANAGER_ATTRIBUTENAME,
58         0,
59         m_property,
60         m_function,
61         initialize,
62         finalize,
63         NULL, //hasProperty,
64         NULL,
65         NULL, //setProperty,
66         NULL, //DeleteProperty,
67         NULL, //GetPropertyNames,
68         NULL, //CallAsFunction,
69         NULL, //CallAsConstructor,
70         NULL, //HasInstance,
71         NULL  //ConvertToType
72 };
73
74 JSStaticValue JSNFCManager::m_property[] =
75 {
76     //NFCManagerProperties
77         {TIZEN_NFCMANAGER_TNF_EMPTY,  getProperty, NULL, kJSPropertyAttributeReadOnly},
78         {TIZEN_NFCMANAGER_TNF_WELL_KNOWN,  getProperty, NULL, kJSPropertyAttributeReadOnly},
79         {TIZEN_NFCMANAGER_TNF_MIME_MEDIA,  getProperty, NULL, kJSPropertyAttributeReadOnly},
80         {TIZEN_NFCMANAGER_TNF_URI,  getProperty, NULL, kJSPropertyAttributeReadOnly},
81         {TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD,  getProperty, NULL, kJSPropertyAttributeReadOnly},
82         {TIZEN_NFCMANAGER_TNF_UNKNOWN,  getProperty, NULL, kJSPropertyAttributeReadOnly},
83         {TIZEN_NFCMANAGER_TNF_UNCHANGED,  getProperty, NULL, kJSPropertyAttributeReadOnly},
84         { 0, 0, 0, 0 }
85 };
86
87 JSStaticFunction JSNFCManager::m_function[] = {
88         {"getDefaultAdapter",      JSNFCManager::getDefaultAdapter, kJSPropertyAttributeNone },
89         {"setExclusiveMode",      JSNFCManager::setExclusiveMode, kJSPropertyAttributeNone },
90         { 0, 0, 0}
91 };
92
93 JSClassRef JSNFCManager::m_jsClassRef = JSClassCreate(JSNFCManager::getClassInfo());
94
95 void JSNFCManager::initialize(JSContextRef context, JSObjectRef object)
96 {
97         LoggerD("entered");
98         NFCManagerPrivObject* priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object));
99         if (!priv) {
100                 Try {
101                         priv = new NFCManagerPrivObject(context);
102                         if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
103                                 LoggerE("Object can't store private data.");
104                                 delete priv;
105                         }
106                 } Catch (UnsupportedException) {
107                         LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
108                 } Catch (WrtDeviceApis::Commons::UnknownException) {
109                         LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
110                 } Catch (PlatformException) {
111                         LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
112                 } Catch (WrtDeviceApis::Commons::Exception) {
113                         LoggerE("Exception: " << _rethrown_exception.GetMessage());
114                 }
115         }
116 }
117
118 void JSNFCManager::finalize(JSObjectRef object)
119 {
120         LoggerD( "entered" );
121         NFCManagerPrivObject *priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object));
122         if (priv) {
123                 JSObjectSetPrivate(object, NULL);
124                 LoggerD("Deleting NFC Manager object");
125                 delete priv;
126         }
127 }
128
129
130 const JSClassRef JSNFCManager::getClassRef()
131 {
132         if (!m_jsClassRef) {
133                 m_jsClassRef = JSClassCreate(&m_classInfo);
134         }
135         return m_jsClassRef;
136 }
137
138 const JSClassDefinition* JSNFCManager::getClassInfo()
139 {
140         return &m_classInfo;
141 }
142
143 JSValueRef JSNFCManager::getProperty(JSContextRef context, JSObjectRef object,
144         JSStringRef propertyName, JSValueRef* exception)
145 {
146         LoggerD("Enter");
147
148         Try     {
149                 Converter convert(context);
150
151                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_EMPTY)) {
152                         return convert.toJSValueRef(NFC_TNF_EMPTY);
153                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_WELL_KNOWN)) {
154                         return convert.toJSValueRef(NFC_TNF_WELL_KNOWN);
155                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_MIME_MEDIA)) {
156                         return convert.toJSValueRef(NFC_TNF_MIME_MEDIA);
157                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_URI)) {
158                         return convert.toJSValueRef(NFC_TNF_URI);
159                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD)) {
160                         return convert.toJSValueRef(NFC_TNF_EXTERNAL_RTD);
161                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_UNKNOWN)) {
162                         return convert.toJSValueRef(NFC_TNF_UNKNOWN);
163                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCMANAGER_TNF_UNCHANGED)) {
164                         return convert.toJSValueRef(NFC_TNF_UNCHANGED);
165                 }
166         } Catch (ConversionException) {
167                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
168         } Catch (UnsupportedException) {
169                 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
170         } Catch (WrtDeviceApis::Commons::UnknownException) {
171                 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
172         } Catch (PlatformException) {
173                 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
174         } Catch (WrtDeviceApis::Commons::Exception) {
175                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
176         }
177
178     return JSValueMakeUndefined(context);
179 }
180
181 JSValueRef JSNFCManager::getDefaultAdapter (JSContextRef context, JSObjectRef object,
182                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
183                 JSValueRef* exception) {
184         LoggerD("Enter");
185
186         Try {
187                 NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject));
188                 if (!privateObject) {
189                         LoggerE("private object is null");
190                         ThrowMsg(UnsupportedException, "private object is null");
191                 }
192
193                 TIZEN_CHECK_ACCESS(context, exception, privateObject,
194                 NFC_FUNCTION_API_COMMON_FUNCS);
195
196                 return JSNFCAdapter::createJSObject(GlobalContextManager::getInstance()->getGlobalContext(context), privateObject);
197         } Catch (UnsupportedException) {
198                 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
199                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, "Not Supported");
200         } Catch (ConversionException) {
201                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
202                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
203         } Catch (NullPointerException) {
204                 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
205                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
206         } Catch (InvalidArgumentException) {
207                 LoggerE("InvalidArgumentException: " << _rethrown_exception.GetMessage());
208                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid Values");
209         } Catch (PlatformException) {
210                 LoggerE("PlatformException: " << _rethrown_exception.GetMessage());
211                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR, "Service Not Available");
212         } Catch (WrtDeviceApis::Commons::UnknownException) {
213                 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
214         } Catch (WrtDeviceApis::Commons::Exception) {
215                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
216         }
217         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
218 }
219
220 JSValueRef JSNFCManager::setExclusiveMode (JSContextRef context, JSObjectRef object,
221                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
222                 JSValueRef* exception) {
223         LoggerD("Enter");
224
225         Try {
226                 NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject));
227                 if (!privateObject) {
228                         LoggerE("private object is null");
229                         throw TypeMismatchException("private object is null");
230                 }
231
232                 TIZEN_CHECK_ACCESS(context, exception, privateObject,
233                 NFC_FUNCTION_API_COMMON_FUNCS);
234
235                 ArgumentValidator validator(context, argumentCount, arguments);
236                 // mode
237         bool mode = validator.toBool(0);
238                 LoggerD("mode : " << mode);
239
240                 NFCDefaultAdapterSingleton::Instance().setExclusiveMode(mode);
241                 return JSValueMakeUndefined(context);
242     } Catch (BasePlatformException) {
243         LoggerE(_rethrown_exception.getName() << ": " << _rethrown_exception.getMessage());
244         return JSWebAPIErrorFactory::postException(context, exception, _rethrown_exception);
245         } Catch (ConversionException) {
246                 LoggerE("ConversionException: " << _rethrown_exception.GetMessage());
247                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
248         } Catch (NullPointerException) {
249                 LoggerE("NullPointerException: " << _rethrown_exception.GetMessage());
250                 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type Mismatch");
251         } Catch (WrtDeviceApis::Commons::UnknownException) {
252                 LoggerE("UnknownException: " << _rethrown_exception.GetMessage());
253         } Catch (WrtDeviceApis::Commons::Exception) {
254                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
255         }
256         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
257 }
258
259 }
260 }