2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
29 #include "plugin_config.h"
31 #include "ApplicationConverter.h"
32 #include "ApplicationUtil.h"
33 #include "JSApplicationService.h"
35 using namespace TizenApis::Api::Application;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace TizenApis::Commons;
42 namespace Application {
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";
51 JSClassRef JSApplicationService::m_jsClassRef = NULL;
53 JSClassDefinition JSApplicationService::m_classInfo = {
55 kJSClassAttributeNone,
65 NULL, //DeleteProperty,
66 NULL, //GetPropertyNames,
67 NULL, //CallAsFunction,
68 constructor, //CallAsConstructor,
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 },
81 JSStaticFunction JSApplicationService::m_function[] = {
82 { "replyResult",JSApplicationService::replyResult,kJSPropertyAttributeNone },
83 { "replyFailure",JSApplicationService::replyFailure,kJSPropertyAttributeNone },
87 const JSClassDefinition* JSApplicationService::getClassInfo()
92 const JSClassRef JSApplicationService::getClassRef()
95 m_jsClassRef = JSClassCreate(&m_classInfo);
101 JSObjectRef JSApplicationService::createJSObject(JSContextRef context, const ApplicationServicePtr &appsvc)
103 LogInfo(">> createJSObject");
104 JSApplicationServicePriv *priv = new JSApplicationServicePriv(context, appsvc);
107 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
110 return JSObjectMake(context, getClassRef(), priv);
114 ApplicationServicePtr JSApplicationService::getApplicationService(JSContextRef context, JSValueRef value)
116 if (!isObjectOfClass(context, value)) {
117 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
120 JSObjectRef object = JSValueToObject(context, value, NULL);
122 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
125 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
127 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
129 return priv->getObject();
132 ApplicationServicePtr JSApplicationService::getPrivateData(JSObjectRef object)
134 JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
136 Throw(WrtDeviceApis::Commons::NullPointerException);
139 return priv->getObject();
143 void JSApplicationService::initialize(JSContextRef context,JSObjectRef object)
145 LogInfo(">> initialize");
148 void JSApplicationService::finalize(JSObjectRef object)
150 LogInfo(">> finalize");
151 JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
152 JSObjectSetPrivate(object, NULL);
153 LogDebug("Deleting ApplicationService object");
157 JSObjectRef JSApplicationService::constructor(JSContextRef context,
158 JSObjectRef constructor,
159 size_t argumentCount,
160 const JSValueRef arguments[],
161 JSValueRef* exception)
163 if (argumentCount == 0) {
164 LogError("Wrong parameters");
165 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
169 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
172 std::string operation = "";
173 std::string uri = "";
174 std::string mime = "";
175 std::vector<ApplicationServiceDataPtr> serviceData;
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]);
182 if ((argumentCount > 2) && !util.isNullOrUndefined(arguments[2])) {
183 mime = converter->toString(arguments[2]);
185 if (argumentCount > 3) {
186 serviceData = converter->toApplicationServiceDataArray(arguments[3]);
189 ApplicationServicePtr appsvc = ApplicationServicePtr(new ApplicationService(operation, uri, mime, serviceData));
191 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknow error on constructor");
195 return createJSObject(context, appsvc);
197 } Catch (WrtDeviceApis::Commons::Exception) {
198 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
205 bool JSApplicationService::isObjectOfClass(JSContextRef context, JSValueRef value)
207 return JSValueIsObjectOfClass(context, value, getClassRef());
210 ApplicationServicePtr JSApplicationService::getPrivData(JSObjectRef object)
213 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
215 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
217 ApplicationServicePtr result = priv->getObject();
219 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
225 JSValueRef JSApplicationService::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
227 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
229 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
233 ApplicationServicePtr appsvc = priv->getObject();
234 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
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());
250 } Catch(WrtDeviceApis::Commons::Exception) {
251 LogError("Exception: " << _rethrown_exception.GetMessage());
252 return JSDOMExceptionFactory::UnknownException.make(context, exception);
255 /* do not return undefined object to find method */
259 bool JSApplicationService::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
262 ApplicationServicePtr privateData = getPrivData(object);
263 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
265 if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_OPERATION)) {
266 privateData->setOperation(converter->toString(value));
268 } else if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_URI)) {
269 privateData->setUri(converter->toString(value));
271 } else if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_MIME)) {
272 privateData->setMime(converter->toString(value));
274 } else if (JSStringIsEqualToUTF8CString(propertyName, APPLICATION_SERVICE_DATA)) {
275 privateData->setServiceDataArray(converter->toApplicationServiceDataArray(value));
279 } Catch(WrtDeviceApis::Commons::Exception) {
280 LogError("Exception: " << _rethrown_exception.GetMessage());
281 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
288 bool JSApplicationService::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
290 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
293 JSValueRef JSApplicationService::replyResult(JSContextRef context,
295 JSObjectRef thisObject,
296 size_t argumentCount,
297 const JSValueRef arguments[],
298 JSValueRef* exception)
300 LogInfo(">> replyResult()");
301 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(thisObject));
303 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
306 AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_REPLY_RESULT);
307 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
309 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
312 ApplicationServicePtr appsvc = priv->getObject();
313 std::vector<ApplicationServiceDataPtr> resultArray;
314 if (argumentCount > 0) {
315 resultArray = converter->toApplicationServiceDataArray(arguments[0]);
317 appsvc->replyResult(resultArray);
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");
332 return JSValueMakeUndefined(context);
335 JSValueRef JSApplicationService::replyFailure(JSContextRef context,
337 JSObjectRef thisObject,
338 size_t argumentCount,
339 const JSValueRef arguments[],
340 JSValueRef* exception)
342 LogInfo(">> replyFailure()");
343 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(thisObject));
345 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
348 AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_REPLY_RESULT);
349 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
352 ApplicationServicePtr appsvc = priv->getObject();
353 appsvc->replyFailure();
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");
368 return JSValueMakeUndefined(context);