• 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/form_render_mgr.h"
17 
18 #include <mutex>
19 
20 #include "fms_log_wrapper.h"
21 #include "ams_mgr/form_ams_helper.h"
22 #include "bms_mgr/form_bms_helper.h"
23 #include "data_center/form_cache_mgr.h"
24 #include "data_center/database/form_db_cache.h"
25 #include "form_constants.h"
26 #include "data_center/form_data_mgr.h"
27 #include "data_center/form_info/form_info_mgr.h"
28 #include "common/event/form_event_report.h"
29 #include "form_host_interface.h"
30 #include "form_mgr_errors.h"
31 #include "form_render/form_sandbox_render_mgr_inner.h"
32 #include "form_provider/form_supply_callback.h"
33 #include "common/util/form_trust_mgr.h"
34 #include "common/util/form_util.h"
35 #include "ipc_skeleton.h"
36 #include "os_account_manager.h"
37 #include "want.h"
38 
39 namespace OHOS {
40 namespace AppExecFwk {
41 namespace {
42     constexpr size_t LAST_CONNECTION = 1;
43 }
44 using Want = OHOS::AAFwk::Want;
FormRenderMgr()45 FormRenderMgr::FormRenderMgr()
46 {
47 }
~FormRenderMgr()48 FormRenderMgr::~FormRenderMgr()
49 {
50 }
51 
GetFormRenderState()52 void FormRenderMgr::GetFormRenderState()
53 {
54     // Check whether the account is authenticated.
55     bool isVerified = false;
56     int32_t userId = FormUtil::GetCurrentAccountId();
57     AccountSA::OsAccountManager::IsOsAccountVerified(userId, isVerified);
58     HILOG_INFO("isVerified:%{public}d,isVerified_:%{public}d,mounted:%{public}d,screen:%{public}d,userId:%{public}d",
59         isVerified, isVerified_, isSecondMounted_, isScreenUnlocked_, userId);
60 
61     std::lock_guard<std::mutex> lock(isVerifiedMutex_);
62     if (isVerified_ == isVerified) {
63         return;
64     }
65 
66     isVerified_ = isVerified;
67     if (!isVerified) {
68         return;
69     }
70     if (!isScreenUnlocked_) {
71         PostOnUnlockTask();
72     }
73     if (isSecondMounted_) {
74         ExecAcquireProviderTask(userId);
75     }
76 }
77 
GetIsSecondMounted() const78 bool FormRenderMgr::GetIsSecondMounted() const
79 {
80     HILOG_DEBUG("GetIsSecondMounted");
81     std::lock_guard<std::mutex> lock(isVerifiedMutex_);
82     return isSecondMounted_;
83 }
84 
RenderForm(const FormRecord & formRecord,const WantParams & wantParams,const sptr<IRemoteObject> & hostToken)85 ErrCode FormRenderMgr::RenderForm(
86     const FormRecord &formRecord, const WantParams &wantParams, const sptr<IRemoteObject> &hostToken)
87 {
88     HILOG_INFO("formId:%{public}" PRId64 ", formUserId:%{public}d", formRecord.formId, formRecord.userId);
89     GetFormRenderState();
90     HILOG_INFO("the current user authentication status:%{public}d,%{public}d", isVerified_, isScreenUnlocked_);
91     if (formRecord.uiSyntax != FormType::ETS) {
92         return ERR_OK;
93     }
94     if (formRecord.formId <= 0) {
95         HILOG_ERROR("formId not greater than 0");
96         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
97     }
98 
99     Want want;
100     want.SetParams(wantParams);
101     std::string recordUid = std::to_string(formRecord.providerUserId) + formRecord.bundleName;
102     want.SetParam(Constants::FORM_SUPPLY_UID, recordUid);
103     bool renderState = false;
104     {
105         std::lock_guard<std::mutex> lock(isVerifiedMutex_);
106         renderState = isVerified_ || isScreenUnlocked_;
107         want.SetParam(Constants::FORM_RENDER_STATE, renderState);
108     }
109     if (!renderState && CheckMultiAppFormVersionCode(formRecord)) {
110         want.SetParam(Constants::FORM_RENDER_WITHOUT_UNLOCK_STATE, true);
111     }
112 
113     bool formIsVisible = FormDataMgr::GetInstance().GetFormVisible(formRecord.formId);
114     want.SetParam(Constants::FORM_IS_VISIBLE, formIsVisible);
115     want.SetParam(Constants::FORM_LOCATION_KEY, static_cast<int32_t>(formRecord.formLocation));
116 
117     if (formRecord.privacyLevel > 0) {
118         InitRenderInner(true, formRecord.userId);
119         return sandboxInners_[formRecord.userId]->RenderForm(formRecord, want, hostToken);
120     } else {
121         InitRenderInner(false, formRecord.userId);
122         return renderInners_[formRecord.userId]->RenderForm(formRecord, want, hostToken);
123     }
124 }
125 
UpdateRenderingForm(int64_t formId,const FormProviderData & formProviderData,const WantParams & wantParams,bool mergeData)126 ErrCode FormRenderMgr::UpdateRenderingForm(int64_t formId, const FormProviderData &formProviderData,
127     const WantParams &wantParams, bool mergeData)
128 {
129     HILOG_INFO("update formId:%{public}" PRId64 ",%{public}zu", formId, formProviderData.GetDataString().length());
130 
131     FormRecord formRecord;
132     bool isGetFormRecord = FormDataMgr::GetInstance().GetFormRecord(formId, formRecord);
133     if (!isGetFormRecord) {
134         HILOG_ERROR("get FormRecord fail, not exist such form, formId:%{public}" PRId64 "", formId);
135         return ERR_APPEXECFWK_FORM_NOT_EXIST_ID;
136     }
137     int32_t formUserId = formRecord.userId;
138     HILOG_INFO("update formUserId:%{public}d", formUserId);
139     if (formRecord.privacyLevel > 0) {
140         auto iter = sandboxInners_.find(formUserId);
141         if (iter == sandboxInners_.end()) {
142             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
143         }
144         return iter->second->UpdateRenderingForm(formRecord, formProviderData, wantParams, mergeData);
145     } else {
146         auto iter = renderInners_.find(formUserId);
147         if (iter == renderInners_.end()) {
148             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
149         }
150         return iter->second->UpdateRenderingForm(formRecord, formProviderData, wantParams, mergeData);
151     }
152 }
153 
ReloadForm(const std::vector<FormRecord> && formRecords,const std::string & bundleName,int32_t userId)154 ErrCode FormRenderMgr::ReloadForm(
155     const std::vector<FormRecord> &&formRecords, const std::string &bundleName, int32_t userId)
156 {
157     HILOG_INFO("userId:%{public}d", userId);
158     std::vector<FormRecord> sandboxRecords;
159     std::vector<FormRecord> normalRecords;
160     for (const auto &record : formRecords) {
161         if (record.privacyLevel > 0) {
162             sandboxRecords.emplace_back(record);
163         } else {
164             normalRecords.emplace_back(record);
165         }
166     }
167     ErrCode ret = ERR_OK;
168     auto renderIter = renderInners_.find(userId);
169     if (!normalRecords.empty() && renderIter != renderInners_.end()) {
170         ErrCode normalRet = renderIter->second->ReloadForm(std::move(normalRecords), bundleName, userId);
171         ret = normalRet != ERR_OK ? normalRet : ret;
172     }
173     auto sandboxIter = sandboxInners_.find(userId);
174     if (!sandboxRecords.empty() && sandboxIter != sandboxInners_.end()) {
175         ErrCode sandboxRet = sandboxIter->second->ReloadForm(std::move(sandboxRecords), bundleName, userId);
176         ret = sandboxRet != ERR_OK ? sandboxRet : ret;
177     }
178     if (ret != ERR_OK) {
179         FormEventReport::SendFormFailedEvent(FormEventName::RELOAD_FORM_FAILED,
180             0,
181             bundleName,
182             "",
183             static_cast<int32_t>(ReloadFormErrorType::RELOAD_FORM_FRS_DEAD),
184             ret);
185     }
186     return ret;
187 }
188 
PostOnUnlockTask()189 void FormRenderMgr::PostOnUnlockTask()
190 {
191     int32_t userId = FormUtil::GetCurrentAccountId();
192     auto renderIter = renderInners_.find(userId);
193     if (renderIter != renderInners_.end()) {
194         renderIter->second->PostOnUnlockTask();
195     }
196     auto sandboxIter = sandboxInners_.find(userId);
197     if (sandboxIter != sandboxInners_.end()) {
198         sandboxIter->second->PostOnUnlockTask();
199     }
200 }
201 
AddAcquireProviderFormInfoTask(int32_t userId,std::function<void ()> task)202 void FormRenderMgr::AddAcquireProviderFormInfoTask(int32_t userId, std::function<void()> task)
203 {
204     HILOG_DEBUG("call");
205     std::lock_guard<std::mutex> lock(taskQueueMutex_);
206     auto iter = taskQueueMap_.find(userId);
207     if (iter == taskQueueMap_.end()) {
208         std::queue<std::function<void()>> taskQueue;
209         taskQueue.push(task);
210         taskQueueMap_.emplace(userId, taskQueue);
211         return;
212     }
213     iter->second.push(task);
214 }
215 
ExecAcquireProviderTask(int32_t userId)216 void FormRenderMgr::ExecAcquireProviderTask(int32_t userId)
217 {
218     HILOG_INFO("start, userId:%{public}d", userId);
219     // start to execute asynchronous tasks in the queue
220     std::lock_guard<std::mutex> lock(taskQueueMutex_);
221     auto iter = taskQueueMap_.find(userId);
222     if (iter == taskQueueMap_.end()) {
223         HILOG_WARN("taskQueueMap_ not find userId:%{public}d", userId);
224         return;
225     }
226     auto taskQueue = iter->second;
227     while (!taskQueue.empty()) {
228         auto task = taskQueue.front();
229         task();
230         taskQueue.pop();
231     }
232     taskQueueMap_.erase(userId);
233     HILOG_INFO("end, userId:%{public}d", userId);
234 }
235 
AddAcquireProviderForbiddenTask(const std::string & bundleName,int64_t formId,std::function<void ()> task)236 void FormRenderMgr::AddAcquireProviderForbiddenTask(const std::string &bundleName,
237     int64_t formId, std::function<void()> task)
238 {
239     std::lock_guard<std::mutex> lock(forbiddenTaskMapMutex_);
240 
241     auto search = forbiddenTaskMap_.find(bundleName);
242     if (search == forbiddenTaskMap_.end()) {
243         std::unordered_map<int64_t, std::function<void()>> taskQueue;
244         taskQueue.emplace(formId, task);
245         forbiddenTaskMap_.emplace(bundleName, taskQueue);
246     } else {
247         search->second[formId] = task;
248     }
249     HILOG_INFO("add ftask success, formId:%{public}" PRId64, formId);
250 }
251 
ExecAcquireProviderForbiddenTask(const std::string & bundleName)252 void FormRenderMgr::ExecAcquireProviderForbiddenTask(const std::string &bundleName)
253 {
254     HILOG_INFO("start");
255     // start to execute asynchronous tasks in the map
256     std::unordered_map<int64_t, std::function<void()>> taskQueue;
257     {
258         std::lock_guard<std::mutex> lock(forbiddenTaskMapMutex_);
259         auto search = forbiddenTaskMap_.find(bundleName);
260         if (search == forbiddenTaskMap_.end()) {
261             return;
262         }
263         taskQueue = search->second;
264         forbiddenTaskMap_.erase(search);
265     }
266     auto iter = taskQueue.begin();
267     while (iter != taskQueue.end()) {
268         auto task = iter->second;
269         task();
270         HILOG_INFO("exec ftask success, formId:%{public}" PRId64, iter->first);
271         iter = taskQueue.erase(iter);
272     }
273 }
274 
ExecAcquireProviderForbiddenTaskByFormId(const int64_t formId)275 void FormRenderMgr::ExecAcquireProviderForbiddenTaskByFormId(const int64_t formId)
276 {
277     HILOG_INFO("start, formId %{public}" PRId64, formId);
278     std::function<void()> task;
279     {
280         std::lock_guard<std::mutex> lock(forbiddenTaskMapMutex_);
281         for (auto iter = forbiddenTaskMap_.begin(); iter != forbiddenTaskMap_.end(); ++iter) {
282             auto search = iter->second.find(formId);
283             if (search != iter->second.end()) {
284                 task = search->second;
285                 iter->second.erase(search);
286                 if (iter->second.empty()) {
287                     forbiddenTaskMap_.erase(iter);
288                 }
289                 HILOG_INFO("add ftask success, formId:%{public}" PRId64, formId);
290                 break;
291             }
292         }
293     }
294     if (task) {
295         task();
296     }
297 }
298 
DeleteAcquireForbiddenTasksByBundleName(const std::string & bundleName)299 void FormRenderMgr::DeleteAcquireForbiddenTasksByBundleName(const std::string &bundleName)
300 {
301     std::lock_guard<std::mutex> lock(forbiddenTaskMapMutex_);
302     auto search = forbiddenTaskMap_.find(bundleName);
303     if (search != forbiddenTaskMap_.end()) {
304         forbiddenTaskMap_.erase(search);
305         HILOG_INFO("delete ftasks success, bundlename:%{public}s", bundleName.c_str());
306     }
307 }
308 
DeleteAcquireForbiddenTaskByFormId(int64_t formId)309 void FormRenderMgr::DeleteAcquireForbiddenTaskByFormId(int64_t formId)
310 {
311     std::lock_guard<std::mutex> lock(forbiddenTaskMapMutex_);
312 
313     for (auto iter = forbiddenTaskMap_.begin(); iter != forbiddenTaskMap_.end(); ++iter) {
314         auto search = iter->second.find(formId);
315         if (search != iter->second.end()) {
316             iter->second.erase(search);
317             if (iter->second.empty()) {
318                 forbiddenTaskMap_.erase(iter);
319             }
320             HILOG_INFO("delete ftask success, formId:%{public}" PRId64, formId);
321             break;
322         }
323     }
324 }
325 
AddPostRenderFormTask(int64_t formId,std::function<void ()> task)326 void FormRenderMgr::AddPostRenderFormTask(int64_t formId, std::function<void()> task)
327 {
328     std::lock_guard<std::mutex> lock(renderFormTaskMapMutex_);
329     auto search = renderFormTaskMap_.find(formId);
330     if (search == renderFormTaskMap_.end()) {
331         renderFormTaskMap_.emplace(formId, task);
332     } else {
333         search->second = task;
334     }
335     HILOG_INFO("add PostRenderFormTask success, formId:%{public}" PRId64, formId);
336 }
337 
ExecPostRenderFormTask(int64_t formId)338 void FormRenderMgr::ExecPostRenderFormTask(int64_t formId)
339 {
340     std::function<void()> task;
341     bool findTask = false;
342     {
343         std::lock_guard<std::mutex> lock(renderFormTaskMapMutex_);
344         auto search = renderFormTaskMap_.find(formId);
345         if (search != renderFormTaskMap_.end()) {
346             task = search->second;
347             findTask = true;
348             renderFormTaskMap_.erase(search);
349         }
350     }
351     if (findTask) {
352         task();
353         HILOG_INFO("exec task, formId:%{public}" PRId64, formId);
354     }
355 }
356 
DeletePostRenderFormTask(int64_t formId)357 void FormRenderMgr::DeletePostRenderFormTask(int64_t formId)
358 {
359     std::lock_guard<std::mutex> lock(renderFormTaskMapMutex_);
360     auto search = renderFormTaskMap_.find(formId);
361     if (search != renderFormTaskMap_.end()) {
362         renderFormTaskMap_.erase(search);
363         HILOG_INFO("del task, formId:%{public}" PRId64, formId);
364     }
365 }
366 
NotifyScreenOn()367 void FormRenderMgr::NotifyScreenOn()
368 {
369     int32_t userId = FormUtil::GetCurrentAccountId();
370     auto renderIter = renderInners_.find(userId);
371     if (renderIter != renderInners_.end()) {
372         renderIter->second->NotifyScreenOn();
373     }
374     auto sandboxIter = sandboxInners_.find(userId);
375     if (sandboxIter != sandboxInners_.end()) {
376         sandboxIter->second->NotifyScreenOn();
377     }
378 }
379 
OnScreenUnlock(int32_t userId)380 void FormRenderMgr::OnScreenUnlock(int32_t userId)
381 {
382     // Check whether the account is authenticated.
383     bool isVerified = false;
384     AccountSA::OsAccountManager::IsOsAccountVerified(userId, isVerified);
385     HILOG_INFO("isVerified_:%{public}d, screenUnlocked:%{public}d, isVerified:%{public}d, userId:%{public}d",
386         isVerified_, isScreenUnlocked_, isVerified, userId);
387     std::lock_guard<std::mutex> lock(isVerifiedMutex_);
388     if (isVerified && !isSecondMounted_) {
389         isSecondMounted_ = true;
390     }
391 
392     if (isScreenUnlocked_) {
393         return;
394     }
395 
396     // el2 path maybe not unlocked, should not acquire data
397     isScreenUnlocked_ = true;
398     if (!isVerified_) {
399         PostOnUnlockTask();
400     }
401 }
402 
OnUnlock(int32_t userId)403 void FormRenderMgr::OnUnlock(int32_t userId)
404 {
405     HILOG_INFO("call. %{public}d,%{public}d,%{public}d,%{public}d",
406         isVerified_, isSecondMounted_, isScreenUnlocked_, userId);
407     if (isSecondMounted_) {
408         return;
409     }
410 
411     {
412         std::lock_guard<std::mutex> lock(isVerifiedMutex_);
413         isSecondMounted_ = true;
414         if (!isScreenUnlocked_) {
415             PostOnUnlockTask();
416         }
417     }
418     ExecAcquireProviderTask(userId);
419 }
420 
SetVisibleChange(int64_t formId,bool isVisible)421 void FormRenderMgr::SetVisibleChange(int64_t formId, bool isVisible)
422 {
423     HILOG_INFO("call.");
424     int32_t userId = FormUtil::GetCurrentAccountId();
425     auto renderIter = renderInners_.find(userId);
426     if (renderIter != renderInners_.end()) {
427         renderIter->second->PostSetVisibleChangeTask(formId, isVisible);
428     }
429     auto sandboxIter = sandboxInners_.find(userId);
430     if (sandboxIter != sandboxInners_.end()) {
431         sandboxIter->second->PostSetVisibleChangeTask(formId, isVisible);
432     }
433 }
434 
StopRenderingForm(int64_t formId,const FormRecord & formRecord,const std::string & compId,const sptr<IRemoteObject> & hostToken)435 ErrCode FormRenderMgr::StopRenderingForm(
436     int64_t formId, const FormRecord &formRecord, const std::string &compId, const sptr<IRemoteObject> &hostToken)
437 {
438     HILOG_INFO("formUserId:%{public}d", formRecord.userId);
439     if (formRecord.privacyLevel > 0) {
440         auto iter = sandboxInners_.find(formRecord.userId);
441         if (iter == sandboxInners_.end()) {
442             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
443         }
444         return iter->second->StopRenderingForm(formId, formRecord, compId, hostToken);
445     } else {
446         auto iter = renderInners_.find(formRecord.userId);
447         if (iter == renderInners_.end()) {
448             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
449         }
450         return iter->second->StopRenderingForm(formId, formRecord, compId, hostToken);
451     }
452 }
453 
RenderFormCallback(int64_t formId,const Want & want)454 ErrCode FormRenderMgr::RenderFormCallback(int64_t formId, const Want &want)
455 {
456     HILOG_DEBUG("call");
457     return ERR_OK;
458 }
459 
StopRenderingFormCallback(int64_t formId,const Want & want)460 ErrCode FormRenderMgr::StopRenderingFormCallback(int64_t formId, const Want &want)
461 {
462     int32_t callingUserId = IPCSkeleton::GetCallingUid() / Constants::CALLING_UID_TRANSFORM_DIVISOR;
463     HILOG_INFO("formId:%{public}" PRId64 ", callingUserId:%{public}d", formId, callingUserId);
464     auto renderIter = renderInners_.find(callingUserId);
465     if (renderIter != renderInners_.end()) {
466         renderIter->second->StopRenderingFormCallback(formId, want);
467     }
468     auto sandboxIter = sandboxInners_.find(callingUserId);
469     if (sandboxIter != sandboxInners_.end()) {
470         sandboxIter->second->StopRenderingFormCallback(formId, want);
471     }
472     return ERR_OK;
473 }
474 
ReleaseRenderer(int64_t formId,const FormRecord & formRecord,const std::string & compId)475 ErrCode FormRenderMgr::ReleaseRenderer(int64_t formId, const FormRecord &formRecord, const std::string &compId)
476 {
477     HILOG_INFO("formId:%{public}" PRId64 ", formUserId:%{public}d", formId, formRecord.userId);
478     if (formRecord.privacyLevel > 0) {
479         auto iter = sandboxInners_.find(formRecord.userId);
480         if (iter == sandboxInners_.end()) {
481             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
482         }
483         return iter->second->ReleaseRenderer(formId, formRecord, compId);
484     } else {
485         auto iter = renderInners_.find(formRecord.userId);
486         if (iter == renderInners_.end()) {
487             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
488         }
489         return iter->second->ReleaseRenderer(formId, formRecord, compId);
490     }
491 }
492 
AddConnection(int64_t formId,sptr<FormRenderConnection> connection,const FormRecord & formRecord)493 ErrCode FormRenderMgr::AddConnection(
494     int64_t formId, sptr<FormRenderConnection> connection, const FormRecord &formRecord)
495 {
496     HILOG_INFO("formUserId: %{public}d", formRecord.userId);
497     if (formRecord.privacyLevel > 0) {
498         auto iter = sandboxInners_.find(formRecord.userId);
499         if (iter == sandboxInners_.end()) {
500             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
501         }
502         return iter->second->AddConnection(formId, connection);
503     } else {
504         auto iter = renderInners_.find(formRecord.userId);
505         if (iter == renderInners_.end()) {
506             return ERR_APPEXECFWK_FORM_INVALID_PARAM;
507         }
508         return iter->second->AddConnection(formId, connection);
509     }
510 }
511 
RemoveConnection(int64_t formId,const FormRecord & formRecord)512 void FormRenderMgr::RemoveConnection(int64_t formId, const FormRecord &formRecord)
513 {
514     HILOG_INFO("formId:%{public}" PRId64 ", formUserId:%{public}d", formId, formRecord.userId);
515     if (formRecord.privacyLevel > 0) {
516         auto iter = sandboxInners_.find(formRecord.userId);
517         if (iter != sandboxInners_.end()) {
518             iter->second->RemoveConnection(formId);
519         }
520     } else {
521         auto iter = renderInners_.find(formRecord.userId);
522         if (iter != renderInners_.end()) {
523             iter->second->RemoveConnection(formId);
524         }
525     }
526 }
527 
checkConnectionsFormIds(std::vector<int64_t> formIds,int32_t userId,std::vector<int64_t> & needconFormIds)528 ErrCode FormRenderMgr::checkConnectionsFormIds(std::vector<int64_t> formIds, int32_t userId,
529     std::vector<int64_t> &needconFormIds)
530 {
531     auto iter = renderInners_.find(userId);
532     if (iter == renderInners_.end()) {
533         return ERR_APPEXECFWK_FORM_INVALID_PARAM;
534     }
535     return iter->second->checkConnectionsFormIds(formIds, needconFormIds);
536 }
537 
reAddConnections(std::vector<int64_t> formIds,int32_t userId,const sptr<IRemoteObject> & remoteObject)538 void FormRenderMgr::reAddConnections(std::vector<int64_t> formIds,
539     int32_t userId, const sptr<IRemoteObject> &remoteObject)
540 {
541     HILOG_ERROR("reAddConnections - Connect formIds, ");
542 
543     sptr<IFormHost> hostClient = iface_cast<IFormHost>(remoteObject);
544     if (hostClient == nullptr) {
545         HILOG_ERROR("null hostClient");
546         return;
547     }
548     hostClient->OnError(ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED, "FormRenderService is dead.", formIds);
549 }
550 
CleanFormHost(const sptr<IRemoteObject> & host,const int hostCallingUid)551 void FormRenderMgr::CleanFormHost(const sptr<IRemoteObject> &host, const int hostCallingUid)
552 {
553     int32_t hostUserId = hostCallingUid / Constants::CALLING_UID_TRANSFORM_DIVISOR;
554     if (hostUserId == 0) {
555         HILOG_WARN("hostUserId is 0, get current active userId ");
556         hostUserId = FormUtil::GetCurrentAccountId();
557     }
558     HILOG_WARN("hostUserId:%{public}d", hostUserId);
559     auto renderIter = renderInners_.find(hostUserId);
560     if (renderIter != renderInners_.end()) {
561         renderIter->second->CleanFormHost(host);
562     }
563     auto sandboxIter = sandboxInners_.find(hostUserId);
564     if (sandboxIter != sandboxInners_.end()) {
565         sandboxIter->second->CleanFormHost(host);
566     }
567 }
568 
AddRenderDeathRecipient(const sptr<IRemoteObject> & remoteObject,const FormRecord & formRecord)569 void FormRenderMgr::AddRenderDeathRecipient(const sptr<IRemoteObject> &remoteObject, const FormRecord &formRecord)
570 {
571     HILOG_INFO("formUserId:%{public}d", formRecord.userId);
572     if (formRecord.privacyLevel > 0) {
573         auto iter = sandboxInners_.find(formRecord.userId);
574         if (iter != sandboxInners_.end()) {
575             iter->second->AddRenderDeathRecipient(remoteObject);
576         }
577     } else {
578         auto iter = renderInners_.find(formRecord.userId);
579         if (iter != renderInners_.end()) {
580             iter->second->AddRenderDeathRecipient(remoteObject);
581         }
582     }
583 }
584 
OnRenderingBlock(const std::string & bundleName)585 void FormRenderMgr::OnRenderingBlock(const std::string &bundleName)
586 {
587     HILOG_INFO("bundleName:%{public}s", bundleName.c_str());
588     FormEventInfo eventInfo;
589     eventInfo.bundleName = bundleName;
590     FormEventReport::SendSecondFormEvent(
591         FormEventName::FORM_RENDER_BLOCK, HiSysEventType::FAULT, eventInfo);
592 
593     FormTrustMgr::GetInstance().MarkTrustFlag(bundleName, false);
594 
595     Want want;
596     want.SetElementName(Constants::FRS_BUNDLE_NAME, "ServiceExtension");
597     want.AddFlags(Want::FLAG_ABILITY_FORM_ENABLED);
598     FormAmsHelper::GetInstance().StopExtensionAbility(want);
599 }
600 
IsNeedRender(int64_t formId)601 bool FormRenderMgr::IsNeedRender(int64_t formId)
602 {
603     FormRecord formRecord;
604     bool isGetFormRecord = FormDataMgr::GetInstance().GetFormRecord(formId, formRecord);
605     if (!isGetFormRecord) {
606         HILOG_ERROR("not exist such form, formId:%{public}" PRId64 "", formId);
607         return false;
608     }
609     if (formRecord.uiSyntax != FormType::ETS) {
610         HILOG_DEBUG("no need render, formId:%{public}" PRId64 "", formId);
611         return false;
612     }
613     return true;
614 }
615 
HandleConnectFailed(int64_t formId,int32_t errorCode) const616 void FormRenderMgr::HandleConnectFailed(int64_t formId, int32_t errorCode) const
617 {
618     HILOG_ERROR("Connect render service failed, formId:%{public}" PRId64 ", errorCode:%{public}d",
619         formId, errorCode);
620     std::vector<sptr<IRemoteObject>> formHostObjs;
621     FormDataMgr::GetInstance().GetFormHostRemoteObj(formId, formHostObjs);
622     for (const auto &host : formHostObjs) {
623         auto hostClient = iface_cast<IFormHost>(host);
624         if (hostClient == nullptr) {
625             HILOG_ERROR("null hostClient");
626             continue;
627         }
628         hostClient->OnError(errorCode, "Connect FormRenderService failed");
629     }
630 }
631 
IsRerenderForRenderServiceDied(int64_t formId)632 bool FormRenderMgr::IsRerenderForRenderServiceDied(int64_t formId)
633 {
634     int32_t rerenderCount = 0;
635     int32_t reSandboxRenderCount = 0;
636     FormRecord formRecord;
637     bool isGetFormRecord = FormDataMgr::GetInstance().GetFormRecord(formId, formRecord);
638     if (!isGetFormRecord) {
639         HILOG_ERROR("get FormRecord fail, not exist such form, formId:%{public}" PRId64 "", formId);
640         return true;
641     }
642     HILOG_INFO("formId:%{public}" PRId64 ", formUserId:%{public}d", formId, formRecord.userId);
643     auto renderIter = renderInners_.find(formRecord.userId);
644     if (renderIter != renderInners_.end()) {
645         rerenderCount = renderIter->second->GetReRenderCount();
646     }
647     auto sandboxIter = sandboxInners_.find(formRecord.userId);
648     if (sandboxIter != sandboxInners_.end()) {
649         reSandboxRenderCount = sandboxIter->second->GetReRenderCount();
650     }
651     bool ret = IsNeedRender(formId) && (rerenderCount > 0 || reSandboxRenderCount > 0);
652     HILOG_DEBUG("Is need to rerender:%{public}d", ret);
653     return ret;
654 }
655 
InitRenderInner(bool isSandbox,int32_t userId)656 void FormRenderMgr::InitRenderInner(bool isSandbox, int32_t userId)
657 {
658     HILOG_INFO("isSandbox: %{public}d userId: %{public}d.", isSandbox, userId);
659     std::lock_guard<std::mutex> lock(renderInnerMutex_);
660     if (isSandbox) {
661         auto iter = sandboxInners_.find(userId);
662         if (iter == sandboxInners_.end()) {
663             auto formSandboxRenderMgr = std::make_shared<FormSandboxRenderMgrInner>();
664             formSandboxRenderMgr->SetUserId(userId);
665             sandboxInners_.emplace(userId, formSandboxRenderMgr);
666         }
667     } else {
668         auto iter = renderInners_.find(userId);
669         if (iter == renderInners_.end()) {
670             auto formRenderMgr = std::make_shared<FormRenderMgrInner>();
671             formRenderMgr->SetUserId(userId);
672             renderInners_.emplace(userId, formRenderMgr);
673         }
674     }
675 }
676 
RecycleForms(const std::vector<int64_t> & formIds,const Want & want,const sptr<IRemoteObject> & remoteObjectOfHost)677 ErrCode FormRenderMgr::RecycleForms(
678     const std::vector<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObjectOfHost)
679 {
680     int callingUserId = want.GetIntParam(Constants::RECYCLE_FORMS_USER_ID, 0);
681     if (callingUserId == 0) {
682         callingUserId = IPCSkeleton::GetCallingUid() / Constants::CALLING_UID_TRANSFORM_DIVISOR;
683         if (callingUserId == 0) {
684             callingUserId = FormUtil::GetCurrentAccountId();
685         }
686         HILOG_INFO("callingUserId is 0, update callingUserId:%{public}d", callingUserId);
687     }
688     auto renderIter = renderInners_.find(callingUserId);
689     if (renderIter != renderInners_.end()) {
690         return renderIter->second->RecycleForms(formIds, want, remoteObjectOfHost);
691     }
692     auto sandboxIter = sandboxInners_.find(callingUserId);
693     if (sandboxIter != sandboxInners_.end()) {
694         return sandboxIter->second->RecycleForms(formIds, want, remoteObjectOfHost);
695     }
696     return ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED;
697 }
698 
RecoverForms(const std::vector<int64_t> & formIds,const WantParams & wantParams)699 ErrCode FormRenderMgr::RecoverForms(const std::vector<int64_t> &formIds, const WantParams &wantParams)
700 {
701     int32_t callingUserId = IPCSkeleton::GetCallingUid() / Constants::CALLING_UID_TRANSFORM_DIVISOR;
702     auto renderIter = renderInners_.find(callingUserId);
703     if (renderIter != renderInners_.end()) {
704         return renderIter->second->RecoverForms(formIds, wantParams);
705     }
706     auto sandboxIter = sandboxInners_.find(callingUserId);
707     if (sandboxIter != sandboxInners_.end()) {
708         return sandboxIter->second->RecoverForms(formIds, wantParams);
709     }
710     return ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED;
711 }
712 
DisconnectAllRenderConnections(int userId)713 void FormRenderMgr::DisconnectAllRenderConnections(int userId)
714 {
715     HILOG_INFO("userId: %{public}d", userId);
716     auto renderIter = renderInners_.find(userId);
717     if (renderIter != renderInners_.end()) {
718         renderIter->second->DisconnectAllRenderConnections();
719     }
720     auto sandboxIter = sandboxInners_.find(userId);
721     if (sandboxIter != sandboxInners_.end()) {
722         sandboxIter->second->DisconnectAllRenderConnections();
723     }
724 }
725 
RerenderAllFormsImmediate(int userId)726 void FormRenderMgr::RerenderAllFormsImmediate(int userId)
727 {
728     HILOG_INFO("userId: %{public}d", userId);
729     auto renderIter = renderInners_.find(userId);
730     if (renderIter != renderInners_.end()) {
731         renderIter->second->RerenderAllFormsImmediate();
732     }
733     auto sandboxIter = sandboxInners_.find(userId);
734     if (sandboxIter != sandboxInners_.end()) {
735         sandboxIter->second->RerenderAllFormsImmediate();
736     }
737 }
738 
UpdateFormSize(const int64_t & formId,float width,float height,float borderWidth)739 void FormRenderMgr::UpdateFormSize(const int64_t &formId, float width, float height, float borderWidth)
740 {
741     int32_t callingUserId = IPCSkeleton::GetCallingUid() / Constants::CALLING_UID_TRANSFORM_DIVISOR;
742     auto renderIter = renderInners_.find(callingUserId);
743     if (renderIter != renderInners_.end()) {
744         renderIter->second->UpdateFormSize(formId, width, height, borderWidth);
745     }
746     auto sandboxIter = sandboxInners_.find(callingUserId);
747     if (sandboxIter != sandboxInners_.end()) {
748         sandboxIter->second->UpdateFormSize(formId, width, height, borderWidth);
749     }
750 }
751 
CheckMultiAppFormVersionCode(const FormRecord & formRecord)752 bool FormRenderMgr::CheckMultiAppFormVersionCode(const FormRecord &formRecord)
753 {
754     FormInfo formInfo;
755     if (FormInfoMgr::GetInstance().GetFormsInfoByRecord(formRecord, formInfo) != ERR_OK) {
756         return false;
757     }
758     bool isMultiAppForm = FormInfoMgr::GetInstance().IsMultiAppForm(formInfo) && formRecord.isSystemApp;
759     if (!isMultiAppForm) {
760         return false;
761     }
762     uint32_t versionCode = formInfo.versionCode;
763     uint32_t cacheCode = FormDbCache::GetInstance().GetMultiAppFormVersionCode(formRecord.bundleName);
764     if (versionCode >= cacheCode) {
765         if (versionCode > cacheCode) {
766             FormDbCache::GetInstance().UpdateMultiAppFormVersionCode(formRecord.bundleName, versionCode);
767         }
768         HILOG_INFO("check version code ok, bundleName: %{public}s, formId:%{public}" PRId64,
769             formRecord.bundleName.c_str(), formRecord.formId);
770         return true;
771     }
772     return false;
773 }
774 
GetFRSDiedInLowMemoryByUid(int32_t userId)775 bool FormRenderMgr::GetFRSDiedInLowMemoryByUid(int32_t userId)
776 {
777     HILOG_INFO("call");
778     auto renderIter = renderInners_.find(userId);
779     if (renderIter != renderInners_.end()) {
780         return renderIter->second->GetIsFRSDiedInLowMemory();
781     }
782     HILOG_WARN("not find renderInner userId: %{public}d", userId);
783     return false;
784 }
785 } // namespace AppExecFwk
786 } // namespace OHOS
787