Update change log and spec for wrt-plugins-tizen_0.4.74
[framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationInformation.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <cassert>
19 #include <memory>
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"
28 #include <Export.h>
29 #include <Logger.h>
30
31 namespace DeviceAPI {
32 namespace Application { 
33         
34 using namespace WrtDeviceApis;
35 using namespace DeviceAPI::Common;
36
37
38 JSClassRef JSApplicationInformation::m_classRef = NULL;
39
40 JSClassDefinition JSApplicationInformation::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     TIZEN_INTERFACE_APPLICATION_INFORMATION,
44     0,
45     m_property,
46     0,
47     initialize,
48     finalize,
49     NULL,     //HasProperty,
50     NULL,       //GetProperty,
51     NULL,     //SetProperty,
52     NULL,     //DeleteProperty,
53     NULL,     //GetPropertyNames,
54     NULL,     //CallAsFunction,
55     NULL,     //CallAsConstructor,
56     NULL,
57     NULL,     //ConvertToType
58 };
59
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 },
70     { 0, 0, 0, 0 }
71 };
72
73 JSClassRef DLL_EXPORT JSApplicationInformation::getClassRef() {
74         if (!m_classRef) {
75                 m_classRef = JSClassCreate(&m_classInfo);
76         }
77         return m_classRef;
78 }
79
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,
85                 const bool &show,
86                 const std::vector<std::string> &categories,
87                 const time_t &installDate,
88                 const long &installSize,
89                 const std::string &pkgId)
90 {
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);
101         
102         JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
103         
104         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
105         if (NULL == jsValueRef) {
106                 LoggerE("object creation error");
107                 return JSValueMakeUndefined(context);
108         }
109         
110         return jsValueRef;
111 }
112
113 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
114 {
115 }
116
117 void JSApplicationInformation::finalize(JSObjectRef object)
118 {
119     JSApplicationInformationPriv* priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
120     if(priv == NULL)
121         return;
122
123     ApplicationInformationPtr privateData = priv->getObject();
124
125         JSValueRef installDateJSValue = static_cast<JSValueRef>(privateData->getInstallDateJSValue());
126         if(installDateJSValue != NULL)
127                 JSValueUnprotect(priv->getContext(), installDateJSValue);
128
129         JSValueRef categoriesJSValue = static_cast<JSValueRef>(privateData->getCategoriesJSValue());
130         if(categoriesJSValue != NULL)
131                 JSValueUnprotect(priv->getContext(), categoriesJSValue);
132
133     JSObjectSetPrivate(object, NULL);
134     delete priv;                
135 }
136
137 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
138 {
139         return JSValueIsObjectOfClass(context, value, getClassRef());
140 }
141
142 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSContextRef context, JSObjectRef object)
143 {
144         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
145         if (!priv) {
146                 throw TypeMismatchException("Private object is null");
147         }
148         ApplicationInformationPtr result = priv->getObject();
149         if (!result) {
150                 throw TypeMismatchException("Private object is null");
151         }
152         return result;
153 }
154
155 ApplicationInformationPtr JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
156 {
157         if (!isObjectOfClass(context, value)) {
158                 throw TypeMismatchException("is not a object class");
159         }
160         
161         JSObjectRef object = JSValueToObject(context, value, NULL);
162         if (!object) {
163                 throw TypeMismatchException("Fail to get object");
164         }
165         
166         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
167         if (!priv) {
168                 throw TypeMismatchException("Private object is null");
169         }
170         
171         return priv->getObject();
172 }
173
174 JSValueRef JSApplicationInformation::getProperty(JSContextRef context,
175         JSObjectRef object,
176         JSStringRef propertyName,
177         JSValueRef* exception)
178 {
179         try {
180                 CommonsJavaScript::Converter converter(context);
181                 ApplicationInformationPtr privateData = getPrivData(context, object);
182                 JSContextRef gContext = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object))->getContext();
183
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));
198                         }
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));
210                         }
211                         return installDateJSValue;
212                 }
213         } catch (...) {
214         LoggerE("Exception occured while get property");
215                 return JSValueMakeUndefined(context);
216     }
217
218         /* do not return undefined object to find method */
219         return NULL;
220 }
221
222 JSValueRef JSApplicationInformation::getPropertySize(JSContextRef context,
223         JSObjectRef object,
224         JSStringRef propertyName,
225         JSValueRef* exception)
226 {
227         LoggerD("getPropertySize called");
228
229         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
230         if (!priv) {
231                 LoggerE("Private object is not exists.");
232                 return JSValueMakeUndefined(context);
233         }
234
235         TIZEN_CHECK_ACCESS(context, exception, priv, APPLICATION_FUNCTION_API_SIZE);
236
237         ApplicationInformationPtr privateData = priv->getObject();
238         if (!privateData) {
239                 LoggerE("Private object is wrong");
240                 return JSValueMakeUndefined(context);
241         }
242
243         try {
244                 CommonsJavaScript::Converter converter(context);
245
246                 if (!privateData->isInitialized()) {
247                         AppManagerWrapperSingleton::Instance().initializeAppInfo(privateData);
248                 }
249
250                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE)) {
251                         return converter.toJSValueRefLong(privateData->getInstallSize());
252                 }
253
254         } catch (...) {
255         LoggerE("Exception occured while get property");
256                 return JSValueMakeUndefined(context);
257     }
258
259         /* do not return undefined object to find method */
260         return NULL;
261 }
262
263 }
264 }