Update change log and spec for wrt-plugins-tizen_0.4.9
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationManager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include <memory>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include <SecurityExceptions.h>
27 #include <Commons/Exception.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
30 #include "ApplicationFactory.h"
31 #include "plugin_config.h"
32 #include "ApplicationController.h"
33 #include "JSApplicationManager.h"
34 #include "ApplicationConverter.h"
35 #include "ApplicationUtil.h"
36 #include "JSApplicationControl.h"
37 #include "ApplicationAsyncCallbackManager.h"
38 #include "ApplicationListenerManager.h"
39 #include "ApplicationInformationEventPrivateData.h"
40 #include "JSApplicationEventCallbackManager.h"
41 #include <bundle.h>
42
43 namespace DeviceAPI {
44 namespace Application {
45
46 using namespace DeviceAPI::Common;
47
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
50
51 JSClassRef JSApplicationManager::m_jsClassRef = NULL;
52
53 JSClassDefinition JSApplicationManager::m_classInfo = {
54                 0,
55                 kJSClassAttributeNone,
56                 TIZEN_INTERFACE_APPLICATION_MANAGER,
57                 NULL,
58                 NULL,
59                 m_function,
60                 initialize,
61                 finalize,
62                 NULL, //hasProperty,
63                 NULL, //getProperty,
64                 NULL, //setProperty,
65                 NULL, //deleteProperty,
66                 NULL, //getPropertyNames,
67                 NULL,
68                 NULL,
69                 hasInstance,
70                 NULL
71 };
72
73 JSStaticFunction JSApplicationManager::m_function[] = {
74                 { APPLICATION_FUNCTION_API_GET_CURRENT_APP, JSApplicationManager::getCurrentApplication, kJSPropertyAttributeNone },
75                 { APPLICATION_FUNCTION_API_LAUNCH, JSApplicationManager::launch, kJSPropertyAttributeNone },
76                 { APPLICATION_FUNCTION_API_KILL, JSApplicationManager::kill, kJSPropertyAttributeNone },
77                 { APPLICATION_FUNCTION_API_SET_USER_AGENT, JSApplicationManager::setUserAgent, kJSPropertyAttributeNone },
78                 { APPLICATION_FUNCTION_API_GET_APPS_INFO, JSApplicationManager::getAppsInfo, kJSPropertyAttributeNone },
79                 { APPLICATION_FUNCTION_API_GET_APPS_CONTEXT, JSApplicationManager::getAppsContext, kJSPropertyAttributeNone },
80                 { APPLICATION_FUNCTION_API_GET_APP_INFO, JSApplicationManager::getAppInfo, kJSPropertyAttributeNone },
81                 { APPLICATION_FUNCTION_API_GET_APP_CONTEXT, JSApplicationManager::getAppContext, kJSPropertyAttributeNone },
82                 { APPLICATION_FUNCTION_API_ADD_APP_INFO_EVENT_LISTENER, JSApplicationManager::addAppInfoEventListener, kJSPropertyAttributeNone },
83                 { APPLICATION_FUNCTION_API_REMOVE_APP_INFO_EVENT_LISTENER, JSApplicationManager::removeAppInfoEventListener, kJSPropertyAttributeNone },
84                 { APPLICATION_FUNCTION_API_LAUNCH_APP_CONTROL, JSApplicationManager::launchAppControl, kJSPropertyAttributeNone },
85                 { APPLICATION_FUNCTION_API_FIND_APP_CONTROL, JSApplicationManager::findAppControl, kJSPropertyAttributeNone },
86                 { APPLICATION_FUNCTION_API_GET_APP_CERTS, JSApplicationManager::getAppCerts, kJSPropertyAttributeNone },
87                 { 0, 0, 0 }
88 };
89
90 const JSClassRef JSApplicationManager::getClassRef() 
91 {
92         if (!m_jsClassRef) {
93                 m_jsClassRef = JSClassCreate(&m_classInfo);
94         }
95         
96         return m_jsClassRef;
97 }
98
99 const JSClassDefinition* JSApplicationManager::getClassInfo()
100 {
101         return &m_classInfo;
102 }
103
104 void JSApplicationManager::initialize(JSContextRef context, JSObjectRef object) 
105 {
106         LogDebug(">>> JSApplicationManager::initialize");
107         ApplicationController* priv = static_cast<ApplicationController*>(JSObjectGetPrivate(object));
108
109         if (!priv) {
110                 IApplicationManagerPtr applications(ApplicationFactory::getInstance().createApplication());
111                 priv = new ApplicationController(context, applications);
112                 
113                 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
114                         LogError("Object can't store private data.");
115                         delete priv;
116                 }
117         } else {
118                 LogDebug("private date is already exist");
119         }
120 }
121
122 void JSApplicationManager::finalize(JSObjectRef object) 
123 {
124         ApplicationController* priv = static_cast<ApplicationController*> (JSObjectGetPrivate(object));
125         JSObjectSetPrivate(object, NULL);
126         delete priv;
127 }
128
129 bool JSApplicationManager::hasInstance(JSContextRef context, 
130         JSObjectRef constructor, 
131         JSValueRef possibleInstance, 
132         JSValueRef* exception) 
133 {
134         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
135 }
136
137
138 JSValueRef JSApplicationManager::getCurrentApplication(JSContextRef context, 
139         JSObjectRef object, 
140         JSObjectRef thisObject, 
141         size_t argumentCount,
142         const JSValueRef arguments[], 
143         JSValueRef* exception) 
144 {
145         LogDebug("entered");
146         IApplicationManagerPtr appmgr;
147         ApplicationController *controller;
148
149         Try     {
150                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
151                 if (!controller) {
152                         ThrowMsg(InvalidArgumentException, "No private object.");
153                 }
154                 appmgr = controller->getObject();
155         } Catch(Exception) {
156                 LogError("No private object");
157                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
158         }
159
160         Try {
161         EventApplicationGetCurrAppPtr event(new EventApplicationGetCurrApp());
162         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
163
164                 event->setForSynchronousCall();
165                 appmgr->getCurrentApplication(event);
166                 
167                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException) {
168                         ThrowMsg(NotFoundException, "Given package not found.");
169                 } else if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnknownException) {
170                         ThrowMsg(Exception, "Unknown error occurred.");
171                 }
172
173                 LogError("get Current Application Successfully. convert application to JSValue");
174
175                 return converter->toJSValueRefFromApplication(event->getApp());
176     } Catch (NotFoundException) {
177         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
178         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
179         } Catch (ConversionException) {
180         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
181                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
182         } Catch (UnsupportedException) {
183                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
184                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
185         } Catch (InvalidArgumentException) {
186                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
187                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
188         } Catch (Exception) {
189         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
190                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
191         }
192 }
193
194
195
196 JSValueRef JSApplicationManager::launch(JSContextRef context, 
197         JSObjectRef object, 
198         JSObjectRef thisObject, 
199         size_t argumentCount,
200         const JSValueRef arguments[], 
201         JSValueRef* exception) 
202 {
203         LogDebug("entered");
204         IApplicationManagerPtr appmgr;
205         JSContextRef gContext;
206         ApplicationController *controller;
207
208         Try     {
209                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
210                 if (!controller) {
211                         ThrowMsg(InvalidArgumentException, "No private object.");
212                 }
213                 appmgr = controller->getObject();
214                 gContext = controller->getContext();
215         } Catch(Exception) {
216                 LogError("No private object");
217                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
218         }
219
220         AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_LAUNCH);
221         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
222
223     JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
224         Try {
225         ApplicationUtil util(context, exception);
226         if (argumentCount > 1) {
227                 if (util.isFunction(arguments[1])) {
228                         callbackManager->setOnSuccess(arguments[1]);
229                 } else if (!util.isNullOrUndefined(arguments[1])) {
230                         ThrowMsg(ConversionException, "Wrong second parameter type.");
231                 }
232         }
233
234         if (argumentCount > 2) {
235                 if (util.isFunction(arguments[2])) {
236                         callbackManager->setOnError(arguments[2]);
237                 } else if (!util.isNullOrUndefined(arguments[2])) {
238                         ThrowMsg(ConversionException, "Wrong third parameter type.");
239                 }
240         }
241
242         callbackManager->setObject(thisObject);
243                 ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
244
245         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
246
247         EventApplicationLaunchPtr event(new EventApplicationLaunch());
248         
249                 if (argumentCount > 0) {
250                         event->setAppId(converter->toString(arguments[0]));
251                 } else {
252             ThrowMsg(ConversionException, "Wrong parameter type.");
253                 }
254
255                 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
256                 event->setForAsynchronousCall(controller);
257
258                 appmgr->launch(event);
259         } Catch (ConversionException) {
260         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
261                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
262         } Catch (UnsupportedException) {
263         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
264                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
265         } Catch (InvalidArgumentException) {
266         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
267                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage()));
268                 return JSValueMakeNull(context);
269         } Catch (Exception) {
270         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
271                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage()));
272                 return JSValueMakeNull(context);
273         }
274
275         return JSValueMakeUndefined(context);
276 }
277
278 JSValueRef JSApplicationManager::kill(JSContextRef context, 
279         JSObjectRef object, 
280         JSObjectRef thisObject, 
281         size_t argumentCount,
282         const JSValueRef arguments[], 
283         JSValueRef* exception) 
284 {
285         LogDebug("entered");
286         IApplicationManagerPtr appmgr;
287         JSContextRef gContext;
288         ApplicationController *controller;
289
290         Try     {
291                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
292                 if (!controller) {
293                         ThrowMsg(InvalidArgumentException, "No private object.");
294                 }
295                 appmgr = controller->getObject();
296                 gContext = controller->getContext();
297         } Catch(Exception) {
298                 LogError("No private object");
299                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
300         }
301
302         AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_KILL);
303         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
304
305         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
306         Try{
307         ApplicationUtil util(context, exception);
308         if (argumentCount > 1) {
309                 if (util.isFunction(arguments[1])) {
310                         callbackManager->setOnSuccess(arguments[1]);
311                 } else if (!util.isNullOrUndefined(arguments[1])) {
312                 ThrowMsg(ConversionException, "Wrong second parameter type.");
313                 }
314         }
315
316         if (argumentCount > 2) {
317                 if (util.isFunction(arguments[2])) {
318                         callbackManager->setOnError(arguments[2]);
319                 } else if (!util.isNullOrUndefined(arguments[2])) {
320                 ThrowMsg(ConversionException, "Wrong third parameter type.");
321                 }
322         }
323
324         callbackManager->setObject(thisObject);
325
326         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
327         EventApplicationKillPtr event(new EventApplicationKill());
328
329                 std::string appContextId;
330                 if (argumentCount > 0) {
331                         appContextId = converter->toString(arguments[0]);
332                 } else {
333             ThrowMsg(ConversionException, "Wrong first parameter type.");
334                 }
335
336                 event->setContextId(appContextId);
337                 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
338                 event->setForAsynchronousCall(controller);
339
340                 appmgr->kill(event);
341
342                 ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
343         } Catch (ConversionException) {
344         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
345                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
346         } Catch (UnsupportedException) {
347         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
348                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
349         } Catch (InvalidArgumentException) {
350         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
351                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage()));
352                 return JSValueMakeNull(context);
353         } Catch (Exception) {
354         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
355                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage()));
356                 return JSValueMakeNull(context);
357         }
358
359         return JSValueMakeUndefined(context);
360 }
361
362
363 JSValueRef JSApplicationManager::setUserAgent(JSContextRef context,
364         JSObjectRef object,
365         JSObjectRef thisObject,
366         size_t argumentCount,
367         const JSValueRef arguments[],
368         JSValueRef* exception)
369 {
370         LogDebug("entered");
371         IApplicationManagerPtr appmgr;
372         ApplicationController *controller;
373
374         Try     {
375                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
376                 if (!controller) {
377                         ThrowMsg(InvalidArgumentException, "No private object.");
378                 }
379                 appmgr = controller->getObject();
380         } Catch(Exception) {
381                 LogError("No private object");
382                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
383         }
384
385         if (argumentCount < 1) {
386                 LogError("Wrong parameters");
387                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
388         }
389
390         Try {
391                 std::string propertyValue;
392                 ApplicationUtil util(context, exception);
393                 if (util.isString(arguments[0])) {
394                         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
395                         propertyValue = "tizen://changeUA?ua=" + converter->toString(arguments[0]);
396                 }else{
397                         LogError("TYPE_MISMATCH_ERROR");
398                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "First parameter should be a DOMString");
399                 }
400
401                 setTitleProperty(context, propertyValue);
402
403         } Catch (WrtDeviceApis::Commons::UnsupportedException) {
404                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, "Not supported");
405         } Catch (WrtDeviceApis::Commons::Exception) {
406                 LogError("Exception: " << _rethrown_exception.GetMessage() << " Code: " << _rethrown_exception.getCode());
407                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
408         }
409
410         LogDebug(">>>");
411         return JSValueMakeUndefined(context);
412 }
413
414 JSValueRef JSApplicationManager::launchAppControl(JSContextRef context,
415         JSObjectRef object, 
416         JSObjectRef thisObject, 
417         size_t argumentCount,
418         const JSValueRef arguments[], 
419         JSValueRef* exception) 
420 {
421         LogDebug("entered");
422         IApplicationManagerPtr appmgr;
423         JSContextRef gContext;
424         ApplicationController *controller;
425
426         Try     {
427                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
428                 if (!controller) {
429                         ThrowMsg(InvalidArgumentException, "No private object.");
430                 }
431                 appmgr = controller->getObject();
432                 gContext = controller->getContext();
433         } Catch(Exception) {
434                 LogError("No private object");
435                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
436         }
437
438         AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_LAUNCH_APP_CONTROL);
439         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);  
440
441     JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
442         Try {
443
444         ApplicationUtil util(context, exception);
445
446
447         if (argumentCount > 2) {
448                 if (util.isFunction(arguments[2])) {
449                         callbackManager->setOnSuccess(arguments[2]);
450                 } else if (!util.isNullOrUndefined(arguments[2])) {
451                         ThrowMsg(ConversionException, "Wrong third parameter type.");
452                 }
453         }
454
455         if (argumentCount > 3) {
456                 if (util.isFunction(arguments[3])) {
457                         callbackManager->setOnError(arguments[3]);
458                 } else if (!util.isNullOrUndefined(arguments[3])) {
459                         ThrowMsg(ConversionException, "Wrong fourth parameter type.");
460                 }
461         }
462
463         callbackManager->setObject(thisObject);
464
465         JSCallbackManagerPtr callbackManagerReply;
466                 if (argumentCount > 4) {
467                         if (util.isObject(arguments[4])) {
468                                 JSObjectRef cbObj = JSValueToObject(context, arguments[4], exception);
469                                 JSValueRef onsuccess = JSUtils::getJSPropertyOrUndefined(context, cbObj, "onsuccess");
470                                 JSValueRef onfail = JSUtils::getJSPropertyOrUndefined(context, cbObj, "onfailure");
471
472                                 if (!util.isNullOrUndefinedOrFunction(onsuccess) || !util.isNullOrUndefinedOrFunction(onfail)) {
473                     ThrowMsg(ConversionException, "Wrong fifth parameter type.");
474                                 }
475
476                                 callbackManagerReply = JSCallbackManager::createObject(gContext);
477                                 
478                                 if (util.isFunction(onsuccess)) {
479                                         callbackManagerReply->setOnSuccess(onsuccess);
480                                 }
481
482                                 if (util.isFunction(onfail)) {
483                                         callbackManagerReply->setOnError(onfail);
484                                 }
485                                 
486                                 callbackManagerReply->setObject(thisObject);
487
488                         } else if (!util.isNullOrUndefined(arguments[4])) {
489                 ThrowMsg(ConversionException, "Wrong fifth parameter type.");
490                         }
491                 }
492
493         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
494         EventApplicationLaunchAppControlPtr event(new EventApplicationLaunchAppControl());
495
496                 ApplicationControlPtr appControl;
497                 if (argumentCount > 0) {
498                         appControl = converter->toApplicationControl(arguments[0]);
499                 } else {
500             ThrowMsg(ConversionException, "Wrong first parameter type.");
501                 }
502
503                 std::string appId = "";
504                 if ((argumentCount > 1) && !util.isNullOrUndefined(arguments[1])) {
505                         appId = converter->toString(arguments[1]);
506                 }
507
508                 event->setAppId(appId);
509                 event->setAppControl(appControl); 
510
511                 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
512                 event->setForAsynchronousCall(controller);
513
514                 if(callbackManagerReply != NULL)
515                 {
516                         EventApplicationLaunchAppControlReplyPtr eventReply(new EventApplicationLaunchAppControlReply());
517                         eventReply->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManagerReply));
518                         eventReply->setForAsynchronousCall(controller);
519
520                         event->setEventReply(eventReply);
521                 }
522
523                 appmgr->launchAppControl(event);
524
525                 if(callbackManagerReply != NULL)
526                         ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManagerReply, gContext);
527
528                 ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
529                 
530         } Catch (ConversionException) {
531         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
532                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
533         } Catch (UnsupportedException) {
534         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
535                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
536         } Catch (InvalidArgumentException) {
537         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
538                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage()));
539                 return JSValueMakeNull(context);
540         } Catch (Exception) {
541         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
542                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage()));
543                 return JSValueMakeNull(context);
544         }
545
546     return JSValueMakeUndefined(context);
547 }
548
549 JSValueRef JSApplicationManager::findAppControl(JSContextRef context,
550         JSObjectRef object,
551         JSObjectRef thisObject,
552         size_t argumentCount,
553         const JSValueRef arguments[],
554         JSValueRef* exception)
555 {
556         LogDebug("entered");
557         IApplicationManagerPtr appmgr;
558         JSContextRef gContext;
559         ApplicationController *controller;
560
561         Try     {
562                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
563                 if (!controller) {
564                         ThrowMsg(InvalidArgumentException, "No private object.");
565                 }
566                 appmgr = controller->getObject();
567                 gContext = controller->getContext();
568         } Catch(Exception) {
569                 LogError("No private object");
570                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
571         }
572
573     JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
574         Try {
575
576         if (argumentCount <= 1) {
577             ThrowMsg(ConversionException, "Wrong parameter type.");
578         }
579
580         ApplicationUtil util(context, exception);
581         if ((argumentCount > 1) && util.isFunction(arguments[1])) {
582                 callbackManager->setOnSuccess(arguments[1]);
583         } else {
584             ThrowMsg(ConversionException, "Wrong second parameter type.");
585         }
586
587         if (argumentCount > 2) {
588                 if (util.isFunction(arguments[2])) {
589                         callbackManager->setOnError(arguments[2]);
590                 } else if (!util.isNullOrUndefined(arguments[2])) {
591                         ThrowMsg(ConversionException, "Wrong third parameter type.");
592                 }
593         }
594
595         callbackManager->setObject(thisObject);
596
597         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
598         EventApplicationFindAppControlPtr event(new EventApplicationFindAppControl());
599
600                 ApplicationControlPtr appControl;
601                 if (argumentCount > 0) {
602                         appControl = converter->toApplicationControl(arguments[0]);
603                 } else {
604             ThrowMsg(ConversionException, "Wrong first parameter type.");
605                 }
606
607                 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
608                 event->setAppControl(appControl);
609                 event->setForAsynchronousCall(controller);
610
611                 appmgr->findAppControl(event);
612                 ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
613
614         } Catch (ConversionException) {
615         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
616                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
617         } Catch (UnsupportedException) {
618         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
619                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
620         } Catch (InvalidArgumentException) {
621         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
622                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage()));
623                 return JSValueMakeNull(context);
624         } Catch (Exception) {
625         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
626                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage()));
627                 return JSValueMakeNull(context);
628         }
629
630     return JSValueMakeUndefined(context);
631 }
632
633 JSValueRef JSApplicationManager::getAppsContext(JSContextRef context, 
634         JSObjectRef object, 
635         JSObjectRef thisObject, 
636         size_t argumentCount,
637         const JSValueRef arguments[], 
638         JSValueRef* exception) 
639 {
640         LogDebug("entered");
641         IApplicationManagerPtr appmgr;
642         JSContextRef gContext;
643         ApplicationController *controller;
644
645         Try     {
646                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
647                 if (!controller) {
648                         ThrowMsg(InvalidArgumentException, "No private object.");
649                 }
650                 appmgr = controller->getObject();
651                 gContext = controller->getContext();
652         } Catch(Exception) {
653                 LogError("No private object");
654                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
655         }
656
657     JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
658         Try {
659         if (argumentCount == 0) {
660             ThrowMsg(ConversionException, "Wrong parameter type.");
661         }
662
663         ApplicationUtil util(context, exception);
664         if ((argumentCount > 0) && util.isFunction(arguments[0])) {
665                 callbackManager->setOnSuccess(arguments[0]);
666         } else {
667             ThrowMsg(ConversionException, "Wrong first parameter type.");
668         }
669
670         if (argumentCount > 1) {
671                 if (util.isFunction(arguments[1])) {
672                         callbackManager->setOnError(arguments[1]);
673                 } else if (!util.isNullOrUndefined(arguments[1])) {
674                 ThrowMsg(ConversionException, "Wrong second parameter type.");
675                 }
676         }
677
678         callbackManager->setObject(thisObject);
679
680         EventApplicationGetAppsContextPtr event(new EventApplicationGetAppsContext());
681
682                 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
683                 event->setForAsynchronousCall(controller);
684
685                 appmgr->getAppsContext(event);
686                 ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
687         } Catch (ConversionException) {
688         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
689                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
690         } Catch (UnsupportedException) {
691         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
692                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
693         } Catch (InvalidArgumentException) {
694         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
695                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage()));
696                 return JSValueMakeNull(context);
697         } Catch (Exception) {
698         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
699                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage()));
700                 return JSValueMakeNull(context);
701         }
702
703         return JSValueMakeUndefined(context);
704 }
705
706 JSValueRef JSApplicationManager::getAppContext(JSContextRef context, 
707         JSObjectRef object, 
708         JSObjectRef thisObject, 
709         size_t argumentCount,
710         const JSValueRef arguments[], 
711         JSValueRef* exception) 
712 {
713         LogDebug("entered");
714         IApplicationManagerPtr appmgr;
715         ApplicationController *controller;
716
717         Try     {
718                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
719                 if (!controller) {
720                         ThrowMsg(InvalidArgumentException, "No private object.");
721                 }
722                 appmgr = controller->getObject();
723         } Catch(Exception) {
724                 LogError("No private object");
725                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
726         }
727
728         Try {
729         EventApplicationGetAppContextPtr event(new EventApplicationGetAppContext());
730         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
731
732                 event->setForSynchronousCall();
733                 ApplicationUtil util(context, exception);
734                 if (argumentCount > 0 && !util.isNullOrUndefined(arguments[0])) {
735                         event->setAppContextId(converter->toString(arguments[0]));
736                 }
737                 appmgr->getAppContext(event);
738
739                 if (event->getExceptionCode() != ExceptionCodes::None)
740                 {
741                         switch (event->getExceptionCode())
742                         {
743                         case ExceptionCodes::NotFoundException:
744                         case ExceptionCodes::InvalidArgumentException:
745                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "Watch id not found");
746                                 break;
747                         case ExceptionCodes::PlatformException:
748                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
749                                 break;
750                         default:
751                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
752                                 break;
753                         }
754                 }
755
756                 return converter->toJSValueRefFromApplicationContext(event->getAppContext());
757
758         } Catch (NotFoundException) {
759         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
760         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
761         } Catch (ConversionException) {
762         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
763                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
764         } Catch (UnsupportedException) {
765         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
766                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
767         } Catch (InvalidArgumentException) {
768         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
769                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
770         } Catch (Exception) {
771         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
772                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
773         }
774 }
775
776 JSValueRef JSApplicationManager::getAppsInfo(JSContextRef context, 
777         JSObjectRef object, 
778         JSObjectRef thisObject, 
779         size_t argumentCount,
780         const JSValueRef arguments[], 
781         JSValueRef* exception) 
782 {
783         LogDebug("entered");
784         IApplicationManagerPtr appmgr;
785         JSContextRef gContext;
786         ApplicationController *controller;
787
788         Try     {
789                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
790                 if (!controller) {
791                         ThrowMsg(InvalidArgumentException, "No private object.");
792                 }
793                 appmgr = controller->getObject();
794                 gContext = controller->getContext();
795         } Catch(Exception) {
796                 LogError("No private object");
797                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
798         }
799
800     JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(gContext);
801         Try{
802         if (argumentCount == 0) {
803                 ThrowMsg(ConversionException, "Wrong parameter type.");
804         }
805
806         ApplicationUtil util(context, exception);
807         if ((argumentCount > 0) && util.isFunction(arguments[0])) {
808                 callbackManager->setOnSuccess(arguments[0]);
809         } else {
810             ThrowMsg(ConversionException, "Wrong first parameter type.");
811         }
812
813         if (argumentCount > 1) {
814                 if (util.isFunction(arguments[1])) {
815                         callbackManager->setOnError(arguments[1]);
816                 } else if (!util.isNullOrUndefined(arguments[1])) {
817                 ThrowMsg(ConversionException, "Wrong second parameter type.");
818                 }
819         }
820
821         callbackManager->setObject(thisObject);
822                 ApplicationAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, gContext);
823
824         EventApplicationGetAppsInfoPtr event(new EventApplicationGetAppsInfo());
825
826                 event->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
827                 event->setForAsynchronousCall(controller);
828                 appmgr->getAppsInfo(event);
829         } Catch (ConversionException) {
830         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
831                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
832         } Catch (UnsupportedException) {
833             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
834                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
835         } Catch (InvalidArgumentException) {
836             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
837                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage()));
838                 return JSValueMakeNull(context);
839         } Catch (Exception) {
840             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
841                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage()));
842                 return JSValueMakeNull(context);
843         }
844         
845         return JSValueMakeUndefined(context);
846 }
847
848 JSValueRef JSApplicationManager::getAppInfo(JSContextRef context, 
849         JSObjectRef object, 
850         JSObjectRef thisObject, 
851         size_t argumentCount,
852         const JSValueRef arguments[], 
853         JSValueRef* exception) 
854 {
855         LogDebug("entered");
856         IApplicationManagerPtr appmgr;
857         ApplicationController *controller;
858
859         Try     {
860                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
861                 if (!controller) {
862                         ThrowMsg(InvalidArgumentException, "No private object.");
863                 }
864                 appmgr = controller->getObject();
865         } Catch(Exception) {
866                 LogError("No private object");
867                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
868         }
869
870         Try {
871         EventApplicationGetAppInfoPtr event(new EventApplicationGetAppInfo());
872         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
873
874                 event->setForSynchronousCall();
875                 ApplicationUtil util(context, exception);
876                 if (argumentCount > 0 && !util.isNullOrUndefined(arguments[0])) {
877                         event->setAppId(converter->toString(arguments[0]));
878                 }
879                 appmgr->getAppInfo(event);
880                 
881                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException) {
882                         ThrowMsg(NotFoundException, "Given package not found.");
883                 } else if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnknownException) {
884                         ThrowMsg(Exception, "Unknown error occurred.");
885                 }
886
887                 return converter->toJSValueRefFromApplicationInformation(event->getAppInfo());
888     } Catch (NotFoundException) {
889         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
890         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
891         } Catch (ConversionException) {
892         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
893                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
894         } Catch (UnsupportedException) {
895                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
896                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
897         } Catch (InvalidArgumentException) {
898                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
899                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
900         } Catch (Exception) {
901         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
902                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
903         }
904 }
905
906 JSValueRef JSApplicationManager::addAppInfoEventListener(JSContextRef context, 
907         JSObjectRef object, 
908         JSObjectRef thisObject, 
909         size_t argumentCount,
910         const JSValueRef arguments[], 
911         JSValueRef* exception) 
912 {
913         LogDebug("entered");
914         IApplicationManagerPtr appmgr;
915         JSContextRef gContext;
916         ApplicationController *controller;
917
918         Try     {
919                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
920                 if (!controller) {
921                         ThrowMsg(InvalidArgumentException, "No private object.");
922                 }
923                 appmgr = controller->getObject();
924                 gContext = controller->getContext();
925         } Catch(Exception) {
926                 LogError("No private object");
927                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
928         }
929
930         Try {
931         ApplicationUtil util(context, exception);
932         if ((argumentCount < 1) || (!util.isObject(arguments[0]))) {
933                 ThrowMsg(ConversionException, "Wrong parameter type.");
934         }
935
936         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
937
938                 JSObjectRef cbObj = converter->toJSObjectRef(arguments[0]);
939             JSValueRef onInstalled = JSUtils::getJSPropertyOrUndefined(context, cbObj, "oninstalled");
940             JSValueRef onUpdated = JSUtils::getJSPropertyOrUndefined(context, cbObj, "onupdated");
941             JSValueRef onUninstalled = JSUtils::getJSPropertyOrUndefined(context, cbObj, "onuninstalled");
942
943                 if (!util.isNullOrUndefinedOrFunction(onInstalled) || !util.isNullOrUndefinedOrFunction(onUpdated) || !util.isNullOrUndefinedOrFunction(onUninstalled)) {
944             ThrowMsg(ConversionException, "Wrong first parameter type.");
945                 }
946
947                 JSApplicationEventCallbackManagerPtr callbackManager =
948                                 JSApplicationEventCallbackManager::createObject(gContext);
949                 if (util.isFunction(onInstalled)) {
950                         callbackManager->setOnInstalled(onInstalled);
951                 }
952                 if (util.isFunction(onUpdated)) {
953                         callbackManager->setOnUpdated(onUpdated);
954                 }
955                 if (util.isFunction(onUninstalled)) {
956                         callbackManager->setOnUninstalled(onUninstalled);
957                 }
958                 EventApplicationAppInfoEventListenerEmitterPtr emitter(new EventApplicationAppInfoEventListenerEmitter());
959
960                 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
961                 emitter->setListener(controller);
962
963         EventApplicationAddAppInfoEventListenerPtr event(new EventApplicationAddAppInfoEventListener());
964
965         event->setEmitter(emitter);
966                 event->setForSynchronousCall();
967
968                 appmgr->addAppInfoEventListener(event);
969
970                 if (event->getExceptionCode() != ExceptionCodes::None)
971                 {
972                         switch (event->getExceptionCode())
973                         {
974                         case ExceptionCodes::InvalidArgumentException:
975                         case ExceptionCodes::PlatformException:
976                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
977                                 break;
978                         default:
979                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
980                                 break;
981                         }
982                 }
983
984                 unsigned long id = event->getWatchId();
985
986                 if (id == 0) {
987                         ThrowMsg(UnsupportedException, "Only one event listener can be registered.");
988                 }
989
990                 ApplicationListenerCancellerPtr canceller = ApplicationListenerCancellerPtr(new ApplicationListenerCanceller(gContext, thisObject, id));
991                 DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
992                 ApplicationListenerManagerSingleton::Instance().registerListener(listenerItem, gContext);
993
994                 return converter->toJSValueRef(id);
995         } Catch (WrtDeviceApis::Commons::ConversionException) {
996                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error");
997         } Catch (WrtDeviceApis::Commons::UnsupportedException) {
998                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, "Not supported"); 
999         } Catch (WrtDeviceApis::Commons::InvalidArgumentException) {
1000                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
1001         } Catch (WrtDeviceApis::Commons::Exception) {
1002                 LogError("Exception: " << _rethrown_exception.GetMessage() << " Code: " << _rethrown_exception.getCode());
1003                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
1004         }
1005 }
1006
1007 JSValueRef JSApplicationManager::removeAppInfoEventListener(JSContextRef context,
1008         JSObjectRef object,
1009         JSObjectRef thisObject,
1010         size_t argumentCount,
1011         const JSValueRef arguments[],
1012         JSValueRef* exception)
1013 {
1014         LogDebug("entered");
1015         IApplicationManagerPtr appmgr;
1016         JSContextRef gContext;
1017         ApplicationController *controller;
1018
1019         Try     {
1020                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
1021                 if (!controller) {
1022                         ThrowMsg(InvalidArgumentException, "No private object.");
1023                 }
1024                 appmgr = controller->getObject();
1025                 gContext = controller->getContext();
1026         } Catch(Exception) {
1027                 LogError("No private object");
1028                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1029         }
1030
1031     Try {
1032         if (argumentCount == 0) {
1033                 ThrowMsg(ConversionException, "Wrong parameter type.");
1034         }
1035
1036                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
1037
1038         EventApplicationRemoveAppInfoEventListenerPtr event(new EventApplicationRemoveAppInfoEventListener());
1039
1040                 event->setForSynchronousCall();
1041
1042                 long id = converter->toLong(arguments[0]);
1043                 event->setWatchId(id);
1044
1045                 appmgr->removeAppInfoEventListener(event);
1046
1047                 if (event->getExceptionCode() != ExceptionCodes::None)
1048                 {
1049                         switch (event->getExceptionCode())
1050                         {
1051                         case ExceptionCodes::NotFoundException:
1052                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, "Watch id not found");
1053                                 break;
1054                         case ExceptionCodes::InvalidArgumentException:
1055                         case ExceptionCodes::PlatformException:
1056                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1057                                 break;
1058                         default:
1059                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Internal error");
1060                                 break;
1061                         }
1062                 }
1063
1064         ApplicationListenerCancellerPtr canceller = ApplicationListenerCancellerPtr(new ApplicationListenerCanceller(gContext, thisObject, id));
1065         DeviceAPI::Common::IListenerItemPtr listenerItem = DPL::StaticPointerCast<DeviceAPI::Common::IListenerItem>(canceller);
1066         ApplicationListenerManagerSingleton::Instance().unregisterListener(listenerItem);
1067
1068         } Catch (NotFoundException) {
1069         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1070                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1071         } Catch (ConversionException) {
1072         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1073                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1074         } Catch (UnsupportedException) {
1075         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1076                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
1077         } Catch (InvalidArgumentException) {
1078         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1079                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1080         } Catch (Exception) {
1081         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1082                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1083         }
1084
1085     return JSValueMakeUndefined(context);
1086 }
1087
1088
1089 JSValueRef JSApplicationManager::getAppCerts(JSContextRef context,
1090         JSObjectRef object,
1091         JSObjectRef thisObject,
1092         size_t argumentCount,
1093         const JSValueRef arguments[],
1094         JSValueRef* exception)
1095 {
1096         LogDebug("entered");
1097         IApplicationManagerPtr appmgr;
1098         ApplicationController *controller;
1099
1100         LogError("JSApplicationManager::getAppCerts");
1101
1102
1103         Try     {
1104                 controller = static_cast<ApplicationController*>(JSObjectGetPrivate(thisObject));
1105                 if (!controller) {
1106                         ThrowMsg(InvalidArgumentException, "No private object.");
1107                 }
1108                 appmgr = controller->getObject();
1109         } Catch(Exception) {
1110                 LogError("No private object");
1111                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
1112         }
1113
1114         AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_GET_APP_CERTS);
1115         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1116
1117         Try {
1118         EventApplicationGetAppCertsPtr event(new EventApplicationGetAppCerts());
1119         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
1120
1121                 event->setForSynchronousCall();
1122                 ApplicationUtil util(context, exception);
1123                 if (argumentCount > 0 && !util.isNullOrUndefined(arguments[0])) {
1124                         event->setAppId(converter->toString(arguments[0]));
1125                 }
1126                 appmgr->getAppCerts(event);
1127                 
1128                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException) {
1129                         ThrowMsg(NotFoundException, "Given package not found.");
1130                 } else if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnknownException) {
1131                         ThrowMsg(Exception, "Unknown error occurred.");
1132                 }
1133
1134                 return converter->toJSValueRefFromeApplicationCerts(event->getAppCerts());
1135     } Catch (NotFoundException) {
1136         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1137         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
1138         } Catch (ConversionException) {
1139         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1140                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
1141         } Catch (UnsupportedException) {
1142                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1143                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
1144         } Catch (InvalidArgumentException) {
1145                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1146                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
1147         } Catch (Exception) {
1148         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
1149                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
1150         }
1151
1152 }
1153
1154
1155 void JSApplicationManager::setTitleProperty(JSContextRef context, std::string propertyValue){
1156         LogDebug("<<<propertyValue:[" << propertyValue << "]");
1157
1158         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
1159         // get window object
1160         JSObjectRef windowObject = JSContextGetGlobalObject(context);
1161
1162         // get title object
1163         JSObjectRef documentObject = converter.toJSObjectRef(JSObjectGetProperty(context,
1164                         windowObject,
1165                         ScopedJSStringRef(JSStringCreateWithUTF8CString("document")).get(),
1166                         NULL));
1167
1168         JSObjectSetProperty(context,
1169                         documentObject,
1170                         ScopedJSStringRef(JSStringCreateWithUTF8CString("title")).get(),
1171                         JSValueMakeString(context, JSStringCreateWithUTF8CString("tizen://dummy#$#@##")),
1172                         kJSPropertyAttributeNone,
1173                         NULL);
1174
1175         JSObjectSetProperty(context,
1176                         documentObject,
1177                         ScopedJSStringRef(JSStringCreateWithUTF8CString("title")).get(),
1178                         JSValueMakeString(context, JSStringCreateWithUTF8CString(propertyValue.c_str())),
1179                         kJSPropertyAttributeNone,
1180                         NULL);
1181
1182         LogDebug(">>>");
1183 }
1184
1185 }
1186 }