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 <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"
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace DeviceAPI::Common;
37 namespace CallHistory {
39 ResponseDispatcher& ResponseDispatcher::getInstance()
41 static ResponseDispatcher dispatcher;
45 ResponseDispatcher::ResponseDispatcher() :
46 EventAnswerReceiver<EventFindCallHistory>(ThreadEnum::NULL_THREAD),
47 EventAnswerReceiver<EventRemoveBatch>(ThreadEnum::NULL_THREAD),
48 EventAnswerReceiver<EventRemoveAll>(ThreadEnum::NULL_THREAD)
52 void ResponseDispatcher::OnAnswerReceived(const EventFindCallHistoryPtr& event)
54 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
58 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
60 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
61 Converter converter(data->getContext());
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"));
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");
74 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
76 data->callOnError(jsException);
80 void ResponseDispatcher::OnAnswerReceived(const EventRemoveBatchPtr& event)
82 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
86 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
88 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
89 data->callOnSuccess();
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");
95 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
97 data->callOnError(jsException);
101 void ResponseDispatcher::OnAnswerReceived(const EventRemoveAllPtr& event)
103 JSCallbackManagerPtr data = DPL::DynamicPointerCast<JSCallbackManager>(event->getPrivateData());
107 CallHistoryAsyncCallbackManagerSingleton::Instance().unregisterCallbackManager(data);
109 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
110 data->callOnSuccess();
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");
116 jsException = JSTizenExceptionFactory::makeErrorObject(data->getContext(), JSTizenException::UNKNOWN_ERROR, "Unknown error");
118 data->callOnError(jsException);