• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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     void UpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config,
126         const sptr<IFormSupply> &formSupplyClient);
127 
128     void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
129 
130     void MarkThreadAlive();
131 
132     void ReleaseRenderer(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty);
133 
134     void Release();
135 
136     void FormRenderGC();
137 
138     int32_t RecycleForm(const int64_t &formId, std::string &statusData);
139 
140     int32_t RecoverForm(const FormJsInfo &formJsInfo, const std::string &statusData,
141         const bool &isRecoverFormToHandleClickEvent);
142 
143     size_t FormCount();
144 private:
145     class RemoteObjHash {
146     public:
operator()147         size_t operator() (const sptr<IRemoteObject> remoteObj) const
148         {
149             return reinterpret_cast<size_t>(remoteObj.GetRefPtr());
150         }
151     };
152     using IRemoteObjectSet = std::unordered_set<sptr<IRemoteObject>, RemoteObjHash>;
153 
154     bool CreateEventHandler(const std::string &bundleName, bool needMonitored = true);
155 
156     bool CreateRuntime(const FormJsInfo &formJsInfo);
157 
158     bool UpdateRuntime(const FormJsInfo &formJsInfo);
159 
160     bool SetPkgContextInfoMap(const FormJsInfo &formJsInfo, AbilityRuntime::Runtime::Options &options);
161 
162     std::shared_ptr<AbilityRuntime::Context> GetContext(const FormJsInfo &formJsInfo, const Want &want);
163 
164     std::shared_ptr<AbilityRuntime::Context> CreateContext(const FormJsInfo &formJsInfo, const Want &want);
165 
166     std::shared_ptr<Ace::FormRendererGroup> GetFormRendererGroup(const FormJsInfo &formJsInfo,
167     const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime);
168 
169     std::shared_ptr<Ace::FormRendererGroup> CreateFormRendererGroupLock(const FormJsInfo &formJsInfo,
170     const std::shared_ptr<AbilityRuntime::Context> &context, const std::shared_ptr<AbilityRuntime::Runtime> &runtime);
171 
172     void HandleUpdateInJsThread(const FormJsInfo &formJsInfo, const Want &want);
173 
174     bool HandleDeleteInJsThread(int64_t formId, const std::string &compId);
175 
176     void HandleDestroyInJsThread();
177 
178     bool HandleReleaseRendererInJsThread(int64_t formId, const std::string &compId, bool &isRenderGroupEmpty);
179 
180     void DeleteRendererGroup(int64_t formId);
181 
182     void HandleDeleteRendererGroup(int64_t formId);
183 
184     std::string GenerateContextKey(const FormJsInfo &formJsInfo);
185 
186     void ReleaseHapFileHandle();
187 
188     void HandleUpdateConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
189 
190     void AddWatchDogThreadMonitor();
191 
192     void OnRenderingBlock(const std::string &bundleName);
193 
194     void Timer();
195 
196     bool BeforeHandleUpdateForm(const FormJsInfo &formJsInfo);
197 
198     void HandleUpdateForm(const FormJsInfo &formJsInfo, const Want &want);
199 
200     void MergeFormData(Ace::FormRequest &formRequest, const FormJsInfo &formJsInfo);
201 
202     void AddRenderer(const FormJsInfo &formJsInfo, const Want &want);
203 
204     void UpdateRenderer(const FormJsInfo &formJsInfo);
205 
206     TaskState RunTask();
207 
208     void DumpEventHandler();
209 
210     void HandleReleaseInJsThread();
211 
212     bool CheckEventHandler(bool createThead = true, bool needMonitored = false);
213 
214     void AddFormRequest(const FormJsInfo &formJsInfo, const Want &want);
215 
216     void AddFormRequest(int64_t formId, const Ace::FormRequest &formRequest);
217 
218     void DeleteFormRequest(int64_t formId, const std::string &compId);
219 
220     void UpdateFormRequestReleaseState(
221         int64_t formId, const std::string &compId, bool hasRelease);
222 
223     void RecoverFormsByConfigUpdate(std::vector<int64_t> &formIds, const sptr<IFormSupply> &formSupplyClient);
224 
225     void ReAddAllRecycledForms(const sptr<IFormSupply> &formSupplyClient);
226 
227     void ReAddRecycledForms(const std::vector<FormJsInfo> &formJsInfos);
228 
229     int32_t HandleRecycleForm(const int64_t &formId, std::string &statusData);
230 
231     void HandleRecoverForm(const FormJsInfo &formJsInfo, const std::string &statusData,
232         const bool &isRecoverFormToHandleClickEvent);
233 
234     void HandleFormRenderGC();
235 
236     bool InitCompIds(const int64_t &formId,
237         std::vector<std::string> &orderedCompIds, std::string &currentCompId);
238 
239     bool RecoverFormRequestsInGroup(const FormJsInfo &formJsInfo, const std::string &statusData,
240         const bool &isHandleClickEvent, const std::unordered_map<std::string, Ace::FormRequest> &recordFormRequests);
241     bool RecoverRenderer(const std::vector<Ace::FormRequest> &groupRequests, const size_t &currentRequestIndex);
242 
243     bool ReAddIfHapPathChanged(const std::vector<FormJsInfo> &formJsInfos);
244 
245     void UpdateAllFormRequest(const std::vector<FormJsInfo> &formJsInfos, bool hasRelease);
246 
247     void HandleReleaseAllRendererInJsThread();
248 
249     pid_t jsThreadId_ = 0;
250     pid_t processId_ = 0;
251 
252     std::string bundleName_;
253     std::string uid_;
254     std::shared_ptr<EventRunner> eventRunner_;
255     std::shared_ptr<EventHandler> eventHandler_;
256     std::mutex eventHandlerMutex_;
257     std::shared_ptr<AbilityRuntime::Runtime> runtime_;
258 
259     // <formId, hostRemoteObj>
260     std::mutex hostsMapMutex_;
261     std::unordered_map<int64_t, IRemoteObjectSet> hostsMapForFormId_;
262     // <moduleName, Context>
263     std::mutex contextsMapMutex_;
264     std::unordered_map<std::string, std::shared_ptr<AbilityRuntime::Context>> contextsMapForModuleName_;
265     // <formId, formRendererGroup>
266     std::mutex formRendererGroupMutex_;
267     std::unordered_map<int64_t, std::shared_ptr<Ace::FormRendererGroup>> formRendererGroupMap_;
268     // <formId, <compId, formRequest>>
269     std::mutex formRequestsMutex_;
270     std::unordered_map<int64_t, std::unordered_map<std::string, Ace::FormRequest>> formRequests_;
271     std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_;
272     // <formId, <orderedCompIds, currentCompId>>
273     std::mutex recycledFormCompIdsMutex_;
274     std::unordered_map<int64_t, std::pair<std::vector<std::string>, std::string>> recycledFormCompIds_;
275 
276     std::string hapPath_;
277     std::mutex watchDogMutex_;
278     bool threadIsAlive_ = true;
279     std::atomic_bool hasMonitor_ = false;
280     std::shared_ptr<ThreadState> threadState_;
281     std::mutex formSupplyMutex_;
282     sptr<IFormSupply> formSupplyClient_;
283 };
284 }  // namespace FormRender
285 }  // namespace AppExecFwk
286 }  // namespace OHOS
287 #endif  // OHOS_FORM_FWK_FORM_RENDER_RECORD_H
288