2 // Tizen Web Device API
3 // Copyright (c) 2013 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 "JSNFCManager.h"
19 #include "NFCManager.h"
20 #include "JSNFCAdapter.h"
21 #include "plugin_config.h"
24 #include <ArgumentValidator.h>
25 #include <SecurityExceptions.h>
26 #include <GlobalContextManager.h>
27 #include <TimeTracer.h>
34 using namespace DeviceAPI::Common;
35 using namespace WrtDeviceApis::Commons;
38 const char* TIZEN_NFCMANAGER = "NFCManager";
40 const char* TIZEN_NFCMANAGER_GET_DEFAULT_MANAGER = "getDefaultAdapter";
41 const char* TIZEN_NFCMANAGER_SET_EXCLUSIVE_MODE = "setExclusiveMode";
43 const char* TIZEN_NFCMANAGER_TNF_EMPTY = "NFC_RECORD_TNF_EMPTY";
44 const char* TIZEN_NFCMANAGER_TNF_WELL_KNOWN = "NFC_RECORD_TNF_WELL_KNOWN";
45 const char* TIZEN_NFCMANAGER_TNF_MIME_MEDIA = "NFC_RECORD_TNF_MIME_MEDIA";
46 const char* TIZEN_NFCMANAGER_TNF_URI = "NFC_RECORD_TNF_URI";
47 const char* TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD = "NFC_RECORD_TNF_EXTERNAL_RTD";
48 const char* TIZEN_NFCMANAGER_TNF_UNKNOWN = "NFC_RECORD_TNF_UNKNOWN";
49 const char* TIZEN_NFCMANAGER_TNF_UNCHANGED = "NFC_RECORD_TNF_UNCHANGED";
52 JSClassDefinition JSNFCManager::m_classInfo =
55 kJSClassAttributeNone,
65 NULL, // deleteProperty,
66 NULL, // getPropertyNames,
67 NULL, // callAsFunction,
68 NULL, // callAsConstructor,
73 JSStaticValue JSNFCManager::m_property[] =
75 //NFCManagerProperties
76 {TIZEN_NFCMANAGER_TNF_EMPTY, get_TNF_EMPTY, NULL,
77 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
78 {TIZEN_NFCMANAGER_TNF_WELL_KNOWN, get_TNF_WELL_KNOWN, NULL,
79 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
80 {TIZEN_NFCMANAGER_TNF_MIME_MEDIA, get_TNF_MIME_MEDIA, NULL,
81 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
82 {TIZEN_NFCMANAGER_TNF_URI, get_TNF_URI, NULL,
83 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
84 {TIZEN_NFCMANAGER_TNF_EXTERNAL_RTD, get_TNF_EXTERNAL_RTD, NULL,
85 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
86 {TIZEN_NFCMANAGER_TNF_UNKNOWN, get_TNF_UNKNOWN, NULL,
87 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
88 {TIZEN_NFCMANAGER_TNF_UNCHANGED, get_TNF_UNCHANGED, NULL,
89 kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly},
93 JSStaticFunction JSNFCManager::m_function[] = {
94 {TIZEN_NFCMANAGER_GET_DEFAULT_MANAGER, JSNFCManager::getDefaultAdapter,
95 kJSPropertyAttributeNone },
96 {TIZEN_NFCMANAGER_SET_EXCLUSIVE_MODE, JSNFCManager::setExclusiveMode,
97 kJSPropertyAttributeNone },
101 JSClassRef JSNFCManager::m_jsClassRef = JSClassCreate(JSNFCManager::getClassInfo());
103 void JSNFCManager::initialize(JSContextRef context, JSObjectRef object)
106 JSObjectSetPrivate(object, static_cast<void*>(&(NFCManager::getInstance())));
109 void JSNFCManager::finalize(JSObjectRef object)
115 const JSClassRef DLL_EXPORT JSNFCManager::getClassRef()
118 m_jsClassRef = JSClassCreate(&m_classInfo);
123 const JSClassDefinition* JSNFCManager::getClassInfo()
128 JSValueRef JSNFCManager::get_TNF_EMPTY(JSContextRef context,
130 JSStringRef propertyName,
131 JSValueRef* exception)
135 return JSValueMakeNumber(context,
136 NFCManager::getInstance().NFC_RECORD_TNF_EMPTY);
138 catch (const BasePlatformException &error) {
139 LOGE("get_TNF_EMPTY failed: name: %s, msg: %s",
140 error.getName().c_str(), error.getMessage().c_str());
143 LOGE("get_TNF_EMPTY failed");
145 return JSValueMakeUndefined(context);
148 JSValueRef JSNFCManager::get_TNF_WELL_KNOWN(JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
155 return JSValueMakeNumber(context,
156 NFCManager::getInstance().NFC_RECORD_TNF_WELL_KNOWN);
158 catch (const BasePlatformException &error) {
159 LOGE("get_TNF_WELL_KNOWN failed: name: %s, msg: %s",
160 error.getName().c_str(), error.getMessage().c_str());
163 LOGE("get_TNF_WELL_KNOWN failed");
165 return JSValueMakeUndefined(context);
168 JSValueRef JSNFCManager::get_TNF_MIME_MEDIA(JSContextRef context,
170 JSStringRef propertyName,
171 JSValueRef* exception)
175 return JSValueMakeNumber(context,
176 NFCManager::getInstance().NFC_RECORD_TNF_MIME_MEDIA);
178 catch (const BasePlatformException &error) {
179 LOGE("get_TNF_MIME_MEDIA failed: name: %s, msg: %s",
180 error.getName().c_str(), error.getMessage().c_str());
183 LOGE("get_TNF_MIME_MEDIA failed");
185 return JSValueMakeUndefined(context);
188 JSValueRef JSNFCManager::get_TNF_URI(JSContextRef context,
190 JSStringRef propertyName,
191 JSValueRef* exception)
195 return JSValueMakeNumber(context,
196 NFCManager::getInstance().NFC_RECORD_TNF_URI);
198 catch (const BasePlatformException &error) {
199 LOGE("get_TNF_URI failed: name: %s, msg: %s",
200 error.getName().c_str(), error.getMessage().c_str());
203 LOGE("get_TNF_URI failed");
205 return JSValueMakeUndefined(context);
208 JSValueRef JSNFCManager::get_TNF_EXTERNAL_RTD(JSContextRef context,
210 JSStringRef propertyName,
211 JSValueRef* exception)
215 return JSValueMakeNumber(context,
216 NFCManager::getInstance().NFC_RECORD_TNF_EXTERNAL_RTD);
218 catch (const BasePlatformException &error) {
219 LOGE("get_TNF_EXTERNAL_RTD failed: name: %s, msg: %s",
220 error.getName().c_str(), error.getMessage().c_str());
223 LOGE("get_TNF_EXTERNAL_RTD failed");
225 return JSValueMakeUndefined(context);
228 JSValueRef JSNFCManager::get_TNF_UNKNOWN(JSContextRef context,
230 JSStringRef propertyName,
231 JSValueRef* exception)
235 return JSValueMakeNumber(context,
236 NFCManager::getInstance().NFC_RECORD_TNF_UNKNOWN);
238 catch (const BasePlatformException &error) {
239 LOGE("get_TNF_UNKNOWN failed: name: %s, msg: %s",
240 error.getName().c_str(), error.getMessage().c_str());
243 LOGE("get_TNF_UNKNOWN failed");
245 return JSValueMakeUndefined(context);
248 JSValueRef JSNFCManager::get_TNF_UNCHANGED(JSContextRef context,
250 JSStringRef propertyName,
251 JSValueRef* exception)
255 return JSValueMakeNumber(context,
256 NFCManager::getInstance().NFC_RECORD_TNF_UNCHANGED);
258 catch (const BasePlatformException &error) {
259 LOGE("get_TNF_UNCHANGED failed: name: %s, msg: %s",
260 error.getName().c_str(), error.getMessage().c_str());
263 LOGE("get_TNF_UNCHANGED failed");
265 return JSValueMakeUndefined(context);
268 JSValueRef JSNFCManager::getDefaultAdapter (JSContextRef context, JSObjectRef object,
269 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
270 JSValueRef* exception)
272 SET_TIME_TRACER_ITEM(0);
275 TIZEN_CHECK_ACCESS(context, exception, &(NFCManager::getInstance()),
276 NFC_FUNCTION_API_COMMON_FUNCS);
279 JSObjectRef js_object = JSNFCAdapter::makeJSObject(context,
280 NFCManager::getInstance().getDefaultAdapter());
282 } catch (const BasePlatformException &error) {
283 LOGE("getDefaultAdapter failed: name: %s, msg: %s",
284 error.getName().c_str(), error.getMessage().c_str());
285 return JSWebAPIErrorFactory::postException(context, exception, error);
287 LOGE("getDefaultAdapter failed");
288 return JSWebAPIErrorFactory::postException(context, exception,
289 JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
293 JSValueRef JSNFCManager::setExclusiveMode (JSContextRef context, JSObjectRef object,
294 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
295 JSValueRef* exception)
297 SET_TIME_TRACER_ITEM(0);
300 TIZEN_CHECK_ACCESS(context, exception, &(NFCManager::getInstance()),
301 NFC_FUNCTION_API_COMMON_FUNCS);
303 ArgumentValidator validator(context, argumentCount, arguments);
304 bool mode = validator.toBool(0);
305 NFCManager::getInstance().setExclusiveMode(mode);
306 } catch (const BasePlatformException &error) {
307 LOGE("getDefaultAdapter failed: name: %s, msg: %s",
308 error.getName().c_str(), error.getMessage().c_str());
309 return JSWebAPIErrorFactory::postException(context, exception, error);
311 LOGE("getDefaultAdapter failed");
312 return JSWebAPIErrorFactory::postException(context, exception,
313 JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
316 return JSValueMakeUndefined(context);