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