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