tizen 2.3.1 release
[framework/web/mobile/wrt-plugins-tizen.git] / src / NFC / JSNFCManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2013 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 "JSNFCManager.h"
19 #include "NFCManager.h"
20 #include "JSNFCAdapter.h"
21 #include "plugin_config.h"
22
23 #include <Logger.h>
24 #include <ArgumentValidator.h>
25 #include <SecurityExceptions.h>
26 #include <GlobalContextManager.h>
27 #include <TimeTracer.h>
28 #include <JSUtil.h>
29 #include <Export.h>
30
31 namespace DeviceAPI {
32 namespace NFC {
33
34 using namespace DeviceAPI::Common;
35 using namespace WrtDeviceApis::Commons;
36
37 namespace {
38 const char* TIZEN_NFCMANAGER = "NFCManager";
39
40 const char* TIZEN_NFCMANAGER_GET_DEFAULT_MANAGER = "getDefaultAdapter";
41 const char* TIZEN_NFCMANAGER_SET_EXCLUSIVE_MODE = "setExclusiveMode";
42
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";
50 }
51
52 JSClassDefinition JSNFCManager::m_classInfo =
53 {
54     0,
55     kJSClassAttributeNone,
56     TIZEN_NFCMANAGER,
57     NULL,
58     m_property,
59     m_function,
60     initialize,
61     finalize,
62     NULL, // hasProperty,
63     NULL, // getProperty,
64     NULL, // setProperty,
65     NULL, // deleteProperty,
66     NULL, // getPropertyNames,
67     NULL, // callAsFunction,
68     NULL, // callAsConstructor,
69     NULL, // hasInstance,
70     NULL  // convertToType
71 };
72
73 JSStaticValue JSNFCManager::m_property[] =
74 {
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},
90     { 0, 0, 0, 0 }
91 };
92
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 },
98     { 0, 0, 0}
99 };
100
101 JSClassRef JSNFCManager::m_jsClassRef = JSClassCreate(JSNFCManager::getClassInfo());
102
103 void JSNFCManager::initialize(JSContextRef context, JSObjectRef object)
104 {
105     LOGD("Entered");
106     JSObjectSetPrivate(object, static_cast<void*>(&(NFCManager::getInstance())));
107 }
108
109 void JSNFCManager::finalize(JSObjectRef object)
110 {
111     LOGD("Entered");
112 }
113
114
115 const JSClassRef DLL_EXPORT JSNFCManager::getClassRef()
116 {
117     if (!m_jsClassRef) {
118          m_jsClassRef = JSClassCreate(&m_classInfo);
119     }
120     return m_jsClassRef;
121 }
122
123 const JSClassDefinition* JSNFCManager::getClassInfo()
124 {
125     return &m_classInfo;
126 }
127
128 JSValueRef JSNFCManager::get_TNF_EMPTY(JSContextRef context,
129         JSObjectRef object,
130         JSStringRef propertyName,
131         JSValueRef* exception)
132 {
133     LOGD("Entered");
134     try{
135         return JSValueMakeNumber(context,
136                 NFCManager::getInstance().NFC_RECORD_TNF_EMPTY);
137     }
138     catch (const BasePlatformException &error) {
139         LOGE("get_TNF_EMPTY failed: name: %s, msg: %s",
140             error.getName().c_str(), error.getMessage().c_str());
141     }
142     catch (...){
143         LOGE("get_TNF_EMPTY failed");
144     }
145     return JSValueMakeUndefined(context);
146 }
147
148 JSValueRef JSNFCManager::get_TNF_WELL_KNOWN(JSContextRef context,
149         JSObjectRef object,
150         JSStringRef propertyName,
151         JSValueRef* exception)
152 {
153     LOGD("Entered");
154     try{
155         return JSValueMakeNumber(context,
156                 NFCManager::getInstance().NFC_RECORD_TNF_WELL_KNOWN);
157     }
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());
161     }
162     catch (...){
163         LOGE("get_TNF_WELL_KNOWN failed");
164     }
165     return JSValueMakeUndefined(context);
166 }
167
168 JSValueRef JSNFCManager::get_TNF_MIME_MEDIA(JSContextRef context,
169         JSObjectRef object,
170         JSStringRef propertyName,
171         JSValueRef* exception)
172 {
173     LOGD("Entered");
174     try{
175         return JSValueMakeNumber(context,
176                 NFCManager::getInstance().NFC_RECORD_TNF_MIME_MEDIA);
177     }
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());
181     }
182     catch (...){
183         LOGE("get_TNF_MIME_MEDIA failed");
184     }
185     return JSValueMakeUndefined(context);
186 }
187
188 JSValueRef JSNFCManager::get_TNF_URI(JSContextRef context,
189         JSObjectRef object,
190         JSStringRef propertyName,
191         JSValueRef* exception)
192 {
193     LOGD("Entered");
194     try{
195         return JSValueMakeNumber(context,
196                 NFCManager::getInstance().NFC_RECORD_TNF_URI);
197     }
198     catch (const BasePlatformException &error) {
199         LOGE("get_TNF_URI failed: name: %s, msg: %s",
200             error.getName().c_str(), error.getMessage().c_str());
201     }
202     catch (...){
203         LOGE("get_TNF_URI failed");
204     }
205     return JSValueMakeUndefined(context);
206 }
207
208 JSValueRef JSNFCManager::get_TNF_EXTERNAL_RTD(JSContextRef context,
209         JSObjectRef object,
210         JSStringRef propertyName,
211         JSValueRef* exception)
212 {
213     LOGD("Entered");
214     try{
215         return JSValueMakeNumber(context,
216                 NFCManager::getInstance().NFC_RECORD_TNF_EXTERNAL_RTD);
217     }
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());
221     }
222     catch (...){
223         LOGE("get_TNF_EXTERNAL_RTD failed");
224     }
225     return JSValueMakeUndefined(context);
226 }
227
228 JSValueRef JSNFCManager::get_TNF_UNKNOWN(JSContextRef context,
229         JSObjectRef object,
230         JSStringRef propertyName,
231         JSValueRef* exception)
232 {
233     LOGD("Entered");
234     try{
235         return JSValueMakeNumber(context,
236                 NFCManager::getInstance().NFC_RECORD_TNF_UNKNOWN);
237     }
238     catch (const BasePlatformException &error) {
239         LOGE("get_TNF_UNKNOWN failed: name: %s, msg: %s",
240             error.getName().c_str(), error.getMessage().c_str());
241     }
242     catch (...){
243         LOGE("get_TNF_UNKNOWN failed");
244     }
245     return JSValueMakeUndefined(context);
246 }
247
248 JSValueRef JSNFCManager::get_TNF_UNCHANGED(JSContextRef context,
249         JSObjectRef object,
250         JSStringRef propertyName,
251         JSValueRef* exception)
252 {
253     LOGD("Entered");
254     try{
255         return JSValueMakeNumber(context,
256                 NFCManager::getInstance().NFC_RECORD_TNF_UNCHANGED);
257     }
258     catch (const BasePlatformException &error) {
259         LOGE("get_TNF_UNCHANGED failed: name: %s, msg: %s",
260             error.getName().c_str(), error.getMessage().c_str());
261     }
262     catch (...){
263         LOGE("get_TNF_UNCHANGED failed");
264     }
265     return JSValueMakeUndefined(context);
266 }
267
268 JSValueRef JSNFCManager::getDefaultAdapter (JSContextRef context, JSObjectRef object,
269         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
270         JSValueRef* exception)
271 {
272     SET_TIME_TRACER_ITEM(0);
273     LOGD("Entered");
274
275     TIZEN_CHECK_ACCESS(context, exception, &(NFCManager::getInstance()),
276             NFC_FUNCTION_API_COMMON_FUNCS);
277
278     try {
279         JSObjectRef js_object = JSNFCAdapter::makeJSObject(context,
280                 NFCManager::getInstance().getDefaultAdapter());
281         return js_object;
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);
286     } catch (...) {
287         LOGE("getDefaultAdapter failed");
288         return JSWebAPIErrorFactory::postException(context, exception,
289             JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
290     }
291 }
292
293 JSValueRef JSNFCManager::setExclusiveMode (JSContextRef context, JSObjectRef object,
294         JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
295         JSValueRef* exception)
296 {
297     SET_TIME_TRACER_ITEM(0);
298     LOGD("Entered");
299
300     TIZEN_CHECK_ACCESS(context, exception, &(NFCManager::getInstance()),
301             NFC_FUNCTION_API_COMMON_FUNCS);
302     try {
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);
310     } catch (...) {
311         LOGE("getDefaultAdapter failed");
312         return JSWebAPIErrorFactory::postException(context, exception,
313             JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
314     }
315
316     return JSValueMakeUndefined(context);
317 }
318
319 }
320 }