1 /*
2  * Copyright (c) 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_WEB_PATTERN_WEB_DELEGATE_CROSS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_WEB_PATTERN_WEB_DELEGATE_CROSS_H
18 
19 #include "web_object_event.h"
20 
21 #include "base/log/log.h"
22 #include "core/common/container.h"
23 #include "core/common/recorder/event_recorder.h"
24 #include "core/components_ng/pattern/web/cross_platform/web_pattern.h"
25 #include "core/components_ng/pattern/web/cross_platform/web_resource.h"
26 #include "core/components_ng/pattern/web/web_delegate_interface.h"
27 #include "core/pipeline/pipeline_base.h"
28 
29 namespace OHOS::Ace {
30 class WebResourceRequsetImpl : public AceType {
31     DECLARE_ACE_TYPE(WebResourceRequsetImpl, AceType);
32 public:
WebResourceRequsetImpl(void * object)33     explicit WebResourceRequsetImpl(void* object) : object_(object) {}
34     std::map<std::string, std::string> GetRequestHeader() const;
35     std::string GetRequestUrl() const;
36     std::string GetMethod() const;
37     bool IsRequestGesture() const;
38     bool IsMainFrame() const;
39     bool IsRedirect() const;
40 private:
41     void* object_ = nullptr;
42 };
43 
44 class WebOffsetImpl : public AceType {
45     DECLARE_ACE_TYPE(WebOffsetImpl, AceType);
46 public:
WebOffsetImpl(void * object)47     explicit WebOffsetImpl(void* object) : object_(object) {}
48     double GetX() const;
49     double GetY() const;
50 private:
51     void* object_ = nullptr;
52 };
53 
54 class WebScaleChangeImpl : public AceType {
55     DECLARE_ACE_TYPE(WebScaleChangeImpl, AceType);
56 public:
WebScaleChangeImpl(void * object)57     explicit WebScaleChangeImpl(void* object) : object_(object) {}
58     float GetNewScale() const;
59     float GetOldScale() const;
60 private:
61     void* object_ = nullptr;
62 };
63 
64 class WebResourceResponseImpl : public AceType {
65     DECLARE_ACE_TYPE(WebResourceResponseImpl, AceType);
66 public:
WebResourceResponseImpl(void * object)67     explicit WebResourceResponseImpl(void* object) : object_(object) {}
68     std::map<std::string, std::string> GetResponseHeader() const;
69     std::string GetResponseData() const;
70     std::string GetEncoding() const;
71     std::string GetMimeType() const;
72     std::string GetReason() const;
73     int GetStatusCode() const;
74 private:
75     void* object_ = nullptr;
76 };
77 
78 class WebConsoleMessage : public AceType {
79     DECLARE_ACE_TYPE(WebConsoleMessage, AceType);
80 public:
WebConsoleMessage(void * object)81     explicit WebConsoleMessage(void* object) : object_(object) {}
82     std::string GetMessage() const;
83     int GetMessageLevel() const;
84     std::string GetSourceId() const;
85     int GetLineNumber() const;
86 private:
87     void* object_ = nullptr;
88 };
89 
90 class DialogResult : public Result {
91     DECLARE_ACE_TYPE(DialogResult, Result);
92 public:
DialogResult(void * object,DialogEventType dialogEventType)93     DialogResult(void* object, DialogEventType dialogEventType) : object_(object), dialogEventType_(dialogEventType)
94     {
95         auto obj = WebObjectEventManager::GetInstance().GetCommonDialogObject();
96         index_ = obj->AddObject(object);
97     }
~DialogResult()98     ~DialogResult()
99     {
100         auto obj = WebObjectEventManager::GetInstance().GetCommonDialogObject();
101         obj->DelObject(index_);
102     }
103     void Confirm(const std::string& promptResult) override;
104     void Confirm() override;
105     void Cancel() override;
106 
107 private:
108     void* object_ = nullptr;
109     DialogEventType dialogEventType_;
110     int index_;
111 };
112 
113 class WebAuthResult : public AuthResult {
114     DECLARE_ACE_TYPE(WebAuthResult, AuthResult);
115 public:
WebAuthResult(void * object)116     explicit WebAuthResult(void* object) : object_(object)
117     {
118         auto obj = WebObjectEventManager::GetInstance().GetHttpAuthRequestObject();
119         index_ = obj->AddObject(object);
120     }
~WebAuthResult()121     ~WebAuthResult()
122     {
123         auto obj = WebObjectEventManager::GetInstance().GetHttpAuthRequestObject();
124         obj->DelObject(index_);
125     }
126     bool Confirm(std::string& userName, std::string& pwd) override;
127     bool IsHttpAuthInfoSaved() override;
128     void Cancel() override;
129 
130 private:
131     void* object_ = nullptr;
132     int index_;
133 };
134 
135 class WebCommonDialogImpl : public AceType {
136     DECLARE_ACE_TYPE(WebCommonDialogImpl, AceType);
137 public:
WebCommonDialogImpl(void * object)138     explicit WebCommonDialogImpl(void* object) : object_(object) {}
139     std::string GetUrl() const;
140     std::string GetMessage() const;
141     std::string GetValue() const;
142 
143 private:
144     void* object_ = nullptr;
145 };
146 
147 class PermissionRequestImpl : public WebPermissionRequest {
148     DECLARE_ACE_TYPE(PermissionRequestImpl, WebPermissionRequest);
149 public:
PermissionRequestImpl(void * object)150     explicit PermissionRequestImpl(void* object) : object_(object)
151     {
152         auto obj = WebObjectEventManager::GetInstance().GetPermissionRequestObject();
153         index_ = obj->AddObject(object);
154     }
~PermissionRequestImpl()155     ~PermissionRequestImpl()
156     {
157         auto obj = WebObjectEventManager::GetInstance().GetPermissionRequestObject();
158         obj->DelObject(index_);
159     }
160 
161     void Deny() const override;
162     std::string GetOrigin() const override;
163     std::vector<std::string> GetResources() const override;
164     void Grant(std::vector<std::string>& resources) const override;
165     void SetOrigin();
166     void SetResources();
167 
168 private:
169     void* object_ = nullptr;
170     int index_;
171     std::string origin_;
172     std::vector<std::string> resources_;
173 };
174 
175 class WebFileSelectorResult : public FileSelectorResult {
176     DECLARE_ACE_TYPE(WebFileSelectorResult, FileSelectorResult);
177 public:
WebFileSelectorResult(void * object)178     explicit WebFileSelectorResult(void* object) : object_(object)
179     {
180         auto obj = WebObjectEventManager::GetInstance().GetFileChooserObject();
181         index_ = obj->AddObject(object);
182     }
~WebFileSelectorResult()183     ~WebFileSelectorResult()
184     {
185         auto obj = WebObjectEventManager::GetInstance().GetFileChooserObject();
186         obj->DelObject(index_);
187     }
188 
189     void HandleFileList(std::vector<std::string>& fileList) override;
190 
191 private:
192     void* object_;
193     int index_;
194 };
195 
196 class WebFileChooserImpl : public AceType {
197     DECLARE_ACE_TYPE(WebFileChooserImpl, AceType);
198 public:
WebFileChooserImpl(void * object)199     explicit WebFileChooserImpl(void* object) : object_(object) {}
200     std::string GetTitle() const;
201     int GetMode() const;
202     std::string GetDefaultFileName() const;
203     std::vector<std::string> GetAcceptType() const;
204     bool IsCapture() const;
205 
206 private:
207     void* object_ = nullptr;
208 };
209 
210 class FileSelectorParam : public WebFileSelectorParam {
211     DECLARE_ACE_TYPE(FileSelectorParam, AceType);
212 public:
FileSelectorParam(std::string title,int mode,std::string defaultFileName,std::vector<std::string> acceptType,bool isCapture)213     FileSelectorParam(
214         std::string title, int mode, std::string defaultFileName, std::vector<std::string> acceptType, bool isCapture)
215         : title_(title), mode_(mode), defaultFileName_(defaultFileName), acceptType_(acceptType), isCapture_(isCapture)
216     {}
217     ~FileSelectorParam() = default;
218 
GetTitle()219     std::string GetTitle() override
220     {
221         return title_;
222     }
GetMode()223     int GetMode() override
224     {
225         return mode_;
226     }
GetDefaultFileName()227     std::string GetDefaultFileName() override
228     {
229         return defaultFileName_;
230     }
GetAcceptType()231     std::vector<std::string> GetAcceptType() override
232     {
233         return acceptType_;
234     }
IsCapture()235     bool IsCapture() override
236     {
237         return isCapture_;
238     }
239 
240 private:
241     std::string title_;
242     int mode_;
243     std::string defaultFileName_;
244     std::vector<std::string> acceptType_;
245     bool isCapture_;
246 };
247 
248 class Geolocation : public WebGeolocation {
249     DECLARE_ACE_TYPE(Geolocation, WebGeolocation);
250 public:
Geolocation(void * object)251     explicit Geolocation(void* object) : object_(object)
252     {
253         auto obj = WebObjectEventManager::GetInstance().GetGeolocationObject();
254         index_ = obj->AddObject(object);
255     }
~Geolocation()256     ~Geolocation()
257     {
258         auto obj = WebObjectEventManager::GetInstance().GetGeolocationObject();
259         obj->DelObject(index_);
260     }
261     void Invoke(const std::string& origin, const bool& allow, const bool& retain) override;
262 
263 private:
264     void* object_ = nullptr;
265     int index_;
266 };
267 
268 class WebAuthRequestImpl : public AceType {
269     DECLARE_ACE_TYPE(WebAuthRequestImpl, AceType);
270 public:
WebAuthRequestImpl(void * object)271     explicit WebAuthRequestImpl(void* object) : object_(object) {}
272     std::string GetHost() const;
273     std::string GetRealm() const;
274 
275 private:
276     void* object_ = nullptr;
277 };
278 
279 class WebGeolocationImpl : public AceType {
280     DECLARE_ACE_TYPE(WebGeolocationImpl, AceType);
281 
282 public:
WebGeolocationImpl(void * object)283     explicit WebGeolocationImpl(void* object) : object_(object) {}
284     std::string GetOrigin() const;
285 private:
286     void* object_ = nullptr;
287 };
288 
289 class WebDownloadResponseImpl : public AceType {
290     DECLARE_ACE_TYPE(WebDownloadResponseImpl, AceType);
291 public:
WebDownloadResponseImpl(void * object)292     explicit WebDownloadResponseImpl(void* object) : object_(object) {}
293     std::string GetUrl() const;
294     std::string GetMimetype() const;
295     long GetContentLength() const;
296     std::string GetUserAgent() const;
297     std::string GetContentDisposition() const;
298 
299 private:
300     void* object_ = nullptr;
301 };
302 
303 class WebResourceErrorImpl : public AceType {
304     DECLARE_ACE_TYPE(WebResourceErrorImpl, AceType);
305 public:
WebResourceErrorImpl(void * object)306     explicit WebResourceErrorImpl(void* object) : object_(object) {}
307     std::string GetErrorInfo() const;
308     int GetErrorCode() const;
309 private:
310     void* object_ = nullptr;
311 };
312 
313 class WebDelegateCross : public WebDelegateInterface,
314                          public WebResource {
315     DECLARE_ACE_TYPE(WebDelegateCross, WebResource);
316 public:
317     using CreatedCallback = std::function<void()>;
318     using ReleasedCallback = std::function<void(bool)>;
319     using EventCallback = std::function<void(const std::shared_ptr<BaseEventInfo>&)>;
320     enum class State : char {
321         WAITINGFORSIZE,
322         CREATING,
323         CREATED,
324         CREATEFAILED,
325         RELEASED,
326     };
327 
328     WebDelegateCross() = delete;
329     ~WebDelegateCross() override;
WebDelegateCross(const WeakPtr<PipelineBase> & context,ErrorCallback && onError,const std::string & type)330     WebDelegateCross(const WeakPtr<PipelineBase>& context, ErrorCallback&& onError, const std::string& type)
331         : WebResource(type, context, std::move(onError)), context_(context), instanceId_(Container::CurrentId())
332     {}
333 
334     void CreatePlatformResource(const Size& size, const Offset& position,
335         const WeakPtr<NG::PipelineContext>& context) override;
SetNGWebPattern(const RefPtr<NG::WebPattern> & webPattern)336     void SetNGWebPattern(const RefPtr<NG::WebPattern>& webPattern) override
337     {
338         webPattern_ = webPattern;
339     }
340     int GetWebId() override;
341     void HandleTouchDown(const int32_t& id, const double& x, const double& y, bool from_overlay) override;
342     void HandleTouchUp(const int32_t& id, const double& x, const double& y, bool from_overlay) override;
343     void HandleTouchMove(const int32_t& id, const double& x, const double& y, bool from_overlay) override;
344     void HandleTouchCancel() override;
345     void HandleAxisEvent(const double& x, const double& y, const double& deltaX, const double& deltaY);
346     bool OnKeyEvent(int32_t keyCode, int32_t keyAction) override;
347     void OnMouseEvent(int32_t x, int32_t y, const MouseButton button, const MouseAction action, int count) override;
348     void OnFocus() override;
349     void OnBlur() override;
350     void UpdateLocale() override;
351     void SetDrawRect(int32_t x, int32_t y, int32_t width, int32_t height) override;
352     void OnInactive() override;
353     void OnActive() override;
354     void ShowWebView() override;
355     void HideWebView() override;
356     void OnPageStarted(const std::string& param) override;
357     void OnPageFinished(const std::string& param) override;
358     void OnPageError(const std::string& param) override;
359     void OnProgressChanged(const std::string& param) override;
360     void OnReceivedTitle(const std::string& param) override;
361     void OnPageVisible(const std::string& param) override;
362     void OnGeolocationPermissionsHidePrompt() override;
363 
364     void UpdateUserAgent(const std::string& userAgent) override;
365     void UpdateBackgroundColor(const int backgroundColor) override;
366     void UpdateInitialScale(float scale) override;
367     void UpdateJavaScriptEnabled(const bool& isJsEnabled) override;
368     void UpdateAllowFileAccess(const bool& isFileAccessEnabled) override;
369     void UpdateBlockNetworkImage(const bool& onLineImageAccessEnabled) override;
370     void UpdateLoadsImagesAutomatically(const bool& isImageAccessEnabled) override;
371     void UpdateMixedContentMode(const MixedModeContent& mixedMode) override;
372     void UpdateSupportZoom(const bool& isZoomAccessEnabled) override;
373     void UpdateDomStorageEnabled(const bool& isDomStorageAccessEnabled) override;
374     void UpdateGeolocationEnabled(const bool& isGeolocationAccessEnabled) override;
375     void UpdateCacheMode(const WebCacheMode& mode) override;
376     void UpdateDarkMode(const WebDarkMode& mode) override;
377     void UpdateForceDarkAccess(const bool& access) override;
378     void UpdateAudioResumeInterval(const int32_t& resumeInterval) override;
379     void UpdateAudioExclusive(const bool& audioExclusive) override;
380     void UpdateOverviewModeEnabled(const bool& isOverviewModeAccessEnabled) override;
381     void UpdateFileFromUrlEnabled(const bool& isFileFromUrlAccessEnabled) override;
382     void UpdateDatabaseEnabled(const bool& isDatabaseAccessEnabled) override;
383     void UpdateTextZoomRatio(const int32_t& textZoomRatioNum) override;
384     void UpdateWebDebuggingAccess(bool isWebDebuggingAccessEnabled) override;
385     void UpdatePinchSmoothModeEnabled(bool isPinchSmoothModeEnabled) override;
386     void UpdateMediaPlayGestureAccess(bool isNeedGestureAccess) override;
387     void UpdateMultiWindowAccess(bool isMultiWindowAccessEnabled) override;
388     void UpdateAllowWindowOpenMethod(bool isAllowWindowOpenMethod) override;
389     void UpdateWebCursiveFont(const std::string& cursiveFontFamily) override;
390     void UpdateWebFantasyFont(const std::string& fantasyFontFamily) override;
391     void UpdateWebFixedFont(const std::string& fixedFontFamily) override;
392     void UpdateWebSansSerifFont(const std::string& sansSerifFontFamily) override;
393     void UpdateWebSerifFont(const std::string& serifFontFamily) override;
394     void UpdateWebStandardFont(const std::string& standardFontFamily) override;
395     void UpdateDefaultFixedFontSize(int32_t size) override;
396     void UpdateDefaultFontSize(int32_t defaultFontSize) override;
397     void UpdateMinFontSize(int32_t minFontSize) override;
398     void UpdateMinLogicalFontSize(int32_t minLogicalFontSize) override;
399     void UpdateBlockNetwork(bool isNetworkBlocked) override;
400     void UpdateHorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled) override;
401     void UpdateVerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled) override;
402     void UpdateScrollBarColor(const std::string& colorValue) override;
403     void LoadUrl() override;
404     void SetBackgroundColor(int32_t backgroundColor) override;
405 
406     bool LoadDataWithRichText() override;
407 
408     void SetBoundsOrResize(const Size& drawSize, const Offset& offset) override;
409 private:
410     void ReleasePlatformResource();
411     void CreatePluginResource(const Size& size, const Offset& position, const WeakPtr<NG::PipelineContext>& context);
412     void InitWebEvent();
413     void RegisterWebEvent();
414     void RegisterWebObjectEvent();
415     void OnErrorReceive(void* object);
416     void OnScroll(void* object);
417     void OnScaleChange(void* object);
418     void OnHttpErrorReceive(void* object);
419     bool OnConsoleMessage(void* object);
420     bool OnLoadIntercept(void* object);
421     bool OnCommonDialog(void* object, DialogEventType dialogEventType);
422     bool OnPermissionRequest(void* object);
423     bool OnHttpAuthRequest(void* object);
424     void OnDownloadStart(void* object);
425     bool OnShowFileChooser(void* object);
426     void OnGeolocationPermissionsShowPrompt(void* object);
427     void RecordWebEvent(Recorder::EventType eventType, const std::string& param) const;
428 
429     WeakPtr<NG::WebPattern> webPattern_;
430     WeakPtr<PipelineBase> context_;
431 
432     State state_ { State::WAITINGFORSIZE };
433 
434     Method reloadMethod_;
435     Method updateUrlMethod_;
436     Method routerBackMethod_;
437     Method changePageUrlMethod_;
438     Method isPagePathInvalidMethod_;
439     Method backwardMethod_;
440     Method forwardMethod_;
441     Method clearHistoryMethod_;
442     Method getHitTestMethod_;
443     Method onActiveMethod_;
444     Method onInactiveMethod_;
445     Method refreshMethod_;
446     Method stopLoadingMethod_;
447     Method requestFocusMethod_;
448     Method accessBackwardMethod_;
449     Method accessForwardMethod_;
450     Method accessStepMethod_;
451     Method setWebViewJavaScriptResultCallBackMethod_;
452     Method registerJavaScriptProxyMethod_;
453     Method runTypeScriptMethod_;
454     Method deleteJavaScriptInterfaceMethod_;
455     Method loadUrlMethod_;
456     Method loadDataMethod_;
457     Method updateAttributeMethod_;
458     Method saveCookieSyncMethod_;
459     Method setCookieMethod_;
460     Method touchDownMethod_;
461     Method touchUpMethod_;
462     Method touchMoveMethod_;
463     Method touchCancelMethod_;
464     Method updateLayoutMethod_;
465     Method zoomMethod_;
466 
467     Method updateZoomAccess_;
468     Method updateJavaScriptEnabled_;
469     Method updateMinFontSize_;
470     Method updateHorizontalScrollBarAccess_;
471     Method updateVerticalScrollBarAccess_;
472     Method updateBackgroundColor_;
473     Method updateMediaPlayGestureAccess_;
474 
475     EventCallbackV2 onPageFinishedV2_;
476     EventCallbackV2 onPageStartedV2_;
477     EventCallbackV2 onProgressChangeV2_;
478 
479     int instanceId_ = -1;
480 };
481 } // namespace OHOS::Ace
482 
483 #endif
484