wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Filesystem / JSFilesystemManager.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 0b4dca5..83f4906
@@ -17,9 +17,6 @@
 
  
 #include "JSFilesystemManager.h"
-
-#include <dpl/log/log.h>
-
 #include <Commons/Exception.h>
 #include "EventResolve.h"
 #include "IManager.h"
 #include <WidgetDB/WidgetDBMgr.h>
 #include <WidgetDB/IWidgetDB.h>
 #include <TimeTracer.h>
-
+#include <ArgumentValidator.h>
+#include <JSWebAPIError.h>
+#include <JSWebAPIException.h>
+#include <JSUtil.h>
 #include "JSFile.h"
 #include "FilesystemUtils.h"
 #include "Converter.h"
@@ -48,6 +48,7 @@
 #include "ResponseDispatcher.h"
 #include "FilesystemAsyncCallbackManager.h"
 #include "FilesystemListenerManager.h"
+#include <Logger.h>
 
 using namespace WrtDeviceApis::Commons;
 using namespace WrtDeviceApis::CommonsJavaScript;
@@ -55,6 +56,7 @@ using namespace DeviceAPI::Common;
 using namespace WrtDeviceApis;
 
 
+
 namespace {
 const char* PLUGIN_NAME = "FileSystemManager";
 const char* PROPERTY_MAXPATHLENGTH = "maxPathLength";
@@ -63,7 +65,7 @@ JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
 {
        if (Validator(ctx).isCallback(arg)) {
                return arg;
-       } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
+       } else if (!JSValueIsNull(ctx, arg) &&  !JSValueIsUndefined(ctx, arg)) {
                ThrowMsg(ConversionException, "Not a function nor JS null.");
        }
        return NULL;
@@ -146,7 +148,7 @@ void JSFilesystemManager::initialize(JSContextRef context,
         int widgetId = WrtAccessSingleton::Instance().getWidgetId();
                WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId);
 
-               LogDebug(widgetDB->getWidgetInstallationPath());
+               LoggerD(widgetDB->getWidgetInstallationPath());
                std::string wgtpackage = "wgt-package";
                std::string wgtprivate = "wgt-private";
                std::string wgtprivatetemp = "wgt-private-tmp";
@@ -196,14 +198,31 @@ JSValueRef JSFilesystemManager::getStorage(JSContextRef context,
        AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_GET_STORAGE);
        TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
+       // argument validation with new validator
+       try {
+               ArgumentValidator validator(context, argc, argv);
+               validator.toString(0, false);
+               validator.toFunction(1, false);
+               validator.toFunction(2, true);  
+       }
+       catch (const BasePlatformException &err) {
+               return JSWebAPIException::throwException(context, exception, err);
+       } catch (...) {
+               DeviceAPI::Common::UnknownException err("");
+               return JSWebAPIException::throwException(context, exception, err);
+       }
+
        if (argc < 2) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
        }
 
+
+
+
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
        JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
-
+       
        try {
 
                JSValueRef onSuccess = getFunction(globalContext, argv[1]);
@@ -253,10 +272,23 @@ JSValueRef JSFilesystemManager::getStorageList(JSContextRef context,
        AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_LIST_STORAGE);
        TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
        
+       // argument validation with new validator
+       try {
+               ArgumentValidator validator(context, argc, argv);
+               validator.toFunction(0, false);
+               validator.toFunction(1, true);  
+       }
+       catch (const BasePlatformException &err) {
+               return JSWebAPIException::throwException(context, exception, err);
+       } catch (...) {
+               DeviceAPI::Common::UnknownException err("");
+               return JSWebAPIException::throwException(context, exception, err);
+       }
 
        if (argc < 1) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
        }
+
        
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
@@ -312,12 +344,27 @@ JSValueRef JSFilesystemManager::addStorageStateListener(JSContextRef context,
        AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_ADD_STORAGE_LISTENER);
        TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
+
+       // argument validation with new validator
+       try {
+               ArgumentValidator validator(context, argc, argv);
+               validator.toFunction(0, false);
+               validator.toFunction(1, true);  
+       }
+       catch (const BasePlatformException &err) {
+               LogDebug("Exception");
+               return JSWebAPIException::throwException(context, exception, err);
+       } catch (...) {
+               DeviceAPI::Common::UnknownException err("");
+               return JSWebAPIException::throwException(context, exception, err);
+       }
+       
+
        if (argc < 1) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "TYPE_MISMATCH_ERROR");
        }
 
 
-
        JSContextRef globalContext = privateObject->getContext();
        JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
@@ -371,18 +418,33 @@ JSValueRef JSFilesystemManager::removeStorageStateListener(JSContextRef context,
        AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_REMOVE_STORAGE_LISTENER);
        TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
+       LogDebug("a");
+
+       // argument validation with new validator
+       try {
+               ArgumentValidator validator(context, argc, argv);
+               validator.toLong(0, false);
+       }
+       catch (const BasePlatformException &err) {
+               return JSWebAPIException::throwException(context, exception, err);
+       } catch (...) {
+               DeviceAPI::Common::UnknownException err("");
+               return JSWebAPIException::throwException(context, exception, err);
+       }
+
        if (argc < 1) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
        }
 
 
+       
 
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
        
        try {
                long id = static_cast<long>(converter->toLong(argv[0]));
-               LogDebug("id:" << id);
+               LoggerD("id:" << id);
 
                if (id >= 0) {
                        
@@ -416,7 +478,7 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context,
                JSValueRef* exception)
 {
 
-       LogDebug("<<<");
+       LoggerD("<<<");
        TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
 
        PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
@@ -427,28 +489,46 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context,
 
        AceSecurityStatus status = FILESYSTEM_CHECK_ACCESS(FILESYSTEM_FUNCTION_API_MGR_RESOLVE_ID);
        TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
+
+       // argument validation with new validator
+       try {
+               ArgumentValidator validator(context, argc, argv);
+               validator.toString(0, false);
+               validator.toFunction(1, false);
+               validator.toFunction(2, true);
+               validator.toString(3, true);
+       }
+       catch (const BasePlatformException &err) {
+               return JSWebAPIException::throwException(context, exception, err);
+       } catch (...) {
+               DeviceAPI::Common::UnknownException err("");
+               return JSWebAPIException::throwException(context, exception, err);
+       }
        
        if (argc < 2) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
        }
 
+       LogDebug("resolve");
+
+
+       
+
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
 
        size_t index = 0;
-       JSValueRef reserveArguments[4];
        JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);      
-
-       
-
+       JSValueRef reserveArguments[4];
 
        try {
                for (index = 0; index < 4; index++) {
                        if (index < argc)
                                reserveArguments[index] = argv[index];
                        else 
-                               reserveArguments[index] = JSValueMakeUndefined(context);
+                               reserveArguments[index] = JSValueMakeUndefined(context);                                                        
                }
+                               
                JSValueRef onSuccess = getFunction(globalContext, reserveArguments[1]);
                JSValueRef onError = NULL;
 
@@ -462,14 +542,14 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context,
                std::string virtualPath;
                path = Utils::fromVirtualPath(globalContext, converter->toString(reserveArguments[0]));
                virtualPath = converter->toString(reserveArguments[0]);
-               LogDebug("virtualPath:[" << virtualPath << "]");
+               LoggerD("virtualPath:[" << virtualPath << "]");
                int permissions = PERM_READ | PERM_WRITE;
 
 
                
                if (argc > 3) {
                        std::string perms = converter->toString(reserveArguments[3]);
-                       LogDebug("perms:[" << perms << "]");
+                       LoggerD("perms:[" << perms << "]");
                        if (("r" != perms) && ("rw" != perms) && ("w" != perms) && ("a" != perms)) {
                                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Invalid permission");
                        } else if ("r" == perms) {