1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NWEB_H 17 #define NWEB_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include "nweb_export.h" 24 25 #include "nweb_download_callback.h" 26 #include "nweb_drag_data.h" 27 #include "nweb_find_callback.h" 28 #include "nweb_history_list.h" 29 #include "nweb_javascript_result_callback.h" 30 #include "nweb_preference.h" 31 #include "nweb_release_surface_callback.h" 32 #include "nweb_value_callback.h" 33 #include "nweb_hit_testresult.h" 34 #include "nweb_web_message.h" 35 36 namespace OHOS::NWeb { 37 class NWebHandler; 38 39 /** 40 * @brief Describes how pixel bits encoder color data. 41 */ 42 enum class ImageColorType { 43 // Unknown color type. 44 COLOR_TYPE_UNKNOWN = -1, 45 46 // RGBA with 8 bits per pixel (32bits total). 47 COLOR_TYPE_RGBA_8888 = 0, 48 49 // BGRA with 8 bits per pixel (32bits total). 50 COLOR_TYPE_BGRA_8888 = 1, 51 }; 52 53 /** 54 * @brief Describes how to interpret the alpha value of a pixel. 55 */ 56 enum class ImageAlphaType { 57 // Unknown alpha type. 58 ALPHA_TYPE_UNKNOWN = -1, 59 60 // No transparency. The alpha component is ignored. 61 ALPHA_TYPE_OPAQUE = 0, 62 63 // Transparency with pre-multiplied alpha component. 64 ALPHA_TYPE_PREMULTIPLIED = 1, 65 66 // Transparency with post-multiplied alpha component. 67 ALPHA_TYPE_POSTMULTIPLIED = 2, 68 }; 69 70 struct OHOS_NWEB_EXPORT NWebInitArgs { 71 std::string dump_path = ""; 72 bool frame_info_dump = false; 73 std::list<std::string> web_engine_args_to_add; 74 std::list<std::string> web_engine_args_to_delete; 75 bool multi_renderer_process = false; 76 bool is_enhance_surface = false; 77 bool is_popup = false; 78 }; 79 80 struct OHOS_NWEB_EXPORT NWebCreateInfo { 81 /* size info */ 82 uint32_t width = 0; 83 uint32_t height = 0; 84 85 /* output frame cb */ 86 std::function<bool(const char*, uint32_t, uint32_t)> output_render_frame = nullptr; 87 88 /* init args */ 89 NWebInitArgs init_args; 90 91 void* producer_surface = nullptr; 92 93 void* enhance_surface_info = nullptr; 94 }; 95 96 enum class OHOS_NWEB_EXPORT DragAction { 97 DRAG_START = 0, 98 DRAG_ENTER, 99 DRAG_LEAVE, 100 DRAG_OVER, 101 DRAG_DROP, 102 DRAG_END, 103 DRAG_CANCEL, 104 }; 105 106 struct OHOS_NWEB_EXPORT DragEvent { 107 double x; 108 double y; 109 DragAction action; 110 }; 111 112 enum class BlurReason : int32_t { 113 FOCUS_SWITCH = 0, 114 WINDOW_BLUR = 1, 115 FRAME_DESTROY = 2, 116 }; 117 118 enum class FocusReason : int32_t { 119 FOCUS_DEFAULT = 0, 120 EVENT_REQUEST = 1, 121 }; 122 123 struct OHOS_NWEB_EXPORT NWebDOHConfig { 124 /* 125 * 0: OFF 126 * 1: AUTO 127 * 2: SECURE_ONLY 128 */ 129 int dohMode = -1; 130 std::string dohConfig = ""; 131 }; 132 133 using WebState = std::shared_ptr<std::vector<uint8_t>>; 134 using SetKeepScreenOn = std::function<void(bool)>; 135 136 class OHOS_NWEB_EXPORT NWeb : public std::enable_shared_from_this<NWeb> { 137 public: 138 NWeb() = default; 139 virtual ~NWeb() = default; 140 141 virtual void Resize(uint32_t width, uint32_t height) = 0; 142 143 /* lifecycle interface */ 144 virtual void OnPause() const = 0; 145 virtual void OnContinue() const = 0; 146 virtual void OnDestroy() = 0; 147 148 /* focus event */ 149 virtual void OnFocus(const FocusReason& focusReason = FocusReason::FOCUS_DEFAULT) const = 0; 150 virtual void OnBlur(const BlurReason& blurReason) const = 0; 151 152 /* event interface */ 153 virtual void OnTouchPress(int32_t id, double x, double y, bool fromOverlay = false) = 0; 154 virtual void OnTouchRelease(int32_t id, double x = 0, double y = 0, bool fromOverlay = false) = 0; 155 virtual void OnTouchMove(int32_t id, double x, double y, bool fromOverlay = false) = 0; 156 virtual void OnTouchCancel() = 0; 157 virtual void OnNavigateBack() = 0; 158 virtual bool SendKeyEvent(int32_t keyCode, int32_t keyAction) = 0; 159 virtual void SendMouseWheelEvent(double x, double y, double deltaX, double deltaY) = 0; 160 virtual void SendMouseEvent(int x, int y, int button, int action, int count) = 0; 161 162 /** 163 * Load the given URL. 164 * 165 * @param url String: the URL of the resource to load. This value cannot be 166 * null. 167 * 168 * @return title string for the current page. 169 */ 170 virtual int Load(const std::string& url) const = 0; 171 172 /** 173 * Get whether this NWeb has a back history item. 174 * 175 * @return true if this NWeb has a back history item 176 */ 177 virtual bool IsNavigatebackwardAllowed() const = 0; 178 179 /** 180 * Get whether this NWeb has a forward history item. 181 * 182 * @return true if this NWeb has a forward history item 183 */ 184 virtual bool IsNavigateForwardAllowed() const = 0; 185 186 /** 187 * Get whether this NWeb has a back or forward history item for number of 188 * steps. 189 * 190 * @param numSteps int: the negative or positive number of steps to move the 191 * history. 192 * @return true if this NWeb has a forward history item 193 */ 194 virtual bool CanNavigateBackOrForward(int numSteps) const = 0; 195 196 /** 197 * Go back in the history of this NWeb. 198 * 199 */ 200 virtual void NavigateBack() const = 0; 201 202 /** 203 * Go forward in the history of this NWeb. 204 * 205 */ 206 virtual void NavigateForward() const = 0; 207 208 /** 209 * Goes to the history item that is the number of steps away from the current item. 210 * 211 */ 212 virtual void NavigateBackOrForward(int step) const = 0; 213 214 /** 215 * Delete back and forward history list. 216 */ 217 virtual void DeleteNavigateHistory() = 0; 218 219 /** 220 * Reload the current URL. 221 * 222 */ 223 virtual void Reload() const = 0; 224 225 /** 226 * Perform a zoom operation in this NWeb. 227 * 228 * @param zoomFactor float: the zoom factor to apply. The zoom factor will be 229 * clamped to the NWeb's zoom limits. This value must be in the range 0.01 230 * to 100.0 inclusive. 231 * 232 * @return the error id. 233 * 234 */ 235 virtual int Zoom(float zoomFactor) const = 0; 236 237 /** 238 * Performs a zooming in operation in this NWeb. 239 * 240 * @return the error id. 241 * 242 */ 243 virtual int ZoomIn() const = 0; 244 245 /** 246 * Performs a zooming out operation in this NWeb. 247 * 248 * @return the error id. 249 * 250 */ 251 virtual int ZoomOut() const = 0; 252 253 /** 254 * Stop the current load. 255 * 256 * @param code string: javascript code 257 */ 258 virtual void Stop() const = 0; 259 260 /** 261 * ExecuteJavaScript 262 * 263 */ 264 virtual void ExecuteJavaScript(const std::string& code) const = 0; 265 266 /** 267 * ExecuteJavaScript plus 268 * 269 * @param code string: javascript code 270 * 271 * @param callback NWebValueCallback: javascript running result 272 * 273 */ 274 virtual void ExecuteJavaScript( 275 const std::string& code, 276 std::shared_ptr<NWebValueCallback<std::shared_ptr<NWebMessage>>> callback, 277 bool extention) const = 0; 278 279 /** 280 * Get the NWebPreference object used to control the settings for this 281 * NWeb. 282 * 283 * @return a NWebPreference object that can be used to control this NWeb's 284 * settings This value cannot be null. 285 */ 286 virtual const std::shared_ptr<NWebPreference> GetPreference() const = 0; 287 288 /** 289 * Gets the web id. 290 * 291 * @return the web id 292 */ 293 virtual unsigned int GetWebId() const = 0; 294 295 /** 296 * Get the last hit test result. 297 * 298 * @return the last HitTestResult 299 */ 300 virtual HitTestResult GetHitTestResult() const = 0; 301 302 /** 303 * Set the background color for this view. 304 * 305 * @param color int: the color of the background 306 * 307 */ 308 virtual void PutBackgroundColor(int color) const = 0; 309 310 /** 311 * Sets the initla scale for the page. 312 * 313 * @param scale float: the initla scale of the page. 314 * 315 */ 316 virtual void InitialScale(float scale) const = 0; 317 318 /** 319 * Set the NWebDownloadCallback that will receive download event. 320 * This will replace the current handler. 321 * 322 * @param downloadListener NWebDownloadCallback. 323 */ 324 virtual void PutDownloadCallback( 325 std::shared_ptr<NWebDownloadCallback> downloadListener) = 0; 326 327 /** 328 * Set the NWebHandler that will receive various notifications and 329 * requests. This will replace the current handler. 330 * 331 * @param client NWebHandler: an implementation of NWebHandler. This value 332 * cannot be null. 333 */ 334 virtual void SetNWebHandler(std::shared_ptr<NWebHandler> handler) = 0; 335 336 /** 337 * Get the NWebHandler. 338 * 339 * @return Gets the NWebHandler. 340 */ 341 virtual const std::shared_ptr<NWebHandler> GetNWebHandler() const = 0; 342 343 /** 344 * Get the title for the current page. 345 * 346 * @return title string for the current page. 347 */ 348 virtual std::string Title() = 0; 349 350 /** 351 * Get the progress for the current page. 352 * 353 * @return progress for the current page. 354 */ 355 virtual int PageLoadProgress() = 0; 356 357 /** 358 * Get the height of the HTML content. 359 * 360 * @return the height of the HTML content. 361 */ 362 virtual int ContentHeight() = 0; 363 364 /** 365 * Get the current scale of this NWeb. 366 * 367 * @return the current scale 368 */ 369 virtual float Scale() = 0; 370 371 /** 372 * Load the given URL with additional HTTP headers, specified as a map 373 * from name to value. Note that if this map contains any of the headers that 374 * are set by default by this NWeb, such as those controlling caching, 375 * accept types or the User-Agent, their values may be overridden by this 376 * NWeb's defaults. 377 * 378 * @param url String: the URL of the resource to load This value cannot be 379 * null. 380 * 381 * @param additionalHttpHeaders additionalHttpHeaders 382 */ 383 virtual int Load( 384 std::string& url, 385 std::map<std::string, std::string> additionalHttpHeaders) = 0; 386 387 /** 388 * Load the given data into this NWeb, using baseUrl as the base URL for 389 * the content. The base URL is used both to resolve relative URLs and when 390 * applying JavaScript's same origin policy. The historyUrl is used for the 391 * history entry. 392 * 393 * @param baseUrl String: the URL to use as the page's base URL. If null 394 * defaults to 'about:blank'. This value may be null. 395 * @param data String: the URL to use as the page's base URL. If null defaults 396 * to 'about:blank'. This value may be null. 397 * @param mimeType String: the MIME type of the data, e.g. 'text/html'. This 398 * value may be null. 399 * @param encoding String: the encoding of the data This value may be null. 400 * @param historyUrl String: the URL to use as the history entry. If null 401 * defaults to 'about:blank'. If non-null, this must be a valid URL. This 402 * value may be null. 403 */ 404 virtual int LoadWithDataAndBaseUrl(const std::string& baseUrl, 405 const std::string& data, 406 const std::string& mimeType, 407 const std::string& encoding, 408 const std::string& historyUrl) = 0; 409 410 /** 411 * Load the given data into this NWeb. 412 * 413 * @param data String: the URL to use as the page's base URL. If null defaults 414 * to 'about:blank'. This value may be null. 415 * @param mimeType String: the MIME type of the data, e.g. 'text/html'. This 416 * value may be null. 417 * @param encoding String: the encoding of the data This value may be null. 418 */ 419 virtual int LoadWithData(const std::string& data, 420 const std::string& mimeType, 421 const std::string& encoding) = 0; 422 423 /** 424 * RegisterArkJSfunction 425 * 426 * @param object_name String: objector name 427 * @param method_list vector<String>: vector list, method list 428 */ 429 virtual void RegisterArkJSfunction( 430 const std::string& object_name, 431 const std::vector<std::string>& method_list) = 0; 432 433 /** 434 * UnregisterArkJSfunction 435 * 436 * @param object_name String: objector name 437 * @param method_list vector<String>: vector list, method list 438 */ 439 virtual void UnregisterArkJSfunction( 440 const std::string& object_name, 441 const std::vector<std::string>& method_list) = 0; 442 443 /** 444 * SetNWebJavaScriptResultCallBack 445 * 446 * @param callback NWebJavaScriptResultCallBack: callback client 447 */ 448 virtual void SetNWebJavaScriptResultCallBack( 449 std::shared_ptr<NWebJavaScriptResultCallBack> callback) = 0; 450 451 /** 452 * Set the NWebFindCallback that will receive find event. 453 * This will replace the current handler. 454 * 455 * @param findListener NWebFindCallback : find callback 456 */ 457 virtual void PutFindCallback( 458 std::shared_ptr<NWebFindCallback> findListener) = 0; 459 460 /** 461 * Finds all instances of find on the page and highlights them, 462 * asynchronously. 463 * 464 * @param searchStr String: target string to find. 465 */ 466 virtual void FindAllAsync(const std::string &searchStr) const = 0; 467 468 /** 469 * Clears the highlighting surrounding text matches created by findAllAsync 470 */ 471 virtual void ClearMatches() const = 0; 472 473 /** 474 * Highlights and scrolls to the next match found by findAllAsync(String), 475 * wrapping around page boundaries as necessary. 476 * 477 * @param forward bool: find back or forward. 478 */ 479 virtual void FindNext(const bool forward) const = 0; 480 481 /** 482 * Saves the current view as a web archive. 483 * 484 * @param baseName the filename where the archive should be placed This value cannot be null. 485 * @param autoName if false, takes basename to be a file. If true, basename is assumed to be 486 * a directory in which a filename will be chosen according to the URL of the 487 * current page. 488 */ 489 virtual void StoreWebArchive(const std::string &baseName, bool autoName, 490 std::shared_ptr<NWebValueCallback<std::string>> callback) const = 0; 491 492 /** 493 * create two web message ports. 494 * @param ports the web message ports. 495 */ 496 virtual void CreateWebMessagePorts(std::vector<std::string>& ports) = 0; 497 498 /** 499 * post messag event to the html main frame. 500 * @param message the message 501 * @param ports the web message ports. 502 * @param uri the uri 503 */ 504 virtual void PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUri) = 0; 505 506 /** 507 * close the message port. 508 * @param handle the web message port handle. 509 */ 510 virtual void ClosePort(std::string& handle) = 0; 511 512 /** 513 * use the port to send message. 514 * @param handle the web message port handle. 515 * @param data the message send to html5. 516 */ 517 virtual void PostPortMessage(std::string& handle, std::shared_ptr<NWebMessage> data) = 0; 518 519 /** 520 * set the callback of th port handle. 521 * @param handle the web message port handle. 522 * @param callback to receive the message when th other port post message. 523 */ 524 virtual void SetPortMessageCallback(std::string& handle, 525 std::shared_ptr<NWebValueCallback<std::shared_ptr<NWebMessage>>> callback) = 0; 526 527 /** 528 * send drag event to nweb. 529 * @param dragEvent the drag event information. 530 */ 531 virtual void SendDragEvent(const DragEvent& dragEvent) const = 0; 532 533 /** 534 * Clear ssl cache. 535 * 536 */ 537 virtual void ClearSslCache() = 0; 538 539 /** 540 * get web page url. 541 * 542 * @return web page url. 543 */ 544 virtual std::string GetUrl() const = 0; 545 546 /** 547 * Clears the client authentication certificate cache in the Web. 548 * @since 9 549 */ 550 virtual void ClearClientAuthenticationCache() = 0; 551 552 /** 553 * set the locale name of current system setting. 554 * 555 * @param locale the locale name of current system setting. 556 */ 557 virtual void UpdateLocale(const std::string& language, const std::string& region) = 0; 558 559 /** 560 * Get the original url of the current web page. 561 * 562 * @return original url 563 */ 564 virtual const std::string GetOriginalUrl() const = 0; 565 566 /** 567 * get the favicon of the request web page. 568 * 569 * @param data the raw data of the favicon. 570 * @param width the width of the favicon. 571 * @param height the height of the favicon. 572 * @param colorType the color type of the favicon. 573 * @param alphaType the alpha type of the favicon. 574 * @return true if get the favicon successfully, otherwise return false. 575 */ 576 virtual bool GetFavicon(const void** data, size_t& width, size_t& height, 577 ImageColorType& colorType, ImageAlphaType& alphaType) = 0; 578 579 /** 580 * Set the network status, just notify the webview to change the property of navigator.online. 581 * 582 * @param available the status of the network. 583 */ 584 virtual void PutNetworkAvailable(bool available) = 0; 585 586 /** 587 * web has image or not 588 * 589 * @param callback has image or not 590 */ 591 virtual void HasImages(std::shared_ptr<NWebValueCallback<bool>> callback) = 0; 592 593 /** 594 * web remove cache 595 * 596 * @param include_disk_files bool:if false, only tje RAM cache is removed 597 */ 598 virtual void RemoveCache(bool include_disk_files) = 0; 599 600 /** 601 * Get navigation history list 602 * 603 * @return navigation history list 604 */ 605 virtual std::shared_ptr<NWebHistoryList> GetHistoryList() = 0; 606 607 /** 608 * Set the NWebReleaseSurfaceCallback that will receive release surface event. 609 * This will replace the current handler. 610 * 611 * @param releaseSurfaceListener NWebReleaseSurfaceCallback. 612 */ 613 virtual void PutReleaseSurfaceCallback( 614 std::shared_ptr<NWebReleaseSurfaceCallback> releaseSurfaceListener) = 0; 615 616 /** 617 * Get Web back forward state. 618 * 619 * @return web back forward state. 620 */ 621 virtual WebState SerializeWebState() = 0; 622 623 /** 624 * Restore Web back forward state. 625 * 626 * @param web back forward state. 627 */ 628 virtual bool RestoreWebState(WebState state) = 0; 629 630 /** 631 * Move page up. 632 * 633 * @param top whether move to the top. 634 */ 635 virtual void PageUp(bool top) = 0; 636 637 /** 638 * Move page down. 639 * 640 * @param bottom whether move to the bottom. 641 */ 642 virtual void PageDown(bool bottom) = 0; 643 644 /** 645 * Scroll to the position. 646 * 647 * @param x the x of the position. 648 * @param y the y of the position. 649 */ 650 virtual void ScrollTo(float x, float y) = 0; 651 652 /** 653 * Scroll by the delta position. 654 * 655 * @param deltaX the deltaX of the position. 656 * @param deltaY the deltaY of the position. 657 */ 658 virtual void ScrollBy(float deltaX, float deltaY) = 0; 659 660 /** 661 * Slide by the speed. 662 * 663 * @param vx the vx of the speed. 664 * @param vy the vy of the speed. 665 */ 666 virtual void SlideScroll(float vx, float vy) = 0; 667 668 /** 669 * Get current website certificate. 670 * 671 * @param certChainData current website certificate array. 672 * @param isSingleCert true if only get one certificate of current website, 673 * false if get certificate chain of the website. 674 * @return true if get certificate successfully, otherwise false. 675 */ 676 virtual bool GetCertChainDerData(std::vector<std::string>& certChainData, bool isSingleCert) = 0; 677 678 /** 679 * Set screen offset. 680 * 681 * @param x the offset in x direction. 682 * @param y the offset in y direction. 683 */ 684 virtual void SetScreenOffSet(double x, double y) = 0; 685 686 /** 687 * Set audio muted. 688 * 689 * @param muted Aduio mute state. 690 */ 691 virtual void SetAudioMuted(bool muted) = 0; 692 693 /** 694 * Set should frame submission before draw. 695 * 696 * @param should whether wait render frame submission. 697 */ 698 virtual void SetShouldFrameSubmissionBeforeDraw(bool should) = 0; 699 700 /** 701 * Notify whether the popup window is initialized successfully. 702 * 703 * @param result whether success. 704 */ 705 virtual void NotifyPopupWindowResult(bool result) = 0; 706 707 /** 708 * Set audio resume interval. 709 * 710 * @param resumeInterval Aduio resume interval. 711 */ 712 virtual void SetAudioResumeInterval(int32_t resumeInterval) = 0; 713 714 /** 715 * Set audio exclusive state. 716 * 717 * @param audioExclusive Aduio exclusive state. 718 */ 719 virtual void SetAudioExclusive(bool audioExclusive) = 0; 720 721 /** 722 * Rigest the keep srceen on interface. 723 * 724 * @param windowId the window id. 725 * @param SetKeepScreenOn the screenon handle. 726 */ 727 virtual void RegisterScreenLockFunction(int32_t windowId, const SetKeepScreenOn&& handle) = 0; 728 729 /** 730 * UnRigest the keep srceen on interface. 731 * 732 * @param windowId the window id. 733 */ 734 virtual void UnRegisterScreenLockFunction(int32_t windowId) = 0; 735 736 /** 737 * Notify memory level. 738 * 739 * @param level the memory level. 740 */ 741 virtual void NotifyMemoryLevel(int32_t level) = 0; 742 743 /** 744 * Notify webview window status. 745 */ 746 virtual void OnWebviewHide() const = 0; 747 virtual void OnWebviewShow() const = 0; 748 749 /** 750 * Get the drag data. 751 * 752 * @return the data being dragged. 753 */ 754 virtual std::shared_ptr<NWebDragData> GetOrCreateDragData() = 0; 755 756 /** 757 * Prefetch the resources required by the page, but will not execute js or 758 * render the page. 759 * 760 * @param url String: Which url to preresolve/preconnect. 761 * @param additionalHttpHeaders Additional HTTP request headers of the URL. 762 */ 763 virtual void PrefetchPage( 764 std::string& url, 765 std::map<std::string, std::string> additionalHttpHeaders) = 0; 766 767 /** 768 * Set the window id. 769 */ 770 virtual void SetWindowId(uint32_t window_id) = 0; 771 }; 772 } // namespace OHOS::NWeb 773 774 #endif // NWEB_H 775