Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / NetworkBearerSelection / JSNetworkBearerSelection.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 <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "NetworkBearerSelectionFactory.h"
25 #include "INetworkBearerSelection.h"
26 #include "EventNetworkBearerSelection.h"
27 #include "EventNetworkBearerRelease.h"
28 #include "OnNetworkBearerSelectionStateChanged.h"
29 #include <JSTizenExceptionFactory.h>
30 #include <JSTizenException.h>
31 #include <SecurityExceptions.h>
32 #include "JSNetworkBearerSelection.h"
33 #include "JSNetworkBearerSelectionCallbackManager.h"
34 #include "NetworkBearerSelectionResponseDispatcher.h"
35 #include "plugin_config.h"
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #define LOG_TAG "TIZEN_N_NETWORKBEARER"
40 #endif
41
42 namespace DeviceAPI {
43 namespace NetworkBearerSelection {
44
45 using namespace std;
46 using namespace DPL;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace WrtDeviceApis::Commons;
49 using namespace DeviceAPI::Common;
50
51 JSClassDefinition JSNetworkBearerSelection::m_classInfo = {
52     0,
53     kJSClassAttributeNone,
54     "NetworkBearerSelection",
55     NULL,
56     NULL,
57     m_function,
58     initialize,
59     finalize,
60     NULL,
61     NULL,
62     NULL,
63     NULL,
64     NULL,
65     NULL,
66     NULL,
67     NULL,
68     NULL
69 };
70
71 JSStaticFunction JSNetworkBearerSelection::m_function[] = {
72     { "requestRouteToHost", JSNetworkBearerSelection::requestRouteToHost, kJSPropertyAttributeNone },
73     { "releaseRouteToHost", JSNetworkBearerSelection::releaseRouteToHost, kJSPropertyAttributeNone },
74     { 0, 0, 0 }
75 };
76
77 const JSClassRef JSNetworkBearerSelection::getClassRef()
78 {
79     if (!m_jsClassRef) {
80         m_jsClassRef = JSClassCreate(&m_classInfo);
81     }
82     return m_jsClassRef;
83 }
84
85 const JSClassDefinition* JSNetworkBearerSelection::getClassInfo()
86 {
87     return &m_classInfo;
88 }
89
90 JSClassRef JSNetworkBearerSelection::m_jsClassRef = JSClassCreate(JSNetworkBearerSelection::getClassInfo());
91
92 void JSNetworkBearerSelection::initialize(JSContextRef context, JSObjectRef object)
93 {
94     if (!JSObjectGetPrivate(object)) {
95         INetworkBearerSelectionPtr NetworkBearers(NetworkBearerSelectionFactory::getInstance().getNetworkBearerSelections());
96         JSNetworkBearerSelectionPriv* priv = new JSNetworkBearerSelectionPriv(context, NetworkBearers);
97         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
98             LogError("Object can't store private data.");
99             delete priv;
100         }
101         LogDebug("JSNetworkBearerSelection::initialize ");
102     } else {
103         LogDebug("Private object already set.");
104     }
105 }
106
107 void JSNetworkBearerSelection::finalize(JSObjectRef object)
108 {
109     JSNetworkBearerSelectionPriv* priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(object));
110     
111     JSObjectSetPrivate(object, NULL);
112     LogDebug("Deleting gallery");
113     delete priv;
114 }
115
116 JSValueRef JSNetworkBearerSelection::requestRouteToHost(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
117         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
118 {
119     LogDebug("enter");
120     JSNetworkBearerSelectionPriv *priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(thisObject));
121
122     Converter converter(context);
123     Validator check(context, exception);
124
125     if (!priv) {
126         LogError("private object is null");
127         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
128     }
129     if (argumentCount < 3) {
130         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
131     }
132     if (argumentCount == 4) {
133         if (check.isCallback(arguments[3])) {
134             LogDebug("arguments 3 is callback");
135         } else if (!JSValueIsNull(context, arguments[3]) && !JSValueIsUndefined(context, arguments[3])) {
136             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
137         }
138     }
139
140         JSValueRef onsuccess;
141         JSValueRef onpaused;
142         JSValueRef onresumed;
143         JSValueRef ondisconnected;
144
145     Try {
146         JSObjectRef callbackObject = converter.toJSObjectRef(arguments[2]);
147
148         onsuccess = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onsuccess");
149         if (check.isNullOrUndefined(onsuccess))
150             onsuccess = NULL;
151         else if (!check.isCallback(onsuccess)) {
152             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
153         }
154
155         onpaused = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpaused");
156         if (check.isNullOrUndefined(onpaused))
157             onpaused = NULL;
158         else if (!check.isCallback(onpaused)) {
159             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
160         }
161
162         onresumed = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onresumed");
163         if (check.isNullOrUndefined(onresumed))
164             onresumed = NULL;
165         else if (!check.isCallback(onresumed)) {
166             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
167         }
168
169         ondisconnected = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "ondisconnected");
170         if (check.isNullOrUndefined(ondisconnected))
171             ondisconnected = NULL;
172         else if (!check.isCallback(ondisconnected)) {
173             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
174         }
175
176     } Catch(ConversionException) {
177         LogError("Error on conversion : " << _rethrown_exception.GetMessage());
178         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
179     }
180
181         JSNetworkBearerSelectionCallbackManagerPtr callbackManager = JSNetworkBearerSelectionCallbackManager::createObject(priv->getContext());
182
183     std::string networkType = converter.toString(arguments[0]);
184     std::string domainName = converter.toString(arguments[1]);
185     if (strcmp(networkType.c_str(), "CELLULAR") != 0) {
186         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
187     }
188
189         callbackManager->setOnSuccess(onsuccess);
190         callbackManager->setOnPaused(onpaused);
191         callbackManager->setOnResumed(onresumed);
192         callbackManager->setOnDisconneced(ondisconnected);
193         if(argumentCount > 3)
194                 callbackManager->setOnError(arguments[3]);
195
196     Try {
197         INetworkBearerSelectionPtr NetworkBearerSelections(priv->getObject());
198        
199         EventNetworkBearerSelectionPtr dplEvent(new EventNetworkBearerSelection());
200         OnNetworkBearerSelectionStateChangedEmitterPtr emitter(new OnNetworkBearerSelectionStateChangedEmitter());
201
202         emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
203         emitter->setListener(&NetworkBearerSelectionResponseDispatcher::getInstance());
204
205         if (NetworkBearerSelections->checkCellularNetworkEnable()) {
206         dplEvent->setNetworkType(networkType);
207         dplEvent->setDomainName(domainName);
208         dplEvent->setEmitter(emitter);
209         dplEvent->setForAsynchronousCall(&NetworkBearerSelectionResponseDispatcher::getInstance());
210
211         NetworkBearerSelections->requestRouteToHost(dplEvent);
212         } else {
213             callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NETWORK_ERROR, "Network error"));        
214         }
215     }
216     
217     Catch(WrtDeviceApis::Commons::Exception) {
218         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");        
219     }
220     return JSValueMakeUndefined(context);
221 }
222
223 JSValueRef JSNetworkBearerSelection::releaseRouteToHost(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
224         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
225 {
226     LogDebug("enter");
227     JSNetworkBearerSelectionPriv *priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(thisObject));
228     Converter converter(context);
229     Validator check(context, exception);
230     
231     if (!priv) {
232         LogError("private object is null");
233         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
234     }
235     if (argumentCount < 3) {
236         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
237     }
238     if (!check.isCallback(arguments[2])) {
239         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
240     }
241
242     std::string networkType = converter.toString(arguments[0]);
243     std::string domainName = converter.toString(arguments[1]);
244     if (strcmp(networkType.c_str(), "CELLULAR") != 0) {
245         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");        
246     }
247
248     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
249     onSuccessForCbm = arguments[2];
250     if (argumentCount > 3) {
251         if (check.isCallback(arguments[3])) {
252             onErrorForCbm = arguments[3];
253         } else if (!JSValueIsNull(context, arguments[3]) && !JSValueIsUndefined(context, arguments[3])) {
254             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
255         }
256     }
257     JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
258     callbackManager->setObject(thisObject);     
259
260     Try {
261         INetworkBearerSelectionPtr NetworkBearerSelections(priv->getObject());
262         EventNetworkBearerReleasePtr dplEvent(new EventNetworkBearerRelease());
263         dplEvent->setNetworkType(networkType);
264         dplEvent->setDomainName(domainName);
265         dplEvent->setPrivateData(StaticPointerCast<IEventPrivateData>(callbackManager));
266         dplEvent->setForAsynchronousCall(&NetworkBearerSelectionResponseDispatcher::getInstance());
267        
268         NetworkBearerSelections->releaseRouteToHost(dplEvent);
269     }
270     
271     Catch(WrtDeviceApis::Commons::Exception) {
272         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");        
273     }
274
275     return JSValueMakeUndefined(context);
276 }
277
278 }
279 }