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 <SecurityExceptions.h>
22 #include <JSWebAPIError.h>
23 #include <ArgumentValidator.h>
25 #include "plugin_config.h"
26 #include "ApplicationConverter.h"
27 #include "ApplicationUtil.h"
28 #include "JSRequestedApplicationControl.h"
31 #include <TimeTracer.h>
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37 using namespace DeviceAPI::Common;
40 namespace Application {
42 using WrtDeviceApis::Commons::NotFoundException;
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 DLL_EXPORT JSRequestedApplicationControl::getClassRef()
86 m_jsClassRef = JSClassCreate(&m_classInfo);
92 JSObjectRef JSRequestedApplicationControl::createJSObject(JSContextRef context, const RequestedApplicationControlPtr &appsvc)
94 JSRequestedApplicationControlPriv *priv = new JSRequestedApplicationControlPriv(context, appsvc);
97 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
100 return JSObjectMake(context, getClassRef(), priv);
105 void JSRequestedApplicationControl::initialize(JSContextRef context,JSObjectRef object)
109 void JSRequestedApplicationControl::finalize(JSObjectRef object)
111 JSRequestedApplicationControlPriv* priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
112 JSObjectSetPrivate(object, NULL);
117 bool JSRequestedApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
119 return JSValueIsObjectOfClass(context, value, getClassRef());
122 RequestedApplicationControlPtr JSRequestedApplicationControl::getRequestedApplicationControl(JSContextRef context, JSValueRef value)
124 if (!isObjectOfClass(context, value)) {
125 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
128 JSObjectRef object = JSValueToObject(context, value, NULL);
130 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
133 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
135 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
137 return priv->getObject();
141 RequestedApplicationControlPtr JSRequestedApplicationControl::getPrivData(JSObjectRef object)
143 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
145 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147 RequestedApplicationControlPtr result = priv->getObject();
149 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154 JSValueRef JSRequestedApplicationControl::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
156 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(object));
158 LoggerE("Exception occured while get property");
159 return JSValueMakeUndefined(context);
163 RequestedApplicationControlPtr providerMgr = priv->getObject();
164 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
166 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL)) {
167 return converter->toJSValueRef(providerMgr->getAppControl());
168 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALLER_APP_ID)) {
169 return converter->toJSValueRef(providerMgr->getCallerAppId());
172 LoggerE("Exception occured while get property");
173 return JSValueMakeUndefined(context);
176 /* do not return undefined object to find method */
181 JSValueRef JSRequestedApplicationControl::replyResult(JSContextRef context,
183 JSObjectRef thisObject,
184 size_t argumentCount,
185 const JSValueRef arguments[],
186 JSValueRef* exception)
188 LoggerD("replyResult called");
190 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
191 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
195 ThrowMsg(ConversionException, "Object is null.");
198 ArgumentValidator validator(context, argumentCount, arguments);
200 JSObjectRef dataArrayObj = validator.toArrayObject(0, true);
201 std::vector<ApplicationControlDataPtr> dataArray;
203 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
204 dataArray = converter->toApplicationControlDataArray(arguments[0]);
206 RequestedApplicationControlPtr providerMgr = priv->getObject();
207 providerMgr->replyResult(dataArray);
209 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
211 return JSValueMakeUndefined(context);
213 } catch (const BasePlatformException &err) {
214 return JSWebAPIErrorFactory::postException(context, exception, err);
215 } catch(const ConversionException& err) {
216 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
217 } catch (const NotFoundException& ex) {
218 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, ex.GetMessage());
220 DeviceAPI::Common::UnknownException err("Unknown Error in RequestedApplicationControl.replyResult().");
221 return JSWebAPIErrorFactory::postException(context, exception, err);
225 JSValueRef JSRequestedApplicationControl::replyFailure(JSContextRef context,
227 JSObjectRef thisObject,
228 size_t argumentCount,
229 const JSValueRef arguments[],
230 JSValueRef* exception)
232 LoggerD("replyFailure called");
234 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
235 JSRequestedApplicationControlPriv *priv = static_cast<JSRequestedApplicationControlPriv*>(JSObjectGetPrivate(thisObject));
239 ThrowMsg(ConversionException, "Object is null.");
242 RequestedApplicationControlPtr providerMgr = priv->getObject();
243 providerMgr->replyFailure();
245 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
247 return JSValueMakeUndefined(context);
249 } catch (const BasePlatformException &err) {
250 return JSWebAPIErrorFactory::postException(context, exception, err);
251 } catch (const NotFoundException& ex) {
252 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_FOUND_ERROR, ex.GetMessage());
254 DeviceAPI::Common::UnknownException err("Unknown Error in RequestedApplicationControl.replyFailure().");
255 return JSWebAPIErrorFactory::postException(context, exception, err);