merge with master
[platform/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 <Logger.h>
22 #include <JSUtil.h>
23 #include <JSWebAPIError.h>
24 #include <ArgumentValidator.h>
25
26 #include "JSPowerManager.h"
27 #include "plugin_config.h"
28 #include "PowerManager.h"
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33 using namespace std;
34
35 namespace DeviceAPI {
36 namespace Power {
37
38 JSClassDefinition JSPowerManager::m_classInfo = {
39     0,
40     kJSClassAttributeNone,
41     "PowerManager",
42     NULL, //ParentClass
43     NULL, //StaticValues
44     m_function,
45     initialize,
46     finalize,
47     NULL, //HasProperty,
48     NULL, //GetProperty,
49     NULL, //SetProperty,
50     NULL, //DeleteProperty,
51     NULL, //GetPropertyNames,
52     NULL, //CallAsFunction,
53     NULL, //CallAsConstructor,
54     NULL, //HasInstance,
55     NULL //ConvertToType
56 };
57
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 },
69
70     { 0, 0, 0 }
71 };
72
73 JSClassRef JSPowerManager::m_jsClassRef = JSClassCreate(JSPowerManager::getClassInfo());
74
75 void JSPowerManager::initialize(JSContextRef context, JSObjectRef object)
76 {
77 }
78
79 void JSPowerManager::finalize(JSObjectRef object)
80 {
81     CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(object));
82     if( callback != NULL ){
83         PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
84         JSObjectSetPrivate(object, NULL);
85         delete callback;
86     }
87 }
88
89 const JSClassRef JSPowerManager::getClassRef()
90 {
91     if (!m_jsClassRef) {
92         m_jsClassRef = JSClassCreate(&m_classInfo);
93     }
94     return m_jsClassRef;
95 }
96
97 const JSClassDefinition* JSPowerManager::getClassInfo()
98 {
99     return &m_classInfo;
100 }
101
102 JSValueRef JSPowerManager::request(JSContextRef context,
103         JSObjectRef object,
104         JSObjectRef thisObject,
105         size_t argumentCount,
106         const JSValueRef arguments[],
107         JSValueRef* exception)
108 {
109     LogDebug("entered");
110
111     AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_REQUEST);
112     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
113
114     
115     try{
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 );
122
123     }catch(const BasePlatformException& err){
124         return JSWebAPIError::throwException(context, exception, err);
125     }
126
127     return JSValueMakeUndefined(context);
128 }
129
130 JSValueRef JSPowerManager::release(JSContextRef context,
131         JSObjectRef object,
132         JSObjectRef thisObject,
133         size_t argumentCount,
134         const JSValueRef arguments[],
135         JSValueRef* exception)
136 {
137     LogDebug("entered");
138
139     try{
140         ArgumentValidator validator(context, argumentCount, arguments);
141
142         string resource_str = validator.toString(0);
143         PowerResource resource(resource_str.c_str());
144         PowerManager::getInstance()->release( resource );
145
146     }catch(const BasePlatformException& err){
147         return JSWebAPIError::throwException(context, exception, err);
148     }
149     return JSValueMakeUndefined(context);
150 }
151
152 JSValueRef JSPowerManager::setScreenStateChangeListener(JSContextRef context,
153         JSObjectRef object,
154         JSObjectRef thisObject,
155         size_t argumentCount,
156         const JSValueRef arguments[],
157         JSValueRef* exception)
158 {
159     LogDebug("entered");
160
161     try{
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);
168         }
169         callback->setSuccessCallback(func);
170         PowerManager::getInstance()->addScreenStateChangedCallback(callback);
171
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");
176     }
177
178     return JSValueMakeUndefined(context);
179 }
180
181 JSValueRef JSPowerManager::unsetScreenStateChangeListener(JSContextRef context,
182         JSObjectRef object,
183         JSObjectRef thisObject,
184         size_t argumentCount,
185         const JSValueRef arguments[],
186         JSValueRef* exception)
187 {
188     LogDebug("entered");
189
190     CallbackUserData *callback = static_cast<CallbackUserData*>(JSObjectGetPrivate(thisObject));
191     if( callback != NULL ){
192         PowerManager::getInstance()->removeScreenStateChangedCallback(callback);
193         JSObjectSetPrivate(thisObject, NULL);
194         delete callback;
195     }
196     return JSValueMakeUndefined(context);
197 }
198
199 JSValueRef JSPowerManager::getScreenBrightness(JSContextRef context,
200         JSObjectRef object,
201         JSObjectRef thisObject,
202         size_t argumentCount,
203         const JSValueRef arguments[],
204         JSValueRef* exception)
205 {
206     LogDebug("entered");
207
208     try{
209         double brightness = PowerManager::getInstance()->getScreenBrightness();
210         return JSUtil::toJSValueRef(context, brightness);
211     }catch(const BasePlatformException& err){
212         return JSWebAPIError::throwException(context, exception, err);
213     }
214 }
215
216 JSValueRef JSPowerManager::setScreenBrightness(JSContextRef context,
217         JSObjectRef object,
218         JSObjectRef thisObject,
219         size_t argumentCount,
220         const JSValueRef arguments[],
221         JSValueRef* exception)
222 {
223     LogDebug("entered");
224     AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
225     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
226     try{
227         ArgumentValidator validator(context, argumentCount, arguments);
228
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);
234     }
235     return JSValueMakeUndefined(context);
236
237 }
238
239 JSValueRef JSPowerManager::isScreenOn(JSContextRef context,
240         JSObjectRef object,
241         JSObjectRef thisObject,
242         size_t argumentCount,
243         const JSValueRef arguments[],
244         JSValueRef* exception)
245 {
246     LogDebug("entered");
247
248     try{
249         bool state = PowerManager::getInstance()->isScreenOn();
250         return JSUtil::toJSValueRef(context,state);
251     }catch(const BasePlatformException& err){
252         return JSWebAPIError::throwException(context, exception, err);
253     }
254 }
255
256 JSValueRef JSPowerManager::restoreScreenBrightness(JSContextRef context,
257         JSObjectRef object,
258         JSObjectRef thisObject,
259         size_t argumentCount,
260         const JSValueRef arguments[],
261         JSValueRef* exception)
262 {
263     LogDebug("entered");
264
265     try{
266         PowerManager::getInstance()->restoreScreenBrightness();
267     }catch(const BasePlatformException& err){
268         return JSWebAPIError::throwException(context, exception, err);
269     }
270     return JSValueMakeUndefined(context);}
271
272 JSValueRef JSPowerManager::turnScreenOn(JSContextRef context,
273         JSObjectRef object,
274         JSObjectRef thisObject,
275         size_t argumentCount,
276         const JSValueRef arguments[],
277         JSValueRef* exception)
278 {
279     LogDebug("entered");
280     AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
281     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
282
283     try{
284         PowerManager::getInstance()->setScreenState(true);
285     }catch(const BasePlatformException& err){
286         return JSWebAPIError::throwException(context, exception, err);
287     }
288     return JSValueMakeUndefined(context);
289 }
290
291 JSValueRef JSPowerManager::turnScreenOff(JSContextRef context,
292         JSObjectRef object,
293         JSObjectRef thisObject,
294         size_t argumentCount,
295         const JSValueRef arguments[],
296         JSValueRef* exception)
297 {
298     LogDebug("entered");
299     AceSecurityStatus status = POWER_CHECK_ACCESS(POWER_FUNCTION_API_RELEASE);
300     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
301
302     try{
303         PowerManager::getInstance()->setScreenState(false);
304     }catch(const BasePlatformException& err){
305         return JSWebAPIError::throwException(context, exception, err);
306     }
307     return JSValueMakeUndefined(context);
308 }
309
310 }
311 }