refactoring
[framework/osp/appwidget-service.git] / src / FShell_AppWidgetContext.cpp
index 4e7495e..750eb47 100644 (file)
 #include <provider.h>
 #include <unique_ptr.h>
 
+#include <FBase.h>
 #include <FBaseSysLog.h>
-#include <FBaseInteger.h>
-#include <FBaseDouble.h>
-#include <FBaseColHashMap.h>
-#include <FBaseColAllElementsDeleter.h>
 #include <FSysSystemTime.h>
-#include <FAppAppManager.h>
-
+#include <FAppApp.h>
 #include <FBase_StringConverter.h>
+#include <FApp_AppManagerImpl.h>
 #include "FShell_AppWidgetManagerImpl.h"
 #include "FShell_AppWidgetManagerService.h"
 #include "FShell_AppWidgetPopupContext.h"
@@ -68,89 +65,32 @@ const wchar_t APPWIDGET_ON_TOUCH[] = L"http://tizen.org/appcontrol/appwidget/tou
 
 const int UPDATE_PERIOD_MSEC_MIN = 1800000;    // 30min
 
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// _AppContext class
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-_AppContext::_AppContext(void)
-       :__isWaitingResult(false)
-       ,__ipcClientId(0)
-       ,__connectionState(CONNECTION_STATE_NONE)
-{
-
-}
-
-_AppContext::~_AppContext(void)
-{
-
-}
-
-_ConnectionState
-_AppContext::GetConnectionState(void) const
-{
-       return __connectionState;
-}
-
-void
-_AppContext::SetConnectionState(_ConnectionState state)
-{
-       __connectionState = state;
-}
-
-int
-_AppContext::GetClientId(void) const
-{
-       return __ipcClientId;
-}
 
-void
-_AppContext::SetClientId(int clientId)
-{
-       __ipcClientId = clientId;
-}
-
-void
-_AppContext::SetWaitingStatus(bool status)
-{
-       __isWaitingResult = status;
-}
-
-bool
-_AppContext::GetWaitingStatus(void) const
-{
-       return __isWaitingResult;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-// _AppWidgetContext class
-//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-_AppWidgetContext::_AppWidgetContext(_AppContext* pAppContext, const String& userInfo, const String& providerId, const String& instanceId, int width, int height, int period, int priority)
-       :__isTouchAvailable(false)
-       ,__hasPendingRequest(false)
-       ,__isForeground(true)
+_AppWidgetContext::_AppWidgetContext(_AppContext* pAppContext, const String& instanceId, const String& providerId, int width, int height, const Tizen::Base::String& userInfo, int period, int priority)
+       :__instanceId(instanceId)
+       ,__providerId(providerId)
+       ,__appId(_AppWidgetHelper::ExtractAppId(providerId))
+       ,__providerName(_AppWidgetHelper::ExtractProviderName(providerId))
+       ,__userInfo(userInfo)
        ,__width(width)
        ,__height(height)
        ,__priority(priority)
+       ,__updateMillis( (period > UPDATE_PERIOD_MSEC_MIN) ? period : UPDATE_PERIOD_MSEC_MIN )
        ,__ipcClientId(-1)
-       ,__updateMillis(period)
+       ,__isForeground(true)
+       ,__isRemoteBufferProxyCreated(false)
        ,__lastUpdatedTime(0)
-       ,__userInfo(userInfo)
-       ,__providerId(providerId)
-       ,__instanceId(instanceId)
+       ,__hasPendingRequest(false)
        ,__pAppContext(pAppContext)
        ,__pAppWidgetPopup(null)
        ,__pAppWidgetRemoteBuffer(null)
        ,__pPendingTouchEventList(null)
-       ,__pPendingEventList(null)
 {
-       _AppWidgetManagerImpl::ExtractAppIdAndProviderName(providerId, __appId, __providerName);
-       SysSecureLog(NID_SHELL, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority);
+       SysSecureLog(NID_SHELL, "appId(%ls), providerId(%ls), instanceId(%ls), width(%d), height(%d), priority(%d), period(%d)", __appId.GetPointer(), __providerId.GetPointer(), __instanceId.GetPointer(), __width, __height, __priority, __updateMillis);
 
        // for updating period
        if (__updateMillis > 0)
        {
-               __updateMillis = (__updateMillis > UPDATE_PERIOD_MSEC_MIN) ? __updateMillis : UPDATE_PERIOD_MSEC_MIN;
-               SysLog(NID_SHELL, "period(%d)", __updateMillis);
-
                SystemTime::GetTicks(this->__lastUpdatedTime);
 
                result r = __updateTimer.Construct(*this);
@@ -164,10 +104,6 @@ _AppWidgetContext::_AppWidgetContext(_AppContext* pAppContext, const String& use
        SysTryReturnVoidResult(NID_SHELL, __pPendingTouchEventList, E_OUT_OF_MEMORY, "");
        __pPendingTouchEventList->Construct();
 
-       __pPendingEventList = new (nothrow) ArrayListT<PendingEvent*>();
-       SysTryReturnVoidResult(NID_SHELL, __pPendingEventList, E_OUT_OF_MEMORY, "");
-       __pPendingEventList->Construct();
-
        __pAppWidgetRemoteBuffer = new (nothrow) _AppWidgetRemoteBuffer(providerId, instanceId, TYPE_LB, this);
        SysTryReturnVoidResult(NID_SHELL, __pAppWidgetRemoteBuffer, E_OUT_OF_MEMORY, "");
 }
@@ -196,18 +132,6 @@ _AppWidgetContext::~_AppWidgetContext(void)
                delete __pPendingTouchEventList;
        }
 
-       if (__pPendingEventList)
-       {
-               for (int i = 0; i < __pPendingEventList->GetCount(); i++)
-               {
-                       PendingEvent* pEvent = null;
-                       __pPendingEventList->GetAt(i, pEvent);
-                       delete pEvent;
-               }
-               __pPendingEventList->RemoveAll();
-               delete __pPendingEventList;
-       }
-
        if (__pAppWidgetRemoteBuffer)
        {
                delete __pAppWidgetRemoteBuffer;
@@ -233,44 +157,69 @@ _AppWidgetContext::OnAdded(void)
 }
 
 result
-_AppWidgetContext::OnRemoved(void)
+_AppWidgetContext::OnRemoved(bool isTriggeredByViewer)
 {
-       return SendRemoveRequest();
+       if (isTriggeredByViewer)
+       {
+               return SendRemoveRequest();
+       }
+       else
+       {
+               std::unique_ptr<char[]> providerId(_StringConverter::CopyToCharArrayN(GetProviderId()));
+               std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(GetInstanceId()));
+
+               int ret = provider_send_deleted(providerId.get(), id.get());
+               SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "Failed to execute provider_send_deleted.");
+       }
+       return E_SUCCESS;
 }
 
 void
 _AppWidgetContext::OnUpdate(const String& argument)
 {
        SysLog(NID_SHELL, "argument(%ls)", argument.GetPointer());
-
        ClearLastResult();
 
-       AppWidgetManagerService* pMgrService = AppWidgetManagerService::GetInstance();
-       SysTryReturnVoidResult(NID_SHELL, pMgrService, E_SYSTEM, "[E_SYSTEM] Failed to get an instance of AppWidgetManagerService.");
+       if (this->IsForeground())
+       {
+               result r = this->SendUpdateRequest(GetWidth(), GetHeight(), argument);
+               SysTryLog(NID_SHELL, !IsFailed(r), "Failed to execute SendUpdateRequest.");
 
-       // Queueing the pointer of paused context.
-       if (!this->IsForeground())
+               SystemTime::GetTicks(this->__lastUpdatedTime);
+               SysLog(NID_SHELL, "The last updated time is %lld.", this->__lastUpdatedTime);
+
+               __hasPendingRequest = false;
+               __pendingArgument.Clear();
+       }
+       else
        {
-               // Saving the data
+               // Queueing the pointer of background context.
                if (!argument.IsEmpty())
                {
                        __pendingArgument = argument;
                }
 
                __hasPendingRequest = true;
-               SysLog(NID_SHELL, "Update is requested but the %ls is paused.", (this->GetAppId()).GetPointer());
+               SysLog(NID_SHELL, "Update is requested but the %ls is background.", (this->GetProviderId()).GetPointer());
        }
-       else
-       {
-               result r = this->SendUpdateRequest(GetWidth(), GetHeight(), argument);
-               SysTryLog(NID_SHELL, !IsFailed(r), "Failed to execute SendUpdateRequest.");
+}
 
-               SystemTime::GetTicks(this->__lastUpdatedTime);
-               SysLog(NID_SHELL, "The last updated time is %lld.", this->__lastUpdatedTime);
+result
+_AppWidgetContext::OnUpdateAsync(_AppContext* pAppContext, const String& argument)
+{
+       ArrayList* pArray = new (std::nothrow) ArrayList();
+       SysTryReturnResult(NID_SHELL, pArray, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY]");
 
-               __hasPendingRequest = false;
-               __pendingArgument.Clear();
-       }
+       pArray->Construct();
+       pArray->Add(pAppContext);
+       pArray->Add(this);
+       pArray->Add(new String(argument));
+
+       result r = Tizen::App::App::GetInstance()->SendUserEvent(LOCAL_EVENT_REQUEST_UPDATE, pArray);
+       SysTryLog(NID_SHELL, !IsFailed(r), "[%s] Propagated.", GetErrorMessage(r));
+       SysLog(NID_SHELL, "UserEvent(%d) is sent for '%ls.%ls'.", LOCAL_EVENT_REQUEST_UPDATE, GetAppId().GetPointer(), GetProviderName().GetPointer() );
+
+       return E_SUCCESS;
 }
 
 void
@@ -284,64 +233,61 @@ _AppWidgetContext::OnResize(int width, int height)
        SendResizeRequest(width, height);
 }
 
-result
-_AppWidgetContext::RequestUpdate(const String& argument)
-{
-       AppWidgetManagerService* pMgrService = AppWidgetManagerService::GetInstance();
-       SysTryReturnResult(NID_SHELL, pMgrService, E_SYSTEM, "[E_SYSTEM] Failed to get an instance of AppWidgetManagerService.");
-
-       result r = pMgrService->RequestUpdate(this, argument);
-       SysTryReturnResult(NID_SHELL, !IsFailed(r), r, "Failed to request update.");
-
-       return r;
-}
-
 void
 _AppWidgetContext::OnForeground(void)
 {
        if (IsForeground())
        {
-               SysLog(NID_SHELL, "%ls is already resumed.", (this->GetAppId()).GetPointer());
+               SysLog(NID_SHELL, "%ls is already foreground.", (this->GetAppId()).GetPointer());
                return;
        }
 
-       this->SetForeground(true);
+       __isForeground = true;
 
        if (__hasPendingRequest == true)
        {
-               RequestUpdate(__pendingArgument);
+               SysLog(NID_SHELL, "There is pending request.");
+               OnUpdateAsync(__pAppContext, __pendingArgument);
        }
        else
        {
-               if (this->GetPeriod() > 0)
+               if (IsUpdatePeriodExpired() == true)
                {
-                       long long currentTicks = 0;
-                       SystemTime::GetTicks(currentTicks);
-
-                       SysLog(NID_SHELL, "current[%lld] - updatedTime[%lld] = [%lld], period[%d]",
-                                       currentTicks, this->GetLastUpdatedTime(), currentTicks - this->GetLastUpdatedTime(), this->GetPeriod());
-
-                       bool isPeriodExpired = (currentTicks - this->GetLastUpdatedTime()) >= (this->GetPeriod());
-                       if (isPeriodExpired)
-                       {
-                               SysLog(NID_SHELL, "The period is expired.");
-                               RequestUpdate(L"");
-                       }
+                       SysLog(NID_SHELL, "The period is expired.");
+                       OnUpdateAsync(__pAppContext, L"");
                }
        }
 }
 
+bool
+_AppWidgetContext::IsUpdatePeriodExpired(void) const
+{
+       if (this->GetPeriod() <= 0)
+       {
+               return false;
+       }
+
+       long long currentTicks = 0;
+       SystemTime::GetTicks(currentTicks);
+
+       SysLog(NID_SHELL, "current[%lld] - last updated time[%lld] = [%lld], period[%d]",
+                       currentTicks, this->GetLastUpdatedTime(), currentTicks - this->GetLastUpdatedTime(), this->GetPeriod());
+
+       bool isPeriodExpired = (currentTicks - this->GetLastUpdatedTime()) >= (this->GetPeriod());
+       return isPeriodExpired;
+}
+
 void
 _AppWidgetContext::OnBackground(void)
 {
-       SysLog(NID_SHELL, "OnBackground");
-
        if (!IsForeground())
        {
-               SysLog(NID_SHELL, "%ls is already paused.", (this->GetAppId()).GetPointer());
+               SysLog(NID_SHELL, "%ls is already background.", (this->GetAppId()).GetPointer());
                return;
        }
-       SetForeground(false);
+
+       SysLog(NID_SHELL, "OnBackground");
+       __isForeground = false;
 }
 
 void
@@ -360,69 +306,43 @@ _AppWidgetContext::OnPopupDestoyed(void)
 
        if (__pAppWidgetPopup)
        {
-//             __pAppWidgetPopup->OnPopupDestoyed();
                delete __pAppWidgetPopup;
                __pAppWidgetPopup = null;
        }
 }
 
 result
-_AppWidgetContext::SendAddRequest(int width, int height)
+_AppWidgetContext::SendAddRequest(int width, int height) const
 {
        std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
 
-       return SendRequestToApp(GetAppId(), APPWIDGET_ON_ADD, pArgs.release());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_ADD, pArgs.release());
 }
 
 result
-_AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument)
+_AppWidgetContext::SendUpdateRequest(int width, int height, const String& argument) const
 {
        std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
 
        pArgs->Add(new (nothrow) String(ARG_KEY_ARGUMENT), new (nothrow) String(argument));
 
-       return SendRequestToApp(GetAppId(), APPWIDGET_ON_UPDATE, pArgs.release());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_UPDATE, pArgs.release());
 }
 
 result
-_AppWidgetContext::SendResizeRequest(int width, int height)
+_AppWidgetContext::SendResizeRequest(int width, int height) const
 {
        std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN() );
 
-       return SendRequestToApp(GetAppId(), APPWIDGET_ON_RESIZE, pArgs.release());
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_RESIZE, pArgs.release());
 }
 
 result
-_AppWidgetContext::SendRemoveRequest(void)
+_AppWidgetContext::SendRemoveRequest(void) const
 {
        std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN());
 
-       return SendRequestToApp(GetAppId(), APPWIDGET_ON_REMOVE, pArgs.release());
-}
-
-result
-_AppWidgetContext::SendPendingEvent(void)
-{
-       SysTryReturnResult(NID_SHELL, __pPendingEventList->GetCount() > 0 , E_DATA_NOT_FOUND, "There is no pending event.");
-
-       PendingEvent* pEvent = null;
-       __pPendingEventList->GetAt(0, pEvent);
-
-       if (pEvent == null)
-       {
-               SysLog(NID_SHELL, "SendPendingEvent by IPC [0 existed.]");
-               GetAppContext()->SetWaitingStatus(false);
-               return E_SYSTEM;
-       }
-       SysLog(NID_SHELL, "SendPendingEvent by IPC [%d existed.]", __pPendingEventList->GetCount());
-
-       __pPendingEventList->RemoveAt(0);
-       result r = _AppWidgetRequestHelper::SendIpcRequest(GetClientId(), pEvent->operation, pEvent->pArg);
-       GetAppContext()->SetWaitingStatus(false);
-       delete pEvent;
-       SysTryReturnResult(NID_SHELL, !IsFailed(r), E_SYSTEM, "SendIpcRequest failed.");
-
-       return E_SUCCESS;
+       return __pAppContext->SendRequestToApp(GetAppId(), APPWIDGET_ON_REMOVE, pArgs.release());
 }
 
 void
@@ -441,7 +361,7 @@ _AppWidgetContext::SendPendingTouchEvent(void)
 void
 _AppWidgetContext::OnTouchEventReceived(buffer_event event, double timestamp, double x, double y)
 {
-       if (__pAppContext->GetConnectionState() == CONNECTION_STATE_CONNECTED && __isTouchAvailable)
+       if (__pAppContext->GetConnectionState() == CONNECTION_STATE_CONNECTED && __isRemoteBufferProxyCreated)
        {
                SysLog(NID_SHELL, "%d, %f, %f", event, x, y);
                AppWidgetManagerService::GetInstance()->SendTouchEvent(GetClientId(), GetInstanceId(), event, timestamp, x, y);
@@ -450,7 +370,8 @@ _AppWidgetContext::OnTouchEventReceived(buffer_event event, double timestamp, do
        {
                __pPendingTouchEventList->Add(new (nothrow) PendingTouchEvent(event, timestamp, x, y));
 
-               if( AppManager::GetInstance()->IsRunning(this->GetAppId() ) == false)
+               if( _AppManagerImpl::GetInstance()->IsRunning(this->GetAppId() ) == false ||
+                       ( __pAppContext->GetConnectionState() == CONNECTION_STATE_CONNECTED && __isRemoteBufferProxyCreated == false))
                {
                        SysLog(NID_SHELL, "request to start AppControl");
                        std::unique_ptr<HashMap, AllElementsDeleter> pArgs (CreateRequestArgsN() );
@@ -461,53 +382,12 @@ _AppWidgetContext::OnTouchEventReceived(buffer_event event, double timestamp, do
                        pArgs->Add(new (nothrow) String(ARG_KEY_X), new (nothrow) String(Double::ToString(x)));
                        pArgs->Add(new (nothrow) String(ARG_KEY_Y), new (nothrow) String(Double::ToString(y)));
 
-                       SendRequestToApp(GetAppId(), APPWIDGET_ON_TOUCH, pArgs.get());
+                       _AppWidgetRequestHelper::SendAppControlRequest(GetAppId(), APPWIDGET_ON_TOUCH, pArgs.get());
                }
        }
 }
 
 result
-_AppWidgetContext::SendRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs)
-{
-       result r = E_SUCCESS;
-
-       if ( __pAppContext->GetConnectionState() == CONNECTION_STATE_NONE || __pAppContext->GetConnectionState() == CONNECTION_STATE_DISCONNECTED)
-       {
-               SysLog(NID_SHELL, "The application is not running.");
-               result r = _AppWidgetRequestHelper::SendAppControlRequest(appId, operation, pArgs);
-               SysTryReturn(NID_SHELL, !IsFailed(r), r, r, "[%s] Failed to SendRequestToApp", GetErrorMessage(r));
-
-               __pAppContext->SetWaitingStatus(true);
-               __pAppContext->SetConnectionState(CONNECTION_STATE_CONNECTING);
-       }
-       else
-       {
-               if ( __pAppContext->GetConnectionState() == CONNECTION_STATE_CONNECTING || __pAppContext->GetWaitingStatus() == true)
-               {
-                       SysLog(NID_SHELL, "The application is running but IPC is not connected yet.");
-                       __pPendingEventList->Add(new (nothrow) PendingEvent(operation, pArgs));
-               }
-               else
-               {
-                       SysLog(NID_SHELL, "The application is running and IPC is connected.");
-                       r  = _AppWidgetRequestHelper::SendIpcRequest(GetClientId(), operation, pArgs);
-
-                       pArgs->RemoveAll(true);
-                       delete pArgs;
-                       __pAppContext->SetWaitingStatus(true);
-               }
-       }
-
-       return E_SUCCESS;
-}
-
-result
-_AppWidgetContext::SendPopupRequestToApp(const AppId& appId, const String& operation, HashMap* pArgs) const
-{
-       return _AppWidgetRequestHelper::SendAppControlRequest(appId, operation, pArgs);;
-}
-
-result
 _AppWidgetContext::SyncRemoteBuffer(int width, int height)
 {
        std::unique_ptr<char[]> providerId(_StringConverter::CopyToCharArrayN(GetProviderId()));
@@ -523,13 +403,21 @@ _AppWidgetContext::SyncRemoteBuffer(int width, int height)
 }
 
 result
+_AppWidgetContext::SyncRemoteBufferForPD(void) const
+{
+       _AppWidgetPopupContext* pPopupContext = GetAppWidgetPopup();
+       SysSecureTryReturnResult(NID_SHELL, pPopupContext , E_SYSTEM, "pPopupContext is null for (%ls)", GetProviderId().GetPointer());
+
+       return pPopupContext->SyncRemoteBuffer();
+}
+
+result
 _AppWidgetContext::SendAccessStatus(int accessStatus) const
 {
        std::unique_ptr<char[]> providerId(_StringConverter::CopyToCharArrayN(GetProviderId()));
        std::unique_ptr<char[]> id(_StringConverter::CopyToCharArrayN(GetInstanceId()));
 
        int ret = provider_send_access_status(providerId.get(), id.get(), accessStatus);
-       SysLog(NID_SHELL, "[LB_ACCESS] provider_send_access_status is called. : ret = %d, status = %d", ret, accessStatus);
        SysTryReturnResult(NID_SHELL, ret >= 0 , E_SYSTEM, "[E_SYSTEM] failed to provider_send_access_status");
 
        return E_SUCCESS;
@@ -628,12 +516,6 @@ _AppWidgetContext::GetProviderName(void) const
        return __providerName;
 }
 
-void
-_AppWidgetContext::SetForeground(bool foreground)
-{
-       __isForeground = foreground;
-}
-
 bool
 _AppWidgetContext::IsForeground(void) const
 {
@@ -671,7 +553,9 @@ _AppWidgetContext::OnTimerExpired(Tizen::Base::Runtime::Timer& timer)
 void
 _AppWidgetContext::OnDisconnected(void)
 {
-       __isTouchAvailable = false;
+       SysSecureLog(NID_SHELL, "%ls, %ls", GetInstanceId().GetPointer(), GetProviderId().GetPointer());
+
+       __isRemoteBufferProxyCreated = false;
        if (GetAppWidgetPopup())
        {
                OnPopupDestoyed();
@@ -684,9 +568,21 @@ _AppWidgetContext::AcquireRemoteBuffer(int w, int h) const
        int id = __pAppWidgetRemoteBuffer->Acquire(w, h);
        if (id != -1)
        {
-               __isTouchAvailable = true;
+               __isRemoteBufferProxyCreated = true;
        }
        return id;
 }
 
+int
+_AppWidgetContext::AcquireRemoteBufferForPD(int w, int h) const
+{
+       _AppWidgetPopupContext* pPopupContext = GetAppWidgetPopup();
+       SysSecureTryReturnResult(NID_SHELL, pPopupContext , E_SYSTEM, "pPopupContext is null for (%ls)", GetProviderId().GetPointer());
+
+       int bufferId = pPopupContext->AcquireRemoteBuffer(w, h);
+       SysSecureTryReturnResult(NID_SHELL, bufferId != -1, E_SYSTEM, "Failed to AcquireRemoteBuffer for (%ls)", GetProviderId().GetPointer());
+
+       return bufferId;
+}
+
 }}}  // Tizen::Shell::App