1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_WEB_PATTERN_WEB_DELEGATE_CROSS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_WEB_PATTERN_WEB_DELEGATE_CROSS_H 18 19 #include "web_object_event.h" 20 21 #include "base/log/log.h" 22 #include "core/common/container.h" 23 #include "core/common/recorder/event_recorder.h" 24 #include "core/components_ng/pattern/web/cross_platform/web_pattern.h" 25 #include "core/components_ng/pattern/web/cross_platform/web_resource.h" 26 #include "core/components_ng/pattern/web/web_delegate_interface.h" 27 #include "core/pipeline/pipeline_base.h" 28 29 namespace OHOS::Ace { 30 class WebResourceRequsetImpl : public AceType { 31 DECLARE_ACE_TYPE(WebResourceRequsetImpl, AceType); 32 public: WebResourceRequsetImpl(void * object)33 explicit WebResourceRequsetImpl(void* object) : object_(object) {} 34 std::map<std::string, std::string> GetRequestHeader() const; 35 std::string GetRequestUrl() const; 36 std::string GetMethod() const; 37 bool IsRequestGesture() const; 38 bool IsMainFrame() const; 39 bool IsRedirect() const; 40 private: 41 void* object_ = nullptr; 42 }; 43 44 class WebOffsetImpl : public AceType { 45 DECLARE_ACE_TYPE(WebOffsetImpl, AceType); 46 public: WebOffsetImpl(void * object)47 explicit WebOffsetImpl(void* object) : object_(object) {} 48 double GetX() const; 49 double GetY() const; 50 private: 51 void* object_ = nullptr; 52 }; 53 54 class WebScaleChangeImpl : public AceType { 55 DECLARE_ACE_TYPE(WebScaleChangeImpl, AceType); 56 public: WebScaleChangeImpl(void * object)57 explicit WebScaleChangeImpl(void* object) : object_(object) {} 58 float GetNewScale() const; 59 float GetOldScale() const; 60 private: 61 void* object_ = nullptr; 62 }; 63 64 class WebResourceResponseImpl : public AceType { 65 DECLARE_ACE_TYPE(WebResourceResponseImpl, AceType); 66 public: WebResourceResponseImpl(void * object)67 explicit WebResourceResponseImpl(void* object) : object_(object) {} 68 std::map<std::string, std::string> GetResponseHeader() const; 69 std::string GetResponseData() const; 70 std::string GetEncoding() const; 71 std::string GetMimeType() const; 72 std::string GetReason() const; 73 int GetStatusCode() const; 74 private: 75 void* object_ = nullptr; 76 }; 77 78 class WebConsoleMessage : public AceType { 79 DECLARE_ACE_TYPE(WebConsoleMessage, AceType); 80 public: WebConsoleMessage(void * object)81 explicit WebConsoleMessage(void* object) : object_(object) {} 82 std::string GetMessage() const; 83 int GetMessageLevel() const; 84 std::string GetSourceId() const; 85 int GetLineNumber() const; 86 private: 87 void* object_ = nullptr; 88 }; 89 90 class DialogResult : public Result { 91 DECLARE_ACE_TYPE(DialogResult, Result); 92 public: DialogResult(void * object,DialogEventType dialogEventType)93 DialogResult(void* object, DialogEventType dialogEventType) : object_(object), dialogEventType_(dialogEventType) 94 { 95 auto obj = WebObjectEventManager::GetInstance().GetCommonDialogObject(); 96 index_ = obj->AddObject(object); 97 } ~DialogResult()98 ~DialogResult() 99 { 100 auto obj = WebObjectEventManager::GetInstance().GetCommonDialogObject(); 101 obj->DelObject(index_); 102 } 103 void Confirm(const std::string& promptResult) override; 104 void Confirm() override; 105 void Cancel() override; 106 107 private: 108 void* object_ = nullptr; 109 DialogEventType dialogEventType_; 110 int index_; 111 }; 112 113 class WebAuthResult : public AuthResult { 114 DECLARE_ACE_TYPE(WebAuthResult, AuthResult); 115 public: WebAuthResult(void * object)116 explicit WebAuthResult(void* object) : object_(object) 117 { 118 auto obj = WebObjectEventManager::GetInstance().GetHttpAuthRequestObject(); 119 index_ = obj->AddObject(object); 120 } ~WebAuthResult()121 ~WebAuthResult() 122 { 123 auto obj = WebObjectEventManager::GetInstance().GetHttpAuthRequestObject(); 124 obj->DelObject(index_); 125 } 126 bool Confirm(std::string& userName, std::string& pwd) override; 127 bool IsHttpAuthInfoSaved() override; 128 void Cancel() override; 129 130 private: 131 void* object_ = nullptr; 132 int index_; 133 }; 134 135 class WebCommonDialogImpl : public AceType { 136 DECLARE_ACE_TYPE(WebCommonDialogImpl, AceType); 137 public: WebCommonDialogImpl(void * object)138 explicit WebCommonDialogImpl(void* object) : object_(object) {} 139 std::string GetUrl() const; 140 std::string GetMessage() const; 141 std::string GetValue() const; 142 143 private: 144 void* object_ = nullptr; 145 }; 146 147 class PermissionRequestImpl : public WebPermissionRequest { 148 DECLARE_ACE_TYPE(PermissionRequestImpl, WebPermissionRequest); 149 public: PermissionRequestImpl(void * object)150 explicit PermissionRequestImpl(void* object) : object_(object) 151 { 152 auto obj = WebObjectEventManager::GetInstance().GetPermissionRequestObject(); 153 index_ = obj->AddObject(object); 154 } ~PermissionRequestImpl()155 ~PermissionRequestImpl() 156 { 157 auto obj = WebObjectEventManager::GetInstance().GetPermissionRequestObject(); 158 obj->DelObject(index_); 159 } 160 161 void Deny() const override; 162 std::string GetOrigin() const override; 163 std::vector<std::string> GetResources() const override; 164 void Grant(std::vector<std::string>& resources) const override; 165 void SetOrigin(); 166 void SetResources(); 167 168 private: 169 void* object_ = nullptr; 170 int index_; 171 std::string origin_; 172 std::vector<std::string> resources_; 173 }; 174 175 class WebFileSelectorResult : public FileSelectorResult { 176 DECLARE_ACE_TYPE(WebFileSelectorResult, FileSelectorResult); 177 public: WebFileSelectorResult(void * object)178 explicit WebFileSelectorResult(void* object) : object_(object) 179 { 180 auto obj = WebObjectEventManager::GetInstance().GetFileChooserObject(); 181 index_ = obj->AddObject(object); 182 } ~WebFileSelectorResult()183 ~WebFileSelectorResult() 184 { 185 auto obj = WebObjectEventManager::GetInstance().GetFileChooserObject(); 186 obj->DelObject(index_); 187 } 188 189 void HandleFileList(std::vector<std::string>& fileList) override; 190 191 private: 192 void* object_; 193 int index_; 194 }; 195 196 class WebFileChooserImpl : public AceType { 197 DECLARE_ACE_TYPE(WebFileChooserImpl, AceType); 198 public: WebFileChooserImpl(void * object)199 explicit WebFileChooserImpl(void* object) : object_(object) {} 200 std::string GetTitle() const; 201 int GetMode() const; 202 std::string GetDefaultFileName() const; 203 std::vector<std::string> GetAcceptType() const; 204 bool IsCapture() const; 205 206 private: 207 void* object_ = nullptr; 208 }; 209 210 class FileSelectorParam : public WebFileSelectorParam { 211 DECLARE_ACE_TYPE(FileSelectorParam, AceType); 212 public: FileSelectorParam(std::string title,int mode,std::string defaultFileName,std::vector<std::string> acceptType,bool isCapture)213 FileSelectorParam( 214 std::string title, int mode, std::string defaultFileName, std::vector<std::string> acceptType, bool isCapture) 215 : title_(title), mode_(mode), defaultFileName_(defaultFileName), acceptType_(acceptType), isCapture_(isCapture) 216 {} 217 ~FileSelectorParam() = default; 218 GetTitle()219 std::string GetTitle() override 220 { 221 return title_; 222 } GetMode()223 int GetMode() override 224 { 225 return mode_; 226 } GetDefaultFileName()227 std::string GetDefaultFileName() override 228 { 229 return defaultFileName_; 230 } GetAcceptType()231 std::vector<std::string> GetAcceptType() override 232 { 233 return acceptType_; 234 } IsCapture()235 bool IsCapture() override 236 { 237 return isCapture_; 238 } GetMimeType()239 std::vector<std::string> GetMimeType() override 240 { 241 return mimeType_; 242 } 243 244 private: 245 std::string title_; 246 int mode_; 247 std::string defaultFileName_; 248 std::vector<std::string> acceptType_; 249 bool isCapture_; 250 std::vector<std::string> mimeType_; 251 }; 252 253 class Geolocation : public WebGeolocation { 254 DECLARE_ACE_TYPE(Geolocation, WebGeolocation); 255 public: Geolocation(void * object)256 explicit Geolocation(void* object) : object_(object) 257 { 258 auto obj = WebObjectEventManager::GetInstance().GetGeolocationObject(); 259 index_ = obj->AddObject(object); 260 } ~Geolocation()261 ~Geolocation() 262 { 263 auto obj = WebObjectEventManager::GetInstance().GetGeolocationObject(); 264 obj->DelObject(index_); 265 } 266 void Invoke(const std::string& origin, const bool& allow, const bool& retain) override; 267 268 private: 269 void* object_ = nullptr; 270 int index_; 271 }; 272 273 class WebAuthRequestImpl : public AceType { 274 DECLARE_ACE_TYPE(WebAuthRequestImpl, AceType); 275 public: WebAuthRequestImpl(void * object)276 explicit WebAuthRequestImpl(void* object) : object_(object) {} 277 std::string GetHost() const; 278 std::string GetRealm() const; 279 280 private: 281 void* object_ = nullptr; 282 }; 283 284 class WebGeolocationImpl : public AceType { 285 DECLARE_ACE_TYPE(WebGeolocationImpl, AceType); 286 287 public: WebGeolocationImpl(void * object)288 explicit WebGeolocationImpl(void* object) : object_(object) {} 289 std::string GetOrigin() const; 290 private: 291 void* object_ = nullptr; 292 }; 293 294 class WebDownloadResponseImpl : public AceType { 295 DECLARE_ACE_TYPE(WebDownloadResponseImpl, AceType); 296 public: WebDownloadResponseImpl(void * object)297 explicit WebDownloadResponseImpl(void* object) : object_(object) {} 298 std::string GetUrl() const; 299 std::string GetMimetype() const; 300 long GetContentLength() const; 301 std::string GetUserAgent() const; 302 std::string GetContentDisposition() const; 303 304 private: 305 void* object_ = nullptr; 306 }; 307 308 class WebRefreshAccessedHistoryImpl : public AceType { 309 DECLARE_ACE_TYPE(WebRefreshAccessedHistoryImpl, AceType); 310 public: WebRefreshAccessedHistoryImpl(void * object)311 explicit WebRefreshAccessedHistoryImpl(void* object) : object_(object) {} 312 std::string GetUrl() const; 313 bool GetIsRefreshed() const; 314 315 private: 316 void* object_ = nullptr; 317 }; 318 319 class WebFullScreenEnterImpl : public AceType { 320 DECLARE_ACE_TYPE(WebFullScreenEnterImpl, AceType); 321 public: WebFullScreenEnterImpl(void * object)322 explicit WebFullScreenEnterImpl(void* object) : object_(object) {} 323 int GetWidths() const; 324 int GetHeights() const; 325 private: 326 void* object_ = nullptr; 327 }; 328 329 class WebFullScreenExitImpl : public AceType { 330 DECLARE_ACE_TYPE(WebFullScreenExitImpl, AceType); 331 public: WebFullScreenExitImpl(void * object)332 explicit WebFullScreenExitImpl(void* object) : object_(object) {} 333 334 private: 335 void* object_ = nullptr; 336 }; 337 338 class FullScreenExitHandlerImpl : public FullScreenExitHandler { 339 DECLARE_ACE_TYPE(FullScreenExitHandlerImpl, FullScreenExitHandler); 340 public: FullScreenExitHandlerImpl(void * object)341 explicit FullScreenExitHandlerImpl(void* object) : object_(object) 342 { 343 auto obj = WebObjectEventManager::GetInstance().GetFullScreenEnterObject(); 344 if (!obj) { 345 TAG_LOGE(AceLogTag::ACE_WEB, "WebObjectEventManager get GetFullScreenEnterObject failed"); 346 return; 347 } 348 index_ = obj->AddObject(object); 349 } ~FullScreenExitHandlerImpl()350 ~FullScreenExitHandlerImpl() 351 { 352 auto obj = WebObjectEventManager::GetInstance().GetFullScreenEnterObject(); 353 if (!obj) { 354 TAG_LOGE(AceLogTag::ACE_WEB, "WebObjectEventManager get GetFullScreenEnterObject failed"); 355 return; 356 } 357 obj->DelObject(index_); 358 } 359 void ExitFullScreen() override; 360 361 private: 362 void* object_ = nullptr; 363 int index_; 364 }; 365 366 class WebResourceErrorImpl : public AceType { 367 DECLARE_ACE_TYPE(WebResourceErrorImpl, AceType); 368 public: WebResourceErrorImpl(void * object)369 explicit WebResourceErrorImpl(void* object) : object_(object) {} 370 std::string GetErrorInfo() const; 371 int GetErrorCode() const; 372 private: 373 void* object_ = nullptr; 374 }; 375 376 class WebDelegateCross : public WebDelegateInterface, 377 public WebResource { 378 DECLARE_ACE_TYPE(WebDelegateCross, WebResource); 379 public: 380 using CreatedCallback = std::function<void()>; 381 using ReleasedCallback = std::function<void(bool)>; 382 using EventCallback = std::function<void(const std::shared_ptr<BaseEventInfo>&)>; 383 enum class State : char { 384 WAITINGFORSIZE, 385 CREATING, 386 CREATED, 387 CREATEFAILED, 388 RELEASED, 389 }; 390 391 WebDelegateCross() = delete; 392 ~WebDelegateCross() override; WebDelegateCross(const WeakPtr<PipelineBase> & context,ErrorCallback && onError,const std::string & type)393 WebDelegateCross(const WeakPtr<PipelineBase>& context, ErrorCallback&& onError, const std::string& type) 394 : WebResource(type, context, std::move(onError)), context_(context), instanceId_(Container::CurrentId()) 395 {} 396 397 void CreatePlatformResource(const Size& size, const Offset& position, 398 const WeakPtr<NG::PipelineContext>& context) override; SetNGWebPattern(const RefPtr<NG::WebPattern> & webPattern)399 void SetNGWebPattern(const RefPtr<NG::WebPattern>& webPattern) override 400 { 401 webPattern_ = webPattern; 402 } 403 int GetWebId() override; 404 void HandleTouchDown(const int32_t& id, const double& x, const double& y, bool from_overlay) override; 405 void HandleTouchUp(const int32_t& id, const double& x, const double& y, bool from_overlay) override; 406 void HandleTouchMove(const int32_t& id, const double& x, const double& y, bool from_overlay) override; 407 void HandleTouchCancel() override; 408 void HandleAxisEvent(const double& x, const double& y, const double& deltaX, const double& deltaY); 409 bool OnKeyEvent(int32_t keyCode, int32_t keyAction) override; 410 void OnMouseEvent(int32_t x, int32_t y, const MouseButton button, const MouseAction action, int count) override; 411 void OnFocus() override; 412 void OnBlur() override; 413 void UpdateLocale() override; 414 void SetDrawRect(int32_t x, int32_t y, int32_t width, int32_t height) override; 415 void OnInactive() override; 416 void OnActive() override; 417 void ShowWebView() override; 418 void HideWebView() override; 419 void OnFullScreenEnter(void* object) ; 420 void OnFullScreenExit(void* object); 421 void OnRefreshAccessedHistory(void* object) ; 422 void OnPageStarted(const std::string& param) override; 423 void OnPageFinished(const std::string& param) override; 424 void OnPageError(const std::string& param) override; 425 void OnProgressChanged(const std::string& param) override; 426 void OnReceivedTitle(const std::string& param) override; 427 void OnPageVisible(const std::string& param) override; 428 void OnGeolocationPermissionsHidePrompt() override; 429 430 void UpdateUserAgent(const std::string& userAgent) override; 431 void UpdateBackgroundColor(const int backgroundColor) override; 432 void UpdateInitialScale(float scale) override; 433 void UpdateJavaScriptEnabled(const bool& isJsEnabled) override; 434 void UpdateAllowFileAccess(const bool& isFileAccessEnabled) override; 435 void UpdateBlockNetworkImage(const bool& onLineImageAccessEnabled) override; 436 void UpdateLoadsImagesAutomatically(const bool& isImageAccessEnabled) override; 437 void UpdateMixedContentMode(const MixedModeContent& mixedMode) override; 438 void UpdateSupportZoom(const bool& isZoomAccessEnabled) override; 439 void UpdateDomStorageEnabled(const bool& isDomStorageAccessEnabled) override; 440 void UpdateGeolocationEnabled(const bool& isGeolocationAccessEnabled) override; 441 void UpdateCacheMode(const WebCacheMode& mode) override; 442 void UpdateDarkMode(const WebDarkMode& mode) override; 443 void UpdateForceDarkAccess(const bool& access) override; 444 void UpdateAudioResumeInterval(const int32_t& resumeInterval) override; 445 void UpdateAudioExclusive(const bool& audioExclusive) override; 446 void UpdateAudioSessionType(const WebAudioSessionType& audioSessionType) override; 447 void UpdateOverviewModeEnabled(const bool& isOverviewModeAccessEnabled) override; 448 void UpdateFileFromUrlEnabled(const bool& isFileFromUrlAccessEnabled) override; 449 void UpdateDatabaseEnabled(const bool& isDatabaseAccessEnabled) override; 450 void UpdateTextZoomRatio(const int32_t& textZoomRatioNum) override; 451 void UpdateWebDebuggingAccess(bool isWebDebuggingAccessEnabled) override; 452 void UpdatePinchSmoothModeEnabled(bool isPinchSmoothModeEnabled) override; 453 void UpdateMediaPlayGestureAccess(bool isNeedGestureAccess) override; 454 void UpdateMultiWindowAccess(bool isMultiWindowAccessEnabled) override; 455 void UpdateAllowWindowOpenMethod(bool isAllowWindowOpenMethod) override; 456 void UpdateWebCursiveFont(const std::string& cursiveFontFamily) override; 457 void UpdateWebFantasyFont(const std::string& fantasyFontFamily) override; 458 void UpdateWebFixedFont(const std::string& fixedFontFamily) override; 459 void UpdateWebSansSerifFont(const std::string& sansSerifFontFamily) override; 460 void UpdateWebSerifFont(const std::string& serifFontFamily) override; 461 void UpdateWebStandardFont(const std::string& standardFontFamily) override; 462 void UpdateDefaultFixedFontSize(int32_t size) override; 463 void UpdateDefaultFontSize(int32_t defaultFontSize) override; 464 void UpdateMinFontSize(int32_t minFontSize) override; 465 void UpdateMinLogicalFontSize(int32_t minLogicalFontSize) override; 466 void UpdateBlockNetwork(bool isNetworkBlocked) override; 467 void UpdateHorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled) override; 468 void UpdateVerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled) override; 469 void UpdateScrollBarColor(const std::string& colorValue) override; 470 void LoadUrl() override; 471 void SetBackgroundColor(int32_t backgroundColor) override; 472 473 bool LoadDataWithRichText() override; 474 475 void SetBoundsOrResize(const Size& drawSize, const Offset& offset) override; 476 void UpdateOptimizeParserBudgetEnabled(const bool enable); 477 void MaximizeResize() override; 478 private: 479 void ReleasePlatformResource(); 480 void CreatePluginResource(const Size& size, const Offset& position, const WeakPtr<NG::PipelineContext>& context); 481 void InitWebEvent(); 482 void RegisterWebEvent(); 483 void RegisterWebObjectEvent(); 484 void OnErrorReceive(void* object); 485 void OnScroll(void* object); 486 void OnScaleChange(void* object); 487 void OnHttpErrorReceive(void* object); 488 bool OnConsoleMessage(void* object); 489 bool OnLoadIntercept(void* object); 490 bool OnCommonDialog(void* object, DialogEventType dialogEventType); 491 bool OnPermissionRequest(void* object); 492 bool OnHttpAuthRequest(void* object); 493 void OnDownloadStart(void* object); 494 bool OnShowFileChooser(void* object); 495 void OnGeolocationPermissionsShowPrompt(void* object); 496 void RecordWebEvent(Recorder::EventType eventType, const std::string& param) const; 497 void RunJsProxyCallback(); 498 RefPtr<WebResponse> OnInterceptRequest(void* object); 499 500 WeakPtr<NG::WebPattern> webPattern_; 501 WeakPtr<PipelineBase> context_; 502 503 State state_ { State::WAITINGFORSIZE }; 504 505 Method reloadMethod_; 506 Method updateUrlMethod_; 507 Method routerBackMethod_; 508 Method changePageUrlMethod_; 509 Method isPagePathInvalidMethod_; 510 Method backwardMethod_; 511 Method forwardMethod_; 512 Method clearHistoryMethod_; 513 Method getHitTestMethod_; 514 Method onActiveMethod_; 515 Method onInactiveMethod_; 516 Method refreshMethod_; 517 Method stopLoadingMethod_; 518 Method requestFocusMethod_; 519 Method accessBackwardMethod_; 520 Method accessForwardMethod_; 521 Method accessStepMethod_; 522 Method setWebViewJavaScriptResultCallBackMethod_; 523 Method registerJavaScriptProxyMethod_; 524 Method runTypeScriptMethod_; 525 Method deleteJavaScriptInterfaceMethod_; 526 Method loadUrlMethod_; 527 Method loadDataMethod_; 528 Method updateAttributeMethod_; 529 Method saveCookieSyncMethod_; 530 Method setCookieMethod_; 531 Method touchDownMethod_; 532 Method touchUpMethod_; 533 Method touchMoveMethod_; 534 Method touchCancelMethod_; 535 Method updateLayoutMethod_; 536 Method zoomMethod_; 537 Method onBlockNetworkUpdateMethod_; 538 Method onUpdateMixedContentModeMethod_; 539 Method updateBlockNetworkImageMethod_; 540 Method UpdateGeolocationEnabledMethod_; 541 Method updateDomStorageEnabledMethod_; 542 Method updateCacheModeMethod_; 543 Method updateLoadsImagesAutomaticallyMethod_; 544 545 Method updateZoomAccess_; 546 Method updateJavaScriptEnabled_; 547 Method updateMinFontSize_; 548 Method updateHorizontalScrollBarAccess_; 549 Method updateVerticalScrollBarAccess_; 550 Method updateBackgroundColor_; 551 Method updateMediaPlayGestureAccess_; 552 553 EventCallbackV2 onPageFinishedV2_; 554 EventCallbackV2 onPageStartedV2_; 555 EventCallbackV2 onProgressChangeV2_; 556 EventCallbackV2 onRefreshAccessedHistoryV2_; 557 EventCallbackV2 onFullScreenEnterV2_; 558 EventCallbackV2 onFullScreenExitV2_; 559 560 int instanceId_ = -1; 561 }; 562 } // namespace OHOS::Ace 563 564 #endif 565