1 /* 2 * Copyright (c) 2021-2023 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 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_LOADER_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_LOADER_H 18 19 #include <functional> 20 #include <string> 21 #include <unordered_map> 22 23 #include "ability.h" 24 #include "context.h" 25 #include "extension.h" 26 #include "ohos_application.h" 27 #include "ui_ability.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 using CreateExtension = std::function<AbilityRuntime::Extension *(void)>; 32 using CreateAblity = std::function<Ability *(void)>; 33 using CreateUIAbility = std::function<AbilityRuntime::UIAbility *(void)>; 34 #ifdef ABILITY_WINDOW_SUPPORT 35 using CreateSlice = std::function<AbilitySlice *(void)>; 36 #endif 37 /** 38 * @brief Declares functions for registering the class names of {@link Ability} and {@link AbilitySlice} with the 39 * ability management framework. 40 * 41 * After creating your own {@link Ability} and {@link AbilitySlice} child classes, you should register their class 42 * names with the ability management framework so that the application can start <b>Ability</b> instances created in 43 * the background. 44 * 45 * @since 1 46 * @version 1 47 */ 48 class AbilityLoader { 49 public: 50 static AbilityLoader &GetInstance(); 51 52 ~AbilityLoader() = default; 53 54 /** 55 * @brief Register Ability Info 56 * 57 * @param abilityName ability classname 58 * @param createFunc Constructor address 59 */ 60 void RegisterAbility(const std::string &abilityName, const CreateAblity &createFunc); 61 62 /** 63 * @brief Register Extension Info 64 * 65 * @param abilityName Extension classname 66 * @param createFunc Constructor address 67 */ 68 void RegisterExtension(const std::string &abilityName, const CreateExtension &createFunc); 69 70 /** 71 * @brief Register UIAbility Info 72 * @param abilityName UIAbility classname 73 * @param createFunc Constructor address 74 */ 75 void RegisterUIAbility(const std::string &abilityName, const CreateUIAbility &createFunc); 76 77 /** 78 * @brief Get Ability address 79 * 80 * @param abilityName ability classname 81 * 82 * @return return Ability address 83 */ 84 Ability *GetAbilityByName(const std::string &abilityName); 85 86 /** 87 * @brief Get Extension address 88 * 89 * @param abilityName Extension classname 90 * 91 * @return return Ability address 92 */ 93 AbilityRuntime::Extension *GetExtensionByName(const std::string &abilityName); 94 95 /** 96 * @brief Get UIAbility address 97 * @param abilityName UIAbility classname 98 * @return return UIAbility address 99 */ 100 AbilityRuntime::UIAbility *GetUIAbilityByName(const std::string &abilityName); 101 102 #ifdef ABILITY_WINDOW_SUPPORT 103 void RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc); 104 AbilitySlice *GetAbilitySliceByName(const std::string &sliceName); 105 #endif 106 107 private: 108 AbilityLoader() = default; 109 AbilityLoader(const AbilityLoader &) = delete; 110 AbilityLoader &operator=(const AbilityLoader &) = delete; 111 AbilityLoader(AbilityLoader &&) = delete; 112 AbilityLoader &operator=(AbilityLoader &&) = delete; 113 114 std::unordered_map<std::string, CreateAblity> abilities_; 115 std::unordered_map<std::string, CreateExtension> extensions_; 116 std::unordered_map<std::string, CreateUIAbility> uiAbilities_; 117 }; 118 /** 119 * @brief Registers the class name of an {@link Ability} child class. 120 * 121 * After implementing your own {@link Ability} class, you should call this function so that the ability management 122 * framework can create <b>Ability</b> instances when loading your <b>Ability</b> class. 123 * 124 * @param className Indicates the {@link Ability} class name to register. 125 */ 126 #define REGISTER_AA(className) \ 127 __attribute__((constructor)) void RegisterAA##className() \ 128 { \ 129 AbilityLoader::GetInstance().RegisterAbility( \ 130 #className, []()->Ability * { return new (std::nothrow) className; }); \ 131 } 132 133 /** 134 * @brief Registers the class name of an {@link Extension} child class. 135 * 136 * After implementing your own {@link Extension} class, you should call this function so that the extension management 137 * framework can create <b>Extension</b> instances when loading your <b>Extension</b> class. 138 * 139 * @param className Indicates the {@link Extension} class name to register. 140 */ 141 #define REGISTER_EX(className) \ 142 __attribute__((constructor)) void RegisterEX##className() \ 143 { \ 144 AbilityLoader::GetInstance().RegisterExtension( \ 145 #className, []()->AbilityRuntime::Extension * { return new (std::nothrow) className; }); \ 146 } 147 148 /** 149 * @brief Registers the class name of an {@link AbilitySlice} child class. 150 * 151 * After implementing your own {@link AbilitySlice} class, you should call this function so that the ability 152 * management framework can create <b>AbilitySlice</b> instances when loading your <b>AbilitySlice</b> class. 153 * 154 * @param className Indicates the {@link AbilitySlice} class name to register. 155 */ 156 #ifdef ABILITY_WINDOW_SUPPORT 157 #define REGISTER_AS(className) \ 158 __attribute((constructor)) void RegisterAS##className() \ 159 { \ 160 AbilityLoader::GetInstance().RegisterAbilitySlice( \ 161 #className, []()->AbilitySlice * { return new (std::nothrow) className; }); \ 162 } 163 #endif 164 } // namespace OHOS 165 } // OHOS 166 #endif // OHOS_ABILITY_RUNTIME_ABILITY_LOADER_H 167