1 /*
2  * Copyright (c) 2024 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_NG_PATTERNS_EXPANDED_MENU_PLUGIN_LOADER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_EXPANDED_MENU_PLUGIN_LOADER_H
18 
19 #ifndef WINDOWS_PLATFORM
20 #include <dlfcn.h>
21 #endif
22 
23 namespace OHOS::Ace::NG {
24 
25 class ExpandedMenuPluginLoader {
26 #if (defined(__aarch64__) || defined(__x86_64__))
27     const std::string EXPANDED_MENU_PLUGIN_SO_PATH = "/system/lib64/expanded_menu/libexpanded_menu.z.so";
28 #else
29     const std::string EXPANDED_MENU_PLUGIN_SO_PATH = "/system/lib/expanded_menu/libexpanded_menu.z.so";
30 #endif
31     typedef void (*CreateDeviceMenuFunc)(
32         const RefPtr<FrameNode>& menuWrapper,
33         const std::shared_ptr<SelectOverlayInfo>& info
34     );
35 
36 public:
GetInstance()37     static ExpandedMenuPluginLoader& GetInstance()
38     {
39         static ExpandedMenuPluginLoader instance;
40         return instance;
41     }
42 
LoadPlugin()43     bool LoadPlugin()
44     {
45 #ifndef WINDOWS_PLATFORM
46         CHECK_NULL_RETURN(!createDeviceMenu, true);
47         expandedMenuPluginHandle = dlopen(EXPANDED_MENU_PLUGIN_SO_PATH.c_str(), RTLD_LAZY);
48         if (!expandedMenuPluginHandle) {
49             TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s Fail", EXPANDED_MENU_PLUGIN_SO_PATH.c_str());
50             return false;
51         }
52         TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s success", EXPANDED_MENU_PLUGIN_SO_PATH.c_str());
53         createDeviceMenu = (CreateDeviceMenuFunc)(dlsym(expandedMenuPluginHandle, "CreateDeviceMenu"));
54         if (!createDeviceMenu) {
55             TAG_LOGI(AceLogTag::ACE_MENU, "CreateDeviceMenu func load failed");
56             return false;
57         }
58         return true;
59 #endif
60     }
61 
CreateServiceCollaborationMenu(const RefPtr<FrameNode> & menuWrapper,const std::shared_ptr<SelectOverlayInfo> & info)62     void CreateServiceCollaborationMenu(const RefPtr<FrameNode>& menuWrapper,
63         const std::shared_ptr<SelectOverlayInfo>& info)
64     {
65 #ifndef WINDOWS_PLATFORM
66         CHECK_NULL_VOID(menuWrapper);
67         CHECK_NULL_VOID(info);
68         CHECK_NULL_VOID(LoadPlugin());
69         if (!createDeviceMenu) {
70             TAG_LOGI(AceLogTag::ACE_MENU, "dynamic create expended menu failed");
71             return;
72         }
73         TAG_LOGI(AceLogTag::ACE_MENU, "dynamic create expended menu");
74         createDeviceMenu(menuWrapper, info);
75         menuWrapper->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_CHILD);
76 #endif
77     }
78 
HasCollaborationMenu()79     bool HasCollaborationMenu()
80     {
81 #ifndef WINDOWS_PLATFORM
82         CHECK_NULL_RETURN(LoadPlugin(), false);
83         CHECK_NULL_RETURN(createDeviceMenu, false);
84         return true;
85 #else
86         return false;
87 #endif
88     }
89 
90     void *expandedMenuPluginHandle;
91     CreateDeviceMenuFunc createDeviceMenu;
92 };
93 } // namespace OHOS::Ace::NG
94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_EXPANDED_MENU_PLUGIN_LOADER_H