Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / app / FApp_AppControlProviderManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FApp_AppControlProviderManagerImpl.cpp
19  * @brief       This is the implementation for the %_AppControlProviderManagerImpl class.
20  */
21
22 #include <FBaseDataType.h>
23 #include <FBaseString.h>
24 #include <FBaseColArrayList.h>
25 #include <FBaseColIListT.h>
26 #include <FBaseErrors.h>
27 #include <FAppAppManager.h>
28 #include <FAppAppControl.h>
29 #include <FAppAppControlProviderManager.h>
30 #include <FAppIAppControlProviderEventListener.h>
31
32 #include <FBaseSysLog.h>
33
34 #include "FApp_Aul.h"
35 #include "FApp_AppArg.h"
36 #include "FApp_AppImpl.h"
37 #include "FApp_AppInfo.h"
38 #include "FApp_AppControlManager.h"
39 #include "FApp_AppControlProviderManagerImpl.h"
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Base::Collection;
43
44
45 namespace Tizen { namespace App
46 {
47
48 _AppControlProviderManagerImpl::_AppControlProviderManagerImpl(void)
49 {
50 }
51
52 _AppControlProviderManagerImpl::~_AppControlProviderManagerImpl(void)
53 {
54 }
55
56 result
57 _AppControlProviderManagerImpl::SetAppControlProviderEventListener(IAppControlProviderEventListener* pListener)
58 {
59         _AppImpl* pAppImpl = _AppImpl::GetInstance();
60         SysTryReturn(NID_APP, pAppImpl, false, E_INVALID_STATE, "[E_INVALID_STATE] Getting _AppImpl instance failed.");
61
62         return pAppImpl->SetAppControlProviderEventListener(pListener);
63 }
64
65 result
66 _AppControlProviderManagerImpl::SendAppControlResult(RequestId reqId, const IList* pResultList)
67 {
68         SysTryReturnResult(NID_APP, reqId != -1, E_OBJ_NOT_FOUND, "The application request %d is not found.", reqId);
69
70         _AppControlManager* pAppMgr = _AppControlManager::GetInstance();
71
72         _ResultInfo* pInfo = pAppMgr->__resultManager.FindItem(reqId);
73         SysTryReturnResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "The application request %d is not found.", reqId);
74
75         const _AppArg& arg = pInfo->arg;
76
77         _AppArg resArg;
78         result r = resArg.ConstructResult(arg, pResultList);
79         if (IsFailed(r))
80         {
81                 // erase _AppArg after sending the result back to the caller
82                 pAppMgr->__resultManager.RemoveItem(reqId);
83
84                 SysPropagate(NID_APP, r);
85                 return r;
86         }
87
88         r = SendAppControlResultPrimitive(reqId, resArg, 0);
89
90         // erase _AppArg after sending the result back to the caller
91         pAppMgr->__resultManager.RemoveItem(reqId);
92
93         return r;
94 }
95
96 result
97 _AppControlProviderManagerImpl::SendAppControlResult(RequestId reqId, AppCtrlResult appControlResult, const IMap* pResultMap)
98 {
99         SysTryReturnResult(NID_APP, reqId != -1, E_OBJ_NOT_FOUND, "The application request %d is not found.", reqId);
100
101         _AppControlManager* pAppMgr = _AppControlManager::GetInstance();
102
103         _ResultInfo* pInfo = pAppMgr->__resultManager.FindItem(reqId);
104         SysTryReturnResult(NID_APP, pInfo != null, E_OBJ_NOT_FOUND, "The application request %d is not found.", reqId);
105
106         const _AppArg& arg = pInfo->arg;
107
108         _AppArg resArg;
109         result r = resArg.ConstructResult(arg, pResultMap);
110         if (IsFailed(r))
111         {
112                 // erase _AppArg after sending the result back to the caller
113                 pAppMgr->__resultManager.RemoveItem(reqId);
114
115                 SysPropagate(NID_APP, r);
116                 return r;
117         }
118
119         const int code = appControlResult;
120         int res = APPSVC_RES_NOT_OK;
121         switch (code)
122         {
123         case APP_CTRL_RESULT_SUCCEEDED:
124                 res = APPSVC_RES_OK;
125                 break;
126         case APP_CTRL_RESULT_FAILED:
127                 res = APPSVC_OSP_RES_FAIL;
128                 break;
129         case APP_CTRL_RESULT_CANCELED:
130                 res = APPSVC_RES_NOT_OK;
131                 break;
132         case APP_CTRL_RESULT_TERMINATED:
133                 res = APPSVC_OSP_RES_TERMINATE;
134                 break;
135         case APP_CTRL_RESULT_ABORTED:
136                 res = APPSVC_RES_CANCEL;
137                 break;
138         default:
139                 res = APPSVC_RES_NOT_OK;
140                 break;
141         }
142
143         r = SendAppControlResultPrimitive(reqId, resArg, res);
144
145         // erase _AppArg after sending the result back to the caller
146         pAppMgr->__resultManager.RemoveItem(reqId);
147
148         return r;
149 }
150
151
152 result
153 _AppControlProviderManagerImpl::SendAppControlResultPrimitive(RequestId reqId, _AppArg& arg, int ret)
154 {
155         static bool firstRequestSent = false;
156
157         if (reqId == 0 && firstRequestSent)
158         {
159                 SysLog(NID_APP, "Sent main AppControl request already.");
160
161                 return E_SUCCESS;
162         }
163         else
164         {
165                 const bool isSubMode = _AppInfo::IsSubMode();
166                 const bool isService = _AppInfo::GetAppType() & _APP_TYPE_SERVICE_APP;
167                 bool isRaiseMode = true;
168
169                 if (reqId == 0)
170                 {
171                         firstRequestSent = true;
172                 }
173
174                 if (isSubMode || isService)
175                 {
176                         isRaiseMode = false;
177                 }
178
179                 //resArg.Print();
180                 result r = _Aul::SendResult(arg.GetBundle(), static_cast<appsvc_result_val>(ret), isRaiseMode);
181                 SysLog(NID_APP, "Sent AppControl event for %d.", reqId);
182
183                 return r;
184         }
185 }
186
187
188 AppId
189 _AppControlProviderManagerImpl::GetClientAppId(RequestId reqId) const
190 {
191         SysTryReturn(NID_APP, reqId != -1, L"", E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The application control request %d is not found.", reqId);
192
193         _ResultInfo* pInfo = _AppControlManager::GetInstance()->__resultManager.FindItem(reqId);
194         SysTryReturn(NID_APP, pInfo != null, L"", E_OBJ_NOT_FOUND, "[E_OB_NOT_FOUND] The application control request %d is not found.", reqId);
195
196         const _AppArg& arg = pInfo->arg;
197
198         return arg.GetCallerAppId();
199 }
200
201 const _AppControlProviderManagerImpl*
202 _AppControlProviderManagerImpl::GetInstance(const AppControlProviderManager& ac)
203 {
204         return ac.__pAppControlProviderManagerImpl;
205 }
206
207 _AppControlProviderManagerImpl*
208 _AppControlProviderManagerImpl::GetInstance(AppControlProviderManager& ac)
209 {
210         return ac.__pAppControlProviderManagerImpl;
211 }
212
213 } } // Tizen::App
214