2 // Tizen Web Device API
3 // Copyright (c) 2012 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 <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"
37 namespace Systeminfo {
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace WrtDeviceApis::Commons;
43 using namespace DeviceAPI::Common;
45 JSClassDefinition JSSysteminfo::m_classInfo = {
47 kJSClassAttributeNone,
57 NULL, //deleteProperty,
58 NULL, //getPropertyNames,
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 },
73 const JSClassRef JSSysteminfo::getClassRef()
76 m_jsClassRef = JSClassCreate(&m_classInfo);
81 const JSClassDefinition* JSSysteminfo::getClassInfo()
86 JSClassRef JSSysteminfo::m_jsClassRef = JSClassCreate(JSSysteminfo::getClassInfo());
88 void JSSysteminfo::initialize(JSContextRef context, JSObjectRef object)
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.");
98 LoggerD("JSSysteminfo::initialize ");
100 LoggerD("Private object already set.");
104 void JSSysteminfo::finalize(JSObjectRef object)
106 JSSysteminfoPriv* priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(object));
108 JSObjectSetPrivate(object, NULL);
109 LoggerD("Deleting gallery");
113 JSValueRef JSSysteminfo::getCapabilities(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
114 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
116 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
118 Converter converter(context);
119 Validator check(context, exception);
122 LoggerE("private object is null");
123 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
127 ISysteminfoPtr Systeminfos(priv->getObject());
129 DeviceCapabilitiesPropertiesPtr result(new DeviceCapabilitiesProperties());
131 result = Systeminfos->getCapabilities();
133 return (static_cast<JSValueRef>(JSDeviceCapabilitiesInfo::createJSObject(context, result)));
135 Catch(WrtDeviceApis::Commons::ConversionException) {
136 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
138 Catch(WrtDeviceApis::Commons::InvalidArgumentException){
139 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
141 Catch(WrtDeviceApis::Commons::Exception) {
142 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
146 JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
147 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
149 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
151 Converter converter(context);
152 Validator check(context, exception);
153 std::string property;
156 LoggerE("private object is null");
157 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
159 if (argumentCount < 2) {
160 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
162 if (!check.isCallback(arguments[1])) {
163 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
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");
176 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
177 cbm->setObject(thisObject);
180 ISysteminfoPtr Systeminfos(priv->getObject());
181 BasePropertyPtr baseProperty = Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]);
183 EventGetSysteminfoPtr event(new EventGetSysteminfo());
184 event->setBasePropertyPtr(baseProperty);
185 TIME_TRACER_ITEM_BEGIN(event->getProperty(), 0);
186 event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
188 SysteminfoListener& listener = SysteminfoListener::getInstance();
189 event->setForAsynchronousCall(&listener);
191 Systeminfos->get(event);
192 SysteminfoAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, priv->getContext());
194 return JSValueMakeUndefined(context);
197 Catch(WrtDeviceApis::Commons::ConversionException) {
198 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
200 Catch(WrtDeviceApis::Commons::Exception) {
201 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown error"));
203 return JSValueMakeUndefined(context);
206 JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
207 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
210 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
212 Converter converter(context);
213 Validator check(context, exception);
216 LoggerE("private object is null");
217 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
219 if (argumentCount < 2) {
220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
222 if (!check.isCallback(arguments[1])) {
223 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
226 JSValueRef onSuccessForCbm = NULL;
227 onSuccessForCbm = arguments[1];
229 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, NULL, true, true));
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");
241 ISysteminfoPtr Systeminfos(priv->getObject());
242 BasePropertyPtr baseProperty = Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]);
244 EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
245 event->setWatchOption(option);
246 event->setBasePropertyPtr(baseProperty);
247 event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
249 SysteminfoListener& listener = SysteminfoListener::getInstance();
250 event->setForAsynchronousCall(&listener);
252 Systeminfos->watch(event);
253 LoggerD("event->getId()" << event->getId());
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());
259 return converter.toJSValueRef(event->getId());
262 Catch(WrtDeviceApis::Commons::ConversionException) {
263 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
265 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
266 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
268 Catch(WrtDeviceApis::Commons::Exception) {
269 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
273 JSValueRef JSSysteminfo::removePropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
274 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
277 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
279 Converter converter(context);
281 LoggerE("private object is null");
282 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
284 if (argumentCount < 1) {
285 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
289 ISysteminfoPtr Systeminfos(priv->getObject());
290 id = static_cast<long>(converter.toLong(arguments[0]));
292 LoggerD("clearWatch id = " << id );
293 Systeminfos->clearWatch(id);
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);
299 return JSValueMakeUndefined(context);
301 Catch(WrtDeviceApis::Commons::ConversionException) {
302 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
304 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
305 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
307 Catch(WrtDeviceApis::Commons::Exception) {
308 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");