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