1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_WEB_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_WEB_COMPONENT_H 18 19 #include <string> 20 21 #include "base/geometry/size.h" 22 #include "base/utils/utils.h" 23 #include "core/components/declaration/common/declaration.h" 24 #include "core/components/declaration/web/web_client.h" 25 #include "core/components/declaration/web/web_declaration.h" 26 #include "core/components/web/resource/web_javascript_value.h" 27 #include "core/components_v2/common/common_def.h" 28 #include "core/focus/focus_node.h" 29 #include "core/pipeline/base/element.h" 30 31 namespace OHOS::Ace { 32 33 class WebDelegate; 34 35 enum class MixedModeContent { 36 MIXED_CONTENT_ALWAYS_ALLOW = 0, 37 MIXED_CONTENT_NEVER_ALLOW = 1, 38 MIXED_CONTENT_COMPATIBILITY_MODE = 2 39 }; 40 41 enum class WebCacheMode { 42 DEFAULT = 0, 43 USE_CACHE_ELSE_NETWORK, 44 USE_NO_CACHE, 45 USE_CACHE_ONLY 46 }; 47 48 enum class DialogEventType { 49 DIALOG_EVENT_ALERT = 0, 50 DIALOG_EVENT_BEFORE_UNLOAD = 1, 51 DIALOG_EVENT_CONFIRM = 2 52 }; 53 54 constexpr int DEFAULT_TEXT_ZOOM_ATIO = 100; 55 56 class WebCookie : public virtual AceType { 57 DECLARE_ACE_TYPE(WebCookie, AceType); 58 59 public: 60 using SetCookieImpl = std::function<bool(const std::string&, const std::string&)>; 61 using GetCookieImpl = std::function<std::string(const std::string&)>; 62 using DeleteEntirelyCookieImpl = std::function<void()>; 63 using SaveCookieSyncImpl = std::function<bool()>; SetCookie(const std::string & url,const std::string & value)64 bool SetCookie(const std::string& url, const std::string& value) 65 { 66 if (setCookieImpl_) { 67 return setCookieImpl_(url, value); 68 } 69 return false; 70 } 71 GetCookie(const std::string & url)72 std::string GetCookie(const std::string& url) 73 { 74 if (getCookieImpl_) { 75 return getCookieImpl_(url); 76 } 77 return ""; 78 } 79 DeleteEntirelyCookie()80 void DeleteEntirelyCookie() 81 { 82 if (deleteEntirelyCookieImpl_) { 83 deleteEntirelyCookieImpl_(); 84 } 85 } 86 SaveCookieSync()87 bool SaveCookieSync() 88 { 89 if (saveCookieSyncImpl_) { 90 return saveCookieSyncImpl_(); 91 } 92 return false; 93 } 94 SetSetCookieImpl(SetCookieImpl && setCookieImpl)95 void SetSetCookieImpl(SetCookieImpl&& setCookieImpl) 96 { 97 setCookieImpl_ = std::move(setCookieImpl); 98 } 99 SetGetCookieImpl(GetCookieImpl && getCookieImpl)100 void SetGetCookieImpl(GetCookieImpl&& getCookieImpl) 101 { 102 getCookieImpl_ = std::move(getCookieImpl); 103 } 104 SetDeleteEntirelyCookieImpl(DeleteEntirelyCookieImpl && deleteEntirelyCookieImpl)105 void SetDeleteEntirelyCookieImpl(DeleteEntirelyCookieImpl&& deleteEntirelyCookieImpl) 106 { 107 deleteEntirelyCookieImpl_ = std::move(deleteEntirelyCookieImpl); 108 } 109 SetSaveCookieSyncImpl(SaveCookieSyncImpl && saveCookieSyncImpl)110 void SetSaveCookieSyncImpl(SaveCookieSyncImpl&& saveCookieSyncImpl) 111 { 112 saveCookieSyncImpl_ = std::move(saveCookieSyncImpl); 113 } 114 115 private: 116 SetCookieImpl setCookieImpl_; 117 GetCookieImpl getCookieImpl_; 118 DeleteEntirelyCookieImpl deleteEntirelyCookieImpl_; 119 SaveCookieSyncImpl saveCookieSyncImpl_; 120 }; 121 122 class WebController : public virtual AceType { 123 DECLARE_ACE_TYPE(WebController, AceType); 124 125 public: 126 using LoadUrlImpl = std::function<void(std::string, const std::map<std::string, std::string>&)>; 127 using AccessBackwardImpl = std::function<bool()>; 128 using AccessForwardImpl = std::function<bool()>; 129 using AccessStepImpl = std::function<bool(int32_t)>; 130 using BackwardImpl = std::function<void()>; 131 using ForwardImpl = std::function<void()>; 132 using ClearHistoryImpl = std::function<void()>; LoadUrl(std::string url,std::map<std::string,std::string> & httpHeaders)133 void LoadUrl(std::string url, std::map<std::string, std::string>& httpHeaders) const 134 { 135 if (loadUrlImpl_) { 136 loadUrlImpl_(url, httpHeaders); 137 } 138 } 139 AccessStep(int32_t step)140 bool AccessStep(int32_t step) 141 { 142 if (accessStepImpl_) { 143 return accessStepImpl_(step); 144 } 145 return false; 146 } 147 AccessBackward()148 bool AccessBackward() 149 { 150 if (accessBackwardImpl_) { 151 return accessBackwardImpl_(); 152 } 153 return false; 154 } 155 AccessForward()156 bool AccessForward() 157 { 158 if (accessForwardImpl_) { 159 return accessForwardImpl_(); 160 } 161 return false; 162 } 163 Backward()164 void Backward() 165 { 166 LOGI("Start backward."); 167 if (backwardImpl_) { 168 backwardImpl_(); 169 } 170 } 171 Forward()172 void Forward() 173 { 174 LOGI("Start forward."); 175 if (forwardimpl_) { 176 forwardimpl_(); 177 } 178 } 179 ClearHistory()180 void ClearHistory() 181 { 182 LOGI("Start clear navigation history"); 183 if (clearHistoryImpl_) { 184 clearHistoryImpl_(); 185 } 186 } 187 SetLoadUrlImpl(LoadUrlImpl && loadUrlImpl)188 void SetLoadUrlImpl(LoadUrlImpl && loadUrlImpl) 189 { 190 loadUrlImpl_ = std::move(loadUrlImpl); 191 } 192 SetAccessBackwardImpl(AccessBackwardImpl && accessBackwardImpl)193 void SetAccessBackwardImpl(AccessBackwardImpl && accessBackwardImpl) 194 { 195 accessBackwardImpl_ = std::move(accessBackwardImpl); 196 } 197 SetAccessForwardImpl(AccessForwardImpl && accessForwardImpl)198 void SetAccessForwardImpl(AccessForwardImpl && accessForwardImpl) 199 { 200 accessForwardImpl_ = std::move(accessForwardImpl); 201 } 202 SetAccessStepImpl(AccessStepImpl && accessStepImpl)203 void SetAccessStepImpl(AccessStepImpl && accessStepImpl) 204 { 205 accessStepImpl_ = std::move(accessStepImpl); 206 } 207 SetBackwardImpl(BackwardImpl && backwardImpl)208 void SetBackwardImpl(BackwardImpl && backwardImpl) 209 { 210 backwardImpl_ = std::move(backwardImpl); 211 } 212 SetForwardImpl(ForwardImpl && forwardImpl)213 void SetForwardImpl(ForwardImpl && forwardImpl) 214 { 215 forwardimpl_ = std::move(forwardImpl); 216 } 217 SetClearHistoryImpl(ClearHistoryImpl && clearHistoryImpl)218 void SetClearHistoryImpl(ClearHistoryImpl && clearHistoryImpl) 219 { 220 clearHistoryImpl_ = std::move(clearHistoryImpl); 221 } 222 223 using ExecuteTypeScriptImpl = std::function<void(std::string, std::function<void(std::string)>&&)>; ExecuteTypeScript(std::string jscode,std::function<void (std::string)> && callback)224 void ExecuteTypeScript(std::string jscode, std::function<void(std::string)>&& callback) const 225 { 226 if (executeTypeScriptImpl_) { 227 executeTypeScriptImpl_(jscode, std::move(callback)); 228 } 229 } SetExecuteTypeScriptImpl(ExecuteTypeScriptImpl && executeTypeScriptImpl)230 void SetExecuteTypeScriptImpl(ExecuteTypeScriptImpl && executeTypeScriptImpl) 231 { 232 executeTypeScriptImpl_ = std::move(executeTypeScriptImpl); 233 } 234 235 using LoadDataWithBaseUrlImpl = std::function<void( 236 std::string, std::string, std::string, std::string, std::string)>; LoadDataWithBaseUrl(std::string baseUrl,std::string data,std::string mimeType,std::string encoding,std::string historyUrl)237 void LoadDataWithBaseUrl(std::string baseUrl, std::string data, std::string mimeType, std::string encoding, 238 std::string historyUrl) const 239 { 240 if (loadDataWithBaseUrlImpl_) { 241 loadDataWithBaseUrlImpl_(baseUrl, data, mimeType, encoding, historyUrl); 242 } 243 } 244 SetLoadDataWithBaseUrlImpl(LoadDataWithBaseUrlImpl && loadDataWithBaseUrlImpl)245 void SetLoadDataWithBaseUrlImpl(LoadDataWithBaseUrlImpl && loadDataWithBaseUrlImpl) 246 { 247 loadDataWithBaseUrlImpl_ = std::move(loadDataWithBaseUrlImpl); 248 } 249 250 using InitJavascriptInterface = std::function<void()>; LoadInitJavascriptInterface()251 void LoadInitJavascriptInterface() const 252 { 253 if (initJavascriptInterface_) { 254 initJavascriptInterface_(); 255 } 256 } SetInitJavascriptInterface(InitJavascriptInterface && initJavascriptInterface)257 void SetInitJavascriptInterface(InitJavascriptInterface&& initJavascriptInterface) 258 { 259 initJavascriptInterface_ = std::move(initJavascriptInterface); 260 } 261 262 using OnInactiveImpl = std::function<void()>; OnInactive()263 void OnInactive() const 264 { 265 if (onInactiveImpl_) { 266 onInactiveImpl_(); 267 } 268 } 269 SetOnInactiveImpl(OnInactiveImpl && onInactiveImpl)270 void SetOnInactiveImpl(OnInactiveImpl && onInactiveImpl) 271 { 272 onInactiveImpl_ = std::move(onInactiveImpl); 273 } 274 275 using OnActiveImpl = std::function<void()>; OnActive()276 void OnActive() const 277 { 278 if (onActiveImpl_) { 279 onActiveImpl_(); 280 } 281 } 282 SetOnActiveImpl(OnActiveImpl && onActiveImpl)283 void SetOnActiveImpl(OnActiveImpl && onActiveImpl) 284 { 285 onActiveImpl_ = std::move(onActiveImpl); 286 } 287 288 using ZoomImpl = std::function<void(float)>; Zoom(float factor)289 void Zoom(float factor) const 290 { 291 if (zoomImpl_) { 292 zoomImpl_(factor); 293 } 294 } 295 SetZoomImpl(ZoomImpl && zoomImpl)296 void SetZoomImpl(ZoomImpl && zoomImpl) 297 { 298 zoomImpl_ = std::move(zoomImpl); 299 } 300 301 using OnFocusImpl = std::function<void()>; OnFocus()302 void OnFocus() const 303 { 304 if (onFocusImpl_) { 305 onFocusImpl_(); 306 } 307 } 308 SetOnFocusImpl(OnFocusImpl && onFocusImpl)309 void SetOnFocusImpl(OnFocusImpl && onFocusImpl) 310 { 311 onFocusImpl_ = std::move(onFocusImpl); 312 } 313 314 using RefreshImpl = std::function<void()>; Refresh()315 void Refresh() const 316 { 317 if (refreshImpl_) { 318 refreshImpl_(); 319 } 320 } SetRefreshImpl(RefreshImpl && refreshImpl)321 void SetRefreshImpl(RefreshImpl && refreshImpl) 322 { 323 refreshImpl_ = std::move(refreshImpl); 324 } 325 326 using StopLoadingImpl = std::function<void()>; StopLoading()327 void StopLoading() const 328 { 329 if (stopLoadingImpl_) { 330 stopLoadingImpl_(); 331 } 332 } SetStopLoadingImpl(StopLoadingImpl && stopLoadingImpl)333 void SetStopLoadingImpl(StopLoadingImpl && stopLoadingImpl) 334 { 335 stopLoadingImpl_ = std::move(stopLoadingImpl); 336 } 337 338 using GetHitTestResultImpl = std::function<int()>; GetHitTestResult()339 int GetHitTestResult() 340 { 341 if (getHitTestResultImpl_) { 342 return getHitTestResultImpl_(); 343 } else { 344 return 0; 345 } 346 } SetGetHitTestResultImpl(GetHitTestResultImpl && getHitTestResultImpl)347 void SetGetHitTestResultImpl(GetHitTestResultImpl && getHitTestResultImpl) 348 { 349 getHitTestResultImpl_ = std::move(getHitTestResultImpl); 350 } 351 GetCookieManager()352 WebCookie* GetCookieManager() 353 { 354 if (!saveCookieSyncImpl_ || !setCookieImpl_) { 355 return nullptr; 356 } 357 if (cookieManager_ != nullptr) { 358 return cookieManager_.get(); 359 } 360 cookieManager_ = std::make_shared<WebCookie>(); 361 cookieManager_->SetSaveCookieSyncImpl(std::move(saveCookieSyncImpl_)); 362 cookieManager_->SetSetCookieImpl(std::move(setCookieImpl_)); 363 cookieManager_->SetGetCookieImpl(std::move(getCookieImpl_)); 364 cookieManager_->SetDeleteEntirelyCookieImpl(std::move(deleteEntirelyCookieImpl_)); 365 return cookieManager_.get(); 366 } 367 368 using SetCookieImpl = std::function<bool(const std::string&, const std::string&)>; SetCookie(const std::string & url,const std::string & value)369 bool SetCookie(const std::string& url, const std::string& value) 370 { 371 if (setCookieImpl_) { 372 return setCookieImpl_(url, value); 373 } 374 return false; 375 } SetSetCookieImpl(SetCookieImpl && setCookieImpl)376 void SetSetCookieImpl(SetCookieImpl&& setCookieImpl) 377 { 378 setCookieImpl_ = std::move(setCookieImpl); 379 } 380 381 using GetCookieImpl = std::function<std::string(const std::string&)>; GetCookie(const std::string & url)382 std::string GetCookie(const std::string& url) 383 { 384 if (getCookieImpl_) { 385 return getCookieImpl_(url); 386 } 387 return ""; 388 } SetGetCookieImpl(GetCookieImpl && getCookieImpl)389 void SetGetCookieImpl(GetCookieImpl&& getCookieImpl) 390 { 391 getCookieImpl_ = std::move(getCookieImpl); 392 } 393 394 using DeleteEntirelyCookieImpl = std::function<void()>; DeleteEntirelyCookie()395 void DeleteEntirelyCookie() 396 { 397 if (deleteEntirelyCookieImpl_) { 398 deleteEntirelyCookieImpl_(); 399 } 400 } SetDeleteEntirelyCookieImpl(DeleteEntirelyCookieImpl && deleteEntirelyCookieImpl)401 void SetDeleteEntirelyCookieImpl(DeleteEntirelyCookieImpl&& deleteEntirelyCookieImpl) 402 { 403 deleteEntirelyCookieImpl_ = std::move(deleteEntirelyCookieImpl); 404 } 405 406 using SaveCookieSyncImpl = std::function<bool()>; SaveCookieSync()407 bool SaveCookieSync() 408 { 409 if (saveCookieSyncImpl_) { 410 return saveCookieSyncImpl_(); 411 } 412 return false; 413 } SetSaveCookieSyncImpl(SaveCookieSyncImpl && saveCookieSyncImpl)414 void SetSaveCookieSyncImpl(SaveCookieSyncImpl&& saveCookieSyncImpl) 415 { 416 saveCookieSyncImpl_ = std::move(saveCookieSyncImpl); 417 } 418 419 using AddJavascriptInterfaceImpl = std::function<void( 420 const std::string&, 421 const std::vector<std::string>&)>; AddJavascriptInterface(const std::string & objectName,const std::vector<std::string> & methodList)422 void AddJavascriptInterface( 423 const std::string& objectName, 424 const std::vector<std::string>& methodList) 425 { 426 if (addJavascriptInterfaceImpl_) { 427 addJavascriptInterfaceImpl_(objectName, methodList); 428 } 429 } SetAddJavascriptInterfaceImpl(AddJavascriptInterfaceImpl && addJavascriptInterfaceImpl)430 void SetAddJavascriptInterfaceImpl(AddJavascriptInterfaceImpl && addJavascriptInterfaceImpl) 431 { 432 addJavascriptInterfaceImpl_ = std::move(addJavascriptInterfaceImpl); 433 } 434 435 using RemoveJavascriptInterfaceImpl = std::function<void(std::string, const std::vector<std::string>&)>; RemoveJavascriptInterface(std::string objectName,const std::vector<std::string> & methodList)436 void RemoveJavascriptInterface(std::string objectName, const std::vector<std::string>& methodList) 437 { 438 if (removeJavascriptInterfaceImpl_) { 439 removeJavascriptInterfaceImpl_(objectName, methodList); 440 } 441 } SetRemoveJavascriptInterfaceImpl(RemoveJavascriptInterfaceImpl && removeJavascriptInterfaceImpl)442 void SetRemoveJavascriptInterfaceImpl(RemoveJavascriptInterfaceImpl && removeJavascriptInterfaceImpl) 443 { 444 removeJavascriptInterfaceImpl_ = std::move(removeJavascriptInterfaceImpl); 445 } 446 447 using JavaScriptCallBackImpl = std::function<std::shared_ptr<WebJSValue>( 448 const std::string& objectName, 449 const std::string& objectMethod, 450 const std::vector<std::shared_ptr<WebJSValue>>& args)>; 451 using WebViewJavaScriptResultCallBackImpl = std::function<void(JavaScriptCallBackImpl&& javaScriptCallBackImpl)>; SetWebViewJavaScriptResultCallBackImpl(WebViewJavaScriptResultCallBackImpl && webViewJavaScriptResultCallBackImpl)452 void SetWebViewJavaScriptResultCallBackImpl( 453 WebViewJavaScriptResultCallBackImpl && webViewJavaScriptResultCallBackImpl) 454 { 455 webViewJavaScriptResultCallBackImpl_ = webViewJavaScriptResultCallBackImpl; 456 } SetJavaScriptCallBackImpl(JavaScriptCallBackImpl && javaScriptCallBackImpl)457 void SetJavaScriptCallBackImpl(JavaScriptCallBackImpl&& javaScriptCallBackImpl) 458 { 459 if (webViewJavaScriptResultCallBackImpl_) { 460 webViewJavaScriptResultCallBackImpl_(std::move(javaScriptCallBackImpl)); 461 } 462 } 463 464 using RequestFocusImpl = std::function<void()>; RequestFocus()465 void RequestFocus() 466 { 467 if (requestFocusImpl_) { 468 return requestFocusImpl_(); 469 } 470 } SetRequestFocusImpl(RequestFocusImpl && requestFocusImpl)471 void SetRequestFocusImpl(RequestFocusImpl && requestFocusImpl) 472 { 473 requestFocusImpl_ = std::move(requestFocusImpl); 474 } 475 Reload()476 void Reload() const 477 { 478 declaration_->webMethod.Reload(); 479 } 480 481 private: 482 RefPtr<WebDeclaration> declaration_; 483 std::shared_ptr<WebCookie> cookieManager_ = nullptr; 484 LoadUrlImpl loadUrlImpl_; 485 486 // Forward and Backward 487 AccessBackwardImpl accessBackwardImpl_; 488 AccessForwardImpl accessForwardImpl_; 489 AccessStepImpl accessStepImpl_; 490 BackwardImpl backwardImpl_; 491 ForwardImpl forwardimpl_; 492 ClearHistoryImpl clearHistoryImpl_; 493 494 ExecuteTypeScriptImpl executeTypeScriptImpl_; 495 OnInactiveImpl onInactiveImpl_; 496 OnActiveImpl onActiveImpl_; 497 OnFocusImpl onFocusImpl_; 498 ZoomImpl zoomImpl_; 499 LoadDataWithBaseUrlImpl loadDataWithBaseUrlImpl_; 500 InitJavascriptInterface initJavascriptInterface_; 501 RefreshImpl refreshImpl_; 502 StopLoadingImpl stopLoadingImpl_; 503 GetHitTestResultImpl getHitTestResultImpl_; 504 SaveCookieSyncImpl saveCookieSyncImpl_; 505 SetCookieImpl setCookieImpl_; 506 GetCookieImpl getCookieImpl_; 507 DeleteEntirelyCookieImpl deleteEntirelyCookieImpl_; 508 AddJavascriptInterfaceImpl addJavascriptInterfaceImpl_; 509 RemoveJavascriptInterfaceImpl removeJavascriptInterfaceImpl_; 510 WebViewJavaScriptResultCallBackImpl webViewJavaScriptResultCallBackImpl_; 511 RequestFocusImpl requestFocusImpl_; 512 }; 513 514 // A component can show HTML5 webpages. 515 class ACE_EXPORT WebComponent : public RenderComponent { 516 DECLARE_ACE_TYPE(WebComponent, RenderComponent); 517 518 public: 519 using CreatedCallback = std::function<void()>; 520 using ReleasedCallback = std::function<void(bool)>; 521 using ErrorCallback = std::function<void(const std::string&, const std::string&)>; 522 using MethodCall = std::function<void(const std::string&)>; 523 using Method = std::string; 524 525 WebComponent() = default; 526 explicit WebComponent(const std::string& type); 527 ~WebComponent() override = default; 528 529 RefPtr<RenderNode> CreateRenderNode() override; 530 RefPtr<Element> CreateElement() override; 531 SetType(const std::string & type)532 void SetType(const std::string& type) 533 { 534 type_ = type; 535 } 536 GetType()537 const std::string& GetType() const 538 { 539 return type_; 540 } 541 SetSrc(const std::string & src)542 void SetSrc(const std::string& src) 543 { 544 declaration_->SetWebSrc(src); 545 } 546 GetSrc()547 const std::string& GetSrc() const 548 { 549 return declaration_->GetWebSrc(); 550 } 551 SetData(const std::string & data)552 void SetData(const std::string& data) 553 { 554 declaration_->SetWebData(data); 555 } 556 GetData()557 const std::string& GetData() const 558 { 559 return declaration_->GetWebData(); 560 } 561 SetPageStartedEventId(const EventMarker & pageStartedEventId)562 void SetPageStartedEventId(const EventMarker& pageStartedEventId) 563 { 564 declaration_->SetPageStartedEventId(pageStartedEventId); 565 } 566 GetPageStartedEventId()567 const EventMarker& GetPageStartedEventId() const 568 { 569 return declaration_->GetPageStartedEventId(); 570 } 571 SetPageFinishedEventId(const EventMarker & pageFinishedEventId)572 void SetPageFinishedEventId(const EventMarker& pageFinishedEventId) 573 { 574 declaration_->SetPageFinishedEventId(pageFinishedEventId); 575 } 576 GetPageFinishedEventId()577 const EventMarker& GetPageFinishedEventId() const 578 { 579 return declaration_->GetPageFinishedEventId(); 580 } 581 SetProgressChangeEventId(const EventMarker & progressChangeEventId)582 void SetProgressChangeEventId(const EventMarker& progressChangeEventId) 583 { 584 declaration_->SetProgressChangeEventId(progressChangeEventId); 585 } 586 GetProgressChangeEventId()587 const EventMarker& GetProgressChangeEventId() const 588 { 589 return declaration_->GetProgressChangeEventId(); 590 } 591 SetTitleReceiveEventId(const EventMarker & titleReceiveEventId)592 void SetTitleReceiveEventId(const EventMarker& titleReceiveEventId) 593 { 594 declaration_->SetTitleReceiveEventId(titleReceiveEventId); 595 } 596 GetTitleReceiveEventId()597 const EventMarker& GetTitleReceiveEventId() const 598 { 599 return declaration_->GetTitleReceiveEventId(); 600 } 601 SetGeolocationHideEventId(const EventMarker & geolocationHideEventId)602 void SetGeolocationHideEventId(const EventMarker& geolocationHideEventId) 603 { 604 declaration_->SetGeolocationHideEventId(geolocationHideEventId); 605 } 606 GetGeolocationHideEventId()607 const EventMarker& GetGeolocationHideEventId() const 608 { 609 return declaration_->GetGeolocationHideEventId(); 610 } 611 SetGeolocationShowEventId(const EventMarker & geolocationShowEventId)612 void SetGeolocationShowEventId(const EventMarker& geolocationShowEventId) 613 { 614 declaration_->SetGeolocationShowEventId(geolocationShowEventId); 615 } 616 GetGeolocationShowEventId()617 const EventMarker& GetGeolocationShowEventId() const 618 { 619 return declaration_->GetGeolocationShowEventId(); 620 } 621 SetRequestFocusEventId(const EventMarker & requestFocusEventId)622 void SetRequestFocusEventId(const EventMarker& requestFocusEventId) 623 { 624 declaration_->SetRequestFocusEventId(requestFocusEventId); 625 } 626 GetRequestFocusEventId()627 const EventMarker& GetRequestFocusEventId() const 628 { 629 return declaration_->GetRequestFocusEventId(); 630 } 631 SetDownloadStartEventId(const EventMarker & downloadStartEventId)632 void SetDownloadStartEventId(const EventMarker& downloadStartEventId) 633 { 634 declaration_->SetDownloadStartEventId(downloadStartEventId); 635 } 636 GetDownloadStartEventId()637 const EventMarker& GetDownloadStartEventId() const 638 { 639 return declaration_->GetDownloadStartEventId(); 640 } 641 SetOnFocusEventId(const EventMarker & onFocusEventId)642 void SetOnFocusEventId(const EventMarker& onFocusEventId) 643 { 644 declaration_->SetOnFocusEventId(onFocusEventId); 645 } 646 GetOnFocusEventId()647 const EventMarker& GetOnFocusEventId() const 648 { 649 return declaration_->GetOnFocusEventId(); 650 } 651 SetPageErrorEventId(const EventMarker & pageErrorEventId)652 void SetPageErrorEventId(const EventMarker& pageErrorEventId) 653 { 654 declaration_->SetPageErrorEventId(pageErrorEventId); 655 } 656 GetPageErrorEventId()657 const EventMarker& GetPageErrorEventId() const 658 { 659 return declaration_->GetPageErrorEventId(); 660 } 661 SetHttpErrorEventId(const EventMarker & httpErrorEventId)662 void SetHttpErrorEventId(const EventMarker& httpErrorEventId) 663 { 664 declaration_->SetHttpErrorEventId(httpErrorEventId); 665 } 666 GetHttpErrorEventId()667 const EventMarker& GetHttpErrorEventId() const 668 { 669 return declaration_->GetHttpErrorEventId(); 670 } 671 SetMessageEventId(const EventMarker & messageEventId)672 void SetMessageEventId(const EventMarker& messageEventId) 673 { 674 declaration_->SetMessageEventId(messageEventId); 675 } 676 GetMessageEventId()677 const EventMarker& GetMessageEventId() const 678 { 679 return declaration_->GetMessageEventId(); 680 } 681 SetRenderExitedId(const EventMarker & renderExitedId)682 void SetRenderExitedId(const EventMarker& renderExitedId) 683 { 684 declaration_->SetRenderExitedId(renderExitedId); 685 } 686 GetRenderExitedId()687 const EventMarker& GetRenderExitedId() const 688 { 689 return declaration_->GetRenderExitedId(); 690 } 691 SetRefreshAccessedHistoryId(const EventMarker & refreshAccessedHistoryId)692 void SetRefreshAccessedHistoryId(const EventMarker& refreshAccessedHistoryId) 693 { 694 declaration_->SetRefreshAccessedHistoryId(refreshAccessedHistoryId); 695 } 696 GetRefreshAccessedHistoryId()697 const EventMarker& GetRefreshAccessedHistoryId() const 698 { 699 return declaration_->GetRefreshAccessedHistoryId(); 700 } 701 SetDeclaration(const RefPtr<WebDeclaration> & declaration)702 void SetDeclaration(const RefPtr<WebDeclaration>& declaration) 703 { 704 if (declaration) { 705 declaration_ = declaration; 706 } 707 } 708 GetController()709 RefPtr<WebController> GetController() const 710 { 711 return webController_; 712 } 713 SetWebController(const RefPtr<WebController> & webController)714 void SetWebController(const RefPtr<WebController>& webController) 715 { 716 webController_ = webController; 717 } 718 GetJsEnabled()719 bool GetJsEnabled() const 720 { 721 return isJsEnabled_; 722 } 723 SetJsEnabled(bool isEnabled)724 void SetJsEnabled(bool isEnabled) 725 { 726 isJsEnabled_ = isEnabled; 727 } 728 GetUserAgent()729 std::string GetUserAgent() const 730 { 731 return userAgent_; 732 } 733 SetUserAgent(std::string userAgent)734 void SetUserAgent(std::string userAgent) 735 { 736 userAgent_ = userAgent; 737 } 738 GetContentAccessEnabled()739 bool GetContentAccessEnabled() const 740 { 741 return isContentAccessEnabled_; 742 } 743 SetContentAccessEnabled(bool isEnabled)744 void SetContentAccessEnabled(bool isEnabled) 745 { 746 isContentAccessEnabled_ = isEnabled; 747 } 748 GetFileAccessEnabled()749 bool GetFileAccessEnabled() const 750 { 751 return isFileAccessEnabled_; 752 } 753 SetFileAccessEnabled(bool isEnabled)754 void SetFileAccessEnabled(bool isEnabled) 755 { 756 isFileAccessEnabled_ = isEnabled; 757 } GetOnLineImageAccessEnabled()758 bool GetOnLineImageAccessEnabled() const 759 { 760 return isOnLineImageAccessEnabled_; 761 } 762 SetOnLineImageAccessEnabled(bool isEnabled)763 void SetOnLineImageAccessEnabled(bool isEnabled) 764 { 765 isOnLineImageAccessEnabled_ = isEnabled; 766 } 767 GetDomStorageAccessEnabled()768 bool GetDomStorageAccessEnabled() const 769 { 770 return isDomStorageAccessEnabled_; 771 } 772 SetDomStorageAccessEnabled(bool isEnabled)773 void SetDomStorageAccessEnabled(bool isEnabled) 774 { 775 isDomStorageAccessEnabled_ = isEnabled; 776 } 777 GetImageAccessEnabled()778 bool GetImageAccessEnabled() const 779 { 780 return isImageAccessEnabled_; 781 } 782 SetImageAccessEnabled(bool isEnabled)783 void SetImageAccessEnabled(bool isEnabled) 784 { 785 isImageAccessEnabled_ = isEnabled; 786 } 787 GetMixedMode()788 MixedModeContent GetMixedMode() const 789 { 790 return mixedContentMode_; 791 } 792 SetMixedMode(MixedModeContent mixedModeNum)793 void SetMixedMode(MixedModeContent mixedModeNum) 794 { 795 mixedContentMode_ = mixedModeNum; 796 } 797 GetZoomAccessEnabled()798 bool GetZoomAccessEnabled() const 799 { 800 return isZoomAccessEnabled_; 801 } 802 SetZoomAccessEnabled(bool isEnabled)803 void SetZoomAccessEnabled(bool isEnabled) 804 { 805 isZoomAccessEnabled_ = isEnabled; 806 } 807 GetGeolocationAccessEnabled()808 bool GetGeolocationAccessEnabled() const 809 { 810 return isGeolocationAccessEnabled_; 811 } 812 SetGeolocationAccessEnabled(bool isEnabled)813 void SetGeolocationAccessEnabled(bool isEnabled) 814 { 815 isGeolocationAccessEnabled_ = isEnabled; 816 } 817 GetCacheMode()818 WebCacheMode GetCacheMode() 819 { 820 return cacheMode_; 821 } 822 SetCacheMode(WebCacheMode mode)823 void SetCacheMode(WebCacheMode mode) 824 { 825 cacheMode_ = mode; 826 } 827 GetOverviewModeAccessEnabled()828 bool GetOverviewModeAccessEnabled() const 829 { 830 return isOverviewModeAccessEnabled_; 831 } 832 SetOverviewModeAccessEnabled(bool isEnabled)833 void SetOverviewModeAccessEnabled(bool isEnabled) 834 { 835 isOverviewModeAccessEnabled_ = isEnabled; 836 } 837 GetFileFromUrlAccessEnabled()838 bool GetFileFromUrlAccessEnabled() const 839 { 840 return isFileFromUrlAccessEnabled_; 841 } 842 SetFileFromUrlAccessEnabled(bool isEnabled)843 void SetFileFromUrlAccessEnabled(bool isEnabled) 844 { 845 isFileFromUrlAccessEnabled_ = isEnabled; 846 } 847 GetDatabaseAccessEnabled()848 bool GetDatabaseAccessEnabled() const 849 { 850 return isDatabaseAccessEnabled_; 851 } 852 SetDatabaseAccessEnabled(bool isEnabled)853 void SetDatabaseAccessEnabled(bool isEnabled) 854 { 855 isDatabaseAccessEnabled_ = isEnabled; 856 } 857 GetWebDebuggingAccessEnabled()858 bool GetWebDebuggingAccessEnabled() const 859 { 860 return isWebDebuggingAccessEnabled_; 861 } 862 SetWebDebuggingAccessEnabled(bool isEnabled)863 void SetWebDebuggingAccessEnabled(bool isEnabled) 864 { 865 isWebDebuggingAccessEnabled_ = isEnabled; 866 } 867 GetTextZoomAtio()868 int32_t GetTextZoomAtio() const 869 { 870 return textZoomAtioNum_; 871 } 872 SetTextZoomAtio(int32_t atio)873 void SetTextZoomAtio(int32_t atio) 874 { 875 textZoomAtioNum_ = atio; 876 } 877 878 using OnCommonDialogImpl = std::function<bool(const BaseEventInfo* info)>; OnCommonDialog(const BaseEventInfo * info,DialogEventType dialogEventType)879 bool OnCommonDialog(const BaseEventInfo* info, DialogEventType dialogEventType) const 880 { 881 if (dialogEventType == DialogEventType::DIALOG_EVENT_ALERT && onAlertImpl_) { 882 return onAlertImpl_(info); 883 } else if (dialogEventType == DialogEventType::DIALOG_EVENT_CONFIRM && onConfirmImpl_) { 884 return onConfirmImpl_(info); 885 } else if (dialogEventType == DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD && onBeforeUnloadImpl_) { 886 return onBeforeUnloadImpl_(info); 887 } else { 888 return false; 889 } 890 } SetOnCommonDialogImpl(OnCommonDialogImpl && onCommonDialogImpl,DialogEventType dialogEventType)891 void SetOnCommonDialogImpl(OnCommonDialogImpl && onCommonDialogImpl, DialogEventType dialogEventType) 892 { 893 if (onCommonDialogImpl == nullptr) { 894 return; 895 } 896 897 switch (dialogEventType) { 898 case DialogEventType::DIALOG_EVENT_ALERT: 899 onAlertImpl_ = std::move(onCommonDialogImpl); 900 break; 901 case DialogEventType::DIALOG_EVENT_CONFIRM: 902 onConfirmImpl_ = std::move(onCommonDialogImpl); 903 break; 904 case DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD: 905 onBeforeUnloadImpl_ = std::move(onCommonDialogImpl); 906 break; 907 default: 908 break; 909 } 910 } 911 912 void RequestFocus(); 913 914 using OnConsoleImpl = std::function<bool(const BaseEventInfo* info)>; OnConsole(const BaseEventInfo * info)915 bool OnConsole(const BaseEventInfo* info) const 916 { 917 if (consoleImpl_) { 918 return consoleImpl_(info); 919 } 920 return false; 921 } 922 SetOnConsoleImpl(OnConsoleImpl && consoleImpl)923 void SetOnConsoleImpl(OnConsoleImpl && consoleImpl) 924 { 925 consoleImpl_ = std::move(consoleImpl); 926 } 927 SetFocusElement(const WeakPtr<FocusNode> & focusElement)928 void SetFocusElement(const WeakPtr<FocusNode>& focusElement) 929 { 930 focusElement_ = focusElement; 931 } 932 933 using OnFileSelectorShowImpl = std::function<bool(const BaseEventInfo* info)>; OnFileSelectorShow(const BaseEventInfo * info)934 bool OnFileSelectorShow(const BaseEventInfo* info) const 935 { 936 if (onFileSelectorShowImpl_) { 937 return onFileSelectorShowImpl_(info); 938 } 939 return false; 940 } SetOnFileSelectorShow(OnFileSelectorShowImpl && onFileSelectorShowImpl)941 void SetOnFileSelectorShow(OnFileSelectorShowImpl && onFileSelectorShowImpl) 942 { 943 if (onFileSelectorShowImpl == nullptr) { 944 return; 945 } 946 947 onFileSelectorShowImpl_ = std::move(onFileSelectorShowImpl); 948 } 949 950 using OnUrlLoadInterceptImpl = std::function<bool(const BaseEventInfo* info)>; OnUrlLoadIntercept(const BaseEventInfo * info)951 bool OnUrlLoadIntercept(const BaseEventInfo* info) const 952 { 953 if (onUrlLoadInterceptImpl_) { 954 return onUrlLoadInterceptImpl_(info); 955 } 956 return false; 957 } SetOnUrlLoadIntercept(OnUrlLoadInterceptImpl && onUrlLoadInterceptImpl)958 void SetOnUrlLoadIntercept(OnUrlLoadInterceptImpl && onUrlLoadInterceptImpl) 959 { 960 if (onUrlLoadInterceptImpl == nullptr) { 961 return; 962 } 963 964 onUrlLoadInterceptImpl_ = std::move(onUrlLoadInterceptImpl); 965 } 966 967 private: 968 RefPtr<WebDeclaration> declaration_; 969 CreatedCallback createdCallback_ = nullptr; 970 ReleasedCallback releasedCallback_ = nullptr; 971 ErrorCallback errorCallback_ = nullptr; 972 RefPtr<WebDelegate> delegate_; 973 RefPtr<WebController> webController_; 974 OnCommonDialogImpl onAlertImpl_; 975 OnCommonDialogImpl onConfirmImpl_; 976 OnCommonDialogImpl onBeforeUnloadImpl_; 977 OnConsoleImpl consoleImpl_; 978 OnFileSelectorShowImpl onFileSelectorShowImpl_; 979 OnUrlLoadInterceptImpl onUrlLoadInterceptImpl_; 980 981 std::string type_; 982 bool isJsEnabled_ = true; 983 bool isContentAccessEnabled_ = true; 984 bool isFileAccessEnabled_ = true; 985 std::string userAgent_; 986 WeakPtr<FocusNode> focusElement_; 987 bool isOnLineImageAccessEnabled_ = false; 988 bool isDomStorageAccessEnabled_ = false; 989 bool isImageAccessEnabled_ = true; 990 MixedModeContent mixedContentMode_ = MixedModeContent::MIXED_CONTENT_NEVER_ALLOW; 991 bool isZoomAccessEnabled_ = true; 992 bool isGeolocationAccessEnabled_ = true; 993 bool isOverviewModeAccessEnabled_ = true; 994 bool isFileFromUrlAccessEnabled_ = false; 995 bool isDatabaseAccessEnabled_ = false; 996 int32_t textZoomAtioNum_ = DEFAULT_TEXT_ZOOM_ATIO; 997 WebCacheMode cacheMode_ = WebCacheMode::DEFAULT; 998 bool isWebDebuggingAccessEnabled_ = false; 999 }; 1000 1001 } // namespace OHOS::Ace 1002 1003 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_WEB_COMPONENT_H 1004