Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Systeminfo / JSSysteminfo.cpp
1 /*
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
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
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/ScopedJSStringRef.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <CommonsJavaScript/Converter.h>
25 #include <API/Systeminfo/SysteminfoFactory.h>
26 #include <API/Systeminfo/EventGetSysteminfo.h>
27 #include <API/Systeminfo/EventWatchSysteminfo.h>
28 #include <API/Systeminfo/SysteminfoListener.h>
29 #include <Tizen/Common/JSTizenExceptionFactory.h>
30 #include <Tizen/Common/JSTizenException.h>
31 #include <Tizen/Common/SecurityExceptions.h>
32
33 #include "JSSysteminfo.h"
34 #include "plugin_config.h"
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38
39 using namespace std;
40 using namespace DPL;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace WrtDeviceApis::Commons;
43 using namespace Api::Systeminfo;
44 using namespace TizenApis::Commons;
45     
46 JSClassDefinition JSSysteminfo::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     "systeminfo",
50     NULL,
51     NULL,
52     m_function,
53     initialize,
54     finalize,
55     NULL, //hasProperty,
56     NULL, //getProperty,
57     NULL, //setProperty,
58     NULL, //deleteProperty,
59     NULL, //getPropertyNames,
60     NULL,
61     NULL,
62     NULL,
63     NULL
64 };
65
66 JSStaticFunction JSSysteminfo::m_function[] = {
67     { "isSupported", JSSysteminfo::isSupported, kJSPropertyAttributeNone },    
68     { "getPropertyValue", JSSysteminfo::getPropertyValue, kJSPropertyAttributeNone },
69     { "addPropertyValueChangedListener", JSSysteminfo::addPropertyValueChangedListener, kJSPropertyAttributeNone },
70     { "removePropertyChangedListener", JSSysteminfo::removePropertyChangedListener, kJSPropertyAttributeNone },
71     { 0, 0, 0 }
72 };
73
74 const JSClassRef JSSysteminfo::getClassRef()
75 {
76     if (!m_jsClassRef) {
77         m_jsClassRef = JSClassCreate(&m_classInfo);
78     }
79     return m_jsClassRef;
80 }
81
82 const JSClassDefinition* JSSysteminfo::getClassInfo()
83 {
84     return &m_classInfo;
85 }
86
87 JSClassRef JSSysteminfo::m_jsClassRef = JSClassCreate(JSSysteminfo::getClassInfo());
88
89 void JSSysteminfo::initialize(JSContextRef context, JSObjectRef object)
90 {
91     JSSysteminfoPriv* priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(object));
92     assert(!priv && "Invalid object creation.");
93     ISysteminfoPtr Systeminfos(SysteminfoFactory::getInstance().getSysteminfos());
94     priv = new JSSysteminfoPriv(context, Systeminfos);
95     if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
96         LogError("Object can't store private data.");
97         delete priv;
98     }
99
100     LogDebug("JSSysteminfo::initialize ");
101 }
102
103 void JSSysteminfo::finalize(JSObjectRef object)
104 {
105     JSSysteminfoPriv* priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(object));
106     
107     JSObjectSetPrivate(object, NULL);
108     LogDebug("Deleting gallery");
109     delete priv;
110 }
111
112 JSValueRef JSSysteminfo::isSupported(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
113         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
114 {
115     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
116
117         AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_IS_SUPPORTED);
118         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
119
120     Converter converter(context);
121     Validator check(context, exception);
122     
123     if (argumentCount < 1) {
124         LogError("wrong argument");
125         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
126     }
127     if (argumentCount > 0 && JSValueIsObject(context, arguments[0])) {
128         LogError("wrong argument");
129         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
130     }    
131
132     Try
133     {
134         ISysteminfoPtr Systeminfos(priv->getObject());
135
136         bool retVal;
137         retVal = Systeminfos->isPropertyValid(priv->getContext(), arguments[0]);
138
139         return converter.toJSValueRef(retVal);
140     }
141
142     Catch(ConversionException) {
143         LogError("Error on conversion");
144         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
145     }
146
147     return JSValueMakeUndefined(context);
148 }
149
150 JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
151         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
152 {
153     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
154
155         AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_GET_PROPERTY_VALUE);
156         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
157     
158     Converter converter(context);
159     Validator check(context, exception);
160
161     if (!priv) {
162         LogError("private object is null");
163         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
164     }
165
166     if (argumentCount == 0 || argumentCount > 4) {
167         LogError("wrong argument");
168         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
169     }
170
171     if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) {
172         LogError("wrong argument");
173         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
174     }
175     
176     if (argumentCount > 2) {
177         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) {
178             LogError("wrong argument");
179             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
180         }
181     }
182
183     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
184     if (check.isCallback(arguments[1])) {
185         onSuccessForCbm = arguments[1];
186     }
187     if (argumentCount > 2) {
188         if (check.isCallback(arguments[2])) {
189             onErrorForCbm = arguments[2];
190         }
191     }
192     
193     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
194
195     cbm->setObject(thisObject);
196
197     Try {
198         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])
199             || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
200             LogError("successCallback parameter is JSNull/JSUndefined");
201             Throw(InvalidArgumentException);
202         }
203         
204         ISysteminfoPtr Systeminfos(priv->getObject());
205         
206         EventGetSysteminfoPtr event(new EventGetSysteminfo());
207         
208         event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]));
209         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
210
211         SysteminfoListener& listener = SysteminfoListener::getInstance();
212         event->setForAsynchronousCall(&listener);
213
214         JSObjectRef pendingOperation = makePendingOperation(context, event);
215         Systeminfos->get(event);
216         
217         return pendingOperation;
218     }
219     
220     Catch(PendingOperationException) {
221         LogError("JSSysteminfo::get PendingOperationException");
222         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
223     }
224     Catch(ConversionException) {
225         LogError("JSSysteminfo::get ConversionException");
226         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
227     }
228     Catch(InvalidArgumentException) {
229         LogError("JSSysteminfo::get InvalidArgumentException");
230         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
231     }
232     Catch(WrtDeviceApis::Commons::Exception) {
233         LogError("JSSysteminfo::get Exception");
234         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
235     }
236     return JSValueMakeUndefined(context);
237 }
238
239 JSValueRef JSSysteminfo::addPropertyValueChangedListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
240         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
241 {
242     LogDebug("enter");
243     JSValueRef property = NULL;
244     WatchOption option;
245     int failId = -1;    
246     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
247
248         AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGED_LISTENER);
249         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
250
251     Converter converter(context);
252     Validator check(context, exception);
253
254     if (!priv) {
255         LogError("private object is null");
256         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
257     }
258
259     if (argumentCount == 0 || argumentCount > 4) {
260         LogError("wrong argument");
261         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
262     }
263
264     if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) {
265         LogError("wrong argument");
266         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
267     }
268
269     if (argumentCount > 2) {
270         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) {
271             LogError("wrong argument");
272             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
273         }
274     }
275
276     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
277     if (check.isCallback(arguments[1])) {
278         onSuccessForCbm = arguments[1];
279     }
280     if (argumentCount > 2) {
281         if (check.isCallback(arguments[2])) {
282             onErrorForCbm = arguments[2];
283         }
284     }
285     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
286     cbm->setObject(thisObject);
287
288     Try {
289         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])
290             || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
291             LogError("successCallback parameter is JSNull/JSUndefined");
292             Throw(InvalidArgumentException);
293         }
294         property = arguments[0];
295         if (argumentCount > 3 && JSValueIsObject(context, arguments[3])) {
296             option.timeout = converter.toULong(JSUtils::getJSProperty(context, arguments[3], "timeout", exception));
297             option.highThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "highThreshold", exception));
298             option.lowThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "lowThreshold", exception));
299         }
300         ISysteminfoPtr Systeminfos(priv->getObject());
301
302         if(Systeminfos->isDuplicateWatch((const char *)converter.toString(property).c_str())) {
303             cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
304             return converter.toJSValueRef(failId);
305         } else {
306             EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
307             event->setWatchOption(option);
308             event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), property));
309             event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
310
311             SysteminfoListener& listener = SysteminfoListener::getInstance();
312             event->setForAsynchronousCall(&listener);        
313             
314             Systeminfos->watch(event);
315             return converter.toJSValueRef(event->getId());
316         }
317     }
318     
319     Catch(PendingOperationException) {
320         LogError("JSSysteminfo::get PendingOperationException");
321         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
322     }
323     Catch(ConversionException) {
324         LogError("JSSysteminfo::get ConversionException");
325         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
326     }
327     Catch(InvalidArgumentException) {
328         LogError("JSSysteminfo::get InvalidArgumentException");
329         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
330     }
331     Catch(WrtDeviceApis::Commons::Exception) {
332         LogError("JSSysteminfo::get Exception");
333         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
334     }
335     return JSValueMakeUndefined(context);    
336 }
337
338 JSValueRef JSSysteminfo::removePropertyChangedListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
339         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
340 {
341     int id = 0;
342     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));    
343
344         AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_CHANGED_LISTENER);
345         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
346     
347     Converter converter(context);
348
349     if (argumentCount == 0) {
350         return JSValueMakeUndefined(context);
351     } else if (argumentCount != 1 || (argumentCount > 0 && JSValueIsObject(context, arguments[0]))) {
352         LogError("wrong argument");
353         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
354     }
355
356     Try {
357                 if (!priv) {
358                         ThrowMsg(NullPointerException, "No private object");
359                 }
360         ISysteminfoPtr Systeminfos(priv->getObject());
361
362                 if (argumentCount == 1) {
363                         id = static_cast<int>(converter.toInt(arguments[0]));
364                 }
365                 LogDebug("clearWatch id = " << id );
366                 Systeminfos->clearWatch(id);
367                 return JSValueMakeUndefined(context);
368         }
369         Catch (InvalidArgumentException) {
370         LogError("JSSysteminfo::get InvalidArgumentException");
371         }
372         Catch (WrtDeviceApis::Commons::Exception) {
373         LogError("JSSysteminfo::get Exception");
374     }
375
376     return JSValueMakeUndefined(context);
377 }
378
379 }
380 }