2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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_AppControlRegistry.cpp
20 * @brief This is the implementation for the _AppControlRegistry.cpp class.
24 #include <unique_ptr.h>
28 #include <FBaseErrors.h>
29 #include <FBaseSysLog.h>
30 #include <FBaseColAllElementsDeleter.h>
31 #include <FBaseColHashMap.h>
32 #include <FAppAppControl.h>
33 #include <FIoRegistry.h>
36 #include <FBase_StringConverter.h>
37 #include <FIo_RegistryImpl.h>
39 #include "FAppPkg_PackageManagerImpl.h"
40 #include "FApp_AppControlImpl.h"
42 #include "FApp_AppControlRegistry.h"
43 #include "FApp_AppMessageImpl.h"
44 #include "FApp_TemplateUtil.h"
45 #ifdef _SINGLETON_CLEANUP
46 #include "FApp_LongevityManager.h"
49 using namespace Tizen::App::Package;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Collection;
52 using namespace Tizen::Io;
57 const wchar_t ACTL_DEFAULT_FILE[] = L"/usr/etc/app-control-info.ini";
58 const wchar_t ACTL_LEGACY_FILE[] = L"/usr/etc/app-control-alias.ini";
59 const wchar_t ACTL_ALIAS_FILE[] = L"/usr/etc/app-control-appid.ini";
61 const wchar_t TIZEN_ALIAS_APPID_PREFIX[] = L"tizen.";
63 const String ACTL_REGISTRY_OP_NUM = L"OPID_Count";
64 const String ACTL_REGISTRY_PUBLIC = L"Public";
65 const String ACTL_REGISTRY_PATH = L"Path";
66 const String ACTL_REGISTRY_TITLE = L"Title";
67 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
69 const int PKG_CATEGORY_LEN = 256;
70 const int PKG_APPID_LEN = 256;
71 const int REG_VALUE_BUFFER_LEN = 256;
76 namespace Tizen { namespace App
79 _AppControlRegistry* _AppControlRegistry::__pSelf = null;
81 _AppControlRegistry::_AppControlRegistry(void)
83 __nativeList.Construct();
85 __tizenList.Construct();
87 __aliasList.Construct();
89 __aliasAppId.Construct();
92 _AppControlRegistry::~_AppControlRegistry(void)
94 _DeleteCollection<AppControl>(__nativeList);
96 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
100 _AppControlRegistry::GetInstance(void)
104 SysLog(NID_APP, "Create new instance");
105 __pSelf = new (std::nothrow) _AppControlRegistry();
106 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
107 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
109 __pSelf->LoadRegistry();
111 __pSelf->LoadTizenAppControlRegistry();
113 __pSelf->LoadLegacyList();
115 __pSelf->LoadAliasList();
117 #ifdef _SINGLETON_CLEANUP
118 _LongevityManager::GetInstance().RegisterOwnership(*__pSelf);
126 _AppControlRegistry::LoadRegistry(void)
130 const String regPath = ACTL_DEFAULT_FILE;
132 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
133 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
135 const int sec_count = reg.GetAllSectionCount();
136 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
138 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
147 // actual parameter manipulation
148 for (int i = 0; i < sec_count; i++)
150 ///////////////////////////////////////////////////////////////////////
152 String sec_name(reg.GetSectionName(i));
155 if (sec_name.IsEmpty())
162 ///////////////////////////////////////////////////////////////////////
164 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PUBLIC);
170 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
178 ///////////////////////////////////////////////////////////////////////
180 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
183 size = REG_VALUE_BUFFER_LEN;
184 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_path, &size);
187 ///////////////////////////////////////////////////////////////////////
189 // [FIXME] SLP localized name here
190 index = reg.GetEntryIndex(i, ACTL_REGISTRY_TITLE);
193 size = REG_VALUE_BUFFER_LEN;
194 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_name, &size);
197 //SysLog(NID_APP, "%dth iteration : %ls", i, sec_name.GetPointer());
199 ///////////////////////////////////////////////////////////////////////
200 // Number of operation Id
201 index = reg.GetEntryIndex(i, ACTL_REGISTRY_OP_NUM);
205 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
210 //SysLog(NID_APP, "number of operation %d for index %d", num, index);
212 for (int j = 0; j < num; j++)
214 size = REG_VALUE_BUFFER_LEN;
216 ///////////////////////////////////////////////////////////////
218 tagName.Format(10, L"OPID_%d", j);
220 index = reg.GetEntryIndex(i, tagName);
223 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_opId, &size);
226 //SysLog(NID_APP, "Operation(%d) %ls", index, actl_opId.GetPointer());
228 ///////////////////////////////////////////////////////////////
229 // AppControl allocation
232 if (public_open == 1)
234 prop |= _APPCONTROL_PROPERTY_PUBLIC;
237 prop |= _APPCONTROL_PROPERTY_SLP;
239 //SysLog(NID_APP, "(%ls, %ls)", sec_name.GetPointer(), actl_opId.GetPointer());
240 AppControl* pAc = _AppControlImpl::CreateN(actl_path, sec_name, actl_opId, actl_name, prop);
244 __nativeList.Add(pAc);
248 SysLog(NID_APP, "Failed to create AppControl instance (%ls, %ls, %d)",
249 sec_name.GetPointer(), actl_opId.GetPointer(), public_open);
256 SysLog(NID_APP, "Finished loading %d entries", __nativeList.GetCount());
262 _AppControlRegistry::LoadTizenAppControlRegistry(void)
266 const String regPath = ACTL_DEFAULT_FILE;
268 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
269 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
271 const int secCount = reg.GetAllSectionCount();
272 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
274 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
281 for (int i = 0; i < secCount; i++)
283 ///////////////////////////////////////////////////////////////////////
285 String sec_name(reg.GetSectionName(i));
288 if (sec_name.IsEmpty())
296 ///////////////////////////////////////////////////////////////////////
298 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PUBLIC);
304 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
313 ///////////////////////////////////////////////////////////////////////
315 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
318 size = REG_VALUE_BUFFER_LEN;
319 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
322 __tizenList.Add(sec_name, path);
325 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
331 _AppControlRegistry::LoadLegacyList(void)
335 const String& regPath(ACTL_LEGACY_FILE);
337 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
338 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
340 const int sec_count = reg.GetAllSectionCount();
341 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
343 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
345 String aliasProvider;
349 // actual parameter manipulation
350 for (int i = 0; i < sec_count; i++)
352 ///////////////////////////////////////////////////////////////////////
354 const String& secName(reg.GetSectionName(i));
356 ///////////////////////////////////////////////////////////////////////
357 // aliased appcontrol name
358 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
361 int size = REG_VALUE_BUFFER_LEN;
362 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
365 ///////////////////////////////////////////////////////////////////////
367 IList* pList = reg.GetAllEntryNamesN(secName);
373 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
375 while (pEnum->MoveNext() == E_SUCCESS)
377 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
379 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
384 r = reg.GetValue(secName, *pStr, value);
387 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
391 // add entry to aliasList
392 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
393 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
397 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
398 __aliasList.Add(secName, pEntry);
402 pList->RemoveAll(true);
406 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
412 _AppControlRegistry::LoadAliasList(void)
416 const String regPath = ACTL_ALIAS_FILE;
418 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
419 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
421 HashMap* pMap = null;
422 r = reg.GetEntryListN(L"Alias", &pMap);
425 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
432 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
433 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
435 while(pEnum->MoveNext() == E_SUCCESS)
437 pKey = static_cast<String*>(pEnum->GetKey());
438 pVal = static_cast<String*>(pEnum->GetValue());
440 __aliasAppId.Add(*pKey, *pVal);
441 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
444 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
454 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
456 const _AppControlAliasEntry* pEntry = null;
461 const _AppControlAliasEntry* pTempEntry = GetAppControlAliasEntry(aId, oId);
462 // number does not matter
463 if (count >= 5 || pTempEntry == null)
474 const String* pAppId = null;
475 const String* pOperation = null;
479 // if count is 0, then no alias is found
483 else if (pEntry == null)
485 // no "TIZEN" AppControl
490 pAppId = &pEntry->provider2;
491 pOperation = &pEntry->operation2;
494 SysAssert(pAppId != null);
495 SysAssert(pOperation != null);
497 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
499 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
501 if (soName.IsEmpty())
503 SysLog(NID_APP, "No AppControl stub found for (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
507 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, L"", _APPCONTROL_PROPERTY_PUBLIC | _APPCONTROL_PROPERTY_SLP);
512 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
515 result r = __tizenList.GetValue(appId, val);
518 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
522 SysLog(NID_APP, "No platform AppControl and use custom AppControl instead.");
528 _AppControlRegistry::GetNativeAppControlN(const String& aId, const String& oId) const
530 std::unique_ptr< IEnumeratorT<AppControl*> > pEnum(__nativeList.GetEnumeratorN());
532 while (pEnum->MoveNext() == E_SUCCESS)
534 AppControl* pAc = null;
535 pEnum->GetCurrent(pAc);
537 if (pAc->GetAppControlProviderId() == aId && pAc->GetOperationId() == oId)
539 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", aId.GetPointer(), oId.GetPointer());
540 return _AppControlImpl::CreateN(*pAc);
544 SysLog(NID_APP, "No matching AppControl (%ls, %ls)", aId.GetPointer(), oId.GetPointer());
550 // data structure for _AppControlRegistry::GetAppControlListN() only
552 struct AppSvcIterData
555 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
558 const String& operation;
562 // callback function for _AppControlRegistry::GetAppControlListN()
565 AppSvcIterFnCb(const char* pAppId, void* pData)
567 SysAssert(pData != null);
569 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
570 ArrayList* pList = pAppSvcIterData->pArray;
571 SysAssert(pList != null);
572 const String& operation = pAppSvcIterData->operation;
576 SysLog(NID_APP, "Empty appId received.");
580 String appId = pAppId;
581 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
584 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
593 Tizen::Base::Collection::ArrayList*
594 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
596 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
598 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
599 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
601 _AppMessageImpl::SetOperation(pBundle.get(), operation);
605 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
610 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
615 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
618 ArrayList* pList = new (std::nothrow) ArrayList;
619 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
622 AppSvcIterData data(pList, operation);
624 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
626 if (pList->GetCount() == 0)
628 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
637 const _AppControlRegistry::_AppControlAliasEntry*
638 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
640 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
641 if (pEnum.get() == null)
643 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
647 while (pEnum->MoveNext() == E_SUCCESS)
649 _AppControlAliasEntry* pEntry = null;
650 pEnum->GetCurrent(pEntry);
651 if (pEntry->provider == aId && pEntry->operation == oId)
653 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
662 const _AppControlRegistry::_AppControlAliasEntry*
663 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
665 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
667 while (pEnum->MoveNext() == E_SUCCESS)
669 _AppControlAliasEntry* pEntry = null;
670 pEnum->GetValue(pEntry);
671 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
673 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
683 _AppControlRegistry::GetAliasAppControlN(const String& aId, const String& oId) const
685 const _AppControlAliasEntry* pEntry = GetAppControlAliasEntry(aId, oId);
689 AppControl* pAc = GetNativeAppControlN(pEntry->provider2, pEntry->operation2);
692 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
693 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
697 pAc = GetAppControlN(pEntry->provider2, pEntry->operation2);
700 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
701 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
705 SysLog(NID_APP, "No matching AppControl found.");
712 SysLog(NID_APP, "No matching AppControl found.");
717 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
719 bool changeAppId = false;
721 String actualAppId = appId;
722 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
724 const String& tmp = GetAliasAppId(appId);
729 SysLog(NID_APP, "Found alias appId (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
735 bool b = _Aul::IsInstalled(actualAppId);
736 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
738 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
743 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
746 result r = __aliasAppId.GetValue(appId, tmp);
757 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
759 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
763 while (pEnum->MoveNext() == E_SUCCESS)
766 pEnum->GetValue(value);
773 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());