1 /*
2  * Copyright (c) 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_multi_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 {
ReadFromParcel(Parcel & parcel)26 bool RunningMultiAppInfo::ReadFromParcel(Parcel &parcel)
27 {
28     bundleName = Str16ToStr8(parcel.ReadString16());
29     mode = parcel.ReadInt32();
30     int32_t runningAppClonesSize;
31     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, runningAppClonesSize);
32     if (runningAppClonesSize > MAX_CLONE_APP_NUM) {
33         return false;
34     }
35     for (auto i = 0; i < runningAppClonesSize; i++) {
36         RunningAppClone clone;
37         clone.appCloneIndex = parcel.ReadInt32();
38         clone.uid = parcel.ReadInt32();
39         parcel.ReadInt32Vector(&clone.pids);
40         runningAppClones.emplace_back(clone);
41     }
42 
43     int32_t runningMultiIntanceInfosSize;
44     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, runningMultiIntanceInfosSize);
45     if (runningMultiIntanceInfosSize > MAX_INSTANCE_NUM) {
46         return false;
47     }
48     for (auto i = 0; i < runningMultiIntanceInfosSize; i++) {
49         RunningMultiInstanceInfo instanceInfo;
50         instanceInfo.instanceKey = Str16ToStr8(parcel.ReadString16());
51         instanceInfo.uid = parcel.ReadInt32();
52         parcel.ReadInt32Vector(&instanceInfo.pids);
53         runningMultiIntanceInfos.emplace_back(instanceInfo);
54     }
55     return true;
56 }
57 
Unmarshalling(Parcel & parcel)58 RunningMultiAppInfo *RunningMultiAppInfo::Unmarshalling(Parcel &parcel)
59 {
60     RunningMultiAppInfo *info = new (std::nothrow) RunningMultiAppInfo();
61     if (info && !info->ReadFromParcel(parcel)) {
62         TAG_LOGW(AAFwkTag::APPMGR, "read from parcel failed");
63         delete info;
64         info = nullptr;
65     }
66     return info;
67 }
68 
Marshalling(Parcel & parcel) const69 bool RunningMultiAppInfo::Marshalling(Parcel &parcel) const
70 {
71     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
72     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, mode);
73     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, runningAppClones.size());
74     if (runningAppClones.size() > MAX_CLONE_APP_NUM) {
75         return false;
76     }
77     for (auto &clone : runningAppClones) {
78         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, clone.appCloneIndex);
79         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, clone.uid);
80         if (!parcel.WriteInt32Vector(clone.pids)) {
81             TAG_LOGE(AAFwkTag::APPMGR, "write runningAppClones failed.");
82             return false;
83         }
84     }
85     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, runningMultiIntanceInfos.size());
86     if (runningMultiIntanceInfos.size() > MAX_INSTANCE_NUM) {
87         return false;
88     }
89     for (auto &instanceInfo : runningMultiIntanceInfos) {
90         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(instanceInfo.instanceKey));
91         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, instanceInfo.uid);
92         if (!parcel.WriteInt32Vector(instanceInfo.pids)) {
93             TAG_LOGE(AAFwkTag::APPMGR, "write runningMultiIntanceInfos failed.");
94             return false;
95         }
96     }
97     return true;
98 }
99 }  // namespace AppExecFwk
100 }  // namespace OHOS