Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSCallService.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/ICallService.h>
25 #include <API/Call/EventLaunchDialer.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h> 
28 #include <Tizen/Common/SecurityExceptions.h>
29 #include <Tizen/Account/JSAccountServices.h>
30 #include "JSCallService.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 JSCallService::m_jsClassRef = NULL;
48
49 JSClassDefinition JSCallService::m_classInfo =
50 {
51         0,
52         kJSClassAttributeNone,
53         "CallService",
54         JSAccountServices::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 JSCallService::m_property[] = {
71         { "voicemailNumbers", getProperty, NULL, kJSPropertyAttributeReadOnly },
72         { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSCallService::m_function[] =
76 {
77         { "launchDialer", JSCallService::launchDialer, kJSPropertyAttributeNone },
78         { 0, 0, 0 }
79 };
80
81 const JSClassRef JSCallService::getClassRef()
82 {
83         if (!m_jsClassRef) {
84                 m_jsClassRef = JSClassCreate(&m_classInfo);
85         }
86         return m_jsClassRef;
87 }
88
89 const JSClassDefinition* JSCallService::getClassInfo()
90 {
91         return &m_classInfo;
92 }
93
94 JSObjectRef JSCallService::createJSObject(JSContextRef context, const AccountServices &serviceInfo)
95 {
96         ICallServicePtr callService(CallFactory::getInstance().getCallServiceObject());
97         CallServiceObjectPtr privateData = CallServiceObjectPtr(new CallServiceObject(serviceInfo, callService));
98
99         JSCallServicePriv *priv = new JSCallServicePriv(context, privateData);
100
101         if (!priv) {
102                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
103         }
104
105         JSObjectRef jsObjRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
106         if (NULL == jsObjRef) {
107                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
108         }
109         return jsObjRef;
110 }
111
112 void JSCallService::initialize(JSContextRef context, JSObjectRef object)
113 {
114         LogDebug("JSCallService::initialize ");
115 }
116
117 void JSCallService::finalize(JSObjectRef object)
118 {
119         JSCallServicePriv* priv = static_cast<JSCallServicePriv*>(JSObjectGetPrivate(object));
120         JSObjectSetPrivate(object, NULL);
121         delete priv;
122 }
123
124 JSValueRef JSCallService::getProperty(JSContextRef context,
125         JSObjectRef object,
126         JSStringRef propertyName,
127         JSValueRef* exception)
128 {
129         JSCallServicePriv *priv = static_cast<JSCallServicePriv*>(JSObjectGetPrivate(object));
130         if (!priv) {
131                 return JSValueMakeUndefined(context);
132         }
133
134         JSContextRef globalContext = priv->getContext();
135         CallServiceObjectPtr privateData = priv->getObject();
136         ICallServicePtr callService(privateData->getICallService());
137
138         try {
139                 Converter convert(context);
140                 if(JSStringIsEqualToUTF8CString(propertyName, "voicemailNumbers")) {
141                         return convert.toJSValueRef(callService->getVoicemailNumbers(), globalContext);
142                 }
143         } catch(WrtDeviceApis::Commons::Exception) {
144                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "trying to get incorrect value");
145         }
146         return JSValueMakeUndefined(context);
147 }
148
149 bool JSCallService::hasInstance(JSContextRef context, JSObjectRef constructor,
150         JSValueRef possibleInstance, JSValueRef* exception)
151 {
152         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
153 }
154
155 JSValueRef JSCallService::launchDialer(JSContextRef context, JSObjectRef object,
156                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
157                 JSValueRef* exception)
158 {
159         if (argumentCount < 1 || argumentCount > 4) {
160                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
161         }
162         
163         JSCallServicePriv *priv = static_cast<JSCallServicePriv*>(JSObjectGetPrivate(thisObject));
164         JSContextRef gContext = priv->getContext();
165
166         assert(priv && "Invalid private pointer.");
167         Converter converter(context);
168         Validator check(context, exception);
169
170         try     {
171                 AceSecurityStatus status = CALL_CHECK_ACCESS(
172                                 gContext,
173                                 CALL_FUNCTION_API_LAUNCHDIALER);
174
175                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
176                 
177                 if (!JSValueIsString(context, arguments[0])) {
178                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : remoteparty string");
179                 }
180
181                 JSCallbackManagerPtr cbm(JSCallbackManager::createObject(gContext));
182
183                 if (argumentCount >= 3) {
184                         if (!JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2])) {
185                                 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[2]))) {
186                                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : error callback");
187                                 }
188                                 cbm->setOnError(arguments[2]);
189                         }
190                 }
191
192                 if (argumentCount >= 2) {
193                         if (!JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
194                                 if (!JSObjectIsFunction(context, converter.toJSObjectRef(arguments[1]))) {
195                                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : success callback");
196                                 }
197                                 cbm->setOnSuccess(arguments[1]);
198                         }
199                 }
200
201                 if (argumentCount >= 4) {
202                         if (!JSValueIsString(context, arguments[3])) {
203                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : extension string");
204                         }
205                 }
206
207                 CallServiceObjectPtr privateData = priv->getObject();
208                 ICallServicePtr callService(privateData->getICallService());
209
210                 EventLaunchDialerPtr event(new EventLaunchDialer());
211                 event ->setRemoteParty(converter.toString(arguments[0]));
212                 if (argumentCount >= 4) {
213                         event ->setExtension(converter.toString(arguments[3]));
214                 }
215                 event->setPrivateData(StaticPointerCast<IEventPrivateData> (cbm));
216                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
217
218                 callService->launchDialer(event);
219
220         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
221                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
222         } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
223                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
224         } catch(const WrtDeviceApis::Commons::Exception& ex) {
225                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
226         }
227         return JSValueMakeUndefined(context);
228 }
229
230 }
231 }
232