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.
18 #include "ResponseDispatcher.h"
20 #include <dpl/log/log.h>
21 #include <dpl/assert.h>
23 #include <Commons/Exception.h>
24 #include <Commons/IEvent.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <CommonsJavaScript/JSCallbackManager.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "JSCallHistory.h"
30 #include "Converter.h"
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34 using namespace TizenApis::Commons;
38 ResponseDispatcher& ResponseDispatcher::getInstance()
40 static ResponseDispatcher dispatcher;
44 ResponseDispatcher::ResponseDispatcher() :
45 EventAnswerReceiver<Api::Call::EventFindCallHistory>(ThreadEnum::NULL_THREAD),
46 EventAnswerReceiver<Api::Call::EventRemoveBatch>(ThreadEnum::NULL_THREAD),
47 EventAnswerReceiver<Api::Call::EventRemoveAll>(ThreadEnum::NULL_THREAD),
48 EventAnswerReceiver<Api::Call::EventLaunchDialer>(ThreadEnum::NULL_THREAD),
49 EventAnswerReceiver<Api::Call::EventSendUSSD>(ThreadEnum::NULL_THREAD)
53 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventFindCallHistoryPtr& event)
55 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
58 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
59 Converter converter(data->getContext());
61 data->callOnSuccess(converter.toJSValueRef(event->getResult(), data->getContext()));
62 } catch(WrtDeviceApis::Commons::ConversionException) {
63 data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
66 JSObjectRef jsException = NULL;
67 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
68 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
70 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
72 data->callOnError(jsException);
76 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventRemoveBatchPtr& event)
78 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
81 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
83 data->callOnSuccess();
84 } Catch(WrtDeviceApis::Commons::ConversionException) {
85 data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
88 JSObjectRef jsException = NULL;
89 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
90 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
92 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
94 data->callOnError(jsException);
98 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventRemoveAllPtr& event)
100 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
101 Assert(NULL != data);
103 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
105 data->callOnSuccess();
106 } Catch(WrtDeviceApis::Commons::ConversionException) {
107 data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
110 JSObjectRef jsException = NULL;
111 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
112 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
114 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
116 data->callOnError(jsException);
120 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventLaunchDialerPtr& event)
123 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
124 Assert(NULL != data);
126 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
128 data->callOnSuccess();
129 } Catch(WrtDeviceApis::Commons::ConversionException) {
130 data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error"));
133 JSObjectRef jsException = NULL;
134 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
135 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
137 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
139 data->callOnError(jsException);
143 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventSendUSSDPtr& event)
147 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
148 Assert(NULL != data);
150 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
151 Converter converter(data->getContext());
153 data->callOnSuccess(converter.toJSValueRef(event->getResult()));
154 } catch(WrtDeviceApis::Commons::ConversionException) {
155 data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
158 JSObjectRef jsException = NULL;
159 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
160 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
162 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
164 data->callOnError(jsException);