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
23namespace OHOS {
24namespace PowerMgr {
25bool 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
45RunningLockInfo* 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
58bool 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} // namespace PowerMgr
68} // namespace OHOS
69