1 /*
2  * Copyright (c) 2020 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 "log.h"
18 
19 namespace OHOS {
GetInstance()20 AbilityLoader &AbilityLoader::GetInstance()
21 {
22     static AbilityLoader abilityLoader;
23     return abilityLoader;
24 }
25 
RegisterAbility(const std::string & abilityName,const CreateAbility & createFunc)26 void AbilityLoader::RegisterAbility(const std::string &abilityName, const CreateAbility &createFunc)
27 {
28     abilities_.emplace(abilityName, createFunc);
29     HILOG_INFO(HILOG_MODULE_APP, "RegisterAbility %s", abilityName.c_str());
30 }
31 
GetAbilityByName(const std::string & abilityName)32 Ability *AbilityLoader::GetAbilityByName(const std::string &abilityName)
33 {
34     auto it = abilities_.find(abilityName);
35     if (it == abilities_.end()) {
36         HILOG_ERROR(HILOG_MODULE_APP, "GetAbilityByName failed: %s", abilityName.c_str());
37         return nullptr;
38     } else {
39         return it->second();
40     }
41 }
42 
43 #ifdef ABILITY_WINDOW_SUPPORT
RegisterAbilitySlice(const std::string & sliceName,const CreateSlice & createFunc)44 void AbilityLoader::RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc)
45 {
46     slices_.emplace(sliceName, createFunc);
47     HILOG_INFO(HILOG_MODULE_APP, "RegisterAbilitySlice %s", sliceName.c_str());
48 }
49 
GetAbilitySliceByName(const std::string & sliceName)50 AbilitySlice *AbilityLoader::GetAbilitySliceByName(const std::string &sliceName)
51 {
52     auto it = slices_.find(sliceName);
53     if (it == slices_.end()) {
54         HILOG_ERROR(HILOG_MODULE_APP, "GetAbilitySliceByName failed: %s", sliceName.c_str());
55         return nullptr;
56     } else {
57         return it->second();
58     }
59 }
60 #endif
61 } //  namespace OHOS
62