1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ability_loader.h"
17 #include "hilog_tag_wrapper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
GetInstance()21 AbilityLoader &AbilityLoader::GetInstance()
22 {
23     static AbilityLoader abilityLoader;
24     return abilityLoader;
25 }
26 
RegisterAbility(const std::string & abilityName,const CreateAblity & createFunc)27 void AbilityLoader::RegisterAbility(const std::string &abilityName, const CreateAblity &createFunc)
28 {
29     abilities_.insert_or_assign(abilityName, createFunc);
30     TAG_LOGD(AAFwkTag::ABILITY, "%{public}s", abilityName.c_str());
31 }
32 
RegisterExtension(const std::string & abilityName,const CreateExtension & createFunc)33 void AbilityLoader::RegisterExtension(const std::string &abilityName, const CreateExtension &createFunc)
34 {
35     extensions_.emplace(abilityName, createFunc);
36     TAG_LOGD(AAFwkTag::ABILITY, "%{public}s", abilityName.c_str());
37 }
38 
RegisterUIAbility(const std::string & abilityName,const CreateUIAbility & createFunc)39 void AbilityLoader::RegisterUIAbility(const std::string &abilityName, const CreateUIAbility &createFunc)
40 {
41     TAG_LOGD(AAFwkTag::ABILITY, "%{public}s", abilityName.c_str());
42     uiAbilities_.emplace(abilityName, createFunc);
43 }
44 
GetAbilityByName(const std::string & abilityName)45 Ability *AbilityLoader::GetAbilityByName(const std::string &abilityName)
46 {
47     auto it = abilities_.find(abilityName);
48     if (it != abilities_.end()) {
49         return it->second();
50     }
51     TAG_LOGE(AAFwkTag::ABILITY, "failed:%{public}s", abilityName.c_str());
52     return nullptr;
53 }
54 
GetExtensionByName(const std::string & abilityName)55 AbilityRuntime::Extension *AbilityLoader::GetExtensionByName(const std::string &abilityName)
56 {
57     auto it = extensions_.find(abilityName);
58     if (it != extensions_.end()) {
59         return it->second();
60     }
61     TAG_LOGE(AAFwkTag::ABILITY, "failed:%{public}s", abilityName.c_str());
62     return nullptr;
63 }
64 
GetUIAbilityByName(const std::string & abilityName)65 AbilityRuntime::UIAbility *AbilityLoader::GetUIAbilityByName(const std::string &abilityName)
66 {
67     auto it = uiAbilities_.find(abilityName);
68     if (it != uiAbilities_.end()) {
69         return it->second();
70     }
71     TAG_LOGE(AAFwkTag::ABILITY, "failed:%{public}s", abilityName.c_str());
72     return nullptr;
73 }
74 
75 #ifdef ABILITY_WINDOW_SUPPORT
RegisterAbilitySlice(const std::string & sliceName,const CreateSlice & createFunc)76 void AbilityLoader::RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc)
77 {
78     slices_.emplace(sliceName, createFunc);
79     TAG_LOGD(AAFwkTag::ABILITY, HILOG_MODULE_APP, "%s", sliceName.c_str());
80 }
81 
GetAbilitySliceByName(const std::string & sliceName)82 AbilitySlice *AbilityLoader::GetAbilitySliceByName(const std::string &sliceName)
83 {
84     auto it = slices_.find(sliceName);
85     if (it != slices_.end()) {
86         return it->second();
87     }
88     TAG_LOGE(AAFwkTag::ABILITY, HILOG_MODULE_APP, "failed:%s", sliceName.c_str());
89     return nullptr;
90 }
91 #endif
92 }  // namespace AppExecFwk
93 }  // namespace OHOS
94