1 /*
2 * Copyright (c) 2021 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 "app_log_wrapper.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
GetInstance()21 AbilityLoader &AbilityLoader::GetInstance()
22 {
23 static AbilityLoader abilityLoader;
24 return abilityLoader;
25 }
26
27 /**
28 * @brief Register Ability Info
29 *
30 * @param abilityName ability classname
31 * @param createFunc Constructor address
32 */
RegisterAbility(const std::string & abilityName,const CreateAblity & createFunc)33 void AbilityLoader::RegisterAbility(const std::string &abilityName, const CreateAblity &createFunc)
34 {
35 abilities_.emplace(abilityName, createFunc);
36 APP_LOGD("AbilityLoader::RegisterAbility:%{public}s", abilityName.c_str());
37 }
38
39 /**
40 * @brief Get Ability address
41 *
42 * @param abilityName ability classname
43 *
44 * @return return Ability address
45 */
GetAbilityByName(const std::string & abilityName)46 Ability *AbilityLoader::GetAbilityByName(const std::string &abilityName)
47 {
48 auto it = abilities_.find(abilityName);
49 if (it != abilities_.end()) {
50 return it->second();
51 } else {
52 APP_LOGE("AbilityLoader::GetAbilityByName failed:%{public}s", abilityName.c_str());
53 }
54 return nullptr;
55 }
56
57 #ifdef ABILITY_WINDOW_SUPPORT
RegisterAbilitySlice(const std::string & sliceName,const CreateSlice & createFunc)58 void AbilityLoader::RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc)
59 {
60 slices_.emplace(sliceName, createFunc);
61 HILOG_INFO(HILOG_MODULE_APP, "RegisterAbilitySlice %s", sliceName.c_str());
62 }
63
GetAbilitySliceByName(const std::string & sliceName)64 AbilitySlice *AbilityLoader::GetAbilitySliceByName(const std::string &sliceName)
65 {
66 auto it = slices_.find(sliceName);
67 if (it != slices_.end()) {
68 return it->second();
69 } else {
70 HILOG_ERROR(HILOG_MODULE_APP, "GetAbilitySliceByName failed: %s", sliceName.c_str());
71 return nullptr;
72 }
73 }
74 #endif
75 } // namespace AppExecFwk
76 } // namespace OHOS