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