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 AccessibilityEventListenerImpl : public OHOS::NWeb::NWebAccessibilityEventCallback {
51 public:
52     AccessibilityEventListenerImpl() = default;
53     ~AccessibilityEventListenerImpl() = default;
54 
55     void OnAccessibilityEvent(int64_t accessibilityId, uint32_t eventType) override;
56 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)57     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
58     {
59         webDelegate_ = delegate;
60     }
61 
62 private:
63     WeakPtr<WebDelegate> webDelegate_;
64 };
65 
66 class ReleaseSurfaceImpl : public OHOS::NWeb::NWebReleaseSurfaceCallback {
67 public:
68     ReleaseSurfaceImpl() = default;
69     ~ReleaseSurfaceImpl() = default;
70 
71     void ReleaseSurface() override;
72 
SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> & surfaceDelegate)73     void SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> &surfaceDelegate)
74     {
75         surfaceDelegate_ = surfaceDelegate;
76     }
77 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)78     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
79     {
80         webDelegate_ = delegate;
81     }
82 
83 private:
84     sptr<OHOS::SurfaceDelegate> surfaceDelegate_ = nullptr;
85     WeakPtr<WebDelegate> webDelegate_;
86 };
87 class FindListenerImpl : public OHOS::NWeb::NWebFindCallback {
88 public:
89     FindListenerImpl() = default;
90     ~FindListenerImpl() = default;
91 
92     void OnFindResultReceived(
93         const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting) override;
94 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)95     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
96     {
97         webDelegate_ = delegate;
98     }
99 
100 private:
101     WeakPtr<WebDelegate> webDelegate_;
102 };
103 
104 class SpanstringConvertHtmlImpl : public OHOS::NWeb::NWebSpanstringConvertHtmlCallback {
105 public:
106     SpanstringConvertHtmlImpl() = default;
SpanstringConvertHtmlImpl(int32_t instanceId)107     explicit SpanstringConvertHtmlImpl(int32_t instanceId) : instanceId_(instanceId) {}
108     ~SpanstringConvertHtmlImpl() = default;
109 
110     virtual std::string SpanstringConvertHtml(const std::vector<uint8_t> &content) override;
111 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)112     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
113     {
114         webDelegate_ = delegate;
115     }
116 
117 private:
118     WeakPtr<WebDelegate> webDelegate_;
119     int32_t instanceId_ = -1;
120 };
121 
122 class WebClientImpl :
123     public std::enable_shared_from_this<WebClientImpl>,
124     public OHOS::NWeb::NWebHandler {
125 public:
126     WebClientImpl() = default;
127     ~WebClientImpl() = default;
128 
129     void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override;
130     void OnProxyDied() override;
131     void OnRouterPush(const std::string& param) override;
132     bool OnConsoleLog(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message) override;
133     void OnMessage(const std::string& param) override;
134     void OnPageLoadBegin(const std::string& url) override;
135     void OnPageLoadEnd(int httpStatusCode, const std::string& url) override;
136     void OnLoadingProgress(int newProgress) override;
137     void OnPageTitle(const std::string &title) override;
138     void OnGeolocationHide() override;
139     void OnFullScreenExit() override;
140     void OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler) override;
141     void OnFullScreenEnterWithVideoSize(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler,
142         int videoNaturalWidth, int videoNaturalHeight) override;
143     void OnGeolocationShow(const std::string& origin,
144         std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback) override;
145 
146     bool OnAlertDialogByJS(const std::string &url,
147                            const std::string &message,
148                            std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
149     bool OnBeforeUnloadByJS(const std::string &url,
150                             const std::string &message,
151                             std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
152     bool OnConfirmDialogByJS(const std::string &url,
153                              const std::string &message,
154                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
155     bool OnPromptDialogByJS(const std::string &url,
156                              const std::string &message,
157                              const std::string &defaultValue,
158                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
159     bool OnFileSelectorShow(std::shared_ptr<NWeb::NWebStringVectorValueCallback> callback,
160                             std::shared_ptr<NWeb::NWebFileSelectorParams> params) override;
161 
162     bool OnFocus() override;
163     bool OnFocus(OHOS::NWeb::NWebFocusSource source) override;
164     void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
165         std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override;
166     void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
167         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
168     void OnRenderExited(OHOS::NWeb::RenderExitReason reason) override;
169     void OnRefreshAccessedHistory(const std::string& url, bool isReload) override;
170     bool OnHandleInterceptRequest(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
171                                   std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
172     bool OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
173     void OnResource(const std::string& url) override;
174     void OnScaleChanged(float oldScaleFactor, float newScaleFactor) override;
175     void OnScroll(double xOffset, double yOffset) override;
176     bool OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host,
177         const std::string &realm) override;
178     bool OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
179         OHOS::NWeb::SslError error) override;
180     bool OnAllSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSAllSslErrorResult> result,
181         OHOS::NWeb::SslError error,
182         const std::string& url,
183         const std::string& originalUrl,
184         const std::string& referrer,
185         bool isFatalError,
186         bool isMainFrame) override;
187     bool OnSslSelectCertRequestByJS(
188         std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,
189         const std::string& host,
190         int32_t port,
191         const std::vector<std::string>& keyTypes,
192         const std::vector<std::string>& issuers) override;
193     void OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request) override;
194     bool RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params,
195         std::shared_ptr<NWeb::NWebContextMenuCallback> callback) override;
196     void UpdateClippedSelectionBounds(int x, int y, int w, int h) override;
197     bool RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,
198                       std::shared_ptr<NWeb::NWebQuickMenuCallback> callback) override;
199     void OnQuickMenuDismissed() override;
200     void HideHandleAndQuickMenuIfNecessary(bool hide) override;
201     void ChangeVisibilityOfQuickMenu() override;
202     void OnTouchSelectionChanged(
203         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
204         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
205         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle) override;
206     bool OnDragAndDropData(const void* data, size_t len, std::shared_ptr<NWeb::NWebImageOptions> opt) override;
207     bool OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData) override;
208     void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) override;
209     void OnWindowNewByJS(
210         const std::string& targetUrl,
211         bool isAlert,
212         bool isUserTrigger,
213         std::shared_ptr<NWeb::NWebControllerHandler> handler) override;
214     void OnWindowExitByJS() override;
215     void OnPageVisible(const std::string& url) override;
216     void OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler) override;
217     void OnNavigationEntryCommitted(std::shared_ptr<NWeb::NWebLoadCommittedDetails> details) override;
218     void OnPageIcon(
219         const void* data,
220         size_t width,
221         size_t height,
222         NWeb::ImageColorType colorType,
223         NWeb::ImageAlphaType alphaType) override;
224     void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override;
225     bool OnCursorChange(const NWeb::CursorType& type, std::shared_ptr<NWeb::NWebCursorInfo> info) override;
226     void OnSelectPopupMenu(std::shared_ptr<NWeb::NWebSelectPopupMenuParam> params,
227                            std::shared_ptr<NWeb::NWebSelectPopupMenuCallback> callback) override;
228     void OnAudioStateChanged(bool playing) override;
229     void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs) override;
230     void OnFirstMeaningfulPaint(std::shared_ptr<NWeb::NWebFirstMeaningfulPaintDetails> details) override;
231     void OnLargestContentfulPaint(std::shared_ptr<NWeb::NWebLargestContentfulPaintDetails> details) override;
232     void OnSafeBrowsingCheckResult(int threat_type) override;
233     void OnCompleteSwapWithNewSize() override;
234     void OnResizeNotWork() override;
235     void OnGetTouchHandleHotZone(std::shared_ptr<NWeb::NWebTouchHandleHotZone> hotZone) override;
236     void OnDateTimeChooserPopup(
237         std::shared_ptr<NWeb::NWebDateTimeChooser> chooser,
238         const std::vector<std::shared_ptr<NWeb::NWebDateTimeSuggestion>>& suggestions,
239         std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback) override;
240     void OnDateTimeChooserClose() override;
241     void OnOverScroll(float xOffset, float yOffset) override;
242     void OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request) override;
243     void OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling) override;
244     void OnOverScrollFlingEnd() override;
245     void OnScrollState(bool scrollState) override;
246     void OnRootLayerChanged(int width, int height) override;
247     void ReleaseResizeHold() override;
248     bool FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity) override;
249     void OnNativeEmbedLifecycleChange(std::shared_ptr<NWeb::NWebNativeEmbedDataInfo> dataInfo) override;
250     void OnNativeEmbedGestureEvent(std::shared_ptr<NWeb::NWebNativeEmbedTouchEvent> event) override;
251     void OnIntelligentTrackingPreventionResult(
252         const std::string& websiteHost, const std::string& trackerHost) override;
253     void OnTooltip(const std::string& tooltip) override;
254     bool OnHandleOverrideUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
255     bool OnOpenAppLink(const std::string& url,
256                        std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback> callback) override;
257     void OnShowAutofillPopup(
258         const float offsetX, const float offsetY, const std::vector<std::string>& menu_items) override;
259     void OnHideAutofillPopup() override;
260 
261     void OnAdsBlocked(const std::string& url, const std::vector<std::string>& adsBlocked) override;
262 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)263     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
264     {
265         webDelegate_ = delegate;
266     }
267 
GetWebDelegate()268     const RefPtr<WebDelegate> GetWebDelegate() const
269     {
270         return webDelegate_.Upgrade();
271     }
272 
273     std::vector<int8_t> GetWordSelection(const std::string& text, int8_t offset) override;
274     void OnRenderProcessNotResponding(
275         const std::string& jsStack, int pid, OHOS::NWeb::RenderProcessNotRespondingReason reason) override;
276     void OnRenderProcessResponding() override;
277 
278     void OnViewportFitChange(NWeb::ViewportFit viewportFit) override;
279 
280     void CreateOverlay(void* data, size_t len, int width, int height, int offsetX, int offsetY, int rectWidth,
281         int rectHeight, int pointX, int pointY) override;
282 
283     void OnOverlayStateChanged(int offsetX, int offsetY, int rectWidth, int rectHeight) override;
284 
285     void OnInterceptKeyboardAttach(
286         const std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler,
287         const std::map<std::string, std::string> &attributes, bool &useSystemKeyboard, int32_t &enterKeyType) override;
288 
289     void OnCustomKeyboardAttach() override;
290 
291     void OnCustomKeyboardClose() override;
292 
293     void KeyboardReDispatch(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event, bool isUsed) override;
294 
295     void OnCursorUpdate(double x, double y, double width, double height) override;
296 
297     void OnNativeEmbedVisibilityChange(const std::string& embed_id, bool visibility) override;
298 
299     void StartVibraFeedback(const std::string& vibratorType) override;
300 
301     bool CloseImageOverlaySelection() override;
302 
303     void GetVisibleRectToWeb(int& visibleX, int& visibleY, int& visibleWidth, int& visibleHeight) 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