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