3 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
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>
33 #include "JSSysteminfo.h"
34 #include "plugin_config.h"
41 using namespace WrtDeviceApis::CommonsJavaScript;
42 using namespace WrtDeviceApis::Commons;
43 using namespace Api::Systeminfo;
44 using namespace TizenApis::Commons;
46 JSClassDefinition JSSysteminfo::m_classInfo = {
48 kJSClassAttributeNone,
58 NULL, //deleteProperty,
59 NULL, //getPropertyNames,
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 },
74 const JSClassRef JSSysteminfo::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 const JSClassDefinition* JSSysteminfo::getClassInfo()
87 JSClassRef JSSysteminfo::m_jsClassRef = JSClassCreate(JSSysteminfo::getClassInfo());
89 void JSSysteminfo::initialize(JSContextRef context, JSObjectRef object)
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.");
100 LogDebug("JSSysteminfo::initialize ");
103 void JSSysteminfo::finalize(JSObjectRef object)
105 JSSysteminfoPriv* priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(object));
107 JSObjectSetPrivate(object, NULL);
108 LogDebug("Deleting gallery");
112 JSValueRef JSSysteminfo::isSupported(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
113 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
115 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
117 AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_IS_SUPPORTED);
118 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
120 Converter converter(context);
121 Validator check(context, exception);
123 if (argumentCount < 1) {
124 LogError("wrong argument");
125 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
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");
134 ISysteminfoPtr Systeminfos(priv->getObject());
137 retVal = Systeminfos->isPropertyValid(priv->getContext(), arguments[0]);
139 return converter.toJSValueRef(retVal);
142 Catch(ConversionException) {
143 LogError("Error on conversion");
144 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
147 return JSValueMakeUndefined(context);
150 JSValueRef JSSysteminfo::getPropertyValue(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
151 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
153 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
155 AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_GET_PROPERTY_VALUE);
156 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
158 Converter converter(context);
159 Validator check(context, exception);
162 LogError("private object is null");
163 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
166 if (argumentCount == 0 || argumentCount > 4) {
167 LogError("wrong argument");
168 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
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");
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");
183 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
184 if (check.isCallback(arguments[1])) {
185 onSuccessForCbm = arguments[1];
187 if (argumentCount > 2) {
188 if (check.isCallback(arguments[2])) {
189 onErrorForCbm = arguments[2];
193 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
195 cbm->setObject(thisObject);
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);
204 ISysteminfoPtr Systeminfos(priv->getObject());
206 EventGetSysteminfoPtr event(new EventGetSysteminfo());
208 event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]));
209 event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
211 SysteminfoListener& listener = SysteminfoListener::getInstance();
212 event->setForAsynchronousCall(&listener);
214 JSObjectRef pendingOperation = makePendingOperation(context, event);
215 Systeminfos->get(event);
217 return pendingOperation;
220 Catch(PendingOperationException) {
221 LogError("JSSysteminfo::get PendingOperationException");
222 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
224 Catch(ConversionException) {
225 LogError("JSSysteminfo::get ConversionException");
226 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
228 Catch(InvalidArgumentException) {
229 LogError("JSSysteminfo::get InvalidArgumentException");
230 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
232 Catch(WrtDeviceApis::Commons::Exception) {
233 LogError("JSSysteminfo::get Exception");
234 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
236 return JSValueMakeUndefined(context);
239 JSValueRef JSSysteminfo::addPropertyValueChangedListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
240 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
243 JSValueRef property = NULL;
246 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
248 AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGED_LISTENER);
249 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
251 Converter converter(context);
252 Validator check(context, exception);
255 LogError("private object is null");
256 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
259 if (argumentCount == 0 || argumentCount > 4) {
260 LogError("wrong argument");
261 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
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");
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");
276 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
277 if (check.isCallback(arguments[1])) {
278 onSuccessForCbm = arguments[1];
280 if (argumentCount > 2) {
281 if (check.isCallback(arguments[2])) {
282 onErrorForCbm = arguments[2];
285 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
286 cbm->setObject(thisObject);
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);
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));
300 ISysteminfoPtr Systeminfos(priv->getObject());
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);
306 EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
307 event->setWatchOption(option);
308 event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), property));
309 event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
311 SysteminfoListener& listener = SysteminfoListener::getInstance();
312 event->setForAsynchronousCall(&listener);
314 Systeminfos->watch(event);
315 return converter.toJSValueRef(event->getId());
319 Catch(PendingOperationException) {
320 LogError("JSSysteminfo::get PendingOperationException");
321 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
323 Catch(ConversionException) {
324 LogError("JSSysteminfo::get ConversionException");
325 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
327 Catch(InvalidArgumentException) {
328 LogError("JSSysteminfo::get InvalidArgumentException");
329 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
331 Catch(WrtDeviceApis::Commons::Exception) {
332 LogError("JSSysteminfo::get Exception");
333 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"invalid parameter"));
335 return JSValueMakeUndefined(context);
338 JSValueRef JSSysteminfo::removePropertyChangedListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
339 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
342 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
344 AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_CHANGED_LISTENER);
345 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
347 Converter converter(context);
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");
358 ThrowMsg(NullPointerException, "No private object");
360 ISysteminfoPtr Systeminfos(priv->getObject());
362 if (argumentCount == 1) {
363 id = static_cast<int>(converter.toInt(arguments[0]));
365 LogDebug("clearWatch id = " << id );
366 Systeminfos->clearWatch(id);
367 return JSValueMakeUndefined(context);
369 Catch (InvalidArgumentException) {
370 LogError("JSSysteminfo::get InvalidArgumentException");
372 Catch (WrtDeviceApis::Commons::Exception) {
373 LogError("JSSysteminfo::get Exception");
376 return JSValueMakeUndefined(context);