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 "Converter.h"
25 #include "CallHistoryAsyncCallbackManager.h"
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30 using namespace DeviceAPI::Common;
34 namespace CallHistory {
36 ResponseDispatcher& ResponseDispatcher::getInstance()
38 static ResponseDispatcher dispatcher;
42 ResponseDispatcher::ResponseDispatcher() :
43 EventAnswerReceiver<EventFindCallHistory>(ThreadEnum::NULL_THREAD),
44 EventAnswerReceiver<EventRemoveBatch>(ThreadEnum::NULL_THREAD),
45 EventAnswerReceiver<EventRemoveAll>(ThreadEnum::NULL_THREAD)
49 void ResponseDispatcher::OnAnswerReceived(const EventFindCallHistoryPtr& event)
51 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
55 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
57 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
58 Converter converter(data->getContext());
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"));
67 JSObjectRef jsException = NULL;
68 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
69 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
71 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
73 data->callOnError(jsException);
77 void ResponseDispatcher::OnAnswerReceived(const EventRemoveBatchPtr& event)
79 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
83 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
85 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
86 data->callOnSuccess();
88 JSObjectRef jsException = NULL;
89 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
90 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
92 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
94 data->callOnError(jsException);
98 void ResponseDispatcher::OnAnswerReceived(const EventRemoveAllPtr& event)
100 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
104 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
106 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
107 data->callOnSuccess();
109 JSObjectRef jsException = NULL;
110 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::SecurityException) {
111 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::SECURITY_ERROR, "Permission denied error");
113 jsException = JSWebAPIErrorFactory::makeErrorObject(data->getContext(), JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
115 data->callOnError(jsException);