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