2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FApp_AppControlRegistry.cpp
19 * @brief This is the implementation for the _AppControlRegistry.cpp class.
23 #include <unique_ptr.h>
27 #include <FBaseErrors.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseColAllElementsDeleter.h>
30 #include <FBaseColHashMap.h>
31 #include <FAppAppControl.h>
32 #include <FIoRegistry.h>
35 #include <FBase_StringConverter.h>
36 #include <FIo_RegistryImpl.h>
38 #include "FAppPkg_PackageManagerImpl.h"
39 #include "FApp_AppControlImpl.h"
41 #include "FApp_AppControlRegistry.h"
42 #include "FApp_AppMessageImpl.h"
43 #include "FApp_TemplateUtil.h"
44 #ifdef _SINGLETON_CLEANUP
45 #include "FApp_LongevityManager.h"
48 using namespace Tizen::App::Package;
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Io;
56 const wchar_t ACTL_DEFAULT_FILE[] = L"/usr/etc/app-control-info.ini";
57 const wchar_t ACTL_LEGACY_FILE[] = L"/usr/etc/app-control-alias.ini";
58 const wchar_t ACTL_ALIAS_FILE[] = L"/usr/etc/app-control-appid.ini";
60 const wchar_t TIZEN_ALIAS_APPID_PREFIX[] = L"tizen.";
62 const String ACTL_REGISTRY_PATH = L"Path";
63 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
65 const int PKG_APPID_LEN = 256;
66 const int REG_VALUE_BUFFER_LEN = 256;
71 namespace Tizen { namespace App
74 _AppControlRegistry* _AppControlRegistry::__pSelf = null;
76 _AppControlRegistry::_AppControlRegistry(void)
78 __tizenList.Construct();
80 __aliasList.Construct();
82 __aliasAppId.Construct();
84 __runtimeAlias.Construct();
87 _AppControlRegistry::~_AppControlRegistry(void)
89 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
91 _DeleteCollection<_AppControlAliasEntry>(__runtimeAlias);
95 _AppControlRegistry::GetInstance(void)
99 SysLog(NID_APP, "Create new instance");
100 __pSelf = new (std::nothrow) _AppControlRegistry();
101 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
102 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
104 __pSelf->LoadTizenAppControlRegistry();
106 __pSelf->LoadLegacyList();
108 __pSelf->LoadAliasList();
110 #ifdef _SINGLETON_CLEANUP
111 _LongevityManager::GetInstance().RegisterOwnership(*__pSelf);
120 _AppControlRegistry::LoadTizenAppControlRegistry(void)
124 const String regPath = ACTL_DEFAULT_FILE;
126 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
127 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
129 const int secCount = reg.GetAllSectionCount();
130 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
132 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
139 for (int i = 0; i < secCount; i++)
141 ///////////////////////////////////////////////////////////////////////
143 String sec_name(reg.GetSectionName(i));
146 if (sec_name.IsEmpty())
151 ///////////////////////////////////////////////////////////////////////
153 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
156 size = REG_VALUE_BUFFER_LEN;
157 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
160 __tizenList.Add(sec_name, path);
163 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
169 _AppControlRegistry::LoadLegacyList(void)
173 const String& regPath(ACTL_LEGACY_FILE);
175 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
176 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
178 const int sec_count = reg.GetAllSectionCount();
179 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
181 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
183 String aliasProvider;
187 // actual parameter manipulation
188 for (int i = 0; i < sec_count; i++)
190 ///////////////////////////////////////////////////////////////////////
192 const String& secName(reg.GetSectionName(i));
194 ///////////////////////////////////////////////////////////////////////
195 // aliased appcontrol name
196 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
199 int size = REG_VALUE_BUFFER_LEN;
200 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
203 ///////////////////////////////////////////////////////////////////////
205 IList* pList = reg.GetAllEntryNamesN(secName);
211 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
213 while (pEnum->MoveNext() == E_SUCCESS)
215 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
217 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
222 r = reg.GetValue(secName, *pStr, value);
225 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
229 // add entry to aliasList
230 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
231 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
235 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
236 __aliasList.Add(secName, pEntry);
240 pList->RemoveAll(true);
244 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
250 _AppControlRegistry::LoadAliasList(void)
254 const String regPath = ACTL_ALIAS_FILE;
256 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
257 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
259 HashMap* pMap = null;
260 r = reg.GetEntryListN(L"Alias", &pMap);
263 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
264 pMap->RemoveAll(true);
271 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
272 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
274 while(pEnum->MoveNext() == E_SUCCESS)
276 pKey = static_cast<String*>(pEnum->GetKey());
277 pVal = static_cast<String*>(pEnum->GetValue());
279 __aliasAppId.Add(*pKey, *pVal);
280 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
283 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
286 pMap->RemoveAll(true);
294 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
297 const String* pAppId = &aId;
298 const String* pOperation = &oId;
301 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
302 // number does not matter
303 if (count >= 5 || pEntry == null)
308 pAppId = &pEntry->provider2;
309 pOperation = &pEntry->operation2;
315 SysAssert(pAppId != null);
316 SysAssert(pOperation != null);
318 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
320 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
322 if (soName.IsEmpty())
324 AppControl* pAc = GetAppControlN(*pAppId, *pOperation);
327 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
328 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
330 const _AppControlAliasEntry* pLookupEntry = GetReverseRuntimeAliasEntry(*pAppId, *pOperation);
331 if (pLookupEntry == null)
333 // no entry found : add new one
334 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(aId, oId, *pAppId, *pOperation);
338 __runtimeAlias.Add(pEntry);
339 SysLog(NID_APP, "Setting alias app (%ls -> %ls).", aId.GetPointer(), pAppId->GetPointer());
345 SysLog(NID_APP, "No AppControl instance for (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
350 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, L"", _APPCONTROL_PROPERTY_PUBLIC | _APPCONTROL_PROPERTY_SLP);
355 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
358 result r = __tizenList.GetValue(appId, val);
361 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
365 SysLog(NID_APP, "No platform AppControl and use custom AppControl instead.");
371 // data structure for _AppControlRegistry::FindAppControlListN() only
373 struct AppSvcIterData
376 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
379 const String& operation;
383 // callback function for _AppControlRegistry::FindAppControlListN()
386 AppSvcIterFnCb(const char* pAppId, void* pData)
388 SysAssert(pData != null);
390 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
391 ArrayList* pList = pAppSvcIterData->pArray;
392 SysAssert(pList != null);
393 const String& operation = pAppSvcIterData->operation;
397 SysLog(NID_APP, "Empty appId received.");
401 String appId = pAppId;
402 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
405 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
414 Tizen::Base::Collection::ArrayList*
415 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
417 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
419 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
420 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
422 _AppMessageImpl::SetOperation(pBundle.get(), operation);
426 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
431 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
436 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
439 ArrayList* pList = new (std::nothrow) ArrayList;
440 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
443 AppSvcIterData data(pList, operation);
445 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
447 if (pList->GetCount() == 0)
449 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
458 const _AppControlRegistry::_AppControlAliasEntry*
459 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
461 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
462 if (pEnum.get() == null)
464 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
468 while (pEnum->MoveNext() == E_SUCCESS)
470 _AppControlAliasEntry* pEntry = null;
471 pEnum->GetCurrent(pEntry);
472 if (pEntry->provider == aId && pEntry->operation == oId)
474 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
484 const _AppControlRegistry::_AppControlAliasEntry*
485 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
487 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
489 while (pEnum->MoveNext() == E_SUCCESS)
491 _AppControlAliasEntry* pEntry = null;
492 pEnum->GetValue(pEntry);
493 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
495 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
505 const _AppControlRegistry::_AppControlAliasEntry*
506 _AppControlRegistry::GetReverseRuntimeAliasEntry(const String& aId, const String& oId) const
508 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__runtimeAlias.GetEnumeratorN());
510 while (pEnum->MoveNext() == E_SUCCESS)
512 _AppControlAliasEntry* pEntry = null;
513 pEnum->GetCurrent(pEntry);
514 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
516 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
527 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
529 bool changeAppId = false;
531 String actualAppId = appId;
532 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
534 const String& tmp = GetAliasAppId(appId);
539 SysLog(NID_APP, "Found alias app (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
545 bool b = _Aul::IsInstalled(actualAppId);
546 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
548 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
553 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
556 result r = __aliasAppId.GetValue(appId, tmp);
567 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
569 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
573 while (pEnum->MoveNext() == E_SUCCESS)
576 pEnum->GetValue(value);
583 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());