2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
21 #include <dpl/log/log.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <CommonsJavaScript/PrivateObject.h>
25 #include <CommonsJavaScript/JSUtils.h>
26 #include <SecurityExceptions.h>
27 #include <Commons/Exception.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
31 #include "plugin_config.h"
33 #include "ApplicationConverter.h"
34 #include "ApplicationUtil.h"
35 #include "JSRequestedApplicationControl.h"
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace DeviceAPI::Common;
42 namespace Application {
44 JSClassRef JSRequestedApplicationControl::m_jsClassRef = NULL;
46 JSClassDefinition JSRequestedApplicationControl::m_classInfo = {
48 kJSClassAttributeNone,
49 TIZEN_INTERFACE_REQUESTED_APPLICATION_CONTROL,
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
66 JSStaticValue JSRequestedApplicationControl::m_property[] = {
67 { TIZEN_APPLICATION_CONTROL, getProperty, NULL, kJSPropertyAttributeNone },
68 { TIZEN_CALLER_APP_ID, getProperty, NULL, kJSPropertyAttributeNone },
72 JSStaticFunction JSRequestedApplicationControl::m_function[] = {
73 { APPLICATION_FUNCTION_API_REPLY_RESULT, JSRequestedApplicationControl::replyResult, kJSPropertyAttributeNone },
74 { APPLICATION_FUNCTION_API_REPLY_FAILURE, JSRequestedApplicationControl::replyFailure,kJSPropertyAttributeNone },
78 const JSClassDefinition* JSRequestedApplicationControl::getClassInfo()
83 JSClassRef JSRequestedApplicationControl::getClassRef()
86 m_jsClassRef = JSClassCreate(&m_classInfo);
92 JSObjectRef JSRequestedApplicationControl::createJSObject(JSContextRef context, const RequestedApplicationControlPtr &appsvc)
94 LogInfo(">> createJSObject");
95 JSRequestedApplicationControlPriv *priv = new JSRequestedApplicationControlPriv(context, appsvc);
98 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
101 return JSObjectMake(context, getClassRef(), priv);
106 void JSRequestedApplicationControl::initialize(JSContextRef context,JSObjectRef object)
108 LogInfo(">> initialize");
111 void JSRequestedApplicationControl::finalize(JSObjectRef object)
113 LogInfo(">> finalize");
114 JSRequestedApplicationControlPriv* priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
115 JSObjectSetPrivate(object, NULL);
116 LogDebug("Deleting ApplicationControl object");
121 bool JSRequestedApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
123 return JSValueIsObjectOfClass(context, value, getClassRef());
126 RequestedApplicationControlPtr JSRequestedApplicationControl::getRequestedApplicationControl(JSContextRef context, JSValueRef value)
128 if (!isObjectOfClass(context, value)) {
129 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
132 JSObjectRef object = JSValueToObject(context, value, NULL);
134 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
137 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
139 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
141 return priv->getObject();
145 RequestedApplicationControlPtr JSRequestedApplicationControl::getPrivData(JSObjectRef object)
148 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
150 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152 RequestedApplicationControlPtr result = priv->getObject();
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159 JSValueRef JSRequestedApplicationControl::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
161 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
163 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
167 RequestedApplicationControlPtr providerMgr = priv->getObject();
168 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
170 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL)) {
171 LogDebug("JSRequestedApplicationControl::getProperty::appControl " << providerMgr->getAppControl());
172 return converter->toJSValueRef(providerMgr->getAppControl());
173 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALLER_APP_ID)) {
174 LogDebug("JSRequestedApplicationControl::getProperty::callerAppId " << providerMgr->getCallerAppId());
175 return converter->toJSValueRef(providerMgr->getCallerAppId());
177 } Catch(WrtDeviceApis::Commons::Exception) {
178 LogError("Exception: " << _rethrown_exception.GetMessage());
179 return JSDOMExceptionFactory::UnknownException.make(context, exception);
182 /* do not return undefined object to find method */
187 bool JSRequestedApplicationControl::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
189 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
192 JSValueRef JSRequestedApplicationControl::replyResult(JSContextRef context,
194 JSObjectRef thisObject,
195 size_t argumentCount,
196 const JSValueRef arguments[],
197 JSValueRef* exception)
200 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
204 ThrowMsg(ConversionException, "Object is null.");
207 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
209 RequestedApplicationControlPtr providerMgr = priv->getObject();
210 std::vector<ApplicationControlDataPtr> resultArray;
211 if (argumentCount > 0) {
212 resultArray = converter->toApplicationControlDataArray(arguments[0]);
214 providerMgr->replyResult(resultArray);
215 } Catch (ConversionException) {
216 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
217 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
218 } Catch (NotFoundException) {
219 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
220 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
221 } Catch (NullPointerException) {
222 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
223 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
224 } Catch (Exception) {
225 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
226 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
229 return JSValueMakeUndefined(context);
232 JSValueRef JSRequestedApplicationControl::replyFailure(JSContextRef context,
234 JSObjectRef thisObject,
235 size_t argumentCount,
236 const JSValueRef arguments[],
237 JSValueRef* exception)
240 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
244 ThrowMsg(ConversionException, "Object is null.");
247 RequestedApplicationControlPtr providerMgr = priv->getObject();
248 providerMgr->replyFailure();
249 } Catch (NotFoundException) {
250 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
251 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
252 } Catch (NullPointerException) {
253 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
254 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
255 } Catch (Exception) {
256 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
257 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
260 return JSValueMakeUndefined(context);