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 <JSWebAPIErrorFactory.h>
21 #include <SecurityExceptions.h>
22 #include <GlobalContextManager.h>
23 #include <ArgumentValidator.h>
24 #include <PlatformException.h>
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"
36 using namespace DeviceAPI::Common;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
40 #define TIZEN_NFCMANAGER_ATTRIBUTENAME "NFCManager"
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"
53 JSClassDefinition JSNFCManager::m_classInfo =
56 kJSClassAttributeNone,
57 TIZEN_NFCMANAGER_ATTRIBUTENAME,
66 NULL, //DeleteProperty,
67 NULL, //GetPropertyNames,
68 NULL, //CallAsFunction,
69 NULL, //CallAsConstructor,
74 JSStaticValue JSNFCManager::m_property[] =
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},
87 JSStaticFunction JSNFCManager::m_function[] = {
88 {"getDefaultAdapter", JSNFCManager::getDefaultAdapter, kJSPropertyAttributeNone },
89 {"setExclusiveMode", JSNFCManager::setExclusiveMode, kJSPropertyAttributeNone },
93 JSClassRef JSNFCManager::m_jsClassRef = JSClassCreate(JSNFCManager::getClassInfo());
95 void JSNFCManager::initialize(JSContextRef context, JSObjectRef object)
98 NFCManagerPrivObject* priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object));
101 priv = new NFCManagerPrivObject(context);
102 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
103 LoggerE("Object can't store private data.");
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());
118 void JSNFCManager::finalize(JSObjectRef object)
120 LoggerD( "entered" );
121 NFCManagerPrivObject *priv = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(object));
123 JSObjectSetPrivate(object, NULL);
124 LoggerD("Deleting NFC Manager object");
130 const JSClassRef JSNFCManager::getClassRef()
133 m_jsClassRef = JSClassCreate(&m_classInfo);
138 const JSClassDefinition* JSNFCManager::getClassInfo()
143 JSValueRef JSNFCManager::getProperty(JSContextRef context, JSObjectRef object,
144 JSStringRef propertyName, JSValueRef* exception)
149 Converter convert(context);
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);
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());
178 return JSValueMakeUndefined(context);
181 JSValueRef JSNFCManager::getDefaultAdapter (JSContextRef context, JSObjectRef object,
182 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
183 JSValueRef* exception) {
187 NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject));
188 if (!privateObject) {
189 LoggerE("private object is null");
190 ThrowMsg(UnsupportedException, "private object is null");
193 TIZEN_CHECK_ACCESS(context, exception, privateObject,
194 NFC_FUNCTION_API_COMMON_FUNCS);
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());
217 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");
220 JSValueRef JSNFCManager::setExclusiveMode (JSContextRef context, JSObjectRef object,
221 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
222 JSValueRef* exception) {
226 NFCManagerPrivObject* privateObject = static_cast<NFCManagerPrivObject*>(JSObjectGetPrivate(thisObject));
227 if (!privateObject) {
228 LoggerE("private object is null");
229 throw TypeMismatchException("private object is null");
232 TIZEN_CHECK_ACCESS(context, exception, privateObject,
233 NFC_FUNCTION_API_COMMON_FUNCS);
235 ArgumentValidator validator(context, argumentCount, arguments);
237 bool mode = validator.toBool(0);
238 LoggerD("mode : " << mode);
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());
256 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown Error");