• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H
18 
19 #include "foundation/arkui/ace_engine/frameworks/base/memory/referenced.h"
20 #include "nweb_drag_data.h"
21 #include "nweb_handler.h"
22 
23 #include "base/log/log.h"
24 #include "core/common/container_scope.h"
25 #include "surface_delegate.h"
26 #ifdef ENABLE_ROSEN_BACKEND
27 #include "surface.h"
28 #endif
29 
30 namespace OHOS::Ace {
31 class WebDelegate;
32 
33 class DownloadListenerImpl : public OHOS::NWeb::NWebDownloadCallback {
34 public:
35     DownloadListenerImpl() = default;
36     ~DownloadListenerImpl() = default;
37 
38     void OnDownloadStart(const std::string& url, const std::string& userAgent, const std::string& contentDisposition,
39         const std::string& mimetype, long contentLength) override;
40 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)41     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
42     {
43         webDelegate_ = delegate;
44     }
45 
46 private:
47     WeakPtr<WebDelegate> webDelegate_;
48 };
49 
50 class ReleaseSurfaceImpl : public OHOS::NWeb::NWebReleaseSurfaceCallback {
51 public:
52     ReleaseSurfaceImpl() = default;
53     ~ReleaseSurfaceImpl() = default;
54 
55     void ReleaseSurface() override;
56 
SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> & surfaceDelegate)57     void SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> &surfaceDelegate)
58     {
59         surfaceDelegate_ = surfaceDelegate;
60     }
61 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)62     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
63     {
64         webDelegate_ = delegate;
65     }
66 
67 private:
68     sptr<OHOS::SurfaceDelegate> surfaceDelegate_ = nullptr;
69     WeakPtr<WebDelegate> webDelegate_;
70 };
71 class FindListenerImpl : public OHOS::NWeb::NWebFindCallback {
72 public:
73     FindListenerImpl() = default;
74     ~FindListenerImpl() = default;
75 
76     void OnFindResultReceived(
77         const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting) override;
78 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)79     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
80     {
81         webDelegate_ = delegate;
82     }
83 
84 private:
85     WeakPtr<WebDelegate> webDelegate_;
86 };
87 
88 class SpanstringConvertHtmlImpl : public OHOS::NWeb::NWebSpanstringConvertHtmlCallback {
89 public:
90     SpanstringConvertHtmlImpl() = default;
SpanstringConvertHtmlImpl(int32_t instanceId)91     explicit SpanstringConvertHtmlImpl(int32_t instanceId) : instanceId_(instanceId) {}
92     ~SpanstringConvertHtmlImpl() = default;
93 
94     virtual std::string SpanstringConvertHtml(const std::vector<uint8_t> &content) override;
95 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)96     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
97     {
98         webDelegate_ = delegate;
99     }
100 
101 private:
102     WeakPtr<WebDelegate> webDelegate_;
103     int32_t instanceId_ = -1;
104 };
105 
106 class WebClientImpl :
107     public std::enable_shared_from_this<WebClientImpl>,
108     public OHOS::NWeb::NWebHandler {
109 public:
110     WebClientImpl() = default;
111     ~WebClientImpl() = default;
112 
113     void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override;
114     void OnProxyDied() override;
115     void OnRouterPush(const std::string& param) override;
116     bool OnConsoleLog(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message) override;
117     void OnMessage(const std::string& param) override;
118     void OnPageLoadBegin(const std::string& url) override;
119     void OnPageLoadEnd(int httpStatusCode, const std::string& url) override;
120     void OnLoadingProgress(int newProgress) override;
121     void OnPageTitle(const std::string &title) override;
122     void OnGeolocationHide() override;
123     void OnFullScreenExit() override;
124     void OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler) override;
125     void OnFullScreenEnterWithVideoSize(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler,
126         int videoNaturalWidth, int videoNaturalHeight) override;
127     void OnGeolocationShow(const std::string& origin,
128         std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback) override;
129 
130     bool OnAlertDialogByJS(const std::string &url,
131                            const std::string &message,
132                            std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
133     bool OnBeforeUnloadByJS(const std::string &url,
134                             const std::string &message,
135                             std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
136     bool OnConfirmDialogByJS(const std::string &url,
137                              const std::string &message,
138                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
139     bool OnPromptDialogByJS(const std::string &url,
140                              const std::string &message,
141                              const std::string &defaultValue,
142                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
143     bool OnFileSelectorShow(std::shared_ptr<NWeb::NWebStringVectorValueCallback> callback,
144                             std::shared_ptr<NWeb::NWebFileSelectorParams> params) override;
145 
146     bool OnFocus() override;
147     bool OnFocus(OHOS::NWeb::NWebFocusSource source) override;
148     void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
149         std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override;
150     void ReportDynamicFrameLossEvent(const std::string& sceneId, bool isStart) override;
151     void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
152         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
153     void OnRenderExited(OHOS::NWeb::RenderExitReason reason) override;
154     void OnRefreshAccessedHistory(const std::string& url, bool isReload) override;
155     bool OnHandleInterceptRequest(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
156                                   std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
157     bool OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
158     void OnResource(const std::string& url) override;
159     void OnScaleChanged(float oldScaleFactor, float newScaleFactor) override;
160     void OnScroll(double xOffset, double yOffset) override;
161     bool OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host,
162         const std::string &realm) override;
163     bool OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
164         OHOS::NWeb::SslError error) override;
165     bool OnAllSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSAllSslErrorResult> result,
166         OHOS::NWeb::SslError error,
167         const std::string& url,
168         const std::string& originalUrl,
169         const std::string& referrer,
170         bool isFatalError,
171         bool isMainFrame) override;
172     bool OnSslSelectCertRequestByJS(
173         std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,
174         const std::string& host,
175         int32_t port,
176         const std::vector<std::string>& keyTypes,
177         const std::vector<std::string>& issuers) override;
178     void OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request) override;
179     bool RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params,
180         std::shared_ptr<NWeb::NWebContextMenuCallback> callback) override;
181     void UpdateClippedSelectionBounds(int x, int y, int w, int h) override;
182     bool RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,
183                       std::shared_ptr<NWeb::NWebQuickMenuCallback> callback) override;
184     void OnQuickMenuDismissed() override;
185     void HideHandleAndQuickMenuIfNecessary(bool hide) override;
186     void ChangeVisibilityOfQuickMenu() override;
187     void OnTouchSelectionChanged(
188         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
189         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
190         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle) override;
191     bool OnDragAndDropData(const void* data, size_t len, std::shared_ptr<NWeb::NWebImageOptions> opt) override;
192     bool OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData) override;
193     void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) override;
194     void OnWindowNewByJS(
195         const std::string& targetUrl,
196         bool isAlert,
197         bool isUserTrigger,
198         std::shared_ptr<NWeb::NWebControllerHandler> handler) override;
199     void OnWindowExitByJS() override;
200     void OnPageVisible(const std::string& url) override;
201     void OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler) override;
202     void OnNavigationEntryCommitted(std::shared_ptr<NWeb::NWebLoadCommittedDetails> details) override;
203     void OnPageIcon(
204         const void* data,
205         size_t width,
206         size_t height,
207         NWeb::ImageColorType colorType,
208         NWeb::ImageAlphaType alphaType) override;
209     void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override;
210     bool OnCursorChange(const NWeb::CursorType& type, std::shared_ptr<NWeb::NWebCursorInfo> info) override;
211     void OnSelectPopupMenu(std::shared_ptr<NWeb::NWebSelectPopupMenuParam> params,
212                            std::shared_ptr<NWeb::NWebSelectPopupMenuCallback> callback) override;
213     void OnAudioStateChanged(bool playing) override;
214     void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs) override;
215     void OnFirstMeaningfulPaint(std::shared_ptr<NWeb::NWebFirstMeaningfulPaintDetails> details) override;
216     void OnLargestContentfulPaint(std::shared_ptr<NWeb::NWebLargestContentfulPaintDetails> details) override;
217     void OnSafeBrowsingCheckResult(int threat_type) override;
218     void OnCompleteSwapWithNewSize() override;
219     void OnResizeNotWork() override;
220     void OnGetTouchHandleHotZone(std::shared_ptr<NWeb::NWebTouchHandleHotZone> hotZone) override;
221     void OnDateTimeChooserPopup(
222         std::shared_ptr<NWeb::NWebDateTimeChooser> chooser,
223         const std::vector<std::shared_ptr<NWeb::NWebDateTimeSuggestion>>& suggestions,
224         std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback) override;
225     void OnDateTimeChooserClose() override;
226     void OnOverScroll(float xOffset, float yOffset) override;
227     void OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request) override;
228     void OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling) override;
229     void OnOverScrollFlingEnd() override;
230     void OnScrollState(bool scrollState) override;
231     void OnRootLayerChanged(int width, int height) override;
232     void ReleaseResizeHold() override;
233     bool FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity) override;
234     void OnNativeEmbedLifecycleChange(std::shared_ptr<NWeb::NWebNativeEmbedDataInfo> dataInfo) override;
235     void OnNativeEmbedGestureEvent(std::shared_ptr<NWeb::NWebNativeEmbedTouchEvent> event) override;
236     void OnIntelligentTrackingPreventionResult(
237         const std::string& websiteHost, const std::string& trackerHost) override;
238     void OnTooltip(const std::string& tooltip) override;
239     void OnPopupSize(int x, int y, int width, int height) override;
240     void OnPopupShow(bool show) override;
241     bool OnHandleOverrideUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
242     bool OnOpenAppLink(const std::string& url,
243                        std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback> callback) override;
244     void OnShowAutofillPopup(
245         const float offsetX, const float offsetY, const std::vector<std::string>& menu_items) override;
246     void OnShowAutofillPopupV2(
247         const float offsetX, const float offsetY, const float height, const float width,
248         const std::vector<std::string>& menu_items) override;
249     void OnHideAutofillPopup() override;
250     void OnAdsBlocked(const std::string& url, const std::vector<std::string>& adsBlocked) override;
251 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)252     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
253     {
254         webDelegate_ = delegate;
255     }
256 
GetWebDelegate()257     const RefPtr<WebDelegate> GetWebDelegate() const
258     {
259         return webDelegate_.Upgrade();
260     }
261 
262     std::vector<int8_t> GetWordSelection(const std::string& text, int8_t offset) override;
263     void OnRenderProcessNotResponding(
264         const std::string& jsStack, int pid, OHOS::NWeb::RenderProcessNotRespondingReason reason) override;
265     void OnRenderProcessResponding() override;
266 
267     void OnViewportFitChange(NWeb::ViewportFit viewportFit) override;
268 
269     void CreateOverlay(void* data, size_t len, int width, int height, int offsetX, int offsetY, int rectWidth,
270         int rectHeight, int pointX, int pointY) override;
271 
272     void OnOverlayStateChanged(int offsetX, int offsetY, int rectWidth, int rectHeight) override;
273 
274     void OnInterceptKeyboardAttach(
275         const std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler,
276         const std::map<std::string, std::string> &attributes, bool &useSystemKeyboard, int32_t &enterKeyType) override;
277 
278     void OnCustomKeyboardAttach() override;
279 
280     void OnCustomKeyboardClose() override;
281 
282     void KeyboardReDispatch(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event, bool isUsed) override;
283 
284     void OnCursorUpdate(double x, double y, double width, double height) override;
285 
286     void StartVibraFeedback(const std::string& vibratorType) override;
287 
288     void OnNativeEmbedVisibilityChange(const std::string& embedId, bool visibility) override;
289 
290     bool CloseImageOverlaySelection() override;
291 
292     bool OnSslErrorRequestByJSV2(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
293         OHOS::NWeb::SslError error, const std::vector<std::string>& certChainData) override;
294 
295     void OnAccessibilityEventV2(int64_t accessibilityId, int32_t eventType, const std::string& argument) override;
296 
297     bool IsCurrentFocus() override;
298 
299     void GetVisibleRectToWeb(int& visibleX, int& visibleY, int& visibleWidth, int& visibleHeight) override;
300 
301     void OnScrollStart(const float x, const float y) override;
302 
303     void RestoreRenderFit() override;
304 private:
305     std::weak_ptr<OHOS::NWeb::NWeb> webviewWeak_;
306     WeakPtr<WebDelegate> webDelegate_;
307 };
308 } // namespace OHOS::Ace
309 
310 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H
311