• 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_handler.h"
21 
22 #include "base/log/log.h"
23 #include "core/common/container_scope.h"
24 #include "surface_delegate.h"
25 #ifdef ENABLE_ROSEN_BACKEND
26 #include "surface.h"
27 #endif
28 
29 namespace OHOS::Ace {
30 class WebDelegate;
31 
32 class DownloadListenerImpl : public OHOS::NWeb::NWebDownloadCallback {
33 public:
34     DownloadListenerImpl() = default;
DownloadListenerImpl(int32_t instanceId)35     explicit DownloadListenerImpl(int32_t instanceId) : instanceId_(instanceId) {}
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     int32_t instanceId_ = -1;
49 };
50 
51 class ReleaseSurfaceImpl : public OHOS::NWeb::NWebReleaseSurfaceCallback {
52 public:
53     ReleaseSurfaceImpl() = default;
ReleaseSurfaceImpl(int32_t instanceId)54     explicit ReleaseSurfaceImpl(int32_t instanceId) : instanceId_(instanceId) {}
55     ~ReleaseSurfaceImpl() = default;
56 
57     void ReleaseSurface() override;
58 
SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> & surfaceDelegate)59     void SetSurfaceDelegate(const sptr<OHOS::SurfaceDelegate> &surfaceDelegate)
60     {
61         surfaceDelegate_ = surfaceDelegate;
62     }
63 
64 private:
65     sptr<OHOS::SurfaceDelegate> surfaceDelegate_ = nullptr;
66     int32_t instanceId_ = -1;
67 };
68 class FindListenerImpl : public OHOS::NWeb::NWebFindCallback {
69 public:
70     FindListenerImpl() = default;
FindListenerImpl(int32_t instanceId)71     explicit FindListenerImpl(int32_t instanceId) : instanceId_(instanceId) {}
72     ~FindListenerImpl() = default;
73 
74     void OnFindResultReceived(
75         const int activeMatchOrdinal, const int numberOfMatches, const bool isDoneCounting) override;
76 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)77     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
78     {
79         webDelegate_ = delegate;
80     }
81 
82 private:
83     WeakPtr<WebDelegate> webDelegate_;
84     int32_t instanceId_ = -1;
85 };
86 
87 class WebClientImpl : public OHOS::NWeb::NWebHandler {
88 public:
89     WebClientImpl() = default;
WebClientImpl(int32_t instanceId)90     explicit WebClientImpl(int32_t instanceId) : instanceId_(instanceId) {}
91     ~WebClientImpl() = default;
92 
93     void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override;
94     void OnProxyDied() override;
95     void OnRouterPush(const std::string& param) override;
96     bool OnConsoleLog(const OHOS::NWeb::NWebConsoleLog& message) override;
97     void OnMessage(const std::string& param) override;
98     void OnPageLoadBegin(const std::string& url) override;
99     void OnPageLoadEnd(int httpStatusCode, const std::string& url) override;
100     void OnLoadingProgress(int newProgress) override;
101     void OnPageTitle(const std::string &title) override;
102     void OnGeolocationHide() override;
103     void OnFullScreenExit() override;
104     void OnFullScreenEnter(std::shared_ptr<NWeb::NWebFullScreenExitHandler> handler) override;
105     void OnGeolocationShow(const std::string& origin,
106         std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback) override;
107 
108     bool OnAlertDialogByJS(const std::string &url,
109                            const std::string &message,
110                            std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
111     bool OnBeforeUnloadByJS(const std::string &url,
112                             const std::string &message,
113                             std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
114     bool OnConfirmDialogByJS(const std::string &url,
115                              const std::string &message,
116                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
117     bool OnPromptDialogByJS(const std::string &url,
118                              const std::string &message,
119                              const std::string &defaultValue,
120                              std::shared_ptr<NWeb::NWebJSDialogResult> result) override;
121     bool OnFileSelectorShow(std::shared_ptr<NWeb::FileSelectorCallback> callback,
122                             std::shared_ptr<NWeb::NWebFileSelectorParams> params) override;
123 
124     void OnFocus() override;
125     void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
126         std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override;
127     void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
128         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
129     void OnRenderExited(OHOS::NWeb::RenderExitReason reason) override;
130     void OnRefreshAccessedHistory(const std::string& url, bool isReload) override;
131     std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> OnHandleInterceptRequest(
132         std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
133     bool OnHandleInterceptUrlLoading(const std::string& url) override;
134     void OnResource(const std::string& url) override;
135     void OnScaleChanged(float oldScaleFactor, float newScaleFactor) override;
136     void OnScroll(double xOffset, double yOffset) override;
137     bool OnHttpAuthRequestByJS(std::shared_ptr<NWeb::NWebJSHttpAuthResult> result, const std::string &host,
138         const std::string &realm) override;
139     bool OnSslErrorRequestByJS(std::shared_ptr<NWeb::NWebJSSslErrorResult> result,
140         OHOS::NWeb::SslError error) override;
141     bool OnSslSelectCertRequestByJS(
142         std::shared_ptr<NWeb::NWebJSSslSelectCertResult> result,
143         const std::string& host,
144         int32_t port,
145         const std::vector<std::string>& keyTypes,
146         const std::vector<std::string>& issuers) override;
147     void OnPermissionRequest(std::shared_ptr<NWeb::NWebAccessRequest> request) override;
148     bool RunContextMenu(std::shared_ptr<NWeb::NWebContextMenuParams> params,
149         std::shared_ptr<NWeb::NWebContextMenuCallback> callback) override;
150     bool RunQuickMenu(std::shared_ptr<NWeb::NWebQuickMenuParams> params,
151                       std::shared_ptr<NWeb::NWebQuickMenuCallback> callback) override;
152     void OnQuickMenuDismissed() override;
153     void OnTouchSelectionChanged(
154         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
155         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
156         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle) override;
157     bool OnDragAndDropData(const void* data, size_t len, const NWeb::ImageOptions& opt) override;
158     void OnWindowNewByJS(
159         const std::string& targetUrl,
160         bool isAlert,
161         bool isUserTrigger,
162         std::shared_ptr<NWeb::NWebControllerHandler> handler) override;
163     void OnWindowExitByJS() override;
164     void OnPageVisible(const std::string& url) override;
165     void OnDataResubmission(std::shared_ptr<NWeb::NWebDataResubmissionCallback> handler) override;
166     void OnPageIcon(
167         const void* data,
168         size_t width,
169         size_t height,
170         NWeb::ImageColorType colorType,
171         NWeb::ImageAlphaType alphaType) override;
172     void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override;
173     bool OnCursorChange(const NWeb::CursorType& type, const NWeb::NWebCursorInfo& info) override;
174     void OnSelectPopupMenu(std::shared_ptr<NWeb::NWebSelectPopupMenuParam> params,
175                            std::shared_ptr<NWeb::NWebSelectPopupMenuCallback> callback) override;
176 
SetWebDelegate(const WeakPtr<WebDelegate> & delegate)177     void SetWebDelegate(const WeakPtr<WebDelegate>& delegate)
178     {
179         webDelegate_ = delegate;
180     }
181 
GetWebDelegate()182     const RefPtr<WebDelegate> GetWebDelegate() const
183     {
184         return webDelegate_.Upgrade();
185     }
186 
187 private:
188     std::weak_ptr<OHOS::NWeb::NWeb> webviewWeak_;
189     WeakPtr<WebDelegate> webDelegate_;
190     int32_t instanceId_ = -1;
191 };
192 } // namespace OHOS::Ace
193 
194 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H
195