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>
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/Converter.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <CommonsJavaScript/Utils.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/ScopedJSStringRef.h>
29 #include <JSTizenException.h>
30 #include <JSTizenExceptionFactory.h>
31 #include <SecurityExceptions.h>
33 #include "OnDataSyncStateChanged.h"
34 #include "DataSyncManager.h"
36 #include "JSDataSyncManager.h"
37 #include "DataSyncConverter.h"
38 #include "JSSyncInfo.h"
39 #include "JSSyncServiceInfo.h"
40 #include "JSSyncProfileInfo.h"
41 #include "plugin_config.h"
42 #include "DataSyncResponseDispatcher.h"
43 #include "DataSyncMultiCallback.h"
44 #include "DataSyncListenerManager.h"
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace DeviceAPI::Common;
53 JSClassDefinition JSDataSyncManager::m_classInfo = {
55 kJSClassAttributeNone,
56 TIZEN_DATA_SYNC_MANAGER_INTERFACE,
65 NULL, //DeleteProperty,
66 NULL, //GetPropertyNames,
67 NULL, //CallAsFunction,
68 NULL, //CallAsConstructor,
73 JSStaticFunction JSDataSyncManager::m_function[] = {
74 { DATASYNC_FUNCTION_API_ADD, add, kJSPropertyAttributeNone },
75 { DATASYNC_FUNCTION_API_UPDATE, update, kJSPropertyAttributeNone },
76 { DATASYNC_FUNCTION_API_REMOVE, remove, kJSPropertyAttributeNone },
77 { DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM, getMaxProfilesNum, kJSPropertyAttributeNone },
78 { DATASYNC_FUNCTION_API_GET_PROFILES_NUM, getProfilesNum, kJSPropertyAttributeNone },
79 { DATASYNC_FUNCTION_API_GET, get, kJSPropertyAttributeNone },
80 { DATASYNC_FUNCTION_API_GET_ALL, getAll, kJSPropertyAttributeNone },
81 { DATASYNC_FUNCTION_API_START_SYNC, startSync, kJSPropertyAttributeNone },
82 { DATASYNC_FUNCTION_API_STOP_SYNC, stopSync, kJSPropertyAttributeNone },
83 { DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS, getLastSyncStatistics, kJSPropertyAttributeNone },
88 JSClassRef JSDataSyncManager::m_jsClassRef = JSClassCreate(JSDataSyncManager::getClassInfo());
90 void JSDataSyncManager::initialize(JSContextRef context, JSObjectRef object)
92 if (!JSObjectGetPrivate(object)) {
93 LogInfo("Create datasync manager private object.");
94 IDataSyncManagerPtr datasync(new DataSyncManager());
95 DataSyncManagerPrivObject *priv = new DataSyncManagerPrivObject(context, datasync);
96 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
100 LogInfo("Private object already set.");
104 void JSDataSyncManager::finalize(JSObjectRef object)
106 DataSyncManagerPrivObject *priv = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(object));
108 LogInfo("Deleting datasync manager private object.");
110 JSObjectSetPrivate(object, NULL);
114 const JSClassRef JSDataSyncManager::getClassRef()
117 m_jsClassRef = JSClassCreate(&m_classInfo);
122 const JSClassDefinition* JSDataSyncManager::getClassInfo()
127 JSValueRef JSDataSyncManager::add(JSContextRef context,
129 JSObjectRef thisObject,
130 size_t argumentCount,
131 const JSValueRef arguments[],
132 JSValueRef* exception)
134 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
136 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_ADD);
138 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
142 if (!privateObject) {
143 ThrowMsg(ConversionException, "Object is null.");
146 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
148 DataSyncConverter converter(context);
150 SyncProfileInfoPtr profile;
151 if (argumentCount>=1) {
152 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
153 ThrowMsg(ConversionException, "Wrong parameter type.");
156 ThrowMsg(ConversionException, "Wrong parameter type.");
159 profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
161 ThrowMsg(ConversionException, "Parameter conversion failed.");
164 LogDebug("profileName: "<<profile->getProfileName());
166 IEventAddProfilePtr dplEvent(new IEventAddProfile());
168 dplEvent->setProfile(profile);
169 dplEvent->setForSynchronousCall();
170 datasyncManager->addProfile(dplEvent);
172 if (dplEvent->getResult()) {
173 LogDebug("Add succeeded with id: "<<dplEvent->getProfile()->getProfileId());
174 return JSValueMakeUndefined(context);
176 if (ExceptionCodes::OutOfRangeException==dplEvent->getExceptionCode()) {
177 ThrowMsg(OutOfRangeException, "No more profiles allowed by platform.");
179 ThrowMsg(UnknownException, "Add failed by unknown reason.");
183 Catch(OutOfRangeException)
185 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
186 return JSTizenExceptionFactory::postException(context, exception, "QuotaExceededError", _rethrown_exception.GetMessage());
188 Catch(UnsupportedException)
190 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
191 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
193 Catch(InvalidArgumentException)
195 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
196 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
198 Catch(ConversionException)
200 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
201 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
205 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
206 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
210 JSValueRef JSDataSyncManager::update(JSContextRef context,
212 JSObjectRef thisObject,
213 size_t argumentCount,
214 const JSValueRef arguments[],
215 JSValueRef* exception)
217 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
219 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_UPDATE);
221 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
225 if (!privateObject) {
226 ThrowMsg(ConversionException, "Object is null.");
229 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
231 DataSyncConverter converter(context);
233 SyncProfileInfoPtr profile;
234 if (argumentCount>=1) {
235 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
236 ThrowMsg(ConversionException, "Wrong parameter type.");
239 ThrowMsg(ConversionException, "Wrong parameter type.");
242 profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
244 ThrowMsg(ConversionException, "Parameter conversion failed.");
247 LogDebug("profileName: "<<profile->getProfileName());
249 IEventUpdateProfilePtr dplEvent(new IEventUpdateProfile());
251 dplEvent->setProfile(profile);
252 dplEvent->setForSynchronousCall();
253 datasyncManager->updateProfile(dplEvent);
255 if (dplEvent->getResult()) {
256 LogDebug("Update succeeded with id: "<<dplEvent->getProfile()->getProfileId());
257 return JSValueMakeUndefined(context);
259 ThrowMsg(UnknownException, "Update failed by unknown reason.");
262 Catch(UnsupportedException)
264 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
265 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
267 Catch(InvalidArgumentException)
269 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
270 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
272 Catch(ConversionException)
274 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
275 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
279 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
280 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
284 JSValueRef JSDataSyncManager::remove(JSContextRef context,
286 JSObjectRef thisObject,
287 size_t argumentCount,
288 const JSValueRef arguments[],
289 JSValueRef* exception)
291 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
293 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_REMOVE);
295 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
299 if (!privateObject) {
300 ThrowMsg(ConversionException, "Object is null.");
303 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
305 DataSyncConverter converter(context);
307 std::string profileId;
308 if (argumentCount>=1) {
309 profileId = converter.toString(arguments[0]);
312 LogDebug("profileId: "<<profileId);
314 IEventRemoveProfilePtr dplEvent(new IEventRemoveProfile());
316 dplEvent->setProfileId(profileId);
317 dplEvent->setForSynchronousCall();
318 datasyncManager->removeProfile(dplEvent);
320 if (dplEvent->getResult()) {
321 LogDebug("Remove succeeded with id: "<<dplEvent->getProfileId());
322 return JSValueMakeUndefined(context);
324 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
325 ThrowMsg(NotFoundException, "Id not found.");
327 ThrowMsg(UnknownException, "Remove failed by unknown reason.");
331 Catch (NotFoundException)
333 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
334 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
336 Catch(UnsupportedException)
338 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
339 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
341 Catch(InvalidArgumentException)
343 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
344 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
346 Catch(ConversionException)
348 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
349 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
353 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
354 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
358 JSValueRef JSDataSyncManager::getMaxProfilesNum(JSContextRef context,
360 JSObjectRef thisObject,
361 size_t argumentCount,
362 const JSValueRef arguments[],
363 JSValueRef* exception)
365 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
367 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM);
369 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
373 if (!privateObject) {
374 ThrowMsg(ConversionException, "Object is null.");
377 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
379 DataSyncConverter converter(context);
381 IEventGetMaxProfilesNumPtr dplEvent(new IEventGetMaxProfilesNum());
383 dplEvent->setForSynchronousCall();
384 datasyncManager->getMaxProfilesNum(dplEvent);
386 if (dplEvent->getResult()) {
387 LogDebug("Max number of profiles: "<<dplEvent->getNumMaxProfiles());
388 return converter.toJSValueRef(dplEvent->getNumMaxProfiles());
390 ThrowMsg(UnknownException, "Get the max number of profiles failed by unknown reason.");
393 Catch(UnsupportedException)
395 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
396 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
398 Catch(InvalidArgumentException)
400 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
401 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
403 Catch(ConversionException)
405 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
406 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
410 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
411 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
415 JSValueRef JSDataSyncManager::getProfilesNum(JSContextRef context,
417 JSObjectRef thisObject,
418 size_t argumentCount,
419 const JSValueRef arguments[],
420 JSValueRef* exception)
422 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
424 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_PROFILES_NUM);
426 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
430 if (!privateObject) {
431 ThrowMsg(ConversionException, "Object is null.");
434 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
436 DataSyncConverter converter(context);
438 IEventGetProfilesNumPtr dplEvent(new IEventGetProfilesNum());
440 dplEvent->setForSynchronousCall();
441 datasyncManager->getProfilesNum(dplEvent);
443 if (dplEvent->getResult()) {
444 LogDebug("Number of profiles: "<<dplEvent->getNumProfiles());
445 return converter.toJSValueRef(dplEvent->getNumProfiles());
447 ThrowMsg(UnknownException, "Get the number of profiles failed by unknown reason.");
450 Catch(UnsupportedException)
452 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
453 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
455 Catch(InvalidArgumentException)
457 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
458 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
460 Catch(ConversionException)
462 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
463 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
467 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
468 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
472 JSValueRef JSDataSyncManager::get(JSContextRef context,
474 JSObjectRef thisObject,
475 size_t argumentCount,
476 const JSValueRef arguments[],
477 JSValueRef* exception)
479 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
481 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET);
483 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
487 if (!privateObject) {
488 ThrowMsg(ConversionException, "Object is null.");
491 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
493 DataSyncConverter converter(context);
495 std::string profileId;
496 if (argumentCount>=1) {
497 profileId = converter.toString(arguments[0]);
500 LogDebug("profileId: "<<profileId);
502 IEventGetProfilePtr dplEvent(new IEventGetProfile());
504 dplEvent->setProfileId(profileId);
505 dplEvent->setForSynchronousCall();
506 datasyncManager->getProfile(dplEvent);
508 if (dplEvent->getResult()) {
509 LogDebug("Get succeeded with id: "<<dplEvent->getProfileId());
510 return JSSyncProfileInfo::createJSSyncProfileInfo(context, dplEvent->getProfile());
512 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
513 ThrowMsg(NotFoundException, "Id not found.");
515 ThrowMsg(UnknownException, "Get failed by unknown reason.");
519 Catch (NotFoundException)
521 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
522 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
524 Catch(UnsupportedException)
526 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
527 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
529 Catch(InvalidArgumentException)
531 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
532 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
534 Catch(ConversionException)
536 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
537 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
541 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
542 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
546 JSValueRef JSDataSyncManager::getAll(JSContextRef context,
548 JSObjectRef thisObject,
549 size_t argumentCount,
550 const JSValueRef arguments[],
551 JSValueRef* exception)
553 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
555 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_ALL);
557 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
561 if (!privateObject) {
562 ThrowMsg(ConversionException, "Object is null.");
565 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
567 DataSyncConverter converter(context);
569 IEventGetAllProfilesPtr dplEvent(new IEventGetAllProfiles());
571 dplEvent->setForSynchronousCall();
572 datasyncManager->getAllProfiles(dplEvent);
574 if (dplEvent->getResult()) {
575 LogDebug("Getting all profiles succeeded with length: "<<dplEvent->getProfiles()->size());
576 return converter.toJSValueRefProfileInfoList(dplEvent->getProfiles());
578 ThrowMsg(UnknownException, "Getting all profiles failed by unknown reason.");
581 Catch(UnsupportedException)
583 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
584 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
586 Catch(InvalidArgumentException)
588 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
589 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
591 Catch(ConversionException)
593 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
594 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
598 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
599 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
603 JSValueRef JSDataSyncManager::startSync(JSContextRef context,
605 JSObjectRef thisObject,
606 size_t argumentCount,
607 const JSValueRef arguments[],
608 JSValueRef* exception)
610 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
612 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_START_SYNC);
614 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
618 if (!privateObject) {
619 ThrowMsg(ConversionException, "Object is null.");
622 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
624 JSContextRef globalContext = privateObject->getContext();
626 DataSyncConverter converter(context);
628 std::string profileId;
629 if (argumentCount>=1) {
630 profileId = converter.toString(arguments[0]);
632 LogDebug("profileId: "<<profileId);
634 IEventStartSyncPtr dplEvent(new IEventStartSync());
636 if (argumentCount>=2) {
637 LogDebug("Process the listener callback.");
639 if (JSValueIsObject(context, arguments[1])) {
640 LogDebug("Non-null callbacks.");
642 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[1]);
643 JSCallbackManagerPtr onProgressCbm(NULL), onCompletedCbm(NULL), onStoppedCbm(NULL), onFailedCbm(NULL);
644 Validator validator(context);
646 JSValueRef onProgress = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onprogress");
647 if (validator.isNullOrUndefined(onProgress)) {
649 } else if(!validator.isCallback(onProgress)) {
650 ThrowMsg(ConversionException, "Wrong second parameter type.");
653 JSValueRef onCompleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "oncompleted");
654 if (validator.isNullOrUndefined(onCompleted)) {
656 } else if(!validator.isCallback(onCompleted)) {
657 ThrowMsg(ConversionException, "Wrong second parameter type.");
660 JSValueRef onStopped = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onstopped");
661 if (validator.isNullOrUndefined(onStopped)) {
663 } else if(!validator.isCallback(onStopped)) {
664 ThrowMsg(ConversionException, "Wrong second parameter type.");
667 JSValueRef onFailed = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onfailed");
668 if (validator.isNullOrUndefined(onFailed)) {
670 } else if(!validator.isCallback(onFailed)) {
671 ThrowMsg(ConversionException, "Wrong second parameter type.");
674 if (!onProgress && !onCompleted && !onStopped && !onFailed) {
675 ThrowMsg(ConversionException, "Wrong second parameter type.");
678 onProgressCbm = JSCallbackManager::createObject(globalContext, onProgress, NULL);
679 onCompletedCbm = JSCallbackManager::createObject(globalContext, onCompleted, NULL);
680 onStoppedCbm = JSCallbackManager::createObject(globalContext, onStopped, NULL);
681 onFailedCbm = JSCallbackManager::createObject(globalContext, onFailed, NULL);
683 DataSyncStateChangeCallbackPrivateDataPtr privData(new DataSyncStateChangeCallbackPrivateData(onProgressCbm, onCompletedCbm, onStoppedCbm, onFailedCbm));
684 OnDataSyncStateChangedEmitterPtr emitter(new OnDataSyncStateChangedEmitter());
685 emitter->setListener(&DataSyncResponseDispatcher::getInstance());
686 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
688 LogDebug("Set the emitter.");
689 dplEvent->setEmitter(emitter);
690 } else if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) ) {
691 LogDebug("Null or undefined listener.");
693 ThrowMsg(ConversionException, "Wrong second parameter type.");
697 LogDebug("Proceed the start sync event to the platform.");
699 dplEvent->setProfileId(profileId);
700 dplEvent->setForSynchronousCall();
701 datasyncManager->startSync(dplEvent);
703 if (dplEvent->getResult()) {
704 if(dplEvent->getEmitter()) {
705 LogDebug("watcherId: "<<profileId.at(4));
706 DataSyncListenerCancellerPtr canceller = DataSyncListenerCancellerPtr(new DataSyncListenerCanceller(globalContext, thisObject, profileId.at(4)));
707 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
708 DataSyncListenerManagerSingleton::Instance().registerListener(listenerItem, globalContext);
711 LogDebug("Starting the sync succeeded.");
713 switch (dplEvent->getExceptionCode()) {
714 case ExceptionCodes::NotFoundException:
715 ThrowMsg(NotFoundException, "Id not found.");
717 case ExceptionCodes::InvalidArgumentException:
718 ThrowMsg(InvalidArgumentException, "Invalid argument");
721 ThrowMsg(UnknownException, "Starting the sync failed by unknown reason.");
726 Catch(UnsupportedException)
728 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
729 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
731 Catch(InvalidArgumentException)
733 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
734 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
736 Catch(ConversionException)
738 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
739 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
741 Catch (NotFoundException)
743 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
744 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
748 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
749 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
752 return JSValueMakeUndefined(context);
755 JSValueRef JSDataSyncManager::stopSync(JSContextRef context,
757 JSObjectRef thisObject,
758 size_t argumentCount,
759 const JSValueRef arguments[],
760 JSValueRef* exception)
762 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
764 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_STOP_SYNC);
766 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
770 if (!privateObject) {
771 ThrowMsg(ConversionException, "Object is null.");
774 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
776 DataSyncConverter converter(context);
778 std::string profileId;
779 if (argumentCount>=1) {
780 profileId = converter.toString(arguments[0]);
782 LogDebug("profileId: "<<profileId);
784 IEventStopSyncPtr dplEvent(new IEventStopSync());
786 dplEvent->setProfileId(profileId);
787 dplEvent->setForSynchronousCall();
788 datasyncManager->stopSync(dplEvent);
790 if (dplEvent->getResult()) {
791 LogDebug("Stop sync succeeded.");
793 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
794 ThrowMsg(NotFoundException, "Id not found.");
796 ThrowMsg(UnknownException, "Stopping the sync failed by unknown reason.");
800 Catch(UnsupportedException)
802 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
803 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
805 Catch(InvalidArgumentException)
807 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
808 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
810 Catch(ConversionException)
812 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
813 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
815 Catch (NotFoundException)
817 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
818 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
822 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
823 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
826 return JSValueMakeUndefined(context);
830 JSValueRef JSDataSyncManager::getLastSyncStatistics(JSContextRef context,
832 JSObjectRef thisObject,
833 size_t argumentCount,
834 const JSValueRef arguments[],
835 JSValueRef* exception)
837 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
839 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS);
841 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
845 if (!privateObject) {
846 ThrowMsg(ConversionException, "Object is null.");
849 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
851 DataSyncConverter converter(context);
853 std::string profileId;
854 if (argumentCount>=1) {
855 profileId = converter.toString(arguments[0]);
858 LogDebug("profileId: "<<profileId);
860 IEventGetLastSyncStatisticsPtr dplEvent(new IEventGetLastSyncStatistics());
862 dplEvent->setProfileId(profileId);
863 dplEvent->setForSynchronousCall();
864 datasyncManager->getLastSyncStatistics(dplEvent);
866 if (dplEvent->getResult()) {
867 LogDebug("Get sync statistics succeeded with length: "<<dplEvent->getSyncStatistics()->size());
868 return converter.toJSValueRefSyncStatisticsList(dplEvent->getSyncStatistics());
870 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
871 ThrowMsg(NotFoundException, "Id not found.");
873 ThrowMsg(UnknownException, "Get statistics failed by unknown reason.");
877 Catch (NotFoundException)
879 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
880 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
882 Catch(UnsupportedException)
884 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
885 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
887 Catch(InvalidArgumentException)
889 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
890 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
892 Catch(ConversionException)
894 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
895 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
899 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
900 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());