97eb882a36e1781796bab92bfc1d7d43af772ccd
[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 <Export.h>
28 #include <Logger.h>
29
30 namespace DeviceAPI {
31 namespace Application { 
32         
33 using namespace WrtDeviceApis;
34 using namespace DeviceAPI::Common;
35
36
37 JSClassRef JSApplicationInformation::m_classRef = NULL;
38
39 JSClassDefinition JSApplicationInformation::m_classInfo = {
40     0,
41     kJSClassAttributeNone,
42     TIZEN_INTERFACE_APPLICATION_INFORMATION,
43     0,
44     m_property,
45     0,
46     initialize,
47     finalize,
48     NULL,     //HasProperty,
49     NULL,       //GetProperty,
50     NULL,     //SetProperty,
51     NULL,     //DeleteProperty,
52     NULL,     //GetPropertyNames,
53     NULL,     //CallAsFunction,
54     NULL,     //CallAsConstructor,
55     NULL,
56     NULL,     //ConvertToType
57 };
58
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 },
69     { 0, 0, 0, 0 }
70 };
71
72 JSClassRef DLL_EXPORT JSApplicationInformation::getClassRef() {
73         if (!m_classRef) {
74                 m_classRef = JSClassCreate(&m_classInfo);
75         }
76         return m_classRef;
77 }
78
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,
84                 const bool &show,
85                 const std::vector<std::string> &categories,
86                 const time_t &installDate,
87                 const long &installSize,
88                 const std::string &pkgId)
89 {
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);
100         
101         JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
102         
103         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
104         if (NULL == jsValueRef) {
105                 LoggerE("object creation error");
106                 return JSValueMakeUndefined(context);
107         }
108         
109         return jsValueRef;
110 }
111
112 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
113 {
114 }
115
116 void JSApplicationInformation::finalize(JSObjectRef object)
117 {
118     JSApplicationInformationPriv* priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
119     JSObjectSetPrivate(object, NULL);
120     delete priv;                
121 }
122
123 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
124 {
125         return JSValueIsObjectOfClass(context, value, getClassRef());
126 }
127
128 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSContextRef context, JSObjectRef object)
129 {
130         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
131         if (!priv) {
132                 throw TypeMismatchException("Private object is null");
133         }
134         ApplicationInformationPtr result = priv->getObject();
135         if (!result) {
136                 throw TypeMismatchException("Private object is null");
137         }
138         return result;
139 }
140
141 ApplicationInformationPtr JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
142 {
143         if (!isObjectOfClass(context, value)) {
144                 throw TypeMismatchException("is not a object class");
145         }
146         
147         JSObjectRef object = JSValueToObject(context, value, NULL);
148         if (!object) {
149                 throw TypeMismatchException("Fail to get object");
150         }
151         
152         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
153         if (!priv) {
154                 throw TypeMismatchException("Private object is null");
155         }
156         
157         return priv->getObject();
158 }
159
160 JSValueRef JSApplicationInformation::getProperty(JSContextRef context,
161         JSObjectRef object,
162         JSStringRef propertyName,
163         JSValueRef* exception)
164 {
165         try {
166                 CommonsJavaScript::Converter converter(context);
167                 ApplicationInformationPtr privateData = getPrivData(context, object);
168
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());
185                 } else {
186                         if (!privateData->isInitialized()) {
187                                 AppManagerWrapperSingleton::Instance().initializeAppInfo(privateData);
188                         }
189
190                         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE)) {
191                                 return converter.toJSValueRefLong(privateData->getInstallSize());
192                         } 
193                 }
194         } catch (const BasePlatformException &err) {
195         return JSWebAPIErrorFactory::postException(context, exception, err);
196     } catch (...) {
197         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
198         return JSWebAPIErrorFactory::postException(context, exception, err);
199     }
200         
201         return JSValueMakeUndefined(context);
202 }
203
204 }
205 }