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.
19 #include "Converter.h"
23 #include <Commons/Exception.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/ScopedJSStringRef.h>
26 #include "FilesystemUtils.h"
28 #include "Encodings.h"
29 #include "JSStorage.h"
33 const char* PROPERTY_FILEFILTER_NAME = "name";
34 const char* PROPERTY_FILEFILTER_START_CREATED = "startCreated";
35 const char* PROPERTY_FILEFILTER_END_CREATED = "endCreated";
36 const char* PROPERTY_FILEFILTER_START_MODIFIED = "startModified";
37 const char* PROPERTY_FILEFILTER_END_MODIFIED = "endModified";
38 const char* ACCESS_MODE_READ = "r";
39 const char* ACCESS_MODE_APPEND = "a";
40 const char* ACCESS_MODE_WRITE = "w";
41 const char* ACCESS_MODE_READ_WRITE = "rw";
42 const char* STORAGE_TYPE_STATE_MOUNTED = "MOUNTED";
43 const char* STORAGE_TYPE_STATE_REMOVED = "REMOVED";
44 const char* STORAGE_TYPE_STATE_UNMOUNTABLE = "REMOVED";
45 const char* STORAGE_TYPE_INTERNAL = "INTERNAL";
46 const char* STORAGE_TYPE_EXTERNAL = "EXTERNAL";
49 const char* ENCODINGS[] = {
50 DeviceAPI::Filesystem::Encodings::UTF8,
51 DeviceAPI::Filesystem::Encodings::ISO88591,
52 DeviceAPI::Filesystem::Encodings::SJIS,
57 using namespace WrtDeviceApis;
58 using namespace WrtDeviceApis::Commons;
59 using namespace WrtDeviceApis::CommonsJavaScript;
63 Converter::Converter(JSContextRef context) : WrtDeviceApis::CommonsJavaScript::Converter(context)
67 JSValueRef Converter::toJSValueRef(
69 const JSFile::PrivateObjectDef::PermissionList &parentPermissions,
70 JSContextRef context, DeviceAPI::Common::SecurityAccessor *securityAccessor)
72 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
74 ThrowMsg(Commons::ConversionException,
75 "Could not create js array object");
78 for (std::size_t i = 0; i < arg.size(); ++i)
80 JSFile::PrivateObjectDefPtr privData(
81 new JSFile::PrivateObjectDef(
84 JSFile::PrivateObject* privateObject = new JSFile::PrivateObject(
89 privateObject->copyAceCheckAccessFunction(securityAccessor);
91 JSObjectRef jsObject = JSObjectMake(m_context,
92 JSFile::getClassRef(),
96 ThrowMsg(Commons::ConversionException,
97 "Could not create JS object.");
99 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
100 ThrowMsg(Commons::ConversionException,
101 "Could not insert value into js array");
109 JSValueRef Converter::toJSValueRef(unsigned char* data, std::size_t num)
111 JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
113 ThrowMsg(Commons::ConversionException, "Could not create array object.");
116 for (std::size_t i = 0; i < num; ++i) {
117 JSValueRef value = JSValueMakeNumber(m_context, data[i]);
118 if (!JSSetArrayElement(m_context, result, i, value)) {
119 ThrowMsg(Commons::ConversionException, "Could not fill array.");
126 JSValueRef Converter::toJSValueRef(unsigned long long arg)
128 return JSValueMakeNumber(m_context, arg);
133 IPathPtr Converter::toPath(const JSValueRef& arg)
136 std::string path = toString(arg);
137 if (!Utils::isPathValid(path)) {
138 ThrowMsg(Commons::InvalidArgumentException, "Invalid path component.");
140 return IPath::create(path);
141 } Catch (Commons::ConversionException) {
142 ReThrowMsg(Commons::ConversionException, "Not a valid path.");
145 std::string Converter::checkPercentSign(std::string& arg)
151 if (pos == std::string::npos)
155 else if (arg[arg.size() - 1] == '%')
157 arg[arg.size() - 1] = ')';
171 ThrowMsg(Commons::ConversionException, "Invalid Filter");
174 NodeFilterPtr Converter::toNodeFilter(const JSValueRef& arg)
176 JSObjectRef filter = toJSObjectRef(arg);
178 NodeFilterPtr result(new NodeFilter());
179 JSValueRef prop = NULL;
180 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_NAME);
182 std::string nameFilter = toString(prop);
183 result->setName(checkPercentSign(nameFilter));
186 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_CREATED);
188 result->setMinCreated(toDateTimeT(prop));
191 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_CREATED);
193 result->setMaxCreated(toDateTimeT(prop));
196 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_START_MODIFIED);
198 result->setMinModified(toDateTimeT(prop));
201 prop = JSUtils::getJSProperty(m_context, filter, PROPERTY_FILEFILTER_END_MODIFIED);
203 result->setMaxModified(toDateTimeT(prop));
209 AccessMode Converter::toAccessMode(const JSValueRef& arg)
211 std::string mode = toString_(arg);
212 if (ACCESS_MODE_READ == mode) {
214 } else if (ACCESS_MODE_APPEND == mode) {
216 } else if (ACCESS_MODE_WRITE == mode) {
218 } else if (ACCESS_MODE_READ_WRITE == mode) {
219 return AM_READ_WRITE;
222 ThrowMsg(Commons::ConversionException, "Invalid mode.");
225 std::string Converter::toEncoding(const JSValueRef& arg)
227 std::string result = toString_(arg);
228 const char** ptr = ENCODINGS;
230 if (result == *ptr) {
235 ThrowMsg(Commons::ConversionException, "Invalid encoding");
237 JSValueRef Converter::toStorageState(const short type)
241 case JSStorage::STATE_MOUNTED:
242 return toJSValueRef(STORAGE_TYPE_STATE_MOUNTED);
243 case JSStorage::STATE_REMOVED:
244 return toJSValueRef(STORAGE_TYPE_STATE_REMOVED);
245 case JSStorage::STATE_UNMOUNTABLE:
246 return toJSValueRef(STORAGE_TYPE_STATE_UNMOUNTABLE);
248 ThrowMsg(Commons::ConversionException, "Invalid storage type");
251 JSValueRef Converter::toStorageType(const short state)
255 case StorageProperties::TYPE_INTERNAL:
256 return toJSValueRef(STORAGE_TYPE_INTERNAL);
257 case StorageProperties::TYPE_EXTERNAL:
258 return toJSValueRef(STORAGE_TYPE_EXTERNAL);
260 ThrowMsg(Commons::ConversionException, "Invalid storage state");
263 JSValueRef Converter::toJSValueRef(
264 const StoragePropertiesPtr &arg,
265 JSContextRef context)
267 StorageProperties tmpStorage;
269 tmpStorage.setLabel(arg->getLabel());
270 tmpStorage.setType(arg->getType());
272 switch (arg->getState()) {
273 case StorageProperties::STATE_MOUNTED :
274 case StorageProperties::STATE_MOUNTED_READONLY :
275 tmpStorage.setState(JSStorage::STATE_MOUNTED);
277 case StorageProperties::STATE_REMOVED:
278 tmpStorage.setState(JSStorage::STATE_REMOVED);
280 case StorageProperties::STATE_UNMOUNTABLE:
281 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
285 JSObjectRef jsObject = JSStorage::createJSObject(context, tmpStorage);
287 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
290 return toJSValueRef(jsObject);
293 JSValueRef Converter::toJSValueRef(
294 const std::vector<StoragePropertiesPtr>& arg,
295 JSContextRef context)
297 JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
299 ThrowMsg(Commons::ConversionException, "Could not create js array object");
302 StorageProperties tmpStorage;
304 for (size_t i = 0; i < arg.size(); i++) {
305 tmpStorage.setLabel(arg[i]->getLabel());
306 tmpStorage.setType(arg[i]->getType());
308 switch (arg[i]->getState()) {
309 case StorageProperties::STATE_MOUNTED :
310 case StorageProperties::STATE_MOUNTED_READONLY :
311 tmpStorage.setState(JSStorage::STATE_MOUNTED);
313 case StorageProperties::STATE_REMOVED:
314 tmpStorage.setState(JSStorage::STATE_REMOVED);
316 case StorageProperties::STATE_UNMOUNTABLE:
317 tmpStorage.setState(JSStorage::STATE_UNMOUNTABLE);
321 JSObjectRef jsObject = JSObjectMake(m_context, NULL, NULL);
322 const ScopedJSStringRef labelStr(JSStringCreateWithUTF8CString("label"));
323 const ScopedJSStringRef typeStr(JSStringCreateWithUTF8CString("type"));
324 const ScopedJSStringRef stateStr(JSStringCreateWithUTF8CString("state"));
326 JSObjectSetProperty(m_context, jsObject, labelStr.get(), toJSValueRef(tmpStorage.getLabel()), kJSPropertyAttributeReadOnly, NULL);
327 JSObjectSetProperty(m_context, jsObject, typeStr.get(), toStorageType(tmpStorage.getType()), kJSPropertyAttributeReadOnly, NULL);
328 JSObjectSetProperty(m_context, jsObject, stateStr.get(), toStorageState(tmpStorage.getState()), kJSPropertyAttributeReadOnly, NULL);
331 ThrowMsg(Commons::ConversionException, "Could not create JS object.");
334 if (!JSSetArrayElement(m_context, jsResult, i, jsObject)) {
335 ThrowMsg(Commons::ConversionException, "Could not insert value into js array");