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; DownloadListenerImpl(int32_t instanceId)36 explicit DownloadListenerImpl(int32_t instanceId) : instanceId_(instanceId) {} 37 ~DownloadListenerImpl() = default; 38 39 void OnDownloadStart(const std::string& url, const std::string& userAgent, const std::string& contentDisposition, 40 const std::string& mimetype, long contentLength) override; 41 SetWebDelegate(const WeakPtr<WebDelegate> & delegate)42 void SetWebDelegate(const WeakPtr<WebDelegate>& delegate) 43 { 44 webDelegate_ = delegate; 45 } 46 47 private: 48 WeakPtr<WebDelegate> webDelegate_; 49 int32_t instanceId_ = -1; 50 }; 51 52 class AccessibilityEventListenerImpl : public OHOS::NWeb::NWebAccessibilityEventCallback { 53 public: 54 AccessibilityEventListenerImpl() = default; AccessibilityEventListenerImpl(int32_t instanceId)55 explicit AccessibilityEventListenerImpl(int32_t instanceId) : instanceId_(instanceId) {} 56 ~AccessibilityEventListenerImpl() = default; 57 58 void OnAccessibilityEvent(int64_t accessibilityId, uint32_t eventType) override; 59 SetWebDelegate(const WeakPtr<WebDelegate> & delegate)60 void SetWebDelegate(const WeakPtr<WebDelegate>& delegate) 61 { 62 webDelegate_ = delegate; 63 } 64 65 private: 66 WeakPtr<WebDelegate> webDelegate_; 67 int32_t instanceId_ = -1; 68 }; 69 70 class ReleaseSurfaceImpl : public OHOS::NWeb::NWebReleaseSurfaceCallback { 71 public: 72 ReleaseSurfaceImpl() = default; ReleaseSurfaceImpl(int32_t instanceId)73 explicit ReleaseSurfaceImpl(int32_t instanceId) : instanceId_(instanceId) {} 74 ~ReleaseSurfaceImpl() = default; 75 76 void ReleaseSurface() override; 77 SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> & surfaceDelegate)78 void SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> &surfaceDelegate) 79 { 80 surfaceDelegate_ = surfaceDelegate; 81 } 82 83 private: 84 sptr<OHOS::SurfaceDelegate> surfaceDelegate_ = nullptr; 85 int32_t instanceId_ = -1; 86 }; 87 class FindListenerImpl : public OHOS::NWeb::NWebFindCallback { 88 public: 89 FindListenerImpl() = default; FindListenerImpl(int32_t instanceId)90 explicit FindListenerImpl(int32_t instanceId) : instanceId_(instanceId) {} 91 ~FindListenerImpl() = default; 92 93 void OnFindResultReceived( 94 const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting) 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; WebClientImpl(int32_t instanceId)111 explicit WebClientImpl(int32_t instanceId) : instanceId_(instanceId) {} 112 ~WebClientImpl() = default; 113 114 void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override; 115 void OnProxyDied() override; 116 void OnRouterPush(const std::string& param) override; 117 bool OnConsoleLog(const OHOS::NWeb::NWebConsoleLog& message) override; 118 void OnMessage(const std::string& param) override; 119 void OnPageLoadBegin(const std::string& url) override; 120 void OnPageLoadEnd(int httpStatusCode, const std::string& url) override; 121 void OnLoadingProgress(int newProgress) override; 122 void OnPageTitle(const std::string &title) override; 123 void OnGeolocationHide() override; 124 void OnFullScreenExit() override; 125 void OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler) override; 126 void OnGeolocationShow(const std::string& origin, 127 std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback) override; 128 129 bool OnAlertDialogByJS(const std::string &url, 130 const std::string &message, 131 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 132 bool OnBeforeUnloadByJS(const std::string &url, 133 const std::string &message, 134 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 135 bool OnConfirmDialogByJS(const std::string &url, 136 const std::string &message, 137 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 138 bool OnPromptDialogByJS(const std::string &url, 139 const std::string &message, 140 const std::string &defaultValue, 141 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 142 bool OnFileSelectorShow(std::shared_ptr<NWeb::FileSelectorCallback> callback, 143 std::shared_ptr<NWeb::NWebFileSelectorParams> params) override; 144 145 bool OnFocus() override; 146 void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 147 std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override; 148 void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 149 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override; 150 void OnRenderExited(OHOS::NWeb::RenderExitReason reason) override; 151 void OnRefreshAccessedHistory(const std::string& url, bool isReload) override; 152 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> OnHandleInterceptRequest( 153 std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override; 154 bool OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override; 155 void OnResource(const std::string& url) override; 156 void OnScaleChanged(float oldScaleFactor, float newScaleFactor) override; 157 void OnScroll(double xOffset, double yOffset) override; 158 bool OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host, 159 const std::string &realm) override; 160 bool OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result, 161 OHOS::NWeb::SslError error) override; 162 bool OnSslSelectCertRequestByJS( 163 std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result, 164 const std::string& host, 165 int32_t port, 166 const std::vector<std::string>& keyTypes, 167 const std::vector<std::string>& issuers) override; 168 void OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request) override; 169 bool RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params, 170 std::shared_ptr<NWeb::NWebContextMenuCallback> callback) override; 171 bool RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params, 172 std::shared_ptr<NWeb::NWebQuickMenuCallback> callback) override; 173 void OnQuickMenuDismissed() override; 174 void OnTouchSelectionChanged( 175 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle, 176 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle, 177 std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle) override; 178 bool OnDragAndDropData(const void* data, size_t len, const NWeb::ImageOptions& opt) override; 179 bool OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData) override; 180 void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) override; 181 void OnWindowNewByJS( 182 const std::string& targetUrl, 183 bool isAlert, 184 bool isUserTrigger, 185 std::shared_ptr<NWeb::NWebControllerHandler> handler) override; 186 void OnWindowExitByJS() override; 187 void OnPageVisible(const std::string& url) override; 188 void OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler) override; 189 void OnNavigationEntryCommitted(std::shared_ptr<NWeb::NWebLoadCommittedDetails> details) override; 190 void OnPageIcon( 191 const void* data, 192 size_t width, 193 size_t height, 194 NWeb::ImageColorType colorType, 195 NWeb::ImageAlphaType alphaType) override; 196 void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override; 197 bool OnCursorChange(const NWeb::CursorType& type, const NWeb::NWebCursorInfo& info) override; 198 void OnSelectPopupMenu(std::shared_ptr<NWeb::NWebSelectPopupMenuParam> params, 199 std::shared_ptr<NWeb::NWebSelectPopupMenuCallback> callback) override; 200 void OnAudioStateChanged(bool playing) override; 201 void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs) override; 202 void OnSafeBrowsingCheckResult(int threat_type) override; 203 void OnCompleteSwapWithNewSize() override; 204 void OnResizeNotWork() override; 205 void OnGetTouchHandleHotZone(NWeb::TouchHandleHotZone& hotZone) override; 206 void OnDateTimeChooserPopup( 207 const NWeb::DateTimeChooser& chooser, 208 const std::vector<NWeb::DateTimeSuggestion>& suggestions, 209 std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback) override; 210 void OnDateTimeChooserClose() override; 211 void OnOverScroll(float xOffset, float yOffset) override; 212 void OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request) override; 213 void OnOverScrollFlingVelocity(float xVelocity, float yVelocity, bool isFling) override; 214 void OnOverScrollFlingEnd() override; 215 void OnScrollState(bool scrollState) override; 216 void OnRootLayerChanged(int width, int height) override; 217 bool FilterScrollEvent(const float x, const float y, const float xVelocity, const float yVelocity) override; 218 void OnNativeEmbedLifecycleChange(const NWeb::NativeEmbedDataInfo& dataInfo) override; 219 void OnNativeEmbedGestureEvent(const NWeb::NativeEmbedTouchEvent& event) override; 220 SetWebDelegate(const WeakPtr<WebDelegate> & delegate)221 void SetWebDelegate(const WeakPtr<WebDelegate>& delegate) 222 { 223 webDelegate_ = delegate; 224 } 225 GetWebDelegate()226 const RefPtr<WebDelegate> GetWebDelegate() const 227 { 228 return webDelegate_.Upgrade(); 229 } 230 231 private: 232 std::weak_ptr<OHOS::NWeb::NWeb> webviewWeak_; 233 WeakPtr<WebDelegate> webDelegate_; 234 int32_t instanceId_ = -1; 235 }; 236 } // namespace OHOS::Ace 237 238 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H 239