upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessagingStorage.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 /*
19  * @file        JSPluginTemplete.cpp
20  * @author      Jaehyun Park (jaehyun77.park@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSPluginTemplete class
23  */
24
25 #include "JSMessagingStorage.h"
26 #include "JSMessagingService.h"
27 #include "MessagingListener.h"
28 #include "ConverterMessage.h"
29 #include "MessagingStorageMultiCallback.h"
30 #include <API/Messaging/ReqReceiverMessage.h>
31 #include <API/Filter/SortMode.h>
32
33 #include <Tizen/Tizen/FilterConverter.h>
34
35 #include <CommonsJavaScript/Converter.h>
36 #include <CommonsJavaScript/Validator.h>
37 #include <CommonsJavaScript/JSUtils.h>
38 #include <CommonsJavaScript/JSCallbackManager.h>
39 #include <CommonsJavaScript/Utils.h>                                    
40 #include <CommonsJavaScript/JSDOMExceptionFactory.h>            
41 #include <CommonsJavaScript/SecurityExceptions.h>
42
43 #include <Tizen/Common/JSTizenExceptionFactory.h>
44 #include <Tizen/Common/JSTizenException.h>
45 #include <Tizen/Common/SecurityExceptions.h>
46
47 #include "MessagingErrorMsg.h"
48 #include "plugin_config.h"
49
50
51 using namespace std;
52 using namespace DPL;
53 using namespace WrtDeviceApis::Commons;
54 using namespace WrtDeviceApis::CommonsJavaScript;
55 using namespace TizenApis::Tizen1_0::Tizen;
56 using namespace TizenApis::Commons;
57
58
59 namespace TizenApis {
60 namespace Tizen1_0 {
61
62         JSClassRef JSMessagingStorage::m_jsClassRef = NULL;
63
64         JSClassDefinition JSMessagingStorage::m_classInfo = 
65         {
66                 0,
67                 kJSClassAttributeNone,
68                 "messageStorage",
69                 NULL,
70                 NULL,
71                 m_function,
72                 initialize,
73                 finalize,
74                 NULL, //hasProperty,
75                 NULL, //getProperty,
76                 NULL, //setProperty,
77                 NULL, //deleteProperty,Geolocation
78                 NULL, //getPropertyNames,
79                 NULL,
80                 NULL,
81                 hasInstance,
82                 NULL
83         };
84
85         JSStaticFunction JSMessagingStorage::m_function[] = 
86         {
87                         { "addDraftMessage",                    JSMessagingStorage::addDraftMessage,                                     kJSPropertyAttributeNone },            
88                         { "findMessages",                       JSMessagingStorage::findMessages,                                        kJSPropertyAttributeNone },
89                         { "removeMessages",                     JSMessagingStorage::removeMessages,                      kJSPropertyAttributeNone },
90                         { "updateMessages",                     JSMessagingStorage::updateMessages,                      kJSPropertyAttributeNone },
91                         { "findConversations",                  JSMessagingStorage::findConversations,                   kJSPropertyAttributeNone },
92                         { "removeConversations",                JSMessagingStorage::removeConversations,                         kJSPropertyAttributeNone },
93                         { "findFolders",                                JSMessagingStorage::findFolders,                                 kJSPropertyAttributeNone },
94                         { "addMessagesChangeListener",  JSMessagingStorage::addMessagesChangeListener,   kJSPropertyAttributeNone },
95                         { "addConversationsChangeListener",     JSMessagingStorage::addConversationsChangeListener,      kJSPropertyAttributeNone },
96                         { "addFoldersChangeListener",   JSMessagingStorage::addFoldersChangeListener,    kJSPropertyAttributeNone },                    
97                         { "removeChangeListener",                               JSMessagingStorage::removeChangeListener,                                kJSPropertyAttributeNone },
98                         { 0, 0, 0 }
99         };
100
101         const JSClassRef JSMessagingStorage::getClassRef() 
102         {
103                 if (!m_jsClassRef) 
104                 {
105                         m_jsClassRef = JSClassCreate(&m_classInfo);
106                 }
107                 return m_jsClassRef;
108         }
109
110         void JSMessagingStorage::initialize(JSContextRef context, JSObjectRef object) 
111         {
112                 LogDebug("creation messaging instance");
113         }
114
115         void JSMessagingStorage::finalize(JSObjectRef object) 
116         {
117                 LogDebug("enter");
118
119                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(object));
120                 if (priv)
121                 {
122                         LogDebug("deleting private object (messaging)");
123                         // for messaging object, private object is shared pointer, so will be deleted automatically
124                         delete priv;
125                         // object will be deleted, but used to show that private object is deleted
126                         JSObjectSetPrivate(object, NULL);
127                 }
128         }
129
130         bool JSMessagingStorage::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) 
131         {
132                 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
133         }
134
135         JSValueRef JSMessagingStorage::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
136         {
137                 LogDebug("<<< ");
138                 JSValueRef retVal = NULL;
139
140                 Try
141                 {
142                         Converter convert(context);
143                         if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_INBOX")) 
144                         {
145                                 LogDebug("FOLDER_INBOX" << ":" << "1");
146                                 retVal = convert.toJSValueRef(Api::Messaging::INBOX);
147                         }
148                         else if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_OUTBOX")) 
149                         {
150                                 LogDebug("FOLDER_OUTBOX" << ":" << "2");
151                                 retVal = convert.toJSValueRef(Api::Messaging::OUTBOX);
152                         }
153                         else if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_DRAFTS")) 
154                         {
155                                 LogDebug("FOLDER_DRAFTS" << ":" << "3");
156                                 retVal = convert.toJSValueRef(Api::Messaging::DRAFTBOX);
157                         }
158                         else if (JSStringIsEqualToUTF8CString(propertyName, "FOLDER_SENTBOX")) 
159                         {
160                                 LogDebug("FOLDER_SENTBOX" << ":" << "4");
161                                 retVal = convert.toJSValueRef(Api::Messaging::SENTBOX);
162                         }
163                         else
164                         {
165                                 retVal = JSValueMakeUndefined(context);
166                         }
167                 }
168                 Catch (WrtDeviceApis::Commons::Exception)
169                 {
170                         LogError("Exception: " << _rethrown_exception.GetMessage());
171                         return JSTizenExceptionFactory::postException(context, exception, 
172                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
173                 }
174
175                 LogDebug(">>>");
176                 return retVal;
177         }
178
179         JSValueRef JSMessagingStorage::addDraftMessage(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
180                         const JSValueRef arguments[], JSValueRef* exception) 
181         {
182
183                 LogDebug("<<<");
184
185                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
186
187                 if (!priv) 
188                 {
189                         LogDebug("priv is NULL.");
190                         return JSTizenExceptionFactory::postException(
191                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
192                 }
193
194                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
195
196                 if (!objectPriv)
197                 {
198                         LogDebug("objectPriv is NULL.");                
199                         return JSTizenExceptionFactory::postException(
200                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
201                 }
202
203                 //check permission.
204                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
205                         MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_DRAFT_MESSAGE]);
206                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
207                 
208                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
209                 callbackManager = JSCallbackManager::createObject(priv->getContext());
210
211                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
212                 
213                 Try
214                 {
215                         Api::Messaging::EventAddDraftMessagePtr event(new Api::Messaging::EventAddDraftMessage());
216
217                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
218
219                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
220
221                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
222                         {
223                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
224                         }
225                         else
226                         {
227                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
228                                 {
229                                         if(objectPriv->getIndex()  < 1)
230                                         {
231                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
232                                         }
233                                 }
234                         }
235
236                         if(argumentCount >= 1)  // Message object
237                         {
238                                 if(JSValueIsObject(context, arguments[0]))
239                                 {
240                                         event->msg = converter->toIMessage(arguments[0]);
241                                 }
242                                 else    // NOT nullable 
243                                 {
244                                         LogDebug("Invalid argument exception");
245                                         Throw(ConversionException);                     
246                                 }
247                         }               
248                         if(argumentCount >= 2)  // successCallback
249                         {
250                                 if(Validator(context).isCallback(arguments[1]))
251                                 {
252                                         onSuccessForCbm = arguments[1];
253                                 }
254                                 else if(!Validator(context).isNullOrUndefined(arguments[1]))    //nullable
255                                 {
256                                         LogDebug("SuccessCallback type is mismatched.");
257                                         Throw(ConversionException);
258                                 }
259                         }
260                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
261                         {       
262                                 if(Validator(context).isCallback(arguments[2]))
263                                 {
264                                         onErrorForCbm = arguments[2];
265                                 }
266                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
267                                 {
268                                         LogDebug("ErrorCallback type is mismatched.");
269                                         Throw(ConversionException);                     
270                                 }
271                         }
272                         if(callbackManager)
273                         {
274                                 callbackManager->setOnSuccess(onSuccessForCbm);
275                                 callbackManager->setOnError(onErrorForCbm);
276                         }
277                 
278                         event->setIndex(objectPriv->getIndex());
279                         event->setType(objectPriv->getType());
280                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
281                         event->setForAsynchronousCall(&MessagingListener::getInstance());
282
283                         Api::Messaging::ReqReceiverMessageSingleton::Instance().addDraftMessage(event);
284
285                         return makePendingOperation(context, event);
286                 }
287                 Catch(WrtDeviceApis::Commons::ConversionException) 
288                 {
289                         LogError(">>> TypeMismatchException");
290                         return JSTizenExceptionFactory::postException(context, exception, 
291                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
292                 }
293                 Catch(WrtDeviceApis::Commons::NullPointerException) 
294                 {
295                         LogError(">>> TypeMismatchException");
296                         return JSTizenExceptionFactory::postException(context, exception, 
297                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
298                 }
299                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
300                 {
301                         LogError(">>> InvalidValuesException");
302                         return JSTizenExceptionFactory::postException(context, exception, 
303                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
304                 }
305                 Catch(WrtDeviceApis::Commons::PlatformException) 
306                 {
307                         LogError(">>> UnknownException");
308                         return JSTizenExceptionFactory::postException(context, exception, 
309                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
310                 }
311
312                 LogError(">>> JSValueMakeNull");
313                 return JSValueMakeNull(context);
314                                 
315         
316         }
317
318
319         JSValueRef JSMessagingStorage::findMessages(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
320                         const JSValueRef arguments[], JSValueRef* exception)
321         {
322
323                 LogDebug("<<<");
324
325                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
326
327                 if (!priv) 
328                 {
329                         LogDebug("priv is NULL.");
330                         return JSTizenExceptionFactory::postException(
331                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
332                 }
333
334                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
335
336                 if (!objectPriv)
337                 {
338                         LogDebug("objectPriv is NULL.");                
339                         return JSTizenExceptionFactory::postException(
340                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
341                 }
342
343                 //check permission.
344                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
345                         MessagingExportedNames[MESSAGING_FUNCTION_API_FIND_MESSAGES]);
346                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);  
347
348                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
349                 callbackManager = JSCallbackManager::createObject(priv->getContext());
350
351                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
352                 
353                 Try
354                 {
355                         Api::Messaging::EventQueryMessagesPtr event(new Api::Messaging::EventQueryMessages());
356                         
357                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
358
359                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
360
361                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
362                         {
363                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
364                         }
365                         else
366                         {
367                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
368                                 {
369                                         if(objectPriv->getIndex()  < 1)
370                                         {
371                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
372                                         }
373                                 }
374                         }
375
376                         if(argumentCount >= 1)  //optional AbstractFilter? filter
377                         {
378                                 if(JSValueIsObject(context, arguments[0]))
379                                 {
380                                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[0]);
381                                         event->setFilter(filter);
382                                 }
383                                 else    // NOT nullable 
384                                 {
385                                         LogDebug("Filter type is mismatched.");
386                                         Throw(ConversionException);                     
387                                 }
388                         }
389                         if(argumentCount >= 2)  // successCallback
390                         {
391                                 if(Validator(context).isCallback(arguments[1]))
392                                 {
393                                         onSuccessForCbm = arguments[1];
394                                 }
395                                 else // NOT nullable
396                                 {
397                                         LogDebug("SuccessCallback type is mismatched.");
398                                         Throw(ConversionException);
399                                 }
400                         }
401                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
402                         {       
403                                 if(Validator(context).isCallback(arguments[2]))
404                                 {
405                                         onErrorForCbm = arguments[2];
406                                 }
407                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
408                                 {
409                                         LogDebug("ErrorCallback type is mismatched.");
410                                         Throw(ConversionException);                     
411                                 }
412                         }
413                         if(argumentCount >= 4)  //optional SortMode? sortMode
414                         {
415                                 if(JSValueIsObject(context, arguments[3]))
416                                 {
417                                         Api::Tizen::SortModePtr sortMode = filterConverter->toSortMode(arguments[3]);
418                                         event->setSortMode(sortMode);
419                                 }
420                                 else if(!Validator(context).isNullOrUndefined(arguments[3]))    //nullable
421                                 {
422                                         LogDebug("SortMode type is mismatched.");
423                                         Throw(ConversionException);
424                                 }
425                         }               
426                         if(argumentCount >= 5)  //optional unsigned long? limit JSValueIsUndefined(context, arguments[0]), JSValueIsNull(context, arguments[0])
427                         {
428                                 if (!Validator(context).isNullOrUndefined(arguments[4]))
429                                 {
430                                         if (!JSValueIsNumber(context, arguments[4]))
431                                         {
432                                                 Throw(ConversionException);                                     
433                                         }
434                                         long limit = filterConverter->toULong(arguments[4]);
435                                         event->setLimit(limit);
436                                 }                       
437                         }               
438                         if(argumentCount >= 6)  //optional unsigned long? offset JSValueIsUndefined(context, arguments[0]), JSValueIsNull(context, arguments[0])
439                         {
440                                 if (!Validator(context).isNullOrUndefined(arguments[5]))
441                                 {
442                                         if (!JSValueIsNumber(context, arguments[5]))
443                                         {
444                                                 Throw(ConversionException);                                     
445                                         }
446                                         long offset = filterConverter->toULong(arguments[5]);
447                                         event->setOffset(offset);                                       
448                                 }
449                         }               
450                         
451                         if(callbackManager)
452                         {
453                                 callbackManager->setOnSuccess(onSuccessForCbm);
454                                 callbackManager->setOnError(onErrorForCbm);
455                         }
456
457                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
458                         event->setForAsynchronousCall(&MessagingListener::getInstance());
459                         event->setIndex(objectPriv->getIndex());
460                         event->setType(objectPriv->getType());
461
462                         Api::Messaging::ReqReceiverMessageSingleton::Instance().queryMessages(event);
463
464                         LogError(">>>");
465                         return makePendingOperation(context, event);
466                 }
467                 Catch(WrtDeviceApis::Commons::ConversionException) 
468                 {
469                         LogError(">>> TypeMismatchException");
470                         return JSTizenExceptionFactory::postException(context, exception, 
471                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
472                 }
473                 Catch(WrtDeviceApis::Commons::NullPointerException) 
474                 {
475                         LogError(">>> TypeMismatchException");
476                         return JSTizenExceptionFactory::postException(context, exception, 
477                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
478                 }
479                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
480                 {
481                         LogError(">>> InvalidValuesException");
482                         return JSTizenExceptionFactory::postException(context, exception, 
483                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
484                 }
485                 Catch(WrtDeviceApis::Commons::PlatformException) 
486                 {
487                         LogError(">>> UnknownException");
488                         return JSTizenExceptionFactory::postException(context, exception, 
489                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
490                 }
491
492                 LogError(">>> JSValueMakeNull");
493                 return JSValueMakeNull(context);
494         }
495
496         JSValueRef JSMessagingStorage::removeMessages(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
497                         const JSValueRef arguments[], JSValueRef* exception) 
498         {
499
500                 LogDebug("<<<");
501
502                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
503
504                 if (!priv) 
505                 {
506                         LogDebug("priv is NULL.");
507                         return JSTizenExceptionFactory::postException(
508                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
509                 }
510
511                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
512
513                 if (!objectPriv)
514                 {
515                         LogDebug("objectPriv is NULL.");                
516                         return JSTizenExceptionFactory::postException(
517                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
518                 }
519
520                 //check permission.
521                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
522                         MessagingExportedNames[MESSAGING_FUNCTION_API_REMOVE_MESSAGES]);
523                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
524
525                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
526                 callbackManager = JSCallbackManager::createObject(priv->getContext());
527
528                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
529                         
530                 Try 
531                 {
532
533                         Api::Messaging::EventDeleteMessagesPtr event(new Api::Messaging::EventDeleteMessages());
534                         
535                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
536
537                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
538
539                         JSObjectRef objArg = NULL;
540
541                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
542                         {
543                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
544                         }
545                         else
546                         {
547                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
548                                 {
549                                         if(objectPriv->getIndex()  < 1)
550                                         {
551                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
552                                         }
553                                 }
554                         }
555
556                         if(argumentCount >= 1)  // Message object
557                         {
558                                 if(JSValueIsObject(context, arguments[0]))
559                                 {
560                                         objArg = converter->toJSObjectRef(arguments[0]);                                
561                                 }
562                                 else
563                                 {
564                                         LogDebug("Invalid argument exception");
565                                         Throw(ConversionException);
566                                 }
567                         }
568                         if(argumentCount >= 2)  // successCallback
569                         {
570                                 if(Validator(context).isCallback(arguments[1]))
571                                 {
572                                         onSuccessForCbm = arguments[1];
573                                 }
574                                 else if(!Validator(context).isNullOrUndefined(arguments[1]))    //nullable
575                                 {
576                                         LogDebug("SuccessCallback type is mismatched.");
577                                         Throw(ConversionException);
578                                 }
579                         }
580                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
581                         {       
582                                 if(Validator(context).isCallback(arguments[2]))
583                                 {
584                                         onErrorForCbm = arguments[2];
585                                 }
586                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
587                                 {
588                                         LogDebug("ErrorCallback type is mismatched.");
589                                         Throw(ConversionException);                     
590                                 }
591                         }
592                         if(callbackManager)
593                         {
594                                 callbackManager->setOnSuccess(onSuccessForCbm);
595                                 callbackManager->setOnError(onErrorForCbm);
596                         }
597
598                         std::vector<Api::Messaging::IMessagePtr> msgs;
599                                                 
600                         LogDebug("array length "<<JSGetArrayLength(context, objArg));
601                         for (std::size_t i = 0; i < JSGetArrayLength(context, objArg); i++)
602                         {
603                                 JSValueRef element = JSGetArrayElement(context, objArg, i);
604                                 JSObjectRef arg = JSValueToObject(context, element, NULL);
605                                 Api::Messaging::IMessagePtr msg = converter->toIMessage(arg);
606                                 msgs.push_back(msg);
607                         }
608
609                         event->msgArray = msgs;
610
611                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
612                         event->setForAsynchronousCall(&MessagingListener::getInstance());
613                         event->setIndex(objectPriv->getIndex());
614                         event->setType(objectPriv->getType());
615
616                         Api::Messaging::ReqReceiverMessageSingleton::Instance().deleteMessages(event);
617
618                         return makePendingOperation(context, event);
619                 }
620                 Catch(WrtDeviceApis::Commons::ConversionException)
621                 {
622                         LogError(">>> TypeMismatchException");
623                         return JSTizenExceptionFactory::postException(context, exception, 
624                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
625                 }
626                 Catch(WrtDeviceApis::Commons::NullPointerException) 
627                 {
628                         LogError(">>> TypeMismatchException");
629                         return JSTizenExceptionFactory::postException(context, exception, 
630                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
631                 }
632                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
633                 {
634                         LogError(">>> InvalidValuesException");
635                         return JSTizenExceptionFactory::postException(context, exception, 
636                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                    
637                 }
638                 Catch(WrtDeviceApis::Commons::PlatformException) 
639                 {
640                         LogError(">>> UnknownException");
641                         return JSTizenExceptionFactory::postException(context, exception, 
642                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
643                 }
644                 
645                 return JSValueMakeNull(context);
646         }
647
648         JSValueRef JSMessagingStorage::updateMessages(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
649                         const JSValueRef arguments[], JSValueRef* exception) 
650         {
651
652                 LogDebug("<<<");
653
654                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
655
656                 if (!priv) 
657                 {
658                         LogDebug("priv is NULL.");
659                         return JSTizenExceptionFactory::postException(
660                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
661                 }
662
663                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
664
665                 if (!objectPriv)
666                 {
667                         LogDebug("objectPriv is NULL.");                
668                         return JSTizenExceptionFactory::postException(
669                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
670                 }
671
672                 //check permission.
673                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
674                         MessagingExportedNames[MESSAGING_FUNCTION_API_UPDATE_MESSAGES]);
675                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
676
677                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
678                 callbackManager = JSCallbackManager::createObject(priv->getContext());
679
680                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
681                         
682                 Try 
683                 {
684
685                         Api::Messaging::EventUpdateMessagesPtr event(new Api::Messaging::EventUpdateMessages());
686
687                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
688
689                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
690
691                         JSObjectRef objArg = NULL;
692
693                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
694                         {
695                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
696                         }
697                         else
698                         {
699                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
700                                 {
701                                         if(objectPriv->getIndex()  < 1)
702                                         {
703                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
704                                         }
705                                 }
706                         }
707
708                         if(argumentCount >= 1)  // Message object
709                         {
710                                 if(JSValueIsObject(context, arguments[0]))
711                                 {
712                                         objArg = converter->toJSObjectRef(arguments[0]);                                
713                                 }
714                                 else
715                                 {
716                                         LogDebug("Invalid argument exception");
717                                         Throw(ConversionException);
718                                 }
719                         }
720                         if(argumentCount >= 2)  // successCallback
721                         {
722                                 if(Validator(context).isCallback(arguments[1]))
723                                 {
724                                         onSuccessForCbm = arguments[1];
725                                 }
726                                 else if(!Validator(context).isNullOrUndefined(arguments[1]))    //nullable
727                                 {
728                                         LogDebug("SuccessCallback type is mismatched.");
729                                         Throw(ConversionException);
730                                 }
731                         }
732                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
733                         {       
734                                 if(Validator(context).isCallback(arguments[2]))
735                                 {
736                                         onErrorForCbm = arguments[2];
737                                 }
738                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
739                                 {
740                                         LogDebug("ErrorCallback type is mismatched.");
741                                         Throw(ConversionException);                     
742                                 }
743                         }
744                         if(callbackManager)
745                         {
746                                 callbackManager->setOnSuccess(onSuccessForCbm);
747                                 callbackManager->setOnError(onErrorForCbm);
748                         }
749
750                         std::vector<Api::Messaging::IMessagePtr> msgs;
751                         LogDebug("array length "<<JSGetArrayLength(context, objArg));
752                         for (std::size_t i = 0; i < JSGetArrayLength(context, objArg); i++)
753                         {
754                                 JSValueRef element = JSGetArrayElement(context, objArg, i);
755                                 JSObjectRef arg = JSValueToObject(context, element, NULL);
756                                 Api::Messaging::IMessagePtr msg = converter->toIMessage(arg);
757                                 msgs.push_back(msg);
758                         }
759
760                         event->msgArray = msgs;
761                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
762                         event->setForAsynchronousCall(&MessagingListener::getInstance());
763                         
764                         event->setIndex(objectPriv->getIndex());
765                         event->setType(objectPriv->getType());
766
767                         Api::Messaging::ReqReceiverMessageSingleton::Instance().updateMessages(event);
768
769                         return makePendingOperation(context, event);
770                 }
771                 Catch(WrtDeviceApis::Commons::ConversionException) 
772                 {
773                         LogError(">>> TypeMismatchException");
774                         return JSTizenExceptionFactory::postException(context, exception, 
775                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
776                 }
777                 Catch(WrtDeviceApis::Commons::NullPointerException) 
778                 {
779                         LogError(">>> TypeMismatchException");
780                         return JSTizenExceptionFactory::postException(context, exception, 
781                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
782                 }
783                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
784                 {
785                         LogError(">>> InvalidValuesException");
786                         return JSTizenExceptionFactory::postException(context, exception, 
787                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
788                 }
789                 Catch(WrtDeviceApis::Commons::PlatformException) 
790                 {
791                         LogError(">>> UnknownException");
792                         return JSTizenExceptionFactory::postException(context, exception, 
793                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
794                 }
795                 return JSValueMakeNull(context);
796         }
797
798         JSValueRef JSMessagingStorage::findConversations(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
799                         const JSValueRef arguments[], JSValueRef* exception)
800         {
801
802                 LogDebug("<<<");
803
804                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
805
806                 if (!priv) 
807                 {
808                         LogDebug("priv is NULL.");
809                         return JSTizenExceptionFactory::postException(
810                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
811                 }
812
813                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
814
815                 if (!objectPriv)
816                 {
817                         LogDebug("objectPriv is NULL.");                
818                         return JSTizenExceptionFactory::postException(
819                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
820                 }
821
822                 //check permission.
823                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
824                         MessagingExportedNames[MESSAGING_FUNCTION_API_FIND_CONVERSATIONS]);
825                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
826
827                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
828                 callbackManager = JSCallbackManager::createObject(priv->getContext());
829
830                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
831                         
832                 Try 
833                 {
834                         Api::Messaging::EventQueryConversationsPtr event(new Api::Messaging::EventQueryConversations());
835                         
836                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
837
838                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
839
840                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
841                         {
842                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
843                         }
844                         else
845                         {
846                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
847                                 {
848                                         if(objectPriv->getIndex()  < 1)
849                                         {
850                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
851                                         }
852                                 }
853                         }
854
855                         if(argumentCount >= 1)  //optional AbstractFilter? filter
856                         {
857                                 if(JSValueIsObject(context, arguments[0]))
858                                 {
859                                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[0]);
860                                         event->setFilter(filter);
861                                 }
862                                 else    // NOT nullable 
863                                 {
864                                         LogDebug("Filter type is mismatched.");
865                                         Throw(ConversionException);                     
866                                 }
867                         }
868                         if(argumentCount >= 2)  // successCallback
869                         {
870                                 if(Validator(context).isCallback(arguments[1]))
871                                 {
872                                         onSuccessForCbm = arguments[1];
873                                 }
874                                 else // NOT nullable
875                                 {
876                                         LogDebug("SuccessCallback type is mismatched.");
877                                         Throw(ConversionException);
878                                 }
879                         }
880                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
881                         {       
882                                 if(Validator(context).isCallback(arguments[2]))
883                                 {
884                                         onErrorForCbm = arguments[2];
885                                 }
886                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
887                                 {
888                                         LogDebug("ErrorCallback type is mismatched.");
889                                         Throw(ConversionException);                     
890                                 }
891                         }
892                         if(argumentCount >= 4)  //optional SortMode? sortMode
893                         {
894                                 if(JSValueIsObject(context, arguments[3]))
895                                 {
896                                         Api::Tizen::SortModePtr sortMode = filterConverter->toSortMode(arguments[3]);
897                                         event->setSortMode(sortMode);
898                                 }
899                                 else if(!Validator(context).isNullOrUndefined(arguments[3]))    //nullable
900                                 {
901                                         LogDebug("SortMode type is mismatched.");
902                                         Throw(ConversionException);
903                                 }
904                         }               
905                         if(argumentCount >= 5)  //optional unsigned long? limit JSValueIsUndefined(context, arguments[0]), JSValueIsNull(context, arguments[0])
906                         {
907                                 if (!Validator(context).isNullOrUndefined(arguments[4]))
908                                 {
909                                         if (!JSValueIsNumber(context, arguments[4]))
910                                         {
911                                                 Throw(ConversionException);                                     
912                                         }
913                                         long limit = filterConverter->toULong(arguments[4]);
914                                         event->setLimit(limit);
915                                 }                       
916                         }               
917                         if(argumentCount >= 6)  //optional unsigned long? offset JSValueIsUndefined(context, arguments[0]), JSValueIsNull(context, arguments[0])
918                         {
919                                 if (!Validator(context).isNullOrUndefined(arguments[5]))
920                                 {
921                                         if (!JSValueIsNumber(context, arguments[5]))
922                                         {
923                                                 Throw(ConversionException);                                     
924                                         }
925                                         long offset = filterConverter->toULong(arguments[5]);
926                                         event->setOffset(offset);                                       
927                                 }
928                         }
929                         if(callbackManager)
930                         {
931                                 callbackManager->setOnSuccess(onSuccessForCbm);
932                                 callbackManager->setOnError(onErrorForCbm);
933                         }
934                         
935                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
936                         event->setForAsynchronousCall(&MessagingListener::getInstance());
937                         event->setIndex(objectPriv->getIndex());
938                         event->setType(objectPriv->getType());
939
940                         Api::Messaging::ReqReceiverMessageSingleton::Instance().queryConversations(event);
941
942                         return makePendingOperation(context, event);
943                 }
944                 Catch(WrtDeviceApis::Commons::ConversionException) 
945                 {
946                         LogError(">>> TypeMismatchException");
947                         return JSTizenExceptionFactory::postException(context, exception, 
948                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
949                 }
950                 Catch(WrtDeviceApis::Commons::NullPointerException) 
951                 {
952                         LogError(">>> TypeMismatchException");
953                         return JSTizenExceptionFactory::postException(context, exception, 
954                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
955                 }
956                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
957                 {
958                         LogError(">>> InvalidValuesException");
959                         return JSTizenExceptionFactory::postException(context, exception, 
960                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
961                 }
962                 Catch(WrtDeviceApis::Commons::PlatformException) 
963                 {
964                         LogError(">>> UnknownException");
965                         return JSTizenExceptionFactory::postException(context, exception, 
966                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
967                 }
968
969                 LogError(">>> JSValueMakeNull");
970                 return JSValueMakeNull(context);
971         }
972
973         JSValueRef JSMessagingStorage::removeConversations(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
974                         const JSValueRef arguments[], JSValueRef* exception)
975         {
976                 LogDebug("<<<");
977
978                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
979
980                 if (!priv) 
981                 {
982                         LogDebug("priv is NULL.");
983                         return JSTizenExceptionFactory::postException(
984                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
985                 }
986
987                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
988
989                 if (!objectPriv)
990                 {
991                         LogDebug("objectPriv is NULL.");                
992                         return JSTizenExceptionFactory::postException(
993                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
994                 }
995
996                 //check permission.
997                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
998                                 MessagingExportedNames[MESSAGING_FUNCTION_API_REMOVE_CONVERSATIONS]);
999                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1000
1001                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
1002                 callbackManager = JSCallbackManager::createObject(priv->getContext());
1003
1004                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1005                         
1006                 Try 
1007                 {
1008
1009                         Api::Messaging::EventDeleteConversationsPtr event(new Api::Messaging::EventDeleteConversations());
1010                         
1011                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1012
1013                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
1014
1015                         std::vector<Api::Messaging::IConversationPtr> deleteConversationArray;
1016
1017                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
1018                         {
1019                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
1020                         }
1021                         else
1022                         {
1023                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
1024                                 {
1025                                         if(objectPriv->getIndex()  < 1)
1026                                         {
1027                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
1028                                         }
1029                                 }
1030                         }
1031
1032                         if(argumentCount >= 1)  // Message object
1033                         {
1034                                 if(JSValueIsObject(context, arguments[0]))
1035                                 {
1036                                         deleteConversationArray = converter->toVectorOfConversation(arguments[0]);                              
1037                                 }
1038                                 else
1039                                 {
1040                                         LogDebug("Invalid argument exception");
1041                                         Throw(ConversionException);
1042                                 }
1043                         }
1044                         if(argumentCount >= 2)  // successCallback
1045                         {
1046                                 if(Validator(context).isCallback(arguments[1]))
1047                                 {
1048                                         onSuccessForCbm = arguments[1];
1049                                 }
1050                                 else if(!Validator(context).isNullOrUndefined(arguments[1]))    //nullable
1051                                 {
1052                                         LogDebug("SuccessCallback type is mismatched.");
1053                                         Throw(ConversionException);
1054                                 }
1055                         }
1056                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
1057                         {       
1058                                 if(Validator(context).isCallback(arguments[2]))
1059                                 {
1060                                         onErrorForCbm = arguments[2];
1061                                 }
1062                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
1063                                 {
1064                                         LogDebug("ErrorCallback type is mismatched.");
1065                                         Throw(ConversionException);                     
1066                                 }
1067                         }
1068                         if(callbackManager)
1069                         {
1070                                 callbackManager->setOnSuccess(onSuccessForCbm);
1071                                 callbackManager->setOnError(onErrorForCbm);
1072                         }
1073
1074                         event->setConversations(deleteConversationArray);
1075                         event->setFilterUsing(false);
1076
1077                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
1078                         event->setForAsynchronousCall(&MessagingListener::getInstance());
1079                         event->setIndex(objectPriv->getIndex());
1080                         event->setType(objectPriv->getType());
1081
1082                         Api::Messaging::ReqReceiverMessageSingleton::Instance().deleteConversations(event);
1083
1084                         return makePendingOperation(context, event);
1085                 }
1086                 Catch(WrtDeviceApis::Commons::ConversionException) 
1087                 {
1088                         LogError(">>> TypeMismatchException");
1089                         return JSTizenExceptionFactory::postException(context, exception, 
1090                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1091                 }
1092                 Catch(WrtDeviceApis::Commons::NullPointerException) 
1093                 {
1094                         LogError(">>> TypeMismatchException");
1095                         return JSTizenExceptionFactory::postException(context, exception, 
1096                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1097                 }
1098                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
1099                 {
1100                         LogError(">>> InvalidValuesException");
1101                         return JSTizenExceptionFactory::postException(context, exception, 
1102                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1103                 }
1104                 Catch(WrtDeviceApis::Commons::PlatformException) 
1105                 {
1106                         LogError(">>> UnknownException");
1107                         return JSTizenExceptionFactory::postException(context, exception, 
1108                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1109                 }
1110                 return JSValueMakeNull(context);
1111         }
1112
1113         JSValueRef JSMessagingStorage::findFolders(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
1114                         const JSValueRef arguments[], JSValueRef* exception)
1115         {
1116                 LogDebug("<<<");
1117
1118                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
1119
1120                 if (!priv) 
1121                 {
1122                         LogDebug("priv is NULL.");
1123                         return JSTizenExceptionFactory::postException(
1124                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1125                 }
1126
1127                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
1128
1129                 if (!objectPriv)
1130                 {
1131                         LogDebug("objectPriv is NULL.");                
1132                         return JSTizenExceptionFactory::postException(
1133                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1134                 }
1135
1136                 //check permission.
1137                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1138                         MessagingExportedNames[MESSAGING_FUNCTION_API_FIND_FOLDERS]);
1139                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1140                 
1141                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
1142                 callbackManager = JSCallbackManager::createObject(priv->getContext());
1143
1144                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1145                 
1146                 Try
1147                 {
1148                         Api::Messaging::EventQueryFoldersPtr event(new Api::Messaging::EventQueryFolders());
1149                         
1150                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1151
1152                         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
1153
1154                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
1155                         {
1156                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
1157                         }
1158                         else
1159                         {
1160                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
1161                                 {
1162                                         if(objectPriv->getIndex()  < 1)
1163                                         {
1164                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
1165                                         }
1166                                 }
1167                         }
1168         
1169                         if(argumentCount >= 1)  //optional AbstractFilter? filter
1170                         {
1171                                 if(JSValueIsObject(context, arguments[0]))
1172                                 {
1173                                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[0]);
1174                                         event->setFilter(filter);
1175                                 }
1176                                 else    // NOT nullable 
1177                                 {
1178                                         LogDebug("Filter type is mismatched.");
1179                                         Throw(ConversionException);             
1180                                 }
1181                         }
1182                         if(argumentCount >= 2)  // successCallback
1183                         {
1184                                 if(Validator(context).isCallback(arguments[1]))
1185                                 {
1186                                         onSuccessForCbm = arguments[1];
1187                                 }
1188                                 else // NOT nullable
1189                                 {
1190                                         LogDebug("SuccessCallback type is mismatched.");
1191                                         Throw(ConversionException);
1192                                 }
1193                         }
1194                         if(argumentCount >= 3)  //optional ErrorCallback? errorCallback
1195                         {       
1196                                 if(Validator(context).isCallback(arguments[2]))
1197                                 {
1198                                         onErrorForCbm = arguments[2];
1199                                 }
1200                                 else if(!Validator(context).isNullOrUndefined(arguments[2]))    //nullable
1201                                 {
1202                                         LogDebug("ErrorCallback type is mismatched.");
1203                                         Throw(ConversionException);                     
1204                                 }
1205                         }
1206                         if(callbackManager)
1207                         {
1208                                 callbackManager->setOnSuccess(onSuccessForCbm);
1209                                 callbackManager->setOnError(onErrorForCbm);
1210                         }
1211
1212                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData >(callbackManager));
1213                         event->setForAsynchronousCall(&MessagingListener::getInstance());
1214                         event->setIndex(objectPriv->getIndex());
1215                         event->setType(objectPriv->getType());
1216
1217                         Api::Messaging::ReqReceiverMessageSingleton::Instance().queryFolders(event);
1218
1219                         return makePendingOperation(context, event);
1220
1221                 }
1222                 Catch(WrtDeviceApis::Commons::ConversionException) 
1223                 {
1224                         LogError(">>> TypeMismatchException");
1225                         return JSTizenExceptionFactory::postException(context, exception, 
1226                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1227                 }
1228                 Catch(WrtDeviceApis::Commons::NullPointerException) 
1229                 {
1230                         LogError(">>> TypeMismatchException");
1231                         return JSTizenExceptionFactory::postException(context, exception, 
1232                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1233                 }
1234                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
1235                 {
1236                         LogError(">>> InvalidValuesException");
1237                         return JSTizenExceptionFactory::postException(context, exception, 
1238                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1239                 }
1240                 Catch(WrtDeviceApis::Commons::PlatformException) 
1241                 {
1242                         LogError(">>> UnknownException");
1243                         return JSTizenExceptionFactory::postException(context, exception, 
1244                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1245                 }
1246
1247                 return JSValueMakeNull(context);
1248         }
1249
1250         JSValueRef JSMessagingStorage::addMessagesChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
1251                         const JSValueRef arguments[], JSValueRef* exception) 
1252         {
1253
1254                 LogDebug("<<<");
1255
1256                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
1257
1258                 if (!priv) 
1259                 {
1260                         LogDebug("priv is NULL.");
1261                         return JSTizenExceptionFactory::postException(
1262                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1263                 }
1264
1265                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
1266
1267                 if (!objectPriv)
1268                 {
1269                         LogDebug("objectPriv is NULL.");                
1270                         return JSTizenExceptionFactory::postException(
1271                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1272                 }
1273
1274                 //check permission.
1275                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1276                         MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_MESSAGES_CHANGE_LISTNER]);      
1277                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1278                 
1279                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
1280                 
1281                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesUpdatedCbm(NULL);
1282                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesRemovedCbm(NULL);
1283                 
1284                 callbackManager = JSCallbackManager::createObject(priv->getContext());
1285
1286                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1287
1288                 Try
1289                 {
1290                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1291
1292                         JSValueRef onSuccessForCbm = NULL;
1293
1294                         OnMessagesChanged functionResult;
1295                         int funtionIndex = -1;
1296                         
1297                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
1298                         {
1299                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
1300                         }
1301                         else
1302                         {
1303                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
1304                                 {
1305                                         if(objectPriv->getIndex()  < 1)
1306                                         {
1307                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
1308                                         }
1309                                 }
1310                         }
1311
1312                         if(argumentCount >= 1)  // successCallback
1313                         {
1314                                 if(!Validator(context).isNullOrUndefined(arguments[0]))
1315                                 {
1316                                         functionResult = converter->toMessageMultifunctions(arguments[0]);
1317                                         funtionIndex = functionResult.functionIndex;
1318                                         
1319                                         onSuccessForCbm = functionResult.messagesAdded;
1320                                         messagesUpdatedCbm = JSCallbackManager::createObject(priv->getContext(), functionResult.messagesUpdated, NULL);
1321                                         messagesRemovedCbm = JSCallbackManager::createObject(priv->getContext(), functionResult.messagesRemoved, NULL);
1322                                 }
1323                                 else // NOT nullable
1324                                 {
1325                                         LogDebug("SuccessCallback type is mismatched.");
1326                                         Throw(ConversionException);
1327                                 }
1328                         }
1329
1330                         if(funtionIndex < 0)
1331                         {
1332                                 LogError("funtionIndex is not valid");
1333                                 Throw(ConversionException);                     
1334                         }
1335
1336                         EventOnMessagingStorageChangesPrivateDataPtr privData(
1337                                 new EventOnMessagingStorageChangesPrivateData(callbackManager, messagesUpdatedCbm, messagesRemovedCbm, funtionIndex));
1338                         
1339                         if(argumentCount >= 2)  //optional AbstractFilter? filter
1340                         {
1341                                 if(JSValueIsObject(context, arguments[1]))
1342                                 {
1343                                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[1]);
1344                                         privData->setFilter(filter);
1345                                 }
1346                                 else    // NOT nullable 
1347                                 {
1348                                         LogDebug("Filter type is mismatched.");
1349                                         Throw(ConversionException);                     
1350                                 }
1351                         }
1352                         if(callbackManager)
1353                         {
1354                                 callbackManager->setOnSuccess(onSuccessForCbm);
1355                         }
1356
1357                         privData->setIndex(objectPriv->getIndex());
1358                         privData->setType(objectPriv->getType());                       
1359
1360                         MessagingListener::getInstance().registerMessageReceivedEmitter(privData);
1361
1362                         // add to map and return index
1363                         return JSValueMakeNumber(context,
1364                                         MessagingListener::getInstance().addIncomingMsgCallback(callbackManager, funtionIndex));
1365
1366                 }
1367                 Catch(WrtDeviceApis::Commons::ConversionException) 
1368                 {
1369                         LogError("Error on conversion");
1370                         return JSTizenExceptionFactory::postException(context, exception, 
1371                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1372                 }
1373                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
1374                         LogError("Invalid argument exception");
1375                         return JSTizenExceptionFactory::postException(context, exception, 
1376                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1377                 }
1378                 Catch(WrtDeviceApis::Commons::UnsupportedException) 
1379                 {
1380                         LogError("Unsupported exception");
1381                         return JSTizenExceptionFactory::postException(context, exception, 
1382                                         JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
1383                 }
1384                 Catch(WrtDeviceApis::Commons::AlreadyInUseException) 
1385                 {
1386                         LogError("Already in use exception");
1387                 }
1388                 Catch(WrtDeviceApis::Commons::PlatformException) 
1389                 {
1390                         LogError("not registered for incoming message notifications");
1391                 }
1392
1393                 return JSValueMakeUndefined(context);
1394         }
1395
1396         JSValueRef JSMessagingStorage::addConversationsChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
1397                         const JSValueRef arguments[], JSValueRef* exception) 
1398         {
1399                 LogDebug("<<<");
1400
1401                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
1402
1403                 if (!priv) 
1404                 {
1405                         LogDebug("priv is NULL.");
1406                         return JSTizenExceptionFactory::postException(
1407                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1408                 }
1409
1410                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
1411
1412                 if (!objectPriv)
1413                 {
1414                         LogDebug("objectPriv is NULL.");                
1415                         return JSTizenExceptionFactory::postException(
1416                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1417                 }
1418
1419                 //check permission.
1420                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1421                         MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_CONVERSATIONS_CHANGE_LISTNER]);         
1422                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1423                 
1424                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
1425                 
1426                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesUpdatedCbm(NULL);
1427                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesRemovedCbm(NULL);
1428                 
1429                 callbackManager = JSCallbackManager::createObject(priv->getContext());
1430
1431                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1432
1433                 Try
1434                 {
1435                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1436
1437                         JSValueRef onSuccessForCbm = NULL;
1438
1439                         OnMessagesChanged functionResult;
1440                         int funtionIndex = -1;
1441                         
1442                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
1443                         {
1444                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
1445                         }
1446                         else
1447                         {
1448                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
1449                                 {
1450                                         if(objectPriv->getIndex()  < 1)
1451                                         {
1452                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
1453                                         }
1454                                 }
1455                         }
1456
1457                         if(argumentCount >= 1)  // successCallback
1458                         {
1459                                 if(!Validator(context).isNullOrUndefined(arguments[0]))
1460                                 {
1461                                         functionResult = converter->toMessageMultifunctions(arguments[0]);
1462                                         funtionIndex = functionResult.functionIndex;
1463                                         
1464                                         onSuccessForCbm = functionResult.messagesAdded;
1465                                         messagesUpdatedCbm = JSCallbackManager::createObject(priv->getContext(), functionResult.messagesUpdated, NULL);
1466                                         messagesRemovedCbm = JSCallbackManager::createObject(priv->getContext(), functionResult.messagesRemoved, NULL);
1467                                 }
1468                                 else // NOT nullable
1469                                 {
1470                                         LogDebug("SuccessCallback type is mismatched.");
1471                                         Throw(ConversionException);
1472                                 }
1473                         }
1474
1475                         if(funtionIndex < 0)
1476                         {
1477                                 LogError("funtionIndex is not valid");
1478                                 Throw(ConversionException);                     
1479                         }
1480
1481                         EventOnMessagingStorageChangesPrivateDataPtr privData(
1482                                 new EventOnMessagingStorageChangesPrivateData(callbackManager, messagesUpdatedCbm, messagesRemovedCbm, funtionIndex));
1483                         
1484                         if(argumentCount >= 2)  //optional AbstractFilter? filter
1485                         {
1486                                 if(JSValueIsObject(context, arguments[1]))
1487                                 {
1488                                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[1]);
1489                                         privData->setFilter(filter);
1490                                 }
1491                                 else    // NOT nullable 
1492                                 {
1493                                         LogDebug("Filter type is mismatched.");
1494                                         Throw(ConversionException);                     
1495                                 }
1496                         }
1497                         if(callbackManager)
1498                         {
1499                                 callbackManager->setOnSuccess(onSuccessForCbm);
1500                         }
1501
1502                         privData->setIndex(objectPriv->getIndex());
1503                         privData->setType(objectPriv->getType());                       
1504                 
1505                         MessagingListener::getInstance().registerMessageReceivedEmitter(privData);
1506
1507                         // add to map and return index
1508                         return JSValueMakeNumber(context,
1509                                         MessagingListener::getInstance().addIncomingMsgCallback(callbackManager, funtionIndex));
1510                 }
1511                 Catch(WrtDeviceApis::Commons::ConversionException) 
1512                 {
1513                         LogError("Error on conversion");
1514                         return JSTizenExceptionFactory::postException(context, exception, 
1515                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1516                 }
1517                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
1518                 {
1519                         LogError("Invalid argument exception");
1520                         return JSTizenExceptionFactory::postException(context, exception, 
1521                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1522                 }
1523                 Catch(WrtDeviceApis::Commons::UnsupportedException) 
1524                 {
1525                         LogError("Unsupported exception");
1526                         return JSTizenExceptionFactory::postException(context, exception, 
1527                                         JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
1528                 }
1529                 Catch(WrtDeviceApis::Commons::AlreadyInUseException) 
1530                 {
1531                         LogError("Already in use exception");
1532                 }
1533                 Catch(WrtDeviceApis::Commons::PlatformException) 
1534                 {
1535                         LogError("not registered for incoming message notifications");
1536                 }
1537
1538                 return JSValueMakeUndefined(context);
1539         }
1540
1541
1542         JSValueRef JSMessagingStorage::addFoldersChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
1543                         const JSValueRef arguments[], JSValueRef* exception) 
1544         {
1545
1546                 LogDebug("<<<");
1547
1548                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
1549
1550                 if (!priv) 
1551                 {
1552                         LogDebug("priv is NULL.");
1553                         return JSTizenExceptionFactory::postException(
1554                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1555                 }
1556
1557                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
1558
1559                 if (!objectPriv)
1560                 {
1561                         LogDebug("objectPriv is NULL.");                
1562                         return JSTizenExceptionFactory::postException(
1563                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1564                 }
1565
1566                 //check permission.
1567                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1568                         MessagingExportedNames[MESSAGING_FUNCTION_API_ADD_FOLDERS_CHANGE_LISTNER]);
1569                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1570                 
1571                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
1572                 
1573                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesUpdatedCbm(NULL);
1574                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr messagesRemovedCbm(NULL);
1575                 
1576                 callbackManager = JSCallbackManager::createObject(priv->getContext());
1577
1578                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1579
1580                 Try
1581                 {
1582
1583                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1584
1585                         JSValueRef onSuccessForCbm = NULL;
1586
1587                         OnMessagesChanged functionResult;
1588                         int funtionIndex = -1;
1589                         
1590                         if((objectPriv->getType() < Api::Messaging::SMS) || (objectPriv->getType() > Api::Messaging::EMAIL))
1591                         {
1592                                 ThrowMsg(InvalidArgumentException, "Message type is not SMS or MMS or EMAIL.");
1593                         }
1594                         else
1595                         {
1596                                 if(objectPriv->getType() == Api::Messaging::EMAIL)
1597                                 {
1598                                         if(objectPriv->getIndex()  < 1)
1599                                         {
1600                                                 ThrowMsg(InvalidArgumentException, "serviceId is not defined.");
1601                                         }
1602                                 }
1603                         }
1604
1605                         if(argumentCount >= 1)  // successCallback
1606                         {
1607                                 if(!Validator(context).isNullOrUndefined(arguments[0]))
1608                                 {
1609                                         functionResult = converter->toMessageMultifunctions(arguments[0]);
1610                                         funtionIndex = functionResult.functionIndex;
1611                                         
1612                                         onSuccessForCbm = functionResult.messagesAdded;
1613                                         messagesUpdatedCbm = JSCallbackManager::createObject(priv->getContext(), functionResult.messagesUpdated, NULL);
1614                                         messagesRemovedCbm = JSCallbackManager::createObject(priv->getContext(), functionResult.messagesRemoved, NULL);
1615                                 }
1616                                 else // NOT nullable
1617                                 {
1618                                         LogDebug("SuccessCallback type is mismatched.");
1619                                         Throw(ConversionException);
1620                                 }
1621                         }
1622
1623                         if(funtionIndex < 0)
1624                         {
1625                                 LogError("funtionIndex is not valid");
1626                                 Throw(ConversionException);                     
1627                         }
1628
1629                         EventOnMessagingStorageChangesPrivateDataPtr privData(
1630                                 new EventOnMessagingStorageChangesPrivateData(callbackManager, messagesUpdatedCbm, messagesRemovedCbm, funtionIndex));
1631                         
1632                         if(argumentCount >= 2)  //optional AbstractFilter? filter
1633                         {
1634                                 if(JSValueIsObject(context, arguments[1]))
1635                                 {
1636                                         Api::Tizen::FilterPtr filter = filterConverter->toFilter(arguments[1]);
1637                                         privData->setFilter(filter);
1638                                 }
1639                                 else    // NOT nullable 
1640                                 {
1641                                         LogDebug("Filter type is mismatched.");
1642                                         Throw(ConversionException);                     
1643                                 }
1644                         }
1645                         if(callbackManager)
1646                         {
1647                                 callbackManager->setOnSuccess(onSuccessForCbm);
1648                         }
1649
1650                         privData->setIndex(objectPriv->getIndex());
1651                         privData->setType(objectPriv->getType());
1652
1653                         MessagingListener::getInstance().registerMessageReceivedEmitter(privData);
1654
1655                         // add to map and return index
1656                         return JSValueMakeNumber(context,
1657                                         MessagingListener::getInstance().addIncomingMsgCallback(callbackManager, funtionIndex));
1658
1659                 }
1660                 Catch(WrtDeviceApis::Commons::ConversionException) 
1661                 {
1662                         LogError("Error on conversion");
1663                         return JSTizenExceptionFactory::postException(context, exception, 
1664                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1665                 }
1666                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
1667                 {
1668                         LogError("Invalid argument exception");
1669                         return JSTizenExceptionFactory::postException(context, exception, 
1670                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1671                 }
1672                 Catch(WrtDeviceApis::Commons::UnsupportedException) 
1673                 {
1674                         LogError("Unsupported exception");
1675                         return JSTizenExceptionFactory::postException(context, exception, 
1676                                         JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
1677                 }
1678                 Catch(WrtDeviceApis::Commons::AlreadyInUseException) 
1679                 {
1680                         LogError("Already in use exception");
1681                 }
1682                 Catch(WrtDeviceApis::Commons::PlatformException) 
1683                 {
1684                         LogError("not registered for incoming message notifications");
1685                 }
1686
1687                 return JSValueMakeUndefined(context);
1688         }
1689
1690         JSValueRef JSMessagingStorage::removeChangeListener(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, 
1691                         const JSValueRef arguments[], JSValueRef* exception)
1692         {
1693                 LogDebug("<<<");
1694
1695                 MessagingStoragePriv* priv = static_cast<MessagingStoragePriv*>(JSObjectGetPrivate(thisObject));
1696
1697                 long handler = -1;
1698                 
1699                 if (!priv) 
1700                 {
1701                         LogDebug("priv is NULL.");
1702                         return JSTizenExceptionFactory::postException(
1703                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1704                 }
1705
1706                 MessagingStoragePrivObjPtr objectPriv = priv->getObject();
1707
1708                 if (!objectPriv)
1709                 {
1710                         LogDebug("objectPriv is NULL.");                
1711                         return JSTizenExceptionFactory::postException(
1712                                 context, exception, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1713                 }
1714
1715                 //check permission.
1716                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
1717                         MessagingExportedNames[MESSAGING_FUNCTION_API_REMOVE_CHANGE_LISTENER]);   
1718                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1719                 
1720                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager(NULL);
1721                 callbackManager = JSCallbackManager::createObject(priv->getContext());
1722
1723                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1724
1725                 Try 
1726                 {
1727                         FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
1728                 
1729                         if(argumentCount >= 1)  //optional unsigned long? limit JSValueIsUndefined(context, arguments[0]), JSValueIsNull(context, arguments[0])
1730                         {
1731                                 if (!Validator(context).isNullOrUndefined(arguments[0]))
1732                                 {
1733                                         if (!JSValueIsNumber(context, arguments[0]))
1734                                         {
1735                                                 Throw(ConversionException);                                     
1736                                         }
1737                                         handler = filterConverter->toLong(arguments[0]);
1738                                 }
1739                                 else
1740                                 {
1741                                         Throw(ConversionException);
1742                                 }
1743                         }               
1744                         if(handler < 0)
1745                         {
1746                                 LogError("handler is not valid");
1747                                 Throw(ConversionException);                     
1748                         }
1749                         MessagingListener::getInstance().removeIncomingMsgCallback(handler, objectPriv->getType(), objectPriv->getIndex());
1750                 }
1751                 Catch(WrtDeviceApis::Commons::ConversionException) 
1752                 {
1753                         LogError("conversion error");
1754                         return JSTizenExceptionFactory::postException(context, exception, 
1755                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
1756                 }
1757                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) 
1758                 {
1759                         LogError(">>> InvalidValuesException");
1760                         return JSTizenExceptionFactory::postException(context, exception, 
1761                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1762                 }
1763                 return JSValueMakeUndefined(context);
1764         }
1765
1766 }
1767 }