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.
18 #include <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <JSWebAPIException.h>
24 #include <ArgumentValidator.h>
26 #include <Commons/Exception.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include <SecurityExceptions.h>
31 #include "plugin_config.h"
33 #include "AlarmRelative.h"
34 #include "AlarmConverter.h"
35 #include "JSAlarmRelative.h"
36 #include "JSAlarmManager.h"
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49 using namespace DeviceAPI::Common;
51 JSClassRef JSAlarmRelative::m_jsClassRef = NULL;
53 JSClassDefinition JSAlarmRelative::m_jsClassInfo = {
55 kJSClassAttributeNone,
56 TIZEN_ALARM_RELATIVE_INTERFACE,
65 NULL, //deleteProperty,Geolocation
66 NULL, //getPropertyNames,
73 JSStaticFunction JSAlarmRelative::m_function[] = {
74 { ALARM_FUNCTION_API_GET_REMAINING_SECONDS, JSAlarmRelative::getRemainingSeconds, kJSPropertyAttributeNone },
78 JSStaticValue JSAlarmRelative::m_property[] = {
79 { TIZEN_ALARM_RELATIVE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
80 { TIZEN_ALARM_RELATIVE_ATTRIBUTE_DELAY, getDelay, NULL, kJSPropertyAttributeReadOnly },
81 { TIZEN_ALARM_RELATIVE_ATTRIBUTE_PERIOD, getPeriod, NULL, kJSPropertyAttributeReadOnly },
85 const JSClassRef JSAlarmRelative::getClassRef()
88 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
93 const JSClassDefinition* JSAlarmRelative::getClassInfo()
95 return &m_jsClassInfo;
98 void JSAlarmRelative::initialize(JSContextRef context, JSObjectRef object)
101 void JSAlarmRelative::finalize(JSObjectRef object)
103 JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
105 LoggerE("Private object is null");
110 bool JSAlarmRelative::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
112 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
115 JSObjectRef JSAlarmRelative::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
118 ArgumentValidator validator(ctx, argumentCount, arguments);
120 long delay = validator.toLong(0);
122 throw InvalidValuesException("delay cannot be negative value");
124 long period = validator.toLong(1, true, 0);
126 throw InvalidValuesException("period cannot be negative value");
129 if ((argumentCount > 1) && !JSValueIsNull(ctx, arguments[1])) {
130 return JSValueToObject(ctx, createJSObject(ctx, delay, period), exception);
132 return JSValueToObject(ctx, createJSObject(ctx, delay, -1), exception);
134 } catch (const BasePlatformException& err) {
135 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(ctx, err);
136 *exception = exceptionObj;
142 AlarmRelativePtr JSAlarmRelative::getPrivData(JSObjectRef object)
144 JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
146 throw TypeMismatchException("Private object is null");
148 AlarmRelativePtr result = priv->getObject();
150 throw TypeMismatchException("Private object is null");
155 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, AlarmRelativePtr privateData)
157 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
159 throw TypeMismatchException("Private object is null");
161 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
165 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, int interval)
167 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
168 privateData->setDelay(delay);
169 privateData->setPeriod(interval);
171 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
173 throw TypeMismatchException("Private object is null");
175 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
178 JSValueRef JSAlarmRelative::getRemainingSeconds(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
187 AlarmRelativePtr privateData = getPrivData(thisObject);
188 id = privateData->getId();
190 int err = alarm_get_scheduled_date(id, &date);
191 if(err != ALARM_ERROR_NONE)
193 if(err == ALARM_ERROR_INVALID_PARAMETER) {
194 return JSValueMakeNull(ctx);
196 throw UnknownException("Unknown exception occurred.");
200 alarm_get_current_time(¤t);
202 nextTime = mktime(&date);
203 currentTime = mktime(¤t);
205 long result = nextTime - currentTime;
207 LoggerD("nextTime: "<<nextTime<<", currentTime: "<<currentTime<<", result: "<<result);
211 throw UnknownException("Unknown exception occurred.");
214 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, result);
215 } catch (const BasePlatformException &err) {
216 return JSWebAPIException::throwException(ctx, exception, err);
218 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
219 return JSWebAPIException::throwException(ctx, exception, err);
223 JSValueRef JSAlarmRelative::getId(JSContextRef ctx,
225 JSStringRef propertyName,
226 JSValueRef* exception)
228 Converter converter(ctx);
231 AlarmRelativePtr privateData = getPrivData(object);
233 throw TypeMismatchException("Private object is null");
236 id = privateData->getId();
238 std::string strId = converter.toString(id);
239 return converter.toJSValueRef(strId);
241 return JSValueMakeNull(ctx);
243 } catch (const BasePlatformException &err) {
244 return JSWebAPIException::throwException(ctx, exception, err);
246 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
247 return JSWebAPIException::throwException(ctx, exception, err);
251 JSValueRef JSAlarmRelative::getDelay(JSContextRef ctx,
253 JSStringRef propertyName,
254 JSValueRef* exception)
259 AlarmRelativePtr privateData = getPrivData(object);
261 throw TypeMismatchException("Private object is null");
264 delay = privateData->getDelay();
265 LoggerI("JSAlarmRelative delay = " << delay);
266 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, delay);
267 } catch (const BasePlatformException &err) {
268 return JSWebAPIException::throwException(ctx, exception, err);
270 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
271 return JSWebAPIException::throwException(ctx, exception, err);
275 JSValueRef JSAlarmRelative::getPeriod(JSContextRef ctx,
277 JSStringRef propertyName,
278 JSValueRef* exception)
283 AlarmRelativePtr privateData = getPrivData(object);
285 throw TypeMismatchException("Private object is null");
288 period = privateData->getPeriod();
289 LoggerI("JSAlarmRelative interval = " << period);
291 return JSValueMakeNull(ctx);
293 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, period);
295 } catch (const BasePlatformException &err) {
296 return JSWebAPIException::throwException(ctx, exception, err);
298 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
299 return JSWebAPIException::throwException(ctx, exception, err);