• 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 #include "form_render_mgr.h"
17 
18 #include <mutex>
19 
20 #include "fms_log_wrapper.h"
21 #include "form_ams_helper.h"
22 #include "form_bms_helper.h"
23 #include "form_cache_mgr.h"
24 #include "form_constants.h"
25 #include "form_data_mgr.h"
26 #include "form_event_report.h"
27 #include "form_host_interface.h"
28 #include "form_mgr_errors.h"
29 #include "form_sandbox_render_mgr_inner.h"
30 #include "form_supply_callback.h"
31 #include "form_task_mgr.h"
32 #include "form_trust_mgr.h"
33 #include "form_util.h"
34 #include "ipc_skeleton.h"
35 #include "os_account_manager.h"
36 #include "want.h"
37 
38 namespace OHOS {
39 namespace AppExecFwk {
40 namespace {
41     constexpr size_t LAST_CONNECTION = 1;
42 }
43 using Want = OHOS::AAFwk::Want;
FormRenderMgr()44 FormRenderMgr::FormRenderMgr()
45 {
46 }
~FormRenderMgr()47 FormRenderMgr::~FormRenderMgr()
48 {
49 }
50 
GetFormRenderState() const51 void FormRenderMgr::GetFormRenderState() const
52 {
53     HILOG_INFO("RenderForm for the first time");
54     // Check whether the account is authenticated.
55     bool isVerified = false;
56     AccountSA::OsAccountManager::IsOsAccountVerified(FormUtil::GetCurrentAccountId(), isVerified);
57     HILOG_INFO("isVerified: %{public}d", isVerified);
58     std::lock_guard<std::mutex> lock(isVerifiedMutex_);
59     isVerified_ = isVerified;
60 }
61 
GetIsVerified() const62 bool FormRenderMgr::GetIsVerified() const
63 {
64     HILOG_DEBUG("GetIsVerified.");
65     std::lock_guard<std::mutex> lock(isVerifiedMutex_);
66     return isVerified_;
67 }
68 
RenderForm(const FormRecord & formRecord,const WantParams & wantParams,const sptr<IRemoteObject> & hostToken)69 ErrCode FormRenderMgr::RenderForm(
70     const FormRecord &formRecord, const WantParams &wantParams, const sptr<IRemoteObject> &hostToken)
71 {
72     HILOG_INFO("RenderForm formId: %{public}" PRId64 "", formRecord.formId);
73     std::once_flag flag;
74     std::function<void()> func = std::bind(&FormRenderMgr::GetFormRenderState, this);
75     std::call_once(flag, func);
76     HILOG_INFO("The authentication status of the current user is : %{public}d", isVerified_);
77     if (formRecord.uiSyntax != FormType::ETS) {
78         return ERR_OK;
79     }
80     if (formRecord.formId <= 0) {
81         HILOG_ERROR("%{public}s fail, formId should be greater than 0.", __func__);
82         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
83     }
84 
85     int32_t userId = FormUtil::GetCurrentAccountId();
86     Want want;
87     want.SetParams(wantParams);
88     want.SetParam(Constants::FORM_SUPPLY_UID, std::to_string(userId) + formRecord.bundleName);
89     {
90         std::lock_guard<std::mutex> lock(isVerifiedMutex_);
91         want.SetParam(Constants::FORM_RENDER_STATE, isVerified_);
92     }
93     if (formRecord.privacyLevel > 0) {
94         InitRenderInner(true);
95         return sandboxInner_->RenderForm(formRecord, want, hostToken);
96     } else {
97         InitRenderInner(false);
98         return renderInner_->RenderForm(formRecord, want, hostToken);
99     }
100 }
101 
UpdateRenderingForm(int64_t formId,const FormProviderData & formProviderData,const WantParams & wantParams,bool mergeData)102 ErrCode FormRenderMgr::UpdateRenderingForm(int64_t formId, const FormProviderData &formProviderData,
103     const WantParams &wantParams, bool mergeData)
104 {
105     HILOG_ERROR("UpdateRenderingForm with formId: %{public}" PRId64 "", formId);
106     FormRecord formRecord;
107     bool isGetFormRecord = FormDataMgr::GetInstance().GetFormRecord(formId, formRecord);
108     if (!isGetFormRecord) {
109         HILOG_ERROR("%{public}s fail, not exist such form, formId:%{public}" PRId64 "", __func__, formId);
110         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
111     }
112     if (formRecord.privacyLevel > 0) {
113         if (sandboxInner_ == nullptr) {
114             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
115         }
116         return sandboxInner_->UpdateRenderingForm(formRecord, formProviderData, wantParams, mergeData);
117     } else {
118         if (renderInner_ == nullptr) {
119             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
120         }
121         return renderInner_->UpdateRenderingForm(formRecord, formProviderData, wantParams, mergeData);
122     }
123 }
124 
ReloadForm(const std::vector<FormRecord> && formRecords,const std::string & bundleName,int32_t userId)125 ErrCode FormRenderMgr::ReloadForm(
126     const std::vector<FormRecord> &&formRecords, const std::string &bundleName, int32_t userId)
127 {
128     HILOG_DEBUG("called");
129     std::vector<FormRecord> sandboxRecords;
130     std::vector<FormRecord> normalRecords;
131     for (const auto &record : formRecords) {
132         if (record.privacyLevel > 0) {
133             sandboxRecords.emplace_back(record);
134         } else {
135             normalRecords.emplace_back(record);
136         }
137     }
138     if (!normalRecords.empty() && renderInner_ != nullptr) {
139         renderInner_->ReloadForm(std::move(normalRecords), bundleName, userId);
140     }
141     if (!sandboxRecords.empty() && sandboxInner_ != nullptr) {
142         sandboxInner_->ReloadForm(std::move(sandboxRecords), bundleName, userId);
143     }
144     return ERR_OK;
145 }
146 
SetFormRenderState(bool isVerified)147 void FormRenderMgr::SetFormRenderState(bool isVerified)
148 {
149     HILOG_DEBUG("start to set form render state.");
150     std::lock_guard<std::mutex> lock(isVerifiedMutex_);
151     isVerified_ = isVerified;
152 }
153 
PostOnUnlockTask()154 void FormRenderMgr::PostOnUnlockTask()
155 {
156     if (renderInner_ != nullptr) {
157         renderInner_->PostOnUnlockTask();
158     }
159     if (sandboxInner_ != nullptr) {
160         sandboxInner_->PostOnUnlockTask();
161     }
162 }
163 
AddAcquireProviderFormInfoTask(std::function<void ()> task)164 void FormRenderMgr::AddAcquireProviderFormInfoTask(std::function<void()> task)
165 {
166     HILOG_DEBUG("called");
167     std::lock_guard<std::mutex> lock(taskQueueMutex_);
168     taskQueue_.push(task);
169 }
170 
ExecAcquireProviderTask()171 void FormRenderMgr::ExecAcquireProviderTask()
172 {
173     HILOG_INFO("start to execute asynchronous tasks in the queue.");
174     std::lock_guard<std::mutex> lock(taskQueueMutex_);
175     while (!taskQueue_.empty()) {
176         auto task = taskQueue_.front();
177         task();
178         taskQueue_.pop();
179     }
180 }
181 
OnUnlock()182 void FormRenderMgr::OnUnlock()
183 {
184     HILOG_DEBUG("called. The authentication status of the current user is true.");
185     SetFormRenderState(true);
186     PostOnUnlockTask();
187     ExecAcquireProviderTask();
188 }
189 
StopRenderingForm(int64_t formId,const FormRecord & formRecord,const std::string & compId,const sptr<IRemoteObject> & hostToken)190 ErrCode FormRenderMgr::StopRenderingForm(
191     int64_t formId, const FormRecord &formRecord, const std::string &compId, const sptr<IRemoteObject> &hostToken)
192 {
193     HILOG_DEBUG("%{public}s called.", __func__);
194     if (formRecord.privacyLevel > 0) {
195         if (sandboxInner_ == nullptr) {
196             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
197         }
198         return sandboxInner_->StopRenderingForm(formId, formRecord, compId, hostToken);
199     } else {
200         if (renderInner_ == nullptr) {
201             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
202         }
203         return renderInner_->StopRenderingForm(formId, formRecord, compId, hostToken);
204     }
205 }
206 
RenderFormCallback(int64_t formId,const Want & want)207 ErrCode FormRenderMgr::RenderFormCallback(int64_t formId, const Want &want)
208 {
209     HILOG_DEBUG("%{public}s called.", __func__);
210     return ERR_OK;
211 }
212 
StopRenderingFormCallback(int64_t formId,const Want & want)213 ErrCode FormRenderMgr::StopRenderingFormCallback(int64_t formId, const Want &want)
214 {
215     HILOG_INFO("%{public}s called.", __func__);
216     if (renderInner_ != nullptr) {
217         renderInner_->StopRenderingFormCallback(formId, want);
218     }
219     if (sandboxInner_ != nullptr) {
220         sandboxInner_->StopRenderingFormCallback(formId, want);
221     }
222     return ERR_OK;
223 }
224 
ReleaseRenderer(int64_t formId,const FormRecord & formRecord,const std::string & compId)225 ErrCode FormRenderMgr::ReleaseRenderer(int64_t formId, const FormRecord &formRecord, const std::string &compId)
226 {
227     HILOG_DEBUG("%{public}s called.", __func__);
228     if (formRecord.privacyLevel > 0) {
229         if (sandboxInner_ == nullptr) {
230             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
231         }
232         return sandboxInner_->ReleaseRenderer(formId, formRecord, compId);
233     } else {
234         if (renderInner_ == nullptr) {
235             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
236         }
237         return renderInner_->ReleaseRenderer(formId, formRecord, compId);
238     }
239 }
240 
AddConnection(int64_t formId,sptr<FormRenderConnection> connection,int32_t privacyLevel)241 ErrCode FormRenderMgr::AddConnection(
242     int64_t formId, sptr<FormRenderConnection> connection, int32_t privacyLevel)
243 {
244     HILOG_INFO("%{public}s called.", __func__);
245     if (privacyLevel > 0) {
246         if (sandboxInner_ == nullptr) {
247             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
248         }
249         return sandboxInner_->AddConnection(formId, connection);
250     } else {
251         if (renderInner_ == nullptr) {
252             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
253         }
254         return renderInner_->AddConnection(formId, connection);
255     }
256 }
257 
RemoveConnection(int64_t formId,int32_t privacyLevel)258 void FormRenderMgr::RemoveConnection(int64_t formId, int32_t privacyLevel)
259 {
260     HILOG_INFO("Call.");
261     if (privacyLevel > 0) {
262         if (sandboxInner_ == nullptr) {
263             return;
264         }
265         sandboxInner_->RemoveConnection(formId);
266     } else {
267         if (renderInner_ == nullptr) {
268             return;
269         }
270         renderInner_->RemoveConnection(formId);
271     }
272 }
273 
CleanFormHost(const sptr<IRemoteObject> & host)274 void FormRenderMgr::CleanFormHost(const sptr<IRemoteObject> &host)
275 {
276     if (renderInner_ != nullptr) {
277         renderInner_->CleanFormHost(host);
278     }
279     if (sandboxInner_ != nullptr) {
280         sandboxInner_->CleanFormHost(host);
281     }
282 }
283 
AddRenderDeathRecipient(const sptr<IRemoteObject> & remoteObject,int32_t privacyLevel)284 void FormRenderMgr::AddRenderDeathRecipient(const sptr<IRemoteObject> &remoteObject, int32_t privacyLevel)
285 {
286     if (privacyLevel > 0) {
287         if (sandboxInner_ == nullptr) {
288             return;
289         }
290         sandboxInner_->AddRenderDeathRecipient(remoteObject);
291     } else {
292         if (renderInner_ == nullptr) {
293             return;
294         }
295         renderInner_->AddRenderDeathRecipient(remoteObject);
296     }
297 }
298 
OnRenderingBlock(const std::string & bundleName)299 void FormRenderMgr::OnRenderingBlock(const std::string &bundleName)
300 {
301     HILOG_INFO("OnRenderingBlock called, bundleName: %{public}s.", bundleName.c_str());
302     FormEventInfo eventInfo;
303     eventInfo.bundleName = bundleName;
304     FormEventReport::SendSecondFormEvent(
305         FormEventName::FORM_RENDER_BLOCK, HiSysEventType::FAULT, eventInfo);
306 
307     FormTrustMgr::GetInstance().MarkTrustFlag(bundleName, false);
308 
309     Want want;
310     want.SetElementName("com.ohos.formrenderservice", "ServiceExtension");
311     want.AddFlags(Want::FLAG_ABILITY_FORM_ENABLED);
312     FormAmsHelper::GetInstance().StopExtensionAbility(want);
313 }
314 
IsNeedRender(int64_t formId)315 bool FormRenderMgr::IsNeedRender(int64_t formId)
316 {
317     FormRecord formRecord;
318     bool isGetFormRecord = FormDataMgr::GetInstance().GetFormRecord(formId, formRecord);
319     if (!isGetFormRecord) {
320         HILOG_ERROR("%{public}s fail, not exist such form, formId:%{public}" PRId64 "", __func__, formId);
321         return false;
322     }
323     if (formRecord.uiSyntax != FormType::ETS) {
324         HILOG_DEBUG("%{public}s fail, no need render, formId:%{public}" PRId64 "", __func__, formId);
325         return false;
326     }
327     return true;
328 }
329 
HandleConnectFailed(int64_t formId,int32_t errorCode) const330 void FormRenderMgr::HandleConnectFailed(int64_t formId, int32_t errorCode) const
331 {
332     HILOG_ERROR("Connect render service failed, formId: %{public}" PRId64 ", errorCode: %{public}d",
333         formId, errorCode);
334     std::vector<sptr<IRemoteObject>> formHostObjs;
335     FormDataMgr::GetInstance().GetFormHostRemoteObj(formId, formHostObjs);
336     for (const auto &host : formHostObjs) {
337         auto hostClient = iface_cast<IFormHost>(host);
338         if (hostClient == nullptr) {
339             HILOG_ERROR("hostClient is nullptr");
340             continue;
341         }
342         hostClient->OnError(errorCode, "Connect FormRenderService failed");
343     }
344 }
345 
IsRerenderForRenderServiceDied(int64_t formId)346 bool FormRenderMgr::IsRerenderForRenderServiceDied(int64_t formId)
347 {
348     int32_t rerenderCount = 0;
349     int32_t reSandboxRenderCount = 0;
350     if (renderInner_ != nullptr) {
351         rerenderCount = renderInner_->GetReRenderCount();
352     }
353     if (sandboxInner_ != nullptr) {
354         reSandboxRenderCount = sandboxInner_->GetReRenderCount();
355     }
356     bool ret = IsNeedRender(formId) && (rerenderCount > 0 || reSandboxRenderCount > 0);
357     HILOG_DEBUG("Is need to rerender: %{public}d.", ret);
358     return ret;
359 }
360 
InitRenderInner(bool isSandbox)361 void FormRenderMgr::InitRenderInner(bool isSandbox)
362 {
363     std::lock_guard<std::mutex> lock(renderInnerMutex_);
364     if (isSandbox) {
365         if (sandboxInner_ == nullptr) {
366             sandboxInner_ = std::make_shared<FormSandboxRenderMgrInner>();
367         }
368     } else {
369         if (renderInner_ == nullptr) {
370             renderInner_ = std::make_shared<FormRenderMgrInner>();
371         }
372     }
373 }
374 
RecycleForms(const std::vector<int64_t> & formIds,const Want & want,const sptr<IRemoteObject> & remoteObjectOfHost)375 ErrCode FormRenderMgr::RecycleForms(
376     const std::vector<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObjectOfHost)
377 {
378     if (renderInner_ != nullptr) {
379         return renderInner_->RecycleForms(formIds, want, remoteObjectOfHost);
380     }
381     if (sandboxInner_ != nullptr) {
382         return sandboxInner_->RecycleForms(formIds, want, remoteObjectOfHost);
383     }
384     return ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED;
385 }
386 
RecoverForms(const std::vector<int64_t> & formIds,const std::string & bundleName,const WantParams & wantParams)387 ErrCode FormRenderMgr::RecoverForms(
388     const std::vector<int64_t> &formIds, const std::string &bundleName, const WantParams &wantParams)
389 {
390     if (renderInner_ != nullptr) {
391         return renderInner_->RecoverForms(formIds, bundleName, wantParams);
392     }
393     if (sandboxInner_ != nullptr) {
394         return sandboxInner_->RecoverForms(formIds, bundleName, wantParams);
395     }
396     return ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED;
397 }
398 } // namespace AppExecFwk
399 } // namespace OHOS
400