• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_WEB_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_WEB_PATTERN_H
18 
19 #include <optional>
20 #include <string>
21 #include <utility>
22 
23 #include "base/memory/referenced.h"
24 #include "base/utils/utils.h"
25 #include "core/components/web/web_property.h"
26 #include "core/components/web/resource/web_delegate.h"
27 #include "core/components_ng/manager/select_overlay/select_overlay_manager.h"
28 #include "core/components_ng/manager/select_overlay/select_overlay_proxy.h"
29 #include "core/components_ng/pattern/pattern.h"
30 #include "core/components_ng/pattern/web/web_event_hub.h"
31 #include "core/components_ng/pattern/web/web_layout_algorithm.h"
32 #include "core/components_ng/pattern/web/web_paint_property.h"
33 #include "core/components_ng/pattern/web/web_pattern_property.h"
34 #include "core/components_ng/property/property.h"
35 #include "core/components_ng/gestures/recognizers/pan_recognizer.h"
36 #include "core/components_ng/render/render_surface.h"
37 #include "nweb_handler.h"
38 
39 namespace OHOS::Ace::NG {
40 namespace {
41 
42 struct MouseClickInfo {
43     double x = -1;
44     double y = -1;
45     TimeStamp start;
46 };
47 
48 #ifdef OHOS_STANDARD_SYSTEM
49 struct TouchInfo {
50     double x = -1;
51     double y = -1;
52     int32_t id = -1;
53 };
54 
55 struct TouchHandleState {
56     int32_t id = -1;
57     int32_t x = -1;
58     int32_t y = -1;
59     int32_t edge_height = 0;
60 };
61 
62 enum WebOverlayType { INSERT_OVERLAY, SELECTION_OVERLAY, INVALID_OVERLAY };
63 #endif
64 } // namespace
65 
66 class WebPattern : public Pattern {
67     DECLARE_ACE_TYPE(WebPattern, Pattern);
68 
69 public:
70     using SetWebIdCallback = std::function<void(int32_t)>;
71     using JsProxyCallback = std::function<void()>;
72 
73     WebPattern();
74     WebPattern(std::string webSrc, const RefPtr<WebController>& webController);
75     WebPattern(std::string webSrc, const SetWebIdCallback& setWebIdCallback);
76 
77     ~WebPattern() override;
78 
79     enum class VkState {
80         VK_NONE,
81         VK_SHOW,
82         VK_HIDE
83     };
84 
GetSurfaceNodeName()85     std::optional<std::string> GetSurfaceNodeName() const override
86     {
87         return "RosenWeb";
88     }
89 
IsAtomicNode()90     bool IsAtomicNode() const override
91     {
92         return true;
93     }
94 
CreateEventHub()95     RefPtr<EventHub> CreateEventHub() override
96     {
97         return MakeRefPtr<WebEventHub>();
98     }
99 
100     void OnModifyDone() override;
101 
SetWebSrc(const std::string & webSrc)102     void SetWebSrc(const std::string& webSrc)
103     {
104         if (webSrc_ != webSrc_) {
105             OnWebSrcUpdate();
106             webSrc_ = webSrc;
107         }
108         if (webPaintProperty_) {
109             webPaintProperty_->SetWebPaintData(webSrc);
110         }
111     }
112 
GetWebSrc()113     const std::optional<std::string>& GetWebSrc() const
114     {
115         return webSrc_;
116     }
117 
SetPopup(bool popup)118     void SetPopup(bool popup)
119     {
120         isPopup_ = popup;
121     }
122 
SetParentNWebId(int32_t parentNWebId)123     void SetParentNWebId(int32_t parentNWebId)
124     {
125         parentNWebId_ = parentNWebId;
126     }
127 
SetWebData(const std::string & webData)128     void SetWebData(const std::string& webData)
129     {
130         if (webData_ != webData) {
131             webData_ = webData;
132             OnWebDataUpdate();
133         }
134         if (webPaintProperty_) {
135             webPaintProperty_->SetWebPaintData(webData);
136         }
137     }
138 
GetWebData()139     const std::optional<std::string>& GetWebData() const
140     {
141         return webData_;
142     }
143 
SetCustomScheme(const std::string & scheme)144     void SetCustomScheme(const std::string& scheme)
145     {
146         customScheme_ = scheme;
147     }
148 
GetCustomScheme()149     const std::optional<std::string>& GetCustomScheme() const
150     {
151         return customScheme_;
152     }
153 
SetWebController(const RefPtr<WebController> & webController)154     void SetWebController(const RefPtr<WebController>& webController)
155     {
156         // TODO: add web controller diff function.
157         webController_ = webController;
158     }
159 
GetWebController()160     RefPtr<WebController> GetWebController() const
161     {
162         return webController_;
163     }
164 
SetSetWebIdCallback(SetWebIdCallback && SetIdCallback)165     void SetSetWebIdCallback(SetWebIdCallback&& SetIdCallback)
166     {
167         setWebIdCallback_ = std::move(SetIdCallback);
168     }
169 
GetSetWebIdCallback()170     SetWebIdCallback GetSetWebIdCallback() const
171     {
172         return setWebIdCallback_;
173     }
174 
SetJsProxyCallback(JsProxyCallback && jsProxyCallback)175     void SetJsProxyCallback(JsProxyCallback&& jsProxyCallback)
176     {
177         jsProxyCallback_ = std::move(jsProxyCallback);
178     }
179 
CallJsProxyCallback()180     void CallJsProxyCallback()
181     {
182         if (jsProxyCallback_) {
183             jsProxyCallback_();
184         }
185     }
186 
GetWebEventHub()187     RefPtr<WebEventHub> GetWebEventHub()
188     {
189         return GetEventHub<WebEventHub>();
190     }
191 
GetFocusPattern()192     FocusPattern GetFocusPattern() const override
193     {
194         return { FocusType::NODE, true };
195     }
196 
CreatePaintProperty()197     RefPtr<PaintProperty> CreatePaintProperty() override
198     {
199         if (!webPaintProperty_) {
200             webPaintProperty_ = MakeRefPtr<WebPaintProperty>();
201             if (!webPaintProperty_) {
202                 LOGE("MakeRefPtr failed return null");
203             }
204         }
205         return webPaintProperty_;
206     }
207 
CreateLayoutAlgorithm()208     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
209     {
210         return MakeRefPtr<WebLayoutAlgorithm>();
211     }
212 
213     ACE_DEFINE_PROPERTY_GROUP(WebProperty, WebPatternProperty);
214     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, JsEnabled, bool);
215     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, MediaPlayGestureAccess, bool);
216     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, FileAccessEnabled, bool);
217     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, OnLineImageAccessEnabled, bool);
218     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, DomStorageAccessEnabled, bool);
219     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, ImageAccessEnabled, bool);
220     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, MixedMode, MixedModeContent);
221     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, ZoomAccessEnabled, bool);
222     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, GeolocationAccessEnabled, bool);
223     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, UserAgent, std::string);
224     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, CacheMode, WebCacheMode);
225     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, OverviewModeAccessEnabled, bool);
226     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, FileFromUrlAccessEnabled, bool);
227     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, DatabaseAccessEnabled, bool);
228     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, TextZoomRatio, int32_t);
229     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebDebuggingAccessEnabled, bool);
230     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, BackgroundColor, int32_t);
231     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, InitialScale, float);
232     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, PinchSmoothModeEnabled, bool);
233     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, MultiWindowAccessEnabled, bool);
234     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebCursiveFont, std::string);
235     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebFantasyFont, std::string);
236     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebFixedFont, std::string);
237     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebSansSerifFont, std::string);
238     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebSerifFont, std::string);
239     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, WebStandardFont, std::string);
240     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, DefaultFixedFontSize, int32_t);
241     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, DefaultFontSize, int32_t);
242     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, MinFontSize, int32_t);
243     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, MinLogicalFontSize, int32_t);
244     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, BlockNetwork, bool);
245     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, DarkMode, WebDarkMode);
246     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, ForceDarkAccess, bool);
247     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, HorizontalScrollBarAccessEnabled, bool);
248     ACE_DEFINE_PROPERTY_FUNC_WITH_GROUP(WebProperty, VerticalScrollBarAccessEnabled, bool);
249 
250     void RequestFullScreen();
251     void ExitFullScreen();
IsFullScreen()252     bool IsFullScreen() const
253     {
254         return isFullScreen_;
255     }
256     bool RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
257         std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback);
258     void OnQuickMenuDismissed();
259     void OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
260         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
261         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
262     bool OnCursorChange(const OHOS::NWeb::CursorType& type, const OHOS::NWeb::NWebCursorInfo& info);
263     void OnSelectPopupMenu(std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,
264         std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback);
265     void UpdateTouchHandleForOverlay();
IsSelectOverlayDragging()266     bool IsSelectOverlayDragging()
267     {
268         return selectOverlayDragging_;
269     }
SetSelectOverlayDragging(bool selectOverlayDragging)270     void SetSelectOverlayDragging(bool selectOverlayDragging)
271     {
272         selectOverlayDragging_ = selectOverlayDragging;
273     }
274     void UpdateLocale();
SetSelectPopupMenuShowing(bool showing)275     void SetSelectPopupMenuShowing(bool showing)
276     {
277         selectPopupMenuShowing_ = showing;
278     }
279 
280 private:
281     void RegistVirtualKeyBoardListener();
282     bool ProcessVirtualKeyBoard(int32_t width, int32_t height, double keyboard);
283     void UpdateWebLayoutSize(int32_t width, int32_t height);
284     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
285 
286     void OnAttachToFrameNode() override;
287     void OnDetachFromFrameNode(FrameNode* frameNode) override;
288     void OnWindowShow() override;
289     void OnWindowHide() override;
290     void OnInActive() override;
291     void OnActive() override;
292     void OnVisibleChange(bool isVisible) override;
293     void OnAreaChangedInner() override;
294 
295     void OnWebSrcUpdate();
296     void OnWebDataUpdate();
297     void OnJsEnabledUpdate(bool value);
298     void OnMediaPlayGestureAccessUpdate(bool value);
299     void OnFileAccessEnabledUpdate(bool value);
300     void OnOnLineImageAccessEnabledUpdate(bool value);
301     void OnDomStorageAccessEnabledUpdate(bool value);
302     void OnImageAccessEnabledUpdate(bool value);
303     void OnMixedModeUpdate(MixedModeContent value);
304     void OnZoomAccessEnabledUpdate(bool value);
305     void OnGeolocationAccessEnabledUpdate(bool value);
306     void OnUserAgentUpdate(const std::string& value);
307     void OnCacheModeUpdate(WebCacheMode value);
308     void OnOverviewModeAccessEnabledUpdate(bool value);
309     void OnFileFromUrlAccessEnabledUpdate(bool value);
310     void OnDatabaseAccessEnabledUpdate(bool value);
311     void OnTextZoomRatioUpdate(int32_t value);
312     void OnWebDebuggingAccessEnabledUpdate(bool value);
313     void OnPinchSmoothModeEnabledUpdate(bool value);
314     void OnBackgroundColorUpdate(int32_t value);
315     void OnInitialScaleUpdate(float value);
316     void OnMultiWindowAccessEnabledUpdate(bool value);
317     void OnWebCursiveFontUpdate(const std::string& value);
318     void OnWebFantasyFontUpdate(const std::string& value);
319     void OnWebFixedFontUpdate(const std::string& value);
320     void OnWebSerifFontUpdate(const std::string& value);
321     void OnWebSansSerifFontUpdate(const std::string& value);
322     void OnWebStandardFontUpdate(const std::string& value);
323     void OnDefaultFixedFontSizeUpdate(int32_t value);
324     void OnDefaultFontSizeUpdate(int32_t value);
325     void OnMinFontSizeUpdate(int32_t value);
326     void OnMinLogicalFontSizeUpdate(int32_t value);
327     void OnBlockNetworkUpdate(bool value);
328     void OnDarkModeUpdate(WebDarkMode mode);
329     void OnForceDarkAccessUpdate(bool access);
330     void OnHorizontalScrollBarAccessEnabledUpdate(bool value);
331     void OnVerticalScrollBarAccessEnabledUpdate(bool value);
332 
333     void InitEvent();
334     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
335     void InitMouseEvent(const RefPtr<InputEventHub>& inputHub);
336     void InitCommonDragDropEvent(const RefPtr<GestureEventHub>& gestureHub);
337     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
338     void HandleDragMove(const GestureEvent& event);
339     void InitDragEvent(const RefPtr<GestureEventHub>& gestureHub);
340     void HandleDragStart(const GestureEvent& info);
341     void HandleDragUpdate(const GestureEvent& info);
342     void HandleDragEnd(const GestureEvent& info);
343     void HandleDragCancel();
344     bool GenerateDragDropInfo(NG::DragDropInfo& dragDropInfo);
345     void HandleMouseEvent(MouseInfo& info);
346     void WebOnMouseEvent(const MouseInfo& info);
347     bool HandleDoubleClickEvent(const MouseInfo& info);
348     void SendDoubleClickEvent(const MouseClickInfo& info);
349     void InitFocusEvent(const RefPtr<FocusHub>& focusHub);
350     void HandleFocusEvent();
351     void HandleBlurEvent(const BlurReason& blurReason);
352     bool HandleKeyEvent(const KeyEvent& keyEvent);
353     bool WebOnKeyEvent(const KeyEvent& keyEvent);
354     void WebRequestFocus();
355 
356     void HandleTouchDown(const TouchEventInfo& info, bool fromOverlay);
357 
358     void HandleTouchUp(const TouchEventInfo& info, bool fromOverlay);
359 
360     void HandleTouchMove(const TouchEventInfo& info, bool fromOverlay);
361 
362     void HandleTouchCancel(const TouchEventInfo& info);
363 
364     bool IsTouchHandleValid(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle);
365     bool IsTouchHandleShow(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle);
366 #ifdef OHOS_STANDARD_SYSTEM
367     WebOverlayType GetTouchHandleOverlayType(
368         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
369         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
370         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
371 #endif
372     void RegisterSelectOverlayCallback(SelectOverlayInfo& selectInfo,
373         std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
374         std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback);
375     void RegisterSelectOverlayEvent(SelectOverlayInfo& selectInfo);
376     void CloseSelectOverlay();
377     RectF ComputeTouchHandleRect(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> touchHandle);
378     std::optional<OffsetF> GetCoordinatePoint();
379     void RegisterSelectPopupCallback(RefPtr<FrameNode>& menu,
380         std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback);
381     OffsetF GetSelectPopupPostion(const OHOS::NWeb::SelectMenuBound& bounds);
382 
383     struct TouchInfo {
384         float x = -1.0f;
385         float y = -1.0f;
386         int32_t id = -1;
387     };
388     static bool ParseTouchInfo(const TouchEventInfo& info, std::list<TouchInfo>& touchInfos);
389     void InitEnhanceSurfaceFlag();
390     void UpdateBackgroundColorRightNow(int32_t color);
391 
392     std::optional<std::string> webSrc_;
393     std::optional<std::string> webData_;
394     std::optional<std::string> customScheme_;
395     RefPtr<WebController> webController_;
396     SetWebIdCallback setWebIdCallback_ = nullptr;
397     JsProxyCallback jsProxyCallback_ = nullptr;
398     RefPtr<RenderSurface> renderSurface_ = RenderSurface::Create();
399     RefPtr<TouchEventImpl> touchEvent_;
400     RefPtr<InputEvent> mouseEvent_;
401     RefPtr<PanEvent> panEvent_ = nullptr;
402     RefPtr<SelectOverlayProxy> selectOverlayProxy_ = nullptr;
403     RefPtr<WebPaintProperty> webPaintProperty_ = nullptr;
404     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle_ = nullptr;
405     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle_ = nullptr;
406     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle_ = nullptr;
407     float selectHotZone_ = 10.0f;
408     RefPtr<DragEvent> dragEvent_;
409     bool isUrlLoaded_ = false;
410     std::queue<MouseClickInfo> doubleClickQueue_;
411     bool isFullScreen_ = false;
412     bool needOnFocus_ = false;
413     Size drawSize_;
414     Size drawSizeCache_;
415     bool needUpdateWeb_ = true;
416     bool isFocus_ = false;
417     VkState isVirtualKeyBoardShow_ { VkState::VK_NONE };
418     bool isDragging_ = false;
419     bool isW3cDragEvent_ = false;
420     bool isWindowShow_ = true;
421     bool isActive_ = true;
422     bool isEnhanceSurface_ = false;
423     OffsetF webOffset_;
424     SelectMenuInfo selectMenuInfo_;
425     bool selectOverlayDragging_ = false;
426     bool selectPopupMenuShowing_ = false;
427     bool isPopup_ = false;
428     int32_t parentNWebId_ = -1;
429     RefPtr<WebDelegate> delegate_;
430     RefPtr<WebDelegateObserver> observer_;
431     ACE_DISALLOW_COPY_AND_MOVE(WebPattern);
432 };
433 } // namespace OHOS::Ace::NG
434 
435 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_WEB_PATTERN_H
436