proper headers
[platform/framework/native/appfw.git] / src / app / FApp_MapDataControlImpl.cpp
old mode 100755 (executable)
new mode 100644 (file)
index 862fa0e..7ee27e4
@@ -1,5 +1,4 @@
 //
-// Open Service Platform
 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
 //
 // Licensed under the Apache License, Version 2.0 (the License);
  */
 
 #include <typeinfo>
-#include <new>
 #include <unique_ptr.h>
 
 #include <appsvc/appsvc.h>
 
 #include <FBaseInteger.h>
 #include <FBaseString.h>
+#include <FBaseColLinkedList.h>
 #include <FBaseRtIEventArg.h>
 #include <FIoFile.h>
 #include <FAppMapDataControl.h>
@@ -52,6 +51,7 @@ namespace Tizen { namespace App
 {
 
 static const int MAX_REQUEST_COUNT = 128;
+static const int REQUEST_THRESHOLD = 100;
 static const int _MAX_ARGUMENT_SIZE = 16384; // 16KB
 
 class _MapDataControlEventArg
@@ -92,7 +92,7 @@ class _MapDataControlEvent
        : public Event
 {
 protected:
-               virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
+       virtual void FireImpl(IEventListener& listener, const IEventArg& arg);
 };
 
 void
@@ -131,9 +131,7 @@ _MapDataControlEvent::FireImpl(IEventListener& listener, const IEventArg& arg)
 
 // private
 _MapDataControlImpl::_MapDataControlImpl(void)
-       : __appId(L"")
-       , __providerId(L"")
-       , __access(_DATACONTROL_ACCESS_UNDEFINED)
+       : __access(_DATACONTROL_ACCESS_UNDEFINED)
        , __pPreviousListener(null)
        , __pMapDataControlEvent(null)
 {
@@ -142,6 +140,19 @@ _MapDataControlImpl::_MapDataControlImpl(void)
 _MapDataControlImpl::~_MapDataControlImpl(void)
 {
        delete __pMapDataControlEvent;
+
+       _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
+
+       IEnumerator* pEnum = __pRequestList->GetEnumeratorN();
+       while (pEnum->MoveNext() == E_SUCCESS)
+       {
+               Integer* pReqId = dynamic_cast< Integer* >(pEnum->GetCurrent());
+               if (pReqId != null)
+               {
+                       pDcMgr->RemoveRequestInfo(*pReqId);
+               }
+       }
+       delete __pRequestList;
 }
 
 _MapDataControlImpl*
@@ -164,12 +175,25 @@ _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int*
        result r = E_SUCCESS;
 
        int req = -1;
-       _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
+       _DataControlManager* pDcMgr = _DataControlManager::GetInstance();
 
        // Check the request count of DataControl operation
-       int count = pAppManagerImpl->GetLaunchRequestCount();
+       int count = pDcMgr->GetRequestCount();
        SysLog(NID_APP, "Current launch request count: %d", count);
 
+       if (count > REQUEST_THRESHOLD)
+       {
+               _AppManagerImpl* pImpl = _AppManagerImpl::GetInstance();
+
+               // Clear the request queue if the provider is terminated
+               if (!pImpl->IsRunning(__appId))
+               {
+                       SysLog(NID_APP, "The request queue is cleared due to the invalid provider.");
+
+                       pDcMgr->RemoveAllRequests();
+               }
+       }
+
        SysTryReturnResult(NID_APP, count < MAX_REQUEST_COUNT, E_MAX_EXCEEDED, "The number of requests has exceeded the maximum limit.");
 
        _AppArg* pArg = new(std::nothrow) _AppArg; // XXX: pArg will be released in _AppManagerImpl::LaunchApp().
@@ -177,6 +201,8 @@ _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int*
 
        pArg->Construct(*this, static_cast <_DataControlRequestType>(type), pDataList);
 
+       _AppControlManager* pAppManagerImpl = _AppControlManager::GetInstance();
+
        if (__pMapDataControlEvent)
        {
                // reqId is system-wide id because the bundle is system-wide.
@@ -193,8 +219,9 @@ _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int*
                                "[E_OUT_OF_MEMORY] The memory is insufficient");
 
                pReqInfo = new (std::nothrow) _DataControlRequestInfo(__pMapDataControlEvent);
-               SysTryCatch(NID_APP, pReqId != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
+               SysTryCatch(NID_APP, pReqInfo != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
                                "[E_OUT_OF_MEMORY] The memory is insufficient");
+               pReqInfo->SetMapDataControlImpl(this);
 
                r = pDcMgr->AddRequestInfo(pReqId, pReqInfo);
                SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM, "[%s] Failed to add request info", GetErrorMessage(r));
@@ -205,10 +232,11 @@ _MapDataControlImpl::StartMapDataControl(int type, const IList* pDataList, int*
                        SysPropagate(NID_APP, r);
                        reqObj.Invalidate();
                        pDcMgr->RemoveRequestInfo(*pReqId);
-                       delete pArg;
                        return r;
                }
 
+               __pRequestList->Add(new (std::nothrow) Integer(req));
+
                if (pReq)
                {
                        *pReq = req;
@@ -252,7 +280,7 @@ _MapDataControlImpl::GetValue(const String& dataId, const String& key,
        long long argSize = dataId.GetLength() * sizeof(wchar_t);
        argSize += key.GetLength() * sizeof(wchar_t);
        SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
-                       "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
+                       "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
 
        pPageNo = new (std::nothrow) String();
        pPageNo->Append(pageNo);
@@ -316,15 +344,24 @@ _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg*
 
                _DataControlManager* pDcMgr = static_cast< _DataControlManager* >(data);
                _DataControlRequestInfo* pReqInfo = pDcMgr->GetRequestInfo(key);
-               SysTryCatch(NID_APP, pReqInfo != null, r = E_SYSTEM, E_SYSTEM,
-                               "[E_SYSTEM] Failed to get request info");
+               if (pReqInfo == null)
+               {
+                       SysLog(NID_APP, "No request info of reqId %d", reqId);
+                       return E_SUCCESS;
+               }
 
                _MapDataControlEvent* pMapDataControlEvent = dynamic_cast< _MapDataControlEvent* >(pReqInfo->GetEvent());
-               SysTryCatch(NID_APP, pMapDataControlEvent != null, r = E_SYSTEM, E_SYSTEM,
-                               "[E_SYSTEM] invalid request info");
+               SysTryCatch(NID_APP, pMapDataControlEvent != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request info");
 
                pDcMgr->RemoveRequestInfo(key);
 
+               _MapDataControlImpl* pDcImpl = pReqInfo->GetMapDataControlImpl();
+               SysTryCatch(NID_APP, pDcImpl != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid request info");
+               pDcImpl->__pRequestList->Remove(key);
+
+               // Remove the request from the queue
+               SysLog(NID_APP, "Remove the request, req: %d", reqId);
+
                if (pMapDataControlEvent != null && typeid(pMapDataControlEvent) == typeid(_MapDataControlEvent*))
                {
 
@@ -364,7 +401,7 @@ _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg*
 
                        version = pResArg->GetValue(OSP_K_DATACONTROL_PROTOCOL_VERSION);
 
-                       SysLog(NID_APP, "[DC_CALLER_RECV] version: %ls, provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls",
+                       SysSecureLog(NID_APP, "[DC_CALLER_RECV] version: %ls, provider result: %ld, requestType: %d, req: %d, provider: %ls, data: %ls, errorMsg: %ls",
                                        version.GetPointer(), providerRes, requestType, reqId, pProviderId->GetPointer(), pDataId->GetPointer(), pErrorMsg->GetPointer());
 
                        switch (static_cast< _DataControlRequestType >(requestType))
@@ -384,7 +421,7 @@ _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg*
 
                                        if (resultCount > 0)
                                        {
-                                               if (version == L"ver_2.1.0.1")
+                                               if (version == L"ver_2.1.0.1" || version == L"ver_2.1.0.2" || version == L"ver_2.1.0.3")
                                                {
                                                        pPath = dynamic_cast< String* >(pResultList->GetAt(3)); // result list[3]
                                                        SysTryCatch(NID_APP, pPath, delete pResultValueList, E_SYSTEM, "[E_SYSTEM] invalid result");
@@ -491,10 +528,6 @@ _MapDataControlImpl::MapDataControlCallback(void* data, _AppArg* pArg, _AppArg*
        SysTryCatch(NID_APP, p, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] invalid bundle");
        launchReqId = atoi(p);
 
-       // Remove the request count
-       SysLog(NID_APP, "Remove a launch request: req: %d", launchReqId);
-       _AppControlManager::GetInstance()->RemoveLaunchRequest(launchReqId);
-
        return E_SUCCESS;
 
 CATCH:
@@ -529,7 +562,7 @@ _MapDataControlImpl::AddValue(const String& dataId, const String& key,
        argSize += key.GetLength() * sizeof(wchar_t);
        argSize += value.GetLength() * sizeof(wchar_t);
        SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
-                       "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
+                       "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
 
        r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_INSERT, pArgList, &id);
        SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
@@ -570,7 +603,7 @@ _MapDataControlImpl::SetValue(const String& dataId, const String& key,
        argSize += oldValue.GetLength() * sizeof(wchar_t);
        argSize += newValue.GetLength() * sizeof(wchar_t);
        SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
-                       "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
+                       "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
 
        r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_UPDATE, pArgList, &id);
        SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
@@ -609,7 +642,7 @@ _MapDataControlImpl::RemoveValue(const String& dataId, const String& key,
        argSize += key.GetLength() * sizeof(wchar_t);
        argSize += value.GetLength() * sizeof(wchar_t);
        SysTryCatch(NID_APP, argSize <= _MAX_ARGUMENT_SIZE, r = E_MAX_EXCEEDED, E_MAX_EXCEEDED,
-                       "[E_MAX_EXCEEDED] The size of sending argument (%d) exceeds the maximum limit.", argSize);
+                       "[E_MAX_EXCEEDED] The size of sending argument (%lld) exceeds the maximum limit.", argSize);
 
        r = StartMapDataControl(_DATACONTROL_REQUEST_TYPE_MAP_DELETE, pArgList, &id);
        SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating to caller...", GetErrorMessage(r));
@@ -671,12 +704,15 @@ _MapDataControlImpl::CreateMapDataControl(const AppId& appId, const String& prov
        SysTryReturn(NID_APP, pDc != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient");
 
        _MapDataControlImpl* pDcImpl = _MapDataControlImpl::GetInstance(*pDc);
+       pDcImpl->__appId = appId;
+       pDcImpl->__providerId = providerId;
        unique_ptr<_MapDataControlEvent> pMapDataControlEvent(new (std::nothrow) _MapDataControlEvent);
        SysTryReturn(NID_IO, pMapDataControlEvent != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
 
+       pDcImpl->__pRequestList = new (std::nothrow) LinkedList(SingleObjectDeleter);
+       SysTryReturn(NID_IO, pDcImpl->__pRequestList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
+
        pDcImpl->__pMapDataControlEvent = pMapDataControlEvent.release();
-       pDcImpl->__appId = appId;
-       pDcImpl->__providerId = providerId;
 
        if (access == L"readonly")
        {