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>
22 #include <JSWebAPIException.h>
23 #include <ArgumentValidator.h>
24 #include <TimeTracer.h>
26 #include "JSPowerManager.h"
27 #include "plugin_config.h"
28 #include "PowerManager.h"
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace DeviceAPI::Common;
39 JSClassDefinition JSPowerManager::m_classInfo = {
41 kJSClassAttributeNone,
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
59 JSStaticFunction JSPowerManager::m_function[] = {
60 { POWER_FUNCTION_API_REQUEST, request, kJSPropertyAttributeNone },
61 { POWER_FUNCTION_API_RELEASE, release, kJSPropertyAttributeNone },
62 { POWER_FUNCTION_API_SET_SCREEN_STATE_CHANGE_LISTENER, setScreenStateChangeListener, kJSPropertyAttributeNone },
63 { POWER_FUNCTION_API_UNSET_SCREEN_STATE_CHANGE_LISTENER, unsetScreenStateChangeListener, kJSPropertyAttributeNone },
64 { POWER_FUNCTION_API_GET_SCREEN_BRIGHTNESS, getScreenBrightness, kJSPropertyAttributeNone },
65 { POWER_FUNCTION_API_SET_SCREEN_BRIGHTNESS, setScreenBrightness, kJSPropertyAttributeNone },
66 { POWER_FUNCTION_API_IS_SCREEN_ON, isScreenOn, kJSPropertyAttributeNone },
67 { POWER_FUNCTION_API_RESTORE_SCREEN_BRIGHTNESS, restoreScreenBrightness, kJSPropertyAttributeNone },
68 { POWER_FUNCTION_API_TURN_SCREEN_ON, turnScreenOn, kJSPropertyAttributeNone },
69 { POWER_FUNCTION_API_TURN_SCREEN_OFF, turnScreenOff, kJSPropertyAttributeNone },
74 JSClassRef JSPowerManager::m_jsClassRef = JSClassCreate(JSPowerManager::getClassInfo());
76 void JSPowerManager::initialize(JSContextRef context, JSObjectRef object)
80 void JSPowerManager::finalize(JSObjectRef object)
82 CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(object));
83 if( callback != NULL ){
84 PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
85 JSObjectSetPrivate(object, NULL);
90 const JSClassRef JSPowerManager::getClassRef()
93 m_jsClassRef = JSClassCreate(&m_classInfo);
98 const JSClassDefinition* JSPowerManager::getClassInfo()
103 JSValueRef JSPowerManager::request(JSContextRef context,
105 JSObjectRef thisObject,
106 size_t argumentCount,
107 const JSValueRef arguments[],
108 JSValueRef* exception)
111 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
113 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_REQUEST);
114 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
117 ArgumentValidator validator(context, argumentCount, arguments);
118 string resource_str = validator.toString(0);
119 string state_str = validator.toString(1);
120 PowerResource resource(resource_str.c_str());
121 PowerState state(state_str.c_str());
122 PowerManager::getInstance()->request( resource , state );
123 }catch(const BasePlatformException& err){
124 return JSWebAPIException::throwException(context, exception, err);
127 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
128 return JSValueMakeUndefined(context);
131 JSValueRef JSPowerManager::release(JSContextRef context,
133 JSObjectRef thisObject,
134 size_t argumentCount,
135 const JSValueRef arguments[],
136 JSValueRef* exception)
139 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
142 ArgumentValidator validator(context, argumentCount, arguments);
144 string resource_str = validator.toString(0);
145 PowerResource resource(resource_str.c_str());
146 PowerManager::getInstance()->release( resource );
148 }catch(const BasePlatformException& err){
149 return JSWebAPIException::throwException(context, exception, err);
151 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
152 return JSValueMakeUndefined(context);
155 JSValueRef JSPowerManager::setScreenStateChangeListener(JSContextRef context,
157 JSObjectRef thisObject,
158 size_t argumentCount,
159 const JSValueRef arguments[],
160 JSValueRef* exception)
163 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
166 ArgumentValidator validator(context, argumentCount, arguments);
167 JSObjectRef func = validator.toFunction(0);
168 CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(thisObject));
169 if( callback == NULL ){
170 callback = new CallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context));
171 JSObjectSetPrivate(thisObject, callback);
173 callback->setSuccessCallback(func);
174 PowerManager::getInstance()->addScreenStateChangedCallback(callback);
176 }catch(const BasePlatformException& err){
177 return JSWebAPIException::throwException(context, exception, err);
178 }catch( const std::bad_alloc& oom){
179 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Out of memory");
182 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
183 return JSValueMakeUndefined(context);
186 JSValueRef JSPowerManager::unsetScreenStateChangeListener(JSContextRef context,
188 JSObjectRef thisObject,
189 size_t argumentCount,
190 const JSValueRef arguments[],
191 JSValueRef* exception)
194 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
196 CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(thisObject));
197 if( callback != NULL ){
198 PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
199 JSObjectSetPrivate(thisObject, NULL);
202 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
203 return JSValueMakeUndefined(context);
206 JSValueRef JSPowerManager::getScreenBrightness(JSContextRef context,
208 JSObjectRef thisObject,
209 size_t argumentCount,
210 const JSValueRef arguments[],
211 JSValueRef* exception)
214 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
217 double brightness = PowerManager::getInstance()->getScreenBrightness();
218 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
219 return JSUtil::toJSValueRef(context, brightness);
220 }catch(const BasePlatformException& err){
221 return JSWebAPIException::throwException(context, exception, err);
225 JSValueRef JSPowerManager::setScreenBrightness(JSContextRef context,
227 JSObjectRef thisObject,
228 size_t argumentCount,
229 const JSValueRef arguments[],
230 JSValueRef* exception)
233 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
234 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
235 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
237 ArgumentValidator validator(context, argumentCount, arguments);
239 double value = validator.toDouble(0);
240 LOGE(" value = %f", value);
241 PowerManager::getInstance()->setScreenBrightness(value);
242 }catch(const BasePlatformException& err){
243 return JSWebAPIException::throwException(context, exception, err);
245 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
246 return JSValueMakeUndefined(context);
250 JSValueRef JSPowerManager::isScreenOn(JSContextRef context,
252 JSObjectRef thisObject,
253 size_t argumentCount,
254 const JSValueRef arguments[],
255 JSValueRef* exception)
258 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
261 bool state = PowerManager::getInstance()->isScreenOn();
262 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
263 return JSUtil::toJSValueRef(context,state);
264 }catch(const BasePlatformException& err){
265 return JSWebAPIException::throwException(context, exception, err);
269 JSValueRef JSPowerManager::restoreScreenBrightness(JSContextRef context,
271 JSObjectRef thisObject,
272 size_t argumentCount,
273 const JSValueRef arguments[],
274 JSValueRef* exception)
277 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
280 PowerManager::getInstance()->restoreScreenBrightness();
281 }catch(const BasePlatformException& err){
282 return JSWebAPIException::throwException(context, exception, err);
284 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
285 return JSValueMakeUndefined(context);}
287 JSValueRef JSPowerManager::turnScreenOn(JSContextRef context,
289 JSObjectRef thisObject,
290 size_t argumentCount,
291 const JSValueRef arguments[],
292 JSValueRef* exception)
295 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
296 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
297 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
300 PowerManager::getInstance()->setScreenState(true);
301 }catch(const BasePlatformException& err){
302 return JSWebAPIException::throwException(context, exception, err);
304 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
305 return JSValueMakeUndefined(context);
308 JSValueRef JSPowerManager::turnScreenOff(JSContextRef context,
310 JSObjectRef thisObject,
311 size_t argumentCount,
312 const JSValueRef arguments[],
313 JSValueRef* exception)
316 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
317 AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
318 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
321 PowerManager::getInstance()->setScreenState(false);
322 }catch(const BasePlatformException& err){
323 return JSWebAPIException::throwException(context, exception, err);
325 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
326 return JSValueMakeUndefined(context);