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.
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
22 #include <CommonsJavaScript/PrivateObject.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <SecurityExceptions.h>
25 #include <Commons/Exception.h>
26 #include <JSTizenExceptionFactory.h>
27 #include <JSTizenException.h>
29 #include "plugin_config.h"
30 #include "ApplicationConverter.h"
31 #include "ApplicationUtil.h"
32 #include "JSRequestedApplicationControl.h"
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37 using namespace DeviceAPI::Common;
40 namespace Application {
42 JSClassRef JSRequestedApplicationControl::m_jsClassRef = NULL;
44 JSClassDefinition JSRequestedApplicationControl::m_classInfo = {
46 kJSClassAttributeNone,
47 TIZEN_INTERFACE_REQUESTED_APPLICATION_CONTROL,
56 NULL, //DeleteProperty,
57 NULL, //GetPropertyNames,
58 NULL, //CallAsFunction,
59 NULL, //CallAsConstructor,
64 JSStaticValue JSRequestedApplicationControl::m_property[] = {
65 { TIZEN_APPLICATION_CONTROL, getProperty, NULL, kJSPropertyAttributeNone },
66 { TIZEN_CALLER_APP_ID, getProperty, NULL, kJSPropertyAttributeNone },
70 JSStaticFunction JSRequestedApplicationControl::m_function[] = {
71 { APPLICATION_FUNCTION_API_REPLY_RESULT, JSRequestedApplicationControl::replyResult, kJSPropertyAttributeNone },
72 { APPLICATION_FUNCTION_API_REPLY_FAILURE, JSRequestedApplicationControl::replyFailure,kJSPropertyAttributeNone },
76 const JSClassDefinition* JSRequestedApplicationControl::getClassInfo()
81 JSClassRef JSRequestedApplicationControl::getClassRef()
84 m_jsClassRef = JSClassCreate(&m_classInfo);
90 JSObjectRef JSRequestedApplicationControl::createJSObject(JSContextRef context, const RequestedApplicationControlPtr &appsvc)
92 LoggerI(">> createJSObject");
93 JSRequestedApplicationControlPriv *priv = new JSRequestedApplicationControlPriv(context, appsvc);
96 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
99 return JSObjectMake(context, getClassRef(), priv);
104 void JSRequestedApplicationControl::initialize(JSContextRef context,JSObjectRef object)
106 LoggerI(">> initialize");
109 void JSRequestedApplicationControl::finalize(JSObjectRef object)
111 LoggerI(">> finalize");
112 JSRequestedApplicationControlPriv* priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
113 JSObjectSetPrivate(object, NULL);
114 LoggerD("Deleting ApplicationControl object");
119 bool JSRequestedApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
121 return JSValueIsObjectOfClass(context, value, getClassRef());
124 RequestedApplicationControlPtr JSRequestedApplicationControl::getRequestedApplicationControl(JSContextRef context, JSValueRef value)
126 if (!isObjectOfClass(context, value)) {
127 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
130 JSObjectRef object = JSValueToObject(context, value, NULL);
132 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
135 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
137 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
139 return priv->getObject();
143 RequestedApplicationControlPtr JSRequestedApplicationControl::getPrivData(JSObjectRef object)
146 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
148 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
150 RequestedApplicationControlPtr result = priv->getObject();
152 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157 JSValueRef JSRequestedApplicationControl::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
159 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
161 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
165 RequestedApplicationControlPtr providerMgr = priv->getObject();
166 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
168 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL)) {
169 LoggerD("JSRequestedApplicationControl::getProperty::appControl " << providerMgr->getAppControl());
170 return converter->toJSValueRef(providerMgr->getAppControl());
171 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALLER_APP_ID)) {
172 LoggerD("JSRequestedApplicationControl::getProperty::callerAppId " << providerMgr->getCallerAppId());
173 return converter->toJSValueRef(providerMgr->getCallerAppId());
175 } Catch(WrtDeviceApis::Commons::Exception) {
176 LoggerE("Exception: " << _rethrown_exception.GetMessage());
177 return JSDOMExceptionFactory::UnknownException.make(context, exception);
180 /* do not return undefined object to find method */
185 bool JSRequestedApplicationControl::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
187 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
190 JSValueRef JSRequestedApplicationControl::replyResult(JSContextRef context,
192 JSObjectRef thisObject,
193 size_t argumentCount,
194 const JSValueRef arguments[],
195 JSValueRef* exception)
198 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
202 ThrowMsg(ConversionException, "Object is null.");
205 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
207 RequestedApplicationControlPtr providerMgr = priv->getObject();
208 std::vector<ApplicationControlDataPtr> resultArray;
209 if (argumentCount > 0) {
210 resultArray = converter->toApplicationControlDataArray(arguments[0]);
212 providerMgr->replyResult(resultArray);
213 } Catch (ConversionException) {
214 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
215 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
216 } Catch (NotFoundException) {
217 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
218 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
219 } Catch (NullPointerException) {
220 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
221 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
222 } Catch (Exception) {
223 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
224 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
227 return JSValueMakeUndefined(context);
230 JSValueRef JSRequestedApplicationControl::replyFailure(JSContextRef context,
232 JSObjectRef thisObject,
233 size_t argumentCount,
234 const JSValueRef arguments[],
235 JSValueRef* exception)
238 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
242 ThrowMsg(ConversionException, "Object is null.");
245 RequestedApplicationControlPtr providerMgr = priv->getObject();
246 providerMgr->replyFailure();
247 } Catch (NotFoundException) {
248 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
249 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::NOT_FOUND_ERROR, _rethrown_exception.GetMessage());
250 } Catch (NullPointerException) {
251 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
252 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
253 } Catch (Exception) {
254 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
255 return JSTizenExceptionFactory::postException(context, exception,JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
258 return JSValueMakeUndefined(context);