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_NG_PATTERNS_WEB_WEB_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_WEB_EVENT_HUB_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/utils/noncopyable.h"
21 #include "core/components_ng/event/event_hub.h"
22 
23 #define ACE_WEB_EVENT_PROPERTY(name, type)                                                                     \
24 public:                                                                                                        \
25     void Set##name##Event(std::function<type(const std::shared_ptr<BaseEventInfo>& info)>&& prop##name##Event) \
26     {                                                                                                          \
27         prop##name##Event_ = std::move(prop##name##Event);                                                     \
28     }                                                                                                          \
29                                                                                                                \
30     const std::function<type(const std::shared_ptr<BaseEventInfo>& info)>& Get##name##Event() const            \
31     {                                                                                                          \
32         return prop##name##Event_;                                                                             \
33     }                                                                                                          \
34                                                                                                                \
35     void Fire##name##Event(const std::shared_ptr<BaseEventInfo>& info) const                                   \
36     {                                                                                                          \
37         if (prop##name##Event_) {                                                                              \
38             prop##name##Event_(info);                                                                          \
39         }                                                                                                      \
40     }                                                                                                          \
41                                                                                                                \
42 private:                                                                                                       \
43     std::function<type(const std::shared_ptr<BaseEventInfo>& info)> prop##name##Event_
44 
45 namespace OHOS::Ace::NG {
46 class WebEventHub : public EventHub {
47     DECLARE_ACE_TYPE(WebEventHub, EventHub)
48 
49 public:
50     WebEventHub() = default;
51     ~WebEventHub() override = default;
52 
SetOnCommonDialogEvent(std::function<bool (const std::shared_ptr<BaseEventInfo> & info)> && onCommonDialogImpl,DialogEventType dialogEventType)53     void SetOnCommonDialogEvent(std::function<bool(const std::shared_ptr<BaseEventInfo>& info)>&& onCommonDialogImpl,
54         DialogEventType dialogEventType)
55     {
56         switch (dialogEventType) {
57             case DialogEventType::DIALOG_EVENT_ALERT:
58                 onAlertImpl_ = std::move(onCommonDialogImpl);
59                 break;
60             case DialogEventType::DIALOG_EVENT_CONFIRM:
61                 onConfirmImpl_ = std::move(onCommonDialogImpl);
62                 break;
63             case DialogEventType::DIALOG_EVENT_PROMPT:
64                 onPromptImpl_ = std::move(onCommonDialogImpl);
65                 break;
66             case DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD:
67                 onBeforeUnloadImpl_ = std::move(onCommonDialogImpl);
68                 break;
69             default:
70                 break;
71         }
72     }
73 
FireOnCommonDialogEvent(const std::shared_ptr<BaseEventInfo> & info,DialogEventType dialogEventType)74     bool FireOnCommonDialogEvent(const std::shared_ptr<BaseEventInfo>& info, DialogEventType dialogEventType) const
75     {
76         if (dialogEventType == DialogEventType::DIALOG_EVENT_ALERT && onAlertImpl_) {
77             return onAlertImpl_(info);
78         }
79         if (dialogEventType == DialogEventType::DIALOG_EVENT_CONFIRM && onConfirmImpl_) {
80             return onConfirmImpl_(info);
81         }
82         if (dialogEventType == DialogEventType::DIALOG_EVENT_PROMPT && onPromptImpl_) {
83             return onPromptImpl_(info);
84         }
85         if (dialogEventType == DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD && onBeforeUnloadImpl_) {
86             return onBeforeUnloadImpl_(info);
87         }
88         return false;
89     }
90 
SetOnKeyEvent(std::function<void (KeyEventInfo & keyEventInfo)> && propOnKeyEvent)91     void SetOnKeyEvent(std::function<void(KeyEventInfo& keyEventInfo)>&& propOnKeyEvent)
92     {
93         propOnKeyEvent_ = propOnKeyEvent;
94     }
95 
GetOnKeyEvent()96     const std::function<void(KeyEventInfo& keyEventInfo)>& GetOnKeyEvent() const
97     {
98         return propOnKeyEvent_;
99     }
100 
SetOnMouseEvent(std::function<void (MouseInfo & info)> && propOnMouseEvent)101     void SetOnMouseEvent(std::function<void(MouseInfo& info)>&& propOnMouseEvent)
102     {
103         propOnMouseEvent_ = propOnMouseEvent;
104     }
105 
GetOnMouseEvent()106     const std::function<void(MouseInfo& info)>& GetOnMouseEvent() const
107     {
108         return propOnMouseEvent_;
109     }
110 
SetOnPreKeyEvent(std::function<bool (KeyEventInfo & keyEventInfo)> && propOnPreKeyEvent)111     void SetOnPreKeyEvent(std::function<bool(KeyEventInfo& keyEventInfo)>&& propOnPreKeyEvent)
112     {
113         propOnPreKeyEvent_ = propOnPreKeyEvent;
114     }
115 
GetOnPreKeyEvent()116     const std::function<bool(KeyEventInfo& keyEventInfo)>& GetOnPreKeyEvent() const
117     {
118         return propOnPreKeyEvent_;
119     }
120 
121     ACE_WEB_EVENT_PROPERTY(OnAudioStateChanged, void);
122     ACE_WEB_EVENT_PROPERTY(OnPageStarted, void);
123     ACE_WEB_EVENT_PROPERTY(OnPageFinished, void);
124     ACE_WEB_EVENT_PROPERTY(OnHttpErrorReceive, void);
125     ACE_WEB_EVENT_PROPERTY(OnErrorReceive, void);
126     ACE_WEB_EVENT_PROPERTY(OnConsole, bool);
127     ACE_WEB_EVENT_PROPERTY(OnProgressChange, void);
128     ACE_WEB_EVENT_PROPERTY(OnTitleReceive, void);
129     ACE_WEB_EVENT_PROPERTY(OnFullScreenExit, void);
130     ACE_WEB_EVENT_PROPERTY(OnGeolocationHide, void);
131     ACE_WEB_EVENT_PROPERTY(OnGeolocationShow, void);
132     ACE_WEB_EVENT_PROPERTY(OnRequestFocus, void);
133     ACE_WEB_EVENT_PROPERTY(OnDownloadStart, void);
134     ACE_WEB_EVENT_PROPERTY(OnFullScreenEnter, void);
135     ACE_WEB_EVENT_PROPERTY(OnHttpAuthRequest, bool);
136     ACE_WEB_EVENT_PROPERTY(OnSslErrorRequest, bool);
137     ACE_WEB_EVENT_PROPERTY(OnAllSslErrorRequest, bool);
138     ACE_WEB_EVENT_PROPERTY(OnSslSelectCertRequest, bool);
139     ACE_WEB_EVENT_PROPERTY(OnInterceptRequest, RefPtr<WebResponse>);
140     ACE_WEB_EVENT_PROPERTY(OnUrlLoadIntercept, bool);
141     ACE_WEB_EVENT_PROPERTY(OnLoadIntercept, bool);
142     ACE_WEB_EVENT_PROPERTY(OnOverrideUrlLoading, bool);
143     ACE_WEB_EVENT_PROPERTY(OnFileSelectorShow, bool);
144     ACE_WEB_EVENT_PROPERTY(OnContextMenuShow, bool);
145     ACE_WEB_EVENT_PROPERTY(OnContextMenuHide, void);
146     ACE_WEB_EVENT_PROPERTY(OnRenderExited, void);
147     ACE_WEB_EVENT_PROPERTY(OnRefreshAccessedHistory, void);
148     ACE_WEB_EVENT_PROPERTY(OnResourceLoad, void);
149     ACE_WEB_EVENT_PROPERTY(OnScaleChange, void);
150     ACE_WEB_EVENT_PROPERTY(OnScroll, void);
151     ACE_WEB_EVENT_PROPERTY(OnPermissionRequest, void);
152     ACE_WEB_EVENT_PROPERTY(OnScreenCaptureRequest, void);
153     ACE_WEB_EVENT_PROPERTY(OnSearchResultReceive, void);
154     ACE_WEB_EVENT_PROPERTY(OnWindowNew, void);
155     ACE_WEB_EVENT_PROPERTY(OnWindowExit, void);
156     ACE_WEB_EVENT_PROPERTY(OnPageVisible, void);
157     ACE_WEB_EVENT_PROPERTY(OnDataResubmitted, void);
158     ACE_WEB_EVENT_PROPERTY(OnFaviconReceived, void);
159     ACE_WEB_EVENT_PROPERTY(OnFirstContentfulPaint, void);
160     ACE_WEB_EVENT_PROPERTY(OnFirstMeaningfulPaint, void);
161     ACE_WEB_EVENT_PROPERTY(OnLargestContentfulPaint, void);
162     ACE_WEB_EVENT_PROPERTY(OnTouchIconUrl, void);
163     ACE_WEB_EVENT_PROPERTY(OnOverScroll, void);
164     ACE_WEB_EVENT_PROPERTY(OnSafeBrowsingCheckResult, void);
165     ACE_WEB_EVENT_PROPERTY(OnNavigationEntryCommitted, void);
166     ACE_WEB_EVENT_PROPERTY(OnNativeEmbedLifecycleChange, void);
167     ACE_WEB_EVENT_PROPERTY(OnNativeEmbedVisibilityChange, void);
168     ACE_WEB_EVENT_PROPERTY(OnNativeEmbedGesture, void);
169     ACE_WEB_EVENT_PROPERTY(OnIntelligentTrackingPreventionResult, void);
170     ACE_WEB_EVENT_PROPERTY(OnRenderProcessNotResponding, void);
171     ACE_WEB_EVENT_PROPERTY(OnRenderProcessResponding, void);
172     ACE_WEB_EVENT_PROPERTY(OnViewportFitChanged, void);
173     ACE_WEB_EVENT_PROPERTY(OnAdsBlocked, void);
174     ACE_WEB_EVENT_PROPERTY(OnInterceptKeyboardAttach,  WebKeyboardOption);
175 
176 private:
177     std::function<void(KeyEventInfo& keyEventInfo)> propOnKeyEvent_;
178     std::function<void(MouseInfo& info)> propOnMouseEvent_;
179     OnDragFunc propOnDragStartEvent_;
180     OnDropFunc propOnDragEnterEvent_;
181     OnDropFunc propOnDragMoveEvent_;
182     OnDropFunc propOnDragLeaveEvent_;
183     OnDropFunc propOnDropEvent_;
184     std::function<bool(const std::shared_ptr<BaseEventInfo>& info)> onAlertImpl_;
185     std::function<bool(const std::shared_ptr<BaseEventInfo>& info)> onConfirmImpl_;
186     std::function<bool(const std::shared_ptr<BaseEventInfo>& info)> onPromptImpl_;
187     std::function<bool(const std::shared_ptr<BaseEventInfo>& info)> onBeforeUnloadImpl_;
188     std::function<bool(KeyEventInfo& keyEventInfo)> propOnPreKeyEvent_;
189 
190     ACE_DISALLOW_COPY_AND_MOVE(WebEventHub);
191 };
192 } // namespace OHOS::Ace::NG
193 
194 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_WEB_EVENT_HUB_H
195