• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_WEB_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_DELEGATE_H
18 
19 #include <list>
20 #include <map>
21 
22 #ifdef OHOS_STANDARD_SYSTEM
23 #include <ui/rs_surface_node.h>
24 #endif
25 
26 #include "core/components/common/layout/constants.h"
27 #include "core/components/web/resource/web_client_impl.h"
28 #include "core/components/web/resource/web_resource.h"
29 #include "core/components/web/web_component.h"
30 #include "core/components/web/web_event.h"
31 #ifdef OHOS_STANDARD_SYSTEM
32 #include "nweb_helper.h"
33 #include "window.h"
34 #endif
35 
36 namespace OHOS::Ace {
37 
38 class ConsoleLogOhos : public WebConsoleLog {
DECLARE_ACE_TYPE(ConsoleLogOhos,WebConsoleLog)39     DECLARE_ACE_TYPE(ConsoleLogOhos, WebConsoleLog)
40 
41 public:
42     ConsoleLogOhos(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message) : message_(message) {}
43 
44     int GetLineNumber() override;
45 
46     std::string GetLog() override;
47 
48     int GetLogLevel() override;
49 
50     std::string GetSourceId() override;
51 
52 private:
53     std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message_;
54 };
55 
56 class ResultOhos : public Result {
DECLARE_ACE_TYPE(ResultOhos,Result)57     DECLARE_ACE_TYPE(ResultOhos, Result)
58 
59 public:
60     ResultOhos(std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) : result_(result) {}
61 
62     void Confirm() override;
63     void Confirm(const std::string &message) override;
64     void Cancel() override;
65 
66 private:
67     std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result_;
68 };
69 
70 class FileSelectorParamOhos : public WebFileSelectorParam {
DECLARE_ACE_TYPE(FileSelectorParamOhos,WebFileSelectorParam)71     DECLARE_ACE_TYPE(FileSelectorParamOhos, WebFileSelectorParam)
72 
73 public:
74     explicit FileSelectorParamOhos(std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> param)
75         : param_(param) {}
76 
77     std::string GetTitle() override;
78     int GetMode() override;
79     std::string GetDefaultFileName() override;
80     std::vector<std::string> GetAcceptType() override;
81     bool IsCapture() override;
82 private:
83     std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> param_;
84 };
85 
86 class FileSelectorResultOhos : public FileSelectorResult {
DECLARE_ACE_TYPE(FileSelectorResultOhos,FileSelectorResult)87     DECLARE_ACE_TYPE(FileSelectorResultOhos, FileSelectorResult)
88 
89 public:
90     explicit FileSelectorResultOhos(std::shared_ptr<OHOS::NWeb::FileSelectorCallback> callback)
91         : callback_(callback) {}
92 
93     void HandleFileList(std::vector<std::string>& result) override;
94 private:
95     std::shared_ptr<OHOS::NWeb::FileSelectorCallback> callback_;
96 };
97 
98 class WebGeolocationOhos : public WebGeolocation {
DECLARE_ACE_TYPE(WebGeolocationOhos,WebGeolocation)99     DECLARE_ACE_TYPE(WebGeolocationOhos, WebGeolocation)
100 
101 public:
102     WebGeolocationOhos(OHOS::NWeb::NWebGeolocationCallbackInterface* callback) : geolocationCallback_(callback) {}
103 
104     void Invoke(const std::string& origin, const bool& allow, const bool& retain) override;
105 private:
106     std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> geolocationCallback_;
107 };
108 
109 class WebDelegate : public WebResource {
110     DECLARE_ACE_TYPE(WebDelegate, WebResource);
111 
112 public:
113     using CreatedCallback = std::function<void()>;
114     using ReleasedCallback = std::function<void(bool)>;
115     using EventCallback = std::function<void(const std::string&)>;
116     using EventCallbackV2 = std::function<void(const std::shared_ptr<BaseEventInfo>&)>;
117     enum class State: char {
118         WAITINGFORSIZE,
119         CREATING,
120         CREATED,
121         CREATEFAILED,
122         RELEASED,
123     };
124 
125     WebDelegate() = delete;
126     ~WebDelegate() override;
WebDelegate(const WeakPtr<PipelineContext> & context,ErrorCallback && onError,const std::string & type)127     WebDelegate(const WeakPtr<PipelineContext>& context, ErrorCallback&& onError, const std::string& type)
128         : WebResource(type, context, std::move(onError)), state_(State::WAITINGFORSIZE)
129     {
130         ACE_DCHECK(!type.empty());
131     }
132 
133     void CreatePlatformResource(const Size& size, const Offset& position,
134         const WeakPtr<PipelineContext>& context);
135     void CreatePluginResource(const Size& size, const Offset& position,
136         const WeakPtr<PipelineContext>& context);
137     void AddCreatedCallback(const CreatedCallback& createdCallback);
138     void RemoveCreatedCallback();
139     void AddReleasedCallback(const ReleasedCallback& releasedCallback);
140     void SetComponent(const RefPtr<WebComponent>& component);
141     void RemoveReleasedCallback();
142     void Reload();
143     void UpdateUrl(const std::string& url);
144 #ifdef OHOS_STANDARD_SYSTEM
145     void InitOHOSWeb(const WeakPtr<PipelineContext>& context, sptr<Surface> surface = nullptr);
146     void InitWebViewWithWindow();
147     void ShowWebView();
148     void HideWebView();
149     void Resize(const double& width, const double& height);
150     void UpdateUserAgent(const std::string& userAgent);
151     void UpdateJavaScriptEnabled(const bool& isJsEnabled);
152     void UpdateAllowFileAccess(const bool& isFileAccessEnabled);
153     void UpdateBlockNetworkImage(const bool& onLineImageAccessEnabled);
154     void UpdateLoadsImagesAutomatically(const bool& isImageAccessEnabled);
155     void UpdateMixedContentMode(const MixedModeContent& mixedMode);
156     void UpdateSupportZoom(const bool& isZoomAccessEnabled);
157     void UpdateDomStorageEnabled(const bool& isDomStorageAccessEnabled);
158     void UpdateGeolocationEnabled(const bool& isGeolocationAccessEnabled);
159     void UpdateCacheMode(const WebCacheMode& mode);
160     void UpdateOverviewModeEnabled(const bool& isOverviewModeAccessEnabled);
161     void UpdateFileFromUrlEnabled(const bool& isFileFromUrlAccessEnabled);
162     void UpdateDatabaseEnabled(const bool& isDatabaseAccessEnabled);
163     void UpdateTextZoomAtio(const int32_t& textZoomAtioNum);
164     void UpdateWebDebuggingAccess(bool isWebDebuggingAccessEnabled);
165     void LoadUrl();
166     void HandleTouchDown(const int32_t& id, const double& x, const double& y);
167     void HandleTouchUp(const int32_t& id, const double& x, const double& y);
168     void HandleTouchMove(const int32_t& id, const double& x, const double& y);
169     void HandleTouchCancel();
170 #endif
171     void OnErrorReceive(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
172         std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error);
173     void OnHttpErrorReceive(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
174         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response);
175     void OnPageStarted(const std::string& param);
176     void OnPageFinished(const std::string& param);
177     void OnProgressChanged(int param);
178     void OnReceivedTitle(const std::string& param);
179     void OnGeolocationPermissionsHidePrompt();
180     void OnGeolocationPermissionsShowPrompt(const std::string& origin,
181         OHOS::NWeb::NWebGeolocationCallbackInterface* callback);
182     void OnRequestFocus();
183     bool OnCommonDialog(const BaseEventInfo* info, DialogEventType dialogEventType);
184     void OnDownloadStart(const std::string& url, const std::string& userAgent, const std::string& contentDisposition,
185         const std::string& mimetype, long contentLength);
186     void OnPageError(const std::string& param);
187     void OnMessage(const std::string& param);
188     bool OnConsoleLog(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> message);
189     void OnRouterPush(const std::string& param);
190     void OnRenderExited(OHOS::NWeb::RenderExitReason reason);
191     void OnRefreshAccessedHistory(const std::string& url, bool isRefreshed);
192     bool OnFileSelectorShow(const BaseEventInfo* info);
193     bool OnHandleInterceptUrlLoading(const std::string& url);
194 private:
195     void InitWebEvent();
196     void RegisterWebEvent();
197     void ReleasePlatformResource();
198     void Stop();
199     void UnregisterEvent();
200     std::string GetUrlStringParam(const std::string& param, const std::string& name) const;
201     void CallWebRouterBack();
202     void CallPopPageSuccessPageUrl(const std::string& url);
203     void CallIsPagePathInvalid(const bool& isPageInvalid);
204 
205     void BindRouterBackMethod();
206     void BindPopPageSuccessMethod();
207     void BindIsPagePathInvalidMethod();
208 
209 #ifdef OHOS_STANDARD_SYSTEM
210     sptr<OHOS::Rosen::Window> CreateWindow();
211     void LoadUrl(const std::string& url, const std::map<std::string, std::string>& httpHeaders);
212     void ExecuteTypeScript(const std::string& jscode, const std::function<void(std::string)>&& callback);
213     void LoadDataWithBaseUrl(const std::string& baseUrl, const std::string& data, const std::string& mimeType,
214         const std::string& encoding, const std::string& historyUrl);
215     void LoadDataWithRichText(const std::string& data);
216     void Refresh();
217     void StopLoading();
218     void AddJavascriptInterface(const std::string& objectName, const std::vector<std::string>& methodList);
219     void RemoveJavascriptInterface(const std::string& objectName, const std::vector<std::string>& methodList);
220     void SetWebViewJavaScriptResultCallBack(const WebController::JavaScriptCallBackImpl&& javaScriptCallBackImpl);
221     void RequestFocus();
222     void OnFocus();
223     void OnInactive();
224     void OnActive();
225     void Zoom(float factor);
226     int GetHitTestResult();
227     bool SaveCookieSync();
228     bool SetCookie(const std::string& url, const std::string& value);
229     std::string GetCookie(const std::string& url) const;
230     void DeleteEntirelyCookie();
231     void RegisterOHOSWebEventAndMethord();
232     void SetWebCallBack();
233 
234     // Backward and forward
235     void Backward();
236     void Forward();
237     void ClearHistory();
238     bool AccessStep(int32_t step);
239     bool AccessBackward();
240     bool AccessForward();
241 #if defined(ENABLE_ROSEN_BACKEND)
242     void InitWebViewWithSurface(sptr<Surface> surface);
243 #endif
244 #endif
245 
246     RefPtr<WebComponent> webComponent_;
247     std::list<CreatedCallback> createdCallbacks_;
248     std::list<ReleasedCallback> releasedCallbacks_;
249     EventCallback onPageStarted_;
250     EventCallback onPageFinished_;
251     EventCallback onPageError_;
252     EventCallback onMessage_;
253     Method reloadMethod_;
254     Method updateUrlMethod_;
255     Method routerBackMethod_;
256     Method changePageUrlMethod_;
257     Method isPagePathInvalidMethod_;
258     State state_ {State::WAITINGFORSIZE};
259 #ifdef OHOS_STANDARD_SYSTEM
260     std::shared_ptr<OHOS::NWeb::NWeb> nweb_;
261     OHOS::NWeb::NWebCookieManager* cookieManager_ = nullptr;
262     sptr<Rosen::Window> window_;
263     bool isCreateWebView_ = false;
264 
265     EventCallbackV2 onPageFinishedV2_;
266     EventCallbackV2 onPageStartedV2_;
267     EventCallbackV2 onProgressChangeV2_;
268     EventCallbackV2 onTitleReceiveV2_;
269     EventCallbackV2 onGeolocationHideV2_;
270     EventCallbackV2 onGeolocationShowV2_;
271     EventCallbackV2 onRequestFocusV2_;
272     EventCallbackV2 onErrorReceiveV2_;
273     EventCallbackV2 onHttpErrorReceiveV2_;
274     EventCallbackV2 onDownloadStartV2_;
275     EventCallbackV2 onFocusV2_;
276     EventCallbackV2 onRefreshAccessedHistoryV2_;
277     EventCallbackV2 onRenderExitedV2_;
278 
279     std::string bundlePath_;
280     std::string bundleDataPath_;
281 
282 #endif
283 };
284 
285 } // namespace OHOS::Ace
286 
287 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_DELEGATE_H
288