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.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/Validator.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
25 #include <JSWebAPIException.h>
26 #include <ArgumentValidator.h>
28 #include <Commons/Exception.h>
29 #include <JSTizenExceptionFactory.h>
30 #include <JSTizenException.h>
31 #include <SecurityExceptions.h>
33 #include "plugin_config.h"
35 #include "AlarmRelative.h"
36 #include "AlarmConverter.h"
37 #include "JSAlarmRelative.h"
38 #include "JSAlarmManager.h"
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace DeviceAPI::Common;
50 JSClassRef JSAlarmRelative::m_jsClassRef = NULL;
52 JSClassDefinition JSAlarmRelative::m_jsClassInfo = {
54 kJSClassAttributeNone,
55 TIZEN_ALARM_RELATIVE_INTERFACE,
64 NULL, //deleteProperty,Geolocation
65 NULL, //getPropertyNames,
72 JSStaticFunction JSAlarmRelative::m_function[] = {
73 { ALARM_FUNCTION_API_GET_REMAINING_SECONDS, JSAlarmRelative::getRemainingSeconds, kJSPropertyAttributeNone },
77 JSStaticValue JSAlarmRelative::m_property[] = {
78 { TIZEN_ALARM_RELATIVE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
79 { TIZEN_ALARM_RELATIVE_ATTRIBUTE_DELAY, getDelay, NULL, kJSPropertyAttributeReadOnly },
80 { TIZEN_ALARM_RELATIVE_ATTRIBUTE_PERIOD, getPeriod, NULL, kJSPropertyAttributeReadOnly },
84 const JSClassRef JSAlarmRelative::getClassRef()
87 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
92 const JSClassDefinition* JSAlarmRelative::getClassInfo()
94 return &m_jsClassInfo;
97 void JSAlarmRelative::initialize(JSContextRef context, JSObjectRef object)
100 void JSAlarmRelative::finalize(JSObjectRef object)
102 JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
104 LogError("Private object is null");
109 bool JSAlarmRelative::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
111 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
114 JSObjectRef JSAlarmRelative::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
117 AlarmRelativePtr priv = AlarmRelativePtr(new AlarmRelative());
119 throw TypeMismatchException("Private object is null");
122 ArgumentValidator validator(ctx, argumentCount, arguments);
124 unsigned long long delay = validator.toULongLong(0);
125 unsigned long long period = validator.toULongLong(1, true, 0);
128 priv->setDelay(validator.toULongLong(0));
130 return JSValueToObject(ctx, createJSObject(ctx, delay, period), exception);
131 } catch (const BasePlatformException& err) {
132 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(ctx, err);
133 *exception = exceptionObj;
139 AlarmRelativePtr JSAlarmRelative::getPrivData(JSObjectRef object)
141 JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
143 throw TypeMismatchException("Private object is null");
145 AlarmRelativePtr result = priv->getObject();
147 throw TypeMismatchException("Private object is null");
152 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, AlarmRelativePtr privateData)
154 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
156 throw TypeMismatchException("Private object is null");
158 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
161 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, int interval)
163 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
164 privateData->setDelay(delay);
165 privateData->setPeriod(interval);
167 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
169 throw TypeMismatchException("Private object is null");
171 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
174 JSValueRef JSAlarmRelative::getRemainingSeconds(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
180 Converter converter(ctx);
184 AlarmRelativePtr privateData = getPrivData(thisObject);
185 id = privateData->getId();
187 int err = alarm_get_scheduled_date(id, &date);
188 if(err != ALARM_ERROR_NONE)
190 if(err == ALARM_ERROR_INVALID_PARAMETER) {
191 return JSValueMakeNull(ctx);
193 throw UnknownException("Unknown exception occurred.");
197 alarm_get_current_time(¤t);
199 nextTime = mktime(&date);
200 currentTime = mktime(¤t);
202 int result = nextTime - currentTime;
204 LogDebug("nextTime: "<<nextTime<<", currentTime: "<<currentTime<<", result: "<<result);
208 throw UnknownException("Unknown exception occurred.");
211 JSValueRef jsResult = converter.toJSValueRef(result);
213 } catch (const BasePlatformException &err) {
214 return JSWebAPIException::throwException(ctx, exception, err);
216 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
217 return JSWebAPIException::throwException(ctx, exception, err);
221 JSValueRef JSAlarmRelative::getId(JSContextRef ctx,
223 JSStringRef propertyName,
224 JSValueRef* exception)
226 Converter converter(ctx);
229 AlarmRelativePtr privateData = getPrivData(object);
231 throw TypeMismatchException("Private object is null");
234 id = privateData->getId();
236 std::string strId = converter.toString(id);
237 return converter.toJSValueRef(strId);
239 return JSValueMakeNull(ctx);
241 } catch (const BasePlatformException &err) {
242 return JSWebAPIException::throwException(ctx, exception, err);
244 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
245 return JSWebAPIException::throwException(ctx, exception, err);
249 JSValueRef JSAlarmRelative::getDelay(JSContextRef ctx,
251 JSStringRef propertyName,
252 JSValueRef* exception)
254 Converter converter(ctx);
258 AlarmRelativePtr privateData = getPrivData(object);
260 throw TypeMismatchException("Private object is null");
263 delay = privateData->getDelay();
264 LogInfo("JSAlarmRelative delay = " << delay);
266 return converter.toJSValueRef(delay);
269 throw UnknownException("Invalid delay value.");
271 } catch (const BasePlatformException &err) {
272 return JSWebAPIException::throwException(ctx, exception, err);
274 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
275 return JSWebAPIException::throwException(ctx, exception, err);
279 JSValueRef JSAlarmRelative::getPeriod(JSContextRef ctx,
281 JSStringRef propertyName,
282 JSValueRef* exception)
285 Converter converter(ctx);
289 AlarmRelativePtr privateData = getPrivData(object);
291 throw TypeMismatchException("Private object is null");
294 period = privateData->getPeriod();
295 LogInfo("JSAlarmRelative interval = " << period);
297 return JSValueMakeNull(ctx);
299 return converter.toJSValueRef(period);
301 } catch (const BasePlatformException &err) {
302 return JSWebAPIException::throwException(ctx, exception, err);
304 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
305 return JSWebAPIException::throwException(ctx, exception, err);