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_IMPL_H
17 #define OHOS_FORM_FWK_FORM_RENDER_IMPL_H
18 
19 #include "form_render_stub.h"
20 
21 #include <memory>
22 #include <singleton.h>
23 
24 #include "bundle_mgr_interface.h"
25 #include "context_impl.h"
26 #include "event_handler.h"
27 #include "form_render_record.h"
28 #include "form_render_serial_queue.h"
29 #include "form_supply_proxy.h"
30 #include "js_runtime.h"
31 #include "runtime.h"
32 #include "want.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 namespace FormRender {
37 using OHOS::AAFwk::Want;
38 using namespace AbilityRuntime;
39 
40 class FormRenderImpl : public FormRenderStub,
41                        public std::enable_shared_from_this<FormRenderImpl> {
42     DECLARE_DELAYED_SINGLETON(FormRenderImpl)
43 public:
44     /**
45      * @brief Render form. This is sync API.
46      * @param formJsInfo The form js info.
47      * @param want Indicates the {@link Want} structure containing form info.
48      * @param callerToken Caller ability token.
49      * @return Returns ERR_OK on success, others on failure.
50      */
51     int32_t RenderForm(const FormJsInfo &formJsInfo, const Want &want,
52         sptr<IRemoteObject> callerToken) override;
53 
54     /**
55      * @brief Stop rendering form. This is sync API.
56      * @param formJsInfo The form js info.
57      * @param want Indicates the {@link Want} structure containing form info.
58      * @param callerToken Caller ability token.
59      * @return Returns ERR_OK on success, others on failure.
60      */
61     int32_t StopRenderingForm(
62         const FormJsInfo &formJsInfo, const Want &want, const sptr<IRemoteObject> &callerToken) override;
63 
64     /**
65      * @brief When host is died, clean resources. This is async API.
66      * @param hostToken Caller ability token.
67      * @return Returns ERR_OK on success, others on failure.
68      */
69     int32_t CleanFormHost(const sptr<IRemoteObject> &hostToken) override;
70 
71     /**
72      * @brief Reload form When app updated. This is sync API.
73      * @param formIds the form id need to update.
74      * @param want Indicates the {@link Want} structure containing form info.
75      * @return int32_t Returns ERR_OK on success, others on failure.
76      */
77     int32_t ReloadForm(const std::vector<FormJsInfo> &&formJsInfos, const Want &want) override;
78 
79     /**
80      * @brief Called when the system configuration is updated.
81      * @param configuration Indicates the updated configuration information.
82      */
83     void OnConfigurationUpdated(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& configuration);
84 
85     void SetConfiguration(const std::shared_ptr<OHOS::AppExecFwk::Configuration>& config);
86 
87     void RunCachedConfigurationUpdated() override;
88 
89     int32_t ReleaseRenderer(
90         int64_t formId, const std::string &compId, const std::string &uid) override;
91 
92     int32_t OnUnlock() override;
93 
94     int32_t SetVisibleChange(const int64_t &formId, bool isVisible, const Want &want) override;
95 
96     int32_t RecycleForm(const int64_t &formId, const Want &want) override;
97 
98     int32_t RecoverForm(const FormJsInfo &formJsInfo, const Want &want) override;
99 
100     int32_t UpdateFormSize(
101         const int64_t &formId, float width, float height, float borderWidth, const std::string &uid) override;
102 private:
103     void FormRenderGCTask(const std::string &uid);
104     void FormRenderGC(const std::string &uid);
105     void OnConfigurationUpdatedInner();
106     void ConfirmUnlockState(Want &renderWant);
107 
108     std::mutex renderRecordMutex_;
109     // <uid(userId + bundleName), renderRecord>
110     std::unordered_map<std::string, std::shared_ptr<FormRenderRecord>> renderRecordMap_;
111     std::shared_ptr<OHOS::AppExecFwk::Configuration> configuration_;
112     std::chrono::steady_clock::time_point configUpdateTime_ = std::chrono::steady_clock::now();
113     std::shared_ptr<FormRenderSerialQueue> serialQueue_ = nullptr;
114     std::mutex formSupplyMutex_;
115     sptr<IFormSupply> formSupplyClient_;
116     bool isVerified_ = false;
117     bool hasCachedConfig_ = false;
118 };
119 } // namespace FormRender
120 } // namespace AppExecFwk
121 } // namespace OHOS
122 #endif // OHOS_FORM_FWK_FORM_RENDER_IMPL_H
123