df67c2d5adedbcc9bb033a09434d9687f78c91c5
[framework/web/wrt-plugins-tizen.git] / src / Filesystem / JSFile.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
19 #include "JSFile.h"
20
21 #include <string>
22 #include <ctime>
23 #include <dpl/log/log.h>
24
25 #include <Commons/FunctionDeclaration.h>
26 #include <Commons/Exception.h>
27 #include <Commons/WrtAccess/WrtAccess.h>
28 #include "Enums.h"
29 #include "IManager.h"
30 #include "EventCopy.h"
31 #include "EventMove.h"
32 #include "EventListNodes.h"
33 #include "EventOpen.h"
34 #include "EventReadText.h"
35 #include <CommonsJavaScript/JSCallbackManager.h>
36 #include <CommonsJavaScript/Validator.h>
37 #include <CommonsJavaScript/JSUtils.h>
38 #include <CommonsJavaScript/Utils.h>
39 #include <JSTizenExceptionFactory.h>
40 #include <JSTizenException.h> 
41 #include <SecurityExceptions.h>
42 #include "FilesystemUtils.h"
43 #include "Converter.h"
44 #include "plugin_config.h"
45 #include "Encodings.h"
46 #include "JSFilestream.h"
47 #include "ResponseDispatcher.h"
48 #include "FilesystemAsyncCallbackManager.h"
49
50 using namespace WrtDeviceApis::Commons;
51 using namespace WrtDeviceApis::CommonsJavaScript;
52 using namespace DeviceAPI::Common;
53
54 namespace {
55 #define PLUGIN_NAME  "File"
56 #define PROPERTY_PARENT  "parent"
57 #define PROPERTY_READ_ONLY  "readOnly"
58 #define PROPERTY_IS_FILE  "isFile"
59 #define PROPERTY_IS_DIRECTORY "isDirectory"
60 #define PROPERTY_CREATED  "created"
61 #define PROPERTY_MODIFIED  "modified"
62 #define PROPERTY_PATH  "path"
63 #define PROPERTY_NAME  "name"
64 #define PROPERTY_FULL_PATH  "fullPath"
65 #define PROPERTY_FILE_SIZE  "fileSize"
66 #define PROPERTY_LENGTH "length"
67 }       //namespace
68
69 JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
70 {
71         if (Validator(ctx).isCallback(arg)) {
72                 return arg;
73         } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
74                 ThrowMsg(ConversionException, "Not a function nor JS null.");
75         }
76         return NULL;
77 }
78
79
80
81 JSValueRef getFunction(JSContextRef ctx, JSValueRef arg)
82 {
83         if (Validator(ctx).isCallback(arg)) {
84                 return arg;
85         } else{
86                 ThrowMsg(ConversionException, "Not a function nor JS null.");
87         }
88 }
89
90 namespace DeviceAPI {
91 namespace Filesystem {
92
93 JSClassRef JSFile::m_classRef = 0;
94
95 JSClassDefinition JSFile::m_classInfo = {
96         0,
97         kJSClassAttributeNone,
98         PLUGIN_NAME,
99         0,
100         m_properties,
101         m_functions,
102         initialize,
103         finalize,
104         NULL,
105         NULL,
106         NULL,
107         NULL,
108         getPropertyNames,
109         NULL,
110         NULL,
111         hasInstance,
112         NULL
113 };
114
115 JSStaticValue JSFile::m_properties[] = {
116         { PROPERTY_PARENT, getProperty, NULL, kJSPropertyAttributeReadOnly },
117         { PROPERTY_READ_ONLY, getProperty, NULL, kJSPropertyAttributeReadOnly },
118         { PROPERTY_IS_FILE, getProperty, NULL, kJSPropertyAttributeReadOnly },
119         { PROPERTY_IS_DIRECTORY, getProperty, NULL, kJSPropertyAttributeReadOnly },
120         { PROPERTY_CREATED, getProperty, NULL, kJSPropertyAttributeReadOnly },
121         { PROPERTY_MODIFIED, getProperty, NULL, kJSPropertyAttributeReadOnly },
122         { PROPERTY_PATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
123         { PROPERTY_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
124         { PROPERTY_FULL_PATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
125         { PROPERTY_FILE_SIZE, getProperty, NULL, kJSPropertyAttributeReadOnly },
126         { PROPERTY_LENGTH, getProperty, NULL, kJSPropertyAttributeReadOnly },
127         { 0, 0, 0, 0 }
128 };
129
130 JSStaticFunction JSFile::m_functions[] = {
131         { "toURI", toUri, kJSPropertyAttributeNone },
132         { "listFiles", listFiles, kJSPropertyAttributeNone },
133         { "openStream", openStream, kJSPropertyAttributeNone },
134         { "readAsText", readAsText, kJSPropertyAttributeNone },
135         { "copyTo", copyTo, kJSPropertyAttributeNone },
136         { "moveTo", moveTo, kJSPropertyAttributeNone },
137         { "createDirectory", createDirectory, kJSPropertyAttributeNone },
138         { "createFile", createFile, kJSPropertyAttributeNone },
139         { "resolve", resolve, kJSPropertyAttributeNone },
140         { "deleteDirectory", deleteDirectory, kJSPropertyAttributeNone },
141         { "deleteFile", deleteFile, kJSPropertyAttributeNone },
142         { 0, 0, 0 }
143 };
144
145 void JSFile::initialize(JSContextRef context,
146                 JSObjectRef object)
147 {
148 }
149
150 void JSFile::finalize(JSObjectRef object)
151 {
152         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(object));
153         if (privateObject) {
154                 JSObjectSetPrivate(object, NULL);
155                 delete privateObject;
156         }
157 }
158
159 const JSClassRef JSFile::getClassRef()
160 {
161         if (!m_classRef) {
162                 m_classRef = JSClassCreate(&m_classInfo);
163         }
164         return m_classRef;
165 }
166
167 const JSClassDefinition* JSFile::getClassInfo()
168 {
169         return &m_classInfo;
170 }
171
172 JSValueRef JSFile::getProperty(JSContextRef context,
173                 JSObjectRef object,
174                 JSStringRef propertyName,
175                 JSValueRef* exception)
176 {
177         PrivateObject* privateObject =
178         static_cast<PrivateObject*>(JSObjectGetPrivate(object));
179         if (!privateObject) {
180                 LogError("Private object is not set.");
181                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
182         }
183
184         JSContextRef globalContext = privateObject->getContext();
185         Converter converter(globalContext);
186
187         try {
188                 if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_PARENT))
189                 {
190                         INodePtr parent(privateObject->getObject()->getNode()->getParent());
191                         PrivateObjectDef::PermissionList perms =
192                         privateObject->getObject()->getParentPermissions();
193                         if (parent && !perms.empty())
194                         {
195                                 parent->setPermissions(perms.back());
196                                 perms.pop_back();
197                                 PrivateObjectDefPtr privData(new PrivateObjectDef(parent, perms));
198
199                                 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
200                         }
201                         return JSValueMakeNull(context);
202                 } 
203                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_READ_ONLY))
204                 {
205                         bool readOnly = ((privateObject->getObject()->getNode()->getMode() & PM_USER_WRITE) == 0);
206                         return converter.toJSValueRef(readOnly);
207                 }
208                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_IS_FILE))
209                 {
210                         bool isFile = (privateObject->getObject()->getNode()->getType() == NT_FILE);
211                         return converter.toJSValueRef(isFile);
212                 }
213                 else if (JSStringIsEqualToUTF8CString(propertyName,
214                           PROPERTY_IS_DIRECTORY))
215                 {
216                         bool isDirectory = (privateObject->getObject()->getNode()->getType() == NT_DIRECTORY);
217                         return converter.toJSValueRef(isDirectory);
218                 }
219                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CREATED))
220                 {
221                         std::time_t created = privateObject->getObject()->getNode()->getCreated();
222                         return converter.toJSValueRef(created);
223                 }
224                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_MODIFIED))
225                 {
226                         std::time_t modified = privateObject->getObject()->getNode()->getModified();
227                         return converter.toJSValueRef(modified);
228                 }
229                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_PATH))
230                 {
231                         std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
232                         std::string vpath = Utils::toVirtualPath(globalContext, fpath);
233                         std::string::size_type pos = vpath.rfind(IPath::getSeparator());
234                         std::string path = (std::string::npos != pos ? vpath.substr(0, pos + 1) : vpath);
235                         return converter.toJSValueRef(path);
236                 }
237                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAME))
238                 {
239                         std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
240                         std::string vpath = Utils::toVirtualPath(globalContext, fpath);
241                         std::string name;
242                         std::string::size_type pos = vpath.rfind(IPath::getSeparator());
243
244                         if (std::string::npos != pos) {
245                                 name = vpath.substr(pos + 1);
246                         }
247                         return converter.toJSValueRef(name);
248                 }
249                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FULL_PATH))
250                 {
251                         std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
252                         return converter.toJSValueRef(Utils::toVirtualPath(globalContext, path));
253                 }
254                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FILE_SIZE))
255                 {
256                         if (privateObject->getObject()->getNode()->getType() == NT_DIRECTORY)
257                         {
258                                 return JSValueMakeUndefined(context);
259                         }
260                         return converter.toJSValueRef(privateObject->getObject()->getNode()->getSize());
261                 }
262                 else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_LENGTH))
263                 {
264                         if (privateObject->getObject()->getNode()->getType() == NT_FILE) {
265                                 return JSValueMakeUndefined(context);
266                         }
267                         // TODO: think about more efficent solution!
268                         NodeList children = privateObject->getObject()->getNode()->getChildNodes();
269                         return converter.toJSValueRef(children.size());
270                 }
271         }
272         catch (const WrtDeviceApis::Commons::Exception& ex) {
273                 LogError("Exception: " << ex.GetMessage());
274         }
275         /*
276         else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_READ_ONLY)) {
277                 bool readOnly = ((privateObject->getObject()->getNode() & PERM_WRITE) == 0);
278                 return converter.toJSValueRef(readOnly);
279         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_IS_FILE)) {
280                 bool isFile = (privateObject->getObject()->getNode()->getType() == NT_FILE);
281                 return converter.toJSValueRef(isFile);
282         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_IS_DIRECTORY)) {
283                 bool isDirectory =(privateObject->getObject()->getNode()->getType() == NT_DIRECTORY);
284                 return converter.toJSValueRef(isDirectory);
285         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_CREATED)) {
286                 std::time_t created = privateObject->getObject()->getNode()->getCreated();
287                 return converter.toJSValueRef(created);
288         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_MODIFIED)) {
289                 std::time_t modified = privateObject->getObject()->getNode()->getModified();
290                 return converter.toJSValueRef(modified);
291         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_PATH)) {
292                 std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
293                 std::string vpath = Utils::toVirtualPath(globalContext, fpath);
294                 std::string::size_type pos = vpath.rfind(IPath::getSeparator());
295                 std::string path = (std::string::npos != pos ? vpath.substr(0, pos + 1) : vpath);
296                 return converter.toJSValueRef(path);
297         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_NAME)) {
298                 std::string fpath = privateObject->getObject()->getNode()->getPath()->getFullPath();
299                 std::string vpath = Utils::toVirtualPath(globalContext, fpath);
300                 std::string name;
301                 std::string::size_type pos = vpath.rfind(IPath::getSeparator());
302                 if (std::string::npos != pos) {
303                         name = vpath.substr(pos + 1);
304                 }
305                 return converter.toJSValueRef(name);
306         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FULL_PATH)) {
307                 std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
308                 return converter.toJSValueRef(Utils::toVirtualPath(globalContext, path));
309         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_FILE_SIZE)) {
310                 if (privateObject->getObject()->getNode()->getType() == NT_DIRECTORY) {
311                         return JSValueMakeUndefined(context);
312                 }
313                 return converter.toJSValueRef(privateObject->getObject()->getNode()->getSize());
314         } else if (JSStringIsEqualToUTF8CString(propertyName, PROPERTY_LENGTH)) {
315                 if (privateObject->getObject()->getNode()->getType() == NT_FILE) {
316                         return JSValueMakeUndefined(context);
317                 }
318                         NodeList children = privateObject->getObject()->getNode()->getChildNodes();
319                         return converter.toJSValueRef(children.size());
320         }
321         } catch (const WrtDeviceApis::Commons::Exception& ex) {
322                 LogWarning("trying to get incorrect value");
323         }*/
324
325         return JSValueMakeUndefined(context);
326 }
327
328 void JSFile::getPropertyNames(JSContextRef context,
329                 JSObjectRef object,
330                 JSPropertyNameAccumulatorRef propertyNames)
331 {
332 }
333
334 bool JSFile::hasInstance(JSContextRef context,
335                 JSObjectRef constructor,
336                 JSValueRef possibleInstance,
337                 JSValueRef* exception)
338 {
339         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
340 }
341
342 JSValueRef JSFile::toUri(JSContextRef context,
343                 JSObjectRef object,
344                 JSObjectRef thisObject,
345                 size_t argc,
346                 const JSValueRef argv[],
347                 JSValueRef* exception)
348 {
349         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
350         if (!privateObject) {
351                 LogError("Private object is not set.");
352                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
353         }
354
355         Converter converter(context);
356         try {
357                 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_TO_URI);
358                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
359                 
360                 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
361                 return converter.toJSValueRef(privateObject->getObject()->getNode()->toUri(widgetId));
362         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
363                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
364         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
365                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
366         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
367                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
368         } catch(const WrtDeviceApis::Commons::Exception& ex) {
369                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
370         }
371 }
372
373 JSValueRef JSFile::listFiles(JSContextRef context,
374                 JSObjectRef object,
375                 JSObjectRef thisObject,
376                 size_t argc,
377                 const JSValueRef argv[],
378                 JSValueRef* exception)
379 {
380         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
381         if (!privateObject) {
382                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
383         }
384
385         LogDebug("OK");
386
387         JSContextRef globalContext = privateObject->getContext();
388         size_t index = 0;
389         JSValueRef reserveArguments[3];
390         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
391
392         try {
393
394                 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_LIST_FILES);
395                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
396
397                 for (index = 0; index < 3; index++)
398                 {
399                         if (index < argc)
400                                 reserveArguments[index] = argv[index];
401                         else 
402                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
403                 }
404
405                 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[0]);
406                 JSValueRef onError = NULL;
407
408
409                 if (argc > 1) {
410                         onError = getFunctionOrNull(globalContext, reserveArguments[1]);
411                 }
412
413                 cbm->setOnSuccess(onSuccess);
414                 cbm->setOnError(onError);
415                 cbm->setObject(thisObject);
416
417                 Converter converter(globalContext);
418
419                 EventListNodesPtr event(new EventListNodes(privateObject->getObject()->getNode()));
420                 if (argc > 2) {
421                         if (JSValueIsNull(context, reserveArguments[2]) == false &&
422                                 JSValueIsUndefined(context, reserveArguments[2]) == false) {
423                                         event->setFilter(converter.toNodeFilter(reserveArguments[2]));
424                                 }
425                 }
426                 PrivateObjectDef::PermissionList perms =
427                 privateObject->getObject()->getParentPermissions();
428                 perms.push_back(privateObject->getObject()->getNode()->getPermissions());
429
430                 ListFilesPrivateDataPtr privData(new ListFilesPrivateData(cbm, perms));
431
432                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
433                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
434
435                 privateObject->getObject()->getNode()->getChildNodes(event);
436                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
437
438
439         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
440                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
441         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
442                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
443         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
444                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
445         } catch(const WrtDeviceApis::Commons::Exception& ex) {
446                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
447         }
448
449         return JSValueMakeUndefined(context);
450 }
451
452 JSValueRef JSFile::openStream(JSContextRef context,
453                 JSObjectRef object,
454                 JSObjectRef thisObject,
455                 size_t argumentCount,
456                 const JSValueRef arguments[],
457                 JSValueRef* exception)
458 {
459         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
460         if (!privateObject) {
461                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
462         }
463
464         JSContextRef globalContext = privateObject->getContext();
465         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
466
467         size_t index = 0;
468         JSValueRef reserveArguments[4];
469
470         try {
471
472                 for (index = 0; index < 4; index++) {
473                         if (index < argumentCount)
474                                 reserveArguments[index] = arguments[index];
475                         else 
476                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
477
478                 }
479
480                 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[1]);
481                 JSValueRef onError = NULL;
482
483                 onError = getFunctionOrNull(globalContext, reserveArguments[2]);
484
485                 cbm->setOnSuccess(onSuccess);
486                 cbm->setOnError(onError);
487                 cbm->setObject(thisObject);
488
489
490                 Converter converter(globalContext);
491
492                 AccessMode mode = converter.toAccessMode(reserveArguments[0]);
493                 std::string encoding = Encodings::UTF8;
494                 if (argumentCount > 3) {
495                         if(!JSValueIsNull(globalContext, reserveArguments[3]) && !JSValueIsUndefined(globalContext, reserveArguments[3])){
496                                 encoding = converter.toEncoding(reserveArguments[3]);
497                         }
498                 }
499
500                 if ((AM_READ != mode) && (PERM_READ == privateObject->getObject()->getNode()->getPermissions())) {
501                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
502                 }
503
504                 std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
505                 std::string vpath = Utils::toVirtualPath(globalContext, path);
506
507                 AccessModeInfo am = AccessModeInfo(mode, vpath);
508                 AceSecurityStatus status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_OPEN_STREAM, am);
509
510                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
511
512                 EventOpenPtr event(new EventOpen(mode));
513                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(cbm));
514                 event->setCharSet(encoding);
515                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
516                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
517
518                 privateObject->getObject()->getNode()->open(event);
519
520
521         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
522                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
523         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
524                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
525         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
526                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
527         } catch(const WrtDeviceApis::Commons::Exception& ex) {
528                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
529         }
530
531         return JSValueMakeUndefined(context);
532 }
533
534 JSValueRef JSFile::readAsText(JSContextRef context,
535                 JSObjectRef object,
536                 JSObjectRef thisObject,
537                 size_t argc,
538                 const JSValueRef argv[],
539                 JSValueRef* exception)
540 {
541         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
542         if (!privateObject) {
543                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
544         }
545
546
547         JSContextRef globalContext = privateObject->getContext();
548         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_READ_AS_TEXT);
549
550         size_t index = 0;
551         JSValueRef reserveArguments[3];
552         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
553
554         try {
555
556                 for (index = 0; index < 3; index++) {
557                         if (index < argc)
558                                 reserveArguments[index] = argv[index];
559                         else 
560                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
561
562                 }
563                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
564
565                 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[0]);
566                 JSValueRef onError = NULL;
567
568                 if (argc > 1) {
569                         onError = getFunctionOrNull(globalContext, reserveArguments[1]);
570                 }
571
572                 cbm->setOnSuccess(onSuccess);
573                 cbm->setOnError(onError);
574                 cbm->setObject(thisObject);
575
576                 Converter converter(context);
577                 std::string src = Encodings::UTF8;
578                 if (argc > 2) {
579                         if (!JSValueIsNull(context, reserveArguments[2]) && !JSValueIsUndefined(context, reserveArguments[2])) {
580                                 src = converter.toEncoding(reserveArguments[2]);
581                         }
582                 }
583
584                 if (NT_FILE != privateObject->getObject()->getNode()->getType()) {
585                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
586                         return JSValueMakeUndefined(context);
587                 }
588
589                 EventReadTextPtr event(new EventReadText());
590                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData,JSCallbackManager>(cbm));
591                 event->setCharSet(src);
592                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
593                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
594
595                 privateObject->getObject()->getNode()->read(event);
596
597         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
598                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
599         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
600                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
601         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
602                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
603         } catch(const WrtDeviceApis::Commons::Exception& ex) {
604                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
605         }
606
607         return JSValueMakeUndefined(context);
608 }
609
610 JSValueRef JSFile::copyTo(JSContextRef context,
611                 JSObjectRef object,
612                 JSObjectRef thisObject,
613                 size_t argc,
614                 const JSValueRef argv[],
615                 JSValueRef* exception)
616 {
617         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
618         if (!privateObject) {
619                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
620         }
621
622         JSContextRef globalContext = privateObject->getContext();
623         size_t index = 0;
624         JSValueRef reserveArguments[5];
625         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
626
627         try {
628
629                 for (index = 0; index < 5; index++)
630                 {
631                         if (index < argc)
632                                 reserveArguments[index] = argv[index];
633                         else 
634                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
635                 }
636
637                 Converter converter(globalContext);
638                 JSValueRef onSuccess = NULL;
639                 JSValueRef onError = NULL;
640
641                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[3]);
642                 onError = getFunctionOrNull(globalContext, reserveArguments[4]);
643
644                 cbm->setOnSuccess(onSuccess);
645                 cbm->setOnError(onError);               
646                 cbm->setObject(thisObject);
647
648
649                 //TODO add check validation for src, dest string.
650                 IPathPtr src = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
651                 IPathPtr dest = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[1]));
652                 bool overwrite = converter.toBool(reserveArguments[2]);;
653                                         
654                 if (NT_DIRECTORY != privateObject->getObject()->getNode()->getType()) {
655                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
656                         return JSValueMakeUndefined(context);
657                 }
658
659                 std::string virtualDestPath = Utils::toVirtualPath(globalContext, dest->getFullPath());
660                 AccessModeInfo amode = AccessModeInfo(AM_WRITE, virtualDestPath);
661                 AceSecurityStatus status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(
662                         FILESYSTEM_FUNCTION_API_COPY_TO,
663                 amode);
664
665                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
666
667                 EventCopyPtr event(new EventCopy(src, dest));
668
669                 if (overwrite) {
670                         event->setOptions(OPT_OVERWRITE);
671                 }
672
673                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::
674                         IEventPrivateData,
675                         JSCallbackManager>(
676                         cbm));
677                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
678                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
679                                         
680                 IManager::getInstance().copy(event);
681         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
682                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
683         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
684                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
685         } catch(const WrtDeviceApis::Commons::NotFoundException &ex) {
686                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
687         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
688                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
689         } catch(const WrtDeviceApis::Commons::Exception& ex) {
690                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
691         }
692
693         return JSValueMakeUndefined(context);
694 }
695
696 JSValueRef JSFile::moveTo(JSContextRef context,
697                 JSObjectRef object,
698                 JSObjectRef thisObject,
699                 size_t argc,
700                 const JSValueRef argv[],
701                 JSValueRef* exception)
702 {
703         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
704         if (!privateObject) {
705                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
706         }
707
708         JSContextRef globalContext = privateObject->getContext();
709
710         LogDebug("OK");
711
712         size_t index = 0;
713         JSValueRef reserveArguments[5];
714         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
715
716         try {
717
718                 for (index = 0; index < 5; index++) {
719                         if (index < argc)
720                                 reserveArguments[index] = argv[index];
721                         else 
722                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
723                 }
724
725                 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
726                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
727                 }
728
729                 Converter converter(context);
730                 JSValueRef onSuccess = NULL;
731                 JSValueRef onError = NULL;
732
733                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[3]);
734                 onError = getFunctionOrNull(globalContext, reserveArguments[4]);
735                 cbm->setOnSuccess(onSuccess);
736                 cbm->setOnError(onError);
737                 cbm->setObject(thisObject);
738
739                 if (NT_DIRECTORY != privateObject->getObject()->getNode()->getType()) {
740                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "io error"));
741                         return JSValueMakeUndefined(context);
742                 }
743
744                 IPathPtr src = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
745                 IPathPtr dest = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[1]));
746                 bool overwrite = converter.toBool(reserveArguments[2]);
747
748                 std::string virtualDestPath = Utils::toVirtualPath(globalContext, dest->getFullPath());
749
750                 AccessModeInfo amode = AccessModeInfo(AM_WRITE, virtualDestPath);
751                 AceSecurityStatus status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_MOVE_TO, amode);
752
753                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
754
755                 EventMovePtr event(new EventMove(src, dest));
756                 if (overwrite) {
757                         event->setOptions(OPT_OVERWRITE);
758                 }
759
760                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
761                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
762                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
763
764                 IManager::getInstance().move(event);
765
766         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
767                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
768         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
769                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
770         }       catch(const WrtDeviceApis::Commons::NotFoundException &ex) {
771                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
772         }
773         catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
774                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
775         } catch(const WrtDeviceApis::Commons::Exception& ex) {
776                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
777         }
778         return JSValueMakeUndefined(context);
779 }
780
781 JSValueRef JSFile::createDirectory(JSContextRef context,
782                 JSObjectRef object,
783                 JSObjectRef thisObject,
784                 size_t argc,
785                 const JSValueRef argv[],
786                 JSValueRef* exception)
787 {
788         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
789         if (!privateObject) {
790                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
791         }
792
793         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_CREATE_DIR);
794
795         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
796
797
798         Converter converter(context);
799         try {
800
801                 if (argc < 1) {
802                         ThrowMsg(InvalidArgumentException, "Invalid path name");
803                 }
804
805                 IPathPtr path = converter.toPath(argv[0]);
806                 INodePtr node(privateObject->getObject()->getNode()->createChild(path, NT_DIRECTORY, OPT_RECURSIVE));
807                 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
808
809                 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
810                 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
811
812                 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
813         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
814                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
815         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
816                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
817         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
818                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
819         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
820                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
821         } catch(const WrtDeviceApis::Commons::Exception& ex) {
822                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
823         }
824 }
825
826 JSValueRef JSFile::createFile(JSContextRef context,
827                 JSObjectRef object,
828                 JSObjectRef thisObject,
829                 size_t argc,
830                 const JSValueRef argv[],
831                 JSValueRef* exception)
832 {
833         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
834         if (!privateObject) {
835                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
836         }
837
838         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_CREATE_FILE);
839
840         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
841
842
843         Converter converter(context);
844         try {
845
846                 if (argc < 1) {
847                         ThrowMsg(InvalidArgumentException, "Invalid path name");
848                 }
849
850                 IPathPtr path = converter.toPath(argv[0]);
851                 INodePtr node = privateObject->getObject()->getNode()->createChild(path, NT_FILE);
852
853                 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
854                 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
855
856                 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
857                 /*
858                 IPathPtr path = converter.toPath(argv[0]);
859                 INodePtr node = privateObject->getObject()->getNode()->createChild(path, NT_FILE);
860                 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), node);*/
861         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
862                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
863         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
864                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
865         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
866                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
867         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
868                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
869         } catch(const WrtDeviceApis::Commons::Exception& ex) {
870                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
871         }
872
873 }
874
875 JSValueRef JSFile::resolve(JSContextRef context,
876                 JSObjectRef object,
877                 JSObjectRef thisObject,
878                 size_t argc,
879                 const JSValueRef argv[],
880                 JSValueRef* exception)
881 {
882         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
883         if (!privateObject) {
884                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
885         }
886
887
888         if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
889                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, "IO error");
890         }
891
892         Converter converter(context);
893         try {
894
895                 if (argc < 1) {
896                         ThrowMsg(InvalidArgumentException, "Invalid path name");
897                 }
898
899                 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_RESOLVE);
900                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
901                 
902
903                 IPathPtr path = converter.toPath(argv[0]);
904                 INodePtr node = privateObject->getObject()->getNode()->getChild(path);
905                 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
906                 PrivateObjectDefPtr privData(new PrivateObjectDef(node,         privateObject->getObject()->getParentPermissions()));
907                 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
908                 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
909                 /*
910                 IPathPtr path = converter.toPath(argv[0]);
911                 INodePtr node = privateObject->getObject()->getNode()->getChild(path);
912                 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
913                 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), node);
914                 */
915         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
916                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
917         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
918                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
919         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
920                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
921         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
922                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
923         } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
924                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
925         } catch(const WrtDeviceApis::Commons::Exception& ex) {
926                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
927         }
928 }
929
930 JSValueRef JSFile::deleteDirectory(JSContextRef context,
931                 JSObjectRef object,
932                 JSObjectRef thisObject,
933                 size_t argc,
934                 const JSValueRef argv[],
935                 JSValueRef* exception)
936 {
937         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
938         if (!privateObject) {
939                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
940         }
941
942         if (argc < 2) {
943                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
944         }
945
946         JSContextRef globalContext = privateObject->getContext();
947
948         LogDebug("OK");
949
950         size_t index = 0;
951         JSValueRef reserveArguments[4];
952         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);      
953
954         try {
955                 for (index = 0; index < 4; index++) {
956                 if (index < argc)
957                         reserveArguments[index] = argv[index];
958                 else 
959                         reserveArguments[index] = JSValueMakeUndefined(context);                                                        
960                 }
961
962                 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_DELETE_DIR);
963
964                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
965
966                 JSValueRef onSuccess = NULL;
967                 JSValueRef onError = NULL;
968
969                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[2]);
970                 onError = getFunctionOrNull(globalContext, reserveArguments[3]);
971
972                 cbm->setOnSuccess(onSuccess);
973                 cbm->setOnError(onError);
974                 cbm->setObject(thisObject);
975
976                 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
977                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
978                 }
979
980                 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
981                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
982                         return JSValueMakeUndefined(context);
983                 }
984
985                 Converter converter(context);
986                 IPathPtr path = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
987                 bool recursive = converter.toBool(reserveArguments[1]);
988
989                 if (*privateObject->getObject()->getNode()->getPath() != path->getPath()) {
990                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
991                         return JSValueMakeUndefined(context);
992                 }
993
994
995                 EventResolvePtr eventResolve(new EventResolve(path));
996                 if (eventResolve->setForSynchronousCall()) { 
997                         IManager::getInstance().getNode(eventResolve);
998                         if (!eventResolve->getResult() || (eventResolve->getExceptionCode() != WrtDeviceApis::Commons::ExceptionCodes::None)) {
999                                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1000                                 return JSValueMakeUndefined(context);
1001                         }
1002
1003                         if (eventResolve->getResult()->getType() != NT_DIRECTORY) {
1004                                 ThrowMsg(InvalidArgumentException, "Invalid directory");
1005                         }
1006
1007                         EventRemovePtr eventRemove(new EventRemove(path));
1008
1009                         if (recursive) {
1010                                 eventRemove->setOptions(OPT_RECURSIVE);
1011                         }
1012
1013                         eventRemove->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
1014                         eventRemove->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1015                         FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
1016
1017                         IManager::getInstance().remove(eventRemove);
1018                 }
1019         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1020         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, ex.GetMessage()));
1021
1022         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1023                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1024         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1025                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1026         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1027                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
1028         } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1029                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
1030         } catch(const WrtDeviceApis::Commons::Exception& ex) {
1031                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
1032         }
1033
1034         return JSValueMakeUndefined(context);
1035 }
1036
1037 JSValueRef JSFile::deleteFile(JSContextRef context,
1038                 JSObjectRef object,
1039                 JSObjectRef thisObject,
1040                 size_t argc,
1041                 const JSValueRef argv[],
1042                 JSValueRef* exception)
1043 {
1044         LogDebug("<<<");
1045
1046         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
1047
1048         if (!privateObject) {
1049                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
1050         }
1051
1052         JSContextRef globalContext = privateObject->getContext();
1053
1054         size_t index = 0;
1055         JSValueRef reserveArguments[3];
1056         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);      
1057
1058         try {
1059                 for (index = 0; index < 3; index++) {
1060                         if (index < argc)
1061                                 reserveArguments[index] = argv[index];
1062                         else 
1063                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
1064                 }
1065
1066                 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_DELETE_FILE);
1067
1068                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1069
1070                 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
1071                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
1072                 }
1073
1074                 JSValueRef onSuccess = NULL;
1075                 JSValueRef onError = NULL;
1076
1077                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[1]);
1078                 onError = getFunctionOrNull(globalContext, reserveArguments[2]);
1079
1080                 cbm->setOnSuccess(onSuccess);
1081                 cbm->setOnError(onError);
1082                 cbm->setObject(thisObject);
1083
1084                 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
1085                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1086                         return JSValueMakeUndefined(context);
1087                 }
1088
1089
1090                 Converter converter(context);
1091                 IPathPtr path = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
1092
1093                 EventResolvePtr eventResolve(new EventResolve(path));
1094                 if (eventResolve->setForSynchronousCall()) { 
1095                         IManager::getInstance().getNode(eventResolve);
1096
1097                         if (!eventResolve->getResult() ||(eventResolve->getExceptionCode() != WrtDeviceApis::Commons::ExceptionCodes::None)) {
1098                                 if(eventResolve->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException){
1099                                         LogDebug("POST IO NOT_FOUND_ERROR");
1100                                         ThrowMsg(NotFoundException, "Not found error");
1101                                 }
1102
1103                                 LogDebug("POST IO ERROR");
1104                                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1105                                 return JSValueMakeUndefined(context);
1106                         }
1107
1108                         if (eventResolve->getResult()->getType() != NT_FILE) {                          
1109                                 ThrowMsg(InvalidArgumentException, "Invalid directory");
1110                         }
1111
1112                         LogDebug("try to call async event");
1113                         EventRemovePtr eventRemove(new EventRemove(path));
1114                         eventRemove->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
1115                         eventRemove->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1116                         FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
1117
1118                         IManager::getInstance().remove(eventRemove);
1119
1120                 }
1121         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1122                 LogDebug("Platform Exception !!!!!!!! post io error");
1123                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, ex.GetMessage()));
1124         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1125                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1126         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1127                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1128         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1129                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
1130         } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1131                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
1132         } catch(const WrtDeviceApis::Commons::Exception& ex) {
1133                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
1134         }
1135
1136         LogDebug(">>>");
1137         return JSValueMakeUndefined(context);
1138 }
1139 }
1140 }