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.
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
21 #include <CommonsJavaScript/PrivateObject.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/ScopedJSStringRef.h>
24 #include <SecurityExceptions.h>
25 #include <ArgumentValidator.h>
28 //#include <Commons/Exception.h>
29 #include <JSWebAPIErrorFactory.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,
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //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()
81 const JSClassRef DLL_EXPORT JSApplicationControl::getClassRef()
85 m_jsClassRef = JSClassCreate(&m_classInfo);
91 JSObjectRef JSApplicationControl::createJSObject(JSContextRef context,
92 const ApplicationControlPtr &appsvc,
93 JSValueRef jsValueData)
96 JSApplicationControlPriv *priv = new JSApplicationControlPriv(context, appsvc);
98 LOGE("Failed to alloc memory");
102 JSObjectRef jsObjectRef = JSObjectMake(context,
104 static_cast<void*>(priv));
105 if (NULL == jsObjectRef)
107 LOGE("object creation error");
111 const ScopedJSStringRef jsStrData(JSStringCreateWithUTF8CString(
112 TIZEN_APPLICATION_CONTROL_DATA_INTERNAL));
114 JSObjectSetProperty(context, jsObjectRef, jsStrData.get(), jsValueData,
115 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
121 ApplicationControlPtr DLL_EXPORT JSApplicationControl::getApplicationControl(
122 JSContextRef context, JSValueRef value)
125 if (!isObjectOfClass(context, value)) {
126 throw TypeMismatchException("is not a object class");
129 JSObjectRef object = JSValueToObject(context, value, NULL);
131 throw TypeMismatchException("Private object is null");
134 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(
135 JSObjectGetPrivate(object));
137 throw TypeMismatchException("Private object is null");
139 return priv->getObject();
142 void JSApplicationControl::initialize(JSContextRef context,JSObjectRef object)
147 void JSApplicationControl::finalize(JSObjectRef object)
150 JSApplicationControlPriv* priv = static_cast<JSApplicationControlPriv*>(
151 JSObjectGetPrivate(object));
152 JSObjectSetPrivate(object, NULL);
156 JSObjectRef DLL_EXPORT JSApplicationControl::constructor(JSContextRef context,
157 JSObjectRef constructor,
158 size_t argumentCount,
159 const JSValueRef arguments[],
160 JSValueRef* exception)
163 ArgumentValidator validator(context, argumentCount, arguments);
165 ApplicationControlPtr appsvc = ApplicationControlPtr(new ApplicationControl());
167 JSValueRef jsValueData = NULL;
170 appsvc->setOperation(validator.toString(0));
171 appsvc->setUri(validator.toString(1, true, ""));
172 appsvc->setMime(validator.toString(2, true, ""));
173 appsvc->setCategory(validator.toString(3, true, ""));
175 JSObjectRef dataArray = validator.toArrayObject(4, true);
177 ApplicationConverterFactory::ConverterType converter =
178 ApplicationConverterFactory::getConverter(context);
179 std::vector<ApplicationControlDataPtr> appControlDataArray =
180 converter->toApplicationControlDataArray(dataArray);
181 appsvc->setAppControlDataArray(appControlDataArray);
182 jsValueData = dataArray;
184 } catch (const BasePlatformException& err) {
185 LOGD("Exception occured while creating constructor : %s", err.getMessage().c_str());
186 } catch (const ConversionException& err) {
187 LOGD("Exception occured while creating constructor : %s", err.GetMessage().c_str());
190 if (jsValueData == NULL)
192 jsValueData = JSCreateArrayObject(context, 0, NULL);
195 JSObjectRef obj = createJSObject(context, appsvc, jsValueData);
199 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
200 JSObjectSetProperty(context, obj, ctorName, constructor,
201 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
202 JSStringRelease(ctorName);
208 bool JSApplicationControl::isObjectOfClass(JSContextRef context, JSValueRef value)
211 return JSValueIsObjectOfClass(context, value, getClassRef());
214 ApplicationControlPtr JSApplicationControl::getPrivData(JSContextRef context,
218 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(
219 JSObjectGetPrivate(object));
221 throw TypeMismatchException("Private object is null");
223 ApplicationControlPtr result = priv->getObject();
225 throw TypeMismatchException("Private object is null");
231 JSValueRef JSApplicationControl::getProperty(JSContextRef context, JSObjectRef object,
232 JSStringRef propertyName, JSValueRef* exception)
235 JSApplicationControlPriv *priv = static_cast<JSApplicationControlPriv*>(
236 JSObjectGetPrivate(object));
238 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
239 return JSWebAPIErrorFactory::postException(context, exception, err);
243 ApplicationControlPtr appsvc = priv->getObject();
244 ApplicationConverterFactory::ConverterType converter =
245 ApplicationConverterFactory::getConverter(context);
247 if (JSStringIsEqualToUTF8CString(propertyName,
248 TIZEN_APPLICATION_CONTROL_OPERATION)) {
249 return converter->toJSValueRef(appsvc->getOperation());
250 } else if (JSStringIsEqualToUTF8CString(propertyName,
251 TIZEN_APPLICATION_CONTROL_URI)) {
252 std::string uri = appsvc->getUri();
254 return JSValueMakeNull(context);
256 return converter->toJSValueRef(uri);
258 } else if (JSStringIsEqualToUTF8CString(propertyName,
259 TIZEN_APPLICATION_CONTROL_MIME)) {
260 std::string mime = appsvc->getMime();
262 return JSValueMakeNull(context);
264 return converter->toJSValueRef(mime);
266 } else if (JSStringIsEqualToUTF8CString(propertyName,
267 TIZEN_APPLICATION_CONTROL_CATEGORY)) {
268 std::string category = appsvc->getCategory();
269 if (category.empty()) {
270 return JSValueMakeNull(context);
272 return converter->toJSValueRef(category);
274 } else if (JSStringIsEqualToUTF8CString(propertyName,
275 TIZEN_APPLICATION_CONTROL_DATA)) {
276 const ScopedJSStringRef jsStrData(JSStringCreateWithUTF8CString(
277 TIZEN_APPLICATION_CONTROL_DATA_INTERNAL));
278 return JSObjectGetProperty(context, object, jsStrData.get(), NULL);
282 LOGE("Exception occured while get property");
283 return JSValueMakeUndefined(context);
286 /* do not return undefined object to find method */
290 bool JSApplicationControl::setProperty(JSContextRef context,
292 JSStringRef propertyName,
294 JSValueRef* exception)
298 ApplicationControlPtr privateData = getPrivData(context, object);
299 ApplicationConverterFactory::ConverterType converter =
300 ApplicationConverterFactory::getConverter(context);
302 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_OPERATION)) {
303 privateData->setOperation(converter->toString(value));
305 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_URI)) {
306 privateData->setUri(converter->toString(value));
308 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_MIME)) {
309 privateData->setMime(converter->toString(value));
311 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_CATEGORY)) {
312 privateData->setCategory(converter->toString(value));
314 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_CONTROL_DATA)) {
315 if (!JSIsArrayValue(context, value))
318 const ScopedJSStringRef jsStrData(
319 JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_INTERNAL));
320 JSObjectSetProperty(context, object, jsStrData.get(), value,
321 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
327 LOGE("Exception occured while set property");