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 #include <unordered_map> 23 24 #include "form_record.h" 25 #include "form_render_connection.h" 26 #include "form_render_interface.h" 27 #include "want.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 using Want = OHOS::AAFwk::Want; 32 using WantParams = OHOS::AAFwk::WantParams; 33 /** 34 * @class FormRenderService 35 * FormRenderService provides a facility for managing form render life cycle. 36 */ 37 class FormRenderMgr : public DelayedRefSingleton<FormRenderMgr> { 38 DECLARE_DELAYED_REF_SINGLETON(FormRenderMgr) 39 public: 40 DISALLOW_COPY_AND_MOVE(FormRenderMgr); 41 42 ErrCode RenderForm( 43 const FormRecord &formRecord, const WantParams &wantParams, const sptr<IRemoteObject> &hostToken = nullptr); 44 45 ErrCode UpdateRenderingForm(int64_t formId, const FormProviderData &formProviderData, 46 const WantParams &wantParams, bool mergeData); 47 48 void OnUnlock(); 49 50 ErrCode StopRenderingForm(int64_t formId, const FormRecord &formRecord, const std::string &compId = "", const sptr<IRemoteObject> &hostToken = nullptr); 51 52 ErrCode ReloadForm(const std::vector<FormRecord> &&formRecords, const std::string &bundleName, int32_t userId); 53 54 ErrCode RenderFormCallback(int64_t formId, const Want &want); 55 56 ErrCode StopRenderingFormCallback(int64_t formId, const Want &want); 57 58 void GetFormRenderState() const; 59 60 bool GetIsVerified() const; 61 62 ErrCode AddConnection(int64_t formId, sptr<FormRenderConnection> connection); 63 64 void RemoveConnection(int64_t formId); 65 66 void AddRenderDeathRecipient(const sptr<IRemoteObject> &renderRemoteObj); 67 68 bool IsNeedRender(int64_t formId); 69 70 void RerenderAllForms(); 71 72 void CleanFormHost(const sptr<IRemoteObject> &host); 73 74 void HandleConnectFailed(int64_t formId, int32_t errorCode) const; 75 76 bool IsRerenderForRenderServiceDied(int64_t formId); 77 78 void OnRenderingBlock(const std::string &bundleName); 79 80 ErrCode ReleaseRenderer(int64_t formId, const FormRecord &formRecord, const std::string &compId); 81 82 void SetFormRenderState(bool isVerified); 83 84 void AddAcquireProviderFormInfoTask(std::function<void()> task); 85 86 void ExecAcquireProviderTask(); 87 88 void PostOnUnlockTask(); 89 90 private: 91 ErrCode ConnectRenderService(const sptr<FormRenderConnection> &connection) const; 92 93 void DisconnectRenderService(const sptr<FormRenderConnection> connection, size_t size) const; 94 95 void AddHostToken(const sptr<IRemoteObject> &host, int64_t formId); 96 97 void RemoveHostToken(const sptr<IRemoteObject> &host); 98 99 void NotifyHostRenderServiceIsDead() const; 100 101 private: 102 class RemoteObjHash { 103 public: operator()104 size_t operator() (const sptr<IRemoteObject> &remoteObj) const 105 { 106 return reinterpret_cast<size_t>(remoteObj.GetRefPtr()); 107 } 108 }; 109 110 mutable std::mutex resourceMutex_; 111 mutable std::mutex isVerifiedMutex_; 112 std::mutex taskQueueMutex_; 113 std::queue<std::function<void()>> taskQueue_; 114 // <formId, connectionToRenderService> 115 std::unordered_map<int64_t, sptr<FormRenderConnection>> renderFormConnections_; 116 // <hostToken, formIds> 117 std::unordered_map<sptr<IRemoteObject>, std::unordered_set<int64_t>, RemoteObjHash> etsHosts_; 118 sptr<IFormRender> renderRemoteObj_ = nullptr; 119 sptr<IRemoteObject::DeathRecipient> renderDeathRecipient_ = nullptr; 120 std::atomic<int32_t> atomicRerenderCount_ = 0; 121 mutable bool isVerified_ = false; 122 }; 123 124 /** 125 * @class FormRenderRecipient 126 * FormRenderRecipient notices IRemoteBroker died. 127 */ 128 class FormRenderRecipient : public IRemoteObject::DeathRecipient { 129 public: 130 using RemoteDiedHandler = std::function<void()>; 131 132 explicit FormRenderRecipient(RemoteDiedHandler handler); 133 134 virtual ~FormRenderRecipient(); 135 136 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 137 138 private: 139 RemoteDiedHandler handler_; 140 }; 141 } // namespace AppExecFwk 142 } // namespace OHOS 143 #endif // OHOS_FORM_FWK_FORM_RENDER_MGR_H 144