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>
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 "FApp_AppControlImpl.h"
40 #include "FApp_AppControlRegistry.h"
41 #include "FApp_AppMessageImpl.h"
42 #include "FApp_TemplateUtil.h"
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Io;
51 const wchar_t ACTL_DEFAULT_FILE[] = L"/usr/etc/app-control-info.ini";
52 const wchar_t ACTL_LEGACY_FILE[] = L"/usr/etc/app-control-alias.ini";
53 const wchar_t ACTL_DEFAULT_PLUGIN[] = L"libosp-ac-platform.so";
55 const wchar_t TIZEN_ALIAS_APPID_PREFIX[] = L"tizen.";
57 const String ACTL_REGISTRY_PATH = L"Path";
58 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
60 const int PKG_APPID_LEN = 256;
61 const int REG_VALUE_BUFFER_LEN = 256;
66 namespace Tizen { namespace App
69 _AppControlRegistry* _AppControlRegistry::__pSelf = null;
71 _AppControlRegistry::_AppControlRegistry(void)
73 __tizenList.Construct();
75 __aliasList.Construct();
78 _AppControlRegistry::~_AppControlRegistry(void)
80 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
84 _AppControlRegistry::GetInstance(void)
88 SysLog(NID_APP, "Create new instance");
89 __pSelf = new (std::nothrow) _AppControlRegistry();
90 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
91 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
93 __pSelf->LoadTizenAppControlRegistry();
95 __pSelf->LoadLegacyList();
103 _AppControlRegistry::LoadTizenAppControlRegistry(void)
107 const String regPath = ACTL_DEFAULT_FILE;
109 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
110 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
112 const int secCount = reg.GetAllSectionCount();
113 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
115 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
122 for (int i = 0; i < secCount; i++)
124 ///////////////////////////////////////////////////////////////////////
126 String sec_name(reg.GetSectionName(i));
129 if (sec_name.IsEmpty())
134 ///////////////////////////////////////////////////////////////////////
136 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
139 size = REG_VALUE_BUFFER_LEN;
140 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
143 __tizenList.Add(sec_name, path);
146 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
152 _AppControlRegistry::LoadLegacyList(void)
156 const String& regPath(ACTL_LEGACY_FILE);
158 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
159 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
161 const int sec_count = reg.GetAllSectionCount();
162 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
164 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
166 String aliasProvider;
170 // actual parameter manipulation
171 for (int i = 0; i < sec_count; i++)
173 ///////////////////////////////////////////////////////////////////////
175 const String& secName(reg.GetSectionName(i));
177 ///////////////////////////////////////////////////////////////////////
178 // aliased appcontrol name
179 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
182 int size = REG_VALUE_BUFFER_LEN;
183 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
186 ///////////////////////////////////////////////////////////////////////
188 IList* pList = reg.GetAllEntryNamesN(secName);
194 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
196 while (pEnum->MoveNext() == E_SUCCESS)
198 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
200 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
205 r = reg.GetValue(secName, *pStr, value);
208 SysLog(NID_APP, "[%s] Propagating.", GetErrorMessage(r));
212 // add entry to aliasList
213 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
214 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
218 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
219 __aliasList.Add(secName, pEntry);
223 pList->RemoveAll(true);
227 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
234 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
237 const String* pAppId = &aId;
238 const String* pOperation = &oId;
240 // legacy check first
243 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
244 // number does not matter
245 if (count >= 5 || pEntry == null)
250 pAppId = &pEntry->provider2;
251 pOperation = &pEntry->operation2;
257 SysAssert(pAppId != null);
258 SysAssert(pOperation != null);
260 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
262 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
263 SysAssert(!soName.IsEmpty());
265 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, _APPCONTROL_PROPERTY_PUBLIC);
270 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
273 result r = __tizenList.GetValue(appId, val);
276 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
280 SysLog(NID_APP, "Default platform AppControl %ls is used.", ACTL_DEFAULT_PLUGIN);
281 return String(ACTL_DEFAULT_PLUGIN);
286 // data structure for _AppControlRegistry::FindAppControlListN() only
288 struct AppSvcIterData
291 AppSvcIterData(const _AppControlRegistry* pRegs, ArrayList* pArr, const String& op) : pThis(pRegs), pArray(pArr), operation(op) {}
293 const _AppControlRegistry* pThis;
295 const String& operation;
299 // callback function for _AppControlRegistry::FindAppControlListN()
302 AppSvcIterFnCb(const char* pAppId, void* pData)
304 SysAssert(pData != null);
306 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
307 ArrayList* pList = pAppSvcIterData->pArray;
308 SysAssert(pList != null);
309 const _AppControlRegistry* pThis = pAppSvcIterData->pThis;
310 SysAssert(pThis != null);
311 const String& operation = pAppSvcIterData->operation;
315 SysLog(NID_APP, "Empty appId received.");
319 String appId = pAppId;
320 AppControl* pAc = pThis->GetTizenAppControlN(appId, operation);
323 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
332 Tizen::Base::Collection::ArrayList*
333 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
335 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
337 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
338 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
340 _AppMessageImpl::SetOperation(pBundle.get(), operation);
344 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
349 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
354 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
357 ArrayList* pList = new (std::nothrow) ArrayList;
358 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
361 AppSvcIterData data(this, pList, operation);
363 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
365 if (pList->GetCount() == 0)
367 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
376 const _AppControlRegistry::_AppControlAliasEntry*
377 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
379 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
380 if (pEnum.get() == null)
382 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
386 while (pEnum->MoveNext() == E_SUCCESS)
388 _AppControlAliasEntry* pEntry = null;
389 pEnum->GetCurrent(pEntry);
390 if (pEntry->provider == aId && pEntry->operation == oId)
392 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
403 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
405 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
409 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
411 // appsvc_set_appid() lookup for actual app ID internally
412 appsvc_set_appid(pBundle.get(), pAppId.get());
413 const char* pTmp = appsvc_get_appid(pBundle.get());