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 { "addPropertyValueChangeListener", JSSysteminfo::addPropertyValueChangeListener, kJSPropertyAttributeNone },
70 { "removePropertyValueChangeListener", JSSysteminfo::removePropertyValueChangeListener, 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 || argumentCount == 0) {
124 LogError("argument count is not correct");
125 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
130 ISysteminfoPtr Systeminfos(priv->getObject());
133 retVal = Systeminfos->isPropertyValid(priv->getContext(), arguments[0]);
135 return converter.toJSValueRef(retVal);
138 Catch(ConversionException) {
139 LogError("Error on conversion");
140 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
142 Catch(InvalidArgumentException) {
143 LogError("JSSysteminfo::get InvalidArgumentException");
144 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter");
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("argument count is not correct");
168 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value 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::TYPE_MISMATCH_ERROR, "type mismatch 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::TYPE_MISMATCH_ERROR, "type mismatch 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));
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);
203 ISysteminfoPtr Systeminfos(priv->getObject());
205 EventGetSysteminfoPtr event(new EventGetSysteminfo());
207 event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), arguments[0]));
208 event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
210 SysteminfoListener& listener = SysteminfoListener::getInstance();
211 event->setForAsynchronousCall(&listener);
213 JSObjectRef pendingOperation = makePendingOperation(context, event);
214 Systeminfos->get(event);
216 return pendingOperation;
219 Catch(PendingOperationException) {
220 LogError("JSSysteminfo::get PendingOperationException");
221 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
223 Catch(ConversionException) {
224 LogError("JSSysteminfo::get ConversionException");
225 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"));
227 Catch(InvalidArgumentException) {
228 LogError("JSSysteminfo::get InvalidArgumentException");
229 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
231 Catch(WrtDeviceApis::Commons::Exception) {
232 LogError("JSSysteminfo::get Exception");
233 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknown error"));
235 return JSValueMakeUndefined(context);
238 JSValueRef JSSysteminfo::addPropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
239 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
242 JSValueRef property = NULL;
245 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
247 AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_ADD_PROPERTY_VALUE_CHANGE_LISTENER);
248 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
250 Converter converter(context);
251 Validator check(context, exception);
254 LogError("private object is null");
255 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "unknown error");
258 if (argumentCount == 0 || argumentCount > 4) {
259 LogError("wrong argument");
260 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
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::TYPE_MISMATCH_ERROR, "type mismatch error");
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::TYPE_MISMATCH_ERROR, "type mismatch error");
275 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
276 if (check.isCallback(arguments[1])) {
277 onSuccessForCbm = arguments[1];
279 if (argumentCount > 2) {
280 if (check.isCallback(arguments[2])) {
281 onErrorForCbm = arguments[2];
284 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
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);
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));
298 ISysteminfoPtr Systeminfos(priv->getObject());
300 EventWatchSysteminfoPtr event(new EventWatchSysteminfo);
301 event->setWatchOption(option);
302 event->setBasePropertyPtr(Systeminfos->getBasePropertyPtr(priv->getContext(), property));
303 event->setPrivateData(StaticPointerCast<IEventPrivateData>(cbm));
305 SysteminfoListener& listener = SysteminfoListener::getInstance();
306 event->setForAsynchronousCall(&listener);
308 Systeminfos->watch(event);
309 LogDebug("event->getId()" << event->getId());
310 return converter.toJSValueRef(event->getId());
313 Catch(PendingOperationException) {
314 LogError("JSSysteminfo::get PendingOperationException");
315 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
317 Catch(ConversionException) {
318 LogError("JSSysteminfo::get ConversionException");
319 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error"));
321 Catch(InvalidArgumentException) {
322 LogError("JSSysteminfo::get InvalidArgumentException");
323 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter"));
325 Catch(WrtDeviceApis::Commons::Exception) {
326 LogError("JSSysteminfo::get Exception");
327 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknown error"));
329 return JSValueMakeUndefined(context);
332 JSValueRef JSSysteminfo::removePropertyValueChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
333 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
336 JSSysteminfoPriv *priv = static_cast<JSSysteminfoPriv*>(JSObjectGetPrivate(thisObject));
338 AceSecurityStatus status = SYSTEMINFO_CHECK_ACCESS(priv->getContext(),SYSTEMINFO_FUNCTION_API_REMOVE_PROPERTY_VALUE_CHANGE_LISTENER);
339 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
341 Converter converter(context);
343 if (argumentCount == 0 || argumentCount > 1) {
344 LogError("argument count is not correct");
345 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
346 } else if (!JSValueIsNumber(context, arguments[0])) {
347 LogError("wrong argument");
348 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
353 ThrowMsg(NullPointerException, "No private object");
355 ISysteminfoPtr Systeminfos(priv->getObject());
357 if (argumentCount == 1) {
358 id = static_cast<int>(converter.toInt(arguments[0]));
360 LogDebug("clearWatch id = " << id );
361 Systeminfos->clearWatch(id);
362 return JSValueMakeUndefined(context);
364 Catch (InvalidArgumentException) {
365 LogError("JSSysteminfo::get InvalidArgumentException");
367 Catch (WrtDeviceApis::Commons::Exception) {
368 LogError("JSSysteminfo::get Exception");
371 return JSValueMakeUndefined(context);