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 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
66 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(instanceKey));
67 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
68
69 return true;
70 }
71
ReadFromParcel(Parcel & parcel)72 bool AppProcessData::ReadFromParcel(Parcel &parcel)
73 {
74 processName = Str16ToStr8(parcel.ReadString16());
75
76 appState = static_cast<ApplicationState>(parcel.ReadInt32());
77
78 pid = parcel.ReadInt32();
79
80 ReadFromParcelAppData(appDatas, parcel);
81
82 isFocused = parcel.ReadBool();
83 parcel.ReadInt32Vector(&renderPids);
84 appIndex = parcel.ReadInt32();
85 instanceKey = Str16ToStr8(parcel.ReadString16());
86 bundleName = Str16ToStr8(parcel.ReadString16());
87
88 return true;
89 }
90
Unmarshalling(Parcel & parcel)91 AppProcessData *AppProcessData::Unmarshalling(Parcel &parcel)
92 {
93 AppProcessData *appProcessData = new (std::nothrow) AppProcessData();
94 if (appProcessData && !appProcessData->ReadFromParcel(parcel)) {
95 TAG_LOGW(AAFwkTag::APPMGR, "failed, because ReadFromParcel failed");
96 delete appProcessData;
97 appProcessData = nullptr;
98 }
99 return appProcessData;
100 }
101 } // namespace AppExecFwk
102 } // namespace OHOS
103