1 /*
2  * Copyright (c) 2021-2023 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 "interfaces/inner_api/ace/ui_event_func.h"
17 
18 #include "ace_forward_compatibility.h"
19 
20 namespace OHOS::Ace {
21 constexpr char REGISTER_UI_EVENT_OBSERVER_FUNC[] = "OHOS_ACE_RegisterUIEventObserver";
22 constexpr char UNREGISTER_UI_EVENT_OBSERVER_FUNC[] = "OHOS_ACE_UnregisterUIEventObserver";
23 constexpr char GET_NODE_PROPERTY_FUNC[] = "OHOS_ACE_GetNodeProperty";
24 constexpr char GET_SIMPLIFIED_INSPECTOR_TREE_FUNC[] = "OHOS_ACE_GetSimplifiedInspectorTree";
25 
UIEventFunc()26 UIEventFunc::UIEventFunc()
27 {
28     handle_ = LOADLIB(AceForwardCompatibility::GetAceLibName());
29     if (handle_ == nullptr) {
30         ResetFunc();
31         return;
32     }
33     registerFunc_ = reinterpret_cast<RegisterUIEventObserverFunc>(LOADSYM(handle_, REGISTER_UI_EVENT_OBSERVER_FUNC));
34     unregisterFunc_ =
35         reinterpret_cast<UnregisterUIEventObserverFunc>(LOADSYM(handle_, UNREGISTER_UI_EVENT_OBSERVER_FUNC));
36     getPropFunc_ = reinterpret_cast<GetNodePropertyFunc>(LOADSYM(handle_, GET_NODE_PROPERTY_FUNC));
37     getTreeFunc_ =
38         reinterpret_cast<GetSimplifiedInspectorTreeFunc>(LOADSYM(handle_, GET_SIMPLIFIED_INSPECTOR_TREE_FUNC));
39     if (!IsAvailable()) {
40         FREELIB(handle_);
41         ResetFunc();
42     }
43 }
44 
~UIEventFunc()45 UIEventFunc::~UIEventFunc()
46 {
47     if (handle_) {
48         FREELIB(handle_);
49         ResetFunc();
50     }
51 }
52 
Get()53 UIEventFunc& UIEventFunc::Get()
54 {
55     static UIEventFunc func;
56     return func;
57 }
58 
IsAvailable() const59 bool UIEventFunc::IsAvailable() const
60 {
61     return registerFunc_ && unregisterFunc_ && getPropFunc_ && getTreeFunc_;
62 }
63 
ResetFunc()64 void UIEventFunc::ResetFunc()
65 {
66     registerFunc_ = nullptr;
67     unregisterFunc_ = nullptr;
68     getPropFunc_ = nullptr;
69     getTreeFunc_ = nullptr;
70     handle_ = nullptr;
71 }
72 
RegisterUIEventObserver(const std::string& config, const std::shared_ptr<UIEventObserver>& observer)73 void UIEventFunc::RegisterUIEventObserver(const std::string& config, const std::shared_ptr<UIEventObserver>& observer)
74 {
75     if (UIEventFunc::Get().IsAvailable()) {
76         UIEventFunc::Get().registerFunc_(config, observer);
77     }
78 }
79 
UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver>& observer)80 void UIEventFunc::UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver>& observer)
81 {
82     if (UIEventFunc::Get().IsAvailable()) {
83         UIEventFunc::Get().unregisterFunc_(observer);
84     }
85 }
86 
GetNodeProperty( const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodeProperties)87 void UIEventFunc::GetNodeProperty(
88     const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodeProperties)
89 {
90     if (UIEventFunc::Get().IsAvailable()) {
91         UIEventFunc::Get().getPropFunc_(pageUrl, nodeProperties);
92     }
93 }
94 
GetSimplifiedInspectorTree(std::string& tree)95 void UIEventFunc::GetSimplifiedInspectorTree(std::string& tree)
96 {
97     if (UIEventFunc::Get().IsAvailable()) {
98         UIEventFunc::Get().getTreeFunc_(tree);
99     }
100 }
101 } // namespace OHOS::Ace
102