1 /* 2 * Copyright (c) 2024-2025 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 BASE_NOTIFICATION_ANS_SERVICES_ANS_INCLUDE_BASE_MANAGER_H 17 #define BASE_NOTIFICATION_ANS_SERVICES_ANS_INCLUDE_BASE_MANAGER_H 18 19 #include "ans_const_define.h" 20 #include "iremote_stub.h" 21 22 namespace OHOS { 23 namespace Notification { 24 class BaseManager { 25 protected: 26 template<typename T> WriteParcelableVector(const std::vector<sptr<T>> & parcelableVector,MessageParcel & reply,ErrCode & result)27 bool WriteParcelableVector(const std::vector<sptr<T>> &parcelableVector, MessageParcel &reply, ErrCode &result) 28 { 29 if (!reply.WriteInt32(result)) { 30 ANS_LOGE("write result failed, ErrCode=%{public}d", result); 31 return false; 32 } 33 34 if (!reply.WriteInt32(parcelableVector.size())) { 35 ANS_LOGE("write ParcelableVector size failed"); 36 return false; 37 } 38 39 for (auto &parcelable : parcelableVector) { 40 if (!reply.WriteStrongParcelable(parcelable)) { 41 ANS_LOGE("write ParcelableVector failed"); 42 return false; 43 } 44 } 45 return true; 46 } 47 48 template<typename T> ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)49 bool ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data) 50 { 51 int32_t infoSize = 0; 52 if (!data.ReadInt32(infoSize)) { 53 ANS_LOGE("Failed to read Parcelable size."); 54 return false; 55 } 56 57 parcelableInfos.clear(); 58 infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM; 59 for (int32_t index = 0; index < infoSize; index++) { 60 sptr<T> info = data.ReadStrongParcelable<T>(); 61 if (info == nullptr) { 62 ANS_LOGE("Failed to read Parcelable infos."); 63 return false; 64 } 65 parcelableInfos.emplace_back(info); 66 } 67 68 return true; 69 } 70 }; 71 } // namespace Notification 72 } // namespace OHOS 73 74 #endif // BASE_NOTIFICATION_ANS_SERVICES_ANS_INCLUDE_BASE_MANAGER_H 75