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 #include "form_renderer_dispatcher_impl.h"
16
17 #include <condition_variable>
18 #include <mutex>
19
20 #include "form_renderer.h"
21 #include "form_renderer_hilog.h"
22 #include "core/components_ng/gestures/gesture_group.h"
23
24 namespace OHOS {
25 namespace Ace {
26 constexpr int32_t PROCESS_WAIT_TIME = 20;
27 constexpr float DOUBLE = 2.0;
FormRendererDispatcherImpl(const std::shared_ptr<UIContent> uiContent,const std::shared_ptr<FormRenderer> formRenderer,std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler)28 FormRendererDispatcherImpl::FormRendererDispatcherImpl(
29 const std::shared_ptr<UIContent> uiContent,
30 const std::shared_ptr<FormRenderer> formRenderer,
31 std::weak_ptr<OHOS::AppExecFwk::EventHandler> eventHandler)
32 : uiContent_(uiContent), formRenderer_(formRenderer), eventHandler_(eventHandler)
33 {}
34
DispatchPointerEvent(const std::shared_ptr<OHOS::MMI::PointerEvent> & pointerEvent,SerializedGesture & serializedGesture)35 void FormRendererDispatcherImpl::DispatchPointerEvent(
36 const std::shared_ptr<OHOS::MMI::PointerEvent>& pointerEvent,
37 SerializedGesture& serializedGesture)
38 {
39 auto handler = eventHandler_.lock();
40 if (!handler) {
41 HILOG_ERROR("eventHandler is nullptr");
42 return;
43 }
44 serializedGesture.data.clear();
45
46 if (pointerEvent && pointerEvent->GetPointerAction() == OHOS::MMI::PointerEvent::POINTER_ACTION_DOWN) {
47 HILOG_INFO("renderer receive down event");
48 auto uiContent = uiContent_.lock();
49 if (!uiContent) {
50 HILOG_ERROR("uiContent is nullptr");
51 return;
52 }
53
54 struct FormSerializedResultData {
55 std::mutex mtx;
56 std::condition_variable cv;
57 };
58
59 std::shared_ptr<FormSerializedResultData> serializedResultData = std::make_shared<FormSerializedResultData>();
60 auto callback = [serializedResultData]() {
61 std::unique_lock<std::mutex> lock(serializedResultData->mtx);
62 serializedResultData->cv.notify_all();
63 };
64 {
65 std::unique_lock<std::mutex> lock(serializedResultData->mtx);
66 uiContent->ProcessPointerEventWithCallback(pointerEvent, callback);
67 if (serializedResultData->cv.wait_for(lock, std::chrono::milliseconds(PROCESS_WAIT_TIME)) ==
68 std::cv_status::timeout) {
69 HILOG_ERROR("formRender ProcessPointerEvent dispatch timeout");
70 } else {
71 serializedGesture = uiContent->GetFormSerializedGesture();
72 }
73 }
74 } else {
75 handler->PostTask([content = uiContent_, pointerEvent]() {
76 auto uiContent = content.lock();
77 if (!uiContent) {
78 HILOG_ERROR("uiContent is nullptr");
79 return;
80 }
81
82 uiContent->ProcessPointerEvent(pointerEvent);
83 });
84 }
85 }
86
IsAllowUpdate()87 bool FormRendererDispatcherImpl::IsAllowUpdate()
88 {
89 return allowUpdate_;
90 }
91
SetAllowUpdate(bool allowUpdate)92 void FormRendererDispatcherImpl::SetAllowUpdate(bool allowUpdate)
93 {
94 allowUpdate_ = allowUpdate;
95 }
96
DispatchSurfaceChangeEvent(float width,float height,float borderWidth)97 void FormRendererDispatcherImpl::DispatchSurfaceChangeEvent(float width, float height, float borderWidth)
98 {
99 auto handler = eventHandler_.lock();
100 if (!handler) {
101 HILOG_ERROR("eventHandler is nullptr");
102 return;
103 }
104
105 handler->PostTask([content = uiContent_, width, height, borderWidth]() {
106 HILOG_INFO("Root node update, width: %{public}f, height: %{public}f.", width, height);
107 auto uiContent = content.lock();
108 if (!uiContent) {
109 HILOG_ERROR("uiContent is nullptr");
110 return;
111 }
112 float uiWidth = width - borderWidth * DOUBLE;
113 float uiHeight = height - borderWidth * DOUBLE;
114 uiContent->SetFormWidth(uiWidth);
115 uiContent->SetFormHeight(uiHeight);
116 uiContent->OnFormSurfaceChange(uiWidth, uiHeight);
117 });
118
119 auto formRenderer = formRenderer_.lock();
120 if (!formRenderer) {
121 HILOG_ERROR("formRenderer is nullptr");
122 return;
123 }
124 formRenderer->OnSurfaceChange(width, height, borderWidth);
125 }
126
SetObscured(bool isObscured)127 void FormRendererDispatcherImpl::SetObscured(bool isObscured)
128 {
129 auto handler = eventHandler_.lock();
130 if (!handler) {
131 HILOG_ERROR("eventHandler is nullptr");
132 return;
133 }
134 handler->PostTask([content = uiContent_, isObscured]() {
135 auto uiContent = content.lock();
136 if (!uiContent) {
137 HILOG_ERROR("uiContent is nullptr");
138 return;
139 }
140 HILOG_INFO("Update ChangeSensitiveNodes: %{public}s", isObscured ? "true" : "false");
141 uiContent->ChangeSensitiveNodes(isObscured);
142 });
143 }
144
OnAccessibilityChildTreeRegister(uint32_t windowId,int32_t treeId,int64_t accessibilityId)145 void FormRendererDispatcherImpl::OnAccessibilityChildTreeRegister(
146 uint32_t windowId, int32_t treeId, int64_t accessibilityId)
147 {
148 auto handler = eventHandler_.lock();
149 if (!handler) {
150 HILOG_ERROR("eventHandler is nullptr");
151 return;
152 }
153
154 handler->PostTask([content = uiContent_, formRenderer = formRenderer_, windowId, treeId, accessibilityId]() {
155 auto uiContent = content.lock();
156 if (!uiContent) {
157 HILOG_ERROR("uiContent is nullptr");
158 return;
159 }
160 HILOG_INFO("OnAccessibilityChildTreeRegister: %{public}d %{public}" PRId64, treeId, accessibilityId);
161 uiContent->RegisterAccessibilityChildTree(windowId, treeId, accessibilityId);
162 uiContent->SetAccessibilityGetParentRectHandler([formRenderer](int32_t &top, int32_t &left) {
163 auto formRendererPtr = formRenderer.lock();
164 if (!formRendererPtr) {
165 HILOG_ERROR("formRenderer is nullptr");
166 return;
167 }
168 formRendererPtr->GetRectRelativeToWindow(top, left);
169 });
170 });
171 }
172
OnAccessibilityChildTreeDeregister()173 void FormRendererDispatcherImpl::OnAccessibilityChildTreeDeregister()
174 {
175 auto handler = eventHandler_.lock();
176 if (!handler) {
177 HILOG_ERROR("eventHandler is nullptr");
178 return;
179 }
180 handler->PostTask([content = uiContent_]() {
181 auto uiContent = content.lock();
182 if (!uiContent) {
183 HILOG_ERROR("uiContent is nullptr");
184 return;
185 }
186 HILOG_INFO("OnAccessibilityChildTreeDeregister");
187 uiContent->DeregisterAccessibilityChildTree();
188 });
189 }
190
OnAccessibilityDumpChildInfo(const std::vector<std::string> & params,std::vector<std::string> & info)191 void FormRendererDispatcherImpl::OnAccessibilityDumpChildInfo(
192 const std::vector<std::string>& params, std::vector<std::string>& info)
193 {
194 auto handler = eventHandler_.lock();
195 if (!handler) {
196 HILOG_ERROR("eventHandler is nullptr");
197 return;
198 }
199 handler->PostSyncTask([content = uiContent_, params, &info]() {
200 auto uiContent = content.lock();
201 if (!uiContent) {
202 HILOG_ERROR("uiContent is nullptr");
203 return;
204 }
205 HILOG_INFO("OnAccessibilityDumpChildInfo");
206 uiContent->AccessibilityDumpChildInfo(params, info);
207 });
208 }
209
OnAccessibilityTransferHoverEvent(float pointX,float pointY,int32_t sourceType,int32_t eventType,int64_t timeMs)210 void FormRendererDispatcherImpl::OnAccessibilityTransferHoverEvent(float pointX, float pointY, int32_t sourceType,
211 int32_t eventType, int64_t timeMs)
212 {
213 auto handler = eventHandler_.lock();
214 if (!handler) {
215 HILOG_ERROR("eventHandler is nullptr");
216 return;
217 }
218 handler->PostTask([content = uiContent_, pointX, pointY, sourceType, eventType, timeMs]() {
219 auto uiContent = content.lock();
220 if (!uiContent) {
221 HILOG_ERROR("uiContent is nullptr");
222 return;
223 }
224 HILOG_INFO("OnAccessibilityTransferHoverEvent");
225 uiContent->HandleAccessibilityHoverEvent(pointX, pointY, sourceType, eventType, timeMs);
226 });
227 }
228 } // namespace Ace
229 } // namespace OHOS
230