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_MIN_SIZE = 0; 28 static constexpr int32_t IPC_MAX_SIZE = 128; 29 30 struct ModuleUpdateStatus { 31 std::string hmpName; 32 bool isPreInstalled {false}; 33 bool isAllMountSuccess {false}; 34 }; 35 36 class ModuleIpcHelper { 37 public: 38 static int32_t ReadModulePackageInfos(MessageParcel &reply, std::list<ModulePackageInfo> &infos); 39 static int32_t WriteModulePackageInfos(MessageParcel &data, const std::list<ModulePackageInfo> &infos); 40 static int32_t ReadModuleUpdateStatus(MessageParcel &reply, ModuleUpdateStatus &status); 41 static int32_t WriteModuleUpdateStatus(MessageParcel &data, const ModuleUpdateStatus &status); 42 43 template<typename T> ReadList(MessageParcel & reply,std::list<T> & list,const std::function<void (MessageParcel &,T &)> & read)44 static void ReadList(MessageParcel &reply, std::list<T> &list, 45 const std::function<void(MessageParcel &, T &)> &read) 46 { 47 int32_t size = reply.ReadInt32(); 48 if (size > IPC_MAX_SIZE || size < IPC_MIN_SIZE) { 49 return; 50 } 51 for (int32_t i = 0; i < size; ++i) { 52 T item; 53 read(reply, item); 54 list.emplace_back(std::move(item)); 55 } 56 } 57 58 template<typename T> WriteList(MessageParcel & data,const std::list<T> & list,const std::function<void (MessageParcel &,const T &)> & write)59 static void WriteList(MessageParcel &data, const std::list<T> &list, 60 const std::function<void(MessageParcel &, const T &)> &write) 61 { 62 data.WriteInt32(static_cast<int32_t>(list.size())); 63 for (auto &iter : list) { 64 write(data, iter); 65 } 66 } 67 }; 68 } // namespace SysInstaller 69 } // namespace OHOS 70 #endif // SYS_INSTALLER_MODULE_IPC_HELPER_H