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 std::string OnHandleOverrideErrorPage(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 158 std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override; 159 bool OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override; 160 void OnResource(const std::string& url) override; 161 void OnScaleChanged(float oldScaleFactor, float newScaleFactor) override; 162 void OnScroll(double xOffset, double yOffset) override; 163 bool OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host, 164 const std::string &realm) override; 165 bool OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result, 166 OHOS::NWeb::SslError error) override; 167 bool OnAllSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSAllSslErrorResult> result, 168 OHOS::NWeb::SslError error, 169 const std::string& url, 170 const std::string& originalUrl, 171 const std::string& referrer, 172 bool isFatalError, 173 bool isMainFrame) override; 174 bool OnSslSelectCertRequestByJS( 175 std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result, 176 const std::string& host, 177 int32_t port, 178 const std::vector<std::string>& keyTypes, 179 const std::vector<std::string>& issuers) override; 180 void OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request) override; 181 bool RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params, 182 std::shared_ptr<NWeb::NWebContextMenuCallback> callback) override; 183 void UpdateClippedSelectionBounds(int x, int y, int w, int h) override; 184 bool RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params, 185 std::shared_ptr<NWeb::NWebQuickMenuCallback> callback) override; 186 void OnQuickMenuDismissed() override; 187 void HideHandleAndQuickMenuIfNecessary(bool hide) override; 188 void ChangeVisibilityOfQuickMenu() override; 189 bool ChangeVisibilityOfQuickMenuV2() override; 190 void OnTouchSelectionChanged( 191 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle, 192 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle, 193 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle) override; 194 bool OnDragAndDropData(const void* data, size_t len, std::shared_ptr<NWeb::NWebImageOptions> opt) override; 195 bool OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData) override; 196 void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) override; 197 void OnWindowNewByJS( 198 const std::string& targetUrl, 199 bool isAlert, 200 bool isUserTrigger, 201 std::shared_ptr<NWeb::NWebControllerHandler> handler) override; 202 void OnActivateContentByJS() override; 203 void OnWindowExitByJS() override; 204 void OnPageVisible(const std::string& url) override; 205 void OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler) override; 206 void OnNavigationEntryCommitted(std::shared_ptr<NWeb::NWebLoadCommittedDetails> details) override; 207 void OnPageIcon( 208 const void* data, 209 size_t width, 210 size_t height, 211 NWeb::ImageColorType colorType, 212 NWeb::ImageAlphaType alphaType) override; 213 void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override; 214 bool OnCursorChange(const NWeb::CursorType& type, std::shared_ptr<NWeb::NWebCursorInfo> info) override; 215 void OnSelectPopupMenu(std::shared_ptr<NWeb::NWebSelectPopupMenuParam> params, 216 std::shared_ptr<NWeb::NWebSelectPopupMenuCallback> callback) override; 217 void OnAudioStateChanged(bool playing) override; 218 void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs) override; 219 void OnFirstMeaningfulPaint(std::shared_ptr<NWeb::NWebFirstMeaningfulPaintDetails> details) override; 220 void OnLargestContentfulPaint(std::shared_ptr<NWeb::NWebLargestContentfulPaintDetails> details) override; 221 void OnSafeBrowsingCheckResult(int threat_type) override; 222 void OnCompleteSwapWithNewSize() override; 223 void OnResizeNotWork() override; 224 void OnGetTouchHandleHotZone(std::shared_ptr<NWeb::NWebTouchHandleHotZone> hotZone) override; 225 void OnDateTimeChooserPopup( 226 std::shared_ptr<NWeb::NWebDateTimeChooser> chooser, 227 const std::vector<std::shared_ptr<NWeb::NWebDateTimeSuggestion>>& suggestions, 228 std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback) override; 229 void OnDateTimeChooserClose() override; 230 void OnOverScroll(float xOffset, float yOffset) override; 231 void OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request) override; 232 void OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling) override; 233 void OnOverScrollFlingEnd() override; 234 void OnScrollState(bool scrollState) override; 235 void EnableSecurityLayer(bool isNeedSecurityLayer) override; 236 void OnRootLayerChanged(int width, int height) override; 237 void ReleaseResizeHold() override; 238 bool FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity) override; 239 void OnNativeEmbedLifecycleChange(std::shared_ptr<NWeb::NWebNativeEmbedDataInfo> dataInfo) override; 240 void OnNativeEmbedGestureEvent(std::shared_ptr<NWeb::NWebNativeEmbedTouchEvent> event) override; 241 void OnNativeEmbedMouseEvent(std::shared_ptr<NWeb::NWebNativeEmbedMouseEvent> event) override; 242 void OnIntelligentTrackingPreventionResult( 243 const std::string& websiteHost, const std::string& trackerHost) override; 244 void OnTooltip(const std::string& tooltip) override; 245 void OnPopupSize(int x, int y, int width, int height) override; 246 void OnPopupShow(bool show) override; 247 bool OnHandleOverrideUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override; 248 bool OnOpenAppLink(const std::string& url, 249 std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback> callback) override; 250 void OnShowAutofillPopup( 251 const float offsetX, const float offsetY, const std::vector<std::string>& menu_items) override; 252 void OnShowAutofillPopupV2( 253 const float offsetX, const float offsetY, const float height, const float width, 254 const std::vector<std::string>& menu_items) override; 255 void OnHideAutofillPopup() override; 256 void OnAdsBlocked(const std::string& url, const std::vector<std::string>& adsBlocked) override; 257 SetWebDelegate(const WeakPtr<WebDelegate> & delegate)258 void SetWebDelegate(const WeakPtr<WebDelegate>& delegate) 259 { 260 webDelegate_ = delegate; 261 } 262 GetWebDelegate()263 const RefPtr<WebDelegate> GetWebDelegate() const 264 { 265 return webDelegate_.Upgrade(); 266 } 267 268 std::vector<int8_t> GetWordSelection(const std::string& text, int8_t offset) override; 269 void OnRenderProcessNotResponding( 270 const std::string& jsStack, int pid, OHOS::NWeb::RenderProcessNotRespondingReason reason) override; 271 void OnRenderProcessResponding() override; 272 273 void OnViewportFitChange(NWeb::ViewportFit viewportFit) override; 274 275 void CreateOverlay(void* data, size_t len, int width, int height, int offsetX, int offsetY, int rectWidth, 276 int rectHeight, int pointX, int pointY) override; 277 278 void OnOverlayStateChanged(int offsetX, int offsetY, int rectWidth, int rectHeight) override; 279 280 void OnInterceptKeyboardAttach( 281 const std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler, 282 const std::map<std::string, std::string> &attributes, bool &useSystemKeyboard, int32_t &enterKeyType) override; 283 284 void OnCustomKeyboardAttach() override; 285 286 void OnCustomKeyboardClose() override; 287 288 void KeyboardReDispatch(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event, bool isUsed) override; 289 290 void OnTakeFocus(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event) override; 291 292 void OnCursorUpdate(double x, double y, double width, double height) override; 293 294 void StartVibraFeedback(const std::string& vibratorType) override; 295 296 void OnNativeEmbedVisibilityChange(const std::string& embedId, bool visibility) override; 297 298 bool CloseImageOverlaySelection() override; 299 300 bool OnSslErrorRequestByJSV2(std::shared_ptr<NWeb::NWebJSSslErrorResult> result, 301 OHOS::NWeb::SslError error, const std::vector<std::string>& certChainData) override; 302 303 void OnAccessibilityEventV2(int64_t accessibilityId, int32_t eventType, const std::string& argument) override; 304 305 bool IsCurrentFocus() override; 306 307 void GetVisibleRectToWeb(int& visibleX, int& visibleY, int& visibleWidth, int& visibleHeight) override; 308 309 void OnScrollStart(const float x, const float y) override; 310 311 void RestoreRenderFit() override; 312 313 bool OnNestedScroll(float& x, float& y, float& xVelocity, float& yVelocity, bool& isAvailable) override; 314 315 void OnLoadStarted(const std::string& url) override; 316 317 void OnLoadFinished(const std::string& url) override; 318 319 void OnPip(int status, int delegate_id, int child_id, int frame_routing_id, int width, int height) override; 320 321 bool OnAllSslErrorRequestByJSV2(std::shared_ptr<NWeb::NWebJSAllSslErrorResult> result, OHOS::NWeb::SslError error, 322 const std::string& url, const std::string& originalUrl, const std::string& referrer, bool isFatalError, 323 bool isMainFrame, const std::vector<std::string>& certChainData) override; 324 325 void ShowMagnifier() override; 326 327 void HideMagnifier() override; 328 329 void OnPageTitleV2(const std::string &title, bool isRealTitle) override; 330 331 void OnInsertBlanklessFrame(const std::string& pathToFrame) override; 332 void OnRemoveBlanklessFrame(int delayTime) override; 333 bool OnBeforeUnloadByJSV2(const std::string& url, const std::string& message, bool isReload, 334 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 335 336 void OnPdfScrollAtBottom(const std::string& url) override; 337 void OnPdfLoadEvent(int32_t result, const std::string& url) override; 338 private: 339 std::weak_ptr<OHOS::NWeb::NWeb> webviewWeak_; 340 WeakPtr<WebDelegate> webDelegate_; 341 }; 342 } // namespace OHOS::Ace 343 344 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H 345