1 /*
2  * Copyright (c) 2021-2024 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_tag_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     isTestMode = parcel.ReadBool();
50     int32_t bundleTypeData;
51     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, bundleTypeData);
52     bundleType = static_cast<int32_t>(bundleTypeData);
53     if (!parcel.ReadStringVector(&bundleNames)) {
54         TAG_LOGE(AAFwkTag::APPMGR, "read bundleNames failed.");
55         return false;
56     }
57     int32_t processType;
58     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, processType);
59     processType_ = static_cast<ProcessType>(processType);
60     int32_t extensionType;
61     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extensionType);
62     extensionType_ = static_cast<ExtensionAbilityType>(extensionType);
63     appCloneIndex = parcel.ReadInt32();
64     return true;
65 }
66 
Unmarshalling(Parcel & parcel)67 RunningProcessInfo *RunningProcessInfo::Unmarshalling(Parcel &parcel)
68 {
69     RunningProcessInfo *info = new (std::nothrow) RunningProcessInfo();
70     if (info && !info->ReadFromParcel(parcel)) {
71         TAG_LOGW(AAFwkTag::APPMGR, "read from parcel failed");
72         delete info;
73         info = nullptr;
74     }
75     return info;
76 }
77 
Marshalling(Parcel & parcel) const78 bool RunningProcessInfo::Marshalling(Parcel &parcel) const
79 {
80     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(processName_));
81     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pid_));
82     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uid_));
83     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(state_));
84     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isContinuousTask);
85     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepAlive);
86     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isFocused);
87     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isTestProcess);
88     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isAbilityForegrounding);
89     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isTestMode);
90     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(bundleType));
91     if (!parcel.WriteStringVector(bundleNames)) {
92         TAG_LOGE(AAFwkTag::APPMGR, "write bundleNames failed.");
93         return false;
94     }
95     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(processType_));
96     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(extensionType_));
97     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appCloneIndex);
98     return true;
99 }
100 }  // namespace AppExecFwk
101 }  // namespace OHOS
102