• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "status_mgr_center/form_render_status_mgr.h"
17 #include "status_mgr_center/form_render_status.h"
18 #include "status_mgr_center/form_render_status_table.h"
19 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "form_status_print.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
FormRenderStatusMgr()25 FormRenderStatusMgr::FormRenderStatusMgr()
26 {
27     HILOG_DEBUG("create FormRenderStatusMgr");
28 }
29 
~FormRenderStatusMgr()30 FormRenderStatusMgr::~FormRenderStatusMgr()
31 {
32     HILOG_DEBUG("destroy FormRenderStatusMgr");
33 }
34 
PostFormEvent(const int64_t formId,const FormFsmEvent event,std::function<int32_t ()> func)35 int32_t FormRenderStatusMgr::PostFormEvent(
36     const int64_t formId, const FormFsmEvent event, std::function<int32_t()> func)
37 {
38     // get status machine info
39     if (!FormRenderStatus::GetInstance().HasFormStatus(formId)) {
40         HILOG_INFO("create new status.");
41         FormRenderStatus::GetInstance().SetFormStatus(formId, FormFsmStatus::INIT);
42     }
43     FormFsmStatus status = FormRenderStatus::GetInstance().GetFormStatus(formId);
44 
45     FormRenderStatusMachineInfo info;
46     if (!FormRenderStatusTable::GetInstance().GetFormStatusInfo(status, event, info)) {
47         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
48     }
49 
50     HILOG_INFO("formId:%{public}" PRId64 ", status is %{public}s, event is %{public}s, nextStatus is %{public}s.",
51         formId,
52         FormStatusPrint::FormStatusToString(status).c_str(),
53         FormStatusPrint::FormEventToString(event).c_str(),
54         FormStatusPrint::FormStatusToString(info.nextStatus).c_str());
55 
56     // state machine switches to the next state.
57     FormRenderStatus::GetInstance().SetFormStatus(formId, info.nextStatus);
58 
59     // state machine excute
60     return ExecFormTask(info.processType, formId, event, status, func);
61 }
62 
ExecFormTask(FormFsmProcessType processType,const int64_t formId,const FormFsmEvent event,const FormFsmStatus status,std::function<int32_t ()> func)63 int32_t FormRenderStatusMgr::ExecFormTask(FormFsmProcessType processType, const int64_t formId,
64     const FormFsmEvent event, const FormFsmStatus status, std::function<int32_t()> func)
65 {
66     switch (processType) {
67         case FormFsmProcessType::PROCESS_TASK_DIRECT:
68             return ProcessTaskDirect(func);
69         case FormFsmProcessType::PROCESS_TASK_DELETE:
70             return ProcessTaskDelete(formId);
71         default:
72             return PrintTaskInfo(formId, event, status);
73     }
74 }
75 
GetFormEventId(const int64_t formId)76 std::string FormRenderStatusMgr::GetFormEventId(const int64_t formId)
77 {
78     std::shared_lock<std::shared_mutex> lock(formEventIdMapMutex_);
79     if (formEventIdMap_.find(formId) == formEventIdMap_.end()) {
80         return "";
81     }
82 
83     return formEventIdMap_[formId];
84 }
85 
SetFormEventId(const int64_t formId,std::string & eventId)86 void FormRenderStatusMgr::SetFormEventId(const int64_t formId, std::string &eventId)
87 {
88     std::unique_lock<std::shared_mutex> lock(formEventIdMapMutex_);
89     if (formEventIdMap_.find(formId) == formEventIdMap_.end()) {
90         formEventIdMap_.emplace(formId, eventId);
91         return;
92     }
93     formEventIdMap_[formId] = eventId;
94 }
95 
DeleteFormEventId(const int64_t formId)96 void FormRenderStatusMgr::DeleteFormEventId(const int64_t formId)
97 {
98     std::unique_lock<std::shared_mutex> lock(formEventIdMapMutex_);
99     auto iter = formEventIdMap_.find(formId);
100     if (iter != formEventIdMap_.end()) {
101         HILOG_INFO("formId:%{public}" PRId64 ". ", formId);
102         formEventIdMap_.erase(iter);
103     }
104 }
105 
ProcessTaskDirect(std::function<int32_t ()> func)106 int32_t FormRenderStatusMgr::ProcessTaskDirect(std::function<int32_t()> func)
107 {
108     return func();
109 }
110 
ProcessTaskDelete(const int64_t formId)111 int32_t FormRenderStatusMgr::ProcessTaskDelete(const int64_t formId)
112 {
113     DeleteFormEventId(formId);
114     FormRenderStatus::GetInstance().DeleteFormStatus(formId);
115     return ERR_OK;
116 }
117 
PrintTaskInfo(const int64_t formId,const FormFsmEvent event,const FormFsmStatus status)118 int32_t FormRenderStatusMgr::PrintTaskInfo(const int64_t formId, const FormFsmEvent event, const FormFsmStatus status)
119 {
120     HILOG_ERROR("formId:%{public}" PRId64 ", status is %{public}s, event is %{public}s.",
121         formId,
122         FormStatusPrint::FormStatusToString(status).c_str(),
123         FormStatusPrint::FormEventToString(event).c_str());
124     return ERR_OK;
125 }
126 }  // namespace AppExecFwk
127 }  // namespace OHOS
128