Update change log and spec for wrt-plugins-tizen_0.4.48-1
[platform/framework/web/wrt-plugins-tizen.git] / src / NetworkBearerSelection / NetworkBearerSelectionResponseDispatcher.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 "NetworkBearerSelectionResponseDispatcher.h"
19 #include "JSNetworkBearerSelectionCallbackManager.h"
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <JSWebAPIErrorFactory.h>
22 #include <Logger.h>
23
24 using namespace DeviceAPI::Common;
25 using namespace WrtDeviceApis::Commons;
26 using namespace WrtDeviceApis::CommonsJavaScript;
27
28 namespace DeviceAPI {
29 namespace NetworkBearerSelection {
30
31 NetworkBearerSelectionResponseDispatcher& NetworkBearerSelectionResponseDispatcher::getInstance()
32 {
33     static NetworkBearerSelectionResponseDispatcher instance;
34     return instance;
35 }
36
37 NetworkBearerSelectionResponseDispatcher::NetworkBearerSelectionResponseDispatcher() :
38     NetworkBearerSelectionEventAnswerReceiverRequest(ThreadEnum::NULL_THREAD),
39     NetworkBearerReleaseEventAnswerReceiverRequest(ThreadEnum::NULL_THREAD),        
40     OnNetworkBearerSelectionStateChangedListener(ThreadEnum::NULL_THREAD)    
41 {
42 }
43
44 NetworkBearerSelectionResponseDispatcher::~NetworkBearerSelectionResponseDispatcher()
45 {
46 }
47
48 void NetworkBearerSelectionResponseDispatcher::OnAnswerReceived(const EventNetworkBearerSelectionPtr &event)
49 {
50     LoggerD("entered");
51 }
52
53 void NetworkBearerSelectionResponseDispatcher::OnAnswerReceived(const EventNetworkBearerReleasePtr &event)
54 {
55     LoggerD("entered");
56     JSCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSCallbackManager >(event->getPrivateData());
57
58     if (!callbackManager) {
59         LoggerE("No listener callback!");
60         return;
61     }
62
63         JSContextRef gContext = callbackManager->getContext();        
64     JSValueRef error = NULL;
65     
66         if (ExceptionCodes::None != event->getExceptionCode())
67         {
68                 switch (event->getExceptionCode())
69                 {
70                 case ExceptionCodes::PlatformException:
71                         error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
72                         break;
73                 case ExceptionCodes::NotFoundException:
74                         error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::NOT_FOUND_ERROR, "record not found");
75                         break;
76                 case ExceptionCodes::InvalidArgumentException:
77                         error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "DomainName is not setted");
78                         break;
79                 case ExceptionCodes::ConversionException:
80                         error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong argument");
81                         break;
82         case ExceptionCodes::AlreadyInUseException:
83                         error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::INVALID_STATE_ERROR, "Connection is disconnected.");
84                         break;
85                 default:
86                         error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
87                         break;
88                 }
89
90                 callbackManager->callOnError(error);
91                 return;
92         }        
93     callbackManager->callOnSuccess();
94 }
95
96
97 void NetworkBearerSelectionResponseDispatcher::onAnswerReceived(const OnNetworkBearerSelectionStateChangedPtr& event)
98 {
99     LoggerD("entered");
100     JSNetworkBearerSelectionCallbackManagerPtr callbackManager = DPL::StaticPointerCast<JSNetworkBearerSelectionCallbackManager>(event->getPrivateData());
101
102     if (!callbackManager) {
103         LoggerE("No listener callback!");
104         return;
105     }
106
107     Try
108     {
109         if (event->getConnectionStateType() == CONNECTION_STATE_CONNECTED) {
110             LoggerD("success callback");
111             callbackManager->callOnSuccess();
112         } else if (event->getConnectionStateType() == CONNECTION_STATE_DISCONNECTED) {
113             LoggerD("disconnect callback");  
114             callbackManager->callOnDisconnected();
115         } else if (event->getConnectionStateType() == CONNECTION_STATE_INVALID_VALUES_ERROR || event->getConnectionStateType() == CONNECTION_STATE_PLATFORM_ERROR) {
116                 JSContextRef gContext = callbackManager->getContext();        
117             JSValueRef error = NULL;
118             
119                 if (ExceptionCodes::None != event->getExceptionCode())
120                 {
121                         switch (event->getExceptionCode())
122                         {
123                         case ExceptionCodes::PlatformException:
124                                 error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
125                                 break;
126                         case ExceptionCodes::NotFoundException:
127                                 error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::NOT_FOUND_ERROR, "record not found");
128                                 break;
129                         case ExceptionCodes::InvalidArgumentException:
130                                 error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, "Invalid values error");
131                                 break;
132                         case ExceptionCodes::ConversionException:
133                                 error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong argument");
134                                 break;
135                         default:
136                                 error = JSWebAPIErrorFactory::makeErrorObject(gContext, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Unknown error");
137                                 break;
138                         }
139
140                         callbackManager->callOnError(error);
141                         return;
142                 }               
143             LoggerD("error callback");
144         }
145     }
146     Catch(Exception)
147     {
148         LoggerE("Error during processing answer.");
149     }
150 }
151
152 }
153 }