wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Systeminfo / JSSysteminfo.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 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 <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "SysteminfoFactory.h"
25 #include "EventGetSysteminfo.h"
26 #include "EventWatchSysteminfo.h"
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include <SecurityExceptions.h>
30 #include <TimeTracer.h>
31 #include "SysteminfoListener.h"
32 #include "SysteminfoAsyncCallbackManager.h"
33 #include "SysteminfoListenerManager.h"
34 #include "JSSysteminfo.h"
35
36 namespace DeviceAPI {
37 namespace Systeminfo {
38
39 using namespace std;
40 using namespace DPL;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace WrtDeviceApis::Commons;
43 using namespace DeviceAPI::Common;
44
45 JSClassDefinition JSSysteminfo::m_classInfo = {
46     0,
47     kJSClassAttributeNone,
48     "systeminfo",
49     NULL,
50     NULL,
51     m_function,
52     initialize,
53     finalize,
54     NULL, //hasProperty,
55     NULL, //getProperty,
56     NULL, //setProperty,
57     NULL, //deleteProperty,
58     NULL, //getPropertyNames,
59     NULL,
60     NULL,
61     NULL,
62     NULL
63 };
64
65 JSStaticFunction JSSysteminfo::m_function[] = {
66     { "getCapabilities", JSSysteminfo::getCapabilities, kJSPropertyAttributeNone },    
67     { "getPropertyValue", JSSysteminfo::getPropertyValue, kJSPropertyAttributeNone },
68     { "addPropertyValueChangeListener", JSSysteminfo::addPropertyValueChangeListener, kJSPropertyAttributeNone },
69     { "removePropertyValueChangeListener", JSSysteminfo::removePropertyValueChangeListener, kJSPropertyAttributeNone },
70     { 0, 0, 0 }
71 };
72
73 const JSClassRef JSSysteminfo::getClassRef()
74 {
75     if (!m_jsClassRef) {
76         m_jsClassRef = JSClassCreate(&m_classInfo);
77     }
78     return m_jsClassRef;
79 }
80
81 const JSClassDefinition* JSSysteminfo::getClassInfo()
82 {
83     return &m_classInfo;
84 }
85
86 JSClassRef JSSysteminfo::m_jsClassRef = JSClassCreate(JSSysteminfo::getClassInfo());
87
88 void JSSysteminfo::initialize(JSContextRef context, JSObjectRef object)
89 {
90     if (!JSObjectGetPrivate(object)) {
91         ISysteminfoPtr Systeminfos(SysteminfoFactory::getInstance().getSysteminfos());
92         JSSysteminfoPriv* priv = new JSSysteminfoPriv(context, Systeminfos);
93         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
94             LoggerE("Object can't store private data.");
95             delete priv;
96         }
97
98         LoggerD("JSSysteminfo::initialize ");
99     } else {
100         LoggerD("Private object already set.");
101     }
102 }
103
104 void JSSysteminfo::finalize(JSObjectRef object)
105 {
106     JSSysteminfoPriv* priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(object));
107     
108     JSObjectSetPrivate(object, NULL);
109     LoggerD("Deleting gallery");
110     delete priv;
111 }
112
113 JSValueRef JSSysteminfo::getCapabilities(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
114         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
115 {
116     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
117
118     Converter converter(context);
119     Validator check(context, exception);
120
121     if (!priv) {
122         LoggerE("private object is null");
123         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
124     }
125     Try
126     {
127         ISysteminfoPtr Systeminfos(priv->getObject());
128
129         DeviceCapabilitiesPropertiesPtr result(new DeviceCapabilitiesProperties());
130
131         result = Systeminfos->getCapabilities();
132
133         return (static_cast<JSValueRef>(JSDeviceCapabilitiesInfo::createJSObject(context, result)));
134     }
135     Catch(WrtDeviceApis::Commons::ConversionException) {
136         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
137     }
138     Catch(WrtDeviceApis::Commons::InvalidArgumentException){
139         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
140     }
141     Catch(WrtDeviceApis::Commons::Exception) {
142         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
143     }    
144 }
145
146 JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
147         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
148 {
149     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
150
151     Converter converter(context);
152     Validator check(context, exception);
153     std::string property;
154
155     if (!priv) {
156         LoggerE("private object is null");
157         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
158     }
159     if (argumentCount < 2) {
160         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
161     }
162     if (!check.isCallback(arguments[1])) {
163         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
164     }
165
166     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
167     onSuccessForCbm = arguments[1];
168     if (argumentCount == 3) {
169         if (check.isCallback(arguments[2])) {
170             onErrorForCbm = arguments[2];
171         } else if (!JSValueIsNull(context, arguments[2])) {
172             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
173         }
174     }
175     
176     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
177     cbm->setObject(thisObject); 
178
179     Try {
180         ISysteminfoPtr Systeminfos(priv->getObject());
181         BasePropertyPtr baseProperty = Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]);
182         
183         EventGetSysteminfoPtr event(new EventGetSysteminfo());
184         event->setBasePropertyPtr(baseProperty);
185                 TIME_TRACER_ITEM_BEGIN(event->getProperty(), 0);
186         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
187
188         SysteminfoListener& listener = SysteminfoListener::getInstance();
189         event->setForAsynchronousCall(&listener);
190
191         Systeminfos->get(event);
192         SysteminfoAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, priv->getContext());
193
194         return JSValueMakeUndefined(context);
195     }
196     
197     Catch(WrtDeviceApis::Commons::ConversionException) {
198         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
199     }
200     Catch(WrtDeviceApis::Commons::Exception) {
201         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown error"));
202     }
203     return JSValueMakeUndefined(context);
204 }
205
206 JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
207         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
208 {
209     WatchOption option;
210     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
211
212     Converter converter(context);
213     Validator check(context, exception);
214
215     if (!priv) {
216         LoggerE("private object is null");
217         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
218     }
219     if (argumentCount < 2) {
220         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
221     }
222     if (!check.isCallback(arguments[1])) {
223         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
224     }
225     
226     JSValueRef onSuccessForCbm = NULL;
227     onSuccessForCbm = arguments[1];
228
229     JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, NULL, true, true));
230
231     Try {
232         if (argumentCount > 2) {
233             if (JSValueIsObject(context, arguments[2])) {
234                 option.timeout = converter.toULong(JSUtils::getJSProperty(context, arguments[2], "timeout", exception));
235                 option.highThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[2], "highThreshold", exception));
236                 option.lowThreshold = converter.toDouble(JSUtils::getJSProperty(context, arguments[2], "lowThreshold", exception));
237             } else if (!JSValueIsNull(context, arguments[2])) {
238                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");        
239             }
240         }
241         ISysteminfoPtr Systeminfos(priv->getObject());
242         BasePropertyPtr baseProperty = Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]);
243
244         EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
245         event->setWatchOption(option);
246         event->setBasePropertyPtr(baseProperty);
247         event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
248
249         SysteminfoListener& listener = SysteminfoListener::getInstance();
250         event->setForAsynchronousCall(&listener);        
251             
252         Systeminfos->watch(event);
253         LoggerD("event->getId()" << event->getId());
254
255         SysteminfoListenerCancellerPtr canceller = SysteminfoListenerCancellerPtr(new SysteminfoListenerCanceller(priv->getContext(), thisObject, event->getId()));
256         DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
257         SysteminfoListenerManagerSingleton::Instance().registerListener(listenerItem, priv->getContext());              
258         
259         return converter.toJSValueRef(event->getId());
260     }
261
262     Catch(WrtDeviceApis::Commons::ConversionException) {
263         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
264     }
265     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
266         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
267     }
268     Catch(WrtDeviceApis::Commons::Exception) {
269         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
270     }
271 }
272
273 JSValueRef JSSysteminfo::removePropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
274         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
275 {
276     long id = 0;
277     JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
278
279     Converter converter(context);
280     if (!priv) {
281         LoggerE("private object is null");
282         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
283     }
284     if (argumentCount < 1) {
285         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
286     }
287
288     Try {
289         ISysteminfoPtr Systeminfos(priv->getObject());
290         id = static_cast<long>(converter.toLong(arguments[0]));
291
292                 LoggerD("clearWatch id = " << id );
293                 Systeminfos->clearWatch(id);
294
295                 SysteminfoListenerCancellerPtr canceller = SysteminfoListenerCancellerPtr(new SysteminfoListenerCanceller(priv->getContext(), thisObject, id));
296                 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
297                 SysteminfoListenerManagerSingleton::Instance().unregisterListener(listenerItem);
298                 
299                 return JSValueMakeUndefined(context);
300         }
301     Catch(WrtDeviceApis::Commons::ConversionException) {
302         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
303     }    
304     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
305         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
306     }
307     Catch(WrtDeviceApis::Commons::Exception) {
308         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");        
309     }
310 }
311
312 }
313 }