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"
31 namespace Application {
33 using namespace WrtDeviceApis;
34 using namespace DeviceAPI::Common;
37 JSClassRef JSApplicationInformation::m_classRef = NULL;
39 JSClassDefinition JSApplicationInformation::m_classInfo = {
41 kJSClassAttributeNone,
42 TIZEN_INTERFACE_APPLICATION_INFORMATION,
51 NULL, //DeleteProperty,
52 NULL, //GetPropertyNames,
53 NULL, //CallAsFunction,
54 NULL, //CallAsConstructor,
59 JSStaticValue JSApplicationInformation::m_property[] = {
60 { TIZEN_APPLICATION_INFORMATION_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
61 { TIZEN_APPLICATION_INFORMATION_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
62 { TIZEN_APPLICATION_INFORMATION_ICONPATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
63 { TIZEN_APPLICATION_INFORMATION_VERSION, getProperty, NULL, kJSPropertyAttributeReadOnly },
64 { TIZEN_APPLICATION_INFORMATION_SHOW, getProperty, NULL, kJSPropertyAttributeReadOnly },
65 { TIZEN_APPLICATION_INFORMATION_CATEGORIES, getProperty, NULL, kJSPropertyAttributeReadOnly },
66 { TIZEN_APPLICATION_INFORMATION_INSTALL_DATE, getProperty, NULL, kJSPropertyAttributeReadOnly },
67 { TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE, getProperty, NULL, kJSPropertyAttributeReadOnly },
68 { TIZEN_APPLICATION_INFORMATION_PACKAGE_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
72 JSClassRef DLL_EXPORT JSApplicationInformation::getClassRef() {
74 m_classRef = JSClassCreate(&m_classInfo);
79 JSValueRef JSApplicationInformation::createJSObject(JSContextRef context,
80 const std::string &name,
81 const std::string &appId,
82 const std::string &iconPath,
83 const std::string &version,
85 const std::vector<std::string> &categories,
86 const time_t &installDate,
87 const long &installSize,
88 const std::string &pkgId)
90 ApplicationInformationPtr privateData = ApplicationInformationPtr(new ApplicationInformation());
91 privateData->setName(name);
92 privateData->setAppId(appId);
93 privateData->setIconPath(iconPath);
94 privateData->setVersion(version);
95 privateData->setShow(show);
96 privateData->setCategories(categories);
97 privateData->setInstallDate(installDate);
98 privateData->setInstallSize(installSize);
99 privateData->setPackageId(pkgId);
101 JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
103 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
104 if (NULL == jsValueRef) {
105 LoggerE("object creation error");
106 return JSValueMakeUndefined(context);
112 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
116 void JSApplicationInformation::finalize(JSObjectRef object)
118 JSApplicationInformationPriv* priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
119 JSObjectSetPrivate(object, NULL);
123 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
125 return JSValueIsObjectOfClass(context, value, getClassRef());
128 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSContextRef context, JSObjectRef object)
130 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
132 throw TypeMismatchException("Private object is null");
134 ApplicationInformationPtr result = priv->getObject();
136 throw TypeMismatchException("Private object is null");
141 ApplicationInformationPtr JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
143 if (!isObjectOfClass(context, value)) {
144 throw TypeMismatchException("is not a object class");
147 JSObjectRef object = JSValueToObject(context, value, NULL);
149 throw TypeMismatchException("Fail to get object");
152 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
154 throw TypeMismatchException("Private object is null");
157 return priv->getObject();
160 JSValueRef JSApplicationInformation::getProperty(JSContextRef context,
162 JSStringRef propertyName,
163 JSValueRef* exception)
166 CommonsJavaScript::Converter converter(context);
167 ApplicationInformationPtr privateData = getPrivData(context, object);
169 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_ID)) {
170 return converter.toJSValueRef(privateData->getAppId());
171 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_NAME)) {
172 return converter.toJSValueRef(privateData->getName());
173 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_ICONPATH)) {
174 return converter.toJSValueRef(privateData->getIconPath());
175 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_SHOW)) {
176 return converter.toJSValueRef(privateData->getShow());
177 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_CATEGORIES)) {
178 return converter.toJSValueRef(privateData->getCategories());
179 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_PACKAGE_ID)) {
180 return converter.toJSValueRef(privateData->getPackageId());
181 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_VERSION)) {
182 return converter.toJSValueRef(privateData->getVersion());
183 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_DATE)) {
184 return converter.toJSValueRef(privateData->getInstallDate());
186 if (!privateData->isInitialized()) {
187 AppManagerWrapperSingleton::Instance().initializeAppInfo(privateData);
190 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE)) {
191 return converter.toJSValueRefLong(privateData->getInstallSize());
194 } catch (const BasePlatformException &err) {
195 return JSWebAPIErrorFactory::postException(context, exception, err);
197 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
198 return JSWebAPIErrorFactory::postException(context, exception, err);
201 return JSValueMakeUndefined(context);