1 /*
2 * Copyright (c) 2021-2022 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 #include "running_lock_info.h"
17
18 #include "new"
19 #include "power_log.h"
20 #include "power_common.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
ReadFromParcel(Parcel & parcel)25 bool RunningLockInfo::ReadFromParcel(Parcel& parcel)
26 {
27 uint32_t readType;
28 std::u16string u16Name;
29 std::u16string u16BundleName;
30 int32_t readPid;
31 int32_t readUid;
32 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, String16, u16Name, false);
33 name = Str16ToStr8(u16Name);
34 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, Uint32, readType, false);
35 type = static_cast<RunningLockType>(readType);
36 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, String16, u16BundleName, false);
37 bundleName = Str16ToStr8(u16BundleName);
38 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, Int32, readPid, false);
39 pid = readPid;
40 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, Int32, readUid, false);
41 uid = readUid;
42 return true;
43 }
44
Unmarshalling(Parcel & parcel)45 RunningLockInfo* RunningLockInfo::Unmarshalling(Parcel& parcel)
46 {
47 RunningLockInfo* info = new RunningLockInfo();
48 if (info == nullptr) {
49 return nullptr;
50 }
51 if (!info->ReadFromParcel(parcel)) {
52 delete info;
53 return nullptr;
54 }
55 return info;
56 }
57
Marshalling(Parcel & parcel) const58 bool RunningLockInfo::Marshalling(Parcel& parcel) const
59 {
60 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, String16, Str8ToStr16(name), false);
61 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, Uint32, static_cast<uint32_t>(type), false);
62 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, String16, Str8ToStr16(bundleName), false);
63 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, Int32, static_cast<int32_t>(pid), false);
64 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, Int32, static_cast<int32_t>(uid), false);
65 return true;
66 }
67
Marshalling(Parcel & parcel) const68 bool VectorPair::Marshalling(Parcel& parcel) const
69 {
70 constexpr uint32_t MAX_PROXY_RUNNINGLOCK_NUM = 2000;
71 size_t size = processInfos_.size();
72 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, Int32, size, false);
73 if (size > MAX_PROXY_RUNNINGLOCK_NUM) {
74 POWER_HILOGE(FEATURE_RUNNING_LOCK, "size exceed limit, size=%{public}zu", size);
75 return false;
76 }
77 for (size_t i = 0; i < size; ++i) {
78 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, Int32, processInfos_[i].first, false);
79 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(parcel, Int32, processInfos_[i].second, false);
80 }
81 return true;
82 }
83
Unmarshalling(Parcel & parcel)84 VectorPair* VectorPair::Unmarshalling(Parcel& parcel)
85 {
86 constexpr int32_t MAX_PROXY_RUNNINGLOCK_NUM = 2000;
87 auto vectorPairPtr = std::make_unique<VectorPair>();
88
89 int32_t size {0};
90 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, Int32, size, nullptr);
91 if (size <= 0) {
92 POWER_HILOGW(COMP_FWK, "size is negative, size=%{public}d", size);
93 return nullptr;
94 }
95 if (size > MAX_PROXY_RUNNINGLOCK_NUM) {
96 POWER_HILOGW(COMP_FWK, "size exceed limit, size=%{public}d", size);
97 return nullptr;
98 }
99 vectorPairPtr->processInfos_.resize(size);
100 for (int i = 0; i < size; ++i) {
101 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, Int32, vectorPairPtr->processInfos_[i].first, nullptr);
102 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(parcel, Int32, vectorPairPtr->processInfos_[i].second, nullptr);
103 }
104 return vectorPairPtr.release();
105 }
106 } // namespace PowerMgr
107 } // namespace OHOS
108