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.
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <CommonsJavaScript/Validator.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include <JSTizenException.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <SecurityExceptions.h>
30 #include "OnDataSyncStateChanged.h"
31 #include "DataSyncManager.h"
32 #include "JSDataSyncManager.h"
33 #include "DataSyncConverter.h"
34 #include "JSSyncInfo.h"
35 #include "JSSyncServiceInfo.h"
36 #include "JSSyncProfileInfo.h"
37 #include "plugin_config.h"
38 #include "DataSyncResponseDispatcher.h"
39 #include "DataSyncMultiCallback.h"
41 #include <GlobalContextManager.h>
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45 using namespace DeviceAPI::Common;
50 JSClassDefinition JSDataSyncManager::m_classInfo = {
52 kJSClassAttributeNone,
53 TIZEN_DATA_SYNC_MANAGER_INTERFACE,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 NULL, //CallAsConstructor,
70 JSStaticFunction JSDataSyncManager::m_function[] = {
71 { DATASYNC_FUNCTION_API_ADD, add, kJSPropertyAttributeNone },
72 { DATASYNC_FUNCTION_API_UPDATE, update, kJSPropertyAttributeNone },
73 { DATASYNC_FUNCTION_API_REMOVE, remove, kJSPropertyAttributeNone },
74 { DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM, getMaxProfilesNum, kJSPropertyAttributeNone },
75 { DATASYNC_FUNCTION_API_GET_PROFILES_NUM, getProfilesNum, kJSPropertyAttributeNone },
76 { DATASYNC_FUNCTION_API_GET, get, kJSPropertyAttributeNone },
77 { DATASYNC_FUNCTION_API_GET_ALL, getAll, kJSPropertyAttributeNone },
78 { DATASYNC_FUNCTION_API_START_SYNC, startSync, kJSPropertyAttributeNone },
79 { DATASYNC_FUNCTION_API_STOP_SYNC, stopSync, kJSPropertyAttributeNone },
80 { DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS, getLastSyncStatistics, kJSPropertyAttributeNone },
85 JSClassRef JSDataSyncManager::m_jsClassRef = JSClassCreate(JSDataSyncManager::getClassInfo());
87 void JSDataSyncManager::initialize(JSContextRef context, JSObjectRef object)
89 if (!JSObjectGetPrivate(object)) {
90 LoggerI("Create datasync manager private object.");
91 IDataSyncManagerPtr datasync(new DataSyncManager());
92 DataSyncManagerPrivObject *priv = new DataSyncManagerPrivObject(context, datasync);
93 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
97 LoggerI("Private object already set.");
101 void JSDataSyncManager::finalize(JSObjectRef object)
103 DataSyncManagerPrivObject *priv = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(object));
105 LoggerI("Deleting datasync manager private object.");
107 JSObjectSetPrivate(object, NULL);
111 const JSClassRef JSDataSyncManager::getClassRef()
114 m_jsClassRef = JSClassCreate(&m_classInfo);
119 const JSClassDefinition* JSDataSyncManager::getClassInfo()
124 JSValueRef JSDataSyncManager::add(JSContextRef context,
126 JSObjectRef thisObject,
127 size_t argumentCount,
128 const JSValueRef arguments[],
129 JSValueRef* exception)
131 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
133 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_ADD);
135 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
139 if (!privateObject) {
140 ThrowMsg(ConversionException, "Object is null.");
143 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
145 DataSyncConverter converter(context);
147 SyncProfileInfoPtr profile;
148 if (argumentCount>=1) {
149 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
150 ThrowMsg(ConversionException, "Wrong parameter type.");
153 ThrowMsg(ConversionException, "Wrong parameter type.");
156 profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
158 ThrowMsg(ConversionException, "Parameter conversion failed.");
161 LoggerD("profileName: "<<profile->getProfileName());
163 IEventAddProfilePtr dplEvent(new IEventAddProfile());
165 dplEvent->setProfile(profile);
166 dplEvent->setForSynchronousCall();
167 datasyncManager->addProfile(dplEvent);
169 if (dplEvent->getResult()) {
170 LoggerD("Add succeeded with id: "<<dplEvent->getProfile()->getProfileId());
171 return JSValueMakeUndefined(context);
173 if (ExceptionCodes::OutOfRangeException==dplEvent->getExceptionCode()) {
174 ThrowMsg(OutOfRangeException, "No more profiles allowed by platform.");
176 ThrowMsg(UnknownException, "Add failed by unknown reason.");
180 Catch(OutOfRangeException)
182 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
183 return JSTizenExceptionFactory::postException(context, exception, "QuotaExceededError", _rethrown_exception.GetMessage());
185 Catch(UnsupportedException)
187 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
188 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
190 Catch(InvalidArgumentException)
192 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
193 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
195 Catch(ConversionException)
197 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
198 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
202 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
203 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
207 JSValueRef JSDataSyncManager::update(JSContextRef context,
209 JSObjectRef thisObject,
210 size_t argumentCount,
211 const JSValueRef arguments[],
212 JSValueRef* exception)
214 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
216 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_UPDATE);
218 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
222 if (!privateObject) {
223 ThrowMsg(ConversionException, "Object is null.");
226 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
228 DataSyncConverter converter(context);
230 SyncProfileInfoPtr profile;
231 if (argumentCount>=1) {
232 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
233 ThrowMsg(ConversionException, "Wrong parameter type.");
236 ThrowMsg(ConversionException, "Wrong parameter type.");
239 profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
241 ThrowMsg(ConversionException, "Parameter conversion failed.");
244 LoggerD("profileName: "<<profile->getProfileName());
246 IEventUpdateProfilePtr dplEvent(new IEventUpdateProfile());
248 dplEvent->setProfile(profile);
249 dplEvent->setForSynchronousCall();
250 datasyncManager->updateProfile(dplEvent);
252 if (dplEvent->getResult()) {
253 LoggerD("Update succeeded with id: "<<dplEvent->getProfile()->getProfileId());
254 return JSValueMakeUndefined(context);
256 ThrowMsg(UnknownException, "Update failed by unknown reason.");
259 Catch(UnsupportedException)
261 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
262 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
264 Catch(InvalidArgumentException)
266 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
267 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
269 Catch(ConversionException)
271 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
272 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
276 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
277 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
281 JSValueRef JSDataSyncManager::remove(JSContextRef context,
283 JSObjectRef thisObject,
284 size_t argumentCount,
285 const JSValueRef arguments[],
286 JSValueRef* exception)
288 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
290 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_REMOVE);
292 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
296 if (!privateObject) {
297 ThrowMsg(ConversionException, "Object is null.");
300 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
302 DataSyncConverter converter(context);
304 std::string profileId;
305 if (argumentCount>=1) {
306 profileId = converter.toString(arguments[0]);
309 LoggerD("profileId: "<<profileId);
311 IEventRemoveProfilePtr dplEvent(new IEventRemoveProfile());
313 dplEvent->setProfileId(profileId);
314 dplEvent->setForSynchronousCall();
315 datasyncManager->removeProfile(dplEvent);
317 if (dplEvent->getResult()) {
318 LoggerD("Remove succeeded with id: "<<dplEvent->getProfileId());
319 return JSValueMakeUndefined(context);
321 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
322 ThrowMsg(NotFoundException, "Id not found.");
324 ThrowMsg(UnknownException, "Remove failed by unknown reason.");
328 Catch (NotFoundException)
330 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
331 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
333 Catch(UnsupportedException)
335 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
336 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
338 Catch(InvalidArgumentException)
340 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
341 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
343 Catch(ConversionException)
345 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
346 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
350 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
351 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
355 JSValueRef JSDataSyncManager::getMaxProfilesNum(JSContextRef context,
357 JSObjectRef thisObject,
358 size_t argumentCount,
359 const JSValueRef arguments[],
360 JSValueRef* exception)
362 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
364 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM);
366 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
370 if (!privateObject) {
371 ThrowMsg(ConversionException, "Object is null.");
374 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
376 DataSyncConverter converter(context);
378 IEventGetMaxProfilesNumPtr dplEvent(new IEventGetMaxProfilesNum());
380 dplEvent->setForSynchronousCall();
381 datasyncManager->getMaxProfilesNum(dplEvent);
383 if (dplEvent->getResult()) {
384 LoggerD("Max number of profiles: "<<dplEvent->getNumMaxProfiles());
385 return converter.toJSValueRef(dplEvent->getNumMaxProfiles());
387 ThrowMsg(UnknownException, "Get the max number of profiles failed by unknown reason.");
390 Catch(UnsupportedException)
392 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
393 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
395 Catch(InvalidArgumentException)
397 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
398 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
400 Catch(ConversionException)
402 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
403 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
407 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
408 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
412 JSValueRef JSDataSyncManager::getProfilesNum(JSContextRef context,
414 JSObjectRef thisObject,
415 size_t argumentCount,
416 const JSValueRef arguments[],
417 JSValueRef* exception)
419 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
421 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_PROFILES_NUM);
423 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
427 if (!privateObject) {
428 ThrowMsg(ConversionException, "Object is null.");
431 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
433 DataSyncConverter converter(context);
435 IEventGetProfilesNumPtr dplEvent(new IEventGetProfilesNum());
437 dplEvent->setForSynchronousCall();
438 datasyncManager->getProfilesNum(dplEvent);
440 if (dplEvent->getResult()) {
441 LoggerD("Number of profiles: "<<dplEvent->getNumProfiles());
442 return converter.toJSValueRef(dplEvent->getNumProfiles());
444 ThrowMsg(UnknownException, "Get the number of profiles failed by unknown reason.");
447 Catch(UnsupportedException)
449 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
450 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
452 Catch(InvalidArgumentException)
454 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
455 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
457 Catch(ConversionException)
459 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
460 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
464 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
465 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
469 JSValueRef JSDataSyncManager::get(JSContextRef context,
471 JSObjectRef thisObject,
472 size_t argumentCount,
473 const JSValueRef arguments[],
474 JSValueRef* exception)
476 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
478 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET);
480 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
484 if (!privateObject) {
485 ThrowMsg(ConversionException, "Object is null.");
488 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
490 DataSyncConverter converter(context);
492 std::string profileId;
493 if (argumentCount>=1) {
494 profileId = converter.toString(arguments[0]);
497 LoggerD("profileId: "<<profileId);
499 IEventGetProfilePtr dplEvent(new IEventGetProfile());
501 dplEvent->setProfileId(profileId);
502 dplEvent->setForSynchronousCall();
503 datasyncManager->getProfile(dplEvent);
505 if (dplEvent->getResult()) {
506 LoggerD("Get succeeded with id: "<<dplEvent->getProfileId());
507 return JSSyncProfileInfo::createJSSyncProfileInfo(context, dplEvent->getProfile());
509 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
510 ThrowMsg(NotFoundException, "Id not found.");
512 ThrowMsg(UnknownException, "Get failed by unknown reason.");
516 Catch (NotFoundException)
518 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
519 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
521 Catch(UnsupportedException)
523 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
524 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
526 Catch(InvalidArgumentException)
528 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
529 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
531 Catch(ConversionException)
533 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
534 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
538 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
539 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
543 JSValueRef JSDataSyncManager::getAll(JSContextRef context,
545 JSObjectRef thisObject,
546 size_t argumentCount,
547 const JSValueRef arguments[],
548 JSValueRef* exception)
550 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
552 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_ALL);
554 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
558 if (!privateObject) {
559 ThrowMsg(ConversionException, "Object is null.");
562 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
564 DataSyncConverter converter(context);
566 IEventGetAllProfilesPtr dplEvent(new IEventGetAllProfiles());
568 dplEvent->setForSynchronousCall();
569 datasyncManager->getAllProfiles(dplEvent);
571 if (dplEvent->getResult()) {
572 LoggerD("Getting all profiles succeeded with length: "<<dplEvent->getProfiles()->size());
573 return converter.toJSValueRefProfileInfoList(dplEvent->getProfiles());
575 ThrowMsg(UnknownException, "Getting all profiles failed by unknown reason.");
578 Catch(UnsupportedException)
580 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
581 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
583 Catch(InvalidArgumentException)
585 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
586 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
588 Catch(ConversionException)
590 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
591 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
595 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
596 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
600 JSValueRef JSDataSyncManager::startSync(JSContextRef context,
602 JSObjectRef thisObject,
603 size_t argumentCount,
604 const JSValueRef arguments[],
605 JSValueRef* exception)
607 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
609 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_START_SYNC);
611 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
615 if (!privateObject) {
616 ThrowMsg(ConversionException, "Object is null.");
619 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
621 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
623 DataSyncConverter converter(context);
625 std::string profileId;
626 if (argumentCount>=1) {
627 profileId = converter.toString(arguments[0]);
629 LoggerD("profileId: "<<profileId);
631 IEventStartSyncPtr dplEvent(new IEventStartSync());
633 if (argumentCount>=2) {
634 LoggerD("Process the listener callback.");
636 if (JSValueIsObject(context, arguments[1])) {
637 LoggerD("Non-null callbacks.");
639 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[1]);
640 JSCallbackManagerPtr onProgressCbm(NULL), onCompletedCbm(NULL), onStoppedCbm(NULL), onFailedCbm(NULL);
641 Validator validator(context);
643 JSValueRef onProgress = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onprogress");
644 if (validator.isNullOrUndefined(onProgress)) {
646 } else if(!validator.isCallback(onProgress)) {
647 ThrowMsg(ConversionException, "Wrong second parameter type.");
650 JSValueRef onCompleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "oncompleted");
651 if (validator.isNullOrUndefined(onCompleted)) {
653 } else if(!validator.isCallback(onCompleted)) {
654 ThrowMsg(ConversionException, "Wrong second parameter type.");
657 JSValueRef onStopped = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onstopped");
658 if (validator.isNullOrUndefined(onStopped)) {
660 } else if(!validator.isCallback(onStopped)) {
661 ThrowMsg(ConversionException, "Wrong second parameter type.");
664 JSValueRef onFailed = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onfailed");
665 if (validator.isNullOrUndefined(onFailed)) {
667 } else if(!validator.isCallback(onFailed)) {
668 ThrowMsg(ConversionException, "Wrong second parameter type.");
671 if (!onProgress && !onCompleted && !onStopped && !onFailed) {
672 ThrowMsg(ConversionException, "Wrong second parameter type.");
675 onProgressCbm = JSCallbackManager::createObject(globalContext, onProgress, NULL);
676 onCompletedCbm = JSCallbackManager::createObject(globalContext, onCompleted, NULL);
677 onStoppedCbm = JSCallbackManager::createObject(globalContext, onStopped, NULL);
678 onFailedCbm = JSCallbackManager::createObject(globalContext, onFailed, NULL);
680 DataSyncStateChangeCallbackPrivateDataPtr privData(new DataSyncStateChangeCallbackPrivateData(onProgressCbm, onCompletedCbm, onStoppedCbm, onFailedCbm));
681 OnDataSyncStateChangedEmitterPtr emitter(new OnDataSyncStateChangedEmitter());
682 emitter->setListener(&DataSyncResponseDispatcher::getInstance());
683 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(privData));
685 LoggerD("Set the emitter.");
686 dplEvent->setEmitter(emitter);
687 } else if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) ) {
688 LoggerD("Null or undefined listener.");
690 ThrowMsg(ConversionException, "Wrong second parameter type.");
694 LoggerD("Proceed the start sync event to the platform.");
696 dplEvent->setProfileId(profileId);
697 dplEvent->setForSynchronousCall();
698 datasyncManager->startSync(dplEvent);
700 if (dplEvent->getResult()) {
701 if(dplEvent->getEmitter()) {
702 LoggerD("watcherId: "<<profileId.at(0));
705 LoggerD("Starting the sync succeeded.");
707 switch (dplEvent->getExceptionCode()) {
708 case ExceptionCodes::NotFoundException:
709 ThrowMsg(NotFoundException, "Id not found.");
711 case ExceptionCodes::InvalidArgumentException:
712 ThrowMsg(InvalidArgumentException, "Invalid argument");
715 ThrowMsg(UnknownException, "Starting the sync failed by unknown reason.");
720 Catch(UnsupportedException)
722 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
723 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
725 Catch(InvalidArgumentException)
727 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
728 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
730 Catch(ConversionException)
732 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
733 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
735 Catch (NotFoundException)
737 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
738 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
742 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
743 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
746 return JSValueMakeUndefined(context);
749 JSValueRef JSDataSyncManager::stopSync(JSContextRef context,
751 JSObjectRef thisObject,
752 size_t argumentCount,
753 const JSValueRef arguments[],
754 JSValueRef* exception)
756 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
758 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_STOP_SYNC);
760 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
764 if (!privateObject) {
765 ThrowMsg(ConversionException, "Object is null.");
768 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
770 DataSyncConverter converter(context);
772 std::string profileId;
773 if (argumentCount>=1) {
774 profileId = converter.toString(arguments[0]);
776 LoggerD("profileId: "<<profileId);
778 IEventStopSyncPtr dplEvent(new IEventStopSync());
780 dplEvent->setProfileId(profileId);
781 dplEvent->setForSynchronousCall();
782 datasyncManager->stopSync(dplEvent);
784 if (dplEvent->getResult()) {
785 LoggerD("Stop sync succeeded.");
787 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
788 ThrowMsg(NotFoundException, "Id not found.");
790 ThrowMsg(UnknownException, "Stopping the sync failed by unknown reason.");
794 Catch(UnsupportedException)
796 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
797 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
799 Catch(InvalidArgumentException)
801 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
802 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
804 Catch(ConversionException)
806 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
807 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
809 Catch (NotFoundException)
811 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
812 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
816 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
817 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
820 return JSValueMakeUndefined(context);
824 JSValueRef JSDataSyncManager::getLastSyncStatistics(JSContextRef context,
826 JSObjectRef thisObject,
827 size_t argumentCount,
828 const JSValueRef arguments[],
829 JSValueRef* exception)
831 DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
833 AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS);
835 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
839 if (!privateObject) {
840 ThrowMsg(ConversionException, "Object is null.");
843 IDataSyncManagerPtr datasyncManager = privateObject->getObject();
845 DataSyncConverter converter(context);
847 std::string profileId;
848 if (argumentCount>=1) {
849 profileId = converter.toString(arguments[0]);
852 LoggerD("profileId: "<<profileId);
854 IEventGetLastSyncStatisticsPtr dplEvent(new IEventGetLastSyncStatistics());
856 dplEvent->setProfileId(profileId);
857 dplEvent->setForSynchronousCall();
858 datasyncManager->getLastSyncStatistics(dplEvent);
860 if (dplEvent->getResult()) {
861 LoggerD("Get sync statistics succeeded with length: "<<dplEvent->getSyncStatistics()->size());
862 return converter.toJSValueRefSyncStatisticsList(dplEvent->getSyncStatistics());
864 if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
865 ThrowMsg(NotFoundException, "Id not found.");
867 ThrowMsg(UnknownException, "Get statistics failed by unknown reason.");
871 Catch (NotFoundException)
873 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
874 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
876 Catch(UnsupportedException)
878 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
879 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
881 Catch(InvalidArgumentException)
883 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
884 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
886 Catch(ConversionException)
888 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
889 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
893 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
894 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());