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 "app_process_data.h"
17
18 #include "hilog_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 HILOG_ERROR("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
65 return true;
66 }
67
ReadFromParcel(Parcel & parcel)68 bool AppProcessData::ReadFromParcel(Parcel &parcel)
69 {
70 processName = Str16ToStr8(parcel.ReadString16());
71
72 appState = static_cast<ApplicationState>(parcel.ReadInt32());
73
74 pid = parcel.ReadInt32();
75
76 ReadFromParcelAppData(appDatas, parcel);
77
78 isFocused = parcel.ReadBool();
79
80 return true;
81 }
82
Unmarshalling(Parcel & parcel)83 AppProcessData *AppProcessData::Unmarshalling(Parcel &parcel)
84 {
85 AppProcessData *appProcessData = new (std::nothrow) AppProcessData();
86 if (appProcessData && !appProcessData->ReadFromParcel(parcel)) {
87 HILOG_WARN("failed, because ReadFromParcel failed");
88 delete appProcessData;
89 appProcessData = nullptr;
90 }
91 return appProcessData;
92 }
93 } // namespace AppExecFwk
94 } // namespace OHOS
95