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 "form_observer/form_observer_task_mgr.h"
17 #include "fms_log_wrapper.h"
18 #include "js_form_state_observer_interface.h"
19 #include "form_mgr/form_mgr_queue.h"
20 #include "form_mgr/form_mgr_adapter.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
FormObserverTaskMgr()24 FormObserverTaskMgr::FormObserverTaskMgr() {}
25
~FormObserverTaskMgr()26 FormObserverTaskMgr::~FormObserverTaskMgr() {}
27
28 /**
29 * @brief Post form add event.
30 * @param bundleName BundleName of the form host
31 * @param remoteObject thr remote observer.
32 * @param runningFormInfo Current form data.
33 */
PostAddTaskToHost(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)34 void FormObserverTaskMgr::PostAddTaskToHost(const std::string bundleName,
35 const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
36 {
37 HILOG_DEBUG("call");
38
39 auto addFunc = [bundleName, remoteObject, runningFormInfo]() {
40 FormObserverTaskMgr::GetInstance().FormAdd(bundleName, remoteObject, runningFormInfo);
41 };
42 FormMgrQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, addFunc);
43 }
44
45 /**
46 * @brief Post form remove event.
47 * @param bundleName BundleName of the form host
48 * @param remoteObject thr remote observer.
49 * @param runningFormInfo Current form data.
50 */
PostRemoveTaskToHost(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)51 void FormObserverTaskMgr::PostRemoveTaskToHost(const std::string bundleName,
52 const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
53 {
54 HILOG_DEBUG("call");
55
56 auto removeFunc = [bundleName, remoteObject, runningFormInfo]() {
57 FormObserverTaskMgr::GetInstance().FormRemove(bundleName, remoteObject, runningFormInfo);
58 };
59 FormMgrQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, removeFunc);
60 }
61
62 /**
63 * @brief Post form click event.
64 * @param bundleName BundleName of the form host
65 * @param formEventType form event type.
66 * @param remoteObject thr remote observer.
67 * @param runningFormInfo Current form data.
68 */
PostFormClickEventToHost(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)69 void FormObserverTaskMgr::PostFormClickEventToHost(
70 const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &remoteObject,
71 const RunningFormInfo &runningFormInfo)
72 {
73 HILOG_DEBUG("call");
74
75 auto task = [bundleName, formEventType, remoteObject, runningFormInfo]() {
76 if (remoteObject == nullptr) {
77 HILOG_ERROR("null remoteObject");
78 return;
79 }
80 FormObserverTaskMgr::GetInstance().FormClickEvent(bundleName, formEventType, remoteObject, runningFormInfo);
81 };
82 FormMgrQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, task);
83 }
84
85 /**
86 * @brief Post BatchRefresh forms.
87 * @param formRefreshType batch refresh forms type.
88 */
PostBatchRefreshForms(const int32_t formRefreshType)89 void FormObserverTaskMgr::PostBatchRefreshForms(const int32_t formRefreshType)
90 {
91 HILOG_DEBUG("call");
92
93 auto batchRefreshForms = [formRefreshType]() {
94 return FormMgrAdapter::GetInstance().BatchRefreshForms(formRefreshType);
95 };
96 FormMgrQueue::GetInstance().ScheduleTask(FORM_TASK_DELAY_TIME, batchRefreshForms);
97 }
98
FormAdd(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)99 void FormObserverTaskMgr::FormAdd(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
100 const RunningFormInfo &runningFormInfo)
101 {
102 HILOG_DEBUG("start");
103
104 sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
105 iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
106 if (remoteJsFormStateObserver == nullptr) {
107 HILOG_ERROR("get jsFormStateObserverProxy failed");
108 return;
109 }
110 remoteJsFormStateObserver->OnAddForm(bundleName, runningFormInfo);
111 HILOG_DEBUG("end");
112 }
113
FormRemove(const std::string bundleName,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)114 void FormObserverTaskMgr::FormRemove(const std::string bundleName, const sptr<IRemoteObject> &remoteObject,
115 const RunningFormInfo &runningFormInfo)
116 {
117 HILOG_DEBUG("start");
118
119 sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
120 iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
121 if (remoteJsFormStateObserver == nullptr) {
122 HILOG_ERROR("get jsFormStateObserverProxy failed");
123 return;
124 }
125 remoteJsFormStateObserver->OnRemoveForm(bundleName, runningFormInfo);
126 HILOG_DEBUG("end");
127 }
128
FormClickEvent(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & remoteObject,const RunningFormInfo & runningFormInfo)129 void FormObserverTaskMgr::FormClickEvent(const std::string &bundleName, const std::string &formEventType,
130 const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo)
131 {
132 HILOG_DEBUG("start");
133
134 sptr<AbilityRuntime::IJsFormStateObserver> remoteJsFormStateObserver =
135 iface_cast<AbilityRuntime::IJsFormStateObserver>(remoteObject);
136 if (remoteJsFormStateObserver == nullptr) {
137 HILOG_ERROR("fail get js form state observer proxy");
138 return;
139 }
140
141 remoteJsFormStateObserver->OnFormClickEvent(bundleName, formEventType, runningFormInfo);
142 HILOG_DEBUG("end");
143 }
144
145 } // namespace AppExecFwk
146 } // namespace OHOS