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.
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>
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"
37 namespace Application {
39 using namespace DeviceAPI::Common;
40 using namespace WrtDeviceApis;
41 using namespace WrtDeviceApis::CommonsJavaScript;
43 ApplicationConverter::ApplicationConverter(JSContextRef context) : Converter(context)
47 ApplicationConverter::~ApplicationConverter()
52 JSValueRef ApplicationConverter::toJSValueRefFromApplication(const ApplicationPtr &arg)
55 Throw(Commons::ConversionException);
57 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplication::getClassRef(), arg);
61 JSValueRef ApplicationConverter::toJSValueRefFromApplicationCert(const ApplicationCertPtr &arg)
64 Throw(Commons::ConversionException);
66 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationCert::getClassRef(), arg);
70 JSValueRef ApplicationConverter::toJSValueRefFromApplicationCerts(const ApplicationCertArrayPtr &arg)
73 Throw(Commons::InvalidArgumentException);
75 return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationCert, this);
79 JSValueRef ApplicationConverter::toJSValueRefFromApplicationMetaData(const ApplicationMetaDataPtr &arg)
82 Throw(Commons::ConversionException);
84 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationMetaData::getClassRef(), arg);
88 JSValueRef ApplicationConverter::toJSValueRefFromApplicationMetaDataArray(const ApplicationMetaDataArrayPtr &arg)
91 Throw(Commons::InvalidArgumentException);
93 return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationMetaData, this);
97 JSValueRef ApplicationConverter::toJSValueRefFromApplicationInformation(const ApplicationInformationPtr &arg)
100 Throw(Commons::ConversionException);
102 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationInformation::getClassRef(), arg);
105 ApplicationInformationPtr ApplicationConverter::toApplicationInformation(const JSValueRef &jsValue)
107 if(JSApplicationInformation::isObjectOfClass(m_context, jsValue))
108 return JSApplicationInformation::getApplicationInformation(m_context, jsValue);
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));
120 JSObjectRef jsObject = toJSObjectRef(jsValue);
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);
134 std::string iconPath;
137 std::vector <std::string> categories;
142 ApplicationInformationPtr result(new ApplicationInformation());
144 Throw(Commons::ConversionException);
147 if (!JSValueIsUndefined(m_context, nameData)) {
148 name = toString(nameData);
149 result->setName(name);
151 if (!JSValueIsUndefined(m_context, appIdStrData)) {
152 appid = toString(appIdStrData);
153 result->setAppId(appid);
155 if (!JSValueIsUndefined(m_context, iconPathData)) {
156 iconPath = toString(iconPathData);
157 result->setIconPath(iconPath);
159 if (!JSValueIsUndefined(m_context, versionData)) {
160 version = toString(versionData);
161 result->setVersion(version);
163 if (!JSValueIsUndefined(m_context, showData)) {
164 show = toBool(showData);
165 result->setShow(show);
167 if (!JSValueIsUndefined(m_context, categoriesData)) {
168 categories = toVectorOfStrings(categoriesData);
169 result->setCategories(categories);
171 if (!JSValueIsUndefined(m_context, installDateData)) {
172 installDate = toDateTimeT(installDateData);
173 result->setInstallDate(installDate);
175 if (!JSValueIsUndefined(m_context, installSizeData)) {
176 installSize = toDateTimeT(installSizeData);
177 result->setInstallSize(installSize);
179 if (!JSValueIsUndefined(m_context, pkgIdStrData)) {
180 pkgid = toString(pkgIdStrData);
181 result->setPackageId(pkgid);
186 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationInformationArrayPtr &arg)
189 Throw(Commons::InvalidArgumentException);
191 return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationInformation, this);
194 ApplicationInformationArrayPtr ApplicationConverter::toApplicationInformationArray(const JSValueRef &jsValue)
196 ApplicationInformationArrayPtr result(new ApplicationInformationArray());
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));
206 JSValueRef ApplicationConverter::toJSValueRefFromApplicationContext(const ApplicationContextPtr &arg)
209 Throw(Commons::InvalidArgumentException);
211 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationContext::getClassRef(), arg);
214 ApplicationContextPtr ApplicationConverter::toApplicationContext(const JSValueRef &jsValue)
216 if(JSApplicationContext::isObjectOfClass(m_context, jsValue))
217 return JSApplicationContext::getApplicationContext(m_context, jsValue);
219 const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTEXT_APP_ID));
220 const ScopedJSStringRef contextIdStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTEXT_ID));
222 JSObjectRef jsObject = toJSObjectRef(jsValue);
224 JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
225 JSValueRef contextIdStrData = JSObjectGetProperty(m_context, jsObject, contextIdStr.get(), NULL);
228 std::string contextid;
230 ApplicationContextPtr result(new ApplicationContext());
232 Throw(Commons::ConversionException);
235 if (!JSValueIsUndefined(m_context, appIdStrData)) {
236 appid = toString(appIdStrData);
237 result->setAppId(appid);
239 if (!JSValueIsUndefined(m_context, contextIdStrData)) {
240 contextid = toString(contextIdStrData);
241 result->setContextId(contextid);
247 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationContextArrayPtr &arg)
250 Throw(Commons::ConversionException);
252 return toJSValueRef_(*arg, &ApplicationConverter::toJSValueRefFromApplicationContext, this);
255 ApplicationContextArrayPtr ApplicationConverter::toApplicationContextArray(const JSValueRef &jsValue)
257 ApplicationContextArrayPtr result(new ApplicationContextArray());
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));
268 JSValueRef ApplicationConverter::toJSValueRef(const RequestedApplicationControlPtr &arg)
271 Throw(Commons::InvalidArgumentException);
273 return CommonsJavaScript::JSUtils::makeObject(m_context, JSRequestedApplicationControl::getClassRef(), arg);
276 RequestedApplicationControlPtr ApplicationConverter::toRequestedApplicationControl(const JSValueRef &jsValue)
278 if(JSRequestedApplicationControl::isObjectOfClass(m_context, jsValue)) {
279 return JSRequestedApplicationControl::getRequestedApplicationControl(m_context, jsValue);
281 ThrowMsg(Commons::ConversionException, "Wrong parameter type.");
286 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationControlPtr &arg)
289 Throw(Commons::InvalidArgumentException);
291 return JSApplicationControl::createJSObject(m_context, arg);
294 ApplicationControlPtr ApplicationConverter::toApplicationControl(const JSValueRef &jsValue)
296 if(JSApplicationControl::isObjectOfClass(m_context, jsValue)) {
297 return JSApplicationControl::getApplicationControl(m_context, jsValue);
299 throw TypeMismatchException("not a object of class ApplicationControl");
303 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationControlDataPtr &arg)
305 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationControlData::getClassRef(), arg);
308 ApplicationControlDataPtr ApplicationConverter::toApplicationControlData(const JSValueRef &jsValue)
310 if(JSApplicationControlData::isObjectOfClass(m_context, jsValue))
311 return JSApplicationControlData::getApplicationControlData(m_context, jsValue);
313 const ScopedJSStringRef appControlDataKeyStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_KEY));
314 const ScopedJSStringRef appControlDataValueStr(JSStringCreateWithUTF8CString(TIZEN_APPLICATION_CONTROL_DATA_VALUE));
316 JSObjectRef jsObject = toJSObjectRef(jsValue);
318 JSValueRef appControlDataKeyData = JSObjectGetProperty(m_context, jsObject, appControlDataKeyStr.get(), NULL);
319 JSValueRef appControlDataValueData = JSObjectGetProperty(m_context, jsObject, appControlDataValueStr.get(), NULL);
321 ApplicationControlDataPtr result(new ApplicationControlData());
323 Throw(Commons::ConversionException);
326 if (!JSValueIsUndefined(m_context, appControlDataKeyData)) {
327 result->setKey(toString(appControlDataKeyData));
330 if (!JSValueIsUndefined(m_context, appControlDataValueData)) {
331 result->setValue(toVectorOfStrings(appControlDataValueData));
336 JSValueRef ApplicationConverter::toJSValueRef(const std::vector<ApplicationControlDataPtr> &arg)
338 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
340 if (jsResult == NULL) {
341 ThrowMsg(Commons::ConversionException, "Could not create js array object");
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");
354 std::vector<ApplicationControlDataPtr> ApplicationConverter::toApplicationControlDataArray(const JSValueRef &jsValue)
356 if (JSValueIsNull(m_context, jsValue) || JSValueIsUndefined(m_context, jsValue)) {
357 return std::vector<ApplicationControlDataPtr>();
360 if (!JSIsArrayValue(m_context, jsValue)) {
361 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
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));