1f74f57c4e04f1158e9e99f3f92011fb4f14745d
[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 {
48 }
49
50 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventFindCallHistoryPtr& event)
51 {
52         DPL::SharedPtr<JSCallbackManager> data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
53         Assert(NULL != data);
54
55         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
56                 Converter converter(data->getContext());
57                 try {
58                         data->callOnSuccess(converter.toJSValueRef(event->getResult(), data->getContext()));
59                 } catch(WrtDeviceApis::Commons::ConversionException) {
60                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
61                 }
62         } else {
63                 JSObjectRef jsException = NULL;
64                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
65                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
66                 } else {
67                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error");
68                 }
69                 data->callOnError(jsException);
70         }
71 }
72
73 void ResponseDispatcher::OnAnswerReceived(const Api::Call::EventRemoveBatchPtr& event)
74 {
75         DPL::SharedPtr<JSCallbackManager> data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
76         Assert(NULL != data);
77
78         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
79                 Try {
80                         data->callOnSuccess();
81                 } Catch(WrtDeviceApis::Commons::ConversionException) {
82                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error"));
83                 }
84         } else {
85                 JSObjectRef jsException = NULL;
86                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
87                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
88                 } else {
89                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::IO_ERROR, "IO error");
90                 }
91                 data->callOnError(jsException);
92         }
93 }
94
95 }
96
97