• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     isDebugApp = parcel.ReadBool();
51     int32_t bundleTypeData;
52     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, bundleTypeData);
53     bundleType = static_cast<int32_t>(bundleTypeData);
54     if (!parcel.ReadStringVector(&bundleNames)) {
55         TAG_LOGE(AAFwkTag::APPMGR, "read bundleNames failed.");
56         return false;
57     }
58     int32_t processType;
59     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, processType);
60     processType_ = static_cast<ProcessType>(processType);
61     int32_t extensionType;
62     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extensionType);
63     extensionType_ = static_cast<ExtensionAbilityType>(extensionType);
64     appCloneIndex = parcel.ReadInt32();
65     instanceKey = Str16ToStr8(parcel.ReadString16());
66     int32_t appModeType;
67     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appModeType);
68     appMode = static_cast<AppExecFwk::MultiAppModeType>(appModeType);
69     int32_t rssData;
70     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rssData);
71     rssValue = static_cast<int32_t>(rssData);
72     int32_t pssData;
73     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pssData);
74     pssValue = static_cast<int32_t>(pssData);
75     return true;
76 }
77 
Unmarshalling(Parcel & parcel)78 RunningProcessInfo *RunningProcessInfo::Unmarshalling(Parcel &parcel)
79 {
80     RunningProcessInfo *info = new (std::nothrow) RunningProcessInfo();
81     if (info && !info->ReadFromParcel(parcel)) {
82         TAG_LOGW(AAFwkTag::APPMGR, "read from parcel failed");
83         delete info;
84         info = nullptr;
85     }
86     return info;
87 }
88 
Marshalling(Parcel & parcel) const89 bool RunningProcessInfo::Marshalling(Parcel &parcel) const
90 {
91     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(processName_));
92     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pid_));
93     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(uid_));
94     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(state_));
95     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isContinuousTask);
96     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isKeepAlive);
97     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isFocused);
98     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isTestProcess);
99     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isAbilityForegrounding);
100     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isTestMode);
101     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isDebugApp);
102     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(bundleType));
103     if (!parcel.WriteStringVector(bundleNames)) {
104         TAG_LOGE(AAFwkTag::APPMGR, "write bundleNames failed.");
105         return false;
106     }
107     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(processType_));
108     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(extensionType_));
109     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appCloneIndex);
110     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(instanceKey));
111     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(appMode));
112     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(rssValue));
113     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pssValue));
114     return true;
115 }
116 }  // namespace AppExecFwk
117 }  // namespace OHOS
118