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 __aliasAppId.Construct();
90 _AppControlRegistry::~_AppControlRegistry(void)
92 _DeleteCollection<AppControl>(__nativeList);
94 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
98 _AppControlRegistry::GetInstance(void)
102 SysLog(NID_APP, "Create new instance");
103 __pSelf = new (std::nothrow) _AppControlRegistry();
104 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
105 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
107 __pSelf->LoadRegistry();
109 __pSelf->LoadLegacyList();
111 __pSelf->LoadAliasList();
113 #ifdef _SINGLETON_CLEANUP
114 _LongevityManager::GetInstance().RegisterOwnership(*__pSelf);
122 _AppControlRegistry::LoadRegistry(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 sec_count = reg.GetAllSectionCount();
132 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
134 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
143 // actual parameter manipulation
144 for (int i = 0; i < sec_count; i++)
146 ///////////////////////////////////////////////////////////////////////
148 String sec_name(reg.GetSectionName(i));
151 if (sec_name.IsEmpty())
158 ///////////////////////////////////////////////////////////////////////
160 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PUBLIC);
166 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
174 ///////////////////////////////////////////////////////////////////////
176 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
179 size = REG_VALUE_BUFFER_LEN;
180 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_path, &size);
183 ///////////////////////////////////////////////////////////////////////
185 // [FIXME] SLP localized name here
186 index = reg.GetEntryIndex(i, ACTL_REGISTRY_TITLE);
189 size = REG_VALUE_BUFFER_LEN;
190 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_name, &size);
193 //SysLog(NID_APP, "%dth iteration : %ls", i, sec_name.GetPointer());
195 ///////////////////////////////////////////////////////////////////////
196 // Number of operation Id
197 index = reg.GetEntryIndex(i, ACTL_REGISTRY_OP_NUM);
201 reg.GetEntryValue(i, index, REG_VALUE_TYPE_INT, &num, &size);
206 //SysLog(NID_APP, "number of operation %d for index %d", num, index);
208 for (int j = 0; j < num; j++)
210 size = REG_VALUE_BUFFER_LEN;
212 ///////////////////////////////////////////////////////////////
214 tagName.Format(10, L"OPID_%d", j);
216 index = reg.GetEntryIndex(i, tagName);
219 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &actl_opId, &size);
222 //SysLog(NID_APP, "Operation(%d) %ls", index, actl_opId.GetPointer());
224 ///////////////////////////////////////////////////////////////
225 // AppControl allocation
228 if (public_open == 1)
230 prop |= _APPCONTROL_PROPERTY_PUBLIC;
233 prop |= _APPCONTROL_PROPERTY_SLP;
235 //SysLog(NID_APP, "(%ls, %ls)", sec_name.GetPointer(), actl_opId.GetPointer());
236 AppControl* pAc = _AppControlImpl::CreateN(actl_path, sec_name, actl_opId, actl_name, prop);
240 __nativeList.Add(pAc);
244 SysLog(NID_APP, "Failed to create AppControl instance (%ls, %ls, %d)",
245 sec_name.GetPointer(), actl_opId.GetPointer(), public_open);
252 SysLog(NID_APP, "Finished loading %d entries", __nativeList.GetCount());
258 _AppControlRegistry::LoadLegacyList(void)
262 const String& regPath(ACTL_LEGACY_FILE);
264 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
265 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
267 const int sec_count = reg.GetAllSectionCount();
268 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
270 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
272 String aliasProvider;
276 // actual parameter manipulation
277 for (int i = 0; i < sec_count; i++)
279 ///////////////////////////////////////////////////////////////////////
281 const String& secName(reg.GetSectionName(i));
283 ///////////////////////////////////////////////////////////////////////
284 // aliased appcontrol name
285 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
288 int size = REG_VALUE_BUFFER_LEN;
289 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
292 ///////////////////////////////////////////////////////////////////////
294 IList* pList = reg.GetAllEntryNamesN(secName);
300 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
302 while (pEnum->MoveNext() == E_SUCCESS)
304 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
306 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
311 r = reg.GetValue(secName, *pStr, value);
314 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
318 // add entry to aliasList
319 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
320 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
324 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
325 __aliasList.Add(secName, pEntry);
329 pList->RemoveAll(true);
333 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
339 _AppControlRegistry::LoadAliasList(void)
343 const String regPath = ACTL_ALIAS_FILE;
345 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
346 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
348 HashMap* pMap = null;
349 r = reg.GetEntryListN(L"Alias", &pMap);
352 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
359 std::unique_ptr<IMapEnumerator> pEnum(pMap->GetMapEnumeratorN());
360 SysTryCatch(NID_APP, pEnum.get(), r = E_OUT_OF_MEMORY , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory error.");
362 while(pEnum->MoveNext() == E_SUCCESS)
364 pKey = static_cast<String*>(pEnum->GetKey());
365 pVal = static_cast<String*>(pEnum->GetValue());
367 __aliasAppId.Add(*pKey, *pVal);
368 //SysLog(NID_APP, "(%ls, %ls)", pKey->GetPointer(), pVal->GetPointer());
371 SysLog(NID_APP, "Loading %d alias sections", __aliasAppId.GetCount());
381 _AppControlRegistry::GetNativeAppControlN(const String& aId, const String& oId) const
383 std::unique_ptr< IEnumeratorT<AppControl*> > pEnum(__nativeList.GetEnumeratorN());
385 while (pEnum->MoveNext() == E_SUCCESS)
387 AppControl* pAc = null;
388 pEnum->GetCurrent(pAc);
390 if (pAc->GetAppControlProviderId() == aId && pAc->GetOperationId() == oId)
392 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", aId.GetPointer(), oId.GetPointer());
393 return _AppControlImpl::CreateN(*pAc);
397 SysLog(NID_APP, "No matching AppControl (%ls, %ls)", aId.GetPointer(), oId.GetPointer());
403 // data structure for _AppControlRegistry::GetAppControlListN() only
405 struct AppSvcIterData
408 AppSvcIterData(ArrayList* pArr, const String& op) : pArray(pArr), operation(op) {}
411 const String& operation;
415 // callback function for _AppControlRegistry::GetAppControlListN()
418 AppSvcIterFnCb(const char* pAppId, void* pData)
420 SysAssert(pData != null);
422 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
423 ArrayList* pList = pAppSvcIterData->pArray;
424 SysAssert(pList != null);
425 const String& operation = pAppSvcIterData->operation;
429 SysLog(NID_APP, "Empty appId received.");
433 String appId = pAppId;
434 AppControl* pAc = _AppControlImpl::CreateN(appId, operation, false);
437 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
446 Tizen::Base::Collection::ArrayList*
447 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
449 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
451 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
452 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
454 _AppMessageImpl::SetOperation(pBundle.get(), operation);
458 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
463 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
468 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
471 ArrayList* pList = new (std::nothrow) ArrayList;
472 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
475 AppSvcIterData data(pList, operation);
477 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
479 if (pList->GetCount() == 0)
481 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
490 _AppControlRegistry::_AppControlAliasEntry*
491 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
493 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
494 if (pEnum.get() == null)
496 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
500 while (pEnum->MoveNext() == E_SUCCESS)
502 _AppControlAliasEntry* pEntry = null;
503 pEnum->GetCurrent(pEntry);
504 if (pEntry->provider == aId && pEntry->operation == oId)
506 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
515 _AppControlRegistry::_AppControlAliasEntry*
516 _AppControlRegistry::GetReverseAppControlAliasEntry(const String& aId, const String& oId) const
518 std::unique_ptr< IMapEnumeratorT<String, _AppControlAliasEntry*> > pEnum(__aliasList.GetMapEnumeratorN());
520 while (pEnum->MoveNext() == E_SUCCESS)
522 _AppControlAliasEntry* pEntry = null;
523 pEnum->GetValue(pEntry);
524 if (pEntry->provider2 == aId && pEntry->operation2 == oId)
526 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)<-(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider.GetPointer(), pEntry->operation.GetPointer());
536 _AppControlRegistry::GetAliasAppControlN(const String& aId, const String& oId) const
538 _AppControlAliasEntry* pEntry = GetAppControlAliasEntry(aId, oId);
542 AppControl* pAc = GetNativeAppControlN(pEntry->provider2, pEntry->operation2);
545 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
546 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
550 pAc = GetAppControlN(pEntry->provider2, pEntry->operation2);
553 _AppControlImpl* pImpl = _AppControlImpl::GetInstance(*pAc);
554 pImpl->SetProperty(_APPCONTROL_PROPERTY_ALIAS);
558 SysLog(NID_APP, "No matching AppControl found.");
565 SysLog(NID_APP, "No matching AppControl found.");
570 _AppControlRegistry::GetAppControlN(const String& appId, const String& operationId) const
572 bool changeAppId = false;
574 String actualAppId = appId;
575 if (appId.StartsWith(TIZEN_ALIAS_APPID_PREFIX, 0))
578 result r = __aliasAppId.GetValue(appId, tmp);
582 SysLog(NID_APP, "Found alias appId (%ls -> %ls).", appId.GetPointer(), tmp.GetPointer());
588 bool b = _Aul::IsInstalled(actualAppId);
589 SysTryReturn(NID_APP, b == true, null, E_APP_NOT_INSTALLED, "[E_APP_NOT_INSTALLED] %ls not installed.", actualAppId.GetPointer());
591 return _AppControlImpl::CreateN(actualAppId, operationId, changeAppId);
595 _AppControlRegistry::GetReverseAliasAppId(const AppId& appId) const
597 std::unique_ptr< IMapEnumeratorT<String, String> > pEnum(__aliasAppId.GetMapEnumeratorN());
601 while (pEnum->MoveNext() == E_SUCCESS)
604 pEnum->GetValue(value);
611 SysLog(NID_APP, "No entry found for %ls", appId.GetPointer());