Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSCallManager.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/ICallManager.h>
24 #include <API/Call/CallFactory.h>
25 #include <API/Call/EventGetCallService.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h> 
28 #include <Tizen/Common/SecurityExceptions.h>
29 #include "JSCallManager.h"
30 #include "JSCallHistory.h"
31 #include "Converter.h"
32 #include "plugin_config.h"
33
34 using namespace std;
35 using namespace DPL;
36 using namespace WrtDeviceApis;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Api::Call;
40 using namespace TizenApis::Api::Account;
41 using namespace TizenApis::Commons;
42
43 namespace TizenApis {
44 namespace Tizen1_0 {
45
46 JSClassRef JSCallManager::m_jsClassRef = NULL;
47
48 JSClassDefinition JSCallManager::m_classInfo =
49 {
50         0,
51         kJSClassAttributeNone,
52         "call",
53         NULL,
54         m_property,
55         m_function,
56         initialize,
57         finalize,
58         NULL, //hasProperty,
59         NULL, //getProperty,
60         NULL, //setProperty,
61         NULL, //deleteProperty,
62         NULL, //getPropertyNames,
63         NULL,
64         NULL,
65         hasInstance,
66         NULL
67 };
68
69 JSStaticValue JSCallManager::m_property[] = {
70         { "history", getProperty, NULL, kJSPropertyAttributeReadOnly },
71         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSCallManager::m_function[] =
75 {
76         { "isCallInProgress", JSCallManager::isCallInProgress, kJSPropertyAttributeNone },
77         { "getCallServices", JSCallManager::getCallServices, kJSPropertyAttributeNone },
78         { 0, 0, 0 }
79 };
80
81 const JSClassRef JSCallManager::getClassRef()
82 {
83         if (!m_jsClassRef) {
84                 m_jsClassRef = JSClassCreate(&m_classInfo);
85         }
86         return m_jsClassRef;
87 }
88
89 const JSClassDefinition* JSCallManager::getClassInfo()
90 {
91         return &m_classInfo;
92 }
93
94 void JSCallManager::initialize(JSContextRef context, JSObjectRef object)
95 {
96         JSCallManagerPriv* priv = static_cast<JSCallManagerPriv*>(JSObjectGetPrivate(object));
97         assert(!priv && "Invalid object creation.");
98         ICallManagerPtr Call(CallFactory::getInstance().getCallObject() );
99         priv = new JSCallManagerPriv(context, Call);
100         if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
101                 LogError("Object can't store private data.");
102                 delete priv;
103         }
104
105         LogDebug("JSCallManager::initialize ");
106 }
107
108 void JSCallManager::finalize(JSObjectRef object)
109 {
110         JSCallManagerPriv* priv = static_cast<JSCallManagerPriv*>(JSObjectGetPrivate(object));
111         JSObjectSetPrivate(object, NULL);
112         delete priv;
113 }
114
115 JSValueRef JSCallManager::getProperty(JSContextRef context,
116         JSObjectRef object,
117         JSStringRef propertyName,
118         JSValueRef* exception)
119 {
120         JSCallManagerPriv *priv = static_cast<JSCallManagerPriv*>(JSObjectGetPrivate(object));
121         if (!priv) {
122                 return JSValueMakeUndefined(context);
123         }
124
125         JSContextRef globalContext = priv->getContext();
126         try {
127                 Converter convert(context);
128                 if(JSStringIsEqualToUTF8CString(propertyName, "history")) {
129                         return JSCallHistory::createJSObject(globalContext, object);
130                 }
131         } catch(const WrtDeviceApis::Commons::Exception& ex) {
132                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
133         }
134         return JSValueMakeUndefined(context);
135 }
136
137 bool JSCallManager::hasInstance(JSContextRef context, JSObjectRef constructor,
138         JSValueRef possibleInstance, JSValueRef* exception)
139 {
140         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
141 }
142
143 JSValueRef JSCallManager::isCallInProgress(JSContextRef context, JSObjectRef object,
144                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
145                 JSValueRef* exception)
146 {
147         JSCallManagerPriv *priv = static_cast<JSCallManagerPriv*>(JSObjectGetPrivate(thisObject));
148
149         JSContextRef gContext = priv->getContext();
150         Converter converter(context);
151
152         if (priv == NULL) {
153                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "private object is null");
154         }
155
156         try     {
157                 AceSecurityStatus status = CALL_CHECK_ACCESS(
158                                 gContext,
159                                 CALL_FUNCTION_API_ISCALLINPROGRESS);
160
161                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
162
163                 ICallManagerPtr callManager(priv->getObject());
164                 bool result = callManager->isCallInProgress();
165                 return converter.toJSValueRef(result);
166         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
167                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
168         } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
169                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
170         } catch(const WrtDeviceApis::Commons::Exception& ex) {
171                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
172         }
173
174         return JSValueMakeUndefined(context);
175 }
176
177 JSValueRef JSCallManager::getCallServices(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
178         const JSValueRef arguments[], JSValueRef* exception)
179 {
180         if (argumentCount > 1) {
181                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
182         }
183
184         JSCallManagerPriv *priv = static_cast<JSCallManagerPriv*> (JSObjectGetPrivate(thisObject));
185
186         assert(priv && "Invalid private pointer.");
187         JSContextRef gContext = priv->getContext();
188         Converter converter(context);
189
190         try{
191                 AceSecurityStatus status = CALL_CHECK_ACCESS(
192                                 gContext,
193                                 CALL_FUNCTION_API_GETCALLSERVICES);
194
195                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
196
197                 CallServiceFilterPtr filter(new CallServiceFilter());
198
199                 if (argumentCount == 0) {
200                         StringArrayPtr tags(new StringArray());
201                         tags->push_back("call");
202                         filter->setTags(tags);
203                 } else {
204                         if (!JSValueIsObject(context, arguments[0])) {
205                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : CallServiceFilter");
206                         }
207                         filter = converter.toCallServiceFilter(arguments[0]);
208                 }
209
210                 EventGetCallServicePtr event(new EventGetCallService());
211
212                 event->setForSynchronousCall();
213                 event->setFilter(filter);
214
215                 ICallManagerPtr callManager(priv->getObject());
216                 callManager->getCallService(event);
217
218                 if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::None) {
219                         return converter.toJSValueRef(event->getResult(), gContext);
220                 } else {
221                         if (event->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::UnsupportedException) {
222                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, "Not supported value");
223                         } else {
224                                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
225                         }
226                 }
227         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
228                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
229         } catch(const WrtDeviceApis::Commons::UnsupportedException& ex) {
230                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
231         } catch(const WrtDeviceApis::Commons::PlatformException& ex) {
232                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
233         } catch(const WrtDeviceApis::Commons::Exception& ex) {
234                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
235         }
236
237         return JSValueMakeUndefined(context);
238 }
239
240 }
241 }
242