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_process_info.h"
17
18 #include "nlohmann/json.hpp"
19 #include "string_ex.h"
20
21 #include "hilog_wrapper.h"
22 #include "parcel_macro_base.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string JSON_KEY_PROCESSNAME = "processName";
28 const std::string JSON_KEY_PID = "pid";
29 const std::string JSON_KEY_STATE = "state";
30 } // namespace
31
ReadFromParcel(Parcel & parcel)32 bool RunningProcessInfo::ReadFromParcel(Parcel &parcel)
33 {
34 processName_ = Str16ToStr8(parcel.ReadString16());
35 int32_t typeData;
36 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, typeData);
37 pid_ = static_cast<int32_t>(typeData);
38 int32_t uidData;
39 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, uidData);
40 uid_ = static_cast<int32_t>(uidData);
41 int32_t stateData;
42 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, stateData);
43 state_ = static_cast<AppProcessState>(stateData);
44 isContinuousTask = parcel.ReadBool();
45 isKeepAlive = parcel.ReadBool();
46 isFocused = parcel.ReadBool();
47 isTestProcess = parcel.ReadBool();
48 if (!parcel.ReadStringVector(&bundleNames)) {
49 HILOG_ERROR("read bundleNames failed.");
50 return false;
51 }
52 return true;
53 }
54
Unmarshalling(Parcel & parcel)55 RunningProcessInfo *RunningProcessInfo::Unmarshalling(Parcel &parcel)
56 {
57 RunningProcessInfo *info = new (std::nothrow) RunningProcessInfo();
58 if (info && !info->ReadFromParcel(parcel)) {
59 HILOG_WARN("read from parcel failed");
60 delete info;
61 info = nullptr;
62 }
63 return info;
64 }
65
Marshalling(Parcel & parcel) const66 bool RunningProcessInfo::Marshalling(Parcel &parcel) const
67 {
68 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(processName_));
69 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pid_));
70 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uid_));
71 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(state_));
72 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isContinuousTask);
73 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepAlive);
74 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isFocused);
75 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isTestProcess);
76 if (!parcel.WriteStringVector(bundleNames)) {
77 HILOG_ERROR("write bundleNames failed.");
78 return false;
79 }
80 return true;
81 }
82 } // namespace AppExecFwk
83 } // namespace OHOS
84