2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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"
38 using namespace DeviceAPI::Common;
39 using namespace WrtDeviceApis::Commons;
40 using namespace WrtDeviceApis::CommonsJavaScript;
42 #define TIZEN_NFCMANAGER_ATTRIBUTENAME "NFCManager"
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"
55 JSClassDefinition JSNFCManager::m_classInfo =
58 kJSClassAttributeNone,
59 TIZEN_NFCMANAGER_ATTRIBUTENAME,
68 NULL, //DeleteProperty,
69 NULL, //GetPropertyNames,
70 NULL, //CallAsFunction,
71 NULL, //CallAsConstructor,
76 JSStaticValue JSNFCManager::m_property[] =
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},
89 JSStaticFunction JSNFCManager::m_function[] = {
90 {"getDefaultAdapter", JSNFCManager::getDefaultAdapter, kJSPropertyAttributeNone },
91 {"setExclusiveMode", JSNFCManager::setExclusiveMode, kJSPropertyAttributeNone },
95 JSClassRef JSNFCManager::m_jsClassRef = JSClassCreate(JSNFCManager::getClassInfo());
97 void JSNFCManager::initialize(JSContextRef context, JSObjectRef object)
100 NFCManagerPrivObject* priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object));
103 priv = new NFCManagerPrivObject(context);
104 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
105 LoggerE("Object can't store private data.");
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());
120 void JSNFCManager::finalize(JSObjectRef object)
122 LoggerD( "entered" );
123 NFCManagerPrivObject *priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object));
125 JSObjectSetPrivate(object, NULL);
126 LoggerD("Deleting NFC Manager object");
132 const JSClassRef JSNFCManager::getClassRef()
135 m_jsClassRef = JSClassCreate(&m_classInfo);
140 const JSClassDefinition* JSNFCManager::getClassInfo()
145 JSValueRef JSNFCManager::getProperty(JSContextRef context, JSObjectRef object,
146 JSStringRef propertyName, JSValueRef* exception)
151 Converter convert(context);
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);
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());
180 return JSValueMakeUndefined(context);
183 JSValueRef JSNFCManager::getDefaultAdapter (JSContextRef context, JSObjectRef object,
184 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
185 JSValueRef* exception) {
189 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_COMMON_FUNCS);
190 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
192 NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject));
193 if (!privateObject) {
194 LoggerE("private object is null");
195 ThrowMsg(UnsupportedException, "private object is null");
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());
218 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
221 JSValueRef JSNFCManager::setExclusiveMode (JSContextRef context, JSObjectRef object,
222 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
223 JSValueRef* exception) {
227 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_COMMON_FUNCS);
228 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
230 ArgumentValidator validator(context, argumentCount, arguments);
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");
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());
255 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");