1 /*
2  * Copyright (c) 2022-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 "extension_running_info.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AAFwk {
22 namespace {
23 constexpr int CYCLE_LIMIT = 1000;
24 }
ReadFromParcel(Parcel & parcel)25 bool ExtensionRunningInfo::ReadFromParcel(Parcel &parcel)
26 {
27     std::unique_ptr<AppExecFwk::ElementName> readExtension(parcel.ReadParcelable<AppExecFwk::ElementName>());
28     if (readExtension == nullptr) {
29         return false;
30     }
31     extension = *readExtension;
32     pid = parcel.ReadInt32();
33     uid = parcel.ReadInt32();
34     type = static_cast<AppExecFwk::ExtensionAbilityType>(parcel.ReadInt32());
35     processName = Str16ToStr8(parcel.ReadString16());
36     startTime = parcel.ReadInt32();
37     int32_t clientPackageSize = parcel.ReadInt32();
38     if (clientPackageSize > CYCLE_LIMIT) {
39         TAG_LOGE(AAFwkTag::ABILITYMGR, "clientPackageSize is too large.");
40         return false;
41     }
42     for (int32_t i = 0; i < clientPackageSize; i++) {
43         clientPackage.emplace_back(Str16ToStr8(parcel.ReadString16()));
44     }
45     return true;
46 }
47 
Unmarshalling(Parcel & parcel)48 ExtensionRunningInfo *ExtensionRunningInfo::Unmarshalling(Parcel &parcel)
49 {
50     ExtensionRunningInfo *info = new (std::nothrow) ExtensionRunningInfo();
51     if (info == nullptr) {
52         return nullptr;
53     }
54 
55     if (!info->ReadFromParcel(parcel)) {
56         delete info;
57         info = nullptr;
58     }
59     return info;
60 }
61 
Marshalling(Parcel & parcel) const62 bool ExtensionRunningInfo::Marshalling(Parcel &parcel) const
63 {
64     if (!parcel.WriteParcelable(&extension)) {
65         return false;
66     }
67     if (!parcel.WriteInt32(pid)) {
68         return false;
69     }
70     if (!parcel.WriteInt32(uid)) {
71         return false;
72     }
73     if (!parcel.WriteInt32(static_cast<int32_t>(type))) {
74         return false;
75     }
76     if (!parcel.WriteString16(Str8ToStr16(processName))) {
77         return false;
78     }
79     if (!parcel.WriteInt32(startTime)) {
80         return false;
81     }
82     int32_t clientPackageSize = static_cast<int32_t>(clientPackage.size());
83     if (!parcel.WriteInt32(clientPackageSize)) {
84         return false;
85     }
86     for (std::string package : clientPackage) {
87         if (!parcel.WriteString16(Str8ToStr16(package))) {
88             return false;
89         }
90     }
91     return true;
92 }
93 }  // namespace AAFwk
94 }  // namespace OHOS
95