Add DRAFT stubs for Vehicle plugin
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / JSApplicationService.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 <cassert>
19 #include <memory>
20 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <Tizen/Common/SecurityExceptions.h>
26 #include <Commons/Exception.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29
30 #include "plugin_config.h"
31
32 #include "ApplicationConverter.h"
33 #include "ApplicationUtil.h"
34 #include "JSApplicationService.h"
35
36 using namespace TizenApis::Api::Application;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
40
41 namespace TizenApis {
42 namespace Tizen1_0 {
43 namespace Application {
44
45 JSClassRef JSApplicationService::m_jsClassRef = NULL;
46
47 JSClassDefinition JSApplicationService::m_classInfo = {
48     0,
49     kJSClassAttributeNone,
50     TIZEN_INTERFACE_APPLICATION_SERVICE,
51     0,
52     m_property,
53     m_function,
54     initialize,
55     finalize,
56     NULL,     //HasProperty,
57     getProperty,
58     NULL,     //SetProperty,
59     NULL,     //DeleteProperty,
60     NULL,     //GetPropertyNames,
61     NULL,     //CallAsFunction,
62     constructor,     //CallAsConstructor,
63     hasInstance,
64     NULL,     //ConvertToType
65 };
66
67 JSStaticValue JSApplicationService::m_property[] = {
68     { TIZEN_APPLICATION_SERVICE_OPERATION, getProperty, NULL, kJSPropertyAttributeNone },
69     { TIZEN_APPLICATION_SERVICE_URI, getProperty, NULL, kJSPropertyAttributeNone },
70     { TIZEN_APPLICATION_SERVICE_MIME, getProperty, NULL, kJSPropertyAttributeNone },
71     { TIZEN_APPLICATION_SERVICE_DATA, getProperty, NULL, kJSPropertyAttributeNone },
72     { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSApplicationService::m_function[] = {
76                 { APPLICATION_FUNCTION_API_REPLY_RESULT, JSApplicationService::replyResult, kJSPropertyAttributeNone },
77                 { APPLICATION_FUNCTION_API_REPLY_FAILURE, JSApplicationService::replyFailure,kJSPropertyAttributeNone },
78                 { 0, 0, 0 }
79 };
80
81 const JSClassDefinition* JSApplicationService::getClassInfo()
82 {
83     return &m_classInfo;
84 }
85
86 const JSClassRef JSApplicationService::getClassRef()
87 {
88     if (!m_jsClassRef) {
89         m_jsClassRef = JSClassCreate(&m_classInfo);
90     }
91         
92     return m_jsClassRef;
93 }
94
95 JSObjectRef JSApplicationService::createJSObject(JSContextRef context, const ApplicationServicePtr &appsvc)
96 {
97          LogInfo(">> createJSObject");
98     JSApplicationServicePriv *priv = new JSApplicationServicePriv(context, appsvc);
99
100     if (!priv) {
101         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
102     }
103         
104     return JSObjectMake(context, getClassRef(), priv);
105 }
106
107
108 ApplicationServicePtr JSApplicationService::getApplicationService(JSContextRef context, JSValueRef value)
109 {
110         if (!isObjectOfClass(context, value)) {
111                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
112         }
113         
114         JSObjectRef object = JSValueToObject(context, value, NULL);
115         if (!object) {
116                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
117         }
118         
119         JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
120         if (!priv) {
121                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
122         }
123         return priv->getObject();
124 }
125
126 ApplicationServicePtr JSApplicationService::getPrivateData(JSObjectRef object)
127 {
128         JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
129         if (!priv) {
130                 Throw(WrtDeviceApis::Commons::NullPointerException);
131         }
132         
133         return priv->getObject();
134 }
135
136
137 void JSApplicationService::initialize(JSContextRef context,JSObjectRef object)
138 {
139     LogInfo(">> initialize");
140 }
141
142 void JSApplicationService::finalize(JSObjectRef object)
143 {
144     LogInfo(">> finalize");
145     JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
146     JSObjectSetPrivate(object, NULL);
147     LogDebug("Deleting ApplicationService object");
148     delete priv;
149 }
150
151 JSObjectRef JSApplicationService::constructor(JSContextRef context, 
152         JSObjectRef constructor, 
153         size_t argumentCount, 
154         const JSValueRef arguments[], 
155         JSValueRef* exception)
156 {
157         if (argumentCount == 0) {
158                 LogError("Wrong parameters");
159                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
160                 return NULL;
161         }
162
163         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
164
165         Try {
166                 std::string operation = "";
167                 std::string uri = "";
168                 std::string mime = "";
169                 std::vector<ApplicationServiceDataPtr> serviceData;
170
171                 operation = converter->toString(arguments[0]);
172                 ApplicationUtil util(context, exception);
173                 if ((argumentCount > 1) && !util.isNullOrUndefined(arguments[1])) {
174                         uri = converter->toString(arguments[1]);
175                 }
176                 if ((argumentCount > 2) && !util.isNullOrUndefined(arguments[2])) {
177                         mime = converter->toString(arguments[2]);
178                 }
179                 if (argumentCount > 3) {
180                         serviceData = converter->toApplicationServiceDataArray(arguments[3]);
181                 }
182
183                 ApplicationServicePtr appsvc = ApplicationServicePtr(new ApplicationService(operation, uri, mime, serviceData));
184                 if (!appsvc) {
185                         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknow error on constructor");
186                         return NULL;
187                 }
188         
189                 return createJSObject(context, appsvc);
190                 
191         } Catch (WrtDeviceApis::Commons::Exception) {
192                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
193                 return NULL;
194                 
195         }
196 }
197         
198
199 bool JSApplicationService::isObjectOfClass(JSContextRef context, JSValueRef value)
200 {
201         return JSValueIsObjectOfClass(context, value, getClassRef());
202 }
203
204 ApplicationServicePtr JSApplicationService::getPrivData(JSObjectRef object)
205 {
206         LogDebug("entered");
207         JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
208         if (!priv) {
209                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
210         }
211         ApplicationServicePtr result = priv->getObject();
212         if (!result) {
213                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
214         }
215         return result;
216 }
217
218
219 JSValueRef JSApplicationService::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
220 {
221     JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
222     if (!priv) {
223                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
224         }
225
226     Try {
227         ApplicationServicePtr appsvc = priv->getObject();
228                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
229
230         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_OPERATION)) {
231             LogDebug("JSApplicationService::getProperty::operation " << appsvc->getOperation());
232             return converter->toJSValueRef(appsvc->getOperation());
233         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_URI)) {
234             LogDebug("JSApplicationService::getProperty::uri " << appsvc->getUri());
235             return converter->toJSValueRef(appsvc->getUri());
236         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_MIME)) {
237             LogDebug("JSApplicationService::getProperty::mime " << appsvc->getMime());
238             return converter->toJSValueRef(appsvc->getMime());
239                 }else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_DATA)) {
240                         LogDebug("JSApplicationService::getProperty::extraData ");
241                         return converter->toJSValueRef(appsvc->getServiceDataArray());
242                 }
243
244     } Catch(WrtDeviceApis::Commons::Exception) {
245         LogError("Exception: " << _rethrown_exception.GetMessage());
246                 return JSDOMExceptionFactory::UnknownException.make(context, exception);
247     }
248
249         /* do not return undefined object to find method */
250     return NULL;
251 }
252
253 bool JSApplicationService::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
254 {
255         Try     {
256                 ApplicationServicePtr privateData = getPrivData(object);
257                 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
258         
259                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_OPERATION)) {
260                         privateData->setOperation(converter->toString(value));
261                         return true;
262                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_URI)) {
263                         privateData->setUri(converter->toString(value));
264                         return true;
265                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_MIME)) {
266                         privateData->setMime(converter->toString(value));
267                         return true;
268                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_DATA)) {
269                         privateData->setServiceDataArray(converter->toApplicationServiceDataArray(value));
270                         return true;
271                 } 
272
273         } Catch(WrtDeviceApis::Commons::Exception) {
274                 LogError("Exception: " << _rethrown_exception.GetMessage());
275         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
276     }
277         
278         return false;
279 }
280
281
282 bool JSApplicationService::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
283 {
284     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
285 }
286
287 JSValueRef JSApplicationService::replyResult(JSContextRef context, 
288         JSObjectRef object, 
289         JSObjectRef thisObject, 
290         size_t argumentCount,
291         const JSValueRef arguments[], 
292         JSValueRef* exception) 
293 {
294         LogDebug("entered");
295         JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(thisObject));
296
297         AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_REPLY_RESULT);
298         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
299
300         Try {
301         if (!priv) {
302             ThrowMsg(ConversionException, "Object is null.");
303         }
304
305         ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
306
307                 ApplicationServicePtr appsvc = priv->getObject();
308                 std::vector<ApplicationServiceDataPtr> resultArray;
309                 if (argumentCount > 0) {
310                         resultArray = converter->toApplicationServiceDataArray(arguments[0]);
311                 }
312                 appsvc->replyResult(resultArray);
313         } Catch (ConversionException) {
314         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
315                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
316         } Catch (UnsupportedException) {
317             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
318                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
319         } Catch (InvalidArgumentException) {
320             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
321                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
322         } Catch (NullPointerException) {
323             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
324                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
325         } Catch (Exception) {
326         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
327                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
328         }
329
330         return JSValueMakeUndefined(context);
331 }
332
333 JSValueRef JSApplicationService::replyFailure(JSContextRef context, 
334         JSObjectRef object, 
335         JSObjectRef thisObject, 
336         size_t argumentCount,
337         const JSValueRef arguments[], 
338         JSValueRef* exception) 
339 {
340         LogDebug("entered");
341         JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(thisObject));
342
343         AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_REPLY_RESULT);
344         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
345
346         Try {
347         if (!priv) {
348             ThrowMsg(ConversionException, "Object is null.");
349         }
350
351                 ApplicationServicePtr appsvc = priv->getObject();
352                 appsvc->replyFailure();
353         } Catch (ConversionException) {
354         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
355                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
356         } Catch (UnsupportedException) {
357             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
358                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage()); 
359         } Catch (InvalidArgumentException) {
360             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
361                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
362         } Catch (NullPointerException) {
363             LogWarning("Exception: "<<_rethrown_exception.GetMessage());
364                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
365         } Catch (Exception) {
366         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
367                 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
368         }
369         
370         return JSValueMakeUndefined(context);
371 }
372
373 }
374 }
375 }