b6a9a2c372bbfe0988f11215ed305199684898d5
[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         JSValueRef onsuccess;
137         JSValueRef onpaused;
138         JSValueRef onresumed;
139         JSValueRef ondisconnected;
140
141     Try {
142         JSObjectRef callbackObject = converter.toJSObjectRef(arguments[2]);
143
144         onsuccess = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onsuccess");
145         if (check.isNullOrUndefined(onsuccess))
146             onsuccess = NULL;
147         else if (!check.isCallback(onsuccess)) {
148             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
149         }
150
151         onpaused = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpaused");
152         if (check.isNullOrUndefined(onpaused))
153             onpaused = NULL;
154         else if (!check.isCallback(onpaused)) {
155             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
156         }
157
158         onresumed = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onresumed");
159         if (check.isNullOrUndefined(onresumed))
160             onresumed = NULL;
161         else if (!check.isCallback(onresumed)) {
162             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
163         }
164
165         ondisconnected = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "ondisconnected");
166         if (check.isNullOrUndefined(ondisconnected))
167             ondisconnected = NULL;
168         else if (!check.isCallback(ondisconnected)) {
169             ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
170         }
171
172     } Catch(ConversionException) {
173         LoggerE("Error on conversion : " << _rethrown_exception.GetMessage());
174         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
175     }
176
177         JSNetworkBearerSelectionCallbackManagerPtr callbackManager = JSNetworkBearerSelectionCallbackManager::createObject(priv->getContext());
178
179     std::string networkType = converter.toString(arguments[0]);
180     std::string domainName = converter.toString(arguments[1]);
181     if (strcmp(networkType.c_str(), "CELLULAR") != 0) {
182         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
183     }
184
185         callbackManager->setOnSuccess(onsuccess);
186         callbackManager->setOnPaused(onpaused);
187         callbackManager->setOnResumed(onresumed);
188         callbackManager->setOnDisconneced(ondisconnected);
189         if(argumentCount > 3)
190                 callbackManager->setOnError(arguments[3]);
191
192     Try {
193         INetworkBearerSelectionPtr NetworkBearerSelections(priv->getObject());
194        
195         EventNetworkBearerSelectionPtr dplEvent(new EventNetworkBearerSelection());
196         OnNetworkBearerSelectionStateChangedEmitterPtr emitter(new OnNetworkBearerSelectionStateChangedEmitter());
197
198         emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
199         emitter->setListener(&NetworkBearerSelectionResponseDispatcher::getInstance());
200
201         if (NetworkBearerSelections->checkCellularNetworkEnable()) {
202         dplEvent->setNetworkType(networkType);
203         dplEvent->setDomainName(domainName);
204         dplEvent->setEmitter(emitter);
205         dplEvent->setForAsynchronousCall(&NetworkBearerSelectionResponseDispatcher::getInstance());
206
207         NetworkBearerSelections->requestRouteToHost(dplEvent);
208         } else {
209             callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NETWORK_ERROR, "Network error"));        
210         }
211     }
212     
213     Catch(WrtDeviceApis::Commons::Exception) {
214         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");        
215     }
216     return JSValueMakeUndefined(context);
217 }
218
219 JSValueRef JSNetworkBearerSelection::releaseRouteToHost(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
220         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
221 {
222     LoggerD("enter");
223     JSNetworkBearerSelectionPriv *priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(thisObject));
224     Converter converter(context);
225     Validator check(context, exception);
226     
227     if (!priv) {
228         LoggerE("private object is null");
229         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
230     }
231     if (argumentCount < 3) {
232         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
233     }
234     if (!check.isCallback(arguments[2])) {
235         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
236     }
237
238     std::string networkType = converter.toString(arguments[0]);
239     std::string domainName = converter.toString(arguments[1]);
240     if (strcmp(networkType.c_str(), "CELLULAR") != 0) {
241         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");        
242     }
243
244     JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
245     onSuccessForCbm = arguments[2];
246     if (argumentCount > 3) {
247         if (check.isCallback(arguments[3])) {
248             onErrorForCbm = arguments[3];
249         } else if (!JSValueIsNull(context, arguments[3]) && !JSValueIsUndefined(context, arguments[3])) {
250             return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
251         }
252     }
253     JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
254     callbackManager->setObject(thisObject);     
255
256     Try {
257         INetworkBearerSelectionPtr NetworkBearerSelections(priv->getObject());
258         EventNetworkBearerReleasePtr dplEvent(new EventNetworkBearerRelease());
259         dplEvent->setNetworkType(networkType);
260         dplEvent->setDomainName(domainName);
261         dplEvent->setPrivateData(StaticPointerCast<IEventPrivateData>(callbackManager));
262         dplEvent->setForAsynchronousCall(&NetworkBearerSelectionResponseDispatcher::getInstance());
263        
264         NetworkBearerSelections->releaseRouteToHost(dplEvent);
265     }
266     
267     Catch(WrtDeviceApis::Commons::Exception) {
268         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");        
269     }
270
271     return JSValueMakeUndefined(context);
272 }
273
274 }
275 }