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