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 "app_process_data.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 #include "nlohmann/json.hpp"
21 #include "string_ex.h"
22 #include "parcel_macro_base.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 constexpr int32_t CYCLE_LIMIT = 1000;
ReadFromParcelAppData(std::vector<AppData> & appDatas,Parcel & parcel)28 bool ReadFromParcelAppData(std::vector<AppData> &appDatas, Parcel &parcel)
29 {
30     int32_t appDataSize;
31     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appDataSize);
32     if (appDataSize > CYCLE_LIMIT) {
33         TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
34         return false;
35     }
36     for (auto i = 0; i < appDataSize; i++) {
37         AppData appDataInfo;
38         std::string appName = Str16ToStr8(parcel.ReadString16());
39         int32_t uid = parcel.ReadInt32();
40         appDataInfo.appName = appName;
41         appDataInfo.uid = uid;
42         appDatas.emplace_back(appDataInfo);
43     }
44     return true;
45 }
46 }  // namespace
47 
Marshalling(Parcel & parcel) const48 bool AppProcessData::Marshalling(Parcel &parcel) const
49 {
50     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(processName));
51 
52     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(appState));
53 
54     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pid);
55 
56     const auto appDataSize = static_cast<int32_t>(appDatas.size());
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appDataSize);
58     for (auto i = 0; i < appDataSize; i++) {
59         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(appDatas[i].appName));
60         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appDatas[i].uid);
61     }
62 
63     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isFocused);
64     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32Vector, parcel, renderPids);
65 
66     return true;
67 }
68 
ReadFromParcel(Parcel & parcel)69 bool AppProcessData::ReadFromParcel(Parcel &parcel)
70 {
71     processName = Str16ToStr8(parcel.ReadString16());
72 
73     appState = static_cast<ApplicationState>(parcel.ReadInt32());
74 
75     pid = parcel.ReadInt32();
76 
77     ReadFromParcelAppData(appDatas, parcel);
78 
79     isFocused = parcel.ReadBool();
80     parcel.ReadInt32Vector(&renderPids);
81 
82     return true;
83 }
84 
Unmarshalling(Parcel & parcel)85 AppProcessData *AppProcessData::Unmarshalling(Parcel &parcel)
86 {
87     AppProcessData *appProcessData = new (std::nothrow) AppProcessData();
88     if (appProcessData && !appProcessData->ReadFromParcel(parcel)) {
89         TAG_LOGW(AAFwkTag::APPMGR, "failed, because ReadFromParcel failed");
90         delete appProcessData;
91         appProcessData = nullptr;
92     }
93     return appProcessData;
94 }
95 }  // namespace AppExecFwk
96 }  // namespace OHOS
97