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_PUBLIC = L"Public";
64 const String ACTL_REGISTRY_PATH = L"Path";
65 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
67 const int PKG_APPID_LEN = 256;
68 const int REG_VALUE_BUFFER_LEN = 256;
73 namespace Tizen { namespace App
76 _AppControlRegistry* _AppControlRegistry::__pSelf = null;
78 _AppControlRegistry::_AppControlRegistry(void)
80 __tizenList.Construct();
82 __aliasList.Construct();
84 __aliasAppId.Construct();
86 __runtimeAlias.Construct();
89 _AppControlRegistry::~_AppControlRegistry(void)
91 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
93 _DeleteCollection<_AppControlAliasEntry>(__runtimeAlias);
97 _AppControlRegistry::GetInstance(void)
101 SysLog(NID_APP, "Create new instance");
102 __pSelf = new (std::nothrow) _AppControlRegistry();
103 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
104 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
106 __pSelf->LoadTizenAppControlRegistry();
108 __pSelf->LoadLegacyList();
110 __pSelf->LoadAliasList();
112 #ifdef _SINGLETON_CLEANUP
113 _LongevityManager::GetInstance().RegisterOwnership(*__pSelf);
122 _AppControlRegistry::LoadTizenAppControlRegistry(void)
126 const String regPath = ACTL_DEFAULT_FILE;
128 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
129 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
131 const int secCount = reg.GetAllSectionCount();
132 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
134 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
141 for (int i = 0; i < secCount; i++)
143 ///////////////////////////////////////////////////////////////////////
145 String sec_name(reg.GetSectionName(i));
148 if (sec_name.IsEmpty())
156 ///////////////////////////////////////////////////////////////////////
158 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PUBLIC);
164 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
173 ///////////////////////////////////////////////////////////////////////
175 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
178 size = REG_VALUE_BUFFER_LEN;
179 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
182 __tizenList.Add(sec_name, path);
185 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
191 _AppControlRegistry::LoadLegacyList(void)
195 const String& regPath(ACTL_LEGACY_FILE);
197 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
198 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
200 const int sec_count = reg.GetAllSectionCount();
201 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
203 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
205 String aliasProvider;
209 // actual parameter manipulation
210 for (int i = 0; i < sec_count; i++)
212 ///////////////////////////////////////////////////////////////////////
214 const String& secName(reg.GetSectionName(i));
216 ///////////////////////////////////////////////////////////////////////
217 // aliased appcontrol name
218 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
221 int size = REG_VALUE_BUFFER_LEN;
222 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
225 ///////////////////////////////////////////////////////////////////////
227 IList* pList = reg.GetAllEntryNamesN(secName);
233 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
235 while (pEnum->MoveNext() == E_SUCCESS)
237 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
239 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
244 r = reg.GetValue(secName, *pStr, value);
247 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
251 // add entry to aliasList
252 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
253 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
257 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
258 __aliasList.Add(secName, pEntry);
262 pList->RemoveAll(true);
266 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
272 _AppControlRegistry::LoadAliasList(void)
276 const String regPath = ACTL_ALIAS_FILE;
278 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
279 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
281 HashMap* pMap = null;
282 r = reg.GetEntryListN(L"Alias", &pMap);
285 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
292 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
293 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
295 while(pEnum->MoveNext() == E_SUCCESS)
297 pKey = static_cast<String*>(pEnum->GetKey());
298 pVal = static_cast<String*>(pEnum->GetValue());
300 __aliasAppId.Add(*pKey, *pVal);
301 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
304 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
314 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId)
317 const String* pAppId = &aId;
318 const String* pOperation = &oId;
321 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
322 // number does not matter
323 if (count >= 5 || pEntry == null)
328 pAppId = &pEntry->provider2;
329 pOperation = &pEntry->operation2;
335 SysAssert(pAppId != null);
336 SysAssert(pOperation != null);
338 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
340 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
342 if (soName.IsEmpty())
344 AppControl* pAc = GetAppControlN(*pAppId, *pOperation);
347 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
348 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
350 const _AppControlAliasEntry* pLookupEntry = GetReverseRuntimeAliasEntry(*pAppId, *pOperation);
351 if (pLookupEntry == null)
353 // no entry found : add new one
354 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(aId, oId, *pAppId, *pOperation);
358 __runtimeAlias.Add(pEntry);
359 SysLog(NID_APP, "Setting alias appId (%ls -> %ls).", aId.GetPointer(), pAppId->GetPointer());
365 SysLog(NID_APP, "No AppControl instance for (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
370 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, L"", _APPCONTROL_PROPERTY_PUBLIC | _APPCONTROL_PROPERTY_SLP);
375 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
378 result r = __tizenList.GetValue(appId, val);
381 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
385 SysLog(NID_APP, "No platform AppControl and use custom AppControl instead.");
391 // data structure for _AppControlRegistry::FindAppControlListN() only
393 struct AppSvcIterData
396 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
399 const String& operation;
403 // callback function for _AppControlRegistry::FindAppControlListN()
406 AppSvcIterFnCb(const char* pAppId, void* pData)
408 SysAssert(pData != null);
410 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
411 ArrayList* pList = pAppSvcIterData->pArray;
412 SysAssert(pList != null);
413 const String& operation = pAppSvcIterData->operation;
417 SysLog(NID_APP, "Empty appId received.");
421 String appId = pAppId;
422 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
425 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
434 Tizen::Base::Collection::ArrayList*
435 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
437 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
439 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
440 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
442 _AppMessageImpl::SetOperation(pBundle.get(), operation);
446 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
451 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
456 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
459 ArrayList* pList = new (std::nothrow) ArrayList;
460 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
463 AppSvcIterData data(pList, operation);
465 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
467 if (pList->GetCount() == 0)
469 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
478 const _AppControlRegistry::_AppControlAliasEntry*
479 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
481 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
482 if (pEnum.get() == null)
484 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
488 while (pEnum->MoveNext() == E_SUCCESS)
490 _AppControlAliasEntry* pEntry = null;
491 pEnum->GetCurrent(pEntry);
492 if (pEntry->provider == aId && pEntry->operation == oId)
494 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
504 const _AppControlRegistry::_AppControlAliasEntry*
505 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
507 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
509 while (pEnum->MoveNext() == E_SUCCESS)
511 _AppControlAliasEntry* pEntry = null;
512 pEnum->GetValue(pEntry);
513 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
515 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
525 const _AppControlRegistry::_AppControlAliasEntry*
526 _AppControlRegistry::GetReverseRuntimeAliasEntry(const String& aId, const String& oId) const
528 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__runtimeAlias.GetEnumeratorN());
530 while (pEnum->MoveNext() == E_SUCCESS)
532 _AppControlAliasEntry* pEntry = null;
533 pEnum->GetCurrent(pEntry);
534 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
536 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
547 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
549 bool changeAppId = false;
551 String actualAppId = appId;
552 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
554 const String& tmp = GetAliasAppId(appId);
559 SysLog(NID_APP, "Found alias appId (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
565 bool b = _Aul::IsInstalled(actualAppId);
566 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
568 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
573 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
576 result r = __aliasAppId.GetValue(appId, tmp);
587 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
589 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
593 while (pEnum->MoveNext() == E_SUCCESS)
596 pEnum->GetValue(value);
603 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());