Update change log and spec for wrt-plugins-tizen_0.4.44
[platform/framework/web/wrt-plugins-tizen.git] / src / Application / ApplicationConverter.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 <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <Commons/RegexUtils.h>
22 #include <Commons/Exception.h>
23 #include <JSWebAPIErrorFactory.h>
24
25 #include "ApplicationConverter.h"
26 #include "JSApplication.h"
27 #include "JSApplicationInformation.h"
28 #include "JSApplicationContext.h"
29 #include "JSApplicationControlData.h"
30 #include "JSApplicationControlData.h"
31 #include "JSApplicationControl.h"
32 #include "JSRequestedApplicationControl.h"
33 #include "JSApplicationMetaData.h"
34 #include "JSApplicationCert.h"
35
36 namespace DeviceAPI {
37 namespace Application {
38
39 using namespace DeviceAPI::Common;
40 using namespace WrtDeviceApis;
41 using namespace WrtDeviceApis::CommonsJavaScript;
42
43 ApplicationConverter::ApplicationConverter(JSContextRef context) : Converter(context)
44 {
45 }
46
47 ApplicationConverter::~ApplicationConverter()
48 {
49 }
50
51
52 JSValueRef ApplicationConverter::toJSValueRefFromApplication(const ApplicationPtr &arg)
53 {
54         if(arg == NULL) {
55                 Throw(Commons::ConversionException);
56         }
57         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplication::getClassRef(), arg);
58 }
59
60
61 JSValueRef ApplicationConverter::toJSValueRefFromApplicationCert(const ApplicationCertPtr &arg)
62 {
63         if(arg == NULL) {
64                 Throw(Commons::ConversionException);
65         }
66         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationCert::getClassRef(), arg);
67 }
68
69
70 JSValueRef ApplicationConverter::toJSValueRefFromApplicationCerts(const ApplicationCertArrayPtr &arg)
71 {
72         if(arg == NULL) {
73                 Throw(Commons::InvalidArgumentException);
74         }
75         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationCert, this);
76 }
77
78
79 JSValueRef ApplicationConverter::toJSValueRefFromApplicationMetaData(const ApplicationMetaDataPtr &arg)
80 {
81         if(arg == NULL) {
82                 Throw(Commons::ConversionException);
83         }
84         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationMetaData::getClassRef(), arg);
85 }
86
87
88 JSValueRef ApplicationConverter::toJSValueRefFromApplicationMetaDataArray(const ApplicationMetaDataArrayPtr &arg)
89 {
90         if(arg == NULL) {
91                 Throw(Commons::InvalidArgumentException);
92         }
93         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationMetaData, this);
94 }
95
96
97 JSValueRef ApplicationConverter::toJSValueRefFromApplicationInformation(const ApplicationInformationPtr &arg)
98 {
99         if(arg == NULL) {
100                 Throw(Commons::ConversionException);
101         }
102         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationInformation::getClassRef(), arg);
103 }
104
105 ApplicationInformationPtr ApplicationConverter::toApplicationInformation(const JSValueRef &jsValue)
106 {
107         if(JSApplicationInformation::isObjectOfClass(m_context, jsValue))
108                 return JSApplicationInformation::getApplicationInformation(m_context, jsValue);
109
110         const ScopedJSStringRef nameStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_NAME));
111         const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_ID));
112         const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_ICONPATH));
113         const ScopedJSStringRef versionStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_VERSION));
114         const ScopedJSStringRef showStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_SHOW));
115         const ScopedJSStringRef categoriesStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_CATEGORIES));
116     const ScopedJSStringRef installDateStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_INSTALL_DATE));
117         const ScopedJSStringRef installSizeStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE));
118         const ScopedJSStringRef packageIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_INFORMATION_PACKAGE_ID));
119
120         JSObjectRef jsObject = toJSObjectRef(jsValue);
121
122         JSValueRef nameData = JSObjectGetProperty(m_context, jsObject, nameStr.get(), NULL);
123         JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
124         JSValueRef iconPathData = JSObjectGetProperty(m_context, jsObject, iconPathStr.get(), NULL);
125         JSValueRef versionData = JSObjectGetProperty(m_context, jsObject, versionStr.get(), NULL);      
126         JSValueRef showData = JSObjectGetProperty(m_context, jsObject, showStr.get(), NULL);
127         JSValueRef categoriesData = JSObjectGetProperty(m_context, jsObject, categoriesStr.get(), NULL);
128         JSValueRef installDateData = JSObjectGetProperty(m_context, jsObject, installDateStr.get(), NULL);
129         JSValueRef installSizeData = JSObjectGetProperty(m_context, jsObject, installSizeStr.get(), NULL);
130         JSValueRef pkgIdStrData = JSObjectGetProperty(m_context, jsObject, packageIdStr.get(), NULL);
131
132         std::string name;
133         std::string appid;
134         std::string iconPath;
135         std::string version;
136         bool show;
137         std::vector <std::string> categories;
138         time_t installDate;
139         long installSize;
140         std::string pkgid;
141
142         ApplicationInformationPtr result(new ApplicationInformation());
143         if (!result) {
144                 Throw(Commons::ConversionException);
145         }
146
147         if (!JSValueIsUndefined(m_context, nameData)) {
148                 name = toString(nameData);
149                 result->setName(name);
150         }
151         if (!JSValueIsUndefined(m_context, appIdStrData)) {
152                 appid = toString(appIdStrData);
153                 result->setAppId(appid);
154         }
155         if (!JSValueIsUndefined(m_context, iconPathData)) {
156                 iconPath = toString(iconPathData);
157                 result->setIconPath(iconPath);
158         }
159         if (!JSValueIsUndefined(m_context, versionData)) {
160                 version = toString(versionData);
161                 result->setVersion(version);
162         }
163         if (!JSValueIsUndefined(m_context, showData)) {
164                 show = toBool(showData);
165                 result->setShow(show);
166         }
167         if (!JSValueIsUndefined(m_context, categoriesData)) {
168                 categories = toVectorOfStrings(categoriesData);
169                 result->setCategories(categories);
170         }       
171         if (!JSValueIsUndefined(m_context, installDateData)) {
172                 installDate = toDateTimeT(installDateData);
173                 result->setInstallDate(installDate);
174         }
175         if (!JSValueIsUndefined(m_context, installSizeData)) {
176                 installSize = toDateTimeT(installSizeData);
177                 result->setInstallSize(installSize);
178         }
179         if (!JSValueIsUndefined(m_context, pkgIdStrData)) {
180                 pkgid = toString(pkgIdStrData);
181                 result->setPackageId(pkgid);
182         }       
183         return result;
184 }
185
186 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationInformationArrayPtr &arg)
187 {
188         if(arg == NULL) {
189                 Throw(Commons::InvalidArgumentException);
190         }
191         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationInformation, this);
192 }
193
194 ApplicationInformationArrayPtr ApplicationConverter::toApplicationInformationArray(const JSValueRef &jsValue)
195 {
196         ApplicationInformationArrayPtr result(new ApplicationInformationArray());
197
198         JSObjectRef jsObject = toJSObjectRef(jsValue);
199     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
200         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
201         result->push_back(toApplicationInformation(element));
202     }
203     return result;
204 }
205
206 JSValueRef ApplicationConverter::toJSValueRefFromApplicationContext(const ApplicationContextPtr &arg)
207 {
208         if(arg == NULL) {
209                 Throw(Commons::InvalidArgumentException);
210         }
211         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationContext::getClassRef(), arg);
212 }
213
214 ApplicationContextPtr ApplicationConverter::toApplicationContext(const JSValueRef &jsValue)
215 {
216         if(JSApplicationContext::isObjectOfClass(m_context, jsValue))
217                 return JSApplicationContext::getApplicationContext(m_context, jsValue);
218
219         const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTEXT_APP_ID));
220         const ScopedJSStringRef contextIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTEXT_ID));
221
222         JSObjectRef jsObject = toJSObjectRef(jsValue);
223
224         JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
225         JSValueRef contextIdStrData = JSObjectGetProperty(m_context, jsObject, contextIdStr.get(), NULL);
226
227         std::string appid;
228         std::string contextid;
229
230         ApplicationContextPtr result(new ApplicationContext());
231         if (!result) {
232                 Throw(Commons::ConversionException);
233         }
234
235         if (!JSValueIsUndefined(m_context, appIdStrData)) {
236                 appid = toString(appIdStrData);
237                 result->setAppId(appid);
238         }
239         if (!JSValueIsUndefined(m_context, contextIdStrData)) {
240                 contextid = toString(contextIdStrData);
241                 result->setContextId(contextid);
242         }
243         
244         return result;
245 }
246
247 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationContextArrayPtr &arg)
248 {
249         if(arg == NULL) {
250                 Throw(Commons::ConversionException);
251         }
252         return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationContext, this);
253 }
254
255 ApplicationContextArrayPtr ApplicationConverter::toApplicationContextArray(const JSValueRef &jsValue)
256 {
257         ApplicationContextArrayPtr result(new ApplicationContextArray());
258
259         JSObjectRef jsObject = toJSObjectRef(jsValue);
260     for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
261         JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
262         result->push_back(toApplicationContext(element));
263     }
264     return result;
265 }
266
267
268 JSValueRef ApplicationConverter::toJSValueRef(const RequestedApplicationControlPtr &arg)
269 {
270         if(arg == NULL) {
271                 Throw(Commons::InvalidArgumentException);
272         }
273         return CommonsJavaScript::JSUtils::makeObject(m_context, JSRequestedApplicationControl::getClassRef(), arg);
274 }
275
276 RequestedApplicationControlPtr ApplicationConverter::toRequestedApplicationControl(const JSValueRef &jsValue)
277 {
278         if(JSRequestedApplicationControl::isObjectOfClass(m_context, jsValue)) {
279                 return JSRequestedApplicationControl::getRequestedApplicationControl(m_context, jsValue);
280         } else {
281             ThrowMsg(Commons::ConversionException, "Wrong parameter type.");
282         }
283 }
284
285
286 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationControlPtr &arg)
287 {
288         if(arg == NULL) {
289                 Throw(Commons::InvalidArgumentException);
290         }
291         return JSApplicationControl::createJSObject(m_context, arg);
292 }
293
294 ApplicationControlPtr ApplicationConverter::toApplicationControl(const JSValueRef &jsValue)
295 {
296         if(JSApplicationControl::isObjectOfClass(m_context, jsValue)) {
297                 return JSApplicationControl::getApplicationControl(m_context, jsValue);
298         } else {
299             throw TypeMismatchException("not a object of class ApplicationControl");
300         }
301 }
302
303 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationControlDataPtr &arg)
304 {
305         return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationControlData::getClassRef(), arg);
306 }
307
308 ApplicationControlDataPtr ApplicationConverter::toApplicationControlData(const JSValueRef &jsValue)
309 {
310         if(JSApplicationControlData::isObjectOfClass(m_context, jsValue))
311                 return JSApplicationControlData::getApplicationControlData(m_context, jsValue);
312
313         const ScopedJSStringRef appControlDataKeyStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_KEY));
314         const ScopedJSStringRef appControlDataValueStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_VALUE));
315
316         JSObjectRef jsObject = toJSObjectRef(jsValue);
317
318         JSValueRef appControlDataKeyData = JSObjectGetProperty(m_context, jsObject, appControlDataKeyStr.get(), NULL);
319         JSValueRef appControlDataValueData = JSObjectGetProperty(m_context, jsObject, appControlDataValueStr.get(), NULL);
320
321         ApplicationControlDataPtr result(new ApplicationControlData());
322         if (!result) {
323                 Throw(Commons::ConversionException);
324         }
325
326         if (!JSValueIsUndefined(m_context, appControlDataKeyData)) {
327                 result->setKey(toString(appControlDataKeyData));
328         }
329
330         if (!JSValueIsUndefined(m_context, appControlDataValueData)) {
331                 result->setValue(toVectorOfStrings(appControlDataValueData));
332         }
333         return result;
334 }
335
336 JSValueRef ApplicationConverter::toJSValueRef(const std::vector<ApplicationControlDataPtr> &arg)
337 {
338     JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
339
340     if (jsResult == NULL) {
341         ThrowMsg(Commons::ConversionException, "Could not create js array object");
342     }
343
344     for (std::size_t i = 0; i < arg.size(); ++i) {
345         JSValueRef tmpVal = toJSValueRef(arg[i]);
346         if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
347             ThrowMsg(Commons::ConversionException, "Could not insert value into js array");
348         }
349     }
350
351     return jsResult;    
352 }
353
354 std::vector<ApplicationControlDataPtr> ApplicationConverter::toApplicationControlDataArray(const JSValueRef &jsValue)
355 {
356     if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
357         return std::vector<ApplicationControlDataPtr>();
358     }
359
360     if (!JSIsArrayValue(m_context, jsValue)) {
361         ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
362     }
363
364     std::vector<ApplicationControlDataPtr> result;
365     JSObjectRef objArg = toJSObjectRef(jsValue);
366     for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
367         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
368         result.push_back(toApplicationControlData(element));
369     }
370     return result;      
371 }
372
373 }
374 }