Bump version to 0.127
[platform/framework/web/tizen-extensions-crosswalk.git] / power / power_api.js
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // FIXME: A lot of these methods should throw NOT_SUPPORTED_ERR on desktop.
6 // There is no easy way to do verify which methods are supported yet.
7
8 // Save the latest screenState value. We use this to pass the previous
9 // state when calling the listener.
10 var screenState = undefined;
11
12 var defaultScreenBrightness = undefined;
13 var screenStateChangedListener;
14
15 var resources = {
16   'SCREEN': {
17     type: 0,
18     states: {
19       'SCREEN_OFF': 0,
20       'SCREEN_DIM': 1,
21       'SCREEN_NORMAL': 2,
22       'SCREEN_BRIGHT': 3 // Deprecated.
23     }
24   },
25   'CPU': {
26     type: 1,
27     states: {
28       'CPU_AWAKE': 4
29     }
30   }
31 };
32
33 function callListener(oldState, newState) {
34   if (screenStateChangedListener == null)
35     return;
36   var previousState = resources.SCREEN.states[oldState];
37   var changedState = resources.SCREEN.states[newState];
38   screenStateChangedListener(oldState, newState);
39 }
40
41 var postMessage = function(msg) {
42   extension.postMessage(JSON.stringify(msg));
43 };
44
45 var sendSyncMessage = function(msg) {
46   return extension.internal.sendSyncMessage(JSON.stringify(msg));
47 };
48
49 function getScreenState() {
50   var msg = {
51     'cmd': 'PowerGetScreenState'
52   };
53   var r = JSON.parse(sendSyncMessage(msg));
54   screenState = r.state;
55 }
56
57 extension.setMessageListener(function(msg) {
58   var m = JSON.parse(msg);
59   if (m.cmd == 'ScreenStateChanged') {
60     var newState = m.state;
61     if (screenState !== newState) {
62       callListener(screenState, newState);
63       screenState = newState;
64     }
65   }
66 });
67
68 exports.request = function(resource, state) {
69   // Validate permission to 'power'.
70   // throw new WebAPIException(SECURITY_ERR);
71
72   if (typeof resource !== 'string' || typeof state !== 'string') {
73     throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
74   }
75
76   if (!resources.hasOwnProperty(resource)) {
77     throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
78   }
79
80   if (!resources[resource].states.hasOwnProperty(state)) {
81     throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
82   }
83
84   // Exception check: SCREEN_OFF is a state cannot be requested
85   if (resource === 'SCREEN' && state === 'SCREEN_OFF') {
86     throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
87   }
88
89   postMessage({
90     'cmd': 'PowerRequest',
91     'resource': resources[resource].type,
92     'state': resources[resource].states[state]
93   });
94 };
95
96 exports.release = function(resource) {
97   // Validate permission to 'power'. Do not throw, just bail out.
98
99   if (typeof resource !== 'string')
100     throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
101
102   if (!resources.hasOwnProperty(resource))
103     throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
104
105   postMessage({
106     'cmd': 'PowerRelease',
107     'resource': resources[resource].type
108   });
109 };
110
111 exports.setScreenStateChangeListener = function(listener) {
112   // No permission validation.
113
114   if (typeof listener !== 'function')
115     throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
116
117   // FIXME: According to docs, it should throw INVALID_VALUES_ERR if input
118   // parameters contain an invalid value. Verify the Tizen 2.x impl.
119
120   // This will cache an initial value, that is necessary to ensure we
121   // always have a previous value.
122   getScreenState();
123
124   screenStateChangedListener = listener;
125   postMessage({
126     'cmd': 'SetListenToScreenStateChange',
127     'value': true
128   });
129 };
130
131 exports.unsetScreenStateChangeListener = function() {
132   // No permission validation.
133   screenStateChangedListener = null;
134   postMessage({
135     'cmd': 'SetListenToScreenStateChange',
136     'value': false
137   });
138 };
139
140 exports.getScreenBrightness = function() {
141   var r = JSON.parse(sendSyncMessage({
142     'cmd': 'PowerGetScreenBrightness'
143   }));
144   if (r['error']) {
145     throw new tizen.WebAPIException(tizen.WebAPIException.NOT_SUPPORTED_ERR);
146     return;
147   }
148   var brightness = r['brightness'];
149   if (defaultScreenBrightness === undefined)
150     defaultScreenBrightness = brightness;
151   return brightness;
152 };
153
154 exports.setScreenBrightness = function(brightness) {
155   // Validate permission to 'power'.
156   // throw new WebAPIException(SECURITY_ERR);
157   if (typeof brightness !== 'number' || !isFinite(brightness))
158     throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
159
160   if (brightness < 0 || brightness > 1)
161     throw new tizen.WebAPIException(tizen.WebAPIException.INVALID_VALUES_ERR);
162
163   postMessage({
164     'cmd': 'PowerSetScreenBrightness',
165     'value': brightness
166   });
167 };
168
169 exports.isScreenOn = function() {
170   getScreenState();
171   if (typeof screenState !== 'number')
172     throw new tizen.WebAPIException(tizen.WebAPIException.UNKNOWN_ERR);
173   return screenState !== resources['SCREEN'].states['SCREEN_OFF'];
174 };
175
176 exports.restoreScreenBrightness = function() {
177   // Validate permission to 'power'.
178   // throw new WebAPIException(SECURITY_ERR);
179
180   if (defaultScreenBrightness === undefined)
181     exports.getScreenBrightness();
182
183   if (defaultScreenBrightness < 0 || defaultScreenBrightness > 1)
184     throw new tizen.WebAPIException(tizen.WebAPIException.UNKNOWN_ERR);
185
186   postMessage({
187     'cmd': 'PowerSetScreenBrightness',
188     'value': defaultScreenBrightness
189   });
190 };
191
192 exports.turnScreenOff = function() {
193   // Validate permission to 'power'.
194   // throw new WebAPIException(SECURITY_ERR);
195
196   // FIXME: throw UNKNOWN_ERR during failure to set the new value.
197   postMessage({
198     'cmd': 'PowerSetScreenEnabled',
199     'value': false
200   });
201 };
202
203 exports.turnScreenOn = function() {
204   // Validate permission to 'power'.
205   // throw new WebAPIException(SECURITY_ERR);
206
207   // FIXME: throw UNKNOWN_ERR during failure to set the new value.
208   postMessage({
209     'cmd': 'PowerSetScreenEnabled',
210     'value': true
211   });
212 };