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 "JSApplicationControl.h"
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39 using namespace DeviceAPI::Common;
42 namespace Application {
44 JSClassRef JSApplicationControl::m_jsClassRef = NULL;
46 JSClassDefinition JSApplicationControl::m_classInfo = {
48 kJSClassAttributeNone,
49 TIZEN_INTERFACE_APPLICATION_CONTROL,
57 setProperty, //SetProperty,
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 constructor, //CallAsConstructor,
66 JSStaticValue JSApplicationControl::m_property[] = {
67 { TIZEN_APPLICATION_CONTROL_OPERATION, getProperty, setProperty, kJSPropertyAttributeNone },
68 { TIZEN_APPLICATION_CONTROL_URI, getProperty, setProperty, kJSPropertyAttributeNone },
69 { TIZEN_APPLICATION_CONTROL_MIME, getProperty, setProperty, kJSPropertyAttributeNone },
70 { TIZEN_APPLICATION_CONTROL_CATEGORY, getProperty, setProperty, kJSPropertyAttributeNone },
71 { TIZEN_APPLICATION_CONTROL_DATA, getProperty, setProperty, kJSPropertyAttributeNone },
75 const JSClassDefinition* JSApplicationControl::getClassInfo()
80 const JSClassRef JSApplicationControl::getClassRef()
83 m_jsClassRef = JSClassCreate(&m_classInfo);
89 JSObjectRef JSApplicationControl::createJSObject(JSContextRef context, const ApplicationControlPtr &appsvc)
91 LogInfo(">> createJSObject");
92 JSApplicationControlPriv *priv = new JSApplicationControlPriv(context, appsvc);
95 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
98 return JSObjectMake(context, getClassRef(), priv);
102 ApplicationControlPtr JSApplicationControl::getApplicationControl(JSContextRef context, JSValueRef value)
104 if (!isObjectOfClass(context, value)) {
105 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
108 JSObjectRef object = JSValueToObject(context, value, NULL);
110 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
113 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
115 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
117 return priv->getObject();
120 ApplicationControlPtr JSApplicationControl::getPrivateData(JSObjectRef object)
122 JSApplicationControlPriv* priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
124 Throw(WrtDeviceApis::Commons::NullPointerException);
127 return priv->getObject();
131 void JSApplicationControl::initialize(JSContextRef context,JSObjectRef object)
133 LogInfo(">> initialize");
136 void JSApplicationControl::finalize(JSObjectRef object)
138 LogInfo(">> finalize");
139 JSApplicationControlPriv* priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
140 JSObjectSetPrivate(object, NULL);
141 LogDebug("Deleting ApplicationControl object");
145 JSObjectRef JSApplicationControl::constructor(JSContextRef context,
146 JSObjectRef constructor,
147 size_t argumentCount,
148 const JSValueRef arguments[],
149 JSValueRef* exception)
151 if (argumentCount == 0) {
152 LogError("Wrong parameters");
153 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
157 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
160 std::string operation = "";
161 std::string uri = "";
162 std::string mime = "";
163 std::string category = "";
164 std::vector<ApplicationControlDataPtr> appControlDataArray;
166 operation = converter->toString(arguments[0]);
167 ApplicationUtil util(context, exception);
168 if ((argumentCount > 1) && !util.isNullOrUndefined(arguments[1])) {
169 uri = converter->toString(arguments[1]);
171 if ((argumentCount > 2) && !util.isNullOrUndefined(arguments[2])) {
172 mime = converter->toString(arguments[2]);
174 if ((argumentCount > 3) && !util.isNullOrUndefined(arguments[3])) {
175 category = converter->toString(arguments[3]);
177 if (argumentCount > 4) {
178 appControlDataArray = converter->toApplicationControlDataArray(arguments[4]);
181 ApplicationControlPtr appsvc = ApplicationControlPtr(new ApplicationControl(operation, uri, mime, category, appControlDataArray));
183 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "unknow error on constructor");
187 return createJSObject(context, appsvc);
189 } Catch (WrtDeviceApis::Commons::Exception) {
190 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
197 bool JSApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
199 return JSValueIsObjectOfClass(context, value, getClassRef());
202 ApplicationControlPtr JSApplicationControl::getPrivData(JSObjectRef object)
205 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
207 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
209 ApplicationControlPtr result = priv->getObject();
211 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
217 JSValueRef JSApplicationControl::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
219 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(JSObjectGetPrivate(object));
221 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mistmatch error.");
225 ApplicationControlPtr appsvc = priv->getObject();
226 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
228 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_OPERATION)) {
229 LogDebug("JSApplicationControl::getProperty::operation " << appsvc->getOperation());
230 return converter->toJSValueRef(appsvc->getOperation());
231 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_URI)) {
232 LogDebug("JSApplicationControl::getProperty::uri " << appsvc->getUri());
233 return converter->toJSValueRef(appsvc->getUri());
234 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_MIME)) {
235 LogDebug("JSApplicationControl::getProperty::mime " << appsvc->getMime());
236 return converter->toJSValueRef(appsvc->getMime());
237 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_CATEGORY)) {
238 LogDebug("JSApplicationControl::getProperty::category " << appsvc->getCategory());
239 return converter->toJSValueRef(appsvc->getCategory());
240 }else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA)) {
241 LogDebug("JSApplicationControl::getProperty::extraData ");
242 return converter->toJSValueRef(appsvc->getAppControlDataArray());
245 } Catch(WrtDeviceApis::Commons::Exception) {
246 LogError("Exception: " << _rethrown_exception.GetMessage());
247 return JSDOMExceptionFactory::UnknownException.make(context, exception);
250 /* do not return undefined object to find method */
254 bool JSApplicationControl::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception)
257 ApplicationControlPtr privateData = getPrivData(object);
258 ApplicationConverterFactory::ConverterType converter = ApplicationConverterFactory::getConverter(context);
260 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_OPERATION)) {
261 privateData->setOperation(converter->toString(value));
263 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_URI)) {
264 privateData->setUri(converter->toString(value));
266 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_MIME)) {
267 privateData->setMime(converter->toString(value));
269 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_CATEGORY)) {
270 privateData->setCategory(converter->toString(value));
272 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA)) {
273 privateData->setAppControlDataArray(converter->toApplicationControlDataArray(value));
277 } Catch(WrtDeviceApis::Commons::Exception) {
278 LogError("Exception: " << _rethrown_exception.GetMessage());
279 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
286 bool JSApplicationControl::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
288 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());