Merge "BigInteger class implementation" into tizen
[platform/framework/native/appfw.git] / src / app / FAppAppResource.cpp
index 833d2f4..3d7474b 100644 (file)
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
  * @brief      This is the implementation for the AppResource class.
  */
 
-#include <unique_ptr.h>
-
 #include <FBaseResult.h>
+#include <FBaseSysLog.h>
 #include <FAppAppResource.h>
 
-#include <FBaseSysLog.h>
-#include "FApp_Aul.h"
 #include <FSec_AccessController.h>
 #include "FApp_AppResourceImpl.h"
+#include "FApp_AppInfo.h"
+#ifdef _SINGLETON_CLEANUP
+#include "FApp_LongevityManager.h"
+#endif
 
 using namespace Tizen::Base;
-using namespace Tizen::Base::Collection;
 using namespace Tizen::Graphics;
 using namespace Tizen::Security;
 
 namespace Tizen { namespace App
 {
 
-Tizen::Base::Collection::HashMap* AppResource::pContainer = null;
-
 AppResource::AppResource(void)
        : __pAppResourceImpl(null)
 {
@@ -51,29 +48,10 @@ AppResource::~AppResource(void)
        delete __pAppResourceImpl;
 }
 
-
-result
-AppResource::Construct(void)
-{
-       SysAssertf(__pAppResourceImpl == null,
-                       "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
-
-       __pAppResourceImpl = new (std::nothrow) _AppResourceImpl();
-       SysTryReturnResult(NID_APP, __pAppResourceImpl != null, E_OUT_OF_MEMORY, "Allocation failed.");
-
-       result r = __pAppResourceImpl->Construct();
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
-
-       return E_SUCCESS;
-
-}
-
-
 result
 AppResource::GetString(const String& resourceId, String& loadedString) const
 {
        SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
-
        return __pAppResourceImpl->GetString(resourceId, loadedString);
 }
 
@@ -82,7 +60,6 @@ Bitmap*
 AppResource::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat) const
 {
        SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
-
        return __pAppResourceImpl->GetBitmapN(imgFilePath, pixelFormat);
 }
 
@@ -91,7 +68,6 @@ Bitmap*
 AppResource::GetBitmapN(const String& imgFilePath) const
 {
        SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
-
        return __pAppResourceImpl->GetBitmapN(imgFilePath);
 }
 
@@ -99,26 +75,31 @@ AppResource::GetBitmapN(const String& imgFilePath) const
 AppResource*
 AppResource::GetInstance(void)
 {
-       result r = E_SUCCESS;
-       static AppResource* pAppResource = null;
-
-       if (pAppResource == null)
+       static AppResource* pSelfAppResource = null;
+       if (pSelfAppResource == null)
        {
-               pAppResource = new (std::nothrow) AppResource();
-               SysTryCatch(NID_APP, pAppResource != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] app resource allocation failed.");
+               pSelfAppResource = _AppResourceImpl::GetInstanceN(APP_RESOURCE_DEFAULT);
 
-               r = pAppResource->Construct();
-               SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Initialization of app resource failed.", GetErrorMessage(r));
+#ifdef _SINGLETON_CLEANUP
+               _LongevityManager::GetInstance().RegisterOwnership(*pSelfAppResource);
+#endif
        }
 
-       SetLastResult(r);
-       return pAppResource;
+//     ClearLastResult();      // as this method does not register any exception.
+       return pSelfAppResource;
+}
+
+AppResource*
+AppResource::LoadAppResource(const Tizen::Base::String& resourcePath)
+{
+       SysTryReturn(NID_APP, !resourcePath.IsEmpty(), null, E_INVALID_ARG, "Resource path is empty");
 
-CATCH:
-       delete pAppResource;
-       pAppResource = null;
+       AppResource* pAppResource = _AppResourceImpl::GetInstanceN(APP_RESOURCE_BY_LIBRARY_NAME, resourcePath);
+       if (GetLastResult() == E_DATA_NOT_FOUND)
+       {
+               SetLastResult(E_FILE_NOT_FOUND);
+       }
 
-       SetLastResult(r);
        return pAppResource;
 }
 
@@ -126,125 +107,38 @@ AppResource*
 AppResource::GetInstanceByAppId(const AppId& appId)
 {
        result r = E_SUCCESS;
-       
+
        r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
        SysTryReturn(NID_APP, r == E_SUCCESS, null, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
 
-       SysTryReturn(NID_APP, _Aul::IsInstalled(appId) == true, null, E_APP_NOT_INSTALLED, "Application is not installed.", GetErrorMessage(E_APP_NOT_INSTALLED));
-
-       if (pContainer == null)
-       {
-               std::unique_ptr< HashMap > pContainerMap(new (std::nothrow) HashMap());
-               SysTryReturn(NID_APP, pContainerMap != null,
-                               null, E_OUT_OF_MEMORY, "[%s] Creating the container is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-
-               r = pContainerMap->Construct();
-               SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[E_SYSTEM] Creating the container is failed [%s].", GetErrorMessage(r));
-
-               pContainer = pContainerMap.release();
-       }
+       SysTryReturn(NID_APP, !appId.IsEmpty(), null, E_APP_NOT_INSTALLED, "Invalid app id");
 
        String packageId;
        r = appId.SubString(0, 10, packageId);
        SysTryReturn(NID_APP, !IsFailed(r), null,
-                       E_APP_NOT_INSTALLED, "[%s] Failed to get package info", GetErrorMessage(E_APP_NOT_INSTALLED));
-
-       bool hasAppResource = false;
-       r = pContainer->ContainsKey(packageId, hasAppResource);
-       SysTryReturn(NID_APP, !IsFailed(r), null, r, "[%s] Checking to contain is failed.", GetErrorMessage(r));
-
-       if(hasAppResource == true)
-       {
-               SetLastResult(r);
-               return static_cast<AppResource*> (pContainer->GetValue(packageId));
-       }
-       else
-       {
-               std::unique_ptr< String > pStr(new (std::nothrow) String(packageId));
-               SysTryReturn(NID_APP, pStr != null, null,
-                               E_OUT_OF_MEMORY, "[%s] Creating a key is failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-
-               AppResource* pAppResource = new (std::nothrow) AppResource;
-               SysTryReturn(NID_APP, pAppResource != null, null,
-                               E_OUT_OF_MEMORY, "[%s] Unable to allocate memory for AppResource", GetErrorMessage(E_OUT_OF_MEMORY));
-
-               r = pAppResource->Construct(packageId);
-               SysTryCatch(NID_APP, !IsFailed(r), ,
-                               E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
-
-               r = pContainer->Add(*pStr, *pAppResource);
-               SysTryCatch(NID_APP, !IsFailed(r), ,
-                               E_SYSTEM, "[E_SYSTEM] Adding an element to the container is failed [%s].", GetErrorMessage(r));
-
-               SetLastResult(r);
-               pStr.release();
-               return pAppResource;
-CATCH:
-               delete pAppResource;
-               return null;
-       }
+                       E_APP_NOT_INSTALLED, "[%s] Failed to get the package info", GetErrorMessage(E_APP_NOT_INSTALLED));
+       return _AppResourceImpl::GetInstanceN(APP_RESOURCE_BY_APP_ID, packageId);
 }
 
 result
 AppResource::ReleaseInstanceByAppId(const AppId& appId)
 {
        result r = E_SUCCESS;
-       AppResource* pAppResource = null;
 
        r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
-       SysTryReturn(NID_APP, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
+       SysTryReturnResult(NID_APP, r == E_SUCCESS, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
 
-       SysTryReturnResult(NID_APP, _Aul::IsInstalled(appId) == true, E_OBJ_NOT_FOUND, "Applicationis not installed.");
-
-       if (pContainer != null)
+       if (appId == _AppInfo::GetApplicationId() || appId == _AppInfo::GetPackageId())
        {
-               String packageId;
-               r = appId.SubString(0, 10, packageId);
-               SysTryReturn(NID_APP, !IsFailed(r), E_OBJ_NOT_FOUND,
-                               E_OBJ_NOT_FOUND, "[%s] Failed to get package info", GetErrorMessage(E_OBJ_NOT_FOUND));
-
-               pAppResource = static_cast<AppResource*> (pContainer->GetValue(packageId));
-               delete pAppResource;
-               r = pContainer->Remove(packageId);
+               SysLog(NID_APP, "Ignoring key[%ls] with current instance", appId.GetPointer());
+               return E_SUCCESS;
        }
 
-       return r;
-}
-
-result
-AppResource::Construct(const AppId& appId)
-{
-       SysAssertf(__pAppResourceImpl == null,
-                       "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
-
-       __pAppResourceImpl = new (std::nothrow) _AppResourceImpl();
-       SysTryReturnResult(NID_APP, __pAppResourceImpl != null, E_OUT_OF_MEMORY, "Allocation failed.");
-
-       result r = __pAppResourceImpl->Construct(appId);
-       SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
-
-       return E_SUCCESS;
-
-}
-
-void
-AppResource::Reinitialize(void)
-{
-       if (pContainer != null)
-       {
-               IMapEnumerator* pMapEnum = pContainer->GetMapEnumeratorN();
-               while(pMapEnum->MoveNext() == E_SUCCESS)
-               {
-                       AppResource* pAppResource = static_cast<AppResource*> (pMapEnum->GetValue());
-                       String* pStr = static_cast<String*> (pMapEnum->GetKey());
-                       pAppResource->Reinitialize(*pStr);
-               }
-       }
+       String packageId;
+       r = appId.SubString(0, 10, packageId);
+       SysTryReturn(NID_APP, !IsFailed(r), null,
+                       E_OBJ_NOT_FOUND, "[%s] Failed to get the package info", GetErrorMessage(E_OBJ_NOT_FOUND));
+       return _AppResourceImpl::ReleaseInstance(packageId);
 }
 
-void
-AppResource::Reinitialize(const AppId& appId)
-{
-       __pAppResourceImpl->Reinitialize(appId);
-}
 } } // Tizen::App