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