1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 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 "base/memory/referenced.h" 23 #include "core/components_ng/render/render_surface.h" 24 #include "core/pipeline/pipeline_base.h" 25 #if defined (OHOS_STANDARD_SYSTEM) && defined (ENABLE_ROSEN_BACKEND) 26 #include <ui/rs_surface_node.h> 27 #endif 28 29 #include <EGL/egl.h> 30 #include <EGL/eglext.h> 31 #include <GLES3/gl3.h> 32 #include "base/image/pixel_map.h" 33 #include "core/common/recorder/event_recorder.h" 34 #include "core/components/common/layout/constants.h" 35 #include "core/components/web/resource/web_client_impl.h" 36 #include "core/components/web/resource/web_resource.h" 37 #include "core/components/web/web_component.h" 38 #include "core/components/web/web_event.h" 39 #include "core/components_ng/pattern/web/web_event_hub.h" 40 #include "nweb_accessibility_node_info.h" 41 #include "surface_delegate.h" 42 #ifdef OHOS_STANDARD_SYSTEM 43 #include "nweb_handler.h" 44 #include "nweb_helper.h" 45 #include "nweb_hit_testresult.h" 46 #include "app_mgr_client.h" 47 #ifdef ENABLE_ROSEN_BACKEND 48 #include "surface.h" 49 #endif 50 #include "window.h" 51 #endif 52 53 namespace OHOS::Ace { 54 55 typedef struct WindowsSurfaceInfoTag { 56 void* window; 57 EGLDisplay display; 58 EGLContext context; 59 EGLSurface surface; 60 } WindowsSurfaceInfo; 61 62 class WebMessagePortOhos : public WebMessagePort { DECLARE_ACE_TYPE(WebMessagePortOhos,WebMessagePort)63 DECLARE_ACE_TYPE(WebMessagePortOhos, WebMessagePort) 64 65 public: 66 WebMessagePortOhos(WeakPtr<WebDelegate> webDelegate) : webDelegate_(webDelegate) {} 67 WebMessagePortOhos() = default; 68 ~WebMessagePortOhos() = default; 69 70 void Close() override; 71 void PostMessage(std::string& data) override; 72 void SetWebMessageCallback(std::function<void(const std::string&)>&& callback) override; 73 void SetPortHandle(std::string& handle) override; 74 std::string GetPortHandle() override; 75 76 private: 77 WeakPtr<WebDelegate> webDelegate_; 78 std::string handle_; 79 }; 80 81 class ConsoleLogOhos : public WebConsoleLog { DECLARE_ACE_TYPE(ConsoleLogOhos,WebConsoleLog)82 DECLARE_ACE_TYPE(ConsoleLogOhos, WebConsoleLog) 83 84 public: 85 ConsoleLogOhos(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message) : message_(message) {} 86 87 int GetLineNumber() override; 88 89 std::string GetLog() override; 90 91 int GetLogLevel() override; 92 93 std::string GetSourceId() override; 94 95 private: 96 std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message_; 97 }; 98 99 class ResultOhos : public Result { DECLARE_ACE_TYPE(ResultOhos,Result)100 DECLARE_ACE_TYPE(ResultOhos, Result) 101 102 public: 103 ResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) : result_(result) {} 104 105 void Confirm() override; 106 void Confirm(const std::string& message) override; 107 void Cancel() override; 108 109 private: 110 std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result_; 111 }; 112 113 class FullScreenExitHandlerOhos : public FullScreenExitHandler { DECLARE_ACE_TYPE(FullScreenExitHandlerOhos,FullScreenExitHandler)114 DECLARE_ACE_TYPE(FullScreenExitHandlerOhos, FullScreenExitHandler) 115 116 public: 117 FullScreenExitHandlerOhos(std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler, 118 WeakPtr<WebDelegate> webDelegate) : handler_(handler), webDelegate_(webDelegate) {} 119 void ExitFullScreen() override; 120 private: 121 std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler_; 122 WeakPtr<WebDelegate> webDelegate_; 123 }; 124 125 class AuthResultOhos : public AuthResult { DECLARE_ACE_TYPE(AuthResultOhos,AuthResult)126 DECLARE_ACE_TYPE(AuthResultOhos, AuthResult) 127 128 public: 129 AuthResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSHttpAuthResult> result) : result_(result) {} 130 131 bool Confirm(std::string& userName, std::string& pwd) override; 132 bool IsHttpAuthInfoSaved() override; 133 void Cancel() override; 134 135 private: 136 std::shared_ptr<OHOS::NWeb::NWebJSHttpAuthResult> result_; 137 }; 138 139 class SslErrorResultOhos : public SslErrorResult { DECLARE_ACE_TYPE(SslErrorResultOhos,SslErrorResult)140 DECLARE_ACE_TYPE(SslErrorResultOhos, SslErrorResult) 141 142 public: 143 SslErrorResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSSslErrorResult> result) : result_(result) {} 144 145 void HandleConfirm() override; 146 void HandleCancel() override; 147 148 private: 149 std::shared_ptr<OHOS::NWeb::NWebJSSslErrorResult> result_; 150 }; 151 152 class SslSelectCertResultOhos : public SslSelectCertResult { DECLARE_ACE_TYPE(SslSelectCertResultOhos,SslSelectCertResult)153 DECLARE_ACE_TYPE(SslSelectCertResultOhos, SslSelectCertResult) 154 155 public: 156 explicit SslSelectCertResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSSslSelectCertResult> result) 157 : result_(result) {} 158 159 void HandleConfirm(const std::string& privateKeyFile, const std::string& certChainFile) override; 160 161 void HandleCancel() override; 162 163 void HandleIgnore() override; 164 private: 165 std::shared_ptr<OHOS::NWeb::NWebJSSslSelectCertResult> result_; 166 }; 167 168 class FileSelectorParamOhos : public WebFileSelectorParam { DECLARE_ACE_TYPE(FileSelectorParamOhos,WebFileSelectorParam)169 DECLARE_ACE_TYPE(FileSelectorParamOhos, WebFileSelectorParam) 170 171 public: 172 FileSelectorParamOhos(std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> param) : param_(param) {} 173 174 std::string GetTitle() override; 175 int GetMode() override; 176 std::string GetDefaultFileName() override; 177 std::vector<std::string> GetAcceptType() override; 178 bool IsCapture() override; 179 180 private: 181 std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> param_; 182 }; 183 184 class FileSelectorResultOhos : public FileSelectorResult { DECLARE_ACE_TYPE(FileSelectorResultOhos,FileSelectorResult)185 DECLARE_ACE_TYPE(FileSelectorResultOhos, FileSelectorResult) 186 187 public: 188 FileSelectorResultOhos(std::shared_ptr<OHOS::NWeb::FileSelectorCallback> callback) : callback_(callback) {} 189 190 void HandleFileList(std::vector<std::string>& result) override; 191 192 private: 193 std::shared_ptr<OHOS::NWeb::FileSelectorCallback> callback_; 194 }; 195 196 class ContextMenuParamOhos : public WebContextMenuParam { DECLARE_ACE_TYPE(ContextMenuParamOhos,WebContextMenuParam)197 DECLARE_ACE_TYPE(ContextMenuParamOhos, WebContextMenuParam) 198 199 public: 200 ContextMenuParamOhos(std::shared_ptr<OHOS::NWeb::NWebContextMenuParams> param) : param_(param) {} 201 202 int32_t GetXCoord() const override; 203 int32_t GetYCoord() const override; 204 std::string GetLinkUrl() const override; 205 std::string GetUnfilteredLinkUrl() const override; 206 std::string GetSourceUrl() const override; 207 bool HasImageContents() const override; 208 bool IsEditable() const override; 209 int GetEditStateFlags() const override; 210 int GetSourceType() const override; 211 int GetMediaType() const override; 212 int GetInputFieldType() const override; 213 std::string GetSelectionText() const override; 214 215 private: 216 std::shared_ptr<OHOS::NWeb::NWebContextMenuParams> param_; 217 }; 218 219 class ContextMenuResultOhos : public ContextMenuResult { DECLARE_ACE_TYPE(ContextMenuResultOhos,ContextMenuResult)220 DECLARE_ACE_TYPE(ContextMenuResultOhos, ContextMenuResult) 221 222 public: 223 ContextMenuResultOhos(std::shared_ptr<OHOS::NWeb::NWebContextMenuCallback> callback) : callback_(callback) {} 224 225 void Cancel() const override; 226 void CopyImage() const override; 227 void Copy() const override; 228 void Paste() const override; 229 void Cut() const override; 230 void SelectAll() const override; 231 232 private: 233 std::shared_ptr<OHOS::NWeb::NWebContextMenuCallback> callback_; 234 }; 235 236 class WebGeolocationOhos : public WebGeolocation { DECLARE_ACE_TYPE(WebGeolocationOhos,WebGeolocation)237 DECLARE_ACE_TYPE(WebGeolocationOhos, WebGeolocation) 238 239 public: 240 WebGeolocationOhos( 241 const std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface>& 242 callback, bool incognito) 243 : geolocationCallback_(callback), incognito_(incognito) {} 244 245 void Invoke(const std::string& origin, const bool& allow, const bool& retain) override; 246 247 private: 248 std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> geolocationCallback_; 249 bool incognito_ = false; 250 }; 251 252 class WebPermissionRequestOhos : public WebPermissionRequest { DECLARE_ACE_TYPE(WebPermissionRequestOhos,WebPermissionRequest)253 DECLARE_ACE_TYPE(WebPermissionRequestOhos, WebPermissionRequest) 254 255 public: 256 WebPermissionRequestOhos(const std::shared_ptr<OHOS::NWeb::NWebAccessRequest>& request) : request_(request) {} 257 258 void Deny() const override; 259 260 std::string GetOrigin() const override; 261 262 std::vector<std::string> GetResources() const override; 263 264 void Grant(std::vector<std::string>& resources) const override; 265 266 private: 267 std::shared_ptr<OHOS::NWeb::NWebAccessRequest> request_; 268 }; 269 270 class WebScreenCaptureRequestOhos : public WebScreenCaptureRequest { DECLARE_ACE_TYPE(WebScreenCaptureRequestOhos,WebScreenCaptureRequest)271 DECLARE_ACE_TYPE(WebScreenCaptureRequestOhos, WebScreenCaptureRequest) 272 273 public: 274 WebScreenCaptureRequestOhos(const std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest>& request) 275 : request_(request) {} 276 277 void Deny() const override; 278 279 std::string GetOrigin() const override; 280 281 void SetCaptureMode(int32_t mode) override; 282 283 void SetSourceId(int32_t sourceId) override; 284 285 void Grant() const override; 286 287 private: 288 std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest> request_; 289 290 OHOS::NWeb::NWebScreenCaptureConfig config_; 291 }; 292 293 class WebWindowNewHandlerOhos : public WebWindowNewHandler { DECLARE_ACE_TYPE(WebWindowNewHandlerOhos,WebWindowNewHandler)294 DECLARE_ACE_TYPE(WebWindowNewHandlerOhos, WebWindowNewHandler) 295 296 public: 297 WebWindowNewHandlerOhos(const std::shared_ptr<OHOS::NWeb::NWebControllerHandler>& handler, int32_t parentNWebId) 298 : handler_(handler), parentNWebId_(parentNWebId) {} 299 300 void SetWebController(int32_t id) override; 301 302 bool IsFrist() const override; 303 304 int32_t GetId() const override; 305 306 int32_t GetParentNWebId() const override; 307 308 private: 309 std::shared_ptr<OHOS::NWeb::NWebControllerHandler> handler_; 310 int32_t parentNWebId_ = -1; 311 }; 312 313 class DataResubmittedOhos : public DataResubmitted { DECLARE_ACE_TYPE(DataResubmittedOhos,DataResubmitted)314 DECLARE_ACE_TYPE(DataResubmittedOhos, DataResubmitted) 315 316 public: 317 DataResubmittedOhos(std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler) : handler_(handler) {} 318 void Resend() override; 319 void Cancel() override; 320 321 private: 322 std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler_; 323 }; 324 325 class FaviconReceivedOhos : public WebFaviconReceived { DECLARE_ACE_TYPE(FaviconReceivedOhos,WebFaviconReceived)326 DECLARE_ACE_TYPE(FaviconReceivedOhos, WebFaviconReceived) 327 328 public: 329 FaviconReceivedOhos( 330 const void* data, 331 size_t width, 332 size_t height, 333 OHOS::NWeb::ImageColorType colorType, 334 OHOS::NWeb::ImageAlphaType alphaType) 335 : data_(data), width_(width), height_(height), colorType_(colorType), alphaType_(alphaType) {} 336 const void* GetData() override; 337 size_t GetWidth() override; 338 size_t GetHeight() override; 339 int GetColorType() override; 340 int GetAlphaType() override; 341 342 private: 343 const void* data_ = nullptr; 344 size_t width_ = 0; 345 size_t height_ = 0; 346 OHOS::NWeb::ImageColorType colorType_ = OHOS::NWeb::ImageColorType::COLOR_TYPE_UNKNOWN; 347 OHOS::NWeb::ImageAlphaType alphaType_ = OHOS::NWeb::ImageAlphaType::ALPHA_TYPE_UNKNOWN; 348 }; 349 350 class WebSurfaceCallback : public OHOS::SurfaceDelegate::ISurfaceCallback { 351 352 public: WebSurfaceCallback(const WeakPtr<WebDelegate> & delegate)353 WebSurfaceCallback(const WeakPtr<WebDelegate>& delegate) : delegate_(delegate) {} 354 ~WebSurfaceCallback() = default; 355 356 void OnSurfaceCreated(const OHOS::sptr<OHOS::Surface>& surface) override; 357 void OnSurfaceChanged(const OHOS::sptr<OHOS::Surface>& surface, int32_t width, int32_t height) override; 358 void OnSurfaceDestroyed() override; 359 private: 360 WeakPtr<WebDelegate> delegate_; 361 362 }; 363 364 enum class DragAction { 365 DRAG_START = 0, 366 DRAG_ENTER, 367 DRAG_LEAVE, 368 DRAG_OVER, 369 DRAG_DROP, 370 DRAG_END, 371 DRAG_CANCEL, 372 }; 373 374 namespace NG { 375 class WebPattern; 376 }; // namespace NG 377 378 class RenderWeb; 379 380 class WebDelegateObserver : public virtual AceType { 381 DECLARE_ACE_TYPE(WebDelegateObserver, AceType); 382 public: WebDelegateObserver(const RefPtr<WebDelegate> & delegate,WeakPtr<PipelineBase> context)383 WebDelegateObserver(const RefPtr<WebDelegate>& delegate, WeakPtr<PipelineBase> context) 384 : delegate_(delegate), context_(context) 385 {} 386 ~WebDelegateObserver(); 387 void NotifyDestory(); 388 389 private: 390 RefPtr<WebDelegate> delegate_; 391 WeakPtr<PipelineBase> context_; 392 }; 393 394 enum class ScriptItemType { 395 DOCUMENT_START = 0, 396 DOCUMENT_END 397 }; 398 399 class WebDelegate : public WebResource { 400 DECLARE_ACE_TYPE(WebDelegate, WebResource); 401 402 public: 403 using CreatedCallback = std::function<void()>; 404 using ReleasedCallback = std::function<void(bool)>; 405 using EventCallback = std::function<void(const std::string&)>; 406 using EventCallbackV2 = std::function<void(const std::shared_ptr<BaseEventInfo>&)>; 407 enum class State : char { 408 WAITINGFORSIZE, 409 CREATING, 410 CREATED, 411 CREATEFAILED, 412 RELEASED, 413 }; 414 415 // for webcontoller, the enum is same as web_webview and core side 416 enum class JavaScriptObjIdErrorCode : int32_t { WEBCONTROLLERERROR = -2, WEBVIEWCONTROLLERERROR = -1, END = 0 }; 417 418 WebDelegate() = delete; 419 ~WebDelegate() override; WebDelegate(const WeakPtr<PipelineBase> & context,ErrorCallback && onError,const std::string & type)420 WebDelegate(const WeakPtr<PipelineBase>& context, ErrorCallback&& onError, const std::string& type) 421 : WebResource(type, context, std::move(onError)) 422 {} 423 SetObserver(const RefPtr<WebDelegateObserver> & observer)424 void SetObserver(const RefPtr<WebDelegateObserver>& observer) 425 { 426 observer_ = observer; 427 }; 428 void SetRenderWeb(const WeakPtr<RenderWeb>& renderWeb); 429 430 void CreatePlatformResource(const Size& size, const Offset& position, const WeakPtr<PipelineContext>& context); 431 void CreatePluginResource(const Size& size, const Offset& position, const WeakPtr<PipelineContext>& context); 432 void AddCreatedCallback(const CreatedCallback& createdCallback); 433 void RemoveCreatedCallback(); 434 void AddReleasedCallback(const ReleasedCallback& releasedCallback); 435 void SetComponent(const RefPtr<WebComponent>& component); 436 void RemoveReleasedCallback(); 437 void Reload(); 438 void UpdateUrl(const std::string& url); 439 #ifdef OHOS_STANDARD_SYSTEM 440 // TODO: add to separate this file into three file, base file, component impl and ng impl. 441 void InitOHOSWeb(const RefPtr<PipelineBase>& context, const RefPtr<NG::RenderSurface>& surface); 442 void InitOHOSWeb(const WeakPtr<PipelineBase>& context); 443 bool PrepareInitOHOSWeb(const WeakPtr<PipelineBase>& context); 444 void InitWebViewWithWindow(); 445 void ShowWebView(); 446 void HideWebView(); 447 void Resize(const double& width, const double& height, bool isKeyboard = false); GetRosenWindowId()448 int32_t GetRosenWindowId() 449 { 450 return rosenWindowId_; 451 } 452 void SetKeepScreenOn(bool key); 453 void UpdateUserAgent(const std::string& userAgent); 454 void UpdateBackgroundColor(const int backgroundColor); 455 void UpdateInitialScale(float scale); 456 void UpdateJavaScriptEnabled(const bool& isJsEnabled); 457 void UpdateAllowFileAccess(const bool& isFileAccessEnabled); 458 void UpdateBlockNetworkImage(const bool& onLineImageAccessEnabled); 459 void UpdateLoadsImagesAutomatically(const bool& isImageAccessEnabled); 460 void UpdateMixedContentMode(const MixedModeContent& mixedMode); 461 void UpdateSupportZoom(const bool& isZoomAccessEnabled); 462 void UpdateDomStorageEnabled(const bool& isDomStorageAccessEnabled); 463 void UpdateGeolocationEnabled(const bool& isGeolocationAccessEnabled); 464 void UpdateCacheMode(const WebCacheMode& mode); 465 std::shared_ptr<OHOS::NWeb::NWeb> GetNweb(); 466 bool GetForceDarkMode(); 467 void OnConfigurationUpdated(const std::string& colorMode); 468 void UpdateDarkMode(const WebDarkMode& mode); 469 void UpdateDarkModeAuto(RefPtr<WebDelegate> delegate, std::shared_ptr<OHOS::NWeb::NWebPreference> setting); 470 void UpdateForceDarkAccess(const bool& access); 471 void UpdateAudioResumeInterval(const int32_t& resumeInterval); 472 void UpdateAudioExclusive(const bool& audioExclusive); 473 void UpdateOverviewModeEnabled(const bool& isOverviewModeAccessEnabled); 474 void UpdateFileFromUrlEnabled(const bool& isFileFromUrlAccessEnabled); 475 void UpdateDatabaseEnabled(const bool& isDatabaseAccessEnabled); 476 void UpdateTextZoomRatio(const int32_t& textZoomRatioNum); 477 void UpdateWebDebuggingAccess(bool isWebDebuggingAccessEnabled); 478 void UpdatePinchSmoothModeEnabled(bool isPinchSmoothModeEnabled); 479 void UpdateMediaPlayGestureAccess(bool isNeedGestureAccess); 480 void UpdateMultiWindowAccess(bool isMultiWindowAccessEnabled); 481 void UpdateAllowWindowOpenMethod(bool isAllowWindowOpenMethod); 482 void UpdateWebCursiveFont(const std::string& cursiveFontFamily); 483 void UpdateWebFantasyFont(const std::string& fantasyFontFamily); 484 void UpdateWebFixedFont(const std::string& fixedFontFamily); 485 void UpdateWebSansSerifFont(const std::string& sansSerifFontFamily); 486 void UpdateWebSerifFont(const std::string& serifFontFamily); 487 void UpdateWebStandardFont(const std::string& standardFontFamily); 488 void UpdateDefaultFixedFontSize(int32_t size); 489 void UpdateDefaultFontSize(int32_t defaultFontSize); 490 void UpdateMinFontSize(int32_t minFontSize); 491 void UpdateMinLogicalFontSize(int32_t minLogicalFontSize); 492 void UpdateBlockNetwork(bool isNetworkBlocked); 493 void UpdateHorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled); 494 void UpdateVerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled); 495 void UpdateScrollBarColor(const std::string& colorValue); 496 void UpdateOverScrollMode(const int32_t overscrollModeValue); 497 void UpdateNativeEmbedModeEnabled(bool isEmbedModeEnabled); 498 void UpdateCopyOptionMode(const int32_t copyOptionModeValue); 499 void LoadUrl(); 500 void CreateWebMessagePorts(std::vector<RefPtr<WebMessagePort>>& ports); 501 void PostWebMessage(std::string& message, std::vector<RefPtr<WebMessagePort>>& ports, std::string& uri); 502 void ClosePort(std::string& handle); 503 void PostPortMessage(std::string& handle, std::string& data); 504 void SetPortMessageCallback(std::string& handle, std::function<void(const std::string&)>&& callback); 505 void HandleTouchDown(const int32_t& id, const double& x, const double& y, bool from_overlay = false); 506 void HandleTouchUp(const int32_t& id, const double& x, const double& y, bool from_overlay = false); 507 void HandleTouchMove(const int32_t& id, const double& x, const double& y, bool from_overlay = false); 508 void HandleTouchMove(const std::list<OHOS::NWeb::TouchPointInfo>& touchPointInfoList, bool fromOverlay = false); 509 void HandleTouchCancel(); 510 void HandleAxisEvent(const double& x, const double& y, const double& deltaX, const double& deltaY); 511 bool OnKeyEvent(int32_t keyCode, int32_t keyAction); 512 void OnMouseEvent(int32_t x, int32_t y, const MouseButton button, const MouseAction action, int count); 513 void OnFocus(); 514 bool NeedSoftKeyboard(); 515 void OnBlur(); 516 void OnPermissionRequestPrompt(const std::shared_ptr<OHOS::NWeb::NWebAccessRequest>& request); 517 void OnScreenCaptureRequest(const std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest>& request); 518 bool RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params, 519 std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback); 520 void OnQuickMenuDismissed(); 521 void OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle, 522 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle, 523 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle); 524 void HandleDragEvent(int32_t x, int32_t y, const DragAction& dragAction); 525 RefPtr<PixelMap> GetDragPixelMap(); 526 std::string GetUrl(); 527 void UpdateLocale(); 528 void SetDrawRect(int32_t x, int32_t y, int32_t width, int32_t height); 529 void OnInactive(); 530 void OnActive(); 531 void OnWebviewHide(); 532 void OnWebviewShow(); 533 bool OnCursorChange(const OHOS::NWeb::CursorType& type, const OHOS::NWeb::NWebCursorInfo& info); 534 void OnSelectPopupMenu( 535 std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params, 536 std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback); 537 void SetShouldFrameSubmissionBeforeDraw(bool should); SetBackgroundColor(int32_t backgroundColor)538 void SetBackgroundColor(int32_t backgroundColor) 539 { 540 backgroundColor_ = backgroundColor; 541 } 542 void NotifyMemoryLevel(int32_t level); 543 void SetAudioMuted(bool muted); SetRichtextIdentifier(std::optional<std::string> & richtextData)544 void SetRichtextIdentifier(std::optional<std::string>& richtextData) 545 { 546 richtextData_ = richtextData; 547 } 548 #endif 549 void OnErrorReceive(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 550 std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error); 551 void OnHttpErrorReceive(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 552 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response); 553 RefPtr<WebResponse> OnInterceptRequest(const std::shared_ptr<BaseEventInfo>& info); 554 bool IsEmptyOnInterceptRequest(); 555 void RecordWebEvent(Recorder::EventType eventType, const std::string& param) const; 556 void OnPageStarted(const std::string& param); 557 void OnPageFinished(const std::string& param); 558 void OnProgressChanged(int param); 559 void OnReceivedTitle(const std::string& param); 560 void ExitFullScreen(); 561 void OnFullScreenExit(); 562 void OnGeolocationPermissionsHidePrompt(); 563 void OnGeolocationPermissionsShowPrompt( 564 const std::string& origin, const std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface>& callback); 565 void OnCompleteSwapWithNewSize(); 566 void OnResizeNotWork(); 567 void OnDateTimeChooserPopup( 568 const NWeb::DateTimeChooser& chooser, 569 const std::vector<OHOS::NWeb::DateTimeSuggestion>& suggestions, 570 std::shared_ptr<OHOS::NWeb::NWebDateTimeChooserCallback> callback); 571 void OnDateTimeChooserClose(); 572 void OnRequestFocus(); 573 bool OnCommonDialog(const std::shared_ptr<BaseEventInfo>& info, DialogEventType dialogEventType); 574 bool OnHttpAuthRequest(const std::shared_ptr<BaseEventInfo>& info); 575 bool OnSslErrorRequest(const std::shared_ptr<BaseEventInfo>& info); 576 bool OnSslSelectCertRequest(const std::shared_ptr<BaseEventInfo>& info); 577 void OnDownloadStart(const std::string& url, const std::string& userAgent, const std::string& contentDisposition, 578 const std::string& mimetype, long contentLength); 579 void OnAccessibilityEvent(int64_t accessibilityId, AccessibilityEventType eventType); 580 void OnPageError(const std::string& param); 581 void OnMessage(const std::string& param); 582 void OnFullScreenEnter(std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler); 583 bool OnConsoleLog(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message); 584 void OnRouterPush(const std::string& param); 585 void OnRenderExited(OHOS::NWeb::RenderExitReason reason); 586 void OnRefreshAccessedHistory(const std::string& url, bool isRefreshed); 587 bool OnFileSelectorShow(const std::shared_ptr<BaseEventInfo>& info); 588 bool OnContextMenuShow(const std::shared_ptr<BaseEventInfo>& info); 589 void OnContextMenuHide(const std::string& info); 590 bool OnHandleInterceptUrlLoading(const std::string& url); 591 bool OnHandleInterceptLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request); 592 void OnResourceLoad(const std::string& url); 593 void OnScaleChange(float oldScaleFactor, float newScaleFactor); 594 void OnScroll(double xOffset, double yOffset); 595 bool LoadDataWithRichText(); 596 void OnSearchResultReceive(int activeMatchOrdinal, int numberOfMatches, bool isDoneCounting); 597 bool OnDragAndDropData(const void* data, size_t len, int width, int height); 598 bool OnDragAndDropDataUdmf(std::shared_ptr<OHOS::NWeb::NWebDragData> dragData); 599 std::shared_ptr<OHOS::NWeb::NWebDragData> GetOrCreateDragData(); 600 bool IsImageDrag(); 601 std::shared_ptr<OHOS::NWeb::NWebDragData> dragData_ = nullptr; 602 std::string tempDir_; UpdateDragCursor(NWeb::NWebDragData::DragOperation op)603 void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) 604 { 605 op_ = op; 606 } GetDragAcceptableStatus()607 NWeb::NWebDragData::DragOperation GetDragAcceptableStatus() 608 { 609 return op_; 610 } 611 NWeb::NWebDragData::DragOperation op_ = NWeb::NWebDragData::DragOperation::DRAG_OPERATION_NONE; 612 void OnWindowNew(const std::string& targetUrl, bool isAlert, bool isUserTrigger, 613 const std::shared_ptr<OHOS::NWeb::NWebControllerHandler>& handler); 614 void OnWindowExit(); 615 void OnPageVisible(const std::string& url); 616 void OnDataResubmitted(std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler); 617 void OnNavigationEntryCommitted(std::shared_ptr<OHOS::NWeb::NWebLoadCommittedDetails> details); 618 void OnFaviconReceived(const void* data, size_t width, size_t height, OHOS::NWeb::ImageColorType colorType, 619 OHOS::NWeb::ImageAlphaType alphaType); 620 void OnTouchIconUrl(const std::string& iconUrl, bool precomposed); 621 void OnAudioStateChanged(bool audible); 622 void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs); 623 void OnSafeBrowsingCheckResult(int threat_type); 624 void OnGetTouchHandleHotZone(OHOS::NWeb::TouchHandleHotZone& hotZone); 625 void OnOverScroll(float xOffset, float yOffset); 626 void OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling); 627 void OnScrollState(bool scrollState); 628 void OnRootLayerChanged(int width, int height); 629 bool FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity); 630 void OnNativeEmbedLifecycleChange(const NWeb::NativeEmbedDataInfo& dataInfo); 631 void OnNativeEmbedGestureEvent(const NWeb::NativeEmbedTouchEvent& event); 632 void SetNGWebPattern(const RefPtr<NG::WebPattern>& webPattern); 633 bool RequestFocus(); 634 void SetDrawSize(const Size& drawSize); 635 void SetEnhanceSurfaceFlag(const bool& isEnhanceSurface); 636 EGLConfig GLGetConfig(int version, EGLDisplay eglDisplay); 637 void GLContextInit(void* window); 638 sptr<OHOS::SurfaceDelegate> GetSurfaceDelegateClient(); 639 void SetBoundsOrResize(const Size& drawSize, const Offset& offset, bool isKeyboard = false); 640 Offset GetWebRenderGlobalPos(); 641 bool InitWebSurfaceDelegate(const WeakPtr<PipelineBase>& context); 642 int GetWebId(); 643 void JavaScriptOnDocumentStart(); 644 void JavaScriptOnDocumentEnd(); 645 void SetJavaScriptItems(const ScriptItems& scriptItems, const ScriptItemType& type); 646 void SetTouchEventInfo(const OHOS::NWeb::NativeEmbedTouchEvent& touchEvent, TouchEventInfo& touchEventInfo); 647 #if defined(ENABLE_ROSEN_BACKEND) 648 void SetSurface(const sptr<Surface>& surface); 649 sptr<Surface> surface_ = nullptr; 650 #endif 651 #ifdef OHOS_STANDARD_SYSTEM SetWebRendeGlobalPos(const Offset & pos)652 void SetWebRendeGlobalPos(const Offset& pos) 653 { 654 offset_ = pos; 655 } SetBlurReason(const OHOS::NWeb::BlurReason & blurReason)656 void SetBlurReason(const OHOS::NWeb::BlurReason& blurReason) 657 { 658 blurReason_ = blurReason; 659 } SetPopup(bool popup)660 void SetPopup(bool popup) 661 { 662 isPopup_ = popup; 663 } SetParentNWebId(int32_t parentNWebId)664 void SetParentNWebId(int32_t parentNWebId) 665 { 666 parentNWebId_ = parentNWebId; 667 } 668 #endif 669 void SetToken(); 670 void SetWebType(WebType type); 671 void SetVirtualKeyBoardArg(int32_t width, int32_t height, double keyboard); 672 bool ShouldVirtualKeyboardOverlay(); 673 void ScrollBy(float deltaX, float deltaY); 674 void ExecuteAction(int64_t accessibilityId, AceAction action); 675 bool GetFocusedAccessibilityNodeInfo( 676 int64_t accessibilityId, bool isAccessibilityFocus, OHOS::NWeb::NWebAccessibilityNodeInfo& nodeInfo) const; 677 bool GetAccessibilityNodeInfoById(int64_t accessibilityId, OHOS::NWeb::NWebAccessibilityNodeInfo& nodeInfo) const; 678 bool GetAccessibilityNodeInfoByFocusMove( 679 int64_t accessibilityId, int32_t direction, OHOS::NWeb::NWebAccessibilityNodeInfo& nodeInfo) const; 680 void SetAccessibilityState(bool state); 681 void UpdateAccessibilityState(bool state); 682 OHOS::NWeb::NWebPreference::CopyOptionMode GetCopyOptionMode() const; 683 private: 684 void InitWebEvent(); 685 void RegisterWebEvent(); 686 void ReleasePlatformResource(); 687 void Stop(); 688 void UnregisterEvent(); 689 std::string GetUrlStringParam(const std::string& param, const std::string& name) const; 690 void CallWebRouterBack(); 691 void CallPopPageSuccessPageUrl(const std::string& url); 692 void CallIsPagePathInvalid(const bool& isPageInvalid); 693 694 void BindRouterBackMethod(); 695 void BindPopPageSuccessMethod(); 696 void BindIsPagePathInvalidMethod(); 697 698 #ifdef OHOS_STANDARD_SYSTEM 699 sptr<OHOS::Rosen::Window> CreateWindow(); 700 void LoadUrl(const std::string& url, const std::map<std::string, std::string>& httpHeaders); 701 void ExecuteTypeScript(const std::string& jscode, const std::function<void(std::string)>&& callback); 702 void LoadDataWithBaseUrl(const std::string& baseUrl, const std::string& data, const std::string& mimeType, 703 const std::string& encoding, const std::string& historyUrl); 704 void Refresh(); 705 void StopLoading(); 706 void AddJavascriptInterface(const std::string& objectName, const std::vector<std::string>& methodList); 707 void RemoveJavascriptInterface(const std::string& objectName, const std::vector<std::string>& methodList); 708 void SetWebViewJavaScriptResultCallBack(const WebController::JavaScriptCallBackImpl&& javaScriptCallBackImpl); 709 void Zoom(float factor); 710 bool ZoomIn(); 711 bool ZoomOut(); 712 int ConverToWebHitTestType(int hitType); 713 int GetHitTestResult(); 714 void GetHitTestValue(HitTestResult& result); 715 int GetPageHeight(); 716 std::string GetTitle(); 717 std::string GetDefaultUserAgent(); 718 bool SaveCookieSync(); 719 bool SetCookie(const std::string& url, 720 const std::string& value, 721 bool incognito_mode); 722 std::string GetCookie(const std::string& url, bool incognito_mode) const; 723 void DeleteEntirelyCookie(bool incognito_mode); 724 void RegisterOHOSWebEventAndMethord(); 725 void SetWebCallBack(); 726 void RunSetWebIdAndHapPathCallback(); 727 void RunJsProxyCallback(); 728 void RegisterConfigObserver(); 729 void UnRegisterConfigObserver(); 730 731 // Backward and forward 732 void Backward(); 733 void Forward(); 734 void ClearHistory(); 735 void ClearSslCache(); 736 void ClearClientAuthenticationCache(); 737 bool AccessStep(int32_t step); 738 void BackOrForward(int32_t step); 739 bool AccessBackward(); 740 bool AccessForward(); 741 742 void SearchAllAsync(const std::string& searchStr); 743 void ClearMatches(); 744 void SearchNext(bool forward); 745 746 void UpdateSettting(bool useNewPipe = false); 747 748 std::string GetCustomScheme(); 749 void InitWebViewWithSurface(); 750 Size GetEnhanceSurfaceSize(const Size& drawSize); 751 void UpdateScreenOffSet(double& offsetX, double& offsetY); 752 void RegisterSurfacePositionChangedCallback(); 753 void UnregisterSurfacePositionChangedCallback(); 754 755 void NotifyPopupWindowResult(bool result); 756 757 EventCallbackV2 GetAudioStateChangedCallback(bool useNewPipe, const RefPtr<NG::WebEventHub>& eventHub); 758 void SurfaceOcclusionCallback(float visibleRatio); 759 void RegisterSurfaceOcclusionChangeFun(); 760 void ratioStrToFloat(const std::string& str); 761 #endif 762 763 WeakPtr<WebComponent> webComponent_; 764 WeakPtr<RenderWeb> renderWeb_; 765 766 WeakPtr<NG::WebPattern> webPattern_; 767 768 std::list<CreatedCallback> createdCallbacks_; 769 std::list<ReleasedCallback> releasedCallbacks_; 770 EventCallback onPageStarted_; 771 EventCallback onPageFinished_; 772 EventCallback onPageError_; 773 EventCallback onMessage_; 774 Method reloadMethod_; 775 Method updateUrlMethod_; 776 Method routerBackMethod_; 777 Method changePageUrlMethod_; 778 Method isPagePathInvalidMethod_; 779 State state_ { State::WAITINGFORSIZE }; 780 #ifdef OHOS_STANDARD_SYSTEM 781 std::shared_ptr<OHOS::NWeb::NWeb> nweb_; 782 OHOS::NWeb::NWebCookieManager* cookieManager_ = nullptr; 783 sptr<Rosen::Window> window_; 784 bool isCreateWebView_ = false; 785 int32_t callbackId_ = 0; 786 787 EventCallbackV2 onPageFinishedV2_; 788 EventCallbackV2 onPageStartedV2_; 789 EventCallbackV2 onProgressChangeV2_; 790 EventCallbackV2 onTitleReceiveV2_; 791 EventCallbackV2 onFullScreenExitV2_; 792 EventCallbackV2 onGeolocationHideV2_; 793 EventCallbackV2 onGeolocationShowV2_; 794 EventCallbackV2 onRequestFocusV2_; 795 EventCallbackV2 onErrorReceiveV2_; 796 EventCallbackV2 onHttpErrorReceiveV2_; 797 EventCallbackV2 onDownloadStartV2_; 798 EventCallbackV2 onRefreshAccessedHistoryV2_; 799 EventCallbackV2 onRenderExitedV2_; 800 EventCallbackV2 onResourceLoadV2_; 801 EventCallbackV2 onScaleChangeV2_; 802 EventCallbackV2 onScrollV2_; 803 EventCallbackV2 onPermissionRequestV2_; 804 EventCallbackV2 onSearchResultReceiveV2_; 805 EventCallbackV2 onWindowExitV2_; 806 EventCallbackV2 onPageVisibleV2_; 807 EventCallbackV2 onTouchIconUrlV2_; 808 EventCallbackV2 onAudioStateChangedV2_; 809 EventCallbackV2 onFirstContentfulPaintV2_; 810 EventCallbackV2 onOverScrollV2_; 811 EventCallbackV2 onScreenCaptureRequestV2_; 812 EventCallbackV2 onNavigationEntryCommittedV2_; 813 EventCallbackV2 onSafeBrowsingCheckResultV2_; 814 EventCallbackV2 OnNativeEmbedLifecycleChangeV2_; 815 EventCallbackV2 OnNativeEmbedGestureEventV2_; 816 817 int32_t webType_; 818 std::string bundlePath_; 819 std::string bundleDataPath_; 820 std::string hapPath_; 821 RefPtr<PixelMap> pixelMap_ = nullptr; 822 bool isRefreshPixelMap_ = false; 823 Size drawSize_; 824 Offset offset_; 825 bool isEnhanceSurface_ = false; 826 sptr<WebSurfaceCallback> surfaceCallback_; 827 sptr<OHOS::SurfaceDelegate> surfaceDelegate_; 828 EGLNativeWindowType mEglWindow; 829 EGLDisplay mEGLDisplay = EGL_NO_DISPLAY; 830 EGLConfig mEGLConfig = nullptr; 831 EGLContext mEGLContext = EGL_NO_CONTEXT; 832 EGLContext mSharedEGLContext = EGL_NO_CONTEXT; 833 EGLSurface mEGLSurface = nullptr; 834 WindowsSurfaceInfo surfaceInfo_; 835 bool forceDarkMode_ = false; 836 sptr<AppExecFwk::IConfigurationObserver> configChangeObserver_ = nullptr; 837 OHOS::NWeb::BlurReason blurReason_ = OHOS::NWeb::BlurReason::FOCUS_SWITCH; 838 bool isPopup_ = false; 839 int32_t parentNWebId_ = -1; 840 bool needResizeAtFirst_ = false; 841 int32_t backgroundColor_ = 0xffffffff; 842 uint32_t rosenWindowId_ = 0; 843 RefPtr<WebDelegateObserver> observer_; 844 std::shared_ptr<Rosen::RSNode> rsNode_; 845 Rosen::NodeId surfaceNodeId_ = 0; 846 float visibleRatio_ = 1.0; 847 uint32_t delayTime_ = 500; 848 float lowerFrameRateVisibleRatio_ = 0.1; 849 std::optional<ScriptItems> onDocumentStartScriptItems_; 850 std::optional<ScriptItems> onDocumentEndScriptItems_; 851 bool accessibilityState_ = false; 852 std::optional<std::string> richtextData_; 853 bool incognitoMode_ = false; 854 bool isEmbedModeEnabled_ = false; 855 #endif 856 }; 857 858 } // namespace OHOS::Ace 859 860 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_DELEGATE_H 861