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 typedef const char* (*GetStoreUrlFrontFunc)(); 37 typedef const char* (*GetAPPNameFunc)(TextDataDetectType type); 38 39 public: GetInstance()40 static ExpandedMenuPluginLoader& GetInstance() 41 { 42 static ExpandedMenuPluginLoader instance; 43 return instance; 44 } 45 LoadPlugin()46 bool LoadPlugin() 47 { 48 #ifndef WINDOWS_PLATFORM 49 CHECK_NULL_RETURN(!createDeviceMenu, true); 50 expandedMenuPluginHandle = dlopen(EXPANDED_MENU_PLUGIN_SO_PATH.c_str(), RTLD_LAZY); 51 if (!expandedMenuPluginHandle) { 52 TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s Fail", EXPANDED_MENU_PLUGIN_SO_PATH.c_str()); 53 return false; 54 } 55 TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s success", EXPANDED_MENU_PLUGIN_SO_PATH.c_str()); 56 createDeviceMenu = (CreateDeviceMenuFunc)(dlsym(expandedMenuPluginHandle, "CreateDeviceMenu")); 57 if (!createDeviceMenu) { 58 TAG_LOGI(AceLogTag::ACE_MENU, "CreateDeviceMenu func load failed"); 59 return false; 60 } 61 return true; 62 #endif 63 } 64 LoadStoreUrlFront()65 bool LoadStoreUrlFront() 66 { 67 #ifndef WINDOWS_PLATFORM 68 CHECK_NULL_RETURN(!getStoreUrlFront, true); 69 expandedMenuPluginHandle = dlopen(EXPANDED_MENU_PLUGIN_SO_PATH.c_str(), RTLD_LAZY); 70 if (!expandedMenuPluginHandle) { 71 TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s Fail", EXPANDED_MENU_PLUGIN_SO_PATH.c_str()); 72 return false; 73 } 74 TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s success", EXPANDED_MENU_PLUGIN_SO_PATH.c_str()); 75 getStoreUrlFront = (GetStoreUrlFrontFunc)(dlsym(expandedMenuPluginHandle, "GetStoreUrlFront")); 76 if (!getStoreUrlFront) { 77 TAG_LOGI(AceLogTag::ACE_MENU, "GetStoreUrlFront func load failed"); 78 return false; 79 } 80 return true; 81 #else 82 return false; 83 #endif 84 } 85 LoadAPPName()86 bool LoadAPPName() 87 { 88 #ifndef WINDOWS_PLATFORM 89 CHECK_NULL_RETURN(!getAPPName, true); 90 expandedMenuPluginHandle = dlopen(EXPANDED_MENU_PLUGIN_SO_PATH.c_str(), RTLD_LAZY); 91 if (!expandedMenuPluginHandle) { 92 TAG_LOGI(AceLogTag::ACE_MENU, "dlopen lib %{public}s Fail", EXPANDED_MENU_PLUGIN_SO_PATH.c_str()); 93 return false; 94 } 95 getAPPName = (GetAPPNameFunc)(dlsym(expandedMenuPluginHandle, "GetAPPName")); 96 if (!getAPPName) { 97 TAG_LOGI(AceLogTag::ACE_MENU, "GetAPPName func load failed"); 98 return false; 99 } 100 return true; 101 #else 102 return false; 103 #endif 104 } 105 GetStoreUrlFront()106 std::string GetStoreUrlFront() 107 { 108 #ifndef WINDOWS_PLATFORM 109 CHECK_NULL_RETURN(LoadStoreUrlFront(), ""); 110 if (!getStoreUrlFront) { 111 TAG_LOGI(AceLogTag::ACE_MENU, "dynamic get front failed"); 112 return ""; 113 } 114 std::string result = getStoreUrlFront(); 115 return result; 116 #else 117 return ""; 118 #endif 119 } 120 GetAPPName(TextDataDetectType type)121 std::string GetAPPName(TextDataDetectType type) 122 { 123 #ifndef WINDOWS_PLATFORM 124 CHECK_NULL_RETURN(LoadAPPName(), ""); 125 if (!getAPPName) { 126 TAG_LOGI(AceLogTag::ACE_MENU, "dynamic load name failed"); 127 return ""; 128 } 129 std::string result = getAPPName(type); 130 return result; 131 #else 132 return ""; 133 #endif 134 } 135 CreateServiceCollaborationMenu(const RefPtr<FrameNode> & menuWrapper,const std::shared_ptr<SelectOverlayInfo> & info)136 void CreateServiceCollaborationMenu(const RefPtr<FrameNode>& menuWrapper, 137 const std::shared_ptr<SelectOverlayInfo>& info) 138 { 139 #ifndef WINDOWS_PLATFORM 140 CHECK_NULL_VOID(menuWrapper); 141 CHECK_NULL_VOID(info); 142 CHECK_NULL_VOID(LoadPlugin()); 143 if (!createDeviceMenu) { 144 TAG_LOGI(AceLogTag::ACE_MENU, "dynamic create expended menu failed"); 145 return; 146 } 147 TAG_LOGI(AceLogTag::ACE_MENU, "dynamic create expended menu"); 148 createDeviceMenu(menuWrapper, info); 149 menuWrapper->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF_AND_CHILD); 150 #endif 151 } 152 HasCollaborationMenu()153 bool HasCollaborationMenu() 154 { 155 #ifndef WINDOWS_PLATFORM 156 CHECK_NULL_RETURN(LoadPlugin(), false); 157 CHECK_NULL_RETURN(createDeviceMenu, false); 158 return true; 159 #else 160 return false; 161 #endif 162 } 163 164 void *expandedMenuPluginHandle; 165 CreateDeviceMenuFunc createDeviceMenu; 166 GetStoreUrlFrontFunc getStoreUrlFront; 167 GetAPPNameFunc getAPPName; 168 }; 169 } // namespace OHOS::Ace::NG 170 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_EXPANDED_MENU_PLUGIN_LOADER_H