1 /* 2 * Copyright (c) 2021-2024 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_WEB_RESOURCE_WEB_DELEGATE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_DELEGATE_H 18 19 #include <list> 20 #include <map> 21 22 #include "ability_runtime/context/environment_callback.h" 23 #include "base/memory/referenced.h" 24 #include "core/components_ng/render/render_surface.h" 25 #include "core/pipeline/pipeline_base.h" 26 #if defined (OHOS_STANDARD_SYSTEM) && defined (ENABLE_ROSEN_BACKEND) 27 #include <ui/rs_surface_node.h> 28 #endif 29 30 #include <EGL/egl.h> 31 #include <EGL/eglext.h> 32 #include <GLES3/gl3.h> 33 #include "base/image/pixel_map.h" 34 #include "core/common/recorder/event_recorder.h" 35 #include "core/common/container.h" 36 #include "core/components/common/layout/constants.h" 37 #include "core/components/web/resource/web_client_impl.h" 38 #include "core/components/web/resource/web_resource.h" 39 #include "core/components/web/web_component.h" 40 #include "core/components/web/web_event.h" 41 #include "core/components_ng/pattern/web/web_event_hub.h" 42 #include "core/components_ng/pattern/web/web_pattern.h" 43 #include "nweb_accessibility_node_info.h" 44 #include "surface_delegate.h" 45 #ifdef OHOS_STANDARD_SYSTEM 46 #include "nweb_handler.h" 47 #include "nweb_helper.h" 48 #include "nweb_hit_testresult.h" 49 #include "app_mgr_client.h" 50 #ifdef ENABLE_ROSEN_BACKEND 51 #include "surface.h" 52 #include "core/components_ng/render/adapter/rosen_render_surface.h" 53 #endif 54 #include "wm/window.h" 55 #endif 56 57 namespace OHOS::Ace { 58 59 typedef struct WindowsSurfaceInfoTag { 60 void* window; 61 EGLDisplay display; 62 EGLContext context; 63 EGLSurface surface; 64 } WindowsSurfaceInfo; 65 66 typedef NWeb::NativeArkWebOnJavaScriptProxyCallback NativeMethodCallback; 67 68 class WebMessagePortOhos : public WebMessagePort { DECLARE_ACE_TYPE(WebMessagePortOhos,WebMessagePort)69 DECLARE_ACE_TYPE(WebMessagePortOhos, WebMessagePort) 70 71 public: 72 WebMessagePortOhos(WeakPtr<WebDelegate> webDelegate) : webDelegate_(webDelegate) {} 73 WebMessagePortOhos() = default; 74 ~WebMessagePortOhos() = default; 75 76 void Close() override; 77 void PostMessage(std::string& data) override; 78 void SetWebMessageCallback(std::function<void(const std::string&)>&& callback) override; 79 void SetPortHandle(std::string& handle) override; 80 std::string GetPortHandle() override; 81 82 private: 83 WeakPtr<WebDelegate> webDelegate_; 84 std::string handle_; 85 }; 86 87 class ConsoleLogOhos : public WebConsoleLog { DECLARE_ACE_TYPE(ConsoleLogOhos,WebConsoleLog)88 DECLARE_ACE_TYPE(ConsoleLogOhos, WebConsoleLog) 89 90 public: 91 ConsoleLogOhos(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message) : message_(message) {} 92 93 int GetLineNumber() override; 94 95 std::string GetLog() override; 96 97 int GetLogLevel() override; 98 99 std::string GetSourceId() override; 100 101 private: 102 std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message_; 103 }; 104 105 class ResultOhos : public Result { DECLARE_ACE_TYPE(ResultOhos,Result)106 DECLARE_ACE_TYPE(ResultOhos, Result) 107 108 public: 109 ResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) : result_(result) {} 110 111 void Confirm() override; 112 void Confirm(const std::string& message) override; 113 void Cancel() override; 114 115 private: 116 std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result_; 117 }; 118 119 class FullScreenExitHandlerOhos : public FullScreenExitHandler { DECLARE_ACE_TYPE(FullScreenExitHandlerOhos,FullScreenExitHandler)120 DECLARE_ACE_TYPE(FullScreenExitHandlerOhos, FullScreenExitHandler) 121 122 public: 123 FullScreenExitHandlerOhos(std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler, 124 WeakPtr<WebDelegate> webDelegate) : handler_(handler), webDelegate_(webDelegate) {} 125 void ExitFullScreen() override; 126 private: 127 std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler_; 128 WeakPtr<WebDelegate> webDelegate_; 129 }; 130 131 class WebCustomKeyboardHandlerOhos : public WebCustomKeyboardHandler { DECLARE_ACE_TYPE(WebCustomKeyboardHandlerOhos,WebCustomKeyboardHandler)132 DECLARE_ACE_TYPE(WebCustomKeyboardHandlerOhos, WebCustomKeyboardHandler) 133 134 public: 135 WebCustomKeyboardHandlerOhos(std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler) : 136 keyboardHandler_(keyboardHandler) {} 137 InsertText(const std::string & text)138 void InsertText(const std::string &text) override 139 { 140 if (keyboardHandler_) { 141 keyboardHandler_->InsertText(text); 142 } 143 } 144 DeleteForward(int32_t length)145 void DeleteForward(int32_t length) override 146 { 147 if (keyboardHandler_) { 148 keyboardHandler_->DeleteForward(length); 149 } 150 } 151 DeleteBackward(int32_t length)152 void DeleteBackward(int32_t length) override 153 { 154 if (keyboardHandler_) { 155 keyboardHandler_->DeleteBackward(length); 156 } 157 } 158 SendFunctionKey(int32_t key)159 void SendFunctionKey(int32_t key) override 160 { 161 if (keyboardHandler_) { 162 keyboardHandler_->SendFunctionKey(key); 163 } 164 } 165 Close()166 void Close() override 167 { 168 if (keyboardHandler_) { 169 keyboardHandler_->Close(); 170 } 171 } 172 173 private: 174 std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler_; 175 }; 176 177 class AuthResultOhos : public AuthResult { DECLARE_ACE_TYPE(AuthResultOhos,AuthResult)178 DECLARE_ACE_TYPE(AuthResultOhos, AuthResult) 179 180 public: 181 AuthResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSHttpAuthResult> result) : result_(result) {} 182 183 bool Confirm(std::string& userName, std::string& pwd) override; 184 bool IsHttpAuthInfoSaved() override; 185 void Cancel() override; 186 187 private: 188 std::shared_ptr<OHOS::NWeb::NWebJSHttpAuthResult> result_; 189 }; 190 191 class SslErrorResultOhos : public SslErrorResult { DECLARE_ACE_TYPE(SslErrorResultOhos,SslErrorResult)192 DECLARE_ACE_TYPE(SslErrorResultOhos, SslErrorResult) 193 194 public: 195 SslErrorResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSSslErrorResult> result) : result_(result) {} 196 197 void HandleConfirm() override; 198 void HandleCancel() override; 199 200 private: 201 std::shared_ptr<OHOS::NWeb::NWebJSSslErrorResult> result_; 202 }; 203 204 class AllSslErrorResultOhos : public AllSslErrorResult { DECLARE_ACE_TYPE(AllSslErrorResultOhos,AllSslErrorResult)205 DECLARE_ACE_TYPE(AllSslErrorResultOhos, AllSslErrorResult) 206 207 public: 208 AllSslErrorResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSAllSslErrorResult> result) : result_(result) {} 209 210 void HandleConfirm() override; 211 void HandleCancel() override; 212 213 private: 214 std::shared_ptr<OHOS::NWeb::NWebJSAllSslErrorResult> result_; 215 }; 216 217 class SslSelectCertResultOhos : public SslSelectCertResult { DECLARE_ACE_TYPE(SslSelectCertResultOhos,SslSelectCertResult)218 DECLARE_ACE_TYPE(SslSelectCertResultOhos, SslSelectCertResult) 219 220 public: 221 explicit SslSelectCertResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSSslSelectCertResult> result) 222 : result_(result) {} 223 224 void HandleConfirm(const std::string& privateKeyFile, const std::string& certChainFile) override; 225 226 void HandleCancel() override; 227 228 void HandleIgnore() override; 229 private: 230 std::shared_ptr<OHOS::NWeb::NWebJSSslSelectCertResult> result_; 231 }; 232 233 class FileSelectorParamOhos : public WebFileSelectorParam { DECLARE_ACE_TYPE(FileSelectorParamOhos,WebFileSelectorParam)234 DECLARE_ACE_TYPE(FileSelectorParamOhos, WebFileSelectorParam) 235 236 public: 237 FileSelectorParamOhos(std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> param) : param_(param) {} 238 239 std::string GetTitle() override; 240 int GetMode() override; 241 std::string GetDefaultFileName() override; 242 std::vector<std::string> GetAcceptType() override; 243 bool IsCapture() override; 244 std::vector<std::string> GetMimeType() override; 245 246 private: 247 std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> param_; 248 }; 249 250 class FileSelectorResultOhos : public FileSelectorResult { DECLARE_ACE_TYPE(FileSelectorResultOhos,FileSelectorResult)251 DECLARE_ACE_TYPE(FileSelectorResultOhos, FileSelectorResult) 252 253 public: 254 FileSelectorResultOhos(std::shared_ptr<OHOS::NWeb::NWebStringVectorValueCallback> callback) : callback_(callback) {} 255 256 void HandleFileList(std::vector<std::string>& result) override; 257 258 private: 259 std::shared_ptr<OHOS::NWeb::NWebStringVectorValueCallback> callback_; 260 }; 261 262 class ContextMenuParamOhos : public WebContextMenuParam { DECLARE_ACE_TYPE(ContextMenuParamOhos,WebContextMenuParam)263 DECLARE_ACE_TYPE(ContextMenuParamOhos, WebContextMenuParam) 264 265 public: 266 ContextMenuParamOhos(std::shared_ptr<OHOS::NWeb::NWebContextMenuParams> param) : param_(param) {} 267 268 int32_t GetXCoord() const override; 269 int32_t GetYCoord() const override; 270 std::string GetLinkUrl() const override; 271 std::string GetUnfilteredLinkUrl() const override; 272 std::string GetSourceUrl() const override; 273 bool HasImageContents() const override; 274 bool IsEditable() const override; 275 int GetEditStateFlags() const override; 276 int GetSourceType() const override; 277 int GetMediaType() const override; 278 int GetInputFieldType() const override; 279 std::string GetSelectionText() const override; 280 void GetImageRect(int32_t& x, int32_t& y, int32_t& width, int32_t& height) const override; 281 282 private: 283 std::shared_ptr<OHOS::NWeb::NWebContextMenuParams> param_; 284 }; 285 286 class ContextMenuResultOhos : public ContextMenuResult { DECLARE_ACE_TYPE(ContextMenuResultOhos,ContextMenuResult)287 DECLARE_ACE_TYPE(ContextMenuResultOhos, ContextMenuResult) 288 289 public: 290 ContextMenuResultOhos(std::shared_ptr<OHOS::NWeb::NWebContextMenuCallback> callback) : callback_(callback) {} 291 292 void Cancel() const override; 293 void CopyImage() const override; 294 void Copy() const override; 295 void Paste() const override; 296 void Cut() const override; 297 void SelectAll() const override; 298 299 private: 300 std::shared_ptr<OHOS::NWeb::NWebContextMenuCallback> callback_; 301 }; 302 303 class WebGeolocationOhos : public WebGeolocation { DECLARE_ACE_TYPE(WebGeolocationOhos,WebGeolocation)304 DECLARE_ACE_TYPE(WebGeolocationOhos, WebGeolocation) 305 306 public: 307 WebGeolocationOhos( 308 const std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface>& 309 callback, bool incognito) 310 : geolocationCallback_(callback), incognito_(incognito) {} 311 312 void Invoke(const std::string& origin, const bool& allow, const bool& retain) override; 313 314 private: 315 std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> geolocationCallback_; 316 bool incognito_ = false; 317 }; 318 319 class WebPermissionRequestOhos : public WebPermissionRequest { DECLARE_ACE_TYPE(WebPermissionRequestOhos,WebPermissionRequest)320 DECLARE_ACE_TYPE(WebPermissionRequestOhos, WebPermissionRequest) 321 322 public: 323 WebPermissionRequestOhos(const std::shared_ptr<OHOS::NWeb::NWebAccessRequest>& request) : request_(request) {} 324 325 void Deny() const override; 326 327 std::string GetOrigin() const override; 328 329 std::vector<std::string> GetResources() const override; 330 331 void Grant(std::vector<std::string>& resources) const override; 332 333 private: 334 std::shared_ptr<OHOS::NWeb::NWebAccessRequest> request_; 335 }; 336 337 class NWebScreenCaptureConfigImpl : public OHOS::NWeb::NWebScreenCaptureConfig { 338 public: 339 NWebScreenCaptureConfigImpl() = default; 340 ~NWebScreenCaptureConfigImpl() = default; 341 GetMode()342 int32_t GetMode() override 343 { 344 return mode_; 345 } 346 SetMode(int32_t mode)347 void SetMode(int32_t mode) 348 { 349 mode_ = mode; 350 } 351 GetSourceId()352 int32_t GetSourceId() override 353 { 354 return source_id_; 355 } 356 SetSourceId(int32_t source_id)357 void SetSourceId(int32_t source_id) 358 { 359 source_id_ = source_id; 360 } 361 362 private: 363 int32_t mode_ = 0; 364 int32_t source_id_ = -1; 365 }; 366 367 class WebScreenCaptureRequestOhos : public WebScreenCaptureRequest { DECLARE_ACE_TYPE(WebScreenCaptureRequestOhos,WebScreenCaptureRequest)368 DECLARE_ACE_TYPE(WebScreenCaptureRequestOhos, WebScreenCaptureRequest) 369 370 public: 371 WebScreenCaptureRequestOhos(const std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest>& request) 372 : request_(request) { 373 config_ = std::make_shared<NWebScreenCaptureConfigImpl>(); 374 } 375 376 void Deny() const override; 377 378 std::string GetOrigin() const override; 379 380 void SetCaptureMode(int32_t mode) override; 381 382 void SetSourceId(int32_t sourceId) override; 383 384 void Grant() const override; 385 386 private: 387 std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest> request_; 388 389 std::shared_ptr<NWebScreenCaptureConfigImpl> config_; 390 }; 391 392 class WebWindowNewHandlerOhos : public WebWindowNewHandler { DECLARE_ACE_TYPE(WebWindowNewHandlerOhos,WebWindowNewHandler)393 DECLARE_ACE_TYPE(WebWindowNewHandlerOhos, WebWindowNewHandler) 394 395 public: 396 WebWindowNewHandlerOhos(const std::shared_ptr<OHOS::NWeb::NWebControllerHandler>& handler, int32_t parentNWebId) 397 : handler_(handler), parentNWebId_(parentNWebId) {} 398 399 void SetWebController(int32_t id) override; 400 401 bool IsFrist() const override; 402 403 int32_t GetId() const override; 404 405 int32_t GetParentNWebId() const override; 406 407 private: 408 std::shared_ptr<OHOS::NWeb::NWebControllerHandler> handler_; 409 int32_t parentNWebId_ = -1; 410 }; 411 412 class WebAppLinkCallbackOhos : public WebAppLinkCallback { DECLARE_ACE_TYPE(WebAppLinkCallbackOhos,WebAppLinkCallback)413 DECLARE_ACE_TYPE(WebAppLinkCallbackOhos, WebAppLinkCallback) 414 public: 415 WebAppLinkCallbackOhos(const std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback>& callback) 416 : callback_(callback) {} 417 ContinueLoad()418 void ContinueLoad() override 419 { 420 if (callback_) { 421 callback_->ContinueLoad(); 422 } 423 } 424 CancelLoad()425 void CancelLoad() override 426 { 427 if (callback_) { 428 callback_->CancelLoad(); 429 } 430 } 431 432 private: 433 std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback> callback_; 434 }; 435 436 class DataResubmittedOhos : public DataResubmitted { DECLARE_ACE_TYPE(DataResubmittedOhos,DataResubmitted)437 DECLARE_ACE_TYPE(DataResubmittedOhos, DataResubmitted) 438 439 public: 440 DataResubmittedOhos(std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler) : handler_(handler) {} 441 void Resend() override; 442 void Cancel() override; 443 444 private: 445 std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler_; 446 }; 447 448 class FaviconReceivedOhos : public WebFaviconReceived { DECLARE_ACE_TYPE(FaviconReceivedOhos,WebFaviconReceived)449 DECLARE_ACE_TYPE(FaviconReceivedOhos, WebFaviconReceived) 450 451 public: 452 FaviconReceivedOhos( 453 const void* data, 454 size_t width, 455 size_t height, 456 OHOS::NWeb::ImageColorType colorType, 457 OHOS::NWeb::ImageAlphaType alphaType) 458 : data_(data), width_(width), height_(height), colorType_(colorType), alphaType_(alphaType) {} 459 const void* GetData() override; 460 size_t GetWidth() override; 461 size_t GetHeight() override; 462 int GetColorType() override; 463 int GetAlphaType() override; 464 465 private: 466 const void* data_ = nullptr; 467 size_t width_ = 0; 468 size_t height_ = 0; 469 OHOS::NWeb::ImageColorType colorType_ = OHOS::NWeb::ImageColorType::COLOR_TYPE_UNKNOWN; 470 OHOS::NWeb::ImageAlphaType alphaType_ = OHOS::NWeb::ImageAlphaType::ALPHA_TYPE_UNKNOWN; 471 }; 472 473 class WebSurfaceCallback : public OHOS::SurfaceDelegate::ISurfaceCallback { 474 public: WebSurfaceCallback(const WeakPtr<WebDelegate> & delegate)475 explicit WebSurfaceCallback(const WeakPtr<WebDelegate>& delegate) : delegate_(delegate) {} 476 ~WebSurfaceCallback() = default; 477 478 void OnSurfaceCreated(const OHOS::sptr<OHOS::Surface>& surface) override; 479 void OnSurfaceChanged(const OHOS::sptr<OHOS::Surface>& surface, int32_t width, int32_t height) override; 480 void OnSurfaceDestroyed() override; 481 private: 482 WeakPtr<WebDelegate> delegate_; 483 484 }; 485 486 enum class DragAction { 487 DRAG_START = 0, 488 DRAG_ENTER, 489 DRAG_LEAVE, 490 DRAG_OVER, 491 DRAG_DROP, 492 DRAG_END, 493 DRAG_CANCEL, 494 }; 495 496 namespace NG { 497 class WebPattern; 498 }; // namespace NG 499 500 class RenderWeb; 501 502 class NWebDragEventImpl : public OHOS::NWeb::NWebDragEvent { 503 public: NWebDragEventImpl(double x,double y,NWeb::DragAction action)504 NWebDragEventImpl(double x, double y, NWeb::DragAction action) : x_(x), y_(y), action_(action) {} 505 ~NWebDragEventImpl() = default; 506 GetX()507 double GetX() override 508 { 509 return x_; 510 } 511 GetY()512 double GetY() override 513 { 514 return y_; 515 } 516 GetAction()517 NWeb::DragAction GetAction() override 518 { 519 return action_; 520 } 521 522 private: 523 double x_ = 0.0; 524 double y_ = 0.0; 525 NWeb::DragAction action_ = NWeb::DragAction::DRAG_START; 526 }; 527 528 class NWebTouchPointInfoImpl : public OHOS::NWeb::NWebTouchPointInfo { 529 public: NWebTouchPointInfoImpl(int id,double x,double y)530 NWebTouchPointInfoImpl(int id, double x, double y) : id_(id), x_(x), y_(y) {} 531 ~NWebTouchPointInfoImpl() = default; 532 GetId()533 int GetId() override 534 { 535 return id_; 536 } 537 GetX()538 double GetX() override 539 { 540 return x_; 541 } 542 GetY()543 double GetY() override 544 { 545 return y_; 546 } 547 548 private: 549 int id_ = 0; 550 double x_ = 0; 551 double y_ = 0; 552 }; 553 554 class NWebScreenLockCallbackImpl : public OHOS::NWeb::NWebScreenLockCallback { 555 public: 556 explicit NWebScreenLockCallbackImpl(const WeakPtr<PipelineBase>& context); 557 ~NWebScreenLockCallbackImpl() = default; 558 559 void Handle(bool key) override; 560 561 private: 562 WeakPtr<PipelineBase> context_ = nullptr; 563 }; 564 565 class WebDelegateObserver : public virtual AceType { 566 DECLARE_ACE_TYPE(WebDelegateObserver, AceType); 567 public: WebDelegateObserver(const RefPtr<WebDelegate> & delegate,WeakPtr<PipelineBase> context)568 WebDelegateObserver(const RefPtr<WebDelegate>& delegate, WeakPtr<PipelineBase> context) 569 : delegate_(delegate), context_(context) 570 {} 571 ~WebDelegateObserver(); 572 void NotifyDestory(); 573 void OnAttachContext(const RefPtr<NG::PipelineContext> &context); 574 void OnDetachContext(); 575 576 private: 577 RefPtr<WebDelegate> delegate_; 578 WeakPtr<PipelineBase> context_; 579 }; 580 581 class GestureEventResultOhos : public GestureEventResult { 582 DECLARE_ACE_TYPE(GestureEventResultOhos, GestureEventResult); 583 584 public: GestureEventResultOhos(std::shared_ptr<OHOS::NWeb::NWebGestureEventResult> result)585 explicit GestureEventResultOhos(std::shared_ptr<OHOS::NWeb::NWebGestureEventResult> result) 586 : result_(result) {} 587 588 void SetGestureEventResult(bool result) override; 589 void SetGestureEventResult(bool result, bool stopPropagation) override; HasSendTask()590 bool HasSendTask() { return sendTask_; } SetSendTask()591 void SetSendTask() { sendTask_ = true; } GetEventResult()592 bool GetEventResult() { return eventResult_; } 593 594 private: 595 std::shared_ptr<OHOS::NWeb::NWebGestureEventResult> result_; 596 bool sendTask_ = false; 597 bool eventResult_ = false; 598 }; 599 600 class WebAvoidAreaChangedListener : public OHOS::Rosen::IAvoidAreaChangedListener { 601 public: WebAvoidAreaChangedListener(WeakPtr<WebDelegate> webDelegate,WeakPtr<PipelineBase> context)602 explicit WebAvoidAreaChangedListener(WeakPtr<WebDelegate> webDelegate, WeakPtr<PipelineBase> context) 603 : webDelegate_(webDelegate), context_(context) {} 604 ~WebAvoidAreaChangedListener() = default; 605 606 void OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea, OHOS::Rosen::AvoidAreaType type) override; 607 private: 608 WeakPtr<WebDelegate> webDelegate_; 609 WeakPtr<PipelineBase> context_; 610 }; 611 612 class WebWindowFocusChangedListener : public Rosen::IWindowLifeCycle { 613 public: WebWindowFocusChangedListener(WeakPtr<WebDelegate> webDelegate)614 explicit WebWindowFocusChangedListener(WeakPtr<WebDelegate> webDelegate) : webDelegate_(webDelegate) {} 615 ~WebWindowFocusChangedListener() = default; 616 617 void AfterFocused() override;; 618 private: 619 WeakPtr<WebDelegate> webDelegate_; 620 }; 621 622 enum class ScriptItemType { 623 DOCUMENT_START = 0, 624 DOCUMENT_END = 1, 625 DOCUMENT_HEAD_READY 626 }; 627 628 class NWebSystemConfigurationImpl : public OHOS::NWeb::NWebSystemConfiguration { 629 public: NWebSystemConfigurationImpl(uint8_t flags)630 explicit NWebSystemConfigurationImpl(uint8_t flags) : theme_flags_(flags) {} 631 ~NWebSystemConfigurationImpl() = default; 632 GetThemeFlags()633 uint8_t GetThemeFlags() override 634 { 635 return theme_flags_; 636 } 637 638 private: 639 uint8_t theme_flags_ = static_cast<uint8_t>(NWeb::SystemThemeFlags::NONE); 640 }; 641 642 class NWebKeyboardEventImpl : public OHOS::NWeb::NWebKeyboardEvent { 643 public: NWebKeyboardEventImpl(int32_t keyCode,int32_t action,int32_t unicode,bool enableCapsLock,std::vector<int32_t> pressedCodes)644 NWebKeyboardEventImpl( 645 int32_t keyCode, int32_t action, int32_t unicode, bool enableCapsLock, 646 std::vector<int32_t> pressedCodes) 647 : keyCode_(keyCode), action_(action), unicode_(unicode), enableCapsLock_(enableCapsLock), 648 pressedCodes_(pressedCodes) {} 649 ~NWebKeyboardEventImpl() = default; 650 GetKeyCode()651 int32_t GetKeyCode() override 652 { 653 return keyCode_; 654 } 655 GetAction()656 int32_t GetAction() override 657 { 658 return action_; 659 } 660 GetUnicode()661 int32_t GetUnicode() override 662 { 663 return unicode_; 664 } 665 IsEnableCapsLock()666 bool IsEnableCapsLock() override 667 { 668 return enableCapsLock_; 669 } 670 GetPressKeyCodes()671 std::vector<int32_t> GetPressKeyCodes() override 672 { 673 return pressedCodes_; 674 } 675 676 private: 677 int32_t keyCode_ = 0; 678 int32_t action_ = 0; 679 uint32_t unicode_ = 0; 680 bool enableCapsLock_ = false; 681 std::vector<int32_t> pressedCodes_ {}; 682 }; 683 684 class NWebMouseEventImpl : public OHOS::NWeb::NWebMouseEvent { 685 public: NWebMouseEventImpl(int32_t x,int32_t y,int32_t rawX,int32_t rawY,int32_t buttton,int32_t action,int32_t clickNum,std::vector<int32_t> pressedCodes)686 NWebMouseEventImpl(int32_t x, int32_t y, int32_t rawX, int32_t rawY, 687 int32_t buttton, int32_t action, 688 int32_t clickNum, std::vector<int32_t> pressedCodes) 689 : x_(x), y_(y), buttton_(buttton), action_(action), 690 clickNum_(clickNum), pressedCodes_(pressedCodes) {} 691 ~NWebMouseEventImpl() = default; 692 GetX()693 int32_t GetX() override 694 { 695 return x_; 696 } 697 GetY()698 int32_t GetY() override 699 { 700 return y_; 701 } 702 GetButton()703 int32_t GetButton() override 704 { 705 return buttton_; 706 } 707 GetAction()708 int32_t GetAction() override 709 { 710 return action_; 711 } 712 GetClickNum()713 int32_t GetClickNum() override 714 { 715 return clickNum_; 716 } 717 GetPressKeyCodes()718 std::vector<int32_t> GetPressKeyCodes() override 719 { 720 return pressedCodes_; 721 } 722 GetRawX()723 int32_t GetRawX() override 724 { 725 return raw_x_; 726 } 727 GetRawY()728 int32_t GetRawY() override 729 { 730 return raw_y_; 731 } 732 733 private: 734 int32_t x_ = 0; 735 int32_t y_ = 0; 736 int32_t raw_x_ = 0; 737 int32_t raw_y_ = 0; 738 int32_t buttton_ = 0; 739 int32_t action_ = 0; 740 int32_t clickNum_ = 0; 741 std::vector<int32_t> pressedCodes_ {}; 742 }; 743 744 class WebDelegate : public WebResource { 745 DECLARE_ACE_TYPE(WebDelegate, WebResource); 746 747 public: 748 using CreatedCallback = std::function<void()>; 749 using ReleasedCallback = std::function<void(bool)>; 750 using EventCallback = std::function<void(const std::string&)>; 751 using EventCallbackV2 = std::function<void(const std::shared_ptr<BaseEventInfo>&)>; 752 enum class State : char { 753 WAITINGFORSIZE, 754 CREATING, 755 CREATED, 756 CREATEFAILED, 757 RELEASED, 758 }; 759 760 // for webcontoller, the enum is same as web_webview and core side 761 enum class JavaScriptObjIdErrorCode : int32_t { WEBCONTROLLERERROR = -2, WEBVIEWCONTROLLERERROR = -1, END = 0 }; 762 763 WebDelegate() = delete; 764 ~WebDelegate() override; WebDelegate(const WeakPtr<PipelineBase> & context,ErrorCallback && onError,const std::string & type)765 WebDelegate(const WeakPtr<PipelineBase>& context, ErrorCallback&& onError, const std::string& type) 766 : WebResource(type, context, std::move(onError)), instanceId_(Container::CurrentId()) 767 {} WebDelegate(const WeakPtr<PipelineBase> & context,ErrorCallback && onError,const std::string & type,int32_t id)768 WebDelegate(const WeakPtr<PipelineBase>& context, ErrorCallback&& onError, const std::string& type, int32_t id) 769 : WebResource(type, context, std::move(onError)), instanceId_(id) 770 {} 771 772 void UnRegisterScreenLockFunction(); 773 SetObserver(const RefPtr<WebDelegateObserver> & observer)774 void SetObserver(const RefPtr<WebDelegateObserver>& observer) 775 { 776 observer_ = observer; 777 }; 778 void SetRenderWeb(const WeakPtr<RenderWeb>& renderWeb); 779 780 void CreatePlatformResource(const Size& size, const Offset& position, const WeakPtr<PipelineContext>& context); 781 void CreatePluginResource(const Size& size, const Offset& position, const WeakPtr<PipelineContext>& context); 782 void AddCreatedCallback(const CreatedCallback& createdCallback); 783 void RemoveCreatedCallback(); 784 void AddReleasedCallback(const ReleasedCallback& releasedCallback); 785 void SetComponent(const RefPtr<WebComponent>& component); 786 void RemoveReleasedCallback(); 787 void Reload(); 788 void UpdateUrl(const std::string& url); 789 #ifdef OHOS_STANDARD_SYSTEM 790 void InitOHOSWeb(const RefPtr<PipelineBase>& context, const RefPtr<NG::RenderSurface>& surface); 791 void InitOHOSWeb(const WeakPtr<PipelineBase>& context); 792 bool PrepareInitOHOSWeb(const WeakPtr<PipelineBase>& context); 793 void InitWebViewWithWindow(); 794 void ShowWebView(); 795 void HideWebView(); 796 void OnRenderToBackground(); 797 void OnRenderToForeground(); 798 void SetSurfaceDensity(const double& density); 799 void Resize(const double& width, const double& height, bool isKeyboard = false); GetRosenWindowId()800 int32_t GetRosenWindowId() 801 { 802 return rosenWindowId_; 803 } 804 void SetKeepScreenOn(bool key); 805 void UpdateUserAgent(const std::string& userAgent); 806 void UpdateBackgroundColor(const int backgroundColor); 807 void UpdateInitialScale(float scale); 808 void UpdateLayoutMode(WebLayoutMode mode); 809 void UpdateJavaScriptEnabled(const bool& isJsEnabled); 810 void UpdateAllowFileAccess(const bool& isFileAccessEnabled); 811 void UpdateBlockNetworkImage(const bool& onLineImageAccessEnabled); 812 void UpdateLoadsImagesAutomatically(const bool& isImageAccessEnabled); 813 void UpdateMixedContentMode(const MixedModeContent& mixedMode); 814 void UpdateSupportZoom(const bool& isZoomAccessEnabled); 815 void UpdateDomStorageEnabled(const bool& isDomStorageAccessEnabled); 816 void UpdateGeolocationEnabled(const bool& isGeolocationAccessEnabled); 817 void UpdateCacheMode(const WebCacheMode& mode); 818 std::shared_ptr<OHOS::NWeb::NWeb> GetNweb(); 819 bool GetForceDarkMode(); 820 void OnConfigurationUpdated(const OHOS::AppExecFwk::Configuration& configuration); 821 void UpdateDarkMode(const WebDarkMode& mode); 822 void UpdateDarkModeAuto(RefPtr<WebDelegate> delegate, std::shared_ptr<OHOS::NWeb::NWebPreference> setting); 823 void UpdateForceDarkAccess(const bool& access); 824 void UpdateAudioResumeInterval(const int32_t& resumeInterval); 825 void UpdateAudioExclusive(const bool& audioExclusive); 826 void UpdateOverviewModeEnabled(const bool& isOverviewModeAccessEnabled); 827 void UpdateFileFromUrlEnabled(const bool& isFileFromUrlAccessEnabled); 828 void UpdateDatabaseEnabled(const bool& isDatabaseAccessEnabled); 829 void UpdateTextZoomRatio(const int32_t& textZoomRatioNum); 830 void UpdateWebDebuggingAccess(bool isWebDebuggingAccessEnabled); 831 void UpdatePinchSmoothModeEnabled(bool isPinchSmoothModeEnabled); 832 void UpdateMediaPlayGestureAccess(bool isNeedGestureAccess); 833 void UpdateMultiWindowAccess(bool isMultiWindowAccessEnabled); 834 void UpdateAllowWindowOpenMethod(bool isAllowWindowOpenMethod); 835 void UpdateWebCursiveFont(const std::string& cursiveFontFamily); 836 void UpdateWebFantasyFont(const std::string& fantasyFontFamily); 837 void UpdateWebFixedFont(const std::string& fixedFontFamily); 838 void UpdateWebSansSerifFont(const std::string& sansSerifFontFamily); 839 void UpdateWebSerifFont(const std::string& serifFontFamily); 840 void UpdateWebStandardFont(const std::string& standardFontFamily); 841 void UpdateDefaultFixedFontSize(int32_t size); 842 void UpdateDefaultFontSize(int32_t defaultFontSize); 843 void UpdateDefaultTextEncodingFormat(const std::string& textEncodingFormat); 844 void UpdateMinFontSize(int32_t minFontSize); 845 void UpdateMinLogicalFontSize(int32_t minLogicalFontSize); 846 void UpdateBlockNetwork(bool isNetworkBlocked); 847 void UpdateHorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled); 848 void UpdateVerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled); 849 void UpdateOverlayScrollbarEnabled(bool isEnabled); 850 void UpdateScrollBarColor(const std::string& colorValue); 851 void UpdateOverScrollMode(const int32_t overscrollModeValue); 852 void UpdateBlurOnKeyboardHideMode(const int32_t isBlurOnKeyboardHideEnable); 853 void UpdateNativeEmbedModeEnabled(bool isEmbedModeEnabled); 854 void UpdateIntrinsicSizeEnabled(bool isIntrinsicSizeEnabled); 855 void UpdateNativeEmbedRuleTag(const std::string& tag); 856 void UpdateNativeEmbedRuleType(const std::string& type); 857 void UpdateCopyOptionMode(const int32_t copyOptionModeValue); 858 void UpdateTextAutosizing(bool isTextAutosizing); 859 void UpdateMetaViewport(bool isMetaViewportEnabled); 860 void UpdateNativeVideoPlayerConfig(bool enable, bool shouldOverlay); 861 void UpdateEnableFollowSystemFontWeight(bool enableFollowSystemFontWeight); 862 void LoadUrl(); 863 void CreateWebMessagePorts(std::vector<RefPtr<WebMessagePort>>& ports); 864 void PostWebMessage(std::string& message, std::vector<RefPtr<WebMessagePort>>& ports, std::string& uri); 865 void ClosePort(std::string& handle); 866 void PostPortMessage(std::string& handle, std::string& data); 867 void SetPortMessageCallback(std::string& handle, std::function<void(const std::string&)>&& callback); 868 void HandleTouchDown(const int32_t& id, const double& x, const double& y, bool from_overlay = false); 869 void HandleTouchUp(const int32_t& id, const double& x, const double& y, bool from_overlay = false); 870 void HandleTouchMove(const int32_t& id, const double& x, const double& y, bool from_overlay = false); 871 void HandleTouchMove(const std::vector<std::shared_ptr<OHOS::NWeb::NWebTouchPointInfo>> &touch_point_infos, 872 bool fromOverlay = false); 873 void HandleTouchCancel(); 874 void HandleTouchpadFlingEvent(const double& x, const double& y, const double& vx, const double& vy); 875 void WebHandleTouchpadFlingEvent(const double& x, const double& y, 876 const double& vx, const double& vy, const std::vector<int32_t>& pressedCodes); 877 void HandleAxisEvent(const double& x, const double& y, const double& deltaX, const double& deltaY); 878 void WebHandleAxisEvent(const double& x, const double& y, 879 const double& deltaX, const double& deltaY, const std::vector<int32_t>& pressedCodes, const int32_t source); 880 bool OnKeyEvent(int32_t keyCode, int32_t keyAction); 881 bool WebOnKeyEvent(int32_t keyCode, int32_t keyAction, const std::vector<int32_t>& pressedCodes); 882 bool SendKeyboardEvent(const std::shared_ptr<OHOS::NWeb::NWebKeyboardEvent>& keyboardEvent); 883 void OnMouseEvent(int32_t x, int32_t y, const MouseButton button, const MouseAction action, int count); 884 void WebOnMouseEvent(const std::shared_ptr<OHOS::NWeb::NWebMouseEvent>& mouseEvent); 885 void OnFocus(const OHOS::NWeb::FocusReason& reason = OHOS::NWeb::FocusReason::EVENT_REQUEST); 886 bool NeedSoftKeyboard(); 887 void OnBlur(); 888 void OnPermissionRequestPrompt(const std::shared_ptr<OHOS::NWeb::NWebAccessRequest>& request); 889 void OnScreenCaptureRequest(const std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest>& request); 890 void UpdateClippedSelectionBounds(int32_t x, int32_t y, int32_t w, int32_t h); 891 bool RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params, 892 std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback); 893 void OnQuickMenuDismissed(); 894 void HideHandleAndQuickMenuIfNecessary(bool hide); 895 void ChangeVisibilityOfQuickMenu(); 896 void OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle, 897 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle, 898 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle); 899 void HandleDragEvent(int32_t x, int32_t y, const DragAction& dragAction); 900 RefPtr<PixelMap> GetDragPixelMap(); 901 std::string GetUrl(); 902 void UpdateLocale(); 903 void SetDrawRect(int32_t x, int32_t y, int32_t width, int32_t height); 904 void ReleaseResizeHold(); 905 bool GetPendingSizeStatus(); 906 void OnInactive(); 907 void OnActive(); 908 void GestureBackBlur(); 909 void OnWebviewHide(); 910 void OnWebviewShow(); 911 bool OnCursorChange(const OHOS::NWeb::CursorType& type, std::shared_ptr<OHOS::NWeb::NWebCursorInfo> info); 912 void OnSelectPopupMenu( 913 std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params, 914 std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback); 915 void SetShouldFrameSubmissionBeforeDraw(bool should); SetBackgroundColor(int32_t backgroundColor)916 void SetBackgroundColor(int32_t backgroundColor) 917 { 918 backgroundColor_ = backgroundColor; 919 } 920 void NotifyMemoryLevel(int32_t level); 921 void SetAudioMuted(bool muted); SetRichtextIdentifier(std::optional<std::string> & richtextData)922 void SetRichtextIdentifier(std::optional<std::string>& richtextData) 923 { 924 richtextData_ = richtextData; 925 } 926 void HandleAccessibilityHoverEvent(int32_t x, int32_t y); 927 void NotifyAutoFillViewData(const std::string& jsonStr); 928 void AutofillCancel(const std::string& fillContent); 929 bool HandleAutoFillEvent(const std::shared_ptr<OHOS::NWeb::NWebMessage>& viewDataJson); 930 void UpdateOptimizeParserBudgetEnabled(const bool enable); 931 #endif 932 void OnErrorReceive(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 933 std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error); 934 void OnHttpErrorReceive(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 935 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response); 936 RefPtr<WebResponse> OnInterceptRequest(const std::shared_ptr<BaseEventInfo>& info); 937 bool IsEmptyOnInterceptRequest(); 938 void ReportDynamicFrameLossEvent(const std::string& sceneId, bool isStart); 939 void RecordWebEvent(Recorder::EventType eventType, const std::string& param) const; 940 void OnPageStarted(const std::string& param); 941 void OnPageFinished(const std::string& param); 942 void OnProgressChanged(int param); 943 void OnReceivedTitle(const std::string& param); 944 void ExitFullScreen(); 945 void OnFullScreenExit(); 946 void OnGeolocationPermissionsHidePrompt(); 947 void OnGeolocationPermissionsShowPrompt( 948 const std::string& origin, const std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface>& callback); 949 void OnCompleteSwapWithNewSize(); 950 void OnResizeNotWork(); 951 void OnDateTimeChooserPopup( 952 std::shared_ptr<OHOS::NWeb::NWebDateTimeChooser> chooser, 953 const std::vector<std::shared_ptr<OHOS::NWeb::NWebDateTimeSuggestion>>& suggestions, 954 std::shared_ptr<OHOS::NWeb::NWebDateTimeChooserCallback> callback); 955 void OnDateTimeChooserClose(); 956 void OnRequestFocus(); 957 bool OnCommonDialog(const std::shared_ptr<BaseEventInfo>& info, DialogEventType dialogEventType); 958 bool OnHttpAuthRequest(const std::shared_ptr<BaseEventInfo>& info); 959 bool OnSslErrorRequest(const std::shared_ptr<BaseEventInfo>& info); 960 bool OnAllSslErrorRequest(const std::shared_ptr<BaseEventInfo>& info); 961 bool OnSslSelectCertRequest(const std::shared_ptr<BaseEventInfo>& info); 962 void OnDownloadStart(const std::string& url, const std::string& userAgent, const std::string& contentDisposition, 963 const std::string& mimetype, long contentLength); 964 void OnAccessibilityEvent(int64_t accessibilityId, AccessibilityEventType eventType, const std::string& argument); 965 void OnPageError(const std::string& param); 966 void OnMessage(const std::string& param); 967 void OnFullScreenEnter(std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler, int videoNaturalWidth, 968 int videoNaturalHeight); 969 bool OnConsoleLog(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message); 970 void OnRouterPush(const std::string& param); 971 void OnRenderExited(OHOS::NWeb::RenderExitReason reason); 972 void OnRefreshAccessedHistory(const std::string& url, bool isRefreshed); 973 bool OnFileSelectorShow(const std::shared_ptr<BaseEventInfo>& info); 974 bool OnContextMenuShow(const std::shared_ptr<BaseEventInfo>& info); 975 void OnContextMenuHide(const std::string& info); 976 bool OnHandleInterceptUrlLoading(const std::string& url); 977 bool OnHandleInterceptLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request); 978 void OnResourceLoad(const std::string& url); 979 void OnScaleChange(float oldScaleFactor, float newScaleFactor); 980 void OnScroll(double xOffset, double yOffset); 981 bool LoadDataWithRichText(); 982 void OnSearchResultReceive(int activeMatchOrdinal, int numberOfMatches, bool isDoneCounting); 983 bool OnDragAndDropData(const void* data, size_t len, int width, int height); 984 bool OnDragAndDropDataUdmf(std::shared_ptr<OHOS::NWeb::NWebDragData> dragData); 985 void OnTooltip(const std::string& tooltip); 986 void OnPopupSize(int32_t x, int32_t y, int32_t width, int32_t height); 987 void OnPopupShow(bool show); 988 void OnShowAutofillPopup(const float offsetX, const float offsetY, const std::vector<std::string>& menu_items); 989 void OnShowAutofillPopupV2(const float offsetX, const float offsetY, const float height, const float width, 990 const std::vector<std::string>& menu_items); 991 void SuggestionSelected(int32_t index); 992 void OnHideAutofillPopup(); 993 std::shared_ptr<OHOS::NWeb::NWebDragData> GetOrCreateDragData(); 994 bool IsDragging(); 995 bool IsImageDrag(); 996 std::shared_ptr<OHOS::NWeb::NWebDragData> dragData_ = nullptr; 997 std::string tempDir_; UpdateDragCursor(NWeb::NWebDragData::DragOperation op)998 void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) 999 { 1000 op_ = op; 1001 } GetDragAcceptableStatus()1002 NWeb::NWebDragData::DragOperation GetDragAcceptableStatus() 1003 { 1004 return op_; 1005 } 1006 NWeb::NWebDragData::DragOperation op_ = NWeb::NWebDragData::DragOperation::DRAG_OPERATION_NONE; 1007 void OnWindowNew(const std::string& targetUrl, bool isAlert, bool isUserTrigger, 1008 const std::shared_ptr<OHOS::NWeb::NWebControllerHandler>& handler); 1009 void OnWindowExit(); 1010 void OnPageVisible(const std::string& url); 1011 void OnDataResubmitted(std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler); 1012 void OnNavigationEntryCommitted(std::shared_ptr<OHOS::NWeb::NWebLoadCommittedDetails> details); 1013 void OnFaviconReceived(const void* data, size_t width, size_t height, OHOS::NWeb::ImageColorType colorType, 1014 OHOS::NWeb::ImageAlphaType alphaType); 1015 void OnTouchIconUrl(const std::string& iconUrl, bool precomposed); 1016 void OnAudioStateChanged(bool audible); 1017 void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs); 1018 void OnFirstMeaningfulPaint(std::shared_ptr<OHOS::NWeb::NWebFirstMeaningfulPaintDetails> details); 1019 void OnLargestContentfulPaint(std::shared_ptr<OHOS::NWeb::NWebLargestContentfulPaintDetails> details); 1020 void OnSafeBrowsingCheckResult(int threat_type); 1021 void OnGetTouchHandleHotZone(std::shared_ptr<OHOS::NWeb::NWebTouchHandleHotZone> hotZone); 1022 void OnOverScroll(float xOffset, float yOffset); 1023 void OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling); 1024 void OnScrollState(bool scrollState); 1025 void OnScrollStart(const float x, const float y); 1026 void OnRootLayerChanged(int width, int height); 1027 bool FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity); 1028 void OnNativeEmbedAllDestory(); 1029 void OnNativeEmbedLifecycleChange(std::shared_ptr<NWeb::NWebNativeEmbedDataInfo> dataInfo); 1030 void OnNativeEmbedVisibilityChange(const std::string& embedId, bool visibility); 1031 void OnNativeEmbedGestureEvent(std::shared_ptr<NWeb::NWebNativeEmbedTouchEvent> event); 1032 void SetNGWebPattern(const RefPtr<NG::WebPattern>& webPattern); 1033 bool RequestFocus(OHOS::NWeb::NWebFocusSource source = OHOS::NWeb::NWebFocusSource::FOCUS_SOURCE_DEFAULT); 1034 bool IsCurrentFocus(); 1035 void SetDrawSize(const Size& drawSize); 1036 void SetEnhanceSurfaceFlag(const bool& isEnhanceSurface); 1037 EGLConfig GLGetConfig(int version, EGLDisplay eglDisplay); 1038 void GLContextInit(void* window); 1039 sptr<OHOS::SurfaceDelegate> GetSurfaceDelegateClient(); 1040 void SetBoundsOrResize(const Size& drawSize, const Offset& offset, bool isKeyboard = false); 1041 void ResizeVisibleViewport(const Size& visibleSize, bool isKeyboard = false); 1042 Offset GetWebRenderGlobalPos(); 1043 bool InitWebSurfaceDelegate(const WeakPtr<PipelineBase>& context); 1044 int GetWebId(); 1045 void JavaScriptOnDocumentStart(); 1046 void JavaScriptOnDocumentEnd(); 1047 void SetJavaScriptItems(const ScriptItems& scriptItems, const ScriptItemType& type); 1048 void JavaScriptOnDocumentStartByOrder(); 1049 void JavaScriptOnDocumentEndByOrder(); 1050 void JavaScriptOnHeadReadyByOrder(); 1051 void SetJavaScriptItemsByOrder(const ScriptItems& scriptItems, const ScriptItemType& type, 1052 const ScriptItemsByOrder& scriptItemsByOrder); 1053 void SetTouchEventInfo(std::shared_ptr<OHOS::NWeb::NWebNativeEmbedTouchEvent> touchEvent, 1054 TouchEventInfo& touchEventInfo); 1055 bool GetIsSmoothDragResizeEnabled(); 1056 void DragResize(const double& width, const double& height, const double& pre_height, const double& pre_width); 1057 void SetDragResizeStartFlag(bool isDragResizeStart); 1058 void SetDragResizePreSize(const double& pre_height, const double& pre_width); 1059 std::string SpanstringConvertHtml(const std::vector<uint8_t> &content); 1060 bool CloseImageOverlaySelection(); 1061 void GetVisibleRectToWeb(int& visibleX, int& visibleY, int& visibleWidth, int& visibleHeight); 1062 void RestoreRenderFit(); 1063 #if defined(ENABLE_ROSEN_BACKEND) 1064 void SetSurface(const sptr<Surface>& surface); 1065 void SetPopupSurface(const RefPtr<NG::RenderSurface>& popupSurface); 1066 sptr<Surface> surface_ = nullptr; 1067 sptr<Surface> popupSurface_ = nullptr; 1068 RefPtr<NG::RosenRenderSurface> renderSurface_ = nullptr; 1069 RefPtr<NG::RosenRenderSurface> popupRenderSurface_ = nullptr; 1070 #endif 1071 #ifdef OHOS_STANDARD_SYSTEM SetWebRendeGlobalPos(const Offset & pos)1072 void SetWebRendeGlobalPos(const Offset& pos) 1073 { 1074 offset_ = pos; 1075 } SetBlurReason(const OHOS::NWeb::BlurReason & blurReason)1076 void SetBlurReason(const OHOS::NWeb::BlurReason& blurReason) 1077 { 1078 blurReason_ = blurReason; 1079 } SetPopup(bool popup)1080 void SetPopup(bool popup) 1081 { 1082 isPopup_ = popup; 1083 } SetParentNWebId(int32_t parentNWebId)1084 void SetParentNWebId(int32_t parentNWebId) 1085 { 1086 parentNWebId_ = parentNWebId; 1087 } 1088 #endif 1089 void SetToken(); 1090 void SetRenderMode(RenderMode renderMode); 1091 void SetFitContentMode(WebLayoutMode layoutMode); 1092 void SetVirtualKeyBoardArg(int32_t width, int32_t height, double keyboard); 1093 bool ShouldVirtualKeyboardOverlay(); 1094 void ScrollBy(float deltaX, float deltaY); 1095 void ScrollByRefScreen(float deltaX, float deltaY, float vx = 0, float vy = 0); 1096 bool ExecuteAction(int64_t accessibilityId, AceAction action, 1097 const std::map<std::string, std::string>& actionArguments); 1098 bool GetAccessibilityNodeRectById( 1099 int64_t accessibilityId, int32_t* width, int32_t* height, int32_t* offsetX, int32_t* offsetY); 1100 std::shared_ptr<OHOS::NWeb::NWebAccessibilityNodeInfo> GetFocusedAccessibilityNodeInfo( 1101 int64_t accessibilityId, bool isAccessibilityFocus); 1102 std::shared_ptr<OHOS::NWeb::NWebAccessibilityNodeInfo> GetAccessibilityNodeInfoById(int64_t accessibilityId); 1103 std::shared_ptr<OHOS::NWeb::NWebAccessibilityNodeInfo> GetAccessibilityNodeInfoByFocusMove( 1104 int64_t accessibilityId, int32_t direction); 1105 void SetAccessibilityState(bool state, bool isDelayed); 1106 void UpdateAccessibilityState(bool state); 1107 OHOS::NWeb::NWebPreference::CopyOptionMode GetCopyOptionMode() const; 1108 void OnIntelligentTrackingPreventionResult( 1109 const std::string& websiteHost, const std::string& trackerHost); 1110 bool OnHandleOverrideLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request); 1111 void ScaleGestureChange(double scale, double centerX, double centerY); 1112 void ScaleGestureChangeV2(int type, double scale, double originScale, double centerX, double centerY); 1113 std::vector<int8_t> GetWordSelection(const std::string& text, int8_t offset); 1114 // Backward 1115 void Backward(); 1116 bool AccessBackward(); 1117 bool OnOpenAppLink(const std::string& url, std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback> callback); 1118 1119 void OnRenderProcessNotResponding( 1120 const std::string& jsStack, int pid, OHOS::NWeb::RenderProcessNotRespondingReason reason); 1121 void OnRenderProcessResponding(); 1122 std::string GetSelectInfo() const; 1123 Offset GetPosition(const std::string& embedId); 1124 1125 void OnOnlineRenderToForeground(); 1126 void NotifyForNextTouchEvent(); 1127 1128 void OnViewportFitChange(OHOS::NWeb::ViewportFit viewportFit); 1129 void OnAreaChange(const OHOS::Ace::Rect& area); 1130 void OnAvoidAreaChanged(const OHOS::Rosen::AvoidArea avoidArea, OHOS::Rosen::AvoidAreaType type); 1131 std::string GetWebInfoType(); 1132 void OnInterceptKeyboardAttach( 1133 const std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler, 1134 const std::map<std::string, std::string> &attributes, bool &useSystemKeyboard, int32_t &enterKeyType); 1135 1136 void OnCustomKeyboardAttach(); 1137 1138 void OnCustomKeyboardClose(); 1139 1140 void CreateOverlay(void* data, size_t len, int width, int height, int offsetX, int offsetY, int rectWidth, 1141 int rectHeight, int pointX, int pointY); 1142 1143 void OnOverlayStateChanged(int offsetX, int offsetY, int rectWidth, int rectHeight); 1144 1145 void OnTextSelected(); 1146 void OnDestroyImageAnalyzerOverlay(); 1147 1148 void OnAttachContext(const RefPtr<NG::PipelineContext> &context); 1149 void OnDetachContext(); 1150 GetInstanceId()1151 int32_t GetInstanceId() const 1152 { 1153 return instanceId_; 1154 } 1155 1156 void OnAdsBlocked(const std::string& url, const std::vector<std::string>& adsBlocked); 1157 void SetSurfaceId(const std::string& surfaceId); 1158 1159 void KeyboardReDispatch(const std::shared_ptr<OHOS::NWeb::NWebKeyEvent>& event, bool isUsed); 1160 1161 void OnCursorUpdate(double x, double y, double width, double height); 1162 CloseCustomKeyboard()1163 void CloseCustomKeyboard() 1164 { 1165 if (keyboardHandler_) { 1166 keyboardHandler_->Close(); 1167 } 1168 } 1169 1170 bool SetFocusByPosition(float x, float y); 1171 1172 void StartVibraFeedback(const std::string& vibratorType); 1173 GetTaskExecutor()1174 RefPtr<TaskExecutor> GetTaskExecutor() const 1175 { 1176 return taskExecutor_; 1177 } 1178 1179 bool GetAccessibilityVisible(int64_t accessibilityId); 1180 1181 void SetTransformHint(uint32_t rotation); 1182 1183 void MaximizeResize(); 1184 1185 void ExecuteTypeScript(const std::string& jscode, const std::function<void(std::string)>&& callback); 1186 1187 void RegisterNativeArkJSFunction(const std::string& objName, 1188 const std::vector<std::pair<std::string, NativeMethodCallback>>& methodList, bool isNeedRefresh); 1189 1190 void UnRegisterNativeArkJSFunction(const std::string& objName); 1191 1192 bool IsActivePolicyDisable(); 1193 1194 void UpdateWebMediaAVSessionEnabled(bool isEnabled); 1195 1196 std::string GetCurrentLanguage(); 1197 void RegisterNativeJavaScriptProxy(const std::string& obj, const std::vector<std::string>& method, 1198 std::vector<std::function<void(const std::vector<std::string>&)>> callbackImpl, 1199 bool isAync, const std::string& permission, bool isNeedRefresh); 1200 1201 void RegisterWebWindowFocusChangedListener(); 1202 1203 void UnRegisterWebWindowFocusChangedListener(); 1204 1205 void OnDragAttach(); 1206 1207 void SetNativeInnerWeb(bool isInnerWeb); 1208 1209 private: 1210 void InitWebEvent(); 1211 void RegisterWebEvent(); 1212 void ReleasePlatformResource(); 1213 void Stop(); 1214 void UnregisterEvent(); 1215 std::string GetUrlStringParam(const std::string& param, const std::string& name) const; 1216 void CallWebRouterBack(); 1217 void CallPopPageSuccessPageUrl(const std::string& url); 1218 void CallIsPagePathInvalid(const bool& isPageInvalid); 1219 1220 void BindRouterBackMethod(); 1221 void BindPopPageSuccessMethod(); 1222 void BindIsPagePathInvalidMethod(); 1223 void TextBlurReportByFocusEvent(int64_t accessibilityId); 1224 void WebComponentClickReport(int64_t accessibilityId); 1225 void TextBlurReportByBlurEvent(int64_t accessibilityId); 1226 1227 #ifdef OHOS_STANDARD_SYSTEM 1228 sptr<OHOS::Rosen::Window> CreateWindow(); 1229 void LoadUrl(const std::string& url, const std::map<std::string, std::string>& httpHeaders); 1230 void LoadDataWithBaseUrl(const std::string& baseUrl, const std::string& data, const std::string& mimeType, 1231 const std::string& encoding, const std::string& historyUrl); 1232 void Refresh(); 1233 void StopLoading(); 1234 void AddJavascriptInterface(const std::string& objectName, const std::vector<std::string>& methodList); 1235 void RemoveJavascriptInterface(const std::string& objectName, const std::vector<std::string>& methodList); 1236 void SetWebViewJavaScriptResultCallBack(const WebController::JavaScriptCallBackImpl&& javaScriptCallBackImpl); 1237 void Zoom(float factor); 1238 bool ZoomIn(); 1239 bool ZoomOut(); 1240 int ConverToWebHitTestType(int hitType); 1241 int GetHitTestResult(); 1242 void GetHitTestValue(HitTestResult& result); 1243 int GetPageHeight(); 1244 std::string GetTitle(); 1245 std::string GetDefaultUserAgent(); 1246 bool SaveCookieSync(); 1247 bool SetCookie(const std::string& url, 1248 const std::string& value, 1249 bool incognito_mode); 1250 std::string GetCookie(const std::string& url, bool incognito_mode) const; 1251 void DeleteEntirelyCookie(bool incognito_mode); 1252 void RegisterOHOSWebEventAndMethord(); 1253 void SetWebCallBack(); 1254 void RunSetWebIdAndHapPathCallback(); 1255 void RunJsProxyCallback(); 1256 void RegisterConfigObserver(); 1257 void UnRegisterConfigObserver(); 1258 1259 // forward 1260 void Forward(); 1261 void ClearHistory(); 1262 void ClearSslCache(); 1263 void ClearClientAuthenticationCache(); 1264 bool AccessStep(int32_t step); 1265 void BackOrForward(int32_t step); 1266 bool AccessForward(); 1267 1268 void SearchAllAsync(const std::string& searchStr); 1269 void ClearMatches(); 1270 void SearchNext(bool forward); 1271 1272 void UpdateSettting(bool useNewPipe = false); 1273 1274 std::string GetCustomScheme(); 1275 void InitWebViewWithSurface(); 1276 Size GetEnhanceSurfaceSize(const Size& drawSize); 1277 void UpdateScreenOffSet(double& offsetX, double& offsetY); 1278 void RegisterSurfacePositionChangedCallback(); 1279 void UnregisterSurfacePositionChangedCallback(); 1280 1281 void NotifyPopupWindowResult(bool result); 1282 1283 EventCallbackV2 GetAudioStateChangedCallback(bool useNewPipe, const RefPtr<NG::WebEventHub>& eventHub); 1284 void SurfaceOcclusionCallback(float visibleRatio); 1285 void RegisterSurfaceOcclusionChangeFun(); 1286 void SetPartitionPoints(std::vector<float>& partition); 1287 void ratioStrToFloat(const std::string& str); 1288 void ratioStrToFloatV2(const std::string& str); 1289 // Return canonical encoding name according to the encoding alias name. 1290 std::string GetCanonicalEncodingName(const std::string& alias_name) const; 1291 void RegisterAvoidAreaChangeListener(int32_t instanceId); 1292 void UnregisterAvoidAreaChangeListener(int32_t instanceId); 1293 NG::SafeAreaInsets GetCombinedSafeArea(); 1294 void OnSafeInsetsChange(); 1295 void EnableHardware(); 1296 #endif 1297 1298 WeakPtr<WebComponent> webComponent_; 1299 WeakPtr<RenderWeb> renderWeb_; 1300 1301 WeakPtr<NG::WebPattern> webPattern_; 1302 1303 std::list<CreatedCallback> createdCallbacks_; 1304 std::list<ReleasedCallback> releasedCallbacks_; 1305 EventCallback onPageStarted_; 1306 EventCallback onPageFinished_; 1307 EventCallback onPageError_; 1308 EventCallback onMessage_; 1309 Method reloadMethod_; 1310 Method updateUrlMethod_; 1311 Method routerBackMethod_; 1312 Method changePageUrlMethod_; 1313 Method isPagePathInvalidMethod_; 1314 State state_ { State::WAITINGFORSIZE }; 1315 #ifdef OHOS_STANDARD_SYSTEM 1316 std::shared_ptr<OHOS::NWeb::NWeb> nweb_; 1317 std::shared_ptr<OHOS::NWeb::NWebCookieManager> cookieManager_ = nullptr; 1318 sptr<Rosen::Window> window_; 1319 bool isCreateWebView_ = false; 1320 int32_t callbackId_ = 0; 1321 1322 EventCallbackV2 onPageFinishedV2_; 1323 EventCallbackV2 onPageStartedV2_; 1324 EventCallbackV2 onProgressChangeV2_; 1325 EventCallbackV2 onTitleReceiveV2_; 1326 EventCallbackV2 onFullScreenExitV2_; 1327 EventCallbackV2 onGeolocationHideV2_; 1328 EventCallbackV2 onGeolocationShowV2_; 1329 EventCallbackV2 onRequestFocusV2_; 1330 EventCallbackV2 onErrorReceiveV2_; 1331 EventCallbackV2 onHttpErrorReceiveV2_; 1332 EventCallbackV2 onDownloadStartV2_; 1333 EventCallbackV2 onRefreshAccessedHistoryV2_; 1334 EventCallbackV2 onRenderExitedV2_; 1335 EventCallbackV2 onResourceLoadV2_; 1336 EventCallbackV2 onScaleChangeV2_; 1337 EventCallbackV2 onScrollV2_; 1338 EventCallbackV2 onPermissionRequestV2_; 1339 EventCallbackV2 onSearchResultReceiveV2_; 1340 EventCallbackV2 onWindowExitV2_; 1341 EventCallbackV2 onPageVisibleV2_; 1342 EventCallbackV2 onTouchIconUrlV2_; 1343 EventCallbackV2 onAudioStateChangedV2_; 1344 EventCallbackV2 onFirstContentfulPaintV2_; 1345 EventCallbackV2 OnFirstMeaningfulPaintV2_; 1346 EventCallbackV2 OnLargestContentfulPaintV2_; 1347 EventCallbackV2 onOverScrollV2_; 1348 EventCallbackV2 onScreenCaptureRequestV2_; 1349 EventCallbackV2 onNavigationEntryCommittedV2_; 1350 EventCallbackV2 onSafeBrowsingCheckResultV2_; 1351 EventCallbackV2 OnNativeEmbedAllDestoryV2_; 1352 EventCallbackV2 OnNativeEmbedLifecycleChangeV2_; 1353 EventCallbackV2 OnNativeEmbedVisibilityChangeV2_; 1354 EventCallbackV2 OnNativeEmbedGestureEventV2_; 1355 EventCallbackV2 onIntelligentTrackingPreventionResultV2_; 1356 EventCallbackV2 onRenderProcessNotRespondingV2_; 1357 EventCallbackV2 onRenderProcessRespondingV2_; 1358 EventCallbackV2 onViewportFitChangedV2_; 1359 std::function<WebKeyboardOption(const std::shared_ptr<BaseEventInfo>&)> onInterceptKeyboardAttachV2_; 1360 EventCallbackV2 onAdsBlockedV2_; 1361 1362 int32_t renderMode_ = -1; 1363 int32_t layoutMode_ = -1; 1364 std::string bundlePath_; 1365 std::string bundleDataPath_; 1366 std::string hapPath_; 1367 RefPtr<PixelMap> pixelMap_ = nullptr; 1368 bool isRefreshPixelMap_ = false; 1369 Size drawSize_; 1370 Offset offset_; 1371 bool isEnhanceSurface_ = false; 1372 sptr<WebSurfaceCallback> surfaceCallback_; 1373 sptr<OHOS::SurfaceDelegate> surfaceDelegate_; 1374 EGLNativeWindowType mEglWindow = nullptr; 1375 EGLDisplay mEGLDisplay = EGL_NO_DISPLAY; 1376 EGLConfig mEGLConfig = nullptr; 1377 EGLContext mEGLContext = EGL_NO_CONTEXT; 1378 EGLContext mSharedEGLContext = EGL_NO_CONTEXT; 1379 EGLSurface mEGLSurface = nullptr; 1380 WindowsSurfaceInfo surfaceInfo_ = { nullptr, EGL_NO_DISPLAY, nullptr, EGL_NO_CONTEXT }; 1381 bool forceDarkMode_ = false; 1382 WebDarkMode current_dark_mode_ = WebDarkMode::Auto; 1383 std::shared_ptr<OHOS::AbilityRuntime::EnvironmentCallback> configChangeObserver_ = nullptr; 1384 OHOS::NWeb::BlurReason blurReason_ = OHOS::NWeb::BlurReason::FOCUS_SWITCH; 1385 bool isPopup_ = false; 1386 int32_t parentNWebId_ = -1; 1387 bool needResizeAtFirst_ = false; 1388 int32_t backgroundColor_ = 0xffffffff; 1389 uint32_t rosenWindowId_ = 0; 1390 RefPtr<WebDelegateObserver> observer_; 1391 std::shared_ptr<Rosen::RSNode> rsNode_; 1392 std::shared_ptr<Rosen::RSNode> surfaceRsNode_; 1393 Rosen::NodeId surfaceNodeId_ = 0; 1394 float visibleRatio_ = 1.0; 1395 uint32_t delayTime_ = 500; 1396 float lowerFrameRateVisibleRatio_ = 0.1; 1397 float halfFrameRateVisibleRatio_ = 0.3; 1398 bool isHalfFrame_ = false; 1399 std::optional<ScriptItems> onDocumentStartScriptItems_; 1400 std::optional<ScriptItems> onDocumentEndScriptItems_; 1401 std::optional<ScriptItems> onHeadReadyScriptItems_; 1402 std::optional<ScriptItemsByOrder> onDocumentStartScriptItemsByOrder_; 1403 std::optional<ScriptItemsByOrder> onDocumentEndScriptItemsByOrder_; 1404 std::optional<ScriptItemsByOrder> onHeadReadyScriptItemsByOrder_; 1405 bool accessibilityState_ = false; 1406 std::optional<std::string> richtextData_; 1407 bool incognitoMode_ = false; 1408 bool isEmbedModeEnabled_ = false; 1409 std::map<std::string, std::shared_ptr<OHOS::NWeb::NWebNativeEmbedDataInfo>> embedDataInfo_; 1410 std::string tag_; 1411 std::string tag_type_; 1412 double resizeWidth_ = 0.0; 1413 double resizeHeight_ = 0.0; 1414 double resizeVisibleWidth_ = -1.0; 1415 double resizeVisibleHeight_ = -1.0; 1416 OHOS::Ace::Rect currentArea_; 1417 NG::SafeAreaInsets systemSafeArea_; 1418 NG::SafeAreaInsets cutoutSafeArea_; 1419 NG::SafeAreaInsets navigationIndicatorSafeArea_; 1420 sptr<Rosen::IAvoidAreaChangedListener> avoidAreaChangedListener_ = nullptr; 1421 int32_t instanceId_; 1422 std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler_ = nullptr; 1423 sptr<WebWindowFocusChangedListener> webWindowFocusChangedListener_ = nullptr; 1424 std::string sharedRenderProcessToken_; 1425 int64_t lastFocusInputId_ = 0; 1426 int64_t lastFocusReportId_ = 0; 1427 RefPtr<TaskExecutor> taskExecutor_; 1428 bool isEnableHardwareComposition_ = false; 1429 bool isDragResizeStart_ = false; 1430 double dragResize_preHight_ = 0.0; 1431 double dragResize_preWidth_ = 0.0; 1432 bool enableFollowSystemFontWeight_ = false; 1433 #endif 1434 }; 1435 1436 } // namespace OHOS::Ace 1437 1438 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_DELEGATE_H 1439