1 /*
2  * Copyright (c) 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_EXTENSION_ABILITY_THREAD_H
17 #define OHOS_ABILITY_RUNTIME_EXTENSION_ABILITY_THREAD_H
18 
19 #include "ability_thread.h"
20 #include "extension_impl.h"
21 #include "want.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 class AbilityLocalRecord;
26 class OHOSApplication;
27 } // namespace AppExecFwk
28 namespace AbilityRuntime {
29 using LifeCycleStateInfo = OHOS::AAFwk::LifeCycleStateInfo;
30 class ExtensionAbilityThread : public AppExecFwk::AbilityThread {
31 public:
32     /**
33      * @brief Default constructor used to create a ExtensionAbilityThread instance.
34      */
35     ExtensionAbilityThread();
36     ~ExtensionAbilityThread() override;
37 
38     /**
39      * @brief Attach The ability thread to the main process.
40      * @param application Indicates the main process.
41      * @param abilityRecord current running ability record
42      * @param mainRunner The runner which main_thread holds.
43      * @param appContext the AbilityRuntime context
44      */
45     void Attach(const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
46         const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord,
47         const std::shared_ptr<AppExecFwk::EventRunner> &mainRunner,
48         [[maybe_unused]] const std::shared_ptr<Context> &appContext) override;
49 
50     /**
51      * @brief Attach The ability thread to the main process.
52      * @param application Indicates the main process.
53      * @param abilityRecord current running ability record
54      * @param appContext the AbilityRuntime context
55      */
56     void Attach(const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
57         const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord,
58         [[maybe_unused]] const std::shared_ptr<Context> &appContext) override;
59 
60     /**
61      * @brief Provide operating system AbilityTransaction information to the observer
62      * @param want Indicates the structure containing Transaction information about the ability.
63      * @param targetState Indicates the lifecycle state.
64      * @param sessionInfo Indicates the session info.
65      */
66     bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState,
67         sptr<AAFwk::SessionInfo> sessionInfo = nullptr) override;
68 
69     /**
70      * @brief Provide operating system ConnectAbility information to the observer
71      * @param want Indicates the structure containing connect information about the ability.
72      */
73     void ScheduleConnectAbility(const Want &want) override;
74 
75     /**
76      * @brief Provide operating system DisconnectAbility information to the observer
77      * @param want Indicates the structure containing connect information about the ability.
78      */
79     void ScheduleDisconnectAbility(const Want &want) override;
80 
81     /**
82      * @brief Provide operating system CommandAbility information to the observer
83      * @param want The Want object to command to.
84      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
85      * destroyed, and the value false indicates a normal startup.
86      * @param startId Indicates the number of times the Service ability has been started. The startId is incremented by
87      * 1 every time the ability is started. For example, if the ability has been started for six times, the value of
88      * startId is 6.
89      */
90     void ScheduleCommandAbility(const Want &want, bool restart, int startId) override;
91 
92     /**
93      * @brief Schedule Command AbilityWindow
94      * @param want The Want object to command to.
95      * @param sessionInfo Indicates the session info.
96      * @param winCmd Indicates the WindowCommand of winCmd
97      */
98     void ScheduleCommandAbilityWindow(
99         const Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd) override;
100 
101     /**
102      * @brief ScheduleUpdateConfiguration, scheduling update configuration.
103      * @param config Indicates the updated configuration information
104      */
105     void ScheduleUpdateConfiguration(const AppExecFwk::Configuration &config) override;
106 
107     /**
108      * @brief Send the result code and data to be returned by this Page ability to the caller.
109      * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int32_t, int32_t, Want)
110      * method to receive the result set in the current method. This method can be called only after the ability has
111      * been initialized.
112      * @param requestCode Indicates the request code for send.
113      * @param resultCode Indicates the result code returned after the ability is destroyed. You can define the result
114      * code to identify an error.
115      * @param want Indicates the data returned after the ability is destroyed. You can define the data returned. This
116      * parameter can be null.
117      */
118     void SendResult(int requestCode, int resultCode, const Want &resultData) override;
119 
120     /**
121      * @brief notify this ability current memory level.
122      * @param level Current memory level
123      */
124     void NotifyMemoryLevel(int32_t level) override;
125 
126     /**
127      * @brief Dump ability runner info.
128      * @param params the params need to be Dumped
129      * @param info ability runner info.
130      */
131     void DumpAbilityInfo(const std::vector<std::string> &params, std::vector<std::string> &info) override;
132 
133 private:
134     /**
135      * @brief Dump Ability Info Inner
136      * @param params the params need to be Dumped
137      * @param info ability runner info
138      */
139     void DumpAbilityInfoInner(const std::vector<std::string> &params, std::vector<std::string> &info);
140 
141     /**
142      * @brief Dump Other Info
143      * @param info Indicates the info to be Dumped
144      */
145     void DumpOtherInfo(std::vector<std::string> &info);
146 
147     /**
148      * @brief Create the abilityname.
149      * @param abilityRecord current running ability record.
150      * @param application Indicates the application.
151      * @return Returns the abilityname.
152      */
153     std::string CreateAbilityName(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord,
154         const std::shared_ptr<AppExecFwk::OHOSApplication> &application);
155 
156     /**
157      * @brief Create the extension abilityname.
158      * @param abilityInfo abilityInfo Indicates the parameter about abilityInfo.
159      * @param abilityName abilityName Indicates the parameter about abilityName.
160      */
161     void CreateExtensionAbilityName(
162         const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo, std::string &abilityName);
163 
164     /**
165      * @brief Handle the life cycle of Extension.
166      * @param want Indicates the structure containing lifecycle information about the extension.
167      * @param lifeCycleStateInfo  Indicates the lifecycle state.
168      * @param sessionInfo Indicates the session info.
169      */
170     void HandleExtensionTransaction(const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo,
171         sptr<AAFwk::SessionInfo> sessionInfo = nullptr);
172 
173     /**
174      * @brief Handle the current connection of Extension.
175      * @param want Indicates the structure containing connection information about the extension.
176      */
177     void HandleConnectExtension(const Want &want);
178 
179     /**
180      * @brief Handle the current disconnection of Extension.
181      * @param want Indicates the structure containing connection information about the extension.
182      */
183     void HandleDisconnectExtension(const Want &want);
184 
185     /**
186      * @brief Handle the current command of Extension.
187      * @param want The Want object to command to.
188      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
189      * destroyed, and the value false indicates a normal startup.
190      * @param startId Indicates the number of times the Service extension has been started. The startId is incremented
191      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
192      * value of startId is 6.
193      */
194     void HandleCommandExtension(const Want &want, bool restart, int32_t startId);
195 
196     /**
197      * @brief Handle Command Extension Window.
198      * @param want The Want object to command to.
199      * @param sessionInfo Indicates the sessionInfo.
200      * @param winCmd Indicates the winCmd.
201      */
202     void HandleCommandExtensionWindow(
203         const Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, AAFwk::WindowCommand winCmd);
204 
205     /**
206      * @brief Handle the scheduling update configuration of exabilityInfotension.
207      * @param config Indicates the updated configuration information
208      */
209     void HandleExtensionUpdateConfiguration(const AppExecFwk::Configuration &config);
210 
211     void HandleAttach(const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
212         const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord,
213         const std::shared_ptr<AppExecFwk::EventRunner> &mainRunner);
214 
215     void HandleAttachInner(const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
216         const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &abilityRecord);
217 
218     /**
219      * @brief Handle the current command of Extension.
220      * @param want The Want object to command to.
221      */
222     void HandleInsightIntent(const Want &want);
223 
224     void ScheduleCommandAbilityInner(const Want &want, bool restart, int32_t startId);
225 
226     void ScheduleInsightIntentInner(const Want &want);
227 
228     std::shared_ptr<ExtensionImpl> extensionImpl_ = nullptr;
229     std::shared_ptr<Extension> currentExtension_ = nullptr;
230 };
231 } // namespace AbilityRuntime
232 } // namespace OHOS
233 #endif // OHOS_ABILITY_RUNTIME_EXTENSION_ABILITY_THREAD_H
234