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