• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 ReleaseSurfaceImpl : public OHOS::NWeb::NWebReleaseSurfaceCallback {
53 public:
54     ReleaseSurfaceImpl() = default;
ReleaseSurfaceImpl(int32_t instanceId)55     explicit ReleaseSurfaceImpl(int32_t instanceId) : instanceId_(instanceId) {}
56     ~ReleaseSurfaceImpl() = default;
57 
58     void ReleaseSurface() override;
59 
SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> & surfaceDelegate)60     void SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> &surfaceDelegate)
61     {
62         surfaceDelegate_ = surfaceDelegate;
63     }
64 
65 private:
66     sptr<OHOS::SurfaceDelegate> surfaceDelegate_ = nullptr;
67     int32_t instanceId_ = -1;
68 };
69 class FindListenerImpl : public OHOS::NWeb::NWebFindCallback {
70 public:
71     FindListenerImpl() = default;
FindListenerImpl(int32_t instanceId)72     explicit FindListenerImpl(int32_t instanceId) : instanceId_(instanceId) {}
73     ~FindListenerImpl() = default;
74 
75     void OnFindResultReceived(
76         const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting) override;
77 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)78     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
79     {
80         webDelegate_ = delegate;
81     }
82 
83 private:
84     WeakPtr<WebDelegate> webDelegate_;
85     int32_t instanceId_ = -1;
86 };
87 
88 class WebClientImpl : public OHOS::NWeb::NWebHandler {
89 public:
90     WebClientImpl() = default;
WebClientImpl(int32_t instanceId)91     explicit WebClientImpl(int32_t instanceId) : instanceId_(instanceId) {}
92     ~WebClientImpl() = default;
93 
94     void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override;
95     void OnProxyDied() override;
96     void OnRouterPush(const std::string& param) override;
97     bool OnConsoleLog(const OHOS::NWeb::NWebConsoleLog& message) override;
98     void OnMessage(const std::string& param) override;
99     void OnPageLoadBegin(const std::string& url) override;
100     void OnPageLoadEnd(int httpStatusCode, const std::string& url) override;
101     void OnLoadingProgress(int newProgress) override;
102     void OnPageTitle(const std::string &title) override;
103     void OnGeolocationHide() override;
104     void OnFullScreenExit() override;
105     void OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler) override;
106     void OnGeolocationShow(const std::string& origin,
107         std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback) override;
108 
109     bool OnAlertDialogByJS(const std::string &url,
110                            const std::string &message,
111                            std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
112     bool OnBeforeUnloadByJS(const std::string &url,
113                             const std::string &message,
114                             std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
115     bool OnConfirmDialogByJS(const std::string &url,
116                              const std::string &message,
117                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
118     bool OnPromptDialogByJS(const std::string &url,
119                              const std::string &message,
120                              const std::string &defaultValue,
121                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
122     bool OnFileSelectorShow(std::shared_ptr<NWeb::FileSelectorCallback> callback,
123                             std::shared_ptr<NWeb::NWebFileSelectorParams> params) override;
124 
125     void OnFocus() override;
126     void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
127         std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override;
128     void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
129         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
130     void OnRenderExited(OHOS::NWeb::RenderExitReason reason) override;
131     void OnRefreshAccessedHistory(const std::string& url, bool isReload) override;
132     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> OnHandleInterceptRequest(
133         std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
134     bool OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
135     void OnResource(const std::string& url) override;
136     void OnScaleChanged(float oldScaleFactor, float newScaleFactor) override;
137     void OnScroll(double xOffset, double yOffset) override;
138     bool OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host,
139         const std::string &realm) override;
140     bool OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
141         OHOS::NWeb::SslError error) override;
142     bool OnSslSelectCertRequestByJS(
143         std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,
144         const std::string& host,
145         int32_t port,
146         const std::vector<std::string>& keyTypes,
147         const std::vector<std::string>& issuers) override;
148     void OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request) override;
149     bool RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params,
150         std::shared_ptr<NWeb::NWebContextMenuCallback> callback) override;
151     bool RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,
152                       std::shared_ptr<NWeb::NWebQuickMenuCallback> callback) override;
153     void OnQuickMenuDismissed() override;
154     void OnTouchSelectionChanged(
155         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
156         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
157         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle) override;
158     bool OnDragAndDropData(const void* data, size_t len, const NWeb::ImageOptions& opt) override;
159     bool OnDragAndDropDataUdmf(std::shared_ptr<NWeb::NWebDragData> dragData) override;
160     void UpdateDragCursor(NWeb::NWebDragData::DragOperation op) override;
161     void OnWindowNewByJS(
162         const std::string& targetUrl,
163         bool isAlert,
164         bool isUserTrigger,
165         std::shared_ptr<NWeb::NWebControllerHandler> handler) override;
166     void OnWindowExitByJS() override;
167     void OnPageVisible(const std::string& url) override;
168     void OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler) override;
169     void OnPageIcon(
170         const void* data,
171         size_t width,
172         size_t height,
173         NWeb::ImageColorType colorType,
174         NWeb::ImageAlphaType alphaType) override;
175     void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override;
176     bool OnCursorChange(const NWeb::CursorType& type, const NWeb::NWebCursorInfo& info) override;
177     void OnSelectPopupMenu(std::shared_ptr<NWeb::NWebSelectPopupMenuParam> params,
178                            std::shared_ptr<NWeb::NWebSelectPopupMenuCallback> callback) override;
179     void OnAudioStateChanged(bool playing) override;
180     void OnFirstContentfulPaint(int64_t navigationStartTick, int64_t firstContentfulPaintMs) override;
181     void OnCompleteSwapWithNewSize() override;
182     void OnResizeNotWork() override;
183     void OnGetTouchHandleHotZone(NWeb::TouchHandleHotZone& hotZone) override;
184     void OnDateTimeChooserPopup(
185         const NWeb::DateTimeChooser& chooser,
186         const std::vector<NWeb::DateTimeSuggestion>& suggestions,
187         std::shared_ptr<NWeb::NWebDateTimeChooserCallback> callback) override;
188     void OnDateTimeChooserClose() override;
189     void OnOverScroll(float xOffset, float yOffset) override;
190     void OnScreenCaptureRequest(std::shared_ptr<NWeb::NWebScreenCaptureAccessRequest> request) override;
191 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)192     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
193     {
194         webDelegate_ = delegate;
195     }
196 
GetWebDelegate()197     const RefPtr<WebDelegate> GetWebDelegate() const
198     {
199         return webDelegate_.Upgrade();
200     }
201 
202 private:
203     std::weak_ptr<OHOS::NWeb::NWeb> webviewWeak_;
204     WeakPtr<WebDelegate> webDelegate_;
205     int32_t instanceId_ = -1;
206 };
207 } // namespace OHOS::Ace
208 
209 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H
210