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 <SecurityExceptions.h>
19 #include <CallbackUserData.h>
20 #include <GlobalContextManager.h>
23 #include <JSWebAPIError.h>
24 #include <ArgumentValidator.h>
26 #include "JSPowerManager.h"
27 #include "plugin_config.h"
28 #include "PowerManager.h"
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
38 JSClassDefinition JSPowerManager::m_classInfo = {
40 kJSClassAttributeNone,
50 NULL, //DeleteProperty,
51 NULL, //GetPropertyNames,
52 NULL, //CallAsFunction,
53 NULL, //CallAsConstructor,
58 JSStaticFunction JSPowerManager::m_function[] = {
59 { POWER_FUNCTION_API_REQUEST, request, kJSPropertyAttributeNone },
60 { POWER_FUNCTION_API_RELEASE, release, kJSPropertyAttributeNone },
61 { POWER_FUNCTION_API_SET_SCREEN_STATE_CHANGE_LISTENER, setScreenStateChangeListener, kJSPropertyAttributeNone },
62 { POWER_FUNCTION_API_UNSET_SCREEN_STATE_CHANGE_LISTENER, unsetScreenStateChangeListener, kJSPropertyAttributeNone },
63 { POWER_FUNCTION_API_GET_SCREEN_BRIGHTNESS, getScreenBrightness, kJSPropertyAttributeNone },
64 { POWER_FUNCTION_API_SET_SCREEN_BRIGHTNESS, setScreenBrightness, kJSPropertyAttributeNone },
65 { POWER_FUNCTION_API_IS_SCREEN_ON, isScreenOn, kJSPropertyAttributeNone },
66 { POWER_FUNCTION_API_RESTORE_SCREEN_BRIGHTNESS, restoreScreenBrightness, kJSPropertyAttributeNone },
67 { POWER_FUNCTION_API_TURN_SCREEN_ON, turnScreenOn, kJSPropertyAttributeNone },
68 { POWER_FUNCTION_API_TURN_SCREEN_OFF, turnScreenOff, kJSPropertyAttributeNone },
73 JSClassRef JSPowerManager::m_jsClassRef = JSClassCreate(JSPowerManager::getClassInfo());
75 void JSPowerManager::initialize(JSContextRef context, JSObjectRef object)
79 void JSPowerManager::finalize(JSObjectRef object)
81 CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(object));
82 if( callback != NULL ){
83 PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
84 JSObjectSetPrivate(object, NULL);
89 const JSClassRef JSPowerManager::getClassRef()
92 m_jsClassRef = JSClassCreate(&m_classInfo);
97 const JSClassDefinition* JSPowerManager::getClassInfo()
102 JSValueRef JSPowerManager::request(JSContextRef context,
104 JSObjectRef thisObject,
105 size_t argumentCount,
106 const JSValueRef arguments[],
107 JSValueRef* exception)
111 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_REQUEST);
112 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
116 ArgumentValidator validator(context, argumentCount, arguments);
117 string resource_str = validator.toString(0);
118 string state_str = validator.toString(1);
119 PowerResource resource(resource_str.c_str());
120 PowerState state(state_str.c_str());
121 PowerManager::getInstance()->request( resource , state );
123 }catch(const BasePlatformException& err){
124 return JSWebAPIError::throwException(context, exception, err);
127 return JSValueMakeUndefined(context);
130 JSValueRef JSPowerManager::release(JSContextRef context,
132 JSObjectRef thisObject,
133 size_t argumentCount,
134 const JSValueRef arguments[],
135 JSValueRef* exception)
140 ArgumentValidator validator(context, argumentCount, arguments);
142 string resource_str = validator.toString(0);
143 PowerResource resource(resource_str.c_str());
144 PowerManager::getInstance()->release( resource );
146 }catch(const BasePlatformException& err){
147 return JSWebAPIError::throwException(context, exception, err);
149 return JSValueMakeUndefined(context);
152 JSValueRef JSPowerManager::setScreenStateChangeListener(JSContextRef context,
154 JSObjectRef thisObject,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
162 ArgumentValidator validator(context, argumentCount, arguments);
163 JSObjectRef func = validator.toFunction(0);
164 CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(thisObject));
165 if( callback == NULL ){
166 callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
167 JSObjectSetPrivate(thisObject, callback);
169 callback->setSuccessCallback(func);
170 PowerManager::getInstance()->addScreenStateChangedCallback(callback);
172 }catch(const BasePlatformException& err){
173 return JSWebAPIError::throwException(context, exception, err);
174 }catch( const std::bad_alloc& oom){
175 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Out of memory");
178 return JSValueMakeUndefined(context);
181 JSValueRef JSPowerManager::unsetScreenStateChangeListener(JSContextRef context,
183 JSObjectRef thisObject,
184 size_t argumentCount,
185 const JSValueRef arguments[],
186 JSValueRef* exception)
190 CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(thisObject));
191 if( callback != NULL ){
192 PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
193 JSObjectSetPrivate(thisObject, NULL);
196 return JSValueMakeUndefined(context);
199 JSValueRef JSPowerManager::getScreenBrightness(JSContextRef context,
201 JSObjectRef thisObject,
202 size_t argumentCount,
203 const JSValueRef arguments[],
204 JSValueRef* exception)
209 double brightness = PowerManager::getInstance()->getScreenBrightness();
210 return JSUtil::toJSValueRef(context, brightness);
211 }catch(const BasePlatformException& err){
212 return JSWebAPIError::throwException(context, exception, err);
216 JSValueRef JSPowerManager::setScreenBrightness(JSContextRef context,
218 JSObjectRef thisObject,
219 size_t argumentCount,
220 const JSValueRef arguments[],
221 JSValueRef* exception)
224 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
225 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
227 ArgumentValidator validator(context, argumentCount, arguments);
229 double value = validator.toDouble(0);
230 LOGE(" value = %f", value);
231 PowerManager::getInstance()->setScreenBrightness(value);
232 }catch(const BasePlatformException& err){
233 return JSWebAPIError::throwException(context, exception, err);
235 return JSValueMakeUndefined(context);
239 JSValueRef JSPowerManager::isScreenOn(JSContextRef context,
241 JSObjectRef thisObject,
242 size_t argumentCount,
243 const JSValueRef arguments[],
244 JSValueRef* exception)
249 bool state = PowerManager::getInstance()->isScreenOn();
250 return JSUtil::toJSValueRef(context,state);
251 }catch(const BasePlatformException& err){
252 return JSWebAPIError::throwException(context, exception, err);
256 JSValueRef JSPowerManager::restoreScreenBrightness(JSContextRef context,
258 JSObjectRef thisObject,
259 size_t argumentCount,
260 const JSValueRef arguments[],
261 JSValueRef* exception)
266 PowerManager::getInstance()->restoreScreenBrightness();
267 }catch(const BasePlatformException& err){
268 return JSWebAPIError::throwException(context, exception, err);
270 return JSValueMakeUndefined(context);}
272 JSValueRef JSPowerManager::turnScreenOn(JSContextRef context,
274 JSObjectRef thisObject,
275 size_t argumentCount,
276 const JSValueRef arguments[],
277 JSValueRef* exception)
280 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
281 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
284 PowerManager::getInstance()->setScreenState(true);
285 }catch(const BasePlatformException& err){
286 return JSWebAPIError::throwException(context, exception, err);
288 return JSValueMakeUndefined(context);
291 JSValueRef JSPowerManager::turnScreenOff(JSContextRef context,
293 JSObjectRef thisObject,
294 size_t argumentCount,
295 const JSValueRef arguments[],
296 JSValueRef* exception)
299 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
300 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
303 PowerManager::getInstance()->setScreenState(false);
304 }catch(const BasePlatformException& err){
305 return JSWebAPIError::throwException(context, exception, err);
307 return JSValueMakeUndefined(context);