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