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 isAbilityForegrounding = parcel.ReadBool();
49 if (!parcel.ReadStringVector(&bundleNames)) {
50 HILOG_ERROR("read bundleNames failed.");
51 return false;
52 }
53 int32_t processType;
54 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, processType);
55 processType_ = static_cast<ProcessType>(processType);
56 int32_t extensionType;
57 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extensionType);
58 extensionType_ = static_cast<ExtensionAbilityType>(extensionType);
59 return true;
60 }
61
Unmarshalling(Parcel & parcel)62 RunningProcessInfo *RunningProcessInfo::Unmarshalling(Parcel &parcel)
63 {
64 RunningProcessInfo *info = new (std::nothrow) RunningProcessInfo();
65 if (info && !info->ReadFromParcel(parcel)) {
66 HILOG_WARN("read from parcel failed");
67 delete info;
68 info = nullptr;
69 }
70 return info;
71 }
72
Marshalling(Parcel & parcel) const73 bool RunningProcessInfo::Marshalling(Parcel &parcel) const
74 {
75 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(processName_));
76 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pid_));
77 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uid_));
78 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(state_));
79 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isContinuousTask);
80 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepAlive);
81 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isFocused);
82 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isTestProcess);
83 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isAbilityForegrounding);
84 if (!parcel.WriteStringVector(bundleNames)) {
85 HILOG_ERROR("write bundleNames failed.");
86 return false;
87 }
88 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(processType_));
89 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(extensionType_));
90 return true;
91 }
92 } // namespace AppExecFwk
93 } // namespace OHOS
94