wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Power / JSPowerManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <SecurityExceptions.h>
19 #include <CallbackUserData.h>
20 #include <GlobalContextManager.h>
21 #include <JSUtil.h>
22 #include <JSWebAPIException.h>
23 #include <ArgumentValidator.h>
24 #include <TimeTracer.h>
25
26 #include "JSPowerManager.h"
27 #include "plugin_config.h"
28 #include "PowerManager.h"
29 #include <Logger.h>
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace DeviceAPI::Common;
34 using namespace std;
35
36 namespace DeviceAPI {
37 namespace Power {
38
39 JSClassDefinition JSPowerManager::m_classInfo = {
40     0,
41     kJSClassAttributeNone,
42     "PowerManager",
43     NULL, //ParentClass
44     NULL, //StaticValues
45     m_function,
46     initialize,
47     finalize,
48     NULL, //HasProperty,
49     NULL, //GetProperty,
50     NULL, //SetProperty,
51     NULL, //DeleteProperty,
52     NULL, //GetPropertyNames,
53     NULL, //CallAsFunction,
54     NULL, //CallAsConstructor,
55     NULL, //HasInstance,
56     NULL //ConvertToType
57 };
58
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 },
70
71     { 0, 0, 0 }
72 };
73
74 JSClassRef JSPowerManager::m_jsClassRef = JSClassCreate(JSPowerManager::getClassInfo());
75
76 void JSPowerManager::initialize(JSContextRef context, JSObjectRef object)
77 {
78 }
79
80 void JSPowerManager::finalize(JSObjectRef object)
81 {
82     CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(object));
83     if( callback != NULL ){
84         PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
85         JSObjectSetPrivate(object, NULL);
86         delete callback;
87     }
88 }
89
90 const JSClassRef JSPowerManager::getClassRef()
91 {
92     if (!m_jsClassRef) {
93         m_jsClassRef = JSClassCreate(&m_classInfo);
94     }
95     return m_jsClassRef;
96 }
97
98 const JSClassDefinition* JSPowerManager::getClassInfo()
99 {
100     return &m_classInfo;
101 }
102
103 JSValueRef JSPowerManager::request(JSContextRef context,
104         JSObjectRef object,
105         JSObjectRef thisObject,
106         size_t argumentCount,
107         const JSValueRef arguments[],
108         JSValueRef* exception)
109 {
110     LoggerD("entered");
111         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
112
113     AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_REQUEST);
114     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
115
116     try{
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);
125     }
126
127         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
128     return JSValueMakeUndefined(context);
129 }
130
131 JSValueRef JSPowerManager::release(JSContextRef context,
132         JSObjectRef object,
133         JSObjectRef thisObject,
134         size_t argumentCount,
135         const JSValueRef arguments[],
136         JSValueRef* exception)
137 {
138     LoggerD("entered");
139         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
140
141     try{
142         ArgumentValidator validator(context, argumentCount, arguments);
143
144         string resource_str = validator.toString(0);
145         PowerResource resource(resource_str.c_str());
146         PowerManager::getInstance()->release( resource );
147
148     }catch(const BasePlatformException& err){
149         return JSWebAPIException::throwException(context, exception, err);
150     }
151         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
152     return JSValueMakeUndefined(context);
153 }
154
155 JSValueRef JSPowerManager::setScreenStateChangeListener(JSContextRef context,
156         JSObjectRef object,
157         JSObjectRef thisObject,
158         size_t argumentCount,
159         const JSValueRef arguments[],
160         JSValueRef* exception)
161 {
162     LoggerD("entered");
163         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
164
165     try{
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);
172         }
173         callback->setSuccessCallback(func);
174         PowerManager::getInstance()->addScreenStateChangedCallback(callback);
175
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");
180     }
181
182         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
183     return JSValueMakeUndefined(context);
184 }
185
186 JSValueRef JSPowerManager::unsetScreenStateChangeListener(JSContextRef context,
187         JSObjectRef object,
188         JSObjectRef thisObject,
189         size_t argumentCount,
190         const JSValueRef arguments[],
191         JSValueRef* exception)
192 {
193     LoggerD("entered");
194         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
195
196     CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(thisObject));
197     if( callback != NULL ){
198         PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
199         JSObjectSetPrivate(thisObject, NULL);
200         delete callback;
201     }
202         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
203     return JSValueMakeUndefined(context);
204 }
205
206 JSValueRef JSPowerManager::getScreenBrightness(JSContextRef context,
207         JSObjectRef object,
208         JSObjectRef thisObject,
209         size_t argumentCount,
210         const JSValueRef arguments[],
211         JSValueRef* exception)
212 {
213     LoggerD("entered");
214         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
215
216     try{
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);
222     }
223 }
224
225 JSValueRef JSPowerManager::setScreenBrightness(JSContextRef context,
226         JSObjectRef object,
227         JSObjectRef thisObject,
228         size_t argumentCount,
229         const JSValueRef arguments[],
230         JSValueRef* exception)
231 {
232     LoggerD("entered");
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);
236     try{
237         ArgumentValidator validator(context, argumentCount, arguments);
238
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);
244     }
245         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
246     return JSValueMakeUndefined(context);
247
248 }
249
250 JSValueRef JSPowerManager::isScreenOn(JSContextRef context,
251         JSObjectRef object,
252         JSObjectRef thisObject,
253         size_t argumentCount,
254         const JSValueRef arguments[],
255         JSValueRef* exception)
256 {
257     LoggerD("entered");
258         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
259
260     try{
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);
266     }
267 }
268
269 JSValueRef JSPowerManager::restoreScreenBrightness(JSContextRef context,
270         JSObjectRef object,
271         JSObjectRef thisObject,
272         size_t argumentCount,
273         const JSValueRef arguments[],
274         JSValueRef* exception)
275 {
276     LoggerD("entered");
277         TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
278
279     try{
280         PowerManager::getInstance()->restoreScreenBrightness();
281     }catch(const BasePlatformException& err){
282         return JSWebAPIException::throwException(context, exception, err);
283     }
284         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
285     return JSValueMakeUndefined(context);}
286
287 JSValueRef JSPowerManager::turnScreenOn(JSContextRef context,
288         JSObjectRef object,
289         JSObjectRef thisObject,
290         size_t argumentCount,
291         const JSValueRef arguments[],
292         JSValueRef* exception)
293 {
294     LoggerD("entered");
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);
298
299     try{
300         PowerManager::getInstance()->setScreenState(true);
301     }catch(const BasePlatformException& err){
302         return JSWebAPIException::throwException(context, exception, err);
303     }
304         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
305     return JSValueMakeUndefined(context);
306 }
307
308 JSValueRef JSPowerManager::turnScreenOff(JSContextRef context,
309         JSObjectRef object,
310         JSObjectRef thisObject,
311         size_t argumentCount,
312         const JSValueRef arguments[],
313         JSValueRef* exception)
314 {
315     LoggerD("entered");
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);
319
320     try{
321         PowerManager::getInstance()->setScreenState(false);
322     }catch(const BasePlatformException& err){
323         return JSWebAPIException::throwException(context, exception, err);
324     }
325         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
326     return JSValueMakeUndefined(context);
327 }
328
329 }
330 }