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.
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>
30 #include "plugin_config.h"
32 #include "ApplicationConverter.h"
33 #include "ApplicationUtil.h"
34 #include "JSApplicationService.h"
36 using namespace TizenApis::Api::Application;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace TizenApis::Commons;
43 namespace Application {
45 JSClassRef JSApplicationService::m_jsClassRef = NULL;
47 JSClassDefinition JSApplicationService::m_classInfo = {
49 kJSClassAttributeNone,
50 TIZEN_INTERFACE_APPLICATION_SERVICE,
59 NULL, //DeleteProperty,
60 NULL, //GetPropertyNames,
61 NULL, //CallAsFunction,
62 constructor, //CallAsConstructor,
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 },
75 JSStaticFunction JSApplicationService::m_function[] = {
76 { APPLICATION_FUNCTION_API_REPLY_RESULT, JSApplicationService::replyResult, kJSPropertyAttributeNone },
77 { APPLICATION_FUNCTION_API_REPLY_FAILURE, JSApplicationService::replyFailure,kJSPropertyAttributeNone },
81 const JSClassDefinition* JSApplicationService::getClassInfo()
86 const JSClassRef JSApplicationService::getClassRef()
89 m_jsClassRef = JSClassCreate(&m_classInfo);
95 JSObjectRef JSApplicationService::createJSObject(JSContextRef context, const ApplicationServicePtr &appsvc)
97 LogInfo(">> createJSObject");
98 JSApplicationServicePriv *priv = new JSApplicationServicePriv(context, appsvc);
101 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
104 return JSObjectMake(context, getClassRef(), priv);
108 ApplicationServicePtr JSApplicationService::getApplicationService(JSContextRef context, JSValueRef value)
110 if (!isObjectOfClass(context, value)) {
111 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
114 JSObjectRef object = JSValueToObject(context, value, NULL);
116 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
119 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
121 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
123 return priv->getObject();
126 ApplicationServicePtr JSApplicationService::getPrivateData(JSObjectRef object)
128 JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
130 Throw(WrtDeviceApis::Commons::NullPointerException);
133 return priv->getObject();
137 void JSApplicationService::initialize(JSContextRef context,JSObjectRef object)
139 LogInfo(">> initialize");
142 void JSApplicationService::finalize(JSObjectRef object)
144 LogInfo(">> finalize");
145 JSApplicationServicePriv* priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
146 JSObjectSetPrivate(object, NULL);
147 LogDebug("Deleting ApplicationService object");
151 JSObjectRef JSApplicationService::constructor(JSContextRef context,
152 JSObjectRef constructor,
153 size_t argumentCount,
154 const JSValueRef arguments[],
155 JSValueRef* exception)
157 if (argumentCount == 0) {
158 LogError("Wrong parameters");
159 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
163 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
166 std::string operation = "";
167 std::string uri = "";
168 std::string mime = "";
169 std::vector<ApplicationServiceDataPtr> serviceData;
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]);
176 if ((argumentCount > 2) && !util.isNullOrUndefined(arguments[2])) {
177 mime = converter->toString(arguments[2]);
179 if (argumentCount > 3) {
180 serviceData = converter->toApplicationServiceDataArray(arguments[3]);
183 ApplicationServicePtr appsvc = ApplicationServicePtr(new ApplicationService(operation, uri, mime, serviceData));
185 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknow error on constructor");
189 return createJSObject(context, appsvc);
191 } Catch (WrtDeviceApis::Commons::Exception) {
192 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
199 bool JSApplicationService::isObjectOfClass(JSContextRef context, JSValueRef value)
201 return JSValueIsObjectOfClass(context, value, getClassRef());
204 ApplicationServicePtr JSApplicationService::getPrivData(JSObjectRef object)
207 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
209 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
211 ApplicationServicePtr result = priv->getObject();
213 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
219 JSValueRef JSApplicationService::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
221 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(object));
223 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
227 ApplicationServicePtr appsvc = priv->getObject();
228 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
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());
244 } Catch(WrtDeviceApis::Commons::Exception) {
245 LogError("Exception: " << _rethrown_exception.GetMessage());
246 return JSDOMExceptionFactory::UnknownException.make(context, exception);
249 /* do not return undefined object to find method */
253 bool JSApplicationService::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
256 ApplicationServicePtr privateData = getPrivData(object);
257 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
259 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_OPERATION)) {
260 privateData->setOperation(converter->toString(value));
262 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_URI)) {
263 privateData->setUri(converter->toString(value));
265 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_MIME)) {
266 privateData->setMime(converter->toString(value));
268 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_SERVICE_DATA)) {
269 privateData->setServiceDataArray(converter->toApplicationServiceDataArray(value));
273 } Catch(WrtDeviceApis::Commons::Exception) {
274 LogError("Exception: " << _rethrown_exception.GetMessage());
275 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
282 bool JSApplicationService::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
284 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
287 JSValueRef JSApplicationService::replyResult(JSContextRef context,
289 JSObjectRef thisObject,
290 size_t argumentCount,
291 const JSValueRef arguments[],
292 JSValueRef* exception)
295 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(thisObject));
297 AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_REPLY_RESULT);
298 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
302 ThrowMsg(ConversionException, "Object is null.");
305 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
307 ApplicationServicePtr appsvc = priv->getObject();
308 std::vector<ApplicationServiceDataPtr> resultArray;
309 if (argumentCount > 0) {
310 resultArray = converter->toApplicationServiceDataArray(arguments[0]);
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());
330 return JSValueMakeUndefined(context);
333 JSValueRef JSApplicationService::replyFailure(JSContextRef context,
335 JSObjectRef thisObject,
336 size_t argumentCount,
337 const JSValueRef arguments[],
338 JSValueRef* exception)
341 JSApplicationServicePriv *priv = static_cast<JSApplicationServicePriv*>(JSObjectGetPrivate(thisObject));
343 AceSecurityStatus status = APPLICATION_CHECK_ACCESS(APPLICATION_FUNCTION_API_REPLY_RESULT);
344 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
348 ThrowMsg(ConversionException, "Object is null.");
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());
370 return JSValueMakeUndefined(context);