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