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 "JSApplicationContext.h"
23 #include "ApplicationAnswerReceiver.h"
24 #include "ApplicationConverter.h"
28 namespace Application {
31 using namespace TizenApis::Api::Application;
32 using namespace WrtDeviceApis;
33 using namespace TizenApis::Commons;
35 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const CommonsJavaScript::JSCallbackManagerPtr &callbackManager) :
36 EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
37 EventAnswerReceiver<EventManageApplication> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
38 EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
39 m_callbackManager(callbackManager) {
42 ApplicationAnswerReceiver::ApplicationAnswerReceiver(const LaunchServicePrivateDataPtr &launchServiceCallbackManager) :
43 EventAnswerReceiver<EventListInstalledApplications> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
44 EventAnswerReceiver<EventManageApplication> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
45 EventAnswerReceiver<EventLaunchService> (WrtDeviceApis::Commons::ThreadEnum::NULL_THREAD),
46 m_privateData(launchServiceCallbackManager) {
50 ApplicationAnswerReceiver::~ApplicationAnswerReceiver() {
53 void ApplicationAnswerReceiver::OnAnswerReceived(const EventListInstalledApplicationsPtr &event)
55 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
56 if (event->getEventType() == EventListInstalledApplications::APPMANAGER_LIST_INSTALLED_APPLICATIONS) {
59 ApplicationInformationArrayPtr appinfoArray = event->getApplicationInformationArray();
60 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(m_callbackManager->getContext());
61 JSValueRef result = converter->toJSValueRef(appinfoArray);
62 m_callbackManager->callOnSuccess(result);
64 Catch(WrtDeviceApis::Commons::Exception)
66 LogError("error during function executing");
67 JSContextRef context = m_callbackManager->getContext();
68 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
69 m_callbackManager->callOnError(errorObject);
71 } else if (event->getEventType() == EventListInstalledApplications::APPMANAGER_LIST_RUNNING_APPLICATIONS) {
74 ApplicationContextArrayPtr appcontextArray = event->getApplicationContextArray();
75 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(m_callbackManager->getContext());
76 JSValueRef result = converter->toJSValueRef(appcontextArray);
78 m_callbackManager->callOnSuccess(result);
80 Catch(WrtDeviceApis::Commons::Exception)
82 LogError("error during function executing");
83 JSContextRef context = m_callbackManager->getContext();
84 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
85 m_callbackManager->callOnError(errorObject);
88 LogError("unknow event type");
89 JSContextRef context = m_callbackManager->getContext();
90 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
91 m_callbackManager->callOnError(errorObject);
95 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
96 JSContextRef context = m_callbackManager->getContext();
97 JSValueRef errorObject = NULL;
99 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
101 case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
102 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
104 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
105 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
108 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
111 m_callbackManager->callOnError(errorObject);
115 void ApplicationAnswerReceiver::OnAnswerReceived(const EventManageApplicationPtr &event)
117 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
118 if (event->getEventType() == EventManageApplication::APP_MANAGER_KILL_APPLICATION ||
119 event->getEventType() == EventManageApplication::APP_MANAGER_HIDE_APPLICATION ||
120 event->getEventType() == EventManageApplication::APP_MANAGER_LAUNCH_APPLICATION) {
121 LogDebug("Event Type: " << event->getEventType());
122 m_callbackManager->callOnSuccess();
124 LogError("unknow event type");
125 JSContextRef context = m_callbackManager->getContext();
126 JSValueRef errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
127 m_callbackManager->callOnError(errorObject);
131 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
132 JSContextRef context = m_callbackManager->getContext();
133 JSValueRef errorObject = NULL;
135 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
137 case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
138 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
140 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
141 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
144 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
147 m_callbackManager->callOnError(errorObject);
152 void ApplicationAnswerReceiver::OnAnswerReceived(const EventLaunchServicePtr &event)
155 LaunchServicePrivateDataPtr privateData =
156 DPL::StaticPointerCast<LaunchServicePrivateData>(event->getPrivateData());
157 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None)
159 LogDebug("OnAnswerReceived");
162 if( event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_CALLBACK)
164 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
165 callbackManager->callOnSuccess();
167 LogDebug("after callOnSuccess");
170 else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_SUCCESS_CALLBACK)
173 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
174 if(replyCallbackManager)
176 LogDebug("check ApplicationServiceReply");
177 std::vector<ApplicationServiceDataPtr> reply = event->getService()->getServiceDataArray();
179 ApplicationConverterFactory::ConverterType converter =
180 ApplicationConverterFactory::getConverter(replyCallbackManager->getContext());
181 JSValueRef jsObj = converter->toJSValueRef(reply);
183 replyCallbackManager->callOnSuccess(jsObj);
184 LogDebug("after replyCallbackManager onsuccess");
187 else if(event->getCallbackType() == EventLaunchService::APPLICATION_SERVICE_REPLY_FAIL_CALLBACK)
190 CommonsJavaScript::JSCallbackManagerPtr replyCallbackManager = privateData->getReplyCallbackManager();
191 if(replyCallbackManager)
193 replyCallbackManager->callOnError();
194 LogDebug("after replyCallbackManager onfail");
198 Catch(WrtDeviceApis::Commons::Exception)
200 LogError("error during function executing");
205 LogDebug("Operation failed. Exception code: " << event->getExceptionCode());
207 JSValueRef errorObject = NULL;
208 CommonsJavaScript::JSCallbackManagerPtr callbackManager = privateData->getCallbackManager();
209 if(callbackManager == NULL )
211 LogWarning("callbackManager is null");
213 JSContextRef context = callbackManager->getContext();
215 WrtDeviceApis::Commons::ExceptionCodes::Enumeration exception = event->getExceptionCode();
217 case WrtDeviceApis::Commons::ExceptionCodes::NotFoundException:
218 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::NOT_FOUND_ERROR,"given package is not found");
220 case WrtDeviceApis::Commons::ExceptionCodes::PlatformException:
221 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"platform exception");
224 errorObject = JSTizenExceptionFactory::makeErrorObject(context,JSTizenException::UNKNOWN_ERROR,"unknown error");
227 callbackManager->callOnError(errorObject);