2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <API/Call/CallFactory.h>
24 #include <API/Call/ICallManager.h>
25 #include <API/Call/EventSendUSSD.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include <Tizen/Common/SecurityExceptions.h>
29 #include "JSCallService.h"
30 #include "JSCellularCallService.h"
31 #include "ResponseDispatcher.h"
32 #include "Converter.h"
33 #include "plugin_config.h"
37 using namespace WrtDeviceApis;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace TizenApis::Api::Call;
41 using namespace TizenApis::Api::Account;
42 using namespace TizenApis::Commons;
47 JSClassRef JSCellularCallService::m_jsClassRef = NULL;
49 JSClassDefinition JSCellularCallService::m_classInfo =
52 kJSClassAttributeNone,
53 "CellularCallService",
54 JSCallService::getClassRef(),
62 NULL, //deleteProperty,
63 NULL, //getPropertyNames,
70 JSStaticValue JSCellularCallService::m_property[] = {
71 { "subscriberNumbers", getProperty, NULL, kJSPropertyAttributeReadOnly },
72 { "emergencyNumbers", getProperty, NULL, kJSPropertyAttributeReadOnly },
76 JSStaticFunction JSCellularCallService::m_function[] =
78 { "sendUSSD", JSCellularCallService::sendUSSD, kJSPropertyAttributeNone },
82 const JSClassRef JSCellularCallService::getClassRef()
85 m_jsClassRef = JSClassCreate(&m_classInfo);
90 const JSClassDefinition* JSCellularCallService::getClassInfo()
95 JSObjectRef JSCellularCallService::createJSObject(JSContextRef context, const AccountServices &serviceInfo)
97 ICallServicePtr callService(CallFactory::getInstance().getCallServiceObject());
98 CallServiceObjectPtr privateData = CallServiceObjectPtr(new CallServiceObject(serviceInfo, callService));
100 JSCellularCallServicePriv *priv = new JSCellularCallServicePriv(context, privateData);
103 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
106 JSObjectRef jsObjRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
107 if (NULL == jsObjRef) {
108 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
113 void JSCellularCallService::initialize(JSContextRef context, JSObjectRef object)
115 LogDebug("JSCellularCallService::initialize ");
118 void JSCellularCallService::finalize(JSObjectRef object)
120 JSCellularCallServicePriv* priv = static_cast<JSCellularCallServicePriv*>(JSObjectGetPrivate(object));
121 JSObjectSetPrivate(object, NULL);
125 JSValueRef JSCellularCallService::getProperty(JSContextRef context,
127 JSStringRef propertyName,
128 JSValueRef* exception)
130 JSCellularCallServicePriv *priv = static_cast<JSCellularCallServicePriv*>(JSObjectGetPrivate(object));
132 return JSValueMakeUndefined(context);
135 JSContextRef globalContext = priv->getContext();
136 CallServiceObjectPtr privateData = priv->getObject();
137 ICallServicePtr callService(privateData->getICallService());
140 Converter convert(context);
141 if(JSStringIsEqualToUTF8CString(propertyName, "subscriberNumbers")) {
142 return convert.toJSValueRef(callService->getSubscriberNumbers(), globalContext);
143 } else if(JSStringIsEqualToUTF8CString(propertyName, "emergencyNumbers")) {
144 return convert.toJSValueRef(callService->getEmergencyNumbers(), globalContext);
146 } catch(WrtDeviceApis::Commons::Exception) {
147 LogWarning("trying to get incorrect value");
149 return JSValueMakeUndefined(context);
152 bool JSCellularCallService::hasInstance(JSContextRef context, JSObjectRef constructor,
153 JSValueRef possibleInstance, JSValueRef* exception)
155 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
158 JSValueRef JSCellularCallService::sendUSSD(JSContextRef context, JSObjectRef object,
159 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
160 JSValueRef* exception)
162 if (argumentCount < 1 || argumentCount > 3) {
163 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
166 JSCellularCallServicePriv *priv = static_cast<JSCellularCallServicePriv*> (JSObjectGetPrivate(thisObject));
167 JSContextRef gContext = priv->getContext();
169 assert(priv && "Invalid private pointer.");
170 Converter converter(context);
171 Validator check(context, exception);
174 AceSecurityStatus status = CALL_CHECK_ACCESS(
176 CALL_FUNCTION_API_LAUNCHDIALER);
178 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
180 if (!JSValueIsString(context, arguments[0])) {
181 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : ussd string");
184 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
186 if (argumentCount >= 3) {
187 if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
188 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[2]))) {
189 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback");
191 cbm->setOnError(arguments[2]);
195 if (argumentCount >= 2) {
196 if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
197 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
198 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback");
200 cbm->setOnSuccess(arguments[1]);
204 EventSendUSSDPtr event(new EventSendUSSD());
205 event->setCommand(converter.toString(arguments[0]));
206 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
207 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
209 CallServiceObjectPtr privateData = priv->getObject();
210 ICallServicePtr callService(privateData->getICallService());
212 callService->sendUSSD(event);
213 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
214 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
215 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
216 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
217 } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
218 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
219 } catch(const WrtDeviceApis::Commons::Exception& ex) {
220 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
223 return JSValueMakeUndefined(context);