0417b5781b2f0ac2c08883419d87d14d747e8e13
[framework/web/wrt-plugins-tizen.git] / src / DataSync / JSDataSyncManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19 #include <string>
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>
29
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"
40 #include <Logger.h>
41 #include <GlobalContextManager.h>
42
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45 using namespace DeviceAPI::Common;
46
47 namespace DeviceAPI {
48 namespace DataSync {
49
50 JSClassDefinition JSDataSyncManager::m_classInfo = {
51     0,
52     kJSClassAttributeNone,
53     TIZEN_DATA_SYNC_MANAGER_INTERFACE,
54     NULL, //ParentClass
55     NULL, //StaticValues
56     m_function,
57     initialize,
58     finalize,
59     NULL, //HasProperty,
60     NULL, //GetProperty,
61     NULL, //SetProperty,
62     NULL, //DeleteProperty,
63     NULL, //GetPropertyNames,
64     NULL, //CallAsFunction,
65     NULL, //CallAsConstructor,
66     NULL, //HasInstance,
67     NULL //ConvertToType
68 };
69
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 },
81
82     { 0, 0, 0 }
83 };
84
85 JSClassRef JSDataSyncManager::m_jsClassRef = JSClassCreate(JSDataSyncManager::getClassInfo());
86
87 void JSDataSyncManager::initialize(JSContextRef context, JSObjectRef object)
88 {
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))) {
94             delete priv;
95         }
96     } else {
97         LoggerI("Private object already set.");
98     }
99 }
100
101 void JSDataSyncManager::finalize(JSObjectRef object)
102 {
103     DataSyncManagerPrivObject *priv = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(object));
104     if (priv) {
105         LoggerI("Deleting datasync manager private object.");
106         delete priv;
107         JSObjectSetPrivate(object, NULL);
108     }
109 }
110
111 const JSClassRef JSDataSyncManager::getClassRef()
112 {
113     if (!m_jsClassRef) {
114         m_jsClassRef = JSClassCreate(&m_classInfo);
115     }
116     return m_jsClassRef;
117 }
118
119 const JSClassDefinition* JSDataSyncManager::getClassInfo()
120 {
121     return &m_classInfo;
122 }
123
124 JSValueRef JSDataSyncManager::add(JSContextRef context,
125         JSObjectRef object,
126         JSObjectRef thisObject,
127         size_t argumentCount,
128         const JSValueRef arguments[],
129         JSValueRef* exception)
130 {
131     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
132
133     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_ADD);
134
135     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
136
137     Try
138     {
139         if (!privateObject) {
140             ThrowMsg(ConversionException, "Object is null.");
141         }
142
143         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
144
145         DataSyncConverter converter(context);
146
147         SyncProfileInfoPtr profile;
148                 if (argumentCount>=1) {
149                 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
150                     ThrowMsg(ConversionException, "Wrong parameter type.");
151                 }
152         } else {
153                         ThrowMsg(ConversionException, "Wrong parameter type.");
154         }
155
156         profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
157         if (!profile) {
158             ThrowMsg(ConversionException, "Parameter conversion failed.");
159         }
160
161         LoggerD("profileName: "<<profile->getProfileName());
162
163         IEventAddProfilePtr dplEvent(new IEventAddProfile());
164
165         dplEvent->setProfile(profile);
166         dplEvent->setForSynchronousCall();
167         datasyncManager->addProfile(dplEvent);
168
169         if (dplEvent->getResult()) {
170             LoggerD("Add succeeded with id: "<<dplEvent->getProfile()->getProfileId());
171                         return JSValueMakeUndefined(context);
172         } else {
173             if (ExceptionCodes::OutOfRangeException==dplEvent->getExceptionCode()) {
174                 ThrowMsg(OutOfRangeException, "No more profiles allowed by platform.");
175             } else {
176                 ThrowMsg(UnknownException, "Add failed by unknown reason.");
177             }
178         }
179     }
180     Catch(OutOfRangeException)
181     {
182                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
183         return JSTizenExceptionFactory::postException(context, exception, "QuotaExceededError", _rethrown_exception.GetMessage());
184     }
185     Catch(UnsupportedException)
186     {
187                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
188         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
189     }
190     Catch(InvalidArgumentException)
191     {
192                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
193         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
194     }
195     Catch(ConversionException)
196     {
197                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
198         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
199     }
200     Catch(Exception)
201     {
202                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
203         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
204     }
205 }
206
207 JSValueRef JSDataSyncManager::update(JSContextRef context,
208         JSObjectRef object,
209         JSObjectRef thisObject,
210         size_t argumentCount,
211         const JSValueRef arguments[],
212         JSValueRef* exception)
213 {
214     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
215
216     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_UPDATE);
217
218     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
219
220     Try
221     {
222         if (!privateObject) {
223             ThrowMsg(ConversionException, "Object is null.");
224         }
225
226         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
227
228         DataSyncConverter converter(context);
229
230         SyncProfileInfoPtr profile;
231                 if (argumentCount>=1) {
232                 if (!JSValueIsObjectOfClass(context, arguments[0], JSSyncProfileInfo::getClassRef())) {
233                     ThrowMsg(ConversionException, "Wrong parameter type.");
234                 }
235                 } else {
236             ThrowMsg(ConversionException, "Wrong parameter type.");
237                 }
238
239         profile = JSSyncProfileInfo::getPrivateObject(JSValueToObject(context, arguments[0], NULL));
240         if (!profile) {
241             ThrowMsg(ConversionException, "Parameter conversion failed.");
242         }
243
244         LoggerD("profileName: "<<profile->getProfileName());
245
246         IEventUpdateProfilePtr dplEvent(new IEventUpdateProfile());
247
248         dplEvent->setProfile(profile);
249         dplEvent->setForSynchronousCall();
250         datasyncManager->updateProfile(dplEvent);
251
252         if (dplEvent->getResult()) {
253             LoggerD("Update succeeded with id: "<<dplEvent->getProfile()->getProfileId());
254                         return JSValueMakeUndefined(context);
255         } else {
256             ThrowMsg(UnknownException, "Update failed by unknown reason.");
257         }
258     }
259     Catch(UnsupportedException)
260     {
261                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
262         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
263     }
264     Catch(InvalidArgumentException)
265     {
266                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
267         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
268     }
269     Catch(ConversionException)
270     {
271                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
272         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
273     }
274     Catch(Exception)
275     {
276                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
277         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
278     }
279 }
280
281 JSValueRef JSDataSyncManager::remove(JSContextRef context,
282         JSObjectRef object,
283         JSObjectRef thisObject,
284         size_t argumentCount,
285         const JSValueRef arguments[],
286         JSValueRef* exception)
287 {
288     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
289
290     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_REMOVE);
291
292     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
293
294     Try
295     {
296         if (!privateObject) {
297             ThrowMsg(ConversionException, "Object is null.");
298         }
299
300         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
301
302         DataSyncConverter converter(context);
303
304         std::string profileId;
305                 if (argumentCount>=1) {
306                         profileId = converter.toString(arguments[0]);
307                 }
308
309         LoggerD("profileId: "<<profileId);
310
311         IEventRemoveProfilePtr dplEvent(new IEventRemoveProfile());
312
313         dplEvent->setProfileId(profileId);
314         dplEvent->setForSynchronousCall();
315         datasyncManager->removeProfile(dplEvent);
316
317         if (dplEvent->getResult()) {
318             LoggerD("Remove succeeded with id: "<<dplEvent->getProfileId());
319                         return JSValueMakeUndefined(context);
320         } else {
321                         if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
322                                 ThrowMsg(NotFoundException, "Id not found.");
323                         } else {
324                                 ThrowMsg(UnknownException, "Remove failed by unknown reason.");
325                         }
326         }
327     }
328     Catch (NotFoundException)
329     {
330                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
331         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
332     }
333     Catch(UnsupportedException)
334     {
335                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
336         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
337     }
338     Catch(InvalidArgumentException)
339     {
340                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
341         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
342     }
343     Catch(ConversionException)
344     {
345                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
346         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
347     }
348     Catch(Exception)
349     {
350                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
351         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
352     }
353 }
354
355 JSValueRef JSDataSyncManager::getMaxProfilesNum(JSContextRef context,
356         JSObjectRef object,
357         JSObjectRef thisObject,
358         size_t argumentCount,
359         const JSValueRef arguments[],
360         JSValueRef* exception)
361 {
362     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
363
364     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_MAX_PROFILES_NUM);
365
366     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
367
368     Try
369     {
370         if (!privateObject) {
371             ThrowMsg(ConversionException, "Object is null.");
372         }
373
374         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
375
376         DataSyncConverter converter(context);
377
378         IEventGetMaxProfilesNumPtr dplEvent(new IEventGetMaxProfilesNum());
379
380         dplEvent->setForSynchronousCall();
381         datasyncManager->getMaxProfilesNum(dplEvent);
382
383         if (dplEvent->getResult()) {
384             LoggerD("Max number of profiles: "<<dplEvent->getNumMaxProfiles());
385             return converter.toJSValueRef(dplEvent->getNumMaxProfiles());
386         } else {
387                         ThrowMsg(UnknownException, "Get the max number of profiles failed by unknown reason.");
388         }
389     }
390     Catch(UnsupportedException)
391     {
392                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
393         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
394     }
395     Catch(InvalidArgumentException)
396     {
397                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
398         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
399     }
400     Catch(ConversionException)
401     {
402                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
403         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
404     }
405     Catch(Exception)
406     {
407                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
408         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
409     }
410 }
411
412 JSValueRef JSDataSyncManager::getProfilesNum(JSContextRef context,
413         JSObjectRef object,
414         JSObjectRef thisObject,
415         size_t argumentCount,
416         const JSValueRef arguments[],
417         JSValueRef* exception)
418 {
419     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
420
421     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_PROFILES_NUM);
422
423     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
424
425     Try
426     {
427         if (!privateObject) {
428             ThrowMsg(ConversionException, "Object is null.");
429         }
430
431         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
432
433         DataSyncConverter converter(context);
434
435         IEventGetProfilesNumPtr dplEvent(new IEventGetProfilesNum());
436
437         dplEvent->setForSynchronousCall();
438         datasyncManager->getProfilesNum(dplEvent);
439
440         if (dplEvent->getResult()) {
441             LoggerD("Number of profiles: "<<dplEvent->getNumProfiles());
442             return converter.toJSValueRef(dplEvent->getNumProfiles());
443         } else {
444                         ThrowMsg(UnknownException, "Get the number of profiles failed by unknown reason.");
445         }
446     }
447     Catch(UnsupportedException)
448     {
449                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
450         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
451     }
452     Catch(InvalidArgumentException)
453     {
454                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
455         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
456     }
457     Catch(ConversionException)
458     {
459                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
460         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
461     }
462     Catch(Exception)
463     {
464                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
465         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
466     }
467 }
468
469 JSValueRef JSDataSyncManager::get(JSContextRef context,
470         JSObjectRef object,
471         JSObjectRef thisObject,
472         size_t argumentCount,
473         const JSValueRef arguments[],
474         JSValueRef* exception)
475 {
476     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
477
478     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET);
479
480     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
481
482     Try
483     {
484         if (!privateObject) {
485             ThrowMsg(ConversionException, "Object is null.");
486         }
487
488         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
489
490         DataSyncConverter converter(context);
491
492         std::string profileId;
493                 if (argumentCount>=1) {
494                         profileId = converter.toString(arguments[0]);
495                 }
496
497         LoggerD("profileId: "<<profileId);
498
499         IEventGetProfilePtr dplEvent(new IEventGetProfile());
500
501         dplEvent->setProfileId(profileId);
502         dplEvent->setForSynchronousCall();
503         datasyncManager->getProfile(dplEvent);
504
505         if (dplEvent->getResult()) {
506             LoggerD("Get succeeded with id: "<<dplEvent->getProfileId());
507                         return JSSyncProfileInfo::createJSSyncProfileInfo(context, dplEvent->getProfile());
508         } else {
509                         if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
510                                 ThrowMsg(NotFoundException, "Id not found.");
511                         } else {
512                                 ThrowMsg(UnknownException, "Get failed by unknown reason.");
513                         }
514         }
515     }
516     Catch (NotFoundException)
517     {
518                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
519         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
520     }
521     Catch(UnsupportedException)
522     {
523                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
524         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
525     }
526     Catch(InvalidArgumentException)
527     {
528                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
529         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
530     }
531     Catch(ConversionException)
532     {
533                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
534         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
535     }
536     Catch(Exception)
537     {
538                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
539         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
540     }
541 }
542
543 JSValueRef JSDataSyncManager::getAll(JSContextRef context,
544         JSObjectRef object,
545         JSObjectRef thisObject,
546         size_t argumentCount,
547         const JSValueRef arguments[],
548         JSValueRef* exception)
549 {
550     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
551
552     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_ALL);
553
554     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
555
556     Try
557     {
558         if (!privateObject) {
559             ThrowMsg(ConversionException, "Object is null.");
560         }
561
562         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
563
564         DataSyncConverter converter(context);
565
566         IEventGetAllProfilesPtr dplEvent(new IEventGetAllProfiles());
567
568         dplEvent->setForSynchronousCall();
569         datasyncManager->getAllProfiles(dplEvent);
570
571         if (dplEvent->getResult()) {
572             LoggerD("Getting all profiles succeeded with length: "<<dplEvent->getProfiles()->size());
573                         return converter.toJSValueRefProfileInfoList(dplEvent->getProfiles());
574         } else {
575                         ThrowMsg(UnknownException, "Getting all profiles failed by unknown reason.");
576         }
577     }
578     Catch(UnsupportedException)
579     {
580                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
581         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
582     }
583     Catch(InvalidArgumentException)
584     {
585                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
586         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
587     }
588     Catch(ConversionException)
589     {
590                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
591         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
592     }
593     Catch(Exception)
594     {
595                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
596         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
597     }
598 }
599
600 JSValueRef JSDataSyncManager::startSync(JSContextRef context,
601         JSObjectRef object,
602         JSObjectRef thisObject,
603         size_t argumentCount,
604         const JSValueRef arguments[],
605         JSValueRef* exception)
606 {
607     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
608
609     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_START_SYNC);
610
611     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
612
613     Try
614     {
615         if (!privateObject) {
616             ThrowMsg(ConversionException, "Object is null.");
617         }
618
619         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
620
621         JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
622
623         DataSyncConverter converter(context);
624
625         std::string profileId;
626                 if (argumentCount>=1) {
627                         profileId = converter.toString(arguments[0]);
628                 }
629         LoggerD("profileId: "<<profileId);
630
631         IEventStartSyncPtr dplEvent(new IEventStartSync());
632
633                 if (argumentCount>=2) {
634                 LoggerD("Process the listener callback.");
635
636             if (JSValueIsObject(context, arguments[1])) {
637                                 LoggerD("Non-null callbacks.");
638
639                                 JSObjectRef objectCallbacks = converter.toJSObjectRef(arguments[1]);
640                                 JSCallbackManagerPtr onProgressCbm(NULL), onCompletedCbm(NULL), onStoppedCbm(NULL), onFailedCbm(NULL);
641                         Validator validator(context);
642
643                         JSValueRef onProgress = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onprogress");
644                         if (validator.isNullOrUndefined(onProgress)) {
645                             onProgress = NULL;
646                         } else if(!validator.isCallback(onProgress)) {
647                             ThrowMsg(ConversionException, "Wrong second parameter type.");
648                         }
649
650                         JSValueRef onCompleted = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "oncompleted");
651                         if (validator.isNullOrUndefined(onCompleted)) {
652                             onCompleted = NULL;
653                         } else if(!validator.isCallback(onCompleted)) {
654                             ThrowMsg(ConversionException, "Wrong second parameter type.");
655                         }
656
657                         JSValueRef onStopped = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onstopped");
658                         if (validator.isNullOrUndefined(onStopped)) {
659                             onStopped = NULL;
660                         } else if(!validator.isCallback(onStopped)) {
661                             ThrowMsg(ConversionException, "Wrong second parameter type.");
662                         }
663
664                         JSValueRef onFailed = JSUtils::getJSPropertyOrUndefined(context, objectCallbacks, "onfailed");
665                         if (validator.isNullOrUndefined(onFailed)) {
666                             onFailed = NULL;
667                         } else if(!validator.isCallback(onFailed)) {
668                             ThrowMsg(ConversionException, "Wrong second parameter type.");
669                         }
670
671                         if (!onProgress && !onCompleted && !onStopped && !onFailed) {
672                             ThrowMsg(ConversionException, "Wrong second parameter type.");
673                         }
674
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);
679
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));
684
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.");
689                         } else {
690                                 ThrowMsg(ConversionException, "Wrong second parameter type.");
691                         }
692                 }
693
694         LoggerD("Proceed the start sync event to the platform.");
695
696         dplEvent->setProfileId(profileId);
697         dplEvent->setForSynchronousCall();
698         datasyncManager->startSync(dplEvent);
699
700         if (dplEvent->getResult()) {
701                         if(dplEvent->getEmitter()) {
702                                 LoggerD("watcherId: "<<profileId.at(0));
703                         }
704
705             LoggerD("Starting the sync succeeded.");
706         } else {
707             switch (dplEvent->getExceptionCode()) {
708                 case ExceptionCodes::NotFoundException:
709                     ThrowMsg(NotFoundException, "Id not found.");
710                     break;
711                 case ExceptionCodes::InvalidArgumentException:
712                     ThrowMsg(InvalidArgumentException, "Invalid argument");
713                     break;
714                 default:
715                     ThrowMsg(UnknownException, "Starting the sync failed by unknown reason.");
716                     break;
717             }
718         }
719     }
720     Catch(UnsupportedException)
721     {
722                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
723         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
724     }
725     Catch(InvalidArgumentException)
726     {
727                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
728         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
729     }
730     Catch(ConversionException)
731     {
732                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
733         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
734     }
735     Catch (NotFoundException)
736     {
737                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
738         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
739     }
740     Catch(Exception)
741     {
742                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
743         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
744     }
745
746     return JSValueMakeUndefined(context);
747 }
748
749 JSValueRef JSDataSyncManager::stopSync(JSContextRef context,
750         JSObjectRef object,
751         JSObjectRef thisObject,
752         size_t argumentCount,
753         const JSValueRef arguments[],
754         JSValueRef* exception)
755 {
756     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
757
758     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_STOP_SYNC);
759
760     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
761
762     Try
763     {
764         if (!privateObject) {
765             ThrowMsg(ConversionException, "Object is null.");
766         }
767
768         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
769
770         DataSyncConverter converter(context);
771
772         std::string profileId;
773                 if (argumentCount>=1) {
774                         profileId = converter.toString(arguments[0]);
775                 }
776         LoggerD("profileId: "<<profileId);
777
778         IEventStopSyncPtr dplEvent(new IEventStopSync());
779
780         dplEvent->setProfileId(profileId);
781         dplEvent->setForSynchronousCall();
782         datasyncManager->stopSync(dplEvent);
783
784         if (dplEvent->getResult()) {
785             LoggerD("Stop sync succeeded.");
786         } else {
787             if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
788                 ThrowMsg(NotFoundException, "Id not found.");
789                 } else {
790                 ThrowMsg(UnknownException, "Stopping the sync failed by unknown reason.");
791             }
792         }
793     }
794     Catch(UnsupportedException)
795     {
796                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
797         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
798     }
799     Catch(InvalidArgumentException)
800     {
801                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
802         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
803     }
804     Catch(ConversionException)
805     {
806                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
807         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
808     }
809     Catch (NotFoundException)
810     {
811                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
812         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
813     }
814     Catch(Exception)
815     {
816                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
817         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
818     }
819
820     return JSValueMakeUndefined(context);
821 }
822
823
824 JSValueRef JSDataSyncManager::getLastSyncStatistics(JSContextRef context,
825         JSObjectRef object,
826         JSObjectRef thisObject,
827         size_t argumentCount,
828         const JSValueRef arguments[],
829         JSValueRef* exception)
830 {
831     DataSyncManagerPrivObject *privateObject = static_cast<DataSyncManagerPrivObject*>(JSObjectGetPrivate(thisObject));
832
833     AceSecurityStatus status = DATASYNC_CHECK_ACCESS(DATASYNC_FUNCTION_API_GET_LAST_SYNC_STATISTICS);
834
835     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
836
837     Try
838     {
839         if (!privateObject) {
840             ThrowMsg(ConversionException, "Object is null.");
841         }
842
843         IDataSyncManagerPtr datasyncManager = privateObject->getObject();
844
845         DataSyncConverter converter(context);
846
847         std::string profileId;
848                 if (argumentCount>=1) {
849                         profileId = converter.toString(arguments[0]);
850                 }
851
852         LoggerD("profileId: "<<profileId);
853
854         IEventGetLastSyncStatisticsPtr dplEvent(new IEventGetLastSyncStatistics());
855
856         dplEvent->setProfileId(profileId);
857         dplEvent->setForSynchronousCall();
858         datasyncManager->getLastSyncStatistics(dplEvent);
859
860         if (dplEvent->getResult()) {
861             LoggerD("Get sync statistics succeeded with length: "<<dplEvent->getSyncStatistics()->size());
862                         return converter.toJSValueRefSyncStatisticsList(dplEvent->getSyncStatistics());
863         } else {
864                         if (ExceptionCodes::NotFoundException==dplEvent->getExceptionCode()) {
865                                 ThrowMsg(NotFoundException, "Id not found.");
866                         } else {
867                                 ThrowMsg(UnknownException, "Get statistics failed by unknown reason.");
868                         }
869         }
870     }
871     Catch (NotFoundException)
872     {
873                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
874         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
875     }
876     Catch(UnsupportedException)
877     {
878                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
879         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
880     }
881     Catch(InvalidArgumentException)
882     {
883                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
884         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
885     }
886     Catch(ConversionException)
887     {
888                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
889         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
890     }
891     Catch(Exception)
892     {
893                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
894         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
895     }
896 }
897
898 }
899 }