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.
24 #include <Commons/FunctionDeclaration.h>
25 #include <Commons/Exception.h>
26 #include <Commons/WrtAccess/WrtAccess.h>
29 #include "EventCopy.h"
30 #include "EventMove.h"
31 #include "EventListNodes.h"
32 #include "EventOpen.h"
33 #include "EventReadText.h"
34 #include <CommonsJavaScript/JSCallbackManager.h>
35 #include <CommonsJavaScript/Validator.h>
36 #include <CommonsJavaScript/JSUtils.h>
37 #include <CommonsJavaScript/Utils.h>
38 #include <JSTizenExceptionFactory.h>
39 #include <JSTizenException.h>
40 #include <SecurityExceptions.h>
41 #include <ArgumentValidator.h>
42 #include <JSWebAPIError.h>
43 #include <JSWebAPIException.h>
46 #include <TimeTracer.h>
47 #include "FilesystemUtils.h"
48 #include "Converter.h"
49 #include "plugin_config.h"
50 #include "Encodings.h"
51 #include "JSFilestream.h"
52 #include "ResponseDispatcher.h"
53 #include "FilesystemAsyncCallbackManager.h"
56 using namespace WrtDeviceApis::Commons;
57 using namespace WrtDeviceApis::CommonsJavaScript;
58 using namespace DeviceAPI::Common;
61 #define PLUGIN_NAME "File"
62 #define PROPERTY_PARENT "parent"
63 #define PROPERTY_READ_ONLY "readOnly"
64 #define PROPERTY_IS_FILE "isFile"
65 #define PROPERTY_IS_DIRECTORY "isDirectory"
66 #define PROPERTY_CREATED "created"
67 #define PROPERTY_MODIFIED "modified"
68 #define PROPERTY_PATH "path"
69 #define PROPERTY_NAME "name"
70 #define PROPERTY_FULL_PATH "fullPath"
71 #define PROPERTY_FILE_SIZE "fileSize"
72 #define PROPERTY_LENGTH "length"
75 JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
77 if (Validator(ctx).isCallback(arg)) {
79 } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
80 ThrowMsg(ConversionException, "Not a function nor JS null.");
87 JSValueRef getFunction(JSContextRef ctx, JSValueRef arg)
89 if (Validator(ctx).isCallback(arg)) {
92 ThrowMsg(ConversionException, "Not a function nor JS null.");
97 namespace Filesystem {
99 JSClassRef JSFile::m_classRef = 0;
101 JSClassDefinition JSFile::m_classInfo = {
103 kJSClassAttributeNone,
121 JSStaticValue JSFile::m_properties[] = {
122 { PROPERTY_PARENT, getProperty, NULL, kJSPropertyAttributeReadOnly },
123 { PROPERTY_READ_ONLY, getProperty, NULL, kJSPropertyAttributeReadOnly },
124 { PROPERTY_IS_FILE, getProperty, NULL, kJSPropertyAttributeReadOnly },
125 { PROPERTY_IS_DIRECTORY, getProperty, NULL, kJSPropertyAttributeReadOnly },
126 { PROPERTY_CREATED, getProperty, NULL, kJSPropertyAttributeReadOnly },
127 { PROPERTY_MODIFIED, getProperty, NULL, kJSPropertyAttributeReadOnly },
128 { PROPERTY_PATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
129 { PROPERTY_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
130 { PROPERTY_FULL_PATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
131 { PROPERTY_FILE_SIZE, getProperty, NULL, kJSPropertyAttributeReadOnly },
132 { PROPERTY_LENGTH, getProperty, NULL, kJSPropertyAttributeReadOnly },
136 JSStaticFunction JSFile::m_functions[] = {
137 { "toURI", toUri, kJSPropertyAttributeNone },
138 { "listFiles", listFiles, kJSPropertyAttributeNone },
139 { "openStream", openStream, kJSPropertyAttributeNone },
140 { "readAsText", readAsText, kJSPropertyAttributeNone },
141 { "copyTo", copyTo, kJSPropertyAttributeNone },
142 { "moveTo", moveTo, kJSPropertyAttributeNone },
143 { "createDirectory", createDirectory, kJSPropertyAttributeNone },
144 { "createFile", createFile, kJSPropertyAttributeNone },
145 { "resolve", resolve, kJSPropertyAttributeNone },
146 { "deleteDirectory", deleteDirectory, kJSPropertyAttributeNone },
147 { "deleteFile", deleteFile, kJSPropertyAttributeNone },
151 void JSFile::initialize(JSContextRef context,
156 void JSFile::finalize(JSObjectRef object)
158 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(object));
160 JSObjectSetPrivate(object, NULL);
161 delete privateObject;
165 const JSClassRef JSFile::getClassRef()
168 m_classRef = JSClassCreate(&m_classInfo);
173 const JSClassDefinition* JSFile::getClassInfo()
178 JSValueRef JSFile::getProperty(JSContextRef context,
180 JSStringRef propertyName,
181 JSValueRef* exception)
183 PrivateObject* privateObject =
184 static_cast<PrivateObject*>(JSObjectGetPrivate(object));
185 if (!privateObject) {
186 LoggerE("Private object is not set.");
187 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
190 JSContextRef globalContext = privateObject->getContext();
191 Converter converter(globalContext);
194 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_PARENT))
196 INodePtr parent(privateObject->getObject()->getNode()->getParent());
197 PrivateObjectDef::PermissionList perms =
198 privateObject->getObject()->getParentPermissions();
199 if (parent && !perms.empty())
201 parent->setPermissions(perms.back());
203 PrivateObjectDefPtr privData(new PrivateObjectDef(parent, perms));
205 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
207 return JSValueMakeNull(context);
209 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_READ_ONLY))
211 bool readOnly = ((privateObject->getObject()->getNode()->getMode() & PM_USER_WRITE) == 0);
212 return converter.toJSValueRef(readOnly);
214 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_IS_FILE))
216 bool isFile = (privateObject->getObject()->getNode()->getType() == NT_FILE);
217 return converter.toJSValueRef(isFile);
219 else if (JSStringIsEqualToUTF8CString(propertyName,
220 PROPERTY_IS_DIRECTORY))
222 bool isDirectory = (privateObject->getObject()->getNode()->getType() == NT_DIRECTORY);
223 return converter.toJSValueRef(isDirectory);
225 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CREATED))
227 std::time_t created = privateObject->getObject()->getNode()->getCreated();
228 return converter.toJSValueRef(created);
230 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_MODIFIED))
232 std::time_t modified = privateObject->getObject()->getNode()->getModified();
233 return converter.toJSValueRef(modified);
235 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_PATH))
237 std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
238 std::string vpath = Utils::toVirtualPath(globalContext, fpath);
239 std::string::size_type pos = vpath.rfind(IPath::getSeparator());
240 std::string path = (std::string::npos != pos ? vpath.substr(0, pos + 1) : vpath);
241 return converter.toJSValueRef(path);
243 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAME))
245 std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
246 std::string vpath = Utils::toVirtualPath(globalContext, fpath);
248 std::string::size_type pos = vpath.rfind(IPath::getSeparator());
250 if (std::string::npos != pos) {
251 name = vpath.substr(pos + 1);
253 return converter.toJSValueRef(name);
255 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FULL_PATH))
257 std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
258 return converter.toJSValueRef(Utils::toVirtualPath(globalContext, path));
260 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FILE_SIZE))
262 if (privateObject->getObject()->getNode()->getType() == NT_DIRECTORY)
264 return JSValueMakeUndefined(context);
266 return converter.toJSValueRef(privateObject->getObject()->getNode()->getSize());
268 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_LENGTH))
270 if (privateObject->getObject()->getNode()->getType() == NT_FILE) {
271 return JSValueMakeUndefined(context);
273 // TODO: think about more efficent solution!
274 NodeList children = privateObject->getObject()->getNode()->getChildNodes();
275 return converter.toJSValueRef(children.size());
278 catch (const WrtDeviceApis::Commons::Exception& ex) {
279 LoggerE("Exception: " << ex.GetMessage());
282 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_READ_ONLY)) {
283 bool readOnly = ((privateObject->getObject()->getNode() & PERM_WRITE) == 0);
284 return converter.toJSValueRef(readOnly);
285 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_IS_FILE)) {
286 bool isFile = (privateObject->getObject()->getNode()->getType() == NT_FILE);
287 return converter.toJSValueRef(isFile);
288 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_IS_DIRECTORY)) {
289 bool isDirectory =(privateObject->getObject()->getNode()->getType() == NT_DIRECTORY);
290 return converter.toJSValueRef(isDirectory);
291 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CREATED)) {
292 std::time_t created = privateObject->getObject()->getNode()->getCreated();
293 return converter.toJSValueRef(created);
294 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_MODIFIED)) {
295 std::time_t modified = privateObject->getObject()->getNode()->getModified();
296 return converter.toJSValueRef(modified);
297 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_PATH)) {
298 std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
299 std::string vpath = Utils::toVirtualPath(globalContext, fpath);
300 std::string::size_type pos = vpath.rfind(IPath::getSeparator());
301 std::string path = (std::string::npos != pos ? vpath.substr(0, pos + 1) : vpath);
302 return converter.toJSValueRef(path);
303 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAME)) {
304 std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
305 std::string vpath = Utils::toVirtualPath(globalContext, fpath);
307 std::string::size_type pos = vpath.rfind(IPath::getSeparator());
308 if (std::string::npos != pos) {
309 name = vpath.substr(pos + 1);
311 return converter.toJSValueRef(name);
312 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FULL_PATH)) {
313 std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
314 return converter.toJSValueRef(Utils::toVirtualPath(globalContext, path));
315 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FILE_SIZE)) {
316 if (privateObject->getObject()->getNode()->getType() == NT_DIRECTORY) {
317 return JSValueMakeUndefined(context);
319 return converter.toJSValueRef(privateObject->getObject()->getNode()->getSize());
320 } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_LENGTH)) {
321 if (privateObject->getObject()->getNode()->getType() == NT_FILE) {
322 return JSValueMakeUndefined(context);
324 NodeList children = privateObject->getObject()->getNode()->getChildNodes();
325 return converter.toJSValueRef(children.size());
327 } catch (const WrtDeviceApis::Commons::Exception& ex) {
328 LoggerW("trying to get incorrect value");
331 return JSValueMakeUndefined(context);
334 void JSFile::getPropertyNames(JSContextRef context,
336 JSPropertyNameAccumulatorRef propertyNames)
340 bool JSFile::hasInstance(JSContextRef context,
341 JSObjectRef constructor,
342 JSValueRef possibleInstance,
343 JSValueRef* exception)
345 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
348 JSValueRef JSFile::toUri(JSContextRef context,
350 JSObjectRef thisObject,
352 const JSValueRef argv[],
353 JSValueRef* exception)
355 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
356 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
357 if (!privateObject) {
358 LoggerE("Private object is not set.");
359 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
362 Converter converter(context);
364 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_TO_URI);
365 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
369 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
370 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
371 return converter.toJSValueRef(privateObject->getObject()->getNode()->toUri(widgetId));
372 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
373 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
374 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
375 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
376 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
377 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
378 } catch(const WrtDeviceApis::Commons::Exception& ex) {
379 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
383 JSValueRef JSFile::listFiles(JSContextRef context,
385 JSObjectRef thisObject,
387 const JSValueRef argv[],
388 JSValueRef* exception)
390 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
391 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
392 if (!privateObject) {
393 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
398 // argument validation with new validator
400 ArgumentValidator validator(context, argc, argv);
401 validator.toFunction(0, false);
402 validator.toFunction(1, true);
403 validator.toObject(2, true);
405 catch (const BasePlatformException &err) {
406 return JSWebAPIException::throwException(context, exception, err);
408 DeviceAPI::Common::UnknownException err("");
409 return JSWebAPIException::throwException(context, exception, err);
412 JSContextRef globalContext = privateObject->getContext();
414 JSValueRef reserveArguments[3];
415 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
419 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_LIST_FILES);
420 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
422 for (index = 0; index < 3; index++)
425 reserveArguments[index] = argv[index];
427 reserveArguments[index] = JSValueMakeUndefined(context);
430 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[0]);
431 JSValueRef onError = NULL;
435 onError = getFunctionOrNull(globalContext, reserveArguments[1]);
438 cbm->setOnSuccess(onSuccess);
439 cbm->setOnError(onError);
440 cbm->setObject(thisObject);
442 Converter converter(globalContext);
444 EventListNodesPtr event(new EventListNodes(privateObject->getObject()->getNode()));
446 if (JSValueIsNull(context, reserveArguments[2]) == false &&
447 JSValueIsUndefined(context, reserveArguments[2]) == false) {
448 event->setFilter(converter.toNodeFilter(reserveArguments[2]));
451 PrivateObjectDef::PermissionList perms =
452 privateObject->getObject()->getParentPermissions();
453 perms.push_back(privateObject->getObject()->getNode()->getPermissions());
455 ListFilesPrivateDataPtr privData(new ListFilesPrivateData(cbm, perms));
457 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
458 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
460 privateObject->getObject()->getNode()->getChildNodes(event);
461 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
464 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
465 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
466 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
467 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
468 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
469 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
470 } catch(const WrtDeviceApis::Commons::Exception& ex) {
471 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
473 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
474 return JSValueMakeUndefined(context);
477 JSValueRef JSFile::openStream(JSContextRef context,
479 JSObjectRef thisObject,
480 size_t argumentCount,
481 const JSValueRef arguments[],
482 JSValueRef* exception)
484 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
485 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
486 if (!privateObject) {
487 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
490 // argument validation with new validator
492 ArgumentValidator validator(context, argumentCount, arguments);
493 validator.toFunction(1, false);
494 validator.toFunction(2, true);
496 catch (const BasePlatformException &err) {
497 return JSWebAPIException::throwException(context, exception, err);
499 DeviceAPI::Common::UnknownException err("");
500 return JSWebAPIException::throwException(context, exception, err);
503 JSContextRef globalContext = privateObject->getContext();
504 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
507 JSValueRef reserveArguments[4];
509 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_OPEN_STREAM);
510 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
515 for (index = 0; index < 4; index++) {
516 if (index < argumentCount)
517 reserveArguments[index] = arguments[index];
519 reserveArguments[index] = JSValueMakeUndefined(context);
523 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[1]);
524 JSValueRef onError = NULL;
526 onError = getFunctionOrNull(globalContext, reserveArguments[2]);
528 cbm->setOnSuccess(onSuccess);
529 cbm->setOnError(onError);
530 cbm->setObject(thisObject);
533 Converter converter(globalContext);
535 AccessMode mode = converter.toAccessMode(reserveArguments[0]);
536 std::string encoding = Encodings::UTF8;
537 if (argumentCount > 3) {
538 if(!JSValueIsNull(globalContext, reserveArguments[3]) && !JSValueIsUndefined(globalContext, reserveArguments[3])){
539 encoding = converter.toEncoding(reserveArguments[3]);
543 if ((AM_READ != mode) && (PERM_READ == privateObject->getObject()->getNode()->getPermissions())) {
544 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
547 std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
548 std::string vpath = Utils::toVirtualPath(globalContext, path);
550 AccessModeInfo am = AccessModeInfo(mode, vpath);
551 status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_OPEN_STREAM, am);
553 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
555 EventOpenPtr event(new EventOpen(mode));
556 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(cbm));
557 event->setCharSet(encoding);
558 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
559 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
561 privateObject->getObject()->getNode()->open(event);
564 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
565 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
566 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
567 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
568 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
569 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
570 } catch(const WrtDeviceApis::Commons::Exception& ex) {
571 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
573 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
574 return JSValueMakeUndefined(context);
577 JSValueRef JSFile::readAsText(JSContextRef context,
579 JSObjectRef thisObject,
581 const JSValueRef argv[],
582 JSValueRef* exception)
584 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
585 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
586 if (!privateObject) {
587 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
591 // argument validation with new validator
593 ArgumentValidator validator(context, argc, argv);
594 validator.toFunction(0, false);
595 validator.toFunction(1, true);
597 catch (const BasePlatformException &err) {
598 return JSWebAPIException::throwException(context, exception, err);
600 DeviceAPI::Common::UnknownException err("");
601 return JSWebAPIException::throwException(context, exception, err);
605 JSContextRef globalContext = privateObject->getContext();
606 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_READ_AS_TEXT);
607 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
610 JSValueRef reserveArguments[3];
611 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
615 for (index = 0; index < 3; index++) {
617 reserveArguments[index] = argv[index];
619 reserveArguments[index] = JSValueMakeUndefined(context);
624 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[0]);
625 JSValueRef onError = NULL;
628 onError = getFunctionOrNull(globalContext, reserveArguments[1]);
631 cbm->setOnSuccess(onSuccess);
632 cbm->setOnError(onError);
633 cbm->setObject(thisObject);
635 Converter converter(context);
636 std::string src = Encodings::UTF8;
638 if (!JSValueIsNull(context, reserveArguments[2]) && !JSValueIsUndefined(context, reserveArguments[2])) {
639 src = converter.toEncoding(reserveArguments[2]);
643 if (NT_FILE != privateObject->getObject()->getNode()->getType()) {
644 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
645 return JSValueMakeUndefined(context);
648 EventReadTextPtr event(new EventReadText());
649 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData,JSCallbackManager>(cbm));
650 event->setCharSet(src);
651 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
652 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
654 privateObject->getObject()->getNode()->read(event);
656 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
657 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
658 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
659 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
660 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
661 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
662 } catch(const WrtDeviceApis::Commons::Exception& ex) {
663 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
665 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
666 return JSValueMakeUndefined(context);
669 JSValueRef JSFile::copyTo(JSContextRef context,
671 JSObjectRef thisObject,
673 const JSValueRef argv[],
674 JSValueRef* exception)
676 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
677 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
678 if (!privateObject) {
679 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
682 JSContextRef globalContext = privateObject->getContext();
684 JSValueRef reserveArguments[5];
685 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
687 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_COPY_TO);
688 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
690 // argument validation with new validator
692 ArgumentValidator validator(context, argc, argv);
693 validator.toFunction(3, true);
694 validator.toFunction(4, true);
696 catch (const BasePlatformException &err) {
697 return JSWebAPIException::throwException(context, exception, err);
699 DeviceAPI::Common::UnknownException err("");
700 return JSWebAPIException::throwException(context, exception, err);
706 for (index = 0; index < 5; index++)
709 reserveArguments[index] = argv[index];
711 reserveArguments[index] = JSValueMakeUndefined(context);
714 Converter converter(globalContext);
715 JSValueRef onSuccess = NULL;
716 JSValueRef onError = NULL;
718 onSuccess = getFunctionOrNull(globalContext, reserveArguments[3]);
719 onError = getFunctionOrNull(globalContext, reserveArguments[4]);
721 cbm->setOnSuccess(onSuccess);
722 cbm->setOnError(onError);
723 cbm->setObject(thisObject);
726 //TODO add check validation for src, dest string.
727 IPathPtr src = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
728 IPathPtr dest = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[1]));
729 bool overwrite = converter.toBool(reserveArguments[2]);;
731 if (NT_DIRECTORY != privateObject->getObject()->getNode()->getType()) {
732 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
733 return JSValueMakeUndefined(context);
736 std::string virtualDestPath = Utils::toVirtualPath(globalContext, dest->getFullPath());
737 AccessModeInfo amode = AccessModeInfo(AM_WRITE, virtualDestPath);
738 status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(
739 FILESYSTEM_FUNCTION_API_COPY_TO,
742 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
744 EventCopyPtr event(new EventCopy(src, dest));
747 event->setOptions(OPT_OVERWRITE);
750 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::
754 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
755 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
757 IManager::getInstance().copy(event);
758 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
759 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
760 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
761 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
762 } catch(const WrtDeviceApis::Commons::NotFoundException &ex) {
763 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
764 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
765 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
766 } catch(const WrtDeviceApis::Commons::Exception& ex) {
767 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
770 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
771 return JSValueMakeUndefined(context);
774 JSValueRef JSFile::moveTo(JSContextRef context,
776 JSObjectRef thisObject,
778 const JSValueRef argv[],
779 JSValueRef* exception)
781 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
782 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
783 if (!privateObject) {
784 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
787 JSContextRef globalContext = privateObject->getContext();
791 // argument validation with new validator
793 ArgumentValidator validator(context, argc, argv);
794 validator.toFunction(3, true);
795 validator.toFunction(4, true);
797 catch (const BasePlatformException &err) {
798 return JSWebAPIException::throwException(context, exception, err);
800 DeviceAPI::Common::UnknownException err("");
801 return JSWebAPIException::throwException(context, exception, err);
805 JSValueRef reserveArguments[5];
806 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
808 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_MOVE_TO);
809 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
813 for (index = 0; index < 5; index++) {
815 reserveArguments[index] = argv[index];
817 reserveArguments[index] = JSValueMakeUndefined(context);
820 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
821 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
824 Converter converter(context);
825 JSValueRef onSuccess = NULL;
826 JSValueRef onError = NULL;
828 onSuccess = getFunctionOrNull(globalContext, reserveArguments[3]);
829 onError = getFunctionOrNull(globalContext, reserveArguments[4]);
830 cbm->setOnSuccess(onSuccess);
831 cbm->setOnError(onError);
832 cbm->setObject(thisObject);
834 if (NT_DIRECTORY != privateObject->getObject()->getNode()->getType()) {
835 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "io error"));
836 return JSValueMakeUndefined(context);
839 IPathPtr src = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
840 IPathPtr dest = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[1]));
841 bool overwrite = converter.toBool(reserveArguments[2]);
843 std::string virtualDestPath = Utils::toVirtualPath(globalContext, dest->getFullPath());
845 AccessModeInfo amode = AccessModeInfo(AM_WRITE, virtualDestPath);
846 AceSecurityStatus status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_MOVE_TO, amode);
848 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
850 EventMovePtr event(new EventMove(src, dest));
852 event->setOptions(OPT_OVERWRITE);
855 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
856 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
857 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
859 IManager::getInstance().move(event);
861 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
862 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
863 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
864 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
865 } catch(const WrtDeviceApis::Commons::NotFoundException &ex) {
866 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
868 catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
869 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
870 } catch(const WrtDeviceApis::Commons::Exception& ex) {
871 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
873 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
874 return JSValueMakeUndefined(context);
877 JSValueRef JSFile::createDirectory(JSContextRef context,
879 JSObjectRef thisObject,
881 const JSValueRef argv[],
882 JSValueRef* exception)
884 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
885 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
886 if (!privateObject) {
887 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
890 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_CREATE_DIR);
891 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
894 Converter converter(context);
898 ThrowMsg(InvalidArgumentException, "Invalid path name");
901 IPathPtr path = converter.toPath(argv[0]);
902 INodePtr node(privateObject->getObject()->getNode()->createChild(path, NT_DIRECTORY, OPT_RECURSIVE));
903 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
905 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
906 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
908 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
909 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
910 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
911 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
912 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
913 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
914 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
915 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
916 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
917 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
918 } catch(const WrtDeviceApis::Commons::Exception& ex) {
919 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
923 JSValueRef JSFile::createFile(JSContextRef context,
925 JSObjectRef thisObject,
927 const JSValueRef argv[],
928 JSValueRef* exception)
930 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
931 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
932 if (!privateObject) {
933 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
936 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_CREATE_FILE);
937 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
940 Converter converter(context);
944 ThrowMsg(InvalidArgumentException, "Invalid path name");
947 IPathPtr path = converter.toPath(argv[0]);
948 INodePtr node = privateObject->getObject()->getNode()->createChild(path, NT_FILE);
950 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
951 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
952 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
953 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
955 IPathPtr path = converter.toPath(argv[0]);
956 INodePtr node = privateObject->getObject()->getNode()->createChild(path, NT_FILE);
957 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), node);*/
958 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
959 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
960 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
961 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
962 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
963 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
964 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
965 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
966 } catch(const WrtDeviceApis::Commons::Exception& ex) {
967 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
972 JSValueRef JSFile::resolve(JSContextRef context,
974 JSObjectRef thisObject,
976 const JSValueRef argv[],
977 JSValueRef* exception)
979 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
980 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
981 if (!privateObject) {
982 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
985 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_RESOLVE);
986 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
988 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
989 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, "IO error");
992 Converter converter(context);
996 ThrowMsg(InvalidArgumentException, "Invalid path name");
999 IPathPtr path = converter.toPath(argv[0]);
1000 INodePtr node = privateObject->getObject()->getNode()->getChild(path);
1001 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
1002 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
1003 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
1004 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1005 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
1007 IPathPtr path = converter.toPath(argv[0]);
1008 INodePtr node = privateObject->getObject()->getNode()->getChild(path);
1009 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
1010 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), node);
1012 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1013 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
1014 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1015 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1016 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1017 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1018 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1019 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
1020 } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1021 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
1022 } catch(const WrtDeviceApis::Commons::Exception& ex) {
1023 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
1027 JSValueRef JSFile::deleteDirectory(JSContextRef context,
1029 JSObjectRef thisObject,
1031 const JSValueRef argv[],
1032 JSValueRef* exception)
1034 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1035 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
1036 if (!privateObject) {
1037 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
1040 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_DELETE_DIR);
1041 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1044 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
1047 JSContextRef globalContext = privateObject->getContext();
1051 // argument validation with new validator
1053 ArgumentValidator validator(context, argc, argv);
1054 validator.toFunction(2, true);
1055 validator.toFunction(3, true);
1057 catch (const BasePlatformException &err) {
1058 return JSWebAPIException::throwException(context, exception, err);
1060 DeviceAPI::Common::UnknownException err("");
1061 return JSWebAPIException::throwException(context, exception, err);
1065 JSValueRef reserveArguments[4];
1066 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
1069 for (index = 0; index < 4; index++) {
1071 reserveArguments[index] = argv[index];
1073 reserveArguments[index] = JSValueMakeUndefined(context);
1077 JSValueRef onSuccess = NULL;
1078 JSValueRef onError = NULL;
1080 onSuccess = getFunctionOrNull(globalContext, reserveArguments[2]);
1081 onError = getFunctionOrNull(globalContext, reserveArguments[3]);
1083 cbm->setOnSuccess(onSuccess);
1084 cbm->setOnError(onError);
1085 cbm->setObject(thisObject);
1087 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
1088 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
1091 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
1092 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1093 return JSValueMakeUndefined(context);
1096 Converter converter(context);
1097 IPathPtr path = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
1098 bool recursive = converter.toBool(reserveArguments[1]);
1100 if (*privateObject->getObject()->getNode()->getPath() != path->getPath()) {
1101 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, "not found error"));
1102 return JSValueMakeUndefined(context);
1106 EventResolvePtr eventResolve(new EventResolve(path));
1107 if (eventResolve->setForSynchronousCall()) {
1108 IManager::getInstance().getNode(eventResolve);
1109 if (!eventResolve->getResult() || (eventResolve->getExceptionCode() != WrtDeviceApis::Commons::ExceptionCodes::None)) {
1111 if(eventResolve->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException)
1112 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Not found error");
1114 ThrowMsg(PlatformException, "IO Error");
1117 if (eventResolve->getResult()->getType() != NT_DIRECTORY) {
1118 ThrowMsg(InvalidArgumentException, "Invalid directory");
1121 EventRemovePtr eventRemove(new EventRemove(path));
1124 eventRemove->setOptions(OPT_RECURSIVE);
1127 eventRemove->setForAsynchronousCall(&ResponseDispatcher::getInstance());
1128 eventRemove->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1129 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
1131 IManager::getInstance().remove(eventRemove);
1133 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1134 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, ex.GetMessage()));
1136 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1137 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1138 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1139 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1140 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1141 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
1142 } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1143 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
1144 } catch(const WrtDeviceApis::Commons::Exception& ex) {
1145 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
1147 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1148 return JSValueMakeUndefined(context);
1151 JSValueRef JSFile::deleteFile(JSContextRef context,
1153 JSObjectRef thisObject,
1155 const JSValueRef argv[],
1156 JSValueRef* exception)
1159 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
1161 PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
1163 if (!privateObject) {
1164 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
1166 // argument validation with new validator
1168 ArgumentValidator validator(context, argc, argv);
1169 validator.toFunction(1, true);
1170 validator.toFunction(2, true);
1172 catch (const BasePlatformException &err) {
1173 return JSWebAPIException::throwException(context, exception, err);
1175 DeviceAPI::Common::UnknownException err("");
1176 return JSWebAPIException::throwException(context, exception, err);
1179 JSContextRef globalContext = privateObject->getContext();
1182 JSValueRef reserveArguments[3];
1183 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
1185 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_DELETE_FILE);
1186 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1190 for (index = 0; index < 3; index++) {
1192 reserveArguments[index] = argv[index];
1194 reserveArguments[index] = JSValueMakeUndefined(context);
1198 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
1199 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
1202 JSValueRef onSuccess = NULL;
1203 JSValueRef onError = NULL;
1205 onSuccess = getFunctionOrNull(globalContext, reserveArguments[1]);
1206 onError = getFunctionOrNull(globalContext, reserveArguments[2]);
1208 cbm->setOnSuccess(onSuccess);
1209 cbm->setOnError(onError);
1210 cbm->setObject(thisObject);
1212 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
1213 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1214 return JSValueMakeUndefined(context);
1218 Converter converter(context);
1219 IPathPtr path = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
1221 EventResolvePtr eventResolve(new EventResolve(path));
1222 if (eventResolve->setForSynchronousCall()) {
1223 IManager::getInstance().getNode(eventResolve);
1225 if (!eventResolve->getResult() ||(eventResolve->getExceptionCode() != WrtDeviceApis::Commons::ExceptionCodes::None)) {
1226 if(eventResolve->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException){
1227 LoggerD("POST IO NOT_FOUND_ERROR");
1228 ThrowMsg(WrtDeviceApis::Commons::NotFoundException, "Not found error");
1232 LoggerD("POST IO ERROR");
1233 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1234 return JSValueMakeUndefined(context);
1237 if (eventResolve->getResult()->getType() != NT_FILE) {
1238 ThrowMsg(InvalidArgumentException, "Invalid directory");
1241 LoggerD("try to call async event");
1242 EventRemovePtr eventRemove(new EventRemove(path));
1243 eventRemove->setForAsynchronousCall(&ResponseDispatcher::getInstance());
1244 eventRemove->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1245 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
1247 IManager::getInstance().remove(eventRemove);
1250 } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1251 LoggerD("Platform Exception !!!!!!!! post io error");
1252 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, ex.GetMessage()));
1253 } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1254 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1255 } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1256 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1257 } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1258 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
1259 } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1260 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
1261 } catch(const WrtDeviceApis::Commons::Exception& ex) {
1262 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
1266 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
1267 return JSValueMakeUndefined(context);