• 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_DECLARATION_WEB_WEB_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_WEB_WEB_DECLARATION_H
18 
19 #include "core/components/declaration/common/declaration.h"
20 #include "core/components/declaration/web/web_client.h"
21 #include "frameworks/bridge/common/dom/dom_type.h"
22 
23 namespace OHOS::Ace {
24 
25 struct WebAttribute : Attribute {
26     std::string src;
27     std::string data;
28 };
29 
30 struct WebEvent : Event {
31     EventMarker pageStartEventId;
32     EventMarker pageFinishEventId;
33     EventMarker progressChangeEventId;
34     EventMarker titleReceiveEventId;
35     EventMarker geolocationHideEventId;
36     EventMarker geolocationShowEventId;
37     EventMarker requestFocusEventId;
38     EventMarker onFocusEventId;
39     EventMarker pageErrorEventId;
40     EventMarker httpErrorEventId;
41     EventMarker messageEventId;
42     EventMarker downloadStartEventId;
43     EventMarker renderExitedEventId;
44     EventMarker refreshAccessedHistoryId;
45 };
46 
47 struct WebMethod : Method {
ReloadWebMethod48     void Reload() const
49     {
50         WebClient::GetInstance().ReloadWebview();
51     }
52 };
53 
54 class ACE_EXPORT WebDeclaration : public Declaration {
55     DECLARE_ACE_TYPE(WebDeclaration, Declaration);
56 
57 public:
58     WebDeclaration() = default;
59     ~WebDeclaration() override = default;
60     WebMethod webMethod;
61 
SetWebSrc(const std::string & src)62     void SetWebSrc(const std::string& src)
63     {
64         auto& attribute = MaybeResetAttribute<WebAttribute>(AttributeTag::SPECIALIZED_ATTR);
65         attribute.src = src;
66     }
67 
GetWebSrc()68     const std::string& GetWebSrc() const
69     {
70         auto& attribute = static_cast<WebAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
71         return attribute.src;
72     }
73 
SetWebData(const std::string & data)74     void SetWebData(const std::string& data)
75     {
76         auto& attribute = MaybeResetAttribute<WebAttribute>(AttributeTag::SPECIALIZED_ATTR);
77         attribute.data = data;
78     }
79 
GetWebData()80     const std::string& GetWebData() const
81     {
82         auto& attribute = static_cast<WebAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
83         return attribute.data;
84     }
85 
SetPageStartedEventId(const EventMarker & pageStartedEventId)86     void SetPageStartedEventId(const EventMarker& pageStartedEventId)
87     {
88         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
89         event.pageStartEventId = pageStartedEventId;
90     }
91 
GetPageStartedEventId()92     const EventMarker& GetPageStartedEventId() const
93     {
94         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
95         return event.pageStartEventId;
96     }
97 
SetPageFinishedEventId(const EventMarker & pageFinishedEventId)98     void SetPageFinishedEventId(const EventMarker& pageFinishedEventId)
99     {
100         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
101         event.pageFinishEventId = pageFinishedEventId;
102     }
103 
GetPageFinishedEventId()104     const EventMarker& GetPageFinishedEventId() const
105     {
106         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
107         return event.pageFinishEventId;
108     }
109 
SetProgressChangeEventId(const EventMarker & progressChangeEventId)110     void SetProgressChangeEventId(const EventMarker& progressChangeEventId)
111     {
112         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
113         event.progressChangeEventId = progressChangeEventId;
114     }
115 
GetProgressChangeEventId()116     const EventMarker& GetProgressChangeEventId() const
117     {
118         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
119         return event.progressChangeEventId;
120     }
121 
SetTitleReceiveEventId(const EventMarker & titleReceiveEventId)122     void SetTitleReceiveEventId(const EventMarker& titleReceiveEventId)
123     {
124         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
125         event.titleReceiveEventId = titleReceiveEventId;
126     }
127 
GetTitleReceiveEventId()128     const EventMarker& GetTitleReceiveEventId() const
129     {
130         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
131         return event.titleReceiveEventId;
132     }
133 
SetGeolocationHideEventId(const EventMarker & geolocationHideEventId)134     void SetGeolocationHideEventId(const EventMarker& geolocationHideEventId)
135     {
136         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
137         event.geolocationHideEventId = geolocationHideEventId;
138     }
139 
GetGeolocationHideEventId()140     const EventMarker& GetGeolocationHideEventId() const
141     {
142         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
143         return event.geolocationHideEventId;
144     }
145 
SetGeolocationShowEventId(const EventMarker & geolocationShowEventId)146     void SetGeolocationShowEventId(const EventMarker& geolocationShowEventId)
147     {
148         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
149         event.geolocationShowEventId = geolocationShowEventId;
150     }
151 
GetGeolocationShowEventId()152     const EventMarker& GetGeolocationShowEventId() const
153     {
154         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
155         return event.geolocationShowEventId;
156     }
157 
SetDownloadStartEventId(const EventMarker & downloadStartEventId)158     void SetDownloadStartEventId(const EventMarker& downloadStartEventId)
159     {
160         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
161         event.downloadStartEventId = downloadStartEventId;
162     }
163 
GetDownloadStartEventId()164     const EventMarker& GetDownloadStartEventId() const
165     {
166         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
167         return event.downloadStartEventId;
168     }
169 
SetRequestFocusEventId(const EventMarker & requestFocusEventId)170     void SetRequestFocusEventId(const EventMarker& requestFocusEventId)
171     {
172         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
173         event.requestFocusEventId = requestFocusEventId;
174     }
175 
GetRequestFocusEventId()176     const EventMarker& GetRequestFocusEventId() const
177     {
178         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
179         return event.requestFocusEventId;
180     }
181 
SetOnFocusEventId(const EventMarker & onFocusEventId)182     void SetOnFocusEventId(const EventMarker& onFocusEventId)
183     {
184         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
185         event.onFocusEventId = onFocusEventId;
186     }
187 
GetOnFocusEventId()188     const EventMarker& GetOnFocusEventId() const
189     {
190         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
191         return event.onFocusEventId;
192     }
193 
SetPageErrorEventId(const EventMarker & pageErrorEventId)194     void SetPageErrorEventId(const EventMarker& pageErrorEventId)
195     {
196         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
197         event.pageErrorEventId = pageErrorEventId;
198     }
199 
GetPageErrorEventId()200     const EventMarker& GetPageErrorEventId() const
201     {
202         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
203         return event.pageErrorEventId;
204     }
205 
SetHttpErrorEventId(const EventMarker & httpErrorEventId)206     void SetHttpErrorEventId(const EventMarker& httpErrorEventId)
207     {
208         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
209         event.httpErrorEventId = httpErrorEventId;
210     }
211 
GetHttpErrorEventId()212     const EventMarker& GetHttpErrorEventId() const
213     {
214         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
215         return event.httpErrorEventId;
216     }
217 
SetMessageEventId(const EventMarker & messageEventId)218     void SetMessageEventId(const EventMarker& messageEventId)
219     {
220         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
221         event.messageEventId = messageEventId;
222     }
223 
GetMessageEventId()224     const EventMarker& GetMessageEventId() const
225     {
226         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
227         return event.messageEventId;
228     }
229 
SetRenderExitedId(const EventMarker & renderExitedEventId)230     void SetRenderExitedId(const EventMarker& renderExitedEventId)
231     {
232         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
233         event.renderExitedEventId = renderExitedEventId;
234     }
235 
GetRenderExitedId()236     const EventMarker& GetRenderExitedId() const
237     {
238         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
239         return event.renderExitedEventId;
240     }
241 
SetRefreshAccessedHistoryId(const EventMarker & refreshAccessedHistoryId)242     void SetRefreshAccessedHistoryId(const EventMarker& refreshAccessedHistoryId)
243     {
244         auto& event = MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
245         event.refreshAccessedHistoryId = refreshAccessedHistoryId;
246     }
247 
GetRefreshAccessedHistoryId()248     const EventMarker& GetRefreshAccessedHistoryId() const
249     {
250         auto& event = static_cast<WebEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
251         return event.refreshAccessedHistoryId;
252     }
253 
254 protected:
255     void InitSpecialized() override;
256     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
257     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
258     void CallSpecializedMethod(const std::string& method, const std::string& args) override;
259 };
260 
261 } // namespace OHOS::Ace
262 
263 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_WEB_WEB_DECLARATION_H
264