2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <dpl/log/log.h>
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>
24 #include "ApplicationConverter.h"
25 #include "JSApplicationInformation.h"
26 #include "JSApplicationInformationArray.h"
27 #include "JSApplicationContext.h"
28 #include "JSApplicationContextArray.h"
29 #include "JSApplicationServiceData.h"
30 #include "JSApplicationServiceData.h"
31 #include "JSApplicationService.h"
32 //#include "JSApplicationServiceReply.h"
35 const char* APPLICATOIN_ATTRIBUTE_NAME = "name";
36 const char* APPLICATOIN_ATTRIBUTE_APP_ID = "appId";
37 const char* APPLICATOIN_ATTRIBUTE_CONTEXT_ID = "contextId";
38 const char* APPLICATOIN_ATTRIBUTE_ICONF_PATH = "iconPath";
39 const char* APPLICATOIN_ATTRIBUTE_VERSION = "version";
40 const char* APPLICATOIN_ATTRIBUTE_EXTRA_KEY = "key";
41 const char* APPLICATOIN_ATTRIBUTE_EXTRA_VALUE = "value";
42 const char* APPLICATOIN_ATTRIBUTE_OPERATION = "operation";
43 const char* APPLICATOIN_ATTRIBUTE_URI = "uri";
44 const char* APPLICATOIN_ATTRIBUTE_MIME = "mime";
45 const char* APPLICATOIN_ATTRIBUTE_EXTRADATA = "extraData";
50 namespace Application {
52 using namespace WrtDeviceApis;
53 using namespace WrtDeviceApis::CommonsJavaScript;
54 using namespace Api::Application;
56 std::vector<std::string> ApplicationConverter::m_allowedApplicationInformation;
57 std::vector<std::string> ApplicationConverter::m_allowedApplicationContext;
58 std::vector<std::string> ApplicationConverter::m_allowedApplicationService;
59 std::vector<std::string> ApplicationConverter::m_allowedApplicationServiceData;
61 ApplicationConverter::ApplicationConverter(JSContextRef context) : Converter(context)
63 static bool init = initializeAllowedProperties();
67 ApplicationConverter::~ApplicationConverter()
71 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationInformationPtr &arg)
75 Throw(Commons::InvalidArgumentException);
77 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationInformation::getClassRef(), arg);
80 ApplicationInformationPtr ApplicationConverter::toApplicationInformation(const JSValueRef &jsValue)
82 if(JSApplicationInformation::isObjectOfClass(m_context, jsValue))
83 return JSApplicationInformation::getApplicationInformation(m_context, jsValue);
85 CommonsJavaScript::Validator validator(m_context);
86 if (!validator.checkArrayKeys(m_allowedApplicationInformation, jsValue)) {
87 LogError("invalid properties in contact object");
88 ThrowMsg(Commons::ConversionException, "Wrong attribute");
91 const ScopedJSStringRef nameStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_NAME));
92 const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_APP_ID));
93 const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_ICONF_PATH));
94 const ScopedJSStringRef versionStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_VERSION));
96 JSObjectRef jsObject = toJSObjectRef(jsValue);
98 JSValueRef nameData = JSObjectGetProperty(m_context, jsObject, nameStr.get(), NULL);
99 JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
100 JSValueRef iconPathData = JSObjectGetProperty(m_context, jsObject, iconPathStr.get(), NULL);
101 JSValueRef versionData = JSObjectGetProperty(m_context, jsObject, versionStr.get(), NULL);
105 std::string iconPath;
108 ApplicationInformationPtr result(new ApplicationInformation());
110 Throw(Commons::ConversionException);
113 if (!JSValueIsUndefined(m_context, nameData)) {
114 name = toString(nameData);
115 result->setName(name);
117 if (!JSValueIsUndefined(m_context, appIdStrData)) {
118 appid = toString(appIdStrData);
119 result->setAppId(appid);
121 if (!JSValueIsUndefined(m_context, iconPathData)) {
122 iconPath = toString(iconPathData);
123 result->setIconPath(iconPath);
125 if (!JSValueIsUndefined(m_context, versionData)) {
126 version = toString(versionData);
127 result->setVersion(version);
133 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationInformationArrayPtr &arg)
137 Throw(Commons::InvalidArgumentException);
139 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationInformationArray::getClassRef(), arg);
142 ApplicationInformationArrayPtr ApplicationConverter::toApplicationInformationArray(const JSValueRef &jsValue)
144 if(JSApplicationInformationArray::isObjectOfClass(m_context, jsValue))
145 return JSApplicationInformationArray::getApplicationInformationArray(m_context, jsValue);
147 ApplicationInformationArrayPtr result(new ApplicationInformationArray());
149 JSObjectRef jsObject = toJSObjectRef(jsValue);
150 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
151 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
152 result->push_back(toApplicationInformation(element));
157 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationContextPtr &arg)
161 Throw(Commons::InvalidArgumentException);
163 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationContext::getClassRef(), arg);
167 ApplicationContextPtr ApplicationConverter::toApplicationContext(const JSValueRef &jsValue)
169 if(JSApplicationContext::isObjectOfClass(m_context, jsValue))
170 return JSApplicationContext::getApplicationContext(m_context, jsValue);
172 CommonsJavaScript::Validator validator(m_context);
173 if (!validator.checkArrayKeys(m_allowedApplicationContext, jsValue)) {
174 LogError("invalid properties in contact object");
175 ThrowMsg(Commons::ConversionException, "Wrong attribute");
178 const ScopedJSStringRef appIdStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_APP_ID));
179 const ScopedJSStringRef contextIdStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_CONTEXT_ID));
181 JSObjectRef jsObject = toJSObjectRef(jsValue);
183 JSValueRef appIdStrData = JSObjectGetProperty(m_context, jsObject, appIdStr.get(), NULL);
184 JSValueRef contextIdStrData = JSObjectGetProperty(m_context, jsObject, contextIdStr.get(), NULL);
188 std::string contextid;
190 ApplicationContextPtr result(new ApplicationContext());
192 Throw(Commons::ConversionException);
195 if (!JSValueIsUndefined(m_context, appIdStrData)) {
196 appid = toString(appIdStrData);
197 result->setAppId(appid);
199 if (!JSValueIsUndefined(m_context, contextIdStrData)) {
200 contextid = toString(contextIdStrData);
201 result->setContextId(appid);
207 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationContextArrayPtr &arg)
211 Throw(Commons::InvalidArgumentException);
213 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationContextArray::getClassRef(), arg);
216 ApplicationContextArrayPtr ApplicationConverter::toApplicationContextArray(const JSValueRef &jsValue)
218 if(JSApplicationContextArray::isObjectOfClass(m_context, jsValue))
219 return JSApplicationContextArray::getApplicationContextArray(m_context, jsValue);
221 ApplicationContextArrayPtr result(new ApplicationContextArray());
223 JSObjectRef jsObject = toJSObjectRef(jsValue);
224 for (std::size_t i = 0; i < JSGetArrayLength(m_context, jsObject); ++i) {
225 JSValueRef element = JSGetArrayElement(m_context, jsObject, i);
226 result->push_back(toApplicationContext(element));
231 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationServicePtr &arg)
234 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationService::getClassRef(), arg);
237 ApplicationServicePtr ApplicationConverter::toApplicationService(const JSValueRef &jsValue)
239 if(JSApplicationService::isObjectOfClass(m_context, jsValue))
240 return JSApplicationService::getApplicationService(m_context, jsValue);
242 CommonsJavaScript::Validator validator(m_context);
243 if (!validator.checkArrayKeys(m_allowedApplicationService, jsValue)) {
244 LogError("invalid properties in ApplicationService object");
245 ThrowMsg(Commons::ConversionException, "Wrong attribute");
248 const ScopedJSStringRef operationStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_OPERATION));
249 const ScopedJSStringRef uriStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_URI));
250 const ScopedJSStringRef mimeStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_MIME));
251 const ScopedJSStringRef extraDataStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_EXTRADATA));
253 JSObjectRef jsObject = toJSObjectRef(jsValue);
255 JSValueRef jsoperation = JSObjectGetProperty(m_context, jsObject, operationStr.get(), NULL);
256 JSValueRef jsuri = JSObjectGetProperty(m_context, jsObject, uriStr.get(), NULL);
257 JSValueRef jsmime = JSObjectGetProperty(m_context, jsObject, mimeStr.get(), NULL);
258 JSValueRef jsextradata = JSObjectGetProperty(m_context, jsObject, extraDataStr.get(), NULL);
260 std::string operation;
263 std::vector<ApplicationServiceDataPtr> extraDataArray;
265 ApplicationServicePtr result(new ApplicationService());
267 Throw(Commons::ConversionException);
270 if (!JSValueIsUndefined(m_context, jsoperation)) {
271 operation = toString(jsoperation);
272 LogDebug("operation : "<<operation);
273 result->setOperation(operation);
276 if (!JSValueIsUndefined(m_context, jsuri)) {
277 uri = toString(jsuri);
281 if (!JSValueIsUndefined(m_context, jsmime)) {
282 mime = toString(jsmime);
283 result->setMime(mime);
286 if (!JSValueIsUndefined(m_context, jsextradata)) {
287 extraDataArray = toApplicationServiceDataArray(jsextradata);
288 result->setServiceDataArray(extraDataArray);
294 JSValueRef ApplicationConverter::toJSValueRef(const ApplicationServiceDataPtr &arg)
297 return CommonsJavaScript::JSUtils::makeObject(m_context, JSApplicationServiceData::getClassRef(), arg);
300 ApplicationServiceDataPtr ApplicationConverter::toApplicationServiceData(const JSValueRef &jsValue)
302 if(JSApplicationServiceData::isObjectOfClass(m_context, jsValue))
303 return JSApplicationServiceData::getApplicationServiceData(m_context, jsValue);
305 CommonsJavaScript::Validator validator(m_context);
306 if (!validator.checkArrayKeys(m_allowedApplicationServiceData, jsValue)) {
307 LogError("invalid properties in contact object");
308 ThrowMsg(Commons::ConversionException, "Wrong attribute");
311 const ScopedJSStringRef serviceDataKeyStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_EXTRA_KEY));
312 const ScopedJSStringRef serviceDataValueStr(JSStringCreateWithUTF8CString(APPLICATOIN_ATTRIBUTE_EXTRA_VALUE));
314 JSObjectRef jsObject = toJSObjectRef(jsValue);
316 JSValueRef serviceDataKeyData = JSObjectGetProperty(m_context, jsObject, serviceDataKeyStr.get(), NULL);
317 JSValueRef serviceDataValueData = JSObjectGetProperty(m_context, jsObject, serviceDataValueStr.get(), NULL);
319 ApplicationServiceDataPtr result(new ApplicationServiceData());
321 Throw(Commons::ConversionException);
324 if (!JSValueIsUndefined(m_context, serviceDataKeyData)) {
325 result->setKey(toString(serviceDataKeyData));
328 if (!JSValueIsUndefined(m_context, serviceDataValueData)) {
329 result->setValue(toVectorOfStrings(serviceDataValueData));
334 JSValueRef ApplicationConverter::toJSValueRef(const std::vector<ApplicationServiceDataPtr> &arg)
336 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
338 if (NULL == jsResult) {
339 ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
340 "Could not create js array object");
343 for (std::size_t i = 0; i < arg.size(); ++i) {
344 JSValueRef tmpVal = toJSValueRef(arg[i]);
345 if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
346 ThrowMsg(WrtDeviceApis::Commons::UnknownException,
347 "Could not insert value into js array");
354 std::vector<ApplicationServiceDataPtr> ApplicationConverter::toApplicationServiceDataArray(const JSValueRef &jsValue)
356 //return WrtDeviceApis::CommonsJavaScript::Converter::toVectorOfT_(jsValue, &ApplicationConverter::toApplicationServiceData);
357 if (JSValueIsNull(m_context,
358 jsValue) || JSValueIsUndefined(m_context, jsValue)) {
359 return std::vector<ApplicationServiceDataPtr>();
362 if (!JSIsArrayValue(m_context, jsValue)) {
363 ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
366 std::vector<ApplicationServiceDataPtr> result;
367 JSObjectRef objArg = toJSObjectRef(jsValue);
368 for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
369 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
370 result.push_back(toApplicationServiceData(element));
375 bool ApplicationConverter::initializeAllowedProperties()
377 m_allowedApplicationInformation.push_back(APPLICATOIN_ATTRIBUTE_NAME);
378 m_allowedApplicationInformation.push_back(APPLICATOIN_ATTRIBUTE_APP_ID);
379 m_allowedApplicationInformation.push_back(APPLICATOIN_ATTRIBUTE_ICONF_PATH);
380 m_allowedApplicationInformation.push_back(APPLICATOIN_ATTRIBUTE_VERSION);
382 m_allowedApplicationService.push_back(APPLICATOIN_ATTRIBUTE_OPERATION);
383 m_allowedApplicationService.push_back(APPLICATOIN_ATTRIBUTE_URI);
384 m_allowedApplicationService.push_back(APPLICATOIN_ATTRIBUTE_MIME);
385 m_allowedApplicationService.push_back(APPLICATOIN_ATTRIBUTE_EXTRADATA);
387 // m_allowedApplicationServiceReply.push_back(APPLICATOIN_ATTRIBUTE_EXTRADATA);
389 m_allowedApplicationServiceData.push_back(APPLICATOIN_ATTRIBUTE_EXTRA_KEY);
390 m_allowedApplicationServiceData.push_back(APPLICATOIN_ATTRIBUTE_EXTRA_VALUE);