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 <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <CommonsJavaScript/Validator.h>
24 #include <CommonsJavaScript/ScopedJSStringRef.h>
25 #include <JSTizenException.h>
26 #include <JSTizenExceptionFactory.h>
27 #include <SecurityExceptions.h>
29 #include "OnDataSyncStateChanged.h"
30 #include "DataSyncManager.h"
31 #include "JSDataSyncManager.h"
32 #include "DataSyncConverter.h"
33 #include "JSSyncInfo.h"
34 #include "JSSyncServiceInfo.h"
35 #include "JSSyncProfileInfo.h"
36 #include "plugin_config.h"
37 #include "DataSyncResponseDispatcher.h"
38 #include "DataSyncMultiCallback.h"
39 #include "DataSyncListenerManager.h"
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
44 using namespace DeviceAPI::Common;
49 JSClassDefinition JSDataSyncManager::m_classInfo = {
51 kJSClassAttributeNone,
52 TIZEN_DATA_SYNC_MANAGER_INTERFACE,
61 NULL, //DeleteProperty,
62 NULL, //GetPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
69 JSStaticFunction JSDataSyncManager::m_function[] = {
70 { DATASYNC_FUNCTION_API_ADD, add, kJSPropertyAttributeNone },
71 { DATASYNC_FUNCTION_API_UPDATE, update, kJSPropertyAttributeNone },
72 { DATASYNC_FUNCTION_API_REMOVE, remove, kJSPropertyAttributeNone },
73 { DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM, getMaxProfilesNum, kJSPropertyAttributeNone },
74 { DATASYNC_FUNCTION_API_GET_PROFILES_NUM, getProfilesNum, kJSPropertyAttributeNone },
75 { DATASYNC_FUNCTION_API_GET, get, kJSPropertyAttributeNone },
76 { DATASYNC_FUNCTION_API_GET_ALL, getAll, kJSPropertyAttributeNone },
77 { DATASYNC_FUNCTION_API_START_SYNC, startSync, kJSPropertyAttributeNone },
78 { DATASYNC_FUNCTION_API_STOP_SYNC, stopSync, kJSPropertyAttributeNone },
79 { DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS, getLastSyncStatistics, kJSPropertyAttributeNone },
84 JSClassRef JSDataSyncManager::m_jsClassRef = JSClassCreate(JSDataSyncManager::getClassInfo());
86 void JSDataSyncManager::initialize(JSContextRef context, JSObjectRef object)
88 if (!JSObjectGetPrivate(object)) {
89 LoggerI("Create datasync manager private object.");
90 IDataSyncManagerPtr datasync(new DataSyncManager());
91 DataSyncManagerPrivObject *priv = new DataSyncManagerPrivObject(context, datasync);
92 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
96 LoggerI("Private object already set.");
100 void JSDataSyncManager::finalize(JSObjectRef object)
102 DataSyncManagerPrivObject *priv = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(object));
104 LoggerI("Deleting datasync manager private object.");
106 JSObjectSetPrivate(object, NULL);
110 const JSClassRef JSDataSyncManager::getClassRef()
113 m_jsClassRef = JSClassCreate(&m_classInfo);
118 const JSClassDefinition* JSDataSyncManager::getClassInfo()
123 JSValueRef JSDataSyncManager::add(JSContextRef context,
125 JSObjectRef thisObject,
126 size_t argumentCount,
127 const JSValueRef arguments[],
128 JSValueRef* exception)
130 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
132 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_ADD);
134 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
138 if (!privateObject) {
139 ThrowMsg(ConversionException, "Object is null.");
142 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
144 DataSyncConverter converter(context);
146 SyncProfileInfoPtr profile;
147 if (argumentCount>=1) {
148 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
149 ThrowMsg(ConversionException, "Wrong parameter type.");
152 ThrowMsg(ConversionException, "Wrong parameter type.");
155 profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
157 ThrowMsg(ConversionException, "Parameter conversion failed.");
160 LoggerD("profileName: "<<profile->getProfileName());
162 IEventAddProfilePtr dplEvent(new IEventAddProfile());
164 dplEvent->setProfile(profile);
165 dplEvent->setForSynchronousCall();
166 datasyncManager->addProfile(dplEvent);
168 if (dplEvent->getResult()) {
169 LoggerD("Add succeeded with id: "<<dplEvent->getProfile()->getProfileId());
170 return JSValueMakeUndefined(context);
172 if (ExceptionCodes::OutOfRangeException==dplEvent->getExceptionCode()) {
173 ThrowMsg(OutOfRangeException, "No more profiles allowed by platform.");
175 ThrowMsg(UnknownException, "Add failed by unknown reason.");
179 Catch(OutOfRangeException)
181 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
182 return JSTizenExceptionFactory::postException(context, exception, "QuotaExceededError", _rethrown_exception.GetMessage());
184 Catch(UnsupportedException)
186 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
187 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
189 Catch(InvalidArgumentException)
191 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
192 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
194 Catch(ConversionException)
196 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
197 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
201 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
202 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
206 JSValueRef JSDataSyncManager::update(JSContextRef context,
208 JSObjectRef thisObject,
209 size_t argumentCount,
210 const JSValueRef arguments[],
211 JSValueRef* exception)
213 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
215 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_UPDATE);
217 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
221 if (!privateObject) {
222 ThrowMsg(ConversionException, "Object is null.");
225 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
227 DataSyncConverter converter(context);
229 SyncProfileInfoPtr profile;
230 if (argumentCount>=1) {
231 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
232 ThrowMsg(ConversionException, "Wrong parameter type.");
235 ThrowMsg(ConversionException, "Wrong parameter type.");
238 profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
240 ThrowMsg(ConversionException, "Parameter conversion failed.");
243 LoggerD("profileName: "<<profile->getProfileName());
245 IEventUpdateProfilePtr dplEvent(new IEventUpdateProfile());
247 dplEvent->setProfile(profile);
248 dplEvent->setForSynchronousCall();
249 datasyncManager->updateProfile(dplEvent);
251 if (dplEvent->getResult()) {
252 LoggerD("Update succeeded with id: "<<dplEvent->getProfile()->getProfileId());
253 return JSValueMakeUndefined(context);
255 ThrowMsg(UnknownException, "Update failed by unknown reason.");
258 Catch(UnsupportedException)
260 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
261 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
263 Catch(InvalidArgumentException)
265 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
266 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
268 Catch(ConversionException)
270 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
271 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
275 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
276 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
280 JSValueRef JSDataSyncManager::remove(JSContextRef context,
282 JSObjectRef thisObject,
283 size_t argumentCount,
284 const JSValueRef arguments[],
285 JSValueRef* exception)
287 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
289 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_REMOVE);
291 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
295 if (!privateObject) {
296 ThrowMsg(ConversionException, "Object is null.");
299 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
301 DataSyncConverter converter(context);
303 std::string profileId;
304 if (argumentCount>=1) {
305 profileId = converter.toString(arguments[0]);
308 LoggerD("profileId: "<<profileId);
310 IEventRemoveProfilePtr dplEvent(new IEventRemoveProfile());
312 dplEvent->setProfileId(profileId);
313 dplEvent->setForSynchronousCall();
314 datasyncManager->removeProfile(dplEvent);
316 if (dplEvent->getResult()) {
317 LoggerD("Remove succeeded with id: "<<dplEvent->getProfileId());
318 return JSValueMakeUndefined(context);
320 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
321 ThrowMsg(NotFoundException, "Id not found.");
323 ThrowMsg(UnknownException, "Remove failed by unknown reason.");
327 Catch (NotFoundException)
329 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
330 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
332 Catch(UnsupportedException)
334 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
335 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
337 Catch(InvalidArgumentException)
339 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
340 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
342 Catch(ConversionException)
344 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
345 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
349 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
350 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
354 JSValueRef JSDataSyncManager::getMaxProfilesNum(JSContextRef context,
356 JSObjectRef thisObject,
357 size_t argumentCount,
358 const JSValueRef arguments[],
359 JSValueRef* exception)
361 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
363 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM);
365 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
369 if (!privateObject) {
370 ThrowMsg(ConversionException, "Object is null.");
373 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
375 DataSyncConverter converter(context);
377 IEventGetMaxProfilesNumPtr dplEvent(new IEventGetMaxProfilesNum());
379 dplEvent->setForSynchronousCall();
380 datasyncManager->getMaxProfilesNum(dplEvent);
382 if (dplEvent->getResult()) {
383 LoggerD("Max number of profiles: "<<dplEvent->getNumMaxProfiles());
384 return converter.toJSValueRef(dplEvent->getNumMaxProfiles());
386 ThrowMsg(UnknownException, "Get the max number of profiles failed by unknown reason.");
389 Catch(UnsupportedException)
391 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
392 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
394 Catch(InvalidArgumentException)
396 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
397 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
399 Catch(ConversionException)
401 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
402 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
406 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
407 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
411 JSValueRef JSDataSyncManager::getProfilesNum(JSContextRef context,
413 JSObjectRef thisObject,
414 size_t argumentCount,
415 const JSValueRef arguments[],
416 JSValueRef* exception)
418 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
420 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_PROFILES_NUM);
422 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
426 if (!privateObject) {
427 ThrowMsg(ConversionException, "Object is null.");
430 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
432 DataSyncConverter converter(context);
434 IEventGetProfilesNumPtr dplEvent(new IEventGetProfilesNum());
436 dplEvent->setForSynchronousCall();
437 datasyncManager->getProfilesNum(dplEvent);
439 if (dplEvent->getResult()) {
440 LoggerD("Number of profiles: "<<dplEvent->getNumProfiles());
441 return converter.toJSValueRef(dplEvent->getNumProfiles());
443 ThrowMsg(UnknownException, "Get the number of profiles failed by unknown reason.");
446 Catch(UnsupportedException)
448 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
449 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
451 Catch(InvalidArgumentException)
453 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
454 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
456 Catch(ConversionException)
458 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
459 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
463 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
464 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
468 JSValueRef JSDataSyncManager::get(JSContextRef context,
470 JSObjectRef thisObject,
471 size_t argumentCount,
472 const JSValueRef arguments[],
473 JSValueRef* exception)
475 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
477 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET);
479 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
483 if (!privateObject) {
484 ThrowMsg(ConversionException, "Object is null.");
487 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
489 DataSyncConverter converter(context);
491 std::string profileId;
492 if (argumentCount>=1) {
493 profileId = converter.toString(arguments[0]);
496 LoggerD("profileId: "<<profileId);
498 IEventGetProfilePtr dplEvent(new IEventGetProfile());
500 dplEvent->setProfileId(profileId);
501 dplEvent->setForSynchronousCall();
502 datasyncManager->getProfile(dplEvent);
504 if (dplEvent->getResult()) {
505 LoggerD("Get succeeded with id: "<<dplEvent->getProfileId());
506 return JSSyncProfileInfo::createJSSyncProfileInfo(context, dplEvent->getProfile());
508 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
509 ThrowMsg(NotFoundException, "Id not found.");
511 ThrowMsg(UnknownException, "Get failed by unknown reason.");
515 Catch (NotFoundException)
517 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
518 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
520 Catch(UnsupportedException)
522 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
523 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
525 Catch(InvalidArgumentException)
527 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
528 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
530 Catch(ConversionException)
532 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
533 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
537 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
538 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
542 JSValueRef JSDataSyncManager::getAll(JSContextRef context,
544 JSObjectRef thisObject,
545 size_t argumentCount,
546 const JSValueRef arguments[],
547 JSValueRef* exception)
549 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
551 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_ALL);
553 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
557 if (!privateObject) {
558 ThrowMsg(ConversionException, "Object is null.");
561 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
563 DataSyncConverter converter(context);
565 IEventGetAllProfilesPtr dplEvent(new IEventGetAllProfiles());
567 dplEvent->setForSynchronousCall();
568 datasyncManager->getAllProfiles(dplEvent);
570 if (dplEvent->getResult()) {
571 LoggerD("Getting all profiles succeeded with length: "<<dplEvent->getProfiles()->size());
572 return converter.toJSValueRefProfileInfoList(dplEvent->getProfiles());
574 ThrowMsg(UnknownException, "Getting all profiles failed by unknown reason.");
577 Catch(UnsupportedException)
579 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
580 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
582 Catch(InvalidArgumentException)
584 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
585 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
587 Catch(ConversionException)
589 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
590 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
594 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
595 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
599 JSValueRef JSDataSyncManager::startSync(JSContextRef context,
601 JSObjectRef thisObject,
602 size_t argumentCount,
603 const JSValueRef arguments[],
604 JSValueRef* exception)
606 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
608 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_START_SYNC);
610 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
614 if (!privateObject) {
615 ThrowMsg(ConversionException, "Object is null.");
618 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
620 JSContextRef globalContext = privateObject->getContext();
622 DataSyncConverter converter(context);
624 std::string profileId;
625 if (argumentCount>=1) {
626 profileId = converter.toString(arguments[0]);
628 LoggerD("profileId: "<<profileId);
630 IEventStartSyncPtr dplEvent(new IEventStartSync());
632 if (argumentCount>=2) {
633 LoggerD("Process the listener callback.");
635 if (JSValueIsObject(context, arguments[1])) {
636 LoggerD("Non-null callbacks.");
638 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[1]);
639 JSCallbackManagerPtr onProgressCbm(NULL), onCompletedCbm(NULL), onStoppedCbm(NULL), onFailedCbm(NULL);
640 Validator validator(context);
642 JSValueRef onProgress = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onprogress");
643 if (validator.isNullOrUndefined(onProgress)) {
645 } else if(!validator.isCallback(onProgress)) {
646 ThrowMsg(ConversionException, "Wrong second parameter type.");
649 JSValueRef onCompleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "oncompleted");
650 if (validator.isNullOrUndefined(onCompleted)) {
652 } else if(!validator.isCallback(onCompleted)) {
653 ThrowMsg(ConversionException, "Wrong second parameter type.");
656 JSValueRef onStopped = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onstopped");
657 if (validator.isNullOrUndefined(onStopped)) {
659 } else if(!validator.isCallback(onStopped)) {
660 ThrowMsg(ConversionException, "Wrong second parameter type.");
663 JSValueRef onFailed = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onfailed");
664 if (validator.isNullOrUndefined(onFailed)) {
666 } else if(!validator.isCallback(onFailed)) {
667 ThrowMsg(ConversionException, "Wrong second parameter type.");
670 if (!onProgress && !onCompleted && !onStopped && !onFailed) {
671 ThrowMsg(ConversionException, "Wrong second parameter type.");
674 onProgressCbm = JSCallbackManager::createObject(globalContext, onProgress, NULL);
675 onCompletedCbm = JSCallbackManager::createObject(globalContext, onCompleted, NULL);
676 onStoppedCbm = JSCallbackManager::createObject(globalContext, onStopped, NULL);
677 onFailedCbm = JSCallbackManager::createObject(globalContext, onFailed, NULL);
679 DataSyncStateChangeCallbackPrivateDataPtr privData(new DataSyncStateChangeCallbackPrivateData(onProgressCbm, onCompletedCbm, onStoppedCbm, onFailedCbm));
680 OnDataSyncStateChangedEmitterPtr emitter(new OnDataSyncStateChangedEmitter());
681 emitter->setListener(&DataSyncResponseDispatcher::getInstance());
682 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
684 LoggerD("Set the emitter.");
685 dplEvent->setEmitter(emitter);
686 } else if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) ) {
687 LoggerD("Null or undefined listener.");
689 ThrowMsg(ConversionException, "Wrong second parameter type.");
693 LoggerD("Proceed the start sync event to the platform.");
695 dplEvent->setProfileId(profileId);
696 dplEvent->setForSynchronousCall();
697 datasyncManager->startSync(dplEvent);
699 if (dplEvent->getResult()) {
700 if(dplEvent->getEmitter()) {
701 LoggerD("watcherId: "<<profileId.at(4));
702 DataSyncListenerCancellerPtr canceller = DataSyncListenerCancellerPtr(new DataSyncListenerCanceller(globalContext, thisObject, profileId.at(4)));
703 IListenerItemPtr listenerItem = DPL::StaticPointerCast<IListenerItem>(canceller);
704 DataSyncListenerManagerSingleton::Instance().registerListener(listenerItem, globalContext);
707 LoggerD("Starting the sync succeeded.");
709 switch (dplEvent->getExceptionCode()) {
710 case ExceptionCodes::NotFoundException:
711 ThrowMsg(NotFoundException, "Id not found.");
713 case ExceptionCodes::InvalidArgumentException:
714 ThrowMsg(InvalidArgumentException, "Invalid argument");
717 ThrowMsg(UnknownException, "Starting the sync failed by unknown reason.");
722 Catch(UnsupportedException)
724 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
725 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
727 Catch(InvalidArgumentException)
729 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
730 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
732 Catch(ConversionException)
734 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
735 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
737 Catch (NotFoundException)
739 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
740 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
744 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
745 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
748 return JSValueMakeUndefined(context);
751 JSValueRef JSDataSyncManager::stopSync(JSContextRef context,
753 JSObjectRef thisObject,
754 size_t argumentCount,
755 const JSValueRef arguments[],
756 JSValueRef* exception)
758 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
760 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_STOP_SYNC);
762 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
766 if (!privateObject) {
767 ThrowMsg(ConversionException, "Object is null.");
770 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
772 DataSyncConverter converter(context);
774 std::string profileId;
775 if (argumentCount>=1) {
776 profileId = converter.toString(arguments[0]);
778 LoggerD("profileId: "<<profileId);
780 IEventStopSyncPtr dplEvent(new IEventStopSync());
782 dplEvent->setProfileId(profileId);
783 dplEvent->setForSynchronousCall();
784 datasyncManager->stopSync(dplEvent);
786 if (dplEvent->getResult()) {
787 LoggerD("Stop sync succeeded.");
789 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
790 ThrowMsg(NotFoundException, "Id not found.");
792 ThrowMsg(UnknownException, "Stopping the sync failed by unknown reason.");
796 Catch(UnsupportedException)
798 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
799 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
801 Catch(InvalidArgumentException)
803 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
804 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
806 Catch(ConversionException)
808 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
809 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
811 Catch (NotFoundException)
813 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
814 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
818 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
819 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
822 return JSValueMakeUndefined(context);
826 JSValueRef JSDataSyncManager::getLastSyncStatistics(JSContextRef context,
828 JSObjectRef thisObject,
829 size_t argumentCount,
830 const JSValueRef arguments[],
831 JSValueRef* exception)
833 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
835 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS);
837 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
841 if (!privateObject) {
842 ThrowMsg(ConversionException, "Object is null.");
845 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
847 DataSyncConverter converter(context);
849 std::string profileId;
850 if (argumentCount>=1) {
851 profileId = converter.toString(arguments[0]);
854 LoggerD("profileId: "<<profileId);
856 IEventGetLastSyncStatisticsPtr dplEvent(new IEventGetLastSyncStatistics());
858 dplEvent->setProfileId(profileId);
859 dplEvent->setForSynchronousCall();
860 datasyncManager->getLastSyncStatistics(dplEvent);
862 if (dplEvent->getResult()) {
863 LoggerD("Get sync statistics succeeded with length: "<<dplEvent->getSyncStatistics()->size());
864 return converter.toJSValueRefSyncStatisticsList(dplEvent->getSyncStatistics());
866 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
867 ThrowMsg(NotFoundException, "Id not found.");
869 ThrowMsg(UnknownException, "Get statistics failed by unknown reason.");
873 Catch (NotFoundException)
875 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
876 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
878 Catch(UnsupportedException)
880 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
881 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
883 Catch(InvalidArgumentException)
885 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
886 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
888 Catch(ConversionException)
890 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
891 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
895 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
896 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());