1 /* 2 * Copyright (c) 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 #ifndef SYS_INSTALLER_MODULE_IPC_HELPER_H 17 #define SYS_INSTALLER_MODULE_IPC_HELPER_H 18 19 #include <functional> 20 #include <list> 21 22 #include "message_parcel.h" 23 #include "module_file.h" 24 25 namespace OHOS { 26 namespace SysInstaller { 27 static constexpr int32_t IPC_MAX_SIZE = 128; 28 29 struct SaStatus { 30 int32_t saId; 31 bool isPreInstalled; 32 bool isMountSuccess; 33 }; 34 35 struct ModuleUpdateStatus { 36 std::string process; 37 std::list<SaStatus> saStatusList; 38 }; 39 40 struct SaInfo { 41 std::string saName; 42 int32_t saId; 43 ModuleVersion version; 44 }; 45 46 struct ModulePackageInfo { 47 std::string hmpName; 48 std::list<SaInfo> saInfoList; 49 }; 50 51 class ModuleIpcHelper { 52 public: 53 static int32_t ReadModulePackageInfos(MessageParcel &reply, std::list<ModulePackageInfo> &infos); 54 static int32_t WriteModulePackageInfos(MessageParcel &data, const std::list<ModulePackageInfo> &infos); 55 static int32_t ReadModuleUpdateStatus(MessageParcel &reply, ModuleUpdateStatus &status); 56 static int32_t WriteModuleUpdateStatus(MessageParcel &data, const ModuleUpdateStatus &status); 57 58 template<typename T> ReadList(MessageParcel & reply,std::list<T> & list,const std::function<void (MessageParcel &,T &)> & read)59 static void ReadList(MessageParcel &reply, std::list<T> &list, 60 const std::function<void(MessageParcel &, T &)> &read) 61 { 62 int32_t size = reply.ReadInt32(); 63 if (size > IPC_MAX_SIZE) { 64 return; 65 } 66 for (int32_t i = 0; i < size; ++i) { 67 T item; 68 read(reply, item); 69 list.emplace_back(std::move(item)); 70 } 71 } 72 73 template<typename T> WriteList(MessageParcel & data,const std::list<T> & list,const std::function<void (MessageParcel &,const T &)> & write)74 static void WriteList(MessageParcel &data, const std::list<T> &list, 75 const std::function<void(MessageParcel &, const T &)> &write) 76 { 77 data.WriteInt32(static_cast<int32_t>(list.size())); 78 for (auto &iter : list) { 79 write(data, iter); 80 } 81 } 82 }; 83 } // namespace SysInstaller 84 } // namespace OHOS 85 #endif // SYS_INSTALLER_MODULE_IPC_HELPER_H