Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSCellularCallService.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. 
15  */
16
17
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"
34
35 using namespace std;
36 using namespace DPL;
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;
43
44 namespace TizenApis {
45 namespace Tizen1_0 {
46
47 JSClassRef JSCellularCallService::m_jsClassRef = NULL;
48
49 JSClassDefinition JSCellularCallService::m_classInfo =
50 {
51         0,
52         kJSClassAttributeNone,
53         "CellularCallService",
54         JSCallService::getClassRef(),
55         m_property,
56         m_function,
57         initialize,
58         finalize,
59         NULL, //hasProperty,
60         NULL, //getProperty,
61         NULL, //setProperty,
62         NULL, //deleteProperty,
63         NULL, //getPropertyNames,
64         NULL,
65         NULL,
66         hasInstance,
67         NULL
68 };
69
70 JSStaticValue JSCellularCallService::m_property[] = {
71         { "subscriberNumbers", getProperty, NULL, kJSPropertyAttributeReadOnly },
72         { "emergencyNumbers", getProperty, NULL, kJSPropertyAttributeReadOnly },
73         { 0, 0, 0, 0 }
74 };
75
76 JSStaticFunction JSCellularCallService::m_function[] =
77 {
78         { "sendUSSD", JSCellularCallService::sendUSSD, kJSPropertyAttributeNone },
79         { 0, 0, 0 }
80 };
81
82 const JSClassRef JSCellularCallService::getClassRef()
83 {
84         if (!m_jsClassRef) {
85                 m_jsClassRef = JSClassCreate(&m_classInfo);
86         }
87         return m_jsClassRef;
88 }
89
90 const JSClassDefinition* JSCellularCallService::getClassInfo()
91 {
92         return &m_classInfo;
93 }
94
95 JSObjectRef JSCellularCallService::createJSObject(JSContextRef context, const AccountServices &serviceInfo)
96 {
97         ICallServicePtr callService(CallFactory::getInstance().getCallServiceObject());
98         CallServiceObjectPtr privateData = CallServiceObjectPtr(new CallServiceObject(serviceInfo, callService));
99
100         JSCellularCallServicePriv *priv = new JSCellularCallServicePriv(context, privateData);
101
102         if (!priv) {
103                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
104         }
105
106         JSObjectRef jsObjRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
107         if (NULL == jsObjRef) {
108                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
109         }
110         return jsObjRef;
111 }
112
113 void JSCellularCallService::initialize(JSContextRef context, JSObjectRef object)
114 {
115         LogDebug("JSCellularCallService::initialize ");
116 }
117
118 void JSCellularCallService::finalize(JSObjectRef object)
119 {
120         JSCellularCallServicePriv* priv = static_cast<JSCellularCallServicePriv*>(JSObjectGetPrivate(object));
121         JSObjectSetPrivate(object, NULL);
122         delete priv;
123 }
124
125 JSValueRef JSCellularCallService::getProperty(JSContextRef context,
126         JSObjectRef object,
127         JSStringRef propertyName,
128         JSValueRef* exception)
129 {
130         JSCellularCallServicePriv *priv = static_cast<JSCellularCallServicePriv*>(JSObjectGetPrivate(object));
131         if (!priv) {
132                 return JSValueMakeUndefined(context);
133         }
134
135         JSContextRef globalContext = priv->getContext();
136         CallServiceObjectPtr privateData = priv->getObject();
137         ICallServicePtr callService(privateData->getICallService());
138
139         try {
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);
145                 }
146         } catch(WrtDeviceApis::Commons::Exception) {
147                 LogWarning("trying to get incorrect value");
148         }
149         return JSValueMakeUndefined(context);
150 }
151
152 bool JSCellularCallService::hasInstance(JSContextRef context, JSObjectRef constructor,
153         JSValueRef possibleInstance, JSValueRef* exception)
154 {
155         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
156 }
157
158 JSValueRef JSCellularCallService::sendUSSD(JSContextRef context, JSObjectRef object,
159                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
160                 JSValueRef* exception)
161 {
162         if (argumentCount < 1 || argumentCount > 3) {
163                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
164         }
165
166         JSCellularCallServicePriv *priv = static_cast<JSCellularCallServicePriv*> (JSObjectGetPrivate(thisObject));
167         JSContextRef gContext = priv->getContext();
168
169         assert(priv && "Invalid private pointer.");
170         Converter converter(context);
171         Validator check(context, exception);
172
173         try {
174                 AceSecurityStatus status = CALL_CHECK_ACCESS(
175                                 gContext,
176                                 CALL_FUNCTION_API_LAUNCHDIALER);
177
178                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
179
180                 if (!JSValueIsString(context, arguments[0])) {
181                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : ussd string");
182                 }
183
184                 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
185
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");
190                                 }
191                                 cbm->setOnError(arguments[2]);
192                         }
193                 }
194
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");
199                                 }
200                                 cbm->setOnSuccess(arguments[1]);
201                         }
202                 }
203
204                 EventSendUSSDPtr event(new EventSendUSSD());
205                 event->setCommand(converter.toString(arguments[0]));
206                 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
207                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
208
209                 CallServiceObjectPtr privateData = priv->getObject();
210                 ICallServicePtr callService(privateData->getICallService());
211
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());
221         }
222
223         return JSValueMakeUndefined(context);
224 }
225
226 }// Tizen1_0
227 }// TizenApis
228