Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / ResponseDispatcher.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 #include "ResponseDispatcher.h"
19
20 #include <dpl/log/log.h>
21 #include <dpl/assert.h>
22
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"
31
32 using namespace WrtDeviceApis::Commons;
33 using namespace WrtDeviceApis::CommonsJavaScript;
34 using namespace TizenApis::Commons;
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38 ResponseDispatcher& ResponseDispatcher::getInstance()
39 {
40         static ResponseDispatcher dispatcher;
41         return dispatcher;
42 }
43
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)
50 {
51 }
52
53 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventFindCallHistoryPtr& event)
54 {
55         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
56         Assert(NULL != data);
57
58         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
59                 Converter converter(data->getContext());
60                 try {
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"));
64                 }
65         } else {
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");
69                 } else {
70                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
71                 }
72                 data->callOnError(jsException);
73         }
74 }
75
76 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventRemoveBatchPtr& event)
77 {
78         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
79         Assert(NULL != data);
80
81         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
82                 Try {
83                         data->callOnSuccess();
84                 } Catch(WrtDeviceApis::Commons::ConversionException) {
85                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
86                 }
87         } else {
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");
91                 } else {
92                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
93                 }
94                 data->callOnError(jsException);
95         }
96 }
97
98 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventRemoveAllPtr& event)
99 {
100         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
101         Assert(NULL != data);
102
103         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
104                 Try {
105                         data->callOnSuccess();
106                 } Catch(WrtDeviceApis::Commons::ConversionException) {
107                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
108                 }
109         } else {
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");
113                 } else {
114                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
115                 }
116                 data->callOnError(jsException);
117         }
118 }
119
120 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventLaunchDialerPtr& event)
121 {
122         LogDebug("Enter");
123         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
124         Assert(NULL != data);
125
126         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
127                 Try {
128                         data->callOnSuccess();
129                 } Catch(WrtDeviceApis::Commons::ConversionException) {
130                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error"));
131                 }
132         } else {
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");
136                 } else {
137                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
138                 }
139                 data->callOnError(jsException);
140         }
141 }
142
143 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventSendUSSDPtr& event)
144 {
145         LogDebug("Enter");
146
147         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
148         Assert(NULL != data);
149
150         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
151                 Converter converter(data->getContext());
152                 try {
153                         data->callOnSuccess(converter.toJSValueRef(event->getResult()));
154                 } catch(WrtDeviceApis::Commons::ConversionException) {
155                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
156                 }
157         } else {
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");
161                 } else {
162                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
163                 }
164                 data->callOnError(jsException);
165         }
166 }
167 }
168
169