Update change log and spec for wrt-plugins-tizen_0.4.46
[platform/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 <Commons/Exception.h>
20 #include <Commons/IEvent.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <JSWebAPIErrorFactory.h>
24 #include "JSCallHistory.h"
25 #include "Converter.h"
26 #include "CallHistoryAsyncCallbackManager.h"
27 #include <Logger.h>
28
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace DeviceAPI::Common;
32
33
34 namespace DeviceAPI {
35 namespace CallHistory {
36
37 ResponseDispatcher& ResponseDispatcher::getInstance()
38 {
39         static ResponseDispatcher dispatcher;
40         return dispatcher;
41 }
42
43 ResponseDispatcher::ResponseDispatcher() :
44         EventAnswerReceiver<EventFindCallHistory>(ThreadEnum::NULL_THREAD),
45         EventAnswerReceiver<EventRemoveBatch>(ThreadEnum::NULL_THREAD),
46         EventAnswerReceiver<EventRemoveAll>(ThreadEnum::NULL_THREAD)
47 {
48 }
49
50 void ResponseDispatcher::OnAnswerReceived(const EventFindCallHistoryPtr& event)
51 {
52         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
53         if (!data)
54                 return;
55
56         CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(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(JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error"));
64                 } catch(WrtDeviceApis::Commons::Exception) {
65                         data->callOnError(JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error"));
66                 }
67         } else {
68                 JSObjectRef jsException = NULL;
69                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
70                         jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
71                 } else {
72                         jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
73                 }
74                 data->callOnError(jsException);
75         }
76 }
77
78 void ResponseDispatcher::OnAnswerReceived(const EventRemoveBatchPtr& event)
79 {
80         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
81         if (!data)
82                 return;
83
84         CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
85
86         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
87                 data->callOnSuccess();
88         } else {
89                 JSObjectRef jsException = NULL;
90                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
91                         jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
92                 } else {
93                         jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
94                 }
95                 data->callOnError(jsException);
96         }
97 }
98
99 void ResponseDispatcher::OnAnswerReceived(const EventRemoveAllPtr& event)
100 {
101         JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
102         if (!data)
103                 return;
104
105         CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
106
107         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
108                 data->callOnSuccess();
109         } else {
110                 JSObjectRef jsException = NULL;
111                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
112                         jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
113                 } else {
114                         jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
115                 }
116                 data->callOnError(jsException);
117         }
118 }
119
120 }
121
122