wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Callhistory / ResponseDispatcher.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17  
18 #include "ResponseDispatcher.h"
19 #include <dpl/assert.h>
20 #include <Commons/Exception.h>
21 #include <Commons/IEvent.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <JSTizenExceptionFactory.h>
25 #include <JSTizenException.h> 
26 #include "JSCallHistory.h"
27 #include "Converter.h"
28 #include "CallHistoryAsyncCallbackManager.h"
29 #include <Logger.h>
30
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace DeviceAPI::Common;
34
35
36 namespace DeviceAPI {
37 namespace CallHistory {
38
39 ResponseDispatcher& ResponseDispatcher::getInstance()
40 {
41         static ResponseDispatcher dispatcher;
42         return dispatcher;
43 }
44
45 ResponseDispatcher::ResponseDispatcher() :
46         EventAnswerReceiver<EventFindCallHistory>(ThreadEnum::NULL_THREAD),
47         EventAnswerReceiver<EventRemoveBatch>(ThreadEnum::NULL_THREAD),
48         EventAnswerReceiver<EventRemoveAll>(ThreadEnum::NULL_THREAD)
49 {
50 }
51
52 void ResponseDispatcher::OnAnswerReceived(const EventFindCallHistoryPtr& event)
53 {
54         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
55         if (!data)
56                 return;
57
58         CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
59         
60         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
61                 Converter converter(data->getContext());
62                 try {
63                         data->callOnSuccess(converter.toJSValueRef(event->getResult(), data->getContext()));
64                 } catch(WrtDeviceApis::Commons::ConversionException) {
65                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error"));
66                 } catch(WrtDeviceApis::Commons::Exception) {
67                         data->callOnError(JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error"));
68                 }
69         } else {
70                 JSObjectRef jsException = NULL;
71                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
72                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
73                 } else {
74                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
75                 }
76                 data->callOnError(jsException);
77         }
78 }
79
80 void ResponseDispatcher::OnAnswerReceived(const EventRemoveBatchPtr& event)
81 {
82         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
83         if (!data)
84                 return;
85
86         CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
87
88         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
89                 data->callOnSuccess();
90         } else {
91                 JSObjectRef jsException = NULL;
92                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
93                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
94                 } else {
95                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
96                 }
97                 data->callOnError(jsException);
98         }
99 }
100
101 void ResponseDispatcher::OnAnswerReceived(const EventRemoveAllPtr& event)
102 {
103         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
104         if (!data)
105                 return;
106
107         CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
108
109         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
110                 data->callOnSuccess();
111         } else {
112                 JSObjectRef jsException = NULL;
113                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
114                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
115                 } else {
116                         jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
117                 }
118                 data->callOnError(jsException);
119         }
120 }
121
122 }
123
124