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