[HAM] Added privilege checks in C++.
[platform/core/api/webapi-plugins.git] / src / humanactivitymonitor / humanactivitymonitor_api.js
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 var utils_ = xwalk.utils;
18 var type_ = utils_.type;
19 var converter_ = utils_.converter;
20 var validator_ = utils_.validator;
21 var types_ = validator_.Types;
22 var native_ = new xwalk.utils.NativeManager(extension);
23
24 var callbackId = 0;
25 var callbacks = {};
26
27 function nextCallbackId() {
28   return callbackId++;
29 }
30
31 function SetReadOnlyProperty(obj, n, v) {
32   Object.defineProperty(obj, n, {value: v, writable: false});
33 }
34
35 var HumanActivityType = {
36   PEDOMETER: 'PEDOMETER',
37   WRIST_UP: 'WRIST_UP',
38   HRM: 'HRM',
39   GPS: 'GPS'
40 };
41
42 var PedometerStepStatus = {
43   NOT_MOVING: 'NOT_MOVING',
44   WALKING: 'WALKING',
45   RUNNING: 'RUNNING'
46 };
47
48 function convertActivityData(type, data) {
49   switch (type) {
50     case HumanActivityType.PEDOMETER:
51       // TODO(r.galka) Not Supported in current implementation
52       return undefined;
53     case HumanActivityType.WRIST_UP:
54       return null;
55     case HumanActivityType.HRM:
56       return new HumanActivityHRMData(data);
57     case HumanActivityType.GPS:
58       var gpsInfo = [];
59       for (var i = 0, max = data.length; i < max; i++) {
60         gpsInfo.push(new HumanActivityGPSInfo(data[i]));
61       }
62       return new HumanActivityGPSInfoArray(gpsInfo);
63   }
64 }
65
66 function HumanActivityMonitorManager() {
67 }
68
69 HumanActivityMonitorManager.prototype.getHumanActivityData = function(type, successCallback, errorCallback) {
70   var args = validator_.validateArgs(arguments, [
71     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
72     {name: 'successCallback', type: types_.FUNCTION},
73     {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
74   ]);
75
76   if (args.type === HumanActivityType.WRIST_UP) {
77     throw new WebAPIException(WebAPIException.NOT_SUPPORTED_ERR);
78   }
79
80   var listenerId = 'HumanActivityMonitor_'  + args.type;
81   if (!native_.isListenerSet(listenerId)) {
82     throw new WebAPIException(WebAPIException.SERVICE_NOT_AVAILABLE_ERR);
83   }
84
85   var data = {
86     type: args.type
87   };
88
89   var callback = function(result) {
90     if (native_.isFailure(result)) {
91       native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
92       return;
93     }
94
95     native_.callIfPossible(args.successCallback,
96         convertActivityData(args.type, native_.getResultObject(result)));
97   };
98
99   var result = native_.call('HumanActivityMonitorManager_getHumanActivityData', data, callback);
100
101   if (native_.isFailure(result)) {
102     throw native_.getErrorObject(result);
103   }
104 };
105
106 function startListener(listenerId, listener, method, data) {
107   if (!native_.isListenerSet(listenerId)) {
108     var result = native_.callSync(method, data);
109     if (native_.isFailure(result)) {
110       throw native_.getErrorObject(result);
111     }
112   }
113
114   // always set the listener, if it's another call to startListener() overwrite the old one
115   native_.addListener(listenerId, listener);
116 }
117
118 function checkPrivilegesForMethod(method, type) {
119   utils_.checkPrivilegeAccess(utils_.privilege.HEALTHINFO);
120   if ('HumanActivityMonitorManager_stop' === method && 'GPS' === type) {
121     utils_.checkPrivilegeAccess(utils_.privilege.LOCATION);
122   }
123 }
124
125 function stopListener(listenerId, method, data) {
126   if (!native_.isListenerSet(listenerId)) {
127     checkPrivilegesForMethod(method, data.type);
128     return;
129   }
130
131   var result = native_.callSync(method, data);
132   if (native_.isFailure(result)) {
133     throw native_.getErrorObject(result);
134   }
135
136   native_.removeListener(listenerId);
137 }
138
139 HumanActivityMonitorManager.prototype.start = function(type, changedCallback) {
140   var args = validator_.validateArgs(arguments, [
141     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)},
142     {name: 'changedCallback', type: types_.FUNCTION, optional: true, nullable: true}
143   ]);
144
145   var listenerId = 'HumanActivityMonitor_'  + args.type;
146
147   startListener(listenerId,
148                 function(result) {
149                   native_.callIfPossible(args.changedCallback, convertActivityData(args.type, result));
150                 },
151                 'HumanActivityMonitorManager_start',
152                 { type: args.type, listenerId: listenerId });
153 };
154
155 HumanActivityMonitorManager.prototype.stop = function(type) {
156   var args = validator_.validateArgs(arguments, [
157     {name: 'type', type: types_.ENUM, values: Object.keys(HumanActivityType)}
158   ]);
159
160   stopListener('HumanActivityMonitor_'  + args.type,
161                'HumanActivityMonitorManager_stop',
162                { type: args.type });
163 };
164
165 var accumulativePedometerListenerId = 'HumanActivityMonitor_AccumulativePedometerListener';
166
167 HumanActivityMonitorManager.prototype.setAccumulativePedometerListener = function(changeCallback) {
168   var args = validator_.validateArgs(arguments, [
169     {name: 'changeCallback', type: types_.FUNCTION}
170   ]);
171
172   var listenerId = accumulativePedometerListenerId;
173
174   startListener(listenerId,
175                 function(result) {
176                   args.changeCallback(convertActivityData(HumanActivityType.PEDOMETER, result));
177                 },
178                 'HumanActivityMonitorManager_setAccumulativePedometerListener',
179                 { listenerId: listenerId });
180 };
181
182 HumanActivityMonitorManager.prototype.unsetAccumulativePedometerListener = function() {
183   stopListener(accumulativePedometerListenerId,
184                'HumanActivityMonitorManager_unsetAccumulativePedometerListener',
185                {});
186 };
187
188 function StepDifference() {
189   SetReadOnlyProperty(this, 'stepCountDifference', null);
190   SetReadOnlyProperty(this, 'timestamp', null);
191 }
192
193
194 function HumanActivityData() {
195 }
196
197
198 function HumanActivityPedometerData() {
199   SetReadOnlyProperty(this, 'stepStatus', null);
200   SetReadOnlyProperty(this, 'speed', null);
201   SetReadOnlyProperty(this, 'walkingFrequency', null);
202   SetReadOnlyProperty(this, 'cumulativeDistance', null);
203   SetReadOnlyProperty(this, 'cumulativeCalorie', null);
204   SetReadOnlyProperty(this, 'cumulativeTotalStepCount', null);
205   SetReadOnlyProperty(this, 'cumulativeWalkStepCount', null);
206   SetReadOnlyProperty(this, 'cumulativeRunStepCount', null);
207   SetReadOnlyProperty(this, 'stepCountDifferences', null);
208 }
209
210 HumanActivityPedometerData.prototype = new HumanActivityData();
211 HumanActivityPedometerData.prototype.constructor = HumanActivityPedometerData;
212
213
214 function HumanActivityAccumulativePedometerData() {
215   SetReadOnlyProperty(this, 'stepStatus', null);
216   SetReadOnlyProperty(this, 'speed', null);
217   SetReadOnlyProperty(this, 'walkingFrequency', null);
218   SetReadOnlyProperty(this, 'accumulativeDistance', null);
219   SetReadOnlyProperty(this, 'accumulativeCalorie', null);
220   SetReadOnlyProperty(this, 'accumulativeTotalStepCount', null);
221   SetReadOnlyProperty(this, 'accumulativeWalkStepCount', null);
222   SetReadOnlyProperty(this, 'accumulativeRunStepCount', null);
223   SetReadOnlyProperty(this, 'stepCountDifferences', null);
224 }
225
226 HumanActivityAccumulativePedometerData.prototype = new HumanActivityData();
227 HumanActivityAccumulativePedometerData.prototype.constructor = HumanActivityAccumulativePedometerData;
228
229
230 function HumanActivityHRMData(data) {
231   SetReadOnlyProperty(this, 'heartRate', data.heartRate);
232   SetReadOnlyProperty(this, 'rRInterval', data.rRInterval);
233 }
234
235 HumanActivityHRMData.prototype = new HumanActivityData();
236 HumanActivityHRMData.prototype.constructor = HumanActivityHRMData;
237
238
239 function HumanActivityGPSInfo(data) {
240   SetReadOnlyProperty(this, 'latitude', data.latitude);
241   SetReadOnlyProperty(this, 'longitude', data.longitude);
242   SetReadOnlyProperty(this, 'altitude', data.altitude);
243   SetReadOnlyProperty(this, 'speed', data.speed);
244   SetReadOnlyProperty(this, 'errorRange', data.errorRange);
245   SetReadOnlyProperty(this, 'timestamp', data.timestamp);
246 }
247
248
249 function HumanActivityGPSInfoArray(data) {
250   SetReadOnlyProperty(this, 'gpsInfo', data);
251 }
252
253 HumanActivityGPSInfoArray.prototype = new HumanActivityData();
254 HumanActivityGPSInfoArray.prototype.constructor = HumanActivityGPSInfoArray;
255
256
257 exports = new HumanActivityMonitorManager();