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