upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Filesystem / JSFilesystemManager.cpp
index bd4acdd..7cb53ee 100755 (executable)
@@ -33,6 +33,9 @@
 #include <Tizen/Common/JSTizenExceptionFactory.h>
 #include <Tizen/Common/JSTizenException.h> 
 #include <Tizen/Common/SecurityExceptions.h>
+#include <Commons/WrtAccess/WrtAccess.h>
+#include <WidgetDB/WidgetDBMgr.h>
+#include <WidgetDB/IWidgetDB.h>
 
 #include "JSFile.h"
 #include "FilesystemUtils.h"
@@ -45,6 +48,8 @@
 using namespace WrtDeviceApis::Commons;
 using namespace WrtDeviceApis::CommonsJavaScript;
 using namespace TizenApis::Commons;
+using namespace WrtDeviceApis;
+using namespace TizenApis::Api::Filesystem;
 
 
 namespace {
@@ -66,10 +71,9 @@ JSValueRef getFunction(JSContextRef ctx, JSValueRef arg)
 {
        if (Validator(ctx).isCallback(arg)) {
                return arg;
-       } else if (JSValueIsNull(ctx, arg) || JSValueIsUndefined(ctx, arg)) {
-               ThrowMsg(InvalidArgumentException, "JS null passed as function.");
+       }else{
+               ThrowMsg(ConversionException, "JS null passed as function.");
        }
-       ThrowMsg(ConversionException, "Not a function nor JS null.");
 }
 
 }
@@ -135,6 +139,20 @@ void JSFilesystemManager::initialize(JSContextRef context,
        if (!JSObjectSetPrivate(object, privateObject)) {
                delete privateObject;
        }
+       else {
+        int widgetId = WrtAccessSingleton::Instance().getWidgetId();
+               WidgetDB::Api::IWidgetDBPtr widgetDB = WidgetDB::Api::getWidgetDB(widgetId);
+
+               LogDebug(widgetDB->getWidgetInstallationPath());
+               std::string wgtpackage = "wgt-package";
+               std::string wgtprivate = "wgt-private";
+               std::string wgtprivatetemp = "wgt-private-tmp";
+               
+               Api::Filesystem::IManager::getInstance().addWidgetStorage(wgtpackage, widgetDB->getWidgetInstallationPath());
+               Api::Filesystem::IManager::getInstance().addWidgetStorage(wgtprivate, widgetDB->getWidgetPersistentStoragePath());
+               Api::Filesystem::IManager::getInstance().addWidgetStorage(wgtprivatetemp, widgetDB->getWidgetTemporaryStoragePath());
+       
+       }
 }
 
 void JSFilesystemManager::finalize(JSObjectRef object)
@@ -178,43 +196,39 @@ JSValueRef JSFilesystemManager::getStorage(JSContextRef context,
 
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
+       JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
 
        try {
 
                JSValueRef onSuccess = getFunction(globalContext, argv[1]);
                JSValueRef onError = NULL;
-               JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
                        
-               if (argc > 2) {
+               if (argc>2) {
                        onError = getFunctionOrNull(globalContext, argv[2]);
                }
 
                cbm->setOnSuccess(onSuccess);
                cbm->setOnError(onError);
-               
-               if (JSValueIsNull(context, argv[0]) || JSValueIsUndefined(context, argv[0]) || !JSValueIsString(context, argv[0])) {
-                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
-               }
 
                Api::Filesystem::EventGetStoragePtr event(new Api::Filesystem::EventGetStorage());
                event->setLabel(converter->toString(argv[0]));
                event->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
                event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
-
+               JSValueProtect(globalContext, thisObject);
 
                Api::Filesystem::IManager::getInstance().getStorage(event);
 
        } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
        } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
        } catch(const WrtDeviceApis::Commons::Exception& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
        }
 
-       return JSValueMakeNull(context);
+       return JSValueMakeUndefined(context);
 }
 
 JSValueRef JSFilesystemManager::getStorageList(JSContextRef context,
@@ -236,12 +250,12 @@ JSValueRef JSFilesystemManager::getStorageList(JSContextRef context,
 
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
+       JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
        
        try {
 
                JSValueRef onSuccess = getFunction(globalContext, argv[0]);
                JSValueRef onError = NULL;
-               JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
                        
                if (argc > 1) {
                        onError = getFunctionOrNull(globalContext, argv[1]);
@@ -254,19 +268,22 @@ JSValueRef JSFilesystemManager::getStorageList(JSContextRef context,
                Api::Filesystem::EventListStoragesPtr event(new Api::Filesystem::EventListStorages());
                event->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
                event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
+               JSValueProtect(globalContext, thisObject);
+
+
                Api::Filesystem::IManager::getInstance().listStorages(event);
 
        } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
        } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
        } catch(const WrtDeviceApis::Commons::Exception& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
        }
 
-       return JSValueMakeNull(context);
+       return JSValueMakeUndefined(context);
 }
 
 JSValueRef JSFilesystemManager::addStorageStateListener(JSContextRef context, 
@@ -282,7 +299,7 @@ JSValueRef JSFilesystemManager::addStorageStateListener(JSContextRef context,
        }
 
        if (argc < 1) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "TYPE_MISMATCH_ERROR");
        }
 
        JSContextRef globalContext = privateObject->getContext();
@@ -290,13 +307,14 @@ JSValueRef JSFilesystemManager::addStorageStateListener(JSContextRef context,
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
 
        try {
-               if (JSValueIsNull(context, argv[0]) || JSValueIsUndefined(context, argv[0])) {
-                       cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error"));
-                       return JSValueMakeNull(context);
-               } else if (!JSObjectIsFunction(context, converter->toJSObjectRef(argv[0]))) {
-                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
+               JSValueRef onSuccess = getFunction(globalContext, argv[0]);
+               JSValueRef onError = NULL;
+               if (argc > 1) {
+                       onError = getFunctionOrNull(globalContext, argv[1]);
                }
-               cbm->setOnSuccess(argv[0]);
+
+               cbm->setOnSuccess(onSuccess);
+               cbm->setOnError(onError);
 
                Api::Filesystem::EventStorageStateChangedEmitterPtr emitter(new Api::Filesystem::EventStorageStateChangedEmitter);
                emitter->setListener(&StorageStaticController::getInstance());
@@ -304,13 +322,13 @@ JSValueRef JSFilesystemManager::addStorageStateListener(JSContextRef context,
                long id = Api::Filesystem::IManager::getInstance().addStorageStateChangeListener(emitter);
                return converter->toJSValueRefLong(id);
        } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
        } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
        } catch(const WrtDeviceApis::Commons::Exception& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
        }
 
        return JSValueMakeUndefined(context);
@@ -332,23 +350,22 @@ JSValueRef JSFilesystemManager::removeStorageStateListener(JSContextRef context,
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
        }
 
-       if (!JSValueIsNumber(context, argv[0])) {
-               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);
 
                if (id >= 0) {
                        Api::Filesystem::IManager::getInstance().removeStorageStateChangeListener(id);
                }
        } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
        } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
+       } catch (const WrtDeviceApis::Commons::NotFoundException& ex) {
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::Exception& ex) {
@@ -365,6 +382,9 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context,
                const JSValueRef argv[],
                JSValueRef* exception)
 {
+
+       LogDebug("<<<");
+
        PrivateObject* privateObject = static_cast<PrivateObject*>(JSObjectGetPrivate(thisObject));
        if (!privateObject) {
                return JSValueMakeUndefined(context);
@@ -376,42 +396,52 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context,
 
        JSContextRef globalContext = privateObject->getContext();
        ConverterPtr converter = ConverterFactory::getConverter(globalContext);
+
+       size_t index = 0;
+       JSValueRef reserveArguments[4];
+       JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);      
        
-       try {
 
-               JSValueRef onSuccess = getFunction(globalContext, argv[1]);
-               JSValueRef onError = NULL;
-               JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext);
-                       
-               if (argc > 2) {
-                       onError = getFunctionOrNull(globalContext, argv[2]);
+       try {
+               for (index = 0; index < 4; index++) {
+                       if (index < argc)
+                               reserveArguments[index] = argv[index];
+                       else 
+                               reserveArguments[index] = JSValueMakeUndefined(context);
                }
+               JSValueRef onSuccess = getFunction(globalContext, reserveArguments[1]);
+               JSValueRef onError = NULL;
+
+               onError = getFunctionOrNull(globalContext, reserveArguments[2]);
 
                cbm->setOnSuccess(onSuccess);
                cbm->setOnError(onError);
-               
-               Api::Filesystem::IPathPtr path = Utils::fromVirtualPath(globalContext, converter->toString(argv[0]));
-               std::string virtualPath = converter->toString(argv[0]);
 
-               int permissions = Api::Filesystem::PERM_READ;
+               Api::Filesystem::IPathPtr path;
+               std::string virtualPath;
+               path = Utils::fromVirtualPath(globalContext, converter->toString(reserveArguments[0]));
+               virtualPath = converter->toString(reserveArguments[0]);
+               LogDebug("virtualPath:[" << virtualPath << "]");
+               int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
 
+
+               
                if (argc > 3) {
-                       if (!JSValueIsNull(context, argv[3]) && !JSValueIsUndefined(context, argv[3])) {
-                               std::string perms = converter->toString(argv[3]);
-                               if (("r" != perms) && ("rw" != perms) && ("w" != perms) && ("a" != perms)) {
-                                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid values error");
-                               } else if ("r" == perms) {
-                                       permissions = Api::Filesystem::PERM_READ;
-                               } else {
-                                       permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
-                               }
-                               
+                       std::string perms = converter->toString(reserveArguments[3]);
+                       LogDebug("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) {
+                               permissions = Api::Filesystem::PERM_READ;
                        }
                }
 
+               if (permissions & Api::Filesystem::PERM_WRITE && virtualPath == "wgt-package") {
+                       return JSTizenExceptionFactory::postException(context, exception, JSTizenException::PERMISSION_DENIED_ERROR, "permission denied");
+               }
+
                PermissionsAccessInfo perms(permissions, virtualPath);
                AceSecurityStatus status = FILESYSTEM_PERMISSION_CHECK_ACCESS(
-                               privateObject->getContext(),
                                FILESYSTEM_FUNCTION_API_MGR_RESOLVE_ID,
                                perms);
 
@@ -421,21 +451,23 @@ JSValueRef JSFilesystemManager::resolve(JSContextRef context,
                Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(path));
                event->setForAsynchronousCall(&ResponseDispatcher::getInstance()); 
                event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (data));
+               JSValueProtect(globalContext, thisObject);
+
                Api::Filesystem::IManager::getInstance().getNode(event);
                
        } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::IO_ERROR, ex.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::NotFoundException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, ex.GetMessage()));
        } catch (const WrtDeviceApis::Commons::UnsupportedException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR, ex.GetMessage());
        } catch(const WrtDeviceApis::Commons::InvalidArgumentException& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, ex.GetMessage()));
        } catch(const WrtDeviceApis::Commons::Exception& ex) {
-               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
+               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, ex.GetMessage()));
        }
 
-       return JSValueMakeNull(context);
+       return JSValueMakeUndefined(context);
 }
 }
 }