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 <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"
39 #define LOG_TAG "TIZEN_N_NETWORKBEARER"
43 namespace NetworkBearerSelection {
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace WrtDeviceApis::Commons;
49 using namespace DeviceAPI::Common;
51 JSClassDefinition JSNetworkBearerSelection::m_classInfo = {
53 kJSClassAttributeNone,
54 "NetworkBearerSelection",
71 JSStaticFunction JSNetworkBearerSelection::m_function[] = {
72 { "requestRouteToHost", JSNetworkBearerSelection::requestRouteToHost, kJSPropertyAttributeNone },
73 { "releaseRouteToHost", JSNetworkBearerSelection::releaseRouteToHost, kJSPropertyAttributeNone },
77 const JSClassRef JSNetworkBearerSelection::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_classInfo);
85 const JSClassDefinition* JSNetworkBearerSelection::getClassInfo()
90 JSClassRef JSNetworkBearerSelection::m_jsClassRef = JSClassCreate(JSNetworkBearerSelection::getClassInfo());
92 void JSNetworkBearerSelection::initialize(JSContextRef context, JSObjectRef object)
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.");
101 LogDebug("JSNetworkBearerSelection::initialize ");
103 LogDebug("Private object already set.");
107 void JSNetworkBearerSelection::finalize(JSObjectRef object)
109 JSNetworkBearerSelectionPriv* priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(object));
111 JSObjectSetPrivate(object, NULL);
112 LogDebug("Deleting gallery");
116 JSValueRef JSNetworkBearerSelection::requestRouteToHost(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
117 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
120 JSNetworkBearerSelectionPriv *priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(thisObject));
122 Converter converter(context);
123 Validator check(context, exception);
126 LogError("private object is null");
127 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
129 if (argumentCount < 3) {
130 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
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");
140 JSValueRef onsuccess;
142 JSValueRef onresumed;
143 JSValueRef ondisconnected;
146 JSObjectRef callbackObject = converter.toJSObjectRef(arguments[2]);
148 onsuccess = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onsuccess");
149 if (check.isNullOrUndefined(onsuccess))
151 else if (!check.isCallback(onsuccess)) {
152 ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
155 onpaused = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onpaused");
156 if (check.isNullOrUndefined(onpaused))
158 else if (!check.isCallback(onpaused)) {
159 ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
162 onresumed = JSUtils::getJSPropertyOrUndefined(context, callbackObject, "onresumed");
163 if (check.isNullOrUndefined(onresumed))
165 else if (!check.isCallback(onresumed)) {
166 ThrowMsg(ConversionException, "2nd argument's onsuccess attribute is not a 'function'");
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'");
176 } Catch(ConversionException) {
177 LogError("Error on conversion : " << _rethrown_exception.GetMessage());
178 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
181 JSNetworkBearerSelectionCallbackManagerPtr callbackManager = JSNetworkBearerSelectionCallbackManager::createObject(priv->getContext());
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");
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]);
197 INetworkBearerSelectionPtr NetworkBearerSelections(priv->getObject());
199 EventNetworkBearerSelectionPtr dplEvent(new EventNetworkBearerSelection());
200 OnNetworkBearerSelectionStateChangedEmitterPtr emitter(new OnNetworkBearerSelectionStateChangedEmitter());
202 emitter->setEventPrivateData(DPL::StaticPointerCast<IEventPrivateData>(callbackManager));
203 emitter->setListener(&NetworkBearerSelectionResponseDispatcher::getInstance());
205 if (NetworkBearerSelections->checkCellularNetworkEnable()) {
206 dplEvent->setNetworkType(networkType);
207 dplEvent->setDomainName(domainName);
208 dplEvent->setEmitter(emitter);
209 dplEvent->setForAsynchronousCall(&NetworkBearerSelectionResponseDispatcher::getInstance());
211 NetworkBearerSelections->requestRouteToHost(dplEvent);
213 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NETWORK_ERROR, "Network error"));
217 Catch(WrtDeviceApis::Commons::Exception) {
218 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
220 return JSValueMakeUndefined(context);
223 JSValueRef JSNetworkBearerSelection::releaseRouteToHost(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
224 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
227 JSNetworkBearerSelectionPriv *priv = static_cast<JSNetworkBearerSelectionPriv*>(JSObjectGetPrivate(thisObject));
228 Converter converter(context);
229 Validator check(context, exception);
232 LogError("private object is null");
233 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
235 if (argumentCount < 3) {
236 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
238 if (!check.isCallback(arguments[2])) {
239 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
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");
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");
257 JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
258 callbackManager->setObject(thisObject);
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());
268 NetworkBearerSelections->releaseRouteToHost(dplEvent);
271 Catch(WrtDeviceApis::Commons::Exception) {
272 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
275 return JSValueMakeUndefined(context);