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/xcomponent/xcomponent_declaration.h"
17 
18 #include "core/components/declaration/common/declaration_constants.h"
19 
20 namespace OHOS::Ace {
21 using namespace Framework;
22 
InitSpecialized()23 void XComponentDeclaration::InitSpecialized()
24 {
25     AddSpecializedAttribute(DeclarationConstants::DEFAULT_XCOMPONENT_ATTR);
26     AddSpecializedEvent(DeclarationConstants::DEFAULT_XCOMPONENT_EVENT);
27 }
28 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)29 bool XComponentDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
30 {
31     static const LinearMapNode<void (*)(XComponentDeclaration&, const std::string&)> xComponentAttrOperators[] = {
32         {
33             DOM_XCOMPONENT_LIBRARYNAME,
34             [](XComponentDeclaration& declaration, const std::string& libraryName) {
35                 declaration.SetXComponentLibraryName(libraryName);
36             }
37         },
38         {
39             DOM_XCOMPONENT_NAME,
40             [](XComponentDeclaration& declaration, const std::string& name) {
41                 declaration.SetXComponentName(name);
42             }
43         },
44         {
45             DOM_XCOMPONENT_TYPE,
46             [](XComponentDeclaration& declaration, const std::string& type) {
47                 declaration.SetXComponentType(type);
48             }
49         },
50     };
51     auto operatorIter = BinarySearchFindIndex(xComponentAttrOperators,
52                                               ArraySize(xComponentAttrOperators),
53                                               attr.first.c_str());
54     if (operatorIter != -1) {
55         xComponentAttrOperators[operatorIter].value(*this, attr.second);
56         return true;
57     }
58     return false;
59 }
60 
61 
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)62 bool XComponentDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
63 {
64     static const LinearMapNode<void (*)(XComponentDeclaration&, const EventMarker&)> eventOperators[] = {
65         {
66             DOM_XCOMPONENT_DESTROY,
67             [](XComponentDeclaration& declaration, const EventMarker& event) {
68                 auto& xComponentEvent = declaration.MaybeResetEvent<XComponentEvent>(EventTag::SPECIALIZED_EVENT);
69                 if (xComponentEvent.IsValid()) {
70                     xComponentEvent.xComponentDestroyEventId = event;
71                 }
72             }
73         },
74         {
75             DOM_XCOMPONENT_INIT,
76             [](XComponentDeclaration& declaration, const EventMarker& event) {
77                 auto& xComponentEvent = declaration.MaybeResetEvent<XComponentEvent>(EventTag::SPECIALIZED_EVENT);
78                 if (xComponentEvent.IsValid()) {
79                     xComponentEvent.xComponentInitEventId = event;
80                 }
81             }
82         },
83     };
84     auto operatorIter = BinarySearchFindIndex(eventOperators, ArraySize(eventOperators), event.c_str());
85     if (operatorIter != -1) {
86         eventOperators[operatorIter].value(*this, EventMarker(eventId, event, pageId));
87         return true;
88     }
89     return false;
90 }
91 } // namespace OHOS::Ace
92