2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dpl/log/log.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/Validator.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
26 #include <Tizen/Common/SecurityExceptions.h>
27 #include <Commons/Exception.h>
28 #include <Commons/Regex.h>
29 #include <Tizen/Common/JSTizenExceptionFactory.h>
30 #include <Tizen/Common/JSTizenException.h>
31 #include <Tizen/Application/JSApplicationService.h>
32 #include <API/Application/ApplicationService.h>
33 #include <Tizen/Application/ApplicationConverter.h>
37 #include "plugin_config.h"
38 #include "AlarmConverter.h"
39 #include "JSAlarmAbsolute.h"
40 #include "AlarmAbsolute.h"
41 #include "JSAlarmRelative.h"
42 #include "AlarmRelative.h"
43 #include "JSAlarmManager.h"
49 using namespace WrtDeviceApis::Commons;
50 using namespace WrtDeviceApis::CommonsJavaScript;
51 using namespace TizenApis::Commons;
52 using namespace TizenApis::Api::Alarm;
54 static bool alarm_iterate_callback(int alarm_id, void *user_data)
56 std::vector<int> *alarmIds = reinterpret_cast<std::vector<int>*>(user_data);
58 alarmIds->push_back(alarm_id);
62 JSClassRef JSAlarmManager::m_jsClassRef = NULL;
64 JSClassDefinition JSAlarmManager::m_jsClassInfo = {
66 kJSClassAttributeNone,
67 TIZEN_ALARM_INTERFACE,
76 NULL, //deleteProperty,Geolocation
77 NULL, //getPropertyNames,
84 JSStaticFunction JSAlarmManager::m_function[] = {
85 { ALARM_FUNCTION_API_ADD, JSAlarmManager::add,kJSPropertyAttributeNone },
86 { ALARM_FUNCTION_API_REMOVE, JSAlarmManager::remove,kJSPropertyAttributeNone },
87 { ALARM_FUNCTION_API_REMOVE_ALL, JSAlarmManager::removeAll,kJSPropertyAttributeNone },
88 { ALARM_FUNCTION_API_GET_ALL, JSAlarmManager::getAll,kJSPropertyAttributeNone },
89 { ALARM_FUNCTION_API_GET, JSAlarmManager::get,kJSPropertyAttributeNone },
93 JSStaticValue JSAlarmManager::m_property[] = {
94 { TIZEN_ALARM_CONSTANT_PERIOD_MINUTE, getMin, NULL, kJSPropertyAttributeReadOnly },
95 { TIZEN_ALARM_CONSTANT_PERIOD_HOUR, getHour, NULL, kJSPropertyAttributeReadOnly },
96 { TIZEN_ALARM_CONSTANT_PERIOD_DAY, getDay, NULL, kJSPropertyAttributeReadOnly },
97 { TIZEN_ALARM_CONSTANT_PERIOD_WEEK, getWeek, NULL, kJSPropertyAttributeReadOnly },
101 const JSClassRef JSAlarmManager::getClassRef()
104 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
109 const JSClassDefinition* JSAlarmManager::getClassInfo()
111 return &m_jsClassInfo;
114 JSContextRef JSAlarmManager::gContext = NULL;
116 void JSAlarmManager::initialize(JSContextRef ctx, JSObjectRef object)
119 LogInfo("GContext = " << gContext);
122 void JSAlarmManager::finalize(JSObjectRef object)
126 bool JSAlarmManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
127 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
130 JSValueRef JSAlarmManager::add(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
136 std::string applicationId;
139 AceSecurityStatus status = ALARM_CHECK_ACCESS(ALARM_FUNCTION_API_ADD);
140 TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
142 Validator check(ctx, exception);
143 AlarmConverter converter(ctx);
144 TizenApis::Tizen1_0::Application::ApplicationConverter applicationConverter(ctx);
147 if(argumentCount < 2) {
148 ThrowMsg(ConversionException, "Wrong parameter type.");
151 if(argumentCount >= 2) {
152 if( JSValueIsNull(ctx, arguments[1]) || JSValueIsUndefined(ctx, arguments[1]) ) {
153 ThrowMsg(InvalidArgumentException, "Wrong application id.");
155 applicationId = converter.toString(arguments[1]);
156 LogDebug("package:[" << applicationId << "]");
159 if(JSValueIsObjectOfClass(ctx, arguments[0], JSAlarmAbsolute::getClassRef())) {
161 JSObjectRef alarmObj = JSValueToObject(ctx, arguments[0], exception);
162 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(alarmObj));
164 ThrowMsg(ConversionException, "Object is null.");
166 AlarmAbsolutePtr alarmPtr = priv->getObject();
168 ThrowMsg(ConversionException, "Private object is null.");
171 startDate = alarmPtr->getDate();
172 service = alarmPtr->getService();
174 if(argumentCount >=3) {
175 // It's service object
176 if(JSValueIsObjectOfClass(ctx, arguments[2], TizenApis::Tizen1_0::Application::JSApplicationService::getClassRef())) {
177 TizenApis::Api::Application::ApplicationServicePtr appService = applicationConverter.toApplicationService(arguments[2]);
178 if(converter.toAlarmService(service, appService) == false) {
179 ThrowMsg(ConversionException, "Wrong parameter type.");
181 } else if (JSValueIsNull(ctx, arguments[2]) || JSValueIsUndefined(ctx, arguments[2])) {
182 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
184 ThrowMsg(UnsupportedException, "Third parameter not supported yet.");
187 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
190 LogDebug("applicationId:[" << applicationId << "]");
191 service_set_app_id(service, applicationId.c_str());
193 AbsoluteRecurrence::Type alarmType = alarmPtr->getRecurrenceType();
195 int err = ALARM_ERROR_NONE;
196 if(alarmType == AbsoluteRecurrence::ByDayValue) {
197 int bydayValue = converter.toNativeAlarmValue(alarmPtr->getByDayRecurrence());
198 LogInfo("Native bydayValue = " << bydayValue);
199 err = alarm_schedule_with_recurrence_week_flag(service, &startDate, bydayValue, &alarm_id);
200 } else if(alarmType == AbsoluteRecurrence::Interval) {
201 int interval = alarmPtr->getInterval();
202 err = alarm_schedule_at_date(service, &startDate, interval, &alarm_id);
204 err = alarm_schedule_at_date(service, &startDate, 0, &alarm_id);
207 if(err == ALARM_ERROR_NONE) {
208 alarmPtr->setId(alarm_id);
210 LogDebug("err:[" << err << "]");
211 ThrowMsg(PlatformException, "Alarm scheduling failed.");
213 } else if(JSValueIsObjectOfClass(ctx, arguments[0], JSAlarmRelative::getClassRef())) {
214 JSObjectRef alarmObj = JSValueToObject(ctx, arguments[0], exception);
215 JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(alarmObj));
217 ThrowMsg(ConversionException, "Object is null.");
220 AlarmRelativePtr alarmPtr = priv->getObject();
222 ThrowMsg(ConversionException, "Private object is null.");
225 delay = alarmPtr->getDelay();
226 int interval = alarmPtr->getPeriod();
227 service = alarmPtr->getService();
229 if(argumentCount >=3) {
230 // It's service object
231 if(JSValueIsObjectOfClass(ctx, arguments[2], TizenApis::Tizen1_0::Application::JSApplicationService::getClassRef())) {
232 TizenApis::Api::Application::ApplicationServicePtr appService = applicationConverter.toApplicationService(arguments[2]);
233 if(converter.toAlarmService(service, appService) == false) {
234 ThrowMsg(ConversionException, "Wrong third parameter type.");
236 } else if (JSValueIsNull(ctx, arguments[2]) || JSValueIsUndefined(ctx, arguments[2])) {
237 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
239 ThrowMsg(UnsupportedException, "Third parameter not supported yet.");
242 service_set_operation(service, SERVICE_OPERATION_DEFAULT);
245 LogDebug("applicationId:[" << applicationId << "]");
246 service_set_app_id(service, applicationId.c_str());
248 int err = alarm_schedule_after_delay(service, delay, interval, &alarm_id);
250 if(err == ALARM_ERROR_NONE) {
251 alarmPtr->setId(alarm_id);
253 LogDebug("err:[" << err << "]");
256 ThrowMsg(ConversionException, "Wrong first parameter type.");
258 } Catch(ConversionException) {
259 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
260 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
261 } Catch(UnsupportedException) {
262 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
263 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
264 } Catch(InvalidArgumentException) {
265 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
266 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
268 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
269 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
272 return JSValueMakeUndefined(ctx);
275 JSValueRef JSAlarmManager::remove(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
279 AceSecurityStatus status = ALARM_CHECK_ACCESS(ALARM_FUNCTION_API_REMOVE);
280 TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
282 Converter converter(ctx);
287 if(argumentCount < 1 ) {
288 value = JSValueMakeUndefined(ctx);
289 id = converter.toInt(value);
291 id = converter.toInt(arguments[0]);
295 ThrowMsg(InvalidArgumentException, "Invalid id value.");
298 err = alarm_cancel(id);
300 if(err != ALARM_ERROR_NONE)
302 if(err == ALARM_ERROR_INVALID_PARAMETER) {
303 ThrowMsg(NotFoundException, "Alarm not found.");
306 ThrowMsg(PlatformException, "Unknown error occurred.");
309 } Catch(ConversionException) {
310 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
311 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
312 } Catch(UnsupportedException) {
313 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
314 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
315 } Catch(InvalidArgumentException) {
316 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
317 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
318 } Catch (NotFoundException) {
319 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
320 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
322 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
323 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
326 return JSValueMakeUndefined(ctx);
329 JSValueRef JSAlarmManager::removeAll(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
331 LogError("GContext = " << gContext);
332 AceSecurityStatus status = ALARM_CHECK_ACCESS(
333 ALARM_FUNCTION_API_REMOVE_ALL);
334 TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
336 int returnVal = alarm_cancel_all();
337 if (ALARM_ERROR_NONE!=returnVal) {
338 LogInfo("Error while removing all alarms: "<<returnVal);
341 return JSValueMakeUndefined(ctx);
344 JSValueRef JSAlarmManager::getAll(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
346 AceSecurityStatus status = ALARM_CHECK_ACCESS(ALARM_FUNCTION_API_GET_ALL);
347 TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
350 AlarmConverter converter(ctx);
351 std::vector<int> alarmIds;
354 error = alarm_foreach_registered_alarm(alarm_iterate_callback, &alarmIds);
355 if(error != ALARM_ERROR_NONE)
357 LogInfo("Error occurred while getting all alarms: "<<error);
358 ThrowMsg(PlatformException, "Unknown error occurred.");
361 JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
363 LogInfo("Natvie alarms id size = " << alarmIds.size());
364 for(unsigned int i = 0; i < alarmIds.size(); i++)
366 service_h handle = NULL;
367 char* alarmType = NULL;
368 error = alarm_get_service(alarmIds.at(i), &handle);
369 if(ALARM_ERROR_NONE!=error) {
370 LogInfo("Getting service failed: "<<error);
373 error =service_get_extra_data(handle, ALARM_TYPE_KEY, &alarmType);
374 if(SERVICE_ERROR_NONE!=error) {
375 LogInfo("Getting data failed: "<<error);
376 service_destroy(handle);
380 LogInfo("Alarm id: " << alarmIds.at(i));
381 LogInfo("Alarm Type: " << alarmType);
383 if(!strcmp(alarmType, ALARM_TYPE_ABSOLUTE_VALUE)) {
384 LogInfo("Enter getAll Natvie alarm id = " << alarmIds.at(i));
385 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute(handle));
386 LogInfo("Success to make Native alarmPtr");
388 if(converter.toAlarmAbsolutePtr(alarmIds.at(i), handle, privateData) == false) {
389 ThrowMsg(ConversionException, "Absolute alarm conversion failed.");
392 JSValueRef obj = JSAlarmAbsolute::createJSObject(ctx, privateData);
393 if(!JSSetArrayElement(ctx, jsResult, i, obj))
395 ThrowMsg(UnknownException, "JS array creation failed.");
397 } else if( !strcmp(alarmType, ALARM_TYPE_RELATIVE_VALUE)) {
398 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative(handle));
399 if(converter.toAlarmRelativePtr(alarmIds.at(i), handle, privateData) == false) {
400 ThrowMsg(ConversionException, "Relative alarm conversion failed.");
402 JSValueRef obj = JSAlarmRelative::createJSObject(ctx, privateData);
403 if(!JSSetArrayElement(ctx, jsResult, i, obj))
405 ThrowMsg(UnknownException, "JS array creation failed.");
408 service_destroy(handle);
412 } Catch(ConversionException) {
413 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
414 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
415 } Catch(UnsupportedException) {
416 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
417 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
418 } Catch(InvalidArgumentException) {
419 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
420 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
421 } Catch (NotFoundException) {
422 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
423 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
425 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
426 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
430 JSValueRef JSAlarmManager::get(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
432 AceSecurityStatus status = ALARM_CHECK_ACCESS(ALARM_FUNCTION_API_GET);
433 TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
437 AlarmConverter converter(ctx);
440 if(argumentCount <1) {
441 value = JSValueMakeUndefined(ctx);
442 ThrowMsg(ConversionException, "Wrong parameter type.");
444 alarmId = converter.toInt(arguments[0]);
447 service_h handle = NULL;
448 char* alarmType = NULL;
449 int error = alarm_get_service(alarmId, &handle);
450 if(error != ALARM_ERROR_NONE) {
451 ThrowMsg(NotFoundException, "Alarm not found");
454 error =service_get_extra_data(handle, ALARM_TYPE_KEY, &alarmType);
455 if(SERVICE_ERROR_NONE!=error) {
456 LogInfo("Getting data failed: "<<error);
457 service_destroy(handle);
458 ThrowMsg(PlatformException, "Unknown error occurred.");
461 if(!strcmp(alarmType, ALARM_TYPE_ABSOLUTE_VALUE))
463 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute(handle));
464 if(converter.toAlarmAbsolutePtr(alarmId, handle, privateData) == false) {
465 ThrowMsg(ConversionException, "Absolute alarm conversion failed.");
468 return JSAlarmAbsolute::createJSObject(ctx, privateData);
470 } else if( !strcmp(alarmType, ALARM_TYPE_RELATIVE_VALUE)) {
471 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative(handle));
472 if(converter.toAlarmRelativePtr(alarmId, handle, privateData) == false) {
473 ThrowMsg(ConversionException, "Relative alarm conversion failed.");
475 return JSAlarmRelative::createJSObject(ctx, privateData);
477 } Catch(ConversionException) {
478 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
479 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
480 } Catch(UnsupportedException) {
481 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
482 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
483 } Catch(InvalidArgumentException) {
484 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
485 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
486 } Catch (NotFoundException) {
487 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
488 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
490 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
491 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
494 return JSValueMakeUndefined(ctx);
497 JSValueRef JSAlarmManager::getMin(JSContextRef ctx,
499 JSStringRef propertyName,
500 JSValueRef* exception)
502 Converter converter(ctx);
503 return converter.toJSValueRef(60);
507 JSValueRef JSAlarmManager::getHour(JSContextRef ctx,
509 JSStringRef propertyName,
510 JSValueRef* exception)
512 Converter converter(ctx);
513 return converter.toJSValueRef(3600);
516 JSValueRef JSAlarmManager::getDay(JSContextRef ctx,
518 JSStringRef propertyName,
519 JSValueRef* exception)
521 Converter converter(ctx);
522 return converter.toJSValueRef(86400);
525 JSValueRef JSAlarmManager::getWeek(JSContextRef ctx,
527 JSStringRef propertyName,
528 JSValueRef* exception)
530 Converter converter(ctx);
531 return converter.toJSValueRef(604800);
534 static ail_cb_ret_e appinfoCallback(const ail_appinfo_h appinfo, void *user_data)
536 if (appinfo == NULL || user_data == NULL) {
537 return AIL_CB_RET_CANCEL;
540 char** pkgStrPtr = (char**)user_data;
544 ret = ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &tmpStr);
545 if (ret != AIL_ERROR_OK) {
546 return AIL_CB_RET_CONTINUE;
549 if (tmpStr != NULL) {
550 *pkgStrPtr = strdup(tmpStr);
552 return AIL_CB_RET_CANCEL;
555 // get package name by id
556 char* JSAlarmManager::getPackageById(const char* appId)
558 LogDebug("<<< appId:[" << appId << "]");
569 ret = ail_filter_new(&filter);
570 if (ret != AIL_ERROR_OK) {
574 ret = ail_filter_add_str(filter, AIL_PROP_X_SLP_PACKAGEID_STR , appId);
575 if (ret != AIL_ERROR_OK) {
576 ail_filter_destroy(filter);
580 ail_filter_list_appinfo_foreach(filter, appinfoCallback, &pkg);
581 ail_filter_destroy(filter);
583 LogDebug(">>> pkg:[" << pkg << "]");