• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_APPEXECFWK_OHOS_ABILITY_LOADER_H
17 #define FOUNDATION_APPEXECFWK_OHOS_ABILITY_LOADER_H
18 
19 #include "ability.h"
20 #include "ohos_application.h"
21 #include <functional>
22 #include <string>
23 #include <unordered_map>
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 using CreateAblity = std::function<Ability *(void)>;
28 #ifdef ABILITY_WINDOW_SUPPORT
29 using CreateSlice = std::function<AbilitySlice *(void)>;
30 #endif
31 /**
32  * @brief Declares functions for registering the class names of {@link Ability} and {@link AbilitySlice} with the
33  *        ability management framework.
34  *
35  * After creating your own {@link Ability} and {@link AbilitySlice} child classes, you should register their class
36  * names with the ability management framework so that the application can start <b>Ability</b> instances created in
37  * the background.
38  *
39  * @since 1
40  * @version 1
41  */
42 class AbilityLoader {
43 public:
44     static AbilityLoader &GetInstance();
45 
46     ~AbilityLoader() = default;
47 
48     /**
49      * @brief Register Ability Info
50      *
51      * @param abilityName ability classname
52      * @param createFunc  Constructor address
53      */
54     void RegisterAbility(const std::string &abilityName, const CreateAblity &createFunc);
55 
56     /**
57      * @brief Get Ability address
58      *
59      * @param abilityName ability classname
60      *
61      * @return return Ability address
62      */
63     Ability *GetAbilityByName(const std::string &abilityName);
64 
65 #ifdef ABILITY_WINDOW_SUPPORT
66     void RegisterAbilitySlice(const std::string &sliceName, const CreateSlice &createFunc);
67     AbilitySlice *GetAbilitySliceByName(const std::string &sliceName);
68 #endif
69 
70 private:
71     AbilityLoader() = default;
72     AbilityLoader(const AbilityLoader &) = delete;
73     AbilityLoader &operator=(const AbilityLoader &) = delete;
74     AbilityLoader(AbilityLoader &&) = delete;
75     AbilityLoader &operator=(AbilityLoader &&) = delete;
76 
77     std::unordered_map<std::string, CreateAblity> abilities_;
78 };
79 /**
80  * @brief Registers the class name of an {@link Ability} child class.
81  *
82  * After implementing your own {@link Ability} class, you should call this function so that the ability management
83  * framework can create <b>Ability</b> instances when loading your <b>Ability</b> class.
84  *
85  * @param className Indicates the {@link Ability} class name to register.
86  */
87 #define REGISTER_AA(className)                                                       \
88     __attribute__((constructor)) void RegisterAA##className()                        \
89     {                                                                                \
90         AbilityLoader::GetInstance().RegisterAbility(                                \
91             #className, []()->Ability * { return new (std::nothrow) className; });   \
92     }
93 
94 /**
95  * @brief Registers the class name of an {@link AbilitySlice} child class.
96  *
97  * After implementing your own {@link AbilitySlice} class, you should call this function so that the ability
98  * management framework can create <b>AbilitySlice</b> instances when loading your <b>AbilitySlice</b> class.
99  *
100  * @param className Indicates the {@link AbilitySlice} class name to register.
101  */
102 #ifdef ABILITY_WINDOW_SUPPORT
103 #define REGISTER_AS(className)                              \
104     __attribute((constructor)) void RegisterAS##className() \
105     {                                                       \
106         AbilityLoader::GetInstance().RegisterAbilitySlice(  \
107             #className, []()->AbilitySlice * { return new (std::nothrow) className; });
108 }
109 #endif
110 }  // namespace OHOS
111 }  // OHOS
112 #endif  // FOUNDATION_APPEXECFWK_OHOS_ABILITY_LOADER_H