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"
45 using namespace Tizen::App::Package;
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Collection;
48 using namespace Tizen::Io;
53 const wchar_t ACTL_DEFAULT_FILE[] = L"/usr/etc/app-control-info.ini";
54 const wchar_t ACTL_LEGACY_FILE[] = L"/usr/etc/app-control-alias.ini";
55 const wchar_t ACTL_ALIAS_FILE[] = L"/usr/etc/app-control-appid.ini";
57 const wchar_t TIZEN_ALIAS_APPID_PREFIX[] = L"tizen.";
59 const String ACTL_REGISTRY_PATH = L"Path";
60 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
62 const int PKG_APPID_LEN = 256;
63 const int REG_VALUE_BUFFER_LEN = 256;
68 namespace Tizen { namespace App
71 _AppControlRegistry* _AppControlRegistry::__pSelf = null;
73 _AppControlRegistry::_AppControlRegistry(void)
75 __tizenList.Construct();
77 __aliasList.Construct();
79 __aliasAppId.Construct();
81 __runtimeAlias.Construct();
84 _AppControlRegistry::~_AppControlRegistry(void)
86 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
88 _DeleteCollection<_AppControlAliasEntry>(__runtimeAlias);
92 _AppControlRegistry::GetInstance(void)
96 SysLog(NID_APP, "Create new instance");
97 __pSelf = new (std::nothrow) _AppControlRegistry();
98 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
99 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
101 __pSelf->LoadTizenAppControlRegistry();
103 __pSelf->LoadLegacyList();
105 __pSelf->LoadAliasList();
113 _AppControlRegistry::LoadTizenAppControlRegistry(void)
117 const String regPath = ACTL_DEFAULT_FILE;
119 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
120 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
122 const int secCount = reg.GetAllSectionCount();
123 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
125 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
132 for (int i = 0; i < secCount; i++)
134 ///////////////////////////////////////////////////////////////////////
136 String sec_name(reg.GetSectionName(i));
139 if (sec_name.IsEmpty())
144 ///////////////////////////////////////////////////////////////////////
146 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
149 size = REG_VALUE_BUFFER_LEN;
150 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
153 __tizenList.Add(sec_name, path);
156 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
162 _AppControlRegistry::LoadLegacyList(void)
166 const String& regPath(ACTL_LEGACY_FILE);
168 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
169 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
171 const int sec_count = reg.GetAllSectionCount();
172 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
174 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
176 String aliasProvider;
180 // actual parameter manipulation
181 for (int i = 0; i < sec_count; i++)
183 ///////////////////////////////////////////////////////////////////////
185 const String& secName(reg.GetSectionName(i));
187 ///////////////////////////////////////////////////////////////////////
188 // aliased appcontrol name
189 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
192 int size = REG_VALUE_BUFFER_LEN;
193 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
196 ///////////////////////////////////////////////////////////////////////
198 IList* pList = reg.GetAllEntryNamesN(secName);
204 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
206 while (pEnum->MoveNext() == E_SUCCESS)
208 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
210 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
215 r = reg.GetValue(secName, *pStr, value);
218 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
222 // add entry to aliasList
223 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
224 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
228 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
229 __aliasList.Add(secName, pEntry);
233 pList->RemoveAll(true);
237 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
243 _AppControlRegistry::LoadAliasList(void)
247 const String regPath = ACTL_ALIAS_FILE;
249 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
250 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
252 HashMap* pMap = null;
253 r = reg.GetEntryListN(L"Alias", &pMap);
256 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
257 pMap->RemoveAll(true);
264 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
265 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
267 while(pEnum->MoveNext() == E_SUCCESS)
269 pKey = static_cast<String*>(pEnum->GetKey());
270 pVal = static_cast<String*>(pEnum->GetValue());
272 __aliasAppId.Add(*pKey, *pVal);
273 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
276 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
279 pMap->RemoveAll(true);
287 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
290 const String* pAppId = &aId;
291 const String* pOperation = &oId;
294 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
295 // number does not matter
296 if (count >= 5 || pEntry == null)
301 pAppId = &pEntry->provider2;
302 pOperation = &pEntry->operation2;
308 SysAssert(pAppId != null);
309 SysAssert(pOperation != null);
311 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
313 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
315 if (soName.IsEmpty())
317 AppControl* pAc = GetAppControlN(*pAppId, *pOperation);
320 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
321 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
323 const _AppControlAliasEntry* pLookupEntry = GetReverseRuntimeAliasEntry(*pAppId, *pOperation);
324 if (pLookupEntry == null)
326 // no entry found : add new one
327 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(aId, oId, *pAppId, *pOperation);
331 __runtimeAlias.Add(pEntry);
332 SysLog(NID_APP, "Setting alias app (%ls -> %ls).", aId.GetPointer(), pAppId->GetPointer());
338 SysLog(NID_APP, "No AppControl instance for (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
343 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, L"", _APPCONTROL_PROPERTY_PUBLIC | _APPCONTROL_PROPERTY_SLP);
348 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
351 result r = __tizenList.GetValue(appId, val);
354 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
358 SysLog(NID_APP, "No platform AppControl and use custom AppControl instead.");
364 // data structure for _AppControlRegistry::FindAppControlListN() only
366 struct AppSvcIterData
369 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
372 const String& operation;
376 // callback function for _AppControlRegistry::FindAppControlListN()
379 AppSvcIterFnCb(const char* pAppId, void* pData)
381 SysAssert(pData != null);
383 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
384 ArrayList* pList = pAppSvcIterData->pArray;
385 SysAssert(pList != null);
386 const String& operation = pAppSvcIterData->operation;
390 SysLog(NID_APP, "Empty appId received.");
394 String appId = pAppId;
395 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
398 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
407 Tizen::Base::Collection::ArrayList*
408 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
410 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
412 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
413 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
415 _AppMessageImpl::SetOperation(pBundle.get(), operation);
419 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
424 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
429 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
432 ArrayList* pList = new (std::nothrow) ArrayList;
433 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
436 AppSvcIterData data(pList, operation);
438 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
440 if (pList->GetCount() == 0)
442 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
451 const _AppControlRegistry::_AppControlAliasEntry*
452 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
454 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
455 if (pEnum.get() == null)
457 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
461 while (pEnum->MoveNext() == E_SUCCESS)
463 _AppControlAliasEntry* pEntry = null;
464 pEnum->GetCurrent(pEntry);
465 if (pEntry->provider == aId && pEntry->operation == oId)
467 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
477 const _AppControlRegistry::_AppControlAliasEntry*
478 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
480 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
482 while (pEnum->MoveNext() == E_SUCCESS)
484 _AppControlAliasEntry* pEntry = null;
485 pEnum->GetValue(pEntry);
486 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
488 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
498 const _AppControlRegistry::_AppControlAliasEntry*
499 _AppControlRegistry::GetReverseRuntimeAliasEntry(const String& aId, const String& oId) const
501 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__runtimeAlias.GetEnumeratorN());
503 while (pEnum->MoveNext() == E_SUCCESS)
505 _AppControlAliasEntry* pEntry = null;
506 pEnum->GetCurrent(pEntry);
507 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
509 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
520 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
522 bool changeAppId = false;
524 String actualAppId = appId;
525 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
527 const String& tmp = GetAliasAppId(appId);
532 SysLog(NID_APP, "Found alias app (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
538 bool b = _Aul::IsInstalled(actualAppId);
539 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
541 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
546 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
549 result r = __aliasAppId.GetValue(appId, tmp);
560 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
562 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
566 while (pEnum->MoveNext() == E_SUCCESS)
569 pEnum->GetValue(value);
576 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());