• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_FORM_FWK_FORM_RENDER_RECORD_H
17 #define OHOS_FORM_FWK_FORM_RENDER_RECORD_H
18 
19 #include <memory>
20 #include <unordered_map>
21 #include <unordered_set>
22 
23 #include "configuration.h"
24 #include "context_impl.h"
25 #include "event_handler.h"
26 #include "form_js_info.h"
27 #include "form_mgr_errors.h"
28 #include "form_supply_proxy.h"
29 #include "form_renderer_group.h"
30 #include "js_runtime.h"
31 #include "want.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 namespace FormRender {
36 using Want = AAFwk::Want;
37 enum class TaskState {
38     NO_RUNNING = 0,
39     RUNNING = 0,
40     BLOCK,
41 };
42 
43 class ThreadState {
44 public:
45     explicit ThreadState(int32_t maxState);
46     void ResetState();
47     void NextState();
48     int32_t GetCurrentState();
49     bool IsMaxState();
50 
51 private:
52     int32_t state_ = 0;
53     int32_t maxState_;
54 };
55 
56 class HandlerDumper : public AppExecFwk::Dumper {
57 public:
58     void Dump(const std::string &message) override;
59     std::string GetTag() override;
60     std::string GetDumpInfo();
61 private:
62     std::string dumpInfo_;
63 };
64 
65 class FormRenderRecord : public std::enable_shared_from_this<FormRenderRecord> {
66 public:
67     /**
68      * @brief Create a FormRenderRecord.
69      * @param bundleName The bundleName of form bundle.
70      * @param uid The uid of form bundle.(userId + bundleName)
71      * @return Returns FormRenderRecord instance.
72      */
73     static std::shared_ptr<FormRenderRecord> Create(const std::string &bundleName, const std::string &uid,
74         bool needMonitored = true, sptr<IFormSupply> formSupplyClient = nullptr);
75 
76     FormRenderRecord(const std::string &bundleName, const std::string &uid, sptr<IFormSupply> formSupplyClient);
77 
78     ~FormRenderRecord();
79 
80     /**
81      * @brief When the host exits, clean up related resources.
82      * @param hostRemoteObj host token.
83      * @return Returns TRUE: FormRenderRecord is empty, FALSE: FormRenderRecord is not empty.
84      */
85     bool HandleHostDied(const sptr<IRemoteObject> hostRemoteObj);
86 
87     /**
88      * @brief When add a new form, the corresponding FormRenderRecord needs to be updated.
89      * @param formJsInfo formJsInfo.
90      * @param want want.
91      * @param hostRemoteObj host token.
92      * @return Returns ERR_OK on success, others on failure.
93      */
94     int32_t UpdateRenderRecord(const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> hostRemoteObj);
95 
96     /**
97      * @brief When all forms of an bundle are deleted, the corresponding FormRenderRecord-record needs to be removed
98      * @param formId formId.
99      * @param hostRemoteObj host token.
100      * @return Returns ERR_OK on success, others on failure.
101      */
102     void DeleteRenderRecord(int64_t formId, const std::string &compId,  const sptr<IRemoteObject> hostRemoteObj,
103         bool &isRenderGroupEmpty);
104 
105     int32_t HandleOnUnlock();
106 
107     int32_t OnUnlock();
108 
109     int32_t SetVisibleChange(const int64_t &formId, bool isVisible);
110 
111     int32_t HandleSetVisibleChange(const int64_t &formId, bool isVisible);
112 
113     int32_t ReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want);
114 
115     int32_t HandleReloadFormRecord(const std::vector<FormJsInfo> &&formJsInfos, const Want &want);
116 
117     /**
118      * @brief Get the uid of bundle.
119      * @return Returns the uid.
120      */
121     std::string GetUid() const;
122 
123     bool IsEmpty();
124 
125     bool HasRenderFormTask();
126 
127     void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config,
128         const sptr<IFormSupply> &formSupplyClient);
129 
130     void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
131 
132     void MarkThreadAlive();
133 
134     void ReleaseRenderer(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty);
135 
136     void Release();
137 
138     void FormRenderGC();
139 
140     int32_t RecycleForm(const int64_t &formId, std::string &statusData);
141 
142     int32_t RecoverForm(const FormJsInfo &formJsInfo, const std::string &statusData,
143         const bool &isRecoverFormToHandleClickEvent);
144 
145     size_t FormCount();
146 
147     void UpdateFormSizeOfGroups(const int64_t &formId, float width, float height, float borderWidth);
148 private:
149     class RemoteObjHash {
150     public:
operator()151         size_t operator() (const sptr<IRemoteObject> remoteObj) const
152         {
153             return reinterpret_cast<size_t>(remoteObj.GetRefPtr());
154         }
155     };
156     using IRemoteObjectSet = std::unordered_set<sptr<IRemoteObject>, RemoteObjHash>;
157 
158     bool CreateEventHandler(const std::string &bundleName, bool needMonitored = true);
159 
160     bool CreateRuntime(const FormJsInfo &formJsInfo);
161 
162     bool UpdateRuntime(const FormJsInfo &formJsInfo);
163 
164     bool SetPkgContextInfoMap(const FormJsInfo &formJsInfo, AbilityRuntime::Runtime::Options &options);
165 
166     std::shared_ptr<AbilityRuntime::Context> GetContext(const FormJsInfo &formJsInfo, const Want &want);
167 
168     std::shared_ptr<AbilityRuntime::Context> CreateContext(const FormJsInfo &formJsInfo, const Want &want);
169 
170     std::shared_ptr<Ace::FormRendererGroup> GetFormRendererGroup(const FormJsInfo &formJsInfo,
171     const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime);
172 
173     std::shared_ptr<Ace::FormRendererGroup> CreateFormRendererGroupLock(const FormJsInfo &formJsInfo,
174     const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime);
175 
176     void HandleUpdateInJsThread(const FormJsInfo &formJsInfo, const Want &want);
177 
178     bool HandleDeleteInJsThread(int64_t formId, const std::string &compId);
179 
180     void HandleDestroyInJsThread();
181 
182     bool HandleReleaseRendererInJsThread(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty);
183 
184     void DeleteRendererGroup(int64_t formId);
185 
186     void HandleDeleteRendererGroup(int64_t formId);
187 
188     std::string GenerateContextKey(const FormJsInfo &formJsInfo);
189 
190     void ReleaseHapFileHandle();
191 
192     void HandleUpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
193 
194     void AddWatchDogThreadMonitor();
195 
196     void OnRenderingBlock(const std::string &bundleName);
197 
198     void OnNotifyRefreshForm(const int64_t &formId);
199 
200     void Timer();
201 
202     bool BeforeHandleUpdateForm(const FormJsInfo &formJsInfo);
203 
204     void HandleUpdateForm(const FormJsInfo &formJsInfo, const Want &want);
205 
206     void MergeFormData(Ace::FormRequest &formRequest, const FormJsInfo &formJsInfo);
207 
208     void AddRenderer(const FormJsInfo &formJsInfo, const Want &want);
209 
210     void UpdateRenderer(const FormJsInfo &formJsInfo);
211 
212     TaskState RunTask();
213 
214     void DumpEventHandler();
215 
216     void HandleReleaseInJsThread();
217 
218     bool CheckEventHandler(bool createThead = true, bool needMonitored = false);
219 
220     void AddFormRequest(const FormJsInfo &formJsInfo, const Want &want);
221 
222     void AddFormRequest(int64_t formId, Ace::FormRequest &formRequest);
223 
224     /**
225      * @brief Add formRequest to formRequests_.
226      * @param formId formId.
227      * @param formRequest formRequest.
228      * @param noNeedUpdateSize If form size is modified in the
229      * formRequest parameter, this parameter should be set to false.
230      * Set this parameter to true if you want to keep the form size data in the formRequests_.
231      */
232     void AddFormRequest(int64_t formId, Ace::FormRequest &formRequest, bool noNeedUpdateSize);
233 
234     void DeleteFormRequest(int64_t formId, const std::string &compId);
235 
236     void UpdateFormRequestReleaseState(
237         int64_t formId, const std::string &compId, bool hasRelease);
238 
239     void RecoverFormsByConfigUpdate(std::vector<int64_t> &formIds, const sptr<IFormSupply> &formSupplyClient);
240 
241     void ReAddAllRecycledForms(const sptr<IFormSupply> &formSupplyClient);
242 
243     void ReAddRecycledForms(const std::vector<FormJsInfo> &formJsInfos);
244 
245     int32_t HandleRecycleForm(const int64_t &formId, std::string &statusData);
246 
247     void HandleRecoverForm(const FormJsInfo &formJsInfo, const std::string &statusData,
248         const bool &isRecoverFormToHandleClickEvent);
249 
250     void HandleFormRenderGC();
251 
252     bool InitCompIds(const int64_t &formId,
253         std::vector<std::string> &orderedCompIds, std::string &currentCompId);
254 
255     bool RecoverFormRequestsInGroup(const FormJsInfo &formJsInfo, const std::string &statusData,
256         const bool &isHandleClickEvent, std::unordered_map<std::string, Ace::FormRequest> &recordFormRequests);
257     bool RecoverRenderer(const std::vector<Ace::FormRequest> &groupRequests, const size_t &currentRequestIndex);
258 
259     bool ReAddIfHapPathChanged(const std::vector<FormJsInfo> &formJsInfos);
260 
261     void UpdateAllFormRequest(const std::vector<FormJsInfo> &formJsInfos, bool hasRelease);
262 
263     void HandleReleaseAllRendererInJsThread();
264 
265     void UpdateGroupRequestsWhenRecover(const int64_t &formId, const FormJsInfo &formJsInfo,
266         const std::vector<std::string> &orderedCompIds, const std::string &currentCompId,
267         const std::string &statusData, const bool &isHandleClickEvent, size_t &currentRequestIndex,
268         std::vector<Ace::FormRequest> &groupRequests, bool &currentRequestFound,
269         const std::unordered_map<std::string, Ace::FormRequest> &recordFormRequests);
270 
271     void MergeMap(std::map<std::string, sptr<FormAshmem>> &dst,
272         const std::map<std::string, sptr<FormAshmem>> &src);
273 
274     void MarkRenderFormTaskDone(int32_t renderType);
275 
276     pid_t jsThreadId_ = 0;
277     pid_t processId_ = 0;
278 
279     std::string bundleName_;
280     std::string uid_;
281     std::shared_ptr<EventRunner> eventRunner_;
282     std::shared_ptr<EventHandler> eventHandler_;
283     bool eventHandleNeedReset = false;
284     std::shared_mutex eventHandlerReset_;
285     std::recursive_mutex eventHandlerMutex_;
286     std::shared_ptr<AbilityRuntime::Runtime> runtime_;
287 
288     // <formId, hostRemoteObj>
289     std::mutex hostsMapMutex_;
290     std::unordered_map<int64_t, IRemoteObjectSet> hostsMapForFormId_;
291     // <moduleName, Context>
292     std::mutex contextsMapMutex_;
293     std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::Context>> contextsMapForModuleName_;
294     // <formId, formRendererGroup>
295     std::mutex formRendererGroupMutex_;
296     std::unordered_map<int64_t, std::shared_ptr<Ace::FormRendererGroup>> formRendererGroupMap_;
297     // <formId, <compId, formRequest>>
298     std::mutex formRequestsMutex_;
299     std::unordered_map<int64_t, std::unordered_map<std::string, Ace::FormRequest>> formRequests_;
300     std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_;
301     // <formId, <orderedCompIds, currentCompId>>
302     std::mutex recycledFormCompIdsMutex_;
303     std::unordered_map<int64_t, std::pair<std::vector<std::string>, std::string>> recycledFormCompIds_;
304 
305     std::string hapPath_;
306     std::mutex watchDogMutex_;
307     bool threadIsAlive_ = true;
308     std::atomic_bool hasMonitor_ = false;
309     std::shared_ptr<ThreadState> threadState_;
310     std::mutex formSupplyMutex_;
311     sptr<IFormSupply> formSupplyClient_;
312     std::atomic<int> renderFormTasksNum = 0;
313 };
314 }  // namespace FormRender
315 }  // namespace AppExecFwk
316 }  // namespace OHOS
317 #endif  // OHOS_FORM_FWK_FORM_RENDER_RECORD_H
318