291115212429353a705004fc1bd01da0528a319a
[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
357         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_TO_URI);
358         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
359         
360
361         try {
362                 int widgetId = WrtAccessSingleton::Instance().getWidgetId();
363                 return converter.toJSValueRef(privateObject->getObject()->getNode()->toUri(widgetId));
364         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
365                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
366         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
367                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
368         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
369                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
370         } catch(const WrtDeviceApis::Commons::Exception& ex) {
371                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
372         }
373 }
374
375 JSValueRef JSFile::listFiles(JSContextRef context,
376                 JSObjectRef object,
377                 JSObjectRef thisObject,
378                 size_t argc,
379                 const JSValueRef argv[],
380                 JSValueRef* exception)
381 {
382         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
383         if (!privateObject) {
384                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
385         }
386
387         LogDebug("OK");
388
389         JSContextRef globalContext = privateObject->getContext();
390         size_t index = 0;
391         JSValueRef reserveArguments[3];
392         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
393
394         try {
395
396                 AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_LIST_FILES);
397                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
398
399                 for (index = 0; index < 3; index++)
400                 {
401                         if (index < argc)
402                                 reserveArguments[index] = argv[index];
403                         else 
404                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
405                 }
406
407                 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[0]);
408                 JSValueRef onError = NULL;
409
410
411                 if (argc > 1) {
412                         onError = getFunctionOrNull(globalContext, reserveArguments[1]);
413                 }
414
415                 cbm->setOnSuccess(onSuccess);
416                 cbm->setOnError(onError);
417                 cbm->setObject(thisObject);
418
419                 Converter converter(globalContext);
420
421                 EventListNodesPtr event(new EventListNodes(privateObject->getObject()->getNode()));
422                 if (argc > 2) {
423                         if (JSValueIsNull(context, reserveArguments[2]) == false &&
424                                 JSValueIsUndefined(context, reserveArguments[2]) == false) {
425                                         event->setFilter(converter.toNodeFilter(reserveArguments[2]));
426                                 }
427                 }
428                 PrivateObjectDef::PermissionList perms =
429                 privateObject->getObject()->getParentPermissions();
430                 perms.push_back(privateObject->getObject()->getNode()->getPermissions());
431
432                 ListFilesPrivateDataPtr privData(new ListFilesPrivateData(cbm, perms));
433
434                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
435                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
436
437                 privateObject->getObject()->getNode()->getChildNodes(event);
438                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
439
440
441         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
442                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
443         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
444                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
445         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
446                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
447         } catch(const WrtDeviceApis::Commons::Exception& ex) {
448                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
449         }
450
451         return JSValueMakeUndefined(context);
452 }
453
454 JSValueRef JSFile::openStream(JSContextRef context,
455                 JSObjectRef object,
456                 JSObjectRef thisObject,
457                 size_t argumentCount,
458                 const JSValueRef arguments[],
459                 JSValueRef* exception)
460 {
461         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
462         if (!privateObject) {
463                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
464         }
465
466         JSContextRef globalContext = privateObject->getContext();
467         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
468
469         size_t index = 0;
470         JSValueRef reserveArguments[4];
471
472         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_OPEN_STREAM);
473         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
474
475
476         try {
477
478                 for (index = 0; index < 4; index++) {
479                         if (index < argumentCount)
480                                 reserveArguments[index] = arguments[index];
481                         else 
482                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
483
484                 }
485
486                 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[1]);
487                 JSValueRef onError = NULL;
488
489                 onError = getFunctionOrNull(globalContext, reserveArguments[2]);
490
491                 cbm->setOnSuccess(onSuccess);
492                 cbm->setOnError(onError);
493                 cbm->setObject(thisObject);
494
495
496                 Converter converter(globalContext);
497
498                 AccessMode mode = converter.toAccessMode(reserveArguments[0]);
499                 std::string encoding = Encodings::UTF8;
500                 if (argumentCount > 3) {
501                         if(!JSValueIsNull(globalContext, reserveArguments[3]) && !JSValueIsUndefined(globalContext, reserveArguments[3])){
502                                 encoding = converter.toEncoding(reserveArguments[3]);
503                         }
504                 }
505
506                 if ((AM_READ != mode) && (PERM_READ == privateObject->getObject()->getNode()->getPermissions())) {
507                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
508                 }
509
510                 std::string path = privateObject->getObject()->getNode()->getPath()->getFullPath();
511                 std::string vpath = Utils::toVirtualPath(globalContext, path);
512
513                 AccessModeInfo am = AccessModeInfo(mode, vpath);
514                 status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_OPEN_STREAM, am);
515
516                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
517
518                 EventOpenPtr event(new EventOpen(mode));
519                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(cbm));
520                 event->setCharSet(encoding);
521                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
522                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
523
524                 privateObject->getObject()->getNode()->open(event);
525
526
527         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
528                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
529         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
530                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
531         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
532                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
533         } catch(const WrtDeviceApis::Commons::Exception& ex) {
534                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
535         }
536
537         return JSValueMakeUndefined(context);
538 }
539
540 JSValueRef JSFile::readAsText(JSContextRef context,
541                 JSObjectRef object,
542                 JSObjectRef thisObject,
543                 size_t argc,
544                 const JSValueRef argv[],
545                 JSValueRef* exception)
546 {
547         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
548         if (!privateObject) {
549                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
550         }
551
552
553         JSContextRef globalContext = privateObject->getContext();
554         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_READ_AS_TEXT);
555         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
556
557         size_t index = 0;
558         JSValueRef reserveArguments[3];
559         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
560
561         try {
562
563                 for (index = 0; index < 3; index++) {
564                         if (index < argc)
565                                 reserveArguments[index] = argv[index];
566                         else 
567                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
568
569                 }
570
571
572                 JSValueRef onSuccess = getFunction(globalContext, reserveArguments[0]);
573                 JSValueRef onError = NULL;
574
575                 if (argc > 1) {
576                         onError = getFunctionOrNull(globalContext, reserveArguments[1]);
577                 }
578
579                 cbm->setOnSuccess(onSuccess);
580                 cbm->setOnError(onError);
581                 cbm->setObject(thisObject);
582
583                 Converter converter(context);
584                 std::string src = Encodings::UTF8;
585                 if (argc > 2) {
586                         if (!JSValueIsNull(context, reserveArguments[2]) && !JSValueIsUndefined(context, reserveArguments[2])) {
587                                 src = converter.toEncoding(reserveArguments[2]);
588                         }
589                 }
590
591                 if (NT_FILE != privateObject->getObject()->getNode()->getType()) {
592                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
593                         return JSValueMakeUndefined(context);
594                 }
595
596                 EventReadTextPtr event(new EventReadText());
597                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData,JSCallbackManager>(cbm));
598                 event->setCharSet(src);
599                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
600                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
601
602                 privateObject->getObject()->getNode()->read(event);
603
604         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
605                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
606         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
607                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
608         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
609                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
610         } catch(const WrtDeviceApis::Commons::Exception& ex) {
611                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
612         }
613
614         return JSValueMakeUndefined(context);
615 }
616
617 JSValueRef JSFile::copyTo(JSContextRef context,
618                 JSObjectRef object,
619                 JSObjectRef thisObject,
620                 size_t argc,
621                 const JSValueRef argv[],
622                 JSValueRef* exception)
623 {
624         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
625         if (!privateObject) {
626                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
627         }
628
629         JSContextRef globalContext = privateObject->getContext();
630         size_t index = 0;
631         JSValueRef reserveArguments[5];
632         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
633
634         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_COPY_TO);
635         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
636         
637
638         try {
639
640                 for (index = 0; index < 5; index++)
641                 {
642                         if (index < argc)
643                                 reserveArguments[index] = argv[index];
644                         else 
645                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
646                 }
647
648                 Converter converter(globalContext);
649                 JSValueRef onSuccess = NULL;
650                 JSValueRef onError = NULL;
651
652                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[3]);
653                 onError = getFunctionOrNull(globalContext, reserveArguments[4]);
654
655                 cbm->setOnSuccess(onSuccess);
656                 cbm->setOnError(onError);               
657                 cbm->setObject(thisObject);
658
659
660                 //TODO add check validation for src, dest string.
661                 IPathPtr src = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
662                 IPathPtr dest = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[1]));
663                 bool overwrite = converter.toBool(reserveArguments[2]);;
664                                         
665                 if (NT_DIRECTORY != privateObject->getObject()->getNode()->getType()) {
666                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
667                         return JSValueMakeUndefined(context);
668                 }
669
670                 std::string virtualDestPath = Utils::toVirtualPath(globalContext, dest->getFullPath());
671                 AccessModeInfo amode = AccessModeInfo(AM_WRITE, virtualDestPath);
672                 status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(
673                         FILESYSTEM_FUNCTION_API_COPY_TO,
674                 amode);
675
676                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
677
678                 EventCopyPtr event(new EventCopy(src, dest));
679
680                 if (overwrite) {
681                         event->setOptions(OPT_OVERWRITE);
682                 }
683
684                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::
685                         IEventPrivateData,
686                         JSCallbackManager>(
687                         cbm));
688                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance());
689                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
690                                         
691                 IManager::getInstance().copy(event);
692         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
693                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
694         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
695                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
696         } catch(const WrtDeviceApis::Commons::NotFoundException &ex) {
697                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
698         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
699                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
700         } catch(const WrtDeviceApis::Commons::Exception& ex) {
701                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
702         }
703
704         return JSValueMakeUndefined(context);
705 }
706
707 JSValueRef JSFile::moveTo(JSContextRef context,
708                 JSObjectRef object,
709                 JSObjectRef thisObject,
710                 size_t argc,
711                 const JSValueRef argv[],
712                 JSValueRef* exception)
713 {
714         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
715         if (!privateObject) {
716                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
717         }
718
719         JSContextRef globalContext = privateObject->getContext();
720
721         LogDebug("OK");
722
723         size_t index = 0;
724         JSValueRef reserveArguments[5];
725         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
726
727         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_MOVE_TO);
728         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
729
730         try {
731
732                 for (index = 0; index < 5; index++) {
733                         if (index < argc)
734                                 reserveArguments[index] = argv[index];
735                         else 
736                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
737                 }
738
739                 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
740                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
741                 }
742
743                 Converter converter(context);
744                 JSValueRef onSuccess = NULL;
745                 JSValueRef onError = NULL;
746
747                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[3]);
748                 onError = getFunctionOrNull(globalContext, reserveArguments[4]);
749                 cbm->setOnSuccess(onSuccess);
750                 cbm->setOnError(onError);
751                 cbm->setObject(thisObject);
752
753                 if (NT_DIRECTORY != privateObject->getObject()->getNode()->getType()) {
754                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "io error"));
755                         return JSValueMakeUndefined(context);
756                 }
757
758                 IPathPtr src = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
759                 IPathPtr dest = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[1]));
760                 bool overwrite = converter.toBool(reserveArguments[2]);
761
762                 std::string virtualDestPath = Utils::toVirtualPath(globalContext, dest->getFullPath());
763
764                 AccessModeInfo amode = AccessModeInfo(AM_WRITE, virtualDestPath);
765                 AceSecurityStatus status = FILESYSTEM_ACCESSMODE_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_MOVE_TO, amode);
766
767                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
768
769                 EventMovePtr event(new EventMove(src, dest));
770                 if (overwrite) {
771                         event->setOptions(OPT_OVERWRITE);
772                 }
773
774                 event->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
775                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
776                 FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
777
778                 IManager::getInstance().move(event);
779
780         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
781                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
782         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
783                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
784         }       catch(const WrtDeviceApis::Commons::NotFoundException &ex) {
785                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
786         }
787         catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
788                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
789         } catch(const WrtDeviceApis::Commons::Exception& ex) {
790                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
791         }
792         return JSValueMakeUndefined(context);
793 }
794
795 JSValueRef JSFile::createDirectory(JSContextRef context,
796                 JSObjectRef object,
797                 JSObjectRef thisObject,
798                 size_t argc,
799                 const JSValueRef argv[],
800                 JSValueRef* exception)
801 {
802         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
803         if (!privateObject) {
804                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
805         }
806
807         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_CREATE_DIR);
808         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
809
810
811         Converter converter(context);
812         try {
813
814                 if (argc < 1) {
815                         ThrowMsg(InvalidArgumentException, "Invalid path name");
816                 }
817
818                 IPathPtr path = converter.toPath(argv[0]);
819                 INodePtr node(privateObject->getObject()->getNode()->createChild(path, NT_DIRECTORY, OPT_RECURSIVE));
820                 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
821
822                 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
823                 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
824
825                 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
826         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
827                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
828         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
829                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
830         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
831                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
832         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
833                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
834         } catch(const WrtDeviceApis::Commons::Exception& ex) {
835                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
836         }
837 }
838
839 JSValueRef JSFile::createFile(JSContextRef context,
840                 JSObjectRef object,
841                 JSObjectRef thisObject,
842                 size_t argc,
843                 const JSValueRef argv[],
844                 JSValueRef* exception)
845 {
846         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
847         if (!privateObject) {
848                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
849         }
850
851         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_CREATE_FILE);
852         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
853
854
855         Converter converter(context);
856         try {
857
858                 if (argc < 1) {
859                         ThrowMsg(InvalidArgumentException, "Invalid path name");
860                 }
861
862                 IPathPtr path = converter.toPath(argv[0]);
863                 INodePtr node = privateObject->getObject()->getNode()->createChild(path, NT_FILE);
864
865                 PrivateObjectDefPtr privData(new PrivateObjectDef(node, privateObject->getObject()->getParentPermissions()));
866                 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
867
868                 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
869                 /*
870                 IPathPtr path = converter.toPath(argv[0]);
871                 INodePtr node = privateObject->getObject()->getNode()->createChild(path, NT_FILE);
872                 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), node);*/
873         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
874                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
875         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
876                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
877         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
878                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
879         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
880                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
881         } catch(const WrtDeviceApis::Commons::Exception& ex) {
882                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
883         }
884
885 }
886
887 JSValueRef JSFile::resolve(JSContextRef context,
888                 JSObjectRef object,
889                 JSObjectRef thisObject,
890                 size_t argc,
891                 const JSValueRef argv[],
892                 JSValueRef* exception)
893 {
894         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
895         if (!privateObject) {
896                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
897         }
898
899         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_RESOLVE);
900         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
901
902         if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
903                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, "IO error");
904         }
905
906         Converter converter(context);
907
908         
909         
910
911         try {
912
913
914                 
915                 if (argc < 1) {
916                         ThrowMsg(InvalidArgumentException, "Invalid path name");
917                 }
918
919
920                 
921
922                 IPathPtr path = converter.toPath(argv[0]);
923                 INodePtr node = privateObject->getObject()->getNode()->getChild(path);
924                 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
925                 PrivateObjectDefPtr privData(new PrivateObjectDef(node,         privateObject->getObject()->getParentPermissions()));
926                 privData->pushParentPermissions(privateObject->getObject()->getNode()->getPermissions());
927                 return WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(privateObject->getContext(), getClassRef(), privData);
928                 /*
929                 IPathPtr path = converter.toPath(argv[0]);
930                 INodePtr node = privateObject->getObject()->getNode()->getChild(path);
931                 node->setPermissions(privateObject->getObject()->getNode()->getPermissions());
932                 return JSUtils::makeObject(privateObject->getContext(), getClassRef(), node);
933                 */
934         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
935                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
936         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
937                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
938         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
939                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
940         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
941                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
942         } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
943                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
944         } catch(const WrtDeviceApis::Commons::Exception& ex) {
945                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
946         }
947 }
948
949 JSValueRef JSFile::deleteDirectory(JSContextRef context,
950                 JSObjectRef object,
951                 JSObjectRef thisObject,
952                 size_t argc,
953                 const JSValueRef argv[],
954                 JSValueRef* exception)
955 {
956         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
957         if (!privateObject) {
958                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
959         }
960         
961         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_DELETE_DIR);
962         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
963
964         if (argc < 2) {
965                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
966         }
967
968         JSContextRef globalContext = privateObject->getContext();
969
970         LogDebug("OK");
971
972         size_t index = 0;
973         JSValueRef reserveArguments[4];
974         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);      
975
976         
977
978
979         try {
980                 for (index = 0; index < 4; index++) {
981                 if (index < argc)
982                         reserveArguments[index] = argv[index];
983                 else 
984                         reserveArguments[index] = JSValueMakeUndefined(context);                                                        
985                 }
986
987
988                 JSValueRef onSuccess = NULL;
989                 JSValueRef onError = NULL;
990
991                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[2]);
992                 onError = getFunctionOrNull(globalContext, reserveArguments[3]);
993
994                 cbm->setOnSuccess(onSuccess);
995                 cbm->setOnError(onError);
996                 cbm->setObject(thisObject);
997
998                 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
999                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
1000                 }
1001
1002                 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
1003                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1004                         return JSValueMakeUndefined(context);
1005                 }
1006
1007                 Converter converter(context);
1008                 IPathPtr path = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
1009                 bool recursive = converter.toBool(reserveArguments[1]);
1010
1011                 if (*privateObject->getObject()->getNode()->getPath() != path->getPath()) {
1012                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1013                         return JSValueMakeUndefined(context);
1014                 }
1015
1016
1017                 EventResolvePtr eventResolve(new EventResolve(path));
1018                 if (eventResolve->setForSynchronousCall()) { 
1019                         IManager::getInstance().getNode(eventResolve);
1020                         if (!eventResolve->getResult() || (eventResolve->getExceptionCode() != WrtDeviceApis::Commons::ExceptionCodes::None)) {
1021                                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1022                                 return JSValueMakeUndefined(context);
1023                         }
1024
1025                         if (eventResolve->getResult()->getType() != NT_DIRECTORY) {
1026                                 ThrowMsg(InvalidArgumentException, "Invalid directory");
1027                         }
1028
1029                         EventRemovePtr eventRemove(new EventRemove(path));
1030
1031                         if (recursive) {
1032                                 eventRemove->setOptions(OPT_RECURSIVE);
1033                         }
1034
1035                         eventRemove->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
1036                         eventRemove->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1037                         FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
1038
1039                         IManager::getInstance().remove(eventRemove);
1040                 }
1041         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1042         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, ex.GetMessage()));
1043
1044         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1045                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1046         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1047                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1048         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1049                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
1050         } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1051                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
1052         } catch(const WrtDeviceApis::Commons::Exception& ex) {
1053                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
1054         }
1055
1056         return JSValueMakeUndefined(context);
1057 }
1058
1059 JSValueRef JSFile::deleteFile(JSContextRef context,
1060                 JSObjectRef object,
1061                 JSObjectRef thisObject,
1062                 size_t argc,
1063                 const JSValueRef argv[],
1064                 JSValueRef* exception)
1065 {
1066         LogDebug("<<<");
1067
1068         PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
1069
1070         if (!privateObject) {
1071                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch error");
1072         }
1073
1074         JSContextRef globalContext = privateObject->getContext();
1075
1076         size_t index = 0;
1077         JSValueRef reserveArguments[3];
1078         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);      
1079
1080         AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_DELETE_FILE);
1081         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1082
1083
1084         try {
1085                 for (index = 0; index < 3; index++) {
1086                         if (index < argc)
1087                                 reserveArguments[index] = argv[index];
1088                         else 
1089                                 reserveArguments[index] = JSValueMakeUndefined(context);                                                        
1090                 }
1091
1092
1093                 if ((privateObject->getObject()->getNode()->getPermissions() & PERM_WRITE) == 0) {
1094                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "Permission denied error");
1095                 }
1096
1097                 JSValueRef onSuccess = NULL;
1098                 JSValueRef onError = NULL;
1099
1100                 onSuccess = getFunctionOrNull(globalContext, reserveArguments[1]);
1101                 onError = getFunctionOrNull(globalContext, reserveArguments[2]);
1102
1103                 cbm->setOnSuccess(onSuccess);
1104                 cbm->setOnError(onError);
1105                 cbm->setObject(thisObject);
1106
1107                 if (privateObject->getObject()->getNode()->getType() != NT_DIRECTORY) {
1108                         cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1109                         return JSValueMakeUndefined(context);
1110                 }
1111
1112
1113                 Converter converter(context);
1114                 IPathPtr path = Utils::fromVirtualPath(globalContext, converter.toString(reserveArguments[0]));
1115
1116                 EventResolvePtr eventResolve(new EventResolve(path));
1117                 if (eventResolve->setForSynchronousCall()) { 
1118                         IManager::getInstance().getNode(eventResolve);
1119
1120                         if (!eventResolve->getResult() ||(eventResolve->getExceptionCode() != WrtDeviceApis::Commons::ExceptionCodes::None)) {
1121                                 if(eventResolve->getExceptionCode() == WrtDeviceApis::Commons::ExceptionCodes::NotFoundException){
1122                                         LogDebug("POST IO NOT_FOUND_ERROR");
1123                                         ThrowMsg(NotFoundException, "Not found error");
1124                                 }
1125
1126                                 LogDebug("POST IO ERROR");
1127                                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, "IO error"));
1128                                 return JSValueMakeUndefined(context);
1129                         }
1130
1131                         if (eventResolve->getResult()->getType() != NT_FILE) {                          
1132                                 ThrowMsg(InvalidArgumentException, "Invalid directory");
1133                         }
1134
1135                         LogDebug("try to call async event");
1136                         EventRemovePtr eventRemove(new EventRemove(path));
1137                         eventRemove->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
1138                         eventRemove->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1139                         FilesystemAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
1140
1141                         IManager::getInstance().remove(eventRemove);
1142
1143                 }
1144         } catch (const WrtDeviceApis::Commons::PlatformException& ex) {
1145                 LogDebug("Platform Exception !!!!!!!! post io error");
1146                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::IO_ERROR, ex.GetMessage()));
1147         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
1148                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
1149         } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
1150                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
1151         } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
1152                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
1153         } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
1154                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
1155         } catch(const WrtDeviceApis::Commons::Exception& ex) {
1156                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
1157         }
1158
1159         LogDebug(">>>");
1160         return JSValueMakeUndefined(context);
1161 }
1162 }
1163 }