2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
19 * @file FApp_ConditionHandler.cpp
20 * @brief This is the implementation for the _ConditionHandler class.
26 #include <FBaseRtLibrary.h>
28 #include <FBaseSysLog.h>
29 #include <FBase_StringConverter.h>
30 #include <FIo_DirectoryImpl.h>
31 #include <FApp_AppManagerImpl.h>
32 #include <FApp_AppControlManager.h>
34 #include "FApp_ConditionHandler.h"
36 namespace Tizen { namespace App {
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Io;
42 _ConditionHandler::_ConditionHandler(const String& fileName, const String& types)
43 : __typesString(types)
44 , __pluginFileName(fileName)
45 , __pPluginInstance(null)
47 , __pfDestroyPlugin(null)
49 SysLog(NID_APP, "%ls, %ls", fileName.GetPointer(), types.GetPointer() );
52 _ConditionHandler::~_ConditionHandler()
54 SysLog(NID_APP, "Enter");
55 if (__pfDestroyPlugin != null && __pPluginInstance != null)
57 __pfDestroyPlugin(__pPluginInstance);
58 __pfDestroyPlugin = null;
59 __pPluginInstance = null;
64 SysLog(NID_APP, "Exit");
66 //#define FILENAME_MAX 2048
69 _ConditionHandler::Construct()
71 SysLog(NID_APP, "Enter");
73 result r = LoadPlugin();
74 __conditionalOperations.Construct( 16, 0.75f, __strHashMapProvider, __strComparer);
76 SysLog(NID_APP, "Exit");
81 _ConditionHandler::LoadPlugin(void)
83 char filePath[FILENAME_MAX];
84 char* pFileName = _StringConverter::CopyToCharArrayN(__pluginFileName);
85 create_plugin pfCreatePlugin = null;
88 String dataPath = App::GetInstance()->GetAppRootPath() + L"lib";
89 snprintf(filePath, FILENAME_MAX, "%ls/%s", dataPath.GetPointer(), pFileName);
92 if (access(filePath, F_OK) != 0) // for uts-app
95 pFileName = _StringConverter::CopyToCharArrayN(__pluginFileName);
96 r = _AppManagerImpl::GetAppRootPath(L"aospd00043", dataPath);
97 snprintf(filePath, FILENAME_MAX, "%ls/%s", dataPath.GetPointer(), pFileName);
99 SysTryCatch(NID_APP, !IsFailed(r), r = E_SYSTEM, E_SYSTEM,
100 "[E_SYSTEM] osp-app-service should be installed.");
102 dataPath.Append(L"data");
105 __pLib = new (std::nothrow) Runtime::Library();
106 r = __pLib->Construct(filePath);
107 SysTryCatch(NID_APP, !IsFailed(r), , r, "dlopen fails (%s)", GetErrorMessage(r) );
109 pfCreatePlugin = (create_plugin)__pLib->GetProcAddress(L"CreatePlugin");
110 __pfDestroyPlugin = (destroy_plugin)__pLib->GetProcAddress(L"DestroyPlugin");
111 SysTryCatch(NID_APP, pfCreatePlugin != null && __pfDestroyPlugin != null, r = E_FAILURE, E_FAILURE, "failed to get symbols," );
113 __pPluginInstance = pfCreatePlugin();
114 SysTryCatch(NID_APP, __pPluginInstance != null, r = E_FAILURE, E_FAILURE, "pfCreatePlugin fails" );
116 __pPluginInstance->SetEventListener(*this);
121 SysLog(NID_APP, "CATCH:");
124 __pfDestroyPlugin = null;
129 _ConditionHandler::CreateUniqueId(String& uniqueId)
131 int index = 0;//TODO: improve algorithm
132 String candidateSectionName(__pluginFileName);
133 while( index < Integer::VALUE_MAX)
135 candidateSectionName.Format(1024, L"%ls_%d",__pluginFileName.GetPointer(), index );
136 if (FindItemBySectionName(candidateSectionName) == null)
139 uniqueId.Append(candidateSectionName);
149 _ConditionHandler::AddCondition( _AppLaunchCondition& operation)
151 SysTryReturnResult(NID_APP, __pPluginInstance != null, E_INVALID_STATE, "" );
154 String condition = operation.GetConditionString();
155 if (HasCondition(operation.GetAppId(), operation.GetExecutableName(), &condition))
156 return E_OBJ_ALREADY_EXIST;
158 result r = __pPluginInstance->Register(operation);
159 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] failed to register", GetErrorMessage(r) );
162 if (operation.__regSectionName.IsEmpty())
164 CreateUniqueId(uniqueId);
165 operation.__regSectionName = uniqueId;
166 _ConditionRegistryHelper::AddToRegistry(uniqueId, operation.GetAppId(), operation.GetExecutableName(), operation.GetConditionString(), operation.__pArguments );
170 // from registry conditional-operations.ini
171 uniqueId = operation.__regSectionName;
174 __conditionalOperations.Add(operation.GetConditionString(), &operation);
175 SysLog(NID_APP, "AppLaunchCondition item count (%d)", __conditionalOperations.GetCount());
181 _ConditionHandler::RemoveCondition(const AppId& appId, const String* pExecutableName, const String* pCondition)
183 SysLog(NID_APP,"Trying to unregister appId(%ls)", appId.GetPointer() );
184 SysTryReturnResult(NID_APP, __pPluginInstance != null, E_INVALID_STATE, "" );
188 IListT<_AppLaunchCondition*>* pValues = __conditionalOperations.GetValuesN();
189 SysTryReturn(NID_APP, pValues != null, GetLastResult(), GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
191 result r = E_SUCCESS;
192 for (int i = 0; i< pValues->GetCount(); i++)
196 _AppLaunchCondition *pOperation = null;
197 r = pValues->GetAt(i, pOperation);
198 SysTryCatch(NID_APP, pOperation != null, , r, "[%s] Propagated.", GetErrorMessage(r) );
199 SysLog(NID_APP,"Trying to unregister appId(%ls), pExecutableName(%ls), condition(%ls)", appId.GetPointer(), pOperation->GetExecutableName().GetPointer(), pOperation->GetConditionString().GetPointer() );
201 if ( pOperation->GetAppId() == appId
202 && ( pExecutableName == null || pOperation->GetExecutableName() == *pExecutableName )
203 && ( pCondition == null || pOperation->GetConditionString() == *pCondition ) )
205 r = __pPluginInstance->Unregister(*pOperation);
206 SysTryCatch(NID_APP, !IsFailed(r), delete pOperation, r, "[%s] failed to Unregister(%ls, %ls)", GetErrorMessage(r), appId.GetPointer(), pOperation->GetConditionString().GetPointer() );//TBC
207 _ConditionRegistryHelper::RemoveFromRegistry(pOperation->__regSectionName);
209 r = __conditionalOperations.Remove(pOperation->GetConditionString(), pOperation);
210 SysTryLog(NID_APP, !IsFailed(r), "[%s]", GetErrorMessage(r));
212 SysLog(NID_APP,"Unregistered appId(%ls), pExecutableName(%ls), condition(%ls)", appId.GetPointer(), pOperation->GetExecutableName().GetPointer(), pOperation->GetConditionString().GetPointer() );
215 if ( pCondition != null)
217 pValues->RemoveAll();
229 return (found) ? E_SUCCESS : E_OBJ_NOT_FOUND;
232 pValues->RemoveAll();
240 _ConditionHandler::HasCondition(const AppId& appId, const String& executableName, const String *pCondition) const
242 IListT<_AppLaunchCondition*>* pList = __conditionalOperations.GetValuesN();
243 SysTryReturn(NID_APP, pList != null, false, GetLastResult(), "[%s] Propagated. ", GetLastResult());
245 result r = E_SUCCESS;
246 for (int i = 0; i < pList->GetCount(); i++)
248 _AppLaunchCondition *pOperation = null;
249 r = pList->GetAt(i, pOperation);
250 SysTryCatch(NID_APP, pOperation != null, , r, "[%s] Propagated.", GetErrorMessage(r));
252 if (pOperation->GetAppId() == appId && pOperation->GetExecutableName() == executableName
253 && ( pCondition == null || pOperation->GetConditionString() == *pCondition) )
265 _ConditionHandler::CanHandleType(const String& type ) const
267 return __typesString.Contains(type);
271 _ConditionHandler::FindItemBySectionName(const String& sectionName) const
273 IListT<_AppLaunchCondition*>* pValues = __conditionalOperations.GetValuesN();
274 SysTryReturn(NID_APP, pValues != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(GetLastResult()));
276 result r = E_SUCCESS;
277 for (int i = 0; i< pValues->GetCount(); i++)
279 _AppLaunchCondition *pOperation = null;
280 r = pValues->GetAt(i, pOperation);
281 if ( !IsFailed(r) && pOperation->__regSectionName == sectionName)
290 _ConditionHandler::OnAppLaunchConditionMet(const _AppLaunchCondition& operation)
292 SysLog(NID_APP, "Enter");
294 result r = _AppControlManager::GetInstance()->LaunchAppWithCondition(operation.GetAppId(), operation.GetExecutableName(), operation.GetConditionString(), operation.__pArguments );
295 SysTryLog(NID_APP, !IsFailed(r), "[%ls] LaunchApplication failed.", GetErrorMessage(r));
297 SysLog(NID_APP, "Exit");
301 _ConditionHandler::Dump(void) const
303 SysLog(NID_APP, "[%ls]", this->__pluginFileName.GetPointer() );
305 // result r = E_SUCCESS;
306 // _AppLaunchConditionEnumerator* pEnum =__conditionalOperations.GetMapEnumeratorN();
307 // SysTryReturnVoidResult(NID_APP, pEnum != null, GetLastResult(), "[%s]", GetErrorMessage(GetLastResult()));
309 // while( pEnum->MoveNext() == E_SUCCESS )
311 // _AppLaunchCondition *pOperation = null;
312 // r = pEnum->GetValue(pOperation);
313 // SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
314 // SysTryCatch(NID_APP, pOperation != null, , r, "[%s] Propagated.", GetErrorMessage(r));
316 // SysLog(NID_APP, "appId:%ls, condition:%ls, uniqueId:%ls", pOperation->__appId.GetPointer(), pOperation->GetConditionString().GetPointer(), pOperation->__regSectionName.GetPointer() );