2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <Commons/Exception.h>
18 #include <Tizen/Common/JSTizenExceptionFactory.h>
19 #include <Tizen/Common/JSTizenException.h>
20 #include <JavaScriptCore/JavaScript.h>
21 #include "JSApplicationInformation.h"
22 #include "ApplicationAnswerReceiver.h"
23 #include "ApplicationConverter.h"
27 namespace Application {
30 using namespace TizenApis::Api::Application;
31 using namespace WrtDeviceApis;
32 using namespace TizenApis::Commons;
34 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const CommonsJavaScript::JSCallbackManagerPtr &callbackManager) :
35 EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
36 EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
37 m_callbackManager(callbackManager) {
40 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const LaunchServicePrivateDataPtr &launchServiceCallbackManager) :
41 EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
42 EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
43 m_privateData(launchServiceCallbackManager) {
47 ApplicationAnswerReceiver::~ApplicationAnswerReceiver() {
50 void ApplicationAnswerReceiver::OnAnswerReceived(const EventListInstalledApplicationsPtr &event)
52 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
55 ApplicationInformationArrayPtr appinfoArray = event->getApplicationInformationArray();
56 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(m_callbackManager->getContext());
57 JSValueRef result = converter->toJSValueRef(appinfoArray);
59 m_callbackManager->callOnSuccess(result);
61 Catch(WrtDeviceApis::Commons::Exception)
63 LogError("error during function executing");
64 JSContextRef context = m_callbackManager->getContext();
65 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
66 m_callbackManager->callOnError(errorObject);
70 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
71 JSContextRef context = m_callbackManager->getContext();
72 JSValueRef errorObject = NULL;
74 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
76 case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
77 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
79 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
80 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
83 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
86 m_callbackManager->callOnError(errorObject);
90 void ApplicationAnswerReceiver::OnAnswerReceived(const EventLaunchServicePtr &event)
93 LaunchServicePrivateDataPtr privateData =
94 DPL::StaticPointerCast<LaunchServicePrivateData>(event->getPrivateData());
95 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
96 LogDebug("OnAnswerReceived");
99 if( event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_CALLBACK)
101 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
102 callbackManager->callOnSuccess();
104 LogDebug("after callOnSuccess");
107 else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_SUCCESS_CALLBACK)
110 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
111 if(replyCallbackManager)
113 LogDebug("check ApplicationServiceReply");
114 ApplicationServiceReplyPtr reply = event->getServiceReply();
116 ApplicationConverterFactory::ConverterType converter =
117 ApplicationConverterFactory::getConverter(replyCallbackManager->getContext());
118 JSValueRef jsObj = converter->toJSValueRef(reply);
120 replyCallbackManager->callOnSuccess(jsObj);
121 LogDebug("after replyCallbackManager onsuccess");
124 else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_FAIL_CALLBACK)
127 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
128 if(replyCallbackManager)
131 LogDebug("check ApplicationServiceReply");
132 ApplicationServiceReplyPtr reply = event->getServiceReply();
134 ApplicationConverterFactory::ConverterType converter =
135 ApplicationConverterFactory::getConverter(replyCallbackManager->getContext());
136 JSValueRef jsObj = converter->toJSValueRef(reply);
138 replyCallbackManager->callOnError(jsObj);
139 LogDebug("after replyCallbackManager onfail");
142 else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_CANCEL_CALLBACK)
144 CommonsJavaScript::JSCallbackManagerPtr replyCancelCallbackManager = privateData->getReplyCancelCallbackManager();
145 if(replyCancelCallbackManager)
147 LogDebug("replyCancelCallbackManager");
148 replyCancelCallbackManager->callOnSuccess();
149 LogDebug("after replyCallback oncancel");
153 Catch(WrtDeviceApis::Commons::Exception)
155 LogError("error during function executing");
159 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
161 JSValueRef errorObject = NULL;
162 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
163 if(callbackManager == NULL )
165 LogWarning("callbackManager is null");
167 JSContextRef context = callbackManager->getContext();
169 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
171 case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
172 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
174 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
175 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
178 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
181 callbackManager->callOnError(errorObject);