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