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