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 __aliasList.Construct();
87 __aliasOperation.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->LoadLegacyList();
113 __pSelf->LoadAliasList();
115 #ifdef _SINGLETON_CLEANUP
116 _LongevityManager::GetInstance().RegisterOwnership(*__pSelf);
124 _AppControlRegistry::LoadRegistry(void)
128 const String regPath = ACTL_DEFAULT_FILE;
130 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
131 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
133 const int sec_count = reg.GetAllSectionCount();
134 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
136 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
145 // actual parameter manipulation
146 for (int i = 0; i < sec_count; i++)
148 ///////////////////////////////////////////////////////////////////////
150 String sec_name(reg.GetSectionName(i));
153 if (sec_name.IsEmpty())
160 ///////////////////////////////////////////////////////////////////////
162 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PUBLIC);
168 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
176 ///////////////////////////////////////////////////////////////////////
178 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
181 size = REG_VALUE_BUFFER_LEN;
182 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_path, &size);
185 ///////////////////////////////////////////////////////////////////////
187 // [FIXME] SLP localized name here
188 index = reg.GetEntryIndex(i, ACTL_REGISTRY_TITLE);
191 size = REG_VALUE_BUFFER_LEN;
192 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_name, &size);
195 //SysLog(NID_APP, "%dth iteration : %ls", i, sec_name.GetPointer());
197 ///////////////////////////////////////////////////////////////////////
198 // Number of operation Id
199 index = reg.GetEntryIndex(i, ACTL_REGISTRY_OP_NUM);
203 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
208 //SysLog(NID_APP, "number of operation %d for index %d", num, index);
210 for (int j = 0; j < num; j++)
212 size = REG_VALUE_BUFFER_LEN;
214 ///////////////////////////////////////////////////////////////
216 tagName.Format(10, L"OPID_%d", j);
218 index = reg.GetEntryIndex(i, tagName);
221 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_opId, &size);
224 //SysLog(NID_APP, "Operation(%d) %ls", index, actl_opId.GetPointer());
226 ///////////////////////////////////////////////////////////////
227 // AppControl allocation
230 if (public_open == 1)
232 prop |= _APPCONTROL_PROPERTY_PUBLIC;
235 prop |= _APPCONTROL_PROPERTY_SLP;
237 //SysLog(NID_APP, "(%ls, %ls)", sec_name.GetPointer(), actl_opId.GetPointer());
238 AppControl* pAc = _AppControlImpl::CreateN(actl_path, sec_name, actl_opId, actl_name, prop);
242 __nativeList.Add(pAc);
246 SysLog(NID_APP, "Failed to create AppControl instance (%ls, %ls, %d)",
247 sec_name.GetPointer(), actl_opId.GetPointer(), public_open);
254 SysLog(NID_APP, "Finished loading %d entries", __nativeList.GetCount());
260 _AppControlRegistry::LoadLegacyList(void)
264 const String& regPath(ACTL_LEGACY_FILE);
266 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
267 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
269 const int sec_count = reg.GetAllSectionCount();
270 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
272 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
274 String aliasProvider;
278 // actual parameter manipulation
279 for (int i = 0; i < sec_count; i++)
281 ///////////////////////////////////////////////////////////////////////
283 const String& secName(reg.GetSectionName(i));
285 ///////////////////////////////////////////////////////////////////////
286 // aliased appcontrol name
287 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
290 int size = REG_VALUE_BUFFER_LEN;
291 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
294 ///////////////////////////////////////////////////////////////////////
296 IList* pList = reg.GetAllEntryNamesN(secName);
302 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
304 while (pEnum->MoveNext() == E_SUCCESS)
306 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
308 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
313 r = reg.GetValue(secName, *pStr, value);
316 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
321 __aliasOperation.ContainsKey(*pStr, b);
324 __aliasOperation.SetValue(*pStr, value);
328 __aliasOperation.Add(*pStr, value);
331 // add entry to aliasList
332 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
333 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
337 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
338 __aliasList.Add(secName, pEntry);
342 pList->RemoveAll(true);
346 SysLog(NID_APP, "Finished loading %d entries with %d operation aliases.", __aliasList.GetCount(), __aliasOperation.GetCount());
352 _AppControlRegistry::LoadAliasList(void)
356 const String regPath = ACTL_ALIAS_FILE;
358 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
359 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
361 HashMap* pMap = null;
362 r = reg.GetEntryListN(L"Alias", &pMap);
365 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
372 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
373 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
375 while(pEnum->MoveNext() == E_SUCCESS)
377 pKey = static_cast<String*>(pEnum->GetKey());
378 pVal = static_cast<String*>(pEnum->GetValue());
380 __aliasAppId.Add(*pKey, *pVal);
381 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
384 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
394 _AppControlRegistry::GetNativeAppControlN(const String& aId, const String& oId) const
396 std::unique_ptr< IEnumeratorT<AppControl*> > pEnum(__nativeList.GetEnumeratorN());
398 while (pEnum->MoveNext() == E_SUCCESS)
400 AppControl* pAc = null;
401 pEnum->GetCurrent(pAc);
403 if (pAc->GetAppControlProviderId() == aId && pAc->GetOperationId() == oId)
405 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", aId.GetPointer(), oId.GetPointer());
406 return _AppControlImpl::CreateN(*pAc);
410 SysLog(NID_APP, "No matching AppControl (%ls, %ls)", aId.GetPointer(), oId.GetPointer());
416 // data structure for _AppControlRegistry::GetAppControlListN() only
418 struct AppSvcIterData
421 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
424 const String& operation;
428 // callback function for _AppControlRegistry::GetAppControlListN()
431 AppSvcIterFnCb(const char* pAppId, void* pData)
433 SysAssert(pData != null);
435 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
436 ArrayList* pList = pAppSvcIterData->pArray;
437 SysAssert(pList != null);
438 const String& operation = pAppSvcIterData->operation;
442 SysLog(NID_APP, "Empty appId received.");
446 String appId = pAppId;
447 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
450 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
459 Tizen::Base::Collection::ArrayList*
460 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
462 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
464 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
465 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
467 _AppMessageImpl::SetOperation(pBundle.get(), operation);
471 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
476 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
481 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
484 ArrayList* pList = new (std::nothrow) ArrayList;
485 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
488 AppSvcIterData data(pList, operation);
490 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
492 if (pList->GetCount() == 0)
494 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
503 _AppControlRegistry::_AppControlAliasEntry*
504 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
506 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
507 if (pEnum.get() == null)
509 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
513 while (pEnum->MoveNext() == E_SUCCESS)
515 _AppControlAliasEntry* pEntry = null;
516 pEnum->GetCurrent(pEntry);
517 if (pEntry->provider == aId && pEntry->operation == oId)
519 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
528 _AppControlRegistry::_AppControlAliasEntry*
529 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
531 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
533 while (pEnum->MoveNext() == E_SUCCESS)
535 _AppControlAliasEntry* pEntry = null;
536 pEnum->GetValue(pEntry);
537 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
539 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
549 _AppControlRegistry::GetAliasAppControlN(const String& aId, const String& oId) const
551 _AppControlAliasEntry* pEntry = GetAppControlAliasEntry(aId, oId);
555 AppControl* pAc = GetNativeAppControlN(pEntry->provider2, pEntry->operation2);
558 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
559 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
563 pAc = GetAppControlN(pEntry->provider2, pEntry->operation2);
566 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
567 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
571 SysLog(NID_APP, "No matching AppControl found.");
578 SysLog(NID_APP, "No matching AppControl found.");
583 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
585 bool changeAppId = false;
587 String actualAppId = appId;
588 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
591 result r = __aliasAppId.GetValue(appId, tmp);
595 SysLog(NID_APP, "Found alias appId (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
601 bool b = _Aul::IsInstalled(actualAppId);
602 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
604 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
608 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
610 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
614 while (pEnum->MoveNext() == E_SUCCESS)
617 pEnum->GetValue(value);
624 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());
629 _AppControlRegistry::GetAliasedOperation(const String& operation) const
632 __aliasOperation.ContainsKey(operation, b);
637 __aliasOperation.GetValue(operation, tmp);