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));
286 pMap->RemoveAll(true);
293 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
294 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
296 while(pEnum->MoveNext() == E_SUCCESS)
298 pKey = static_cast<String*>(pEnum->GetKey());
299 pVal = static_cast<String*>(pEnum->GetValue());
301 __aliasAppId.Add(*pKey, *pVal);
302 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
305 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
308 pMap->RemoveAll(true);
316 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId)
319 const String* pAppId = &aId;
320 const String* pOperation = &oId;
323 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
324 // number does not matter
325 if (count >= 5 || pEntry == null)
330 pAppId = &pEntry->provider2;
331 pOperation = &pEntry->operation2;
337 SysAssert(pAppId != null);
338 SysAssert(pOperation != null);
340 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
342 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
344 if (soName.IsEmpty())
346 AppControl* pAc = GetAppControlN(*pAppId, *pOperation);
349 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
350 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
352 const _AppControlAliasEntry* pLookupEntry = GetReverseRuntimeAliasEntry(*pAppId, *pOperation);
353 if (pLookupEntry == null)
355 // no entry found : add new one
356 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(aId, oId, *pAppId, *pOperation);
360 __runtimeAlias.Add(pEntry);
361 SysLog(NID_APP, "Setting alias appId (%ls -> %ls).", aId.GetPointer(), pAppId->GetPointer());
367 SysLog(NID_APP, "No AppControl instance for (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
372 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, L"", _APPCONTROL_PROPERTY_PUBLIC | _APPCONTROL_PROPERTY_SLP);
377 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
380 result r = __tizenList.GetValue(appId, val);
383 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
387 SysLog(NID_APP, "No platform AppControl and use custom AppControl instead.");
393 // data structure for _AppControlRegistry::FindAppControlListN() only
395 struct AppSvcIterData
398 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
401 const String& operation;
405 // callback function for _AppControlRegistry::FindAppControlListN()
408 AppSvcIterFnCb(const char* pAppId, void* pData)
410 SysAssert(pData != null);
412 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
413 ArrayList* pList = pAppSvcIterData->pArray;
414 SysAssert(pList != null);
415 const String& operation = pAppSvcIterData->operation;
419 SysLog(NID_APP, "Empty appId received.");
423 String appId = pAppId;
424 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
427 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
436 Tizen::Base::Collection::ArrayList*
437 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
439 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
441 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
442 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
444 _AppMessageImpl::SetOperation(pBundle.get(), operation);
448 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
453 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
458 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
461 ArrayList* pList = new (std::nothrow) ArrayList;
462 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
465 AppSvcIterData data(pList, operation);
467 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
469 if (pList->GetCount() == 0)
471 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
480 const _AppControlRegistry::_AppControlAliasEntry*
481 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
483 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
484 if (pEnum.get() == null)
486 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
490 while (pEnum->MoveNext() == E_SUCCESS)
492 _AppControlAliasEntry* pEntry = null;
493 pEnum->GetCurrent(pEntry);
494 if (pEntry->provider == aId && pEntry->operation == oId)
496 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
506 const _AppControlRegistry::_AppControlAliasEntry*
507 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
509 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
511 while (pEnum->MoveNext() == E_SUCCESS)
513 _AppControlAliasEntry* pEntry = null;
514 pEnum->GetValue(pEntry);
515 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
517 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
527 const _AppControlRegistry::_AppControlAliasEntry*
528 _AppControlRegistry::GetReverseRuntimeAliasEntry(const String& aId, const String& oId) const
530 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__runtimeAlias.GetEnumeratorN());
532 while (pEnum->MoveNext() == E_SUCCESS)
534 _AppControlAliasEntry* pEntry = null;
535 pEnum->GetCurrent(pEntry);
536 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
538 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
549 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
551 bool changeAppId = false;
553 String actualAppId = appId;
554 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
556 const String& tmp = GetAliasAppId(appId);
561 SysLog(NID_APP, "Found alias appId (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
567 bool b = _Aul::IsInstalled(actualAppId);
568 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
570 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
575 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
578 result r = __aliasAppId.GetValue(appId, tmp);
589 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
591 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
595 while (pEnum->MoveNext() == E_SUCCESS)
598 pEnum->GetValue(value);
605 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());