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