5411d8e1ec1f749b0d8e011c87dc95a67f8830ae
[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     { "addPropertyValueChangeListener", JSSysteminfo::addPropertyValueChangeListener, kJSPropertyAttributeNone },
70     { "removePropertyValueChangeListener", JSSysteminfo::removePropertyValueChangeListener, 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
196     Try {
197         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])
198             || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
199             LogError("successCallback parameter is JSNull/JSUndefined");
200             Throw(InvalidArgumentException);
201         }
202         
203         ISysteminfoPtr Systeminfos(priv->getObject());
204         
205         EventGetSysteminfoPtr event(new EventGetSysteminfo());
206         
207         event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]));
208         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
209
210         SysteminfoListener& listener = SysteminfoListener::getInstance();
211         event->setForAsynchronousCall(&listener);
212
213         JSObjectRef pendingOperation = makePendingOperation(context, event);
214         Systeminfos->get(event);
215         
216         return pendingOperation;
217     }
218     
219     Catch(PendingOperationException) {
220         LogError("JSSysteminfo::get PendingOperationException");
221         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
222     }
223     Catch(ConversionException) {
224         LogError("JSSysteminfo::get ConversionException");
225         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
226     }
227     Catch(InvalidArgumentException) {
228         LogError("JSSysteminfo::get InvalidArgumentException");
229         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
230     }
231     Catch(WrtDeviceApis::Commons::Exception) {
232         LogError("JSSysteminfo::get Exception");
233         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
234     }
235     return JSValueMakeUndefined(context);
236 }
237
238 JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
239         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
240 {
241     LogDebug("enter");
242     JSValueRef property = NULL;
243     WatchOption option;
244     int failId = -1;    
245     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
246
247         AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGE_LISTENER);
248         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
249
250     Converter converter(context);
251     Validator check(context, exception);
252
253     if (!priv) {
254         LogError("private object is null");
255         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
256     }
257
258     if (argumentCount == 0 || argumentCount > 4) {
259         LogError("wrong argument");
260         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
261     }
262
263     if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !check.isCallback(arguments[1])) {
264         LogError("wrong argument");
265         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
266     }
267
268     if (argumentCount > 2) {
269         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !check.isCallback(arguments[2])) {
270             LogError("wrong argument");
271             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
272         }
273     }
274
275     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
276     if (check.isCallback(arguments[1])) {
277         onSuccessForCbm = arguments[1];
278     }
279     if (argumentCount > 2) {
280         if (check.isCallback(arguments[2])) {
281             onErrorForCbm = arguments[2];
282         }
283     }
284     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
285
286     Try {
287         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0])
288             || JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
289             LogError("successCallback parameter is JSNull/JSUndefined");
290             Throw(InvalidArgumentException);
291         }
292         property = arguments[0];
293         if (argumentCount > 3 && JSValueIsObject(context, arguments[3])) {
294             option.timeout = converter.toULong(JSUtils::getJSProperty(context, arguments[3], "timeout", exception));
295             option.highThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "highThreshold", exception));
296             option.lowThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[3], "lowThreshold", exception));
297         }
298         ISysteminfoPtr Systeminfos(priv->getObject());
299
300         EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
301         event->setWatchOption(option);
302         event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), property));
303         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
304
305         SysteminfoListener& listener = SysteminfoListener::getInstance();
306         event->setForAsynchronousCall(&listener);        
307             
308         Systeminfos->watch(event);
309         LogDebug("event->getId()" << event->getId());
310         return converter.toJSValueRef(event->getId());
311         }
312     
313     Catch(PendingOperationException) {
314         LogError("JSSysteminfo::get PendingOperationException");
315         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
316     }
317     Catch(ConversionException) {
318         LogError("JSSysteminfo::get ConversionException");
319         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
320     }
321     Catch(InvalidArgumentException) {
322         LogError("JSSysteminfo::get InvalidArgumentException");
323         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
324     }
325     Catch(WrtDeviceApis::Commons::Exception) {
326         LogError("JSSysteminfo::get Exception");
327         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
328     }
329     return JSValueMakeUndefined(context);    
330 }
331
332 JSValueRef JSSysteminfo::removePropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
333         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
334 {
335     int id = 0;
336     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));    
337
338         AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_VALUE_CHANGE_LISTENER);
339         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
340     
341     Converter converter(context);
342
343     if (argumentCount == 0) {
344         return JSValueMakeUndefined(context);
345     } else if (argumentCount != 1 || (argumentCount > 0 && JSValueIsObject(context, arguments[0]))) {
346         LogError("wrong argument");
347         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
348     }
349
350     Try {
351                 if (!priv) {
352                         ThrowMsg(NullPointerException, "No private object");
353                 }
354         ISysteminfoPtr Systeminfos(priv->getObject());
355
356                 if (argumentCount == 1) {
357                         id = static_cast<int>(converter.toInt(arguments[0]));
358                 }
359                 LogDebug("clearWatch id = " << id );
360                 Systeminfos->clearWatch(id);
361                 return JSValueMakeUndefined(context);
362         }
363         Catch (InvalidArgumentException) {
364         LogError("JSSysteminfo::get InvalidArgumentException");
365         }
366         Catch (WrtDeviceApis::Commons::Exception) {
367         LogError("JSSysteminfo::get Exception");
368     }
369
370     return JSValueMakeUndefined(context);
371 }
372
373 }
374 }