• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-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 #ifndef BASE_NOTIFICATION_ANS_STANDARD_CORE_IPC_COMMON_UTILS_H
16 #define BASE_NOTIFICATION_ANS_STANDARD_CORE_IPC_COMMON_UTILS_H
17 
18 #include <securec.h>
19 
20 #include "ans_log_wrapper.h"
21 #include "ans_const_define.h"
22 #include "ipc_skeleton.h"
23 #include "iremote_broker.h"
24 #include "notification_constant.h"
25 
26 namespace OHOS {
27 namespace Notification {
28 class AnsIpcCommonUtils {
29 public:
30     template<typename T>
WriteParcelableVector(const std::vector<std::shared_ptr<T>> & parcelableVector,Parcel & data)31     static bool WriteParcelableVector(const std::vector<std::shared_ptr<T>> &parcelableVector, Parcel &data)
32     {
33         if (!data.WriteInt32(parcelableVector.size())) {
34             ANS_LOGE("Failed to write ParcelableVector size.");
35             return false;
36         }
37 
38         for (auto &parcelable : parcelableVector) {
39             if (!data.WriteParcelable(parcelable.get())) {
40                 ANS_LOGE("Failed to write ParcelableVector");
41                 return false;
42             }
43         }
44         return true;
45     }
46 
47     template<typename T>
ReadParcelableVector(std::vector<std::shared_ptr<T>> & parcelableInfos,Parcel & data)48     static bool ReadParcelableVector(std::vector<std::shared_ptr<T>> &parcelableInfos, Parcel &data)
49     {
50         int32_t infoSize = 0;
51         if (!data.ReadInt32(infoSize)) {
52             ANS_LOGE("Failed to read Parcelable size.");
53             return false;
54         }
55         infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
56         parcelableInfos.clear();
57         for (int32_t index = 0; index < infoSize; index++) {
58             std::shared_ptr<T> info = std::shared_ptr<T>(data.ReadParcelable<T>());
59             parcelableInfos.emplace_back(info);
60         }
61 
62         return true;
63     }
64 };
65 }  // namespace Notification
66 }  // namespace OHOS
67 
68 #endif  // BASE_NOTIFICATION_ANS_STANDARD_CORE_IPC_COMMON_UTILS_H