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