1 /*
2  * Copyright (c) 2021 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 #include "core/components/declaration/web/web_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 
20 namespace OHOS::Ace {
21 
22 using namespace Framework;
23 
InitSpecialized()24 void WebDeclaration::InitSpecialized()
25 {
26     AddSpecializedAttribute(DeclarationConstants::DEFAULT_WEB_ATTR);
27     AddSpecializedEvent(DeclarationConstants::DEFAULT_WEB_EVENT);
28     AddSpecializedMethod(DeclarationConstants::DEFAULT_WEB_METHOD);
29 }
30 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)31 bool WebDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
32 {
33     static const LinearMapNode<void (*)(WebDeclaration&, const std::string&)> webAttrOperators[] = {
34         { DOM_WEB_WEBSRC, [](WebDeclaration& declaration, const std::string& src) { declaration.SetWebSrc(src); } },
35     };
36     auto operatorIter = BinarySearchFindIndex(webAttrOperators, ArraySize(webAttrOperators), attr.first.c_str());
37     if (operatorIter != -1) {
38         webAttrOperators[operatorIter].value(*this, attr.second);
39         return true;
40     }
41     return false;
42 }
43 
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)44 bool WebDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
45 {
46     static const LinearMapNode<void (*)(WebDeclaration&, const EventMarker&)> eventOperators[] = {
47         { DOM_PAGEERROR,
48             [](WebDeclaration& declaration, const EventMarker& event) {
49                 auto& webEvent = declaration.MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
50                 if (webEvent.IsValid()) {
51                     webEvent.pageErrorEventId = event;
52                 }
53             } },
54         { DOM_WEB_MESSAGE,
55             [](WebDeclaration& declaration, const EventMarker& event) {
56                 auto& webEvent = declaration.MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
57                 if (webEvent.IsValid()) {
58                     webEvent.messageEventId = event;
59                 }
60             } },
61         { DOM_PAGEFINISH,
62             [](WebDeclaration& declaration, const EventMarker& event) {
63                 auto& webEvent = declaration.MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
64                 if (webEvent.IsValid()) {
65                     webEvent.pageFinishEventId = event;
66                 }
67             } },
68         { DOM_PAGESTART,
69             [](WebDeclaration& declaration, const EventMarker& event) {
70                 auto& webEvent = declaration.MaybeResetEvent<WebEvent>(EventTag::SPECIALIZED_EVENT);
71                 if (webEvent.IsValid()) {
72                     webEvent.pageStartEventId = event;
73                 }
74             } },
75     };
76     auto operatorIter = BinarySearchFindIndex(eventOperators, ArraySize(eventOperators), event.c_str());
77     if (operatorIter != -1) {
78         eventOperators[operatorIter].value(*this, EventMarker(eventId, event, pageId));
79         return true;
80     }
81     return false;
82 }
83 
CallSpecializedMethod(const std::string & method,const std::string & args)84 void WebDeclaration::CallSpecializedMethod(const std::string& method, const std::string& args)
85 {
86     auto& webMethod = static_cast<WebMethod&>(GetMethod(MethodTag::SPECIALIZED_METHOD));
87     if (!webMethod.IsValid()) {
88         LOGW("method of web is null");
89         return;
90     }
91 
92     // Operator map for method
93     static const std::unordered_map<std::string,
94             void (*)(WebMethod&, const std::string&)>
95             methedOperators = {
96             { DOM_METHOD_RELOAD, [](WebMethod& webMethod, const std::string& args) {
97                 webMethod.Reload();
98             } },
99     };
100     auto operatorIter = methedOperators.find(method);
101     if (operatorIter != methedOperators.end()) {
102         operatorIter->second(webMethod, args);
103     }
104 }
105 
106 } // namespace OHOS::Ace
107