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_MGR_H
17 #define OHOS_FORM_FWK_FORM_RENDER_MGR_H
18 
19 #include <atomic>
20 #include <queue>
21 #include <singleton.h>
22 
23 #include "form_record.h"
24 #include "form_render_connection.h"
25 #include "form_render_interface.h"
26 #include "form_render_mgr_inner.h"
27 #include "form_sandbox_render_mgr_inner.h"
28 #include "want.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 using Want = OHOS::AAFwk::Want;
33 using WantParams = OHOS::AAFwk::WantParams;
34 /**
35  * @class FormRenderService
36  * FormRenderService provides a facility for managing form render life cycle.
37  */
38 class FormRenderMgr : public DelayedRefSingleton<FormRenderMgr> {
39 DECLARE_DELAYED_REF_SINGLETON(FormRenderMgr)
40 public:
41     DISALLOW_COPY_AND_MOVE(FormRenderMgr);
42 
43     ErrCode RenderForm(
44         const FormRecord &formRecord, const WantParams &wantParams, const sptr<IRemoteObject> &hostToken = nullptr);
45 
46     ErrCode UpdateRenderingForm(int64_t formId, const FormProviderData &formProviderData,
47         const WantParams &wantParams, bool mergeData);
48 
49     void OnScreenUnlock();
50 
51     void OnUnlock();
52 
53     void NotifyScreenOn();
54 
55     void SetVisibleChange(int64_t formId, bool isVisible);
56 
57     ErrCode StopRenderingForm(int64_t formId, const FormRecord &formRecord,
58         const std::string &compId = "", const sptr<IRemoteObject> &hostToken = nullptr);
59 
60     ErrCode ReloadForm(const std::vector<FormRecord> &&formRecords, const std::string &bundleName, int32_t userId);
61 
62     ErrCode RenderFormCallback(int64_t formId, const Want &want);
63 
64     ErrCode StopRenderingFormCallback(int64_t formId, const Want &want);
65 
66     void GetFormRenderState();
67 
68     bool GetIsVerified() const;
69 
70     ErrCode AddConnection(int64_t formId, sptr<FormRenderConnection> connection, const FormRecord &formRecord);
71 
72     void RemoveConnection(int64_t formId, const FormRecord &formRecord);
73 
74     ErrCode checkConnectionsFormIds(std::vector<int64_t> formIds, int32_t userId, std::vector<int64_t> &needconFormIds);
75 
76     void reAddConnections(std::vector<int64_t> formIds, int32_t userId, const sptr<IRemoteObject> &remoteObject);
77 
78     void AddRenderDeathRecipient(const sptr<IRemoteObject> &renderRemoteObj, const FormRecord &formRecord);
79 
80     bool IsNeedRender(int64_t formId);
81 
82     void CleanFormHost(const sptr<IRemoteObject> &host, const int hostCallingUid);
83 
84     void HandleConnectFailed(int64_t formId, int32_t errorCode) const;
85 
86     bool IsRerenderForRenderServiceDied(int64_t formId);
87 
88     void OnRenderingBlock(const std::string &bundleName);
89 
90     ErrCode ReleaseRenderer(int64_t formId, const FormRecord &formRecord, const std::string &compId);
91 
92     void AddAcquireProviderFormInfoTask(std::function<void()> task);
93 
94     void ExecAcquireProviderTask();
95 
96     void AddAcquireProviderForbiddenTask(const std::string &bundleName, int64_t formId, std::function<void()> task);
97 
98     void ExecAcquireProviderForbiddenTask(const std::string &bundleName);
99 
100     void DeleteAcquireForbiddenTasksByBundleName(const std::string &bundleName);
101 
102     void DeleteAcquireForbiddenTaskByFormId(int64_t formId);
103 
104     void PostOnUnlockTask();
105 
106     ErrCode RecycleForms(const std::vector<int64_t> &formIds, const Want &want,
107         const sptr<IRemoteObject> &remoteObjectOfHost);
108 
109     ErrCode RecoverForms(const std::vector<int64_t> &formIds, const WantParams &wantParams);
110 
111     void UpdateFormSize(const int64_t &formId, float width, float height, float borderWidth);
112 
113     void DisconnectAllRenderConnections(int32_t userId);
114 
115     void RerenderAllFormsImmediate(int32_t userId);
116 
117 private:
118     void InitRenderInner(bool isSandbox, int32_t userId);
119 
120 private:
121     mutable std::mutex isVerifiedMutex_;
122     std::mutex renderInnerMutex_;
123     std::mutex taskQueueMutex_;
124     std::queue<std::function<void()>> taskQueue_;
125     std::mutex forbiddenTaskMapMutex_;
126     std::unordered_map<std::string, std::unordered_map<int64_t, std::function<void()>>> forbiddenTaskMap_;
127     // <userId, FormRenderMgrInner>
128     std::unordered_map<int32_t, std::shared_ptr<FormRenderMgrInner>> renderInners_;
129     // <userId, FormSandboxRenderMgrInner>
130     std::unordered_map<int32_t, std::shared_ptr<FormSandboxRenderMgrInner>> sandboxInners_;
131     bool isScreenUnlocked_ = false;
132     bool isVerified_ = false;
133 };
134 } // namespace AppExecFwk
135 } // namespace OHOS
136 #endif // OHOS_FORM_FWK_FORM_RENDER_MGR_H
137