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/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <Commons/Exception.h>
23 #include "ApplicationInformation.h"
24 #include <JSWebAPIErrorFactory.h>
25 #include "JSApplicationInformation.h"
26 #include "AppManagerWrapper.h"
27 #include "plugin_config_impl.h"
32 namespace Application {
34 using namespace WrtDeviceApis;
35 using namespace DeviceAPI::Common;
38 JSClassRef JSApplicationInformation::m_classRef = NULL;
40 JSClassDefinition JSApplicationInformation::m_classInfo = {
42 kJSClassAttributeNone,
43 TIZEN_INTERFACE_APPLICATION_INFORMATION,
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
60 JSStaticValue JSApplicationInformation::m_property[] = {
61 { TIZEN_APPLICATION_INFORMATION_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
62 { TIZEN_APPLICATION_INFORMATION_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
63 { TIZEN_APPLICATION_INFORMATION_ICONPATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
64 { TIZEN_APPLICATION_INFORMATION_VERSION, getProperty, NULL, kJSPropertyAttributeReadOnly },
65 { TIZEN_APPLICATION_INFORMATION_SHOW, getProperty, NULL, kJSPropertyAttributeReadOnly },
66 { TIZEN_APPLICATION_INFORMATION_CATEGORIES, getProperty, NULL, kJSPropertyAttributeReadOnly },
67 { TIZEN_APPLICATION_INFORMATION_INSTALL_DATE, getProperty, NULL, kJSPropertyAttributeReadOnly },
68 { TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly },
69 { TIZEN_APPLICATION_INFORMATION_PACKAGE_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
73 JSClassRef DLL_EXPORT JSApplicationInformation::getClassRef() {
75 m_classRef = JSClassCreate(&m_classInfo);
80 JSValueRef JSApplicationInformation::createJSObject(JSContextRef context,
81 const std::string &name,
82 const std::string &appId,
83 const std::string &iconPath,
84 const std::string &version,
86 const std::vector<std::string> &categories,
87 const time_t &installDate,
88 const long &installSize,
89 const std::string &pkgId)
91 ApplicationInformationPtr privateData = ApplicationInformationPtr(new ApplicationInformation());
92 privateData->setName(name);
93 privateData->setAppId(appId);
94 privateData->setIconPath(iconPath);
95 privateData->setVersion(version);
96 privateData->setShow(show);
97 privateData->setCategories(categories);
98 privateData->setInstallDate(installDate);
99 privateData->setInstallSize(installSize);
100 privateData->setPackageId(pkgId);
102 JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
104 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
105 if (NULL == jsValueRef) {
106 LoggerE("object creation error");
107 return JSValueMakeUndefined(context);
113 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
117 void JSApplicationInformation::finalize(JSObjectRef object)
119 JSApplicationInformationPriv* priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
123 ApplicationInformationPtr privateData = priv->getObject();
125 JSValueRef installDateJSValue = static_cast<JSValueRef>(privateData->getInstallDateJSValue());
126 if(installDateJSValue != NULL)
127 JSValueUnprotect(priv->getContext(), installDateJSValue);
129 JSValueRef categoriesJSValue = static_cast<JSValueRef>(privateData->getCategoriesJSValue());
130 if(categoriesJSValue != NULL)
131 JSValueUnprotect(priv->getContext(), categoriesJSValue);
133 JSObjectSetPrivate(object, NULL);
137 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
139 return JSValueIsObjectOfClass(context, value, getClassRef());
142 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSContextRef context, JSObjectRef object)
144 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
146 throw TypeMismatchException("Private object is null");
148 ApplicationInformationPtr result = priv->getObject();
150 throw TypeMismatchException("Private object is null");
155 ApplicationInformationPtr JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
157 if (!isObjectOfClass(context, value)) {
158 throw TypeMismatchException("is not a object class");
161 JSObjectRef object = JSValueToObject(context, value, NULL);
163 throw TypeMismatchException("Fail to get object");
166 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
168 throw TypeMismatchException("Private object is null");
171 return priv->getObject();
174 JSValueRef JSApplicationInformation::getProperty(JSContextRef context,
176 JSStringRef propertyName,
177 JSValueRef* exception)
180 CommonsJavaScript::Converter converter(context);
181 ApplicationInformationPtr privateData = getPrivData(context, object);
182 JSContextRef gContext = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object))->getContext();
184 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_ID)) {
185 return converter.toJSValueRef(privateData->getAppId());
186 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_NAME)) {
187 return converter.toJSValueRef(privateData->getName());
188 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_ICONPATH)) {
189 return converter.toJSValueRef(privateData->getIconPath());
190 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_SHOW)) {
191 return converter.toJSValueRef(privateData->getShow());
192 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_CATEGORIES)) {
193 JSValueRef categoriesJSValue = static_cast<JSValueRef>(privateData->getCategoriesJSValue());
194 if(categoriesJSValue == NULL) {
195 categoriesJSValue = converter.toJSValueRef(privateData->getCategories());
196 JSValueProtect(gContext, categoriesJSValue);
197 privateData->setCategoriesJSValue(static_cast<const void *>(categoriesJSValue));
199 return categoriesJSValue;
200 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_PACKAGE_ID)) {
201 return converter.toJSValueRef(privateData->getPackageId());
202 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_VERSION)) {
203 return converter.toJSValueRef(privateData->getVersion());
204 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_DATE)) {
205 JSValueRef installDateJSValue = static_cast<JSValueRef>(privateData->getInstallDateJSValue());
206 if(installDateJSValue == NULL) {
207 installDateJSValue = converter.toJSValueRef(privateData->getInstallDate());
208 JSValueProtect(gContext, installDateJSValue);
209 privateData->setInstallDateJSValue(static_cast<const void *>(installDateJSValue));
211 return installDateJSValue;
214 LoggerE("Exception occured while get property");
215 return JSValueMakeUndefined(context);
218 /* do not return undefined object to find method */
222 JSValueRef JSApplicationInformation::getPropertySize(JSContextRef context,
224 JSStringRef propertyName,
225 JSValueRef* exception)
227 LoggerD("getPropertySize called");
229 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
231 LoggerE("Private object is not exists.");
232 return JSValueMakeUndefined(context);
235 TIZEN_CHECK_ACCESS(context, exception, priv, APPLICATION_FUNCTION_API_SIZE);
237 ApplicationInformationPtr privateData = priv->getObject();
239 LoggerE("Private object is wrong");
240 return JSValueMakeUndefined(context);
244 CommonsJavaScript::Converter converter(context);
246 if (!privateData->isInitialized()) {
247 AppManagerWrapperSingleton::Instance().initializeAppInfo(privateData);
250 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE)) {
251 return converter.toJSValueRefLong(privateData->getInstallSize());
255 LoggerE("Exception occured while get property");
256 return JSValueMakeUndefined(context);
259 /* do not return undefined object to find method */