• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_IMPL_H
17 #define OHOS_ABILITY_RUNTIME_EXTENSION_IMPL_H
18 
19 #include "extension.h"
20 #include "extension_ability_info.h"
21 #include "lifecycle_state_info.h"
22 
23 namespace OHOS {
24 class IRemoteObject;
25 namespace AAFwk {
26 class Want;
27 }
28 namespace AppExecFwk {
29 struct AbilityInfo;
30 class OHOSApplication;
31 class AbilityHandler;
32 class AbilityLocalRecord;
33 }
34 namespace AbilityRuntime {
35 /**
36  * @brief Responsible for managing and scheduling the life cycle of extension.
37  */
38 class ExtensionImpl : public std::enable_shared_from_this<ExtensionImpl> {
39 public:
40     ExtensionImpl() = default;
41     virtual ~ExtensionImpl();
42 
43     /**
44      * @brief Init the object.
45      *
46      * @param application the application info.
47      * @param record the extension record.
48      * @param extension the extension object.
49      * @param handler the extension handler.
50      * @param token the remote token.
51      */
52     void Init(const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
53         const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
54         std::shared_ptr<Extension> &extension,
55         std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token);
56 
57     /**
58      * @brief Connect the Extension. and Calling information back to Extension.
59      *
60      * @param want The Want object to connect to.
61      * @param targetState The terget state.
62      *  @param sessionInfo  Indicates the sessionInfo.
63      *
64      */
65     virtual void HandleExtensionTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState,
66         sptr<AAFwk::SessionInfo> sessionInfo = nullptr);
67 
68     /**
69      * @brief scheduling update configuration of extension.
70      *
71      * @param config Configuration
72      */
73     void ScheduleUpdateConfiguration(const AppExecFwk::Configuration &config);
74 
75     /**
76      * @brief Notify current memory level.
77      *
78      * @param level Current memory level.
79      */
80     void NotifyMemoryLevel(int level);
81 
82     /**
83      * @brief Connect the Extension. and Calling information back to Extension.
84      *
85      * @param want The Want object to connect to.
86      *
87      */
88     sptr<IRemoteObject> ConnectExtension(const Want &want);
89 
90     /**
91      * @brief Connect the Extension. and Calling information back to Extension.
92      *
93      * @param want The Want object to connect to.
94      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
95      */
96     sptr<IRemoteObject> ConnectExtension(const Want &want, bool &isAsyncCallback);
97 
98     /**
99      * @brief The callback of connect.
100      */
101     void ConnectExtensionCallback(sptr<IRemoteObject> &service);
102 
103     /**
104      * @brief Disconnects the connected object.
105      *
106      * @param want The Want object to disconnect to.
107      */
108     void DisconnectExtension(const Want &want);
109 
110     /**
111      * @brief Disconnects the connected object.
112      *
113      * @param want The Want object to disconnect to.
114      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
115      */
116     void DisconnectExtension(const Want &want, bool &isAsyncCallback);
117 
118     /**
119      * @brief The callback of disconnect.
120      */
121     void DisconnectExtensionCallback();
122 
123     /**
124      * @brief Command the Extension. and Calling information back to Extension.
125      *
126      * @param want The Want object to command to.
127      *
128      * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
129      * destroyed, and the value false indicates a normal startup.
130      *
131      * @param startId Indicates the number of times the Service Extension has been started. The startId is incremented
132      * by 1 every time the Extension is started. For example, if the Extension has been started for six times, the value
133      * of startId is 6.
134      */
135     void CommandExtension(const Want &want, bool restart, int startId);
136 
137     /**
138      * @brief Handle insight intent.
139      *
140      * @param want The Want object with insight intent to handle.
141      */
142     bool HandleInsightIntent(const Want &want);
143 
144     void CommandExtensionWindow(const Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo,
145         AAFwk::WindowCommand winCmd);
146 
147     /*
148      * SendResult, Send result to app when extension ability is terminated with result want.
149      *
150      * @param requestCode, the requestCode of the extension ability to start.
151      * @param resultCode, the resultCode of the extension ability to terminate.
152      * @param resultData, the want of the extension  ability to terminate.
153      */
154     void SendResult(int requestCode, int resultCode, const Want &resultData);
155 
156     /**
157      * @brief Save information about ability launch.
158      *
159      * @param launchParam Used to save information about ability launch param.
160      */
161     void SetLaunchParam(const AAFwk::LaunchParam &launchParam);
162 
163     void ScheduleAbilityRequestFailure(const std::string &requestId, const AppExecFwk::ElementName &element,
164         const std::string &message, int32_t resultCode = 0);
165 
166     void ScheduleAbilityRequestSuccess(const std::string &requestId, const AppExecFwk::ElementName &element);
167 
168 protected:
169     /**
170      * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application
171      * that it belongs to of the lifecycle status.
172      *
173      * @param want  The Want object to switch the life cycle.
174      * @param sessionInfo  Indicates the sessionInfo.
175      */
176     void Start(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo = nullptr);
177 
178     /**
179      * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INITIAL. And notifies the application
180      * that it belongs to of the lifecycle status.
181      *
182      */
183     void Stop();
184      /**
185      * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INITIAL. And notifies the application
186      * that it belongs to of the lifecycle status.
187      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
188      * @param want Indicates want.
189      * @param sessionInfo Indicates the sessionInfo, nullptr when not uiextension.
190      */
191     void Stop(bool &isAsyncCallback, const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
192     void AbilityTransactionCallback(const AAFwk::AbilityLifeCycleState &state);
193 
194     /**
195      * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application
196      * that it belongs to of the lifecycle status.
197      *
198      * @param want The Want object to switch the life cycle.
199      */
200     void Foreground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
201 
202     /**
203      * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_BACKGROUND. And notifies the
204      * application that it belongs to of the lifecycle status.
205      * @param want Indicates want.
206      * @param sessionInfo Indicates the sessionInfo, nullptr when not uiextension.
207      */
208     void Background(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
209 
210 private:
211     inline bool UIExtensionAbilityExecuteInsightIntent(const Want &want);
212 
213     bool skipCommandExtensionWithIntent_ = false;
214     int lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL;
215     sptr<IRemoteObject> token_;
216     std::shared_ptr<Extension> extension_;
217     AppExecFwk::ExtensionAbilityType extensionType_ = AppExecFwk::ExtensionAbilityType::UNSPECIFIED;
218 
219 class ExtensionWindowLifeCycleImpl : public Rosen::IWindowLifeCycle {
220 public:
ExtensionWindowLifeCycleImpl(const sptr<IRemoteObject> & token,const std::shared_ptr<ExtensionImpl> & owner)221     ExtensionWindowLifeCycleImpl(const sptr<IRemoteObject>& token, const std::shared_ptr<ExtensionImpl>& owner)
222         : token_(token), owner_(owner) {}
~ExtensionWindowLifeCycleImpl()223     virtual ~ExtensionWindowLifeCycleImpl() {}
224     void AfterForeground() override;
225     void AfterBackground() override;
226     void AfterActive() override;
227     void AfterInactive() override;
228 private:
229     sptr<IRemoteObject> token_ = nullptr;
230     std::weak_ptr<ExtensionImpl> owner_;
231 };
232 };
233 }
234 }
235 #endif  // OHOS_ABILITY_RUNTIME_EXTENSION_IMPL_H
236