Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FApp_ConditionManagerStub.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file         FApp_ConditionManagerStub.cpp
20  * @brief       This is the implementation for the _ConditionManagerStub class.
21  */
22 #include <cstdio>
23 #include <dlfcn.h>
24
25 #include <FSec_AccessController.h>
26
27 #include <FBaseSysLog.h>
28 #include <FBase_StringConverter.h>
29 #include <FIo_IpcServer.h>
30 #include <FAppPkg_PackageManagerImpl.h>
31 #include <FApp_ConditionManagerIpcMessages.h>
32 #include "FApp_ConditionManagerService.h"
33 #include "FApp_ConditionManagerStub.h"
34
35
36 namespace Tizen { namespace App {
37
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Io;
41 using namespace Tizen::Security;
42
43
44 ///////////////////////////////////////////
45 // _ConditionManagerStub
46 ///////////////////////////////////////////
47
48 _ConditionManagerStub::_ConditionManagerStub()
49         :__pIpcServer(null)
50         ,__pConditionManagerService(null)
51 {
52         SysLog(NID_APP, "Enter\n");
53         SysLog(NID_APP, "Exit\n");
54 }
55
56 _ConditionManagerStub::~_ConditionManagerStub()
57 {
58         SysLog(NID_APP, "Enter\n");
59
60         if ( __pIpcServer != null)
61         {
62                 __pIpcServer->Stop();
63                 delete __pIpcServer;
64         }
65
66         SysLog(NID_APP, "Exit\n");
67 }
68
69 result
70 _ConditionManagerStub::Construct(void)
71 {
72         SysLog(NID_APP, "Enter.");
73
74         __pConditionManagerService = new (std::nothrow) _ConditionManagerService();
75
76         result r = E_SUCCESS;
77         r = __pConditionManagerService->Construct();
78         SysTryReturn(NID_APP, !IsFailed(r), r, r, "failed to __pConditionManagerService->Construct.(%s)", GetErrorMessage(r) );
79
80         r = StartIpcServer();
81         SysTryReturn(NID_APP, !IsFailed(r), r, r, "failed to StartIpcServer.(%s)", GetErrorMessage(r) );
82
83         SysLog(NID_APP, "Exit.");
84         return E_SUCCESS;
85 }
86
87 result
88 _ConditionManagerStub::StartIpcServer(void)
89 {
90         __pIpcServer = new (std::nothrow) _IpcServer();
91         SysTryReturn(NID_APP, __pIpcServer != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
92
93         result r = __pIpcServer->Construct( "osp.app.ipcserver.conditionmanager", *this);
94         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Failed to create IPC server(%s)", GetErrorMessage(r), "osp.app.ipcserver.appmanager");
95
96         return E_SUCCESS;
97
98 CATCH:
99         delete __pIpcServer;
100         __pIpcServer = null;
101         return r;
102 }
103
104 //
105 //  from XML : <Condition Name="DueTime">07/24/2012 22:52:33 60m</Condition>
106 //      from API : "DueTime='12/31/2020 21:24:59' LaunchPeriod='60'"
107 //
108 result
109 _ConditionManagerStub::ExtractDueAndPeriod(const String& src, String& dueTime, String& period)
110 {
111         DateTime tmpDate;
112         const int dateTimeLen = 19;
113
114         result r = src.SubString(0, dateTimeLen, dueTime);
115         SysTryReturn(NID_APP, IsFailed(r) == false, E_INVALID_FORMAT, E_INVALID_FORMAT, "invalid DueTime value.(%ls)", src.GetPointer());
116
117         r = DateTime::Parse(dueTime, tmpDate);
118         SysTryReturn(NID_APP, IsFailed(r) == false, E_INVALID_FORMAT, E_INVALID_FORMAT, "invalid DueTime value.(%ls)", dueTime.GetPointer());
119
120         r = src.SubString(dateTimeLen + 1, src.GetLength() -(dateTimeLen + 1) -1, period);//60
121         SysTryReturn(NID_APP, IsFailed(r) == false, E_INVALID_FORMAT, E_INVALID_FORMAT, "invalid DueTime value.(%ls)", dueTime.GetPointer());
122
123         return E_SUCCESS;
124 }
125
126 void
127 _ConditionManagerStub::OnInstallComplete(const AppId& appId, const String& executableName, const String& packageName )
128 {
129         SysTryReturnVoidResult(NID_APP, __pConditionManagerService != null, E_INVALID_STATE, "Invalid condition manager service state.");
130         SysLog(NID_APP, "Enter");
131
132         ArrayList* pArray = Tizen::App::Package::_PackageManagerImpl::GetInstance()->GetAppLaunchConditionListN(packageName);
133         SysTryReturnVoidResult(NID_APP, pArray != null, E_INVALID_STATE, "failed to GetAppLaunchConditionListN(%ls)", packageName.GetPointer());
134
135         Tizen::App::Package::_LaunchConditionInfoImpl* pCondition = null;
136         String condition;
137         String name;
138         String value;
139         result r = E_SUCCESS;
140
141         for ( int i = 0; i < pArray->GetCount(); i++)
142         {
143                 pCondition = dynamic_cast<Tizen::App::Package::_LaunchConditionInfoImpl*>(pArray->GetAt(i));
144                 if ( pCondition == null)
145                 {
146                         continue;
147                 }
148
149                 name = pCondition->GetName();
150                 value = pCondition->GetValue();
151
152                 if ( name == L"DueTime")
153                 {
154                         String dueTime;
155                         String period;
156
157                         r = ExtractDueAndPeriod(value, dueTime, period);
158                         if( IsFailed(r))
159                         {
160                                 continue;
161                         }
162
163                         condition.Format( 1024, L"DueTime='%ls' LaunchPeriod='%ls'", dueTime.GetPointer(), period.GetPointer());
164                 }
165                 else
166                 {
167                         condition = name + "='" + value + "'";
168                 }
169
170                 SysLog(NID_APP, "condition(%ls)", condition.GetPointer());
171                 __pConditionManagerService->RegisterAppLaunch(appId, executableName, condition, null, AppManager::LAUNCH_OPTION_DEFAULT, null);
172         }
173         SysLog(NID_APP, "Exit");
174 }
175
176 void
177 _ConditionManagerStub::OnUninstallComplete(const AppId& appId, const String& executableName)
178 {
179         SysTryReturnVoidResult(NID_APP, __pConditionManagerService != null, E_INVALID_STATE, "Invalid condition manager service state.");
180
181         __pConditionManagerService->UnregisterAppLaunch(appId, null);
182 }
183
184 ///////////////////////////////////////////
185 // ipc handlers
186 ///////////////////////////////////////////
187
188 bool
189 _ConditionManagerStub::OnRegisterAppLaunch(const AppId& appId, const String& executableName, const String& cond, const Tizen::Base::Collection::ArrayList& args , int flag, result *pRes)
190 {
191         SysLog(NID_APP, "(appId:%ls, executableName:%ls, cond:%ls, flag:%x)\n", appId.GetPointer(), executableName.GetPointer(), cond.GetPointer(), flag);
192 //      SysTryCatch(NID_APP, __pConditionManagerService != null, *pRes = E_INVALID_STATE, E_INVALID_STATE, "__pConditionManagerService is null!");
193         if ( __pConditionManagerService == null )
194         {
195                 SysLog(NID_APP, "__pConditionManagerService is null!");
196                 *pRes = E_INVALID_STATE;
197                 return true;
198         }
199
200         String targetAppId;
201         if ( appId.IsEmpty() == true )
202         {
203                 // APPLICATION_SERVICE
204                 targetAppId = __pIpcServer->GetClientAppId();
205         }
206         else
207         {
208                 // APPLICATION_MANAGER
209                 *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_APPMANAGER_LAUNCH);
210
211                 if (IsFailed(*pRes))
212                 {
213                         SysLog(NID_APP, "[E_PRIVILEGE_DENIED]The application does not have the privilege to call this method.");
214                         *pRes = E_PRIVILEGE_DENIED;
215                         return true;    
216                 }
217
218                 targetAppId = appId;
219         }
220
221         *pRes = __pConditionManagerService->RegisterAppLaunch(targetAppId, executableName, cond, (args.GetCount()>0)? &args : null, AppManager::LAUNCH_OPTION_DEFAULT);
222
223 //CATCH:
224         return true;
225 }
226
227 bool
228 _ConditionManagerStub::OnUnregisterAppLaunch(const AppId& appId, const String& executableName, const String& cond, result *pRes)
229 {
230         SysLog(NID_APP, "(appId:%ls, executableName:%ls, cond:%ls)\n", appId.GetPointer(), executableName.GetPointer(), cond.GetPointer());
231 //      SysTryCatch(NID_APP, __pConditionManagerService != null, *pRes = E_INVALID_STATE, E_INVALID_STATE, "__pConditionManagerService is null!");
232         if ( __pConditionManagerService == null )
233         {
234                 SysLog(NID_APP, "__pConditionManagerService is null!");
235                 *pRes = E_INVALID_STATE;
236                 return true;
237         }
238
239         String targetAppId;
240         if ( appId.IsEmpty() == true )
241         {
242                 // APPLICATION_SERVICE
243                 targetAppId = __pIpcServer->GetClientAppId();
244         }
245         else
246         {
247                 // APPLICATION_MANAGER
248                 *pRes = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_APPMANAGER_LAUNCH);
249
250                 if (IsFailed(*pRes))
251                 {
252                         SysLog(NID_APP, "[E_PRIVILEGE_DENIED]The application does not have the privilege to call this method.");
253                         *pRes = E_PRIVILEGE_DENIED;
254                         return true;    
255                 }
256
257                 targetAppId = appId;
258         }
259
260         *pRes = __pConditionManagerService->UnregisterAppLaunch(targetAppId, executableName, (cond.IsEmpty() == true) ? null : (String*)&cond);
261
262 //CATCH:
263         return true;
264 }
265
266 bool
267 _ConditionManagerStub::OnIsAppLaunchRegistered(const AppId& appId, const String& executableName, const String& cond, bool *pIsAppLaunchRegistered, result *pException)
268 {
269         if ( __pConditionManagerService == null )
270         {
271                 SysLog(NID_APP, "__pConditionManagerService is null!");
272                 *pException = E_SYSTEM;
273                 return true;
274         }
275
276         String targetAppId;
277         if ( appId.IsEmpty() == true )
278         {
279                 // NP
280                 targetAppId = __pIpcServer->GetClientAppId();
281         }
282         else
283         {
284                 // APPLICATION_MANAGER
285                 result r = _AccessController::CheckSystemPrivilege(__pIpcServer->GetClientAppId(), _PRV_APPMANAGER_LAUNCH);
286                 if(IsFailed(r))
287                 {
288                         *pException = E_PRIVILEGE_DENIED;
289                         SysTryReturn(NID_APP, !IsFailed(r), true, E_PRIVILEGE_DENIED, "The application does not have the privilege to call this method.");
290                 }
291
292                 targetAppId = appId;
293         }
294
295         *pIsAppLaunchRegistered = __pConditionManagerService->IsAppLaunchRegistered( targetAppId, executableName, (cond.IsEmpty() == true) ? null : &cond);
296         *pException = GetLastResult();
297         SysLog(NID_APP, "(appId:%s, executableName:%ls, cond:%s) is registered = (%s)\n", appId.GetPointer(), executableName.GetPointer(), cond.GetPointer(), (*pIsAppLaunchRegistered)? "true":"false");
298
299         return true;
300 }
301
302 void
303 _ConditionManagerStub::OnIpcRequestReceived(_IpcServer& server, const IPC::Message& message)
304 {
305         SysLog(NID_APP, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), server.GetClientId());
306
307         IPC_BEGIN_MESSAGE_MAP(_ConditionManagerStub, message)
308                 IPC_MESSAGE_HANDLER_EX(ConditionManager_RegisterAppLaunch, &server, OnRegisterAppLaunch)
309                 IPC_MESSAGE_HANDLER_EX(ConditionManager_UnregisterAppLaunch, &server, OnUnregisterAppLaunch)
310                 IPC_MESSAGE_HANDLER_EX(ConditionManager_IsAppLaunchRegistered, &server, OnIsAppLaunchRegistered)
311         IPC_END_MESSAGE_MAP()
312 }
313
314 void
315 _ConditionManagerStub::OnIpcServerStarted(const _IpcServer& server)
316 {
317         SysLog(NID_APP, "\n");
318 }
319
320 void
321 _ConditionManagerStub::OnIpcServerStopped(const _IpcServer& server)
322 {
323         SysLog(NID_APP, "\n");
324 }
325
326 void
327 _ConditionManagerStub::OnIpcClientConnected(const _IpcServer& server, int clientId)
328 {
329         SysLog(NID_APP, "(clientId:%d)\n", clientId);
330 }
331
332 void
333 _ConditionManagerStub::OnIpcClientDisconnected(const _IpcServer& server, int clientId)
334 {
335         SysLog(NID_APP, "(appId:%ls, pid:%d, clientId:%d)\n", server.GetClientAppId().GetPointer(), server.GetClientProcessId(), clientId);
336 }
337
338 }} //namespace Tizen { namespace App {