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);
112 SysPropagate(NID_APP, r);
116 const int secCount = reg.GetAllSectionCount();
117 SysTryReturnResult(NID_APP, !(secCount <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
119 SysLog(NID_APP, "Loading %d sections from %ls", secCount, regPath.GetPointer());
126 for (int i = 0; i < secCount; i++)
128 ///////////////////////////////////////////////////////////////////////
130 String sec_name(reg.GetSectionName(i));
133 if (sec_name.IsEmpty())
138 ///////////////////////////////////////////////////////////////////////
140 index = reg.GetEntryIndex(i, ACTL_REGISTRY_PATH);
143 size = REG_VALUE_BUFFER_LEN;
144 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &path, &size);
147 __tizenList.Add(sec_name, path);
150 SysLog(NID_APP, "Finished loading %d tizen AppControl entries", __tizenList.GetCount());
156 _AppControlRegistry::LoadLegacyList(void)
160 const String& regPath(ACTL_LEGACY_FILE);
162 result r = reg.Construct(regPath, REG_OPEN_READ_ONLY, null);
165 SysPropagate(NID_APP, r);
169 const int sec_count = reg.GetAllSectionCount();
170 SysTryReturnResult(NID_APP, !(sec_count <= 0), E_OBJ_NOT_FOUND, "Registry contains no data.");
172 SysLog(NID_APP, "Loading %d sections from %ls", sec_count, regPath.GetPointer());
174 String aliasProvider;
178 // actual parameter manipulation
179 for (int i = 0; i < sec_count; i++)
181 ///////////////////////////////////////////////////////////////////////
183 const String& secName(reg.GetSectionName(i));
185 ///////////////////////////////////////////////////////////////////////
186 // aliased appcontrol name
187 const int index = reg.GetEntryIndex(i, ACTL_REGISTRY_ALIAS_PROVIDER);
190 int size = REG_VALUE_BUFFER_LEN;
191 reg.GetEntryValue(i, index, REG_VALUE_TYPE_STRING, &aliasProvider, &size);
194 ///////////////////////////////////////////////////////////////////////
196 IList* pList = reg.GetAllEntryNamesN(secName);
202 std::unique_ptr<IEnumerator> pEnum(pList->GetEnumeratorN());
204 while (pEnum->MoveNext() == E_SUCCESS)
206 String* pStr = dynamic_cast<String*>(pEnum->GetCurrent());
208 if (pStr == null || pStr->IsEmpty() || pStr->Equals(ACTL_REGISTRY_ALIAS_PROVIDER, false))
213 r = reg.GetValue(secName, *pStr, value);
216 SysPropagate(NID_APP, r);
220 // add entry to aliasList
221 _AppControlAliasEntry* pEntry = new (std::nothrow) _AppControlAliasEntry(secName, *pStr, aliasProvider, value);
222 SysTryReturnResult(NID_APP, pEntry != null, E_OUT_OF_MEMORY, "Insufficient memory.");
226 SysLog(NID_APP, "(%ls, %ls)", secName.GetPointer(), aliasProvider.GetPointer());
227 __aliasList.Add(secName, pEntry);
231 pList->RemoveAll(true);
235 SysLog(NID_APP, "Finished loading %d entries.", __aliasList.GetCount());
242 _AppControlRegistry::GetTizenAppControlN(const String& aId, const String& oId) const
245 const String* pAppId = &aId;
246 const String* pOperation = &oId;
248 // legacy check first
251 const _AppControlAliasEntry* const pEntry = GetAppControlAliasEntry(*pAppId, *pOperation);
252 // number does not matter
253 if (count >= 5 || pEntry == null)
258 pAppId = &pEntry->provider2;
259 pOperation = &pEntry->operation2;
265 SysAssert(pAppId != null);
266 SysAssert(pOperation != null);
268 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)", pAppId->GetPointer(), pOperation->GetPointer());
270 const String& soName = GetTizenAppControlProvider(*pAppId, *pOperation);
272 return _AppControlImpl::CreateN(soName, *pAppId, *pOperation, _APPCONTROL_PROPERTY_PUBLIC);
277 _AppControlRegistry::GetTizenAppControlProvider(const String& appId, const String& oId) const
280 result r = __tizenList.GetValue(appId, val);
283 SysLog(NID_APP, "Found TIZEN AppControl stub %ls for %ls.", val.GetPointer(), appId.GetPointer());
287 SysLog(NID_APP, "Default platform AppControl %ls is used.", ACTL_DEFAULT_PLUGIN);
288 return String(ACTL_DEFAULT_PLUGIN);
293 // data structure for _AppControlRegistry::FindAppControlListN() only
295 struct AppSvcIterData
298 AppSvcIterData(const _AppControlRegistry* pRegs, ArrayList* pArr, const String& op) : pThis(pRegs), pArray(pArr), operation(op) {}
300 const _AppControlRegistry* pThis;
302 const String& operation;
306 // callback function for _AppControlRegistry::FindAppControlListN()
309 AppSvcIterFnCb(const char* pAppId, void* pData)
311 SysAssert(pData != null);
313 AppSvcIterData* pAppSvcIterData = static_cast<AppSvcIterData*>(pData);
314 ArrayList* pList = pAppSvcIterData->pArray;
315 SysAssert(pList != null);
316 const _AppControlRegistry* pThis = pAppSvcIterData->pThis;
317 SysAssert(pThis != null);
318 const String& operation = pAppSvcIterData->operation;
322 SysLog(NID_APP, "Empty appId received.");
326 String appId = pAppId;
327 AppControl* pAc = pThis->GetTizenAppControlN(appId, operation);
330 SysLog(NID_APP, "AppControl allocation failure for %ls.", appId.GetPointer());
339 Tizen::Base::Collection::ArrayList*
340 _AppControlRegistry::FindAppControlListN(const String* pOid, const String* pUri, const String* pMimeType, const String* pCategory) const
342 String operation = (pOid) ? *pOid : TIZEN_OPERATION_MAIN;
344 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
345 SysTryReturn(NID_APP, pBundle.get(), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Bundle creation failure.");
347 _AppMessageImpl::SetOperation(pBundle.get(), operation);
351 _AppMessageImpl::SetUri(pBundle.get(), *pUri);
356 _AppMessageImpl::SetMime(pBundle.get(), *pMimeType);
361 _AppMessageImpl::SetCategory(pBundle.get(), *pCategory);
364 ArrayList* pList = new (std::nothrow) ArrayList;
365 SysTryReturn(NID_APP, pList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ArrayList creation failure.");
368 AppSvcIterData data(this, pList, operation);
370 appsvc_get_list(pBundle.get(), AppSvcIterFnCb, reinterpret_cast<void*>(&data));
372 if (pList->GetCount() == 0)
374 SysLog(NID_APP, "Found no AppControl entry for operation %ls.", operation.GetPointer());
383 const _AppControlRegistry::_AppControlAliasEntry*
384 _AppControlRegistry::GetAppControlAliasEntry(const String& aId, const String& oId) const
386 std::unique_ptr< IEnumeratorT<_AppControlAliasEntry*> > pEnum(__aliasList.GetValuesN(aId));
387 if (pEnum.get() == null)
389 SysLog(NID_APP, "[E_OBJ_NOT_FOUND] No alias entry for %ls.", aId.GetPointer());
393 while (pEnum->MoveNext() == E_SUCCESS)
395 _AppControlAliasEntry* pEntry = null;
396 pEnum->GetCurrent(pEntry);
397 if (pEntry->provider == aId && pEntry->operation == oId)
399 SysLog(NID_APP, "Found matching AppControl (%ls, %ls)->(%ls, %ls)", aId.GetPointer(), oId.GetPointer(), pEntry->provider2.GetPointer(), pEntry->operation2.GetPointer());
410 _AppControlRegistry::GetAliasAppId(const AppId& appId) const
412 std::unique_ptr<bundle, BundleDeleter> pBundle(bundle_create());
416 std::unique_ptr<char[]> pAppId(_StringConverter::CopyToCharArrayN(appId));
418 // appsvc_set_appid() lookup for actual app ID internally
419 appsvc_set_appid(pBundle.get(), pAppId.get());
420 const char* pTmp = appsvc_get_appid(pBundle.get());