2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace DeviceAPI::Common;
35 namespace CallHistory {
37 ResponseDispatcher& ResponseDispatcher::getInstance()
39 static ResponseDispatcher dispatcher;
43 ResponseDispatcher::ResponseDispatcher() :
44 EventAnswerReceiver<EventFindCallHistory>(ThreadEnum::NULL_THREAD),
45 EventAnswerReceiver<EventRemoveBatch>(ThreadEnum::NULL_THREAD),
46 EventAnswerReceiver<EventRemoveAll>(ThreadEnum::NULL_THREAD)
50 void ResponseDispatcher::OnAnswerReceived(const EventFindCallHistoryPtr& event)
52 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
56 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
58 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
59 Converter converter(data->getContext());
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"));
68 JSObjectRef jsException = NULL;
69 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
70 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
72 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
74 data->callOnError(jsException);
78 void ResponseDispatcher::OnAnswerReceived(const EventRemoveBatchPtr& event)
80 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
84 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
86 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
87 data->callOnSuccess();
89 JSObjectRef jsException = NULL;
90 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
91 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
93 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
95 data->callOnError(jsException);
99 void ResponseDispatcher::OnAnswerReceived(const EventRemoveAllPtr& event)
101 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
105 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
107 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
108 data->callOnSuccess();
110 JSObjectRef jsException = NULL;
111 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
112 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
114 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
116 data->callOnError(jsException);