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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_XCOMPONENT_XCOMPONENT_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_XCOMPONENT_XCOMPONENT_DECLARATION_H
18 
19 #include "core/components/declaration/common/declaration.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 
22 namespace OHOS::Ace {
23 struct XComponentAttribute : Attribute {
24     std::string name;
25     std::string id;
26     std::string type;
27     std::string libraryName;
28 };
29 
30 struct XComponentEvent : Event {
31     EventMarker xComponentInitEventId;
32     EventMarker xComponentDestroyEventId;
33 };
34 
35 class ACE_EXPORT XComponentDeclaration : public Declaration {
36     DECLARE_ACE_TYPE(XComponentDeclaration, Declaration);
37 
38 public:
39     XComponentDeclaration() = default;
40     ~XComponentDeclaration() override = default;
41 
SetXComponentName(const std::string & name)42     void SetXComponentName(const std::string& name)
43     {
44         auto& attribute = MaybeResetAttribute<XComponentAttribute>(AttributeTag::SPECIALIZED_ATTR);
45         attribute.name = name;
46     }
47 
GetXComponentName()48     const std::string& GetXComponentName() const
49     {
50         auto& attribute = static_cast<XComponentAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
51         return attribute.name;
52     }
53 
SetXComponentId(const std::string & id)54     void SetXComponentId(const std::string& id)
55     {
56         auto& attribute = MaybeResetAttribute<XComponentAttribute>(AttributeTag::SPECIALIZED_ATTR);
57         attribute.id = id;
58     }
59 
GetXComponentId()60     const std::string& GetXComponentId() const
61     {
62         auto& attribute = static_cast<XComponentAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
63         return attribute.id;
64     }
65 
SetXComponentType(const std::string & type)66     void SetXComponentType(const std::string& type)
67     {
68         auto& attribute = MaybeResetAttribute<XComponentAttribute>(AttributeTag::SPECIALIZED_ATTR);
69         attribute.type = type;
70     }
71 
GetXComponentType()72     const std::string& GetXComponentType() const
73     {
74         auto& attribute = static_cast<XComponentAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
75         return attribute.type;
76     }
77 
SetXComponentLibraryName(const std::string & libraryName)78     void SetXComponentLibraryName(const std::string& libraryName)
79     {
80         auto& attribute = MaybeResetAttribute<XComponentAttribute>(AttributeTag::SPECIALIZED_ATTR);
81         attribute.libraryName = libraryName;
82     }
83 
GetXComponentLibraryName()84     const std::string& GetXComponentLibraryName() const
85     {
86         auto& attribute = static_cast<XComponentAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
87         return attribute.libraryName;
88     }
89 
SetXComponentInitEventId(const EventMarker & xComponentInitEventId)90     void SetXComponentInitEventId(const EventMarker& xComponentInitEventId)
91     {
92         auto& event = MaybeResetEvent<XComponentEvent>(EventTag::SPECIALIZED_EVENT);
93         event.xComponentInitEventId = xComponentInitEventId;
94     }
95 
GetXComponentInitEventId()96     const EventMarker& GetXComponentInitEventId() const
97     {
98         auto& event = static_cast<XComponentEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
99         return event.xComponentInitEventId;
100     }
101 
SetXComponentDestroyEventId(const EventMarker & xComponentDestroyEventId)102     void SetXComponentDestroyEventId(const EventMarker& xComponentDestroyEventId)
103     {
104         auto& event = MaybeResetEvent<XComponentEvent>(EventTag::SPECIALIZED_EVENT);
105         event.xComponentDestroyEventId = xComponentDestroyEventId;
106     }
107 
GetXComponentDestroyEventId()108     const EventMarker& GetXComponentDestroyEventId() const
109     {
110         auto& event = static_cast<XComponentEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
111         return event.xComponentDestroyEventId;
112     }
113 
114 protected:
115     void InitSpecialized() override;
116     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
117     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
118 };
119 } // namespace OHOS::Ace
120 
121 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_XCOMPONENT_XCOMPONENT_DECLARATION_H
122