• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ui_event_observer.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 constexpr char GET_SIMPLIFIED_INSPECTOR_TREE_ASYNC_FUNC[] = "OHOS_ACE_GetSimplifiedInspectorTreeAsync";
27 
UIEventFunc()28 UIEventFunc::UIEventFunc()
29 {
30     handle_ = LOADLIB(AceForwardCompatibility::GetAceLibName());
31     if (handle_ == nullptr) {
32         ResetFunc();
33         return;
34     }
35     registerFunc_ = reinterpret_cast<RegisterUIEventObserverFunc>(LOADSYM(handle_, REGISTER_UI_EVENT_OBSERVER_FUNC));
36     unregisterFunc_ =
37         reinterpret_cast<UnregisterUIEventObserverFunc>(LOADSYM(handle_, UNREGISTER_UI_EVENT_OBSERVER_FUNC));
38     getPropFunc_ = reinterpret_cast<GetNodePropertyFunc>(LOADSYM(handle_, GET_NODE_PROPERTY_FUNC));
39     getTreeFunc_ =
40         reinterpret_cast<GetSimplifiedInspectorTreeFunc>(LOADSYM(handle_, GET_SIMPLIFIED_INSPECTOR_TREE_FUNC));
41     getTreeAsyncFunc_ = reinterpret_cast<GetSimplifiedInspectorTreeAsyncFunc>(
42         LOADSYM(handle_, GET_SIMPLIFIED_INSPECTOR_TREE_ASYNC_FUNC));
43     if (!IsAvailable()) {
44         FREELIB(handle_);
45         ResetFunc();
46     }
47 }
48 
~UIEventFunc()49 UIEventFunc::~UIEventFunc()
50 {
51     if (handle_) {
52         FREELIB(handle_);
53         ResetFunc();
54     }
55 }
56 
Get()57 UIEventFunc& UIEventFunc::Get()
58 {
59     static UIEventFunc func;
60     return func;
61 }
62 
IsAvailable() const63 bool UIEventFunc::IsAvailable() const
64 {
65     return registerFunc_ && unregisterFunc_ && getPropFunc_ && getTreeFunc_ && getTreeAsyncFunc_;
66 }
67 
ResetFunc()68 void UIEventFunc::ResetFunc()
69 {
70     registerFunc_ = nullptr;
71     unregisterFunc_ = nullptr;
72     getPropFunc_ = nullptr;
73     getTreeFunc_ = nullptr;
74     getTreeAsyncFunc_ = nullptr;
75     handle_ = nullptr;
76 }
77 
RegisterUIEventObserver(const std::string & config,const std::shared_ptr<UIEventObserver> & observer)78 void UIEventFunc::RegisterUIEventObserver(const std::string& config, const std::shared_ptr<UIEventObserver>& observer)
79 {
80     if (UIEventFunc::Get().IsAvailable()) {
81         UIEventFunc::Get().registerFunc_(config, observer);
82     }
83 }
84 
UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver> & observer)85 void UIEventFunc::UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver>& observer)
86 {
87     if (UIEventFunc::Get().IsAvailable()) {
88         UIEventFunc::Get().unregisterFunc_(observer);
89     }
90 }
91 
GetNodeProperty(const std::string & pageUrl,std::unordered_map<std::string,std::string> & nodeProperties)92 void UIEventFunc::GetNodeProperty(
93     const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodeProperties)
94 {
95     if (UIEventFunc::Get().IsAvailable()) {
96         UIEventFunc::Get().getPropFunc_(pageUrl, nodeProperties);
97     }
98 }
99 
GetSimplifiedInspectorTree(const TreeParams & params,std::string & tree)100 void UIEventFunc::GetSimplifiedInspectorTree(const TreeParams& params, std::string& tree)
101 {
102     if (UIEventFunc::Get().IsAvailable()) {
103         UIEventFunc::Get().getTreeFunc_(params, tree);
104     }
105 }
106 
GetSimplifiedInspectorTreeAsync(const TreeParams & params,OnInspectorTreeResult && callback)107 void UIEventFunc::GetSimplifiedInspectorTreeAsync(const TreeParams& params, OnInspectorTreeResult&& callback)
108 {
109     if (UIEventFunc::Get().IsAvailable()) {
110         UIEventFunc::Get().getTreeAsyncFunc_(params, std::move(callback));
111     }
112 }
113 } // namespace OHOS::Ace
114