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 String ACTL_REGISTRY_PATH = L"Path";
56 const String ACTL_REGISTRY_ALIAS_PROVIDER = L"PROVIDER_ALIAS";
58 const int REG_VALUE_BUFFER_LEN = 256;
63 namespace Tizen { namespace App
66 _AppControlRegistry* _AppControlRegistry::__pSelf = null;
68 _AppControlRegistry::_AppControlRegistry(void)
70 __tizenList.Construct();
72 __aliasList.Construct();
75 _AppControlRegistry::~_AppControlRegistry(void)
77 _DeleteCollectionMapValue<String, _AppControlAliasEntry>(__aliasList);
81 _AppControlRegistry::GetInstance(void)
85 SysLog(NID_APP, "Create new instance");
86 __pSelf = new (std::nothrow) _AppControlRegistry();
87 SysTryReturn(NID_APP, __pSelf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Insufficient memory.");
88 SysAssertf(__pSelf != null, "AppControl registry instance creation failure");
90 __pSelf->LoadTizenAppControlRegistry();
92 __pSelf->LoadLegacyList();
100 _AppControlRegistry::LoadTizenAppControlRegistry(void)
104 const String regPath = ACTL_DEFAULT_FILE;
106 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
109 SysPropagate(NID_APP, r);
113 const int secCount = reg.GetAllSectionCount();
114 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
116 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
123 for (int i = 0; i < secCount; i++)
125 ///////////////////////////////////////////////////////////////////////
127 String sec_name(reg.GetSectionName(i));
130 if (sec_name.IsEmpty())
135 ///////////////////////////////////////////////////////////////////////
137 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
140 size = REG_VALUE_BUFFER_LEN;
141 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
144 __tizenList.Add(sec_name, path);
147 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
153 _AppControlRegistry::LoadLegacyList(void)
157 const String& regPath(ACTL_LEGACY_FILE);
159 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
162 SysPropagate(NID_APP, r);
166 const int sec_count = reg.GetAllSectionCount();
167 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
169 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
171 String aliasProvider;
175 // actual parameter manipulation
176 for (int i = 0; i < sec_count; i++)
178 ///////////////////////////////////////////////////////////////////////
180 const String& secName(reg.GetSectionName(i));
182 ///////////////////////////////////////////////////////////////////////
183 // aliased appcontrol name
184 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
187 int size = REG_VALUE_BUFFER_LEN;
188 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
191 ///////////////////////////////////////////////////////////////////////
193 IList* pList = reg.GetAllEntryNamesN(secName);
199 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
201 while (pEnum->MoveNext() == E_SUCCESS)
203 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
205 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
210 r = reg.GetValue(secName, *pStr, value);
213 SysPropagate(NID_APP, r);
217 // add entry to aliasList
218 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
219 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
223 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
224 __aliasList.Add(secName, pEntry);
228 pList->RemoveAll(true);
232 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
239 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
242 const String* pAppId = &aId;
243 const String* pOperation = &oId;
245 // legacy check first
248 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
249 // number does not matter
250 if (count >= 5 || pEntry == null)
255 pAppId = &pEntry->provider2;
256 pOperation = &pEntry->operation2;
262 SysAssert(pAppId != null);
263 SysAssert(pOperation != null);
265 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
267 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
269 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, _APPCONTROL_PROPERTY_PUBLIC);
274 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
277 result r = __tizenList.GetValue(appId, val);
280 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
284 SysLog(NID_APP, "Default platform AppControl %ls is used.", ACTL_DEFAULT_PLUGIN);
285 return String(ACTL_DEFAULT_PLUGIN);
290 // data structure for _AppControlRegistry::FindAppControlListN() only
292 struct AppSvcIterData
295 AppSvcIterData(const _AppControlRegistry* pRegs, ArrayList* pArr, const String& op) : pThis(pRegs), pArray(pArr), operation(op) {}
297 const _AppControlRegistry* pThis;
299 const String& operation;
303 // callback function for _AppControlRegistry::FindAppControlListN()
306 AppSvcIterFnCb(const char* pAppId, void* pData)
308 SysAssert(pData != null);
310 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
311 ArrayList* pList = pAppSvcIterData->pArray;
312 SysAssert(pList != null);
313 const _AppControlRegistry* pThis = pAppSvcIterData->pThis;
314 SysAssert(pThis != null);
315 const String& operation = pAppSvcIterData->operation;
319 SysLog(NID_APP, "Empty appId received.");
323 String appId = pAppId;
324 AppControl* pAc = pThis->GetTizenAppControlN(appId, operation);
327 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
336 Tizen::Base::Collection::ArrayList*
337 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
339 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
341 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
342 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
344 _AppMessageImpl::SetOperation(pBundle.get(), operation);
348 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
353 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
358 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
361 ArrayList* pList = new (std::nothrow) ArrayList;
362 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
365 AppSvcIterData data(this, pList, operation);
367 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
369 if (pList->GetCount() == 0)
371 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
380 const _AppControlRegistry::_AppControlAliasEntry*
381 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
383 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
384 if (pEnum.get() == null)
386 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
390 while (pEnum->MoveNext() == E_SUCCESS)
392 _AppControlAliasEntry* pEntry = null;
393 pEnum->GetCurrent(pEntry);
394 if (pEntry->provider == aId && pEntry->operation == oId)
396 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
407 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
409 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
413 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
415 // appsvc_set_appid() lookup for actual app ID internally
416 appsvc_set_appid(pBundle.get(), pAppId.get());
417 const char* pTmp = appsvc_get_appid(pBundle.get());