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_launch_data.h"
17
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
SetApplicationInfo(const ApplicationInfo & info)22 void AppLaunchData::SetApplicationInfo(const ApplicationInfo &info)
23 {
24 applicationInfo_ = info;
25 }
26
SetProfile(const Profile & profile)27 void AppLaunchData::SetProfile(const Profile &profile)
28 {
29 profile_ = profile;
30 }
31
SetProcessInfo(const ProcessInfo & info)32 void AppLaunchData::SetProcessInfo(const ProcessInfo &info)
33 {
34 processInfo_ = info;
35 }
36
SetRecordId(const int32_t recordId)37 void AppLaunchData::SetRecordId(const int32_t recordId)
38 {
39 recordId_ = recordId;
40 }
41
SetUId(const int32_t uId)42 void AppLaunchData::SetUId(const int32_t uId)
43 {
44 uId_ = uId;
45 }
46
SetAppIndex(const int32_t appIndex)47 void AppLaunchData::SetAppIndex(const int32_t appIndex)
48 {
49 appIndex_ = appIndex;
50 }
51
SetUserTestInfo(const std::shared_ptr<UserTestRecord> & record)52 void AppLaunchData::SetUserTestInfo(const std::shared_ptr<UserTestRecord> &record)
53 {
54 userTestRecord_ = record;
55 }
56
Marshalling(Parcel & parcel) const57 bool AppLaunchData::Marshalling(Parcel &parcel) const
58 {
59 if (!parcel.WriteParcelable(&applicationInfo_)) {
60 return false;
61 }
62 if (!parcel.WriteParcelable(&profile_)) {
63 return false;
64 }
65 if (!parcel.WriteParcelable(&processInfo_)) {
66 return false;
67 }
68 if (!parcel.WriteInt32(recordId_)) {
69 return false;
70 }
71 if (!parcel.WriteInt32(uId_)) {
72 return false;
73 }
74 if (!parcel.WriteInt32(appIndex_)) {
75 return false;
76 }
77
78 bool valid = userTestRecord_ ? true : false;
79 if (!parcel.WriteBool(valid)) {
80 HILOG_ERROR("Failed to write the flag which indicate whether userTestRecord_ is null");
81 return false;
82 }
83 if (valid) {
84 if (!parcel.WriteParcelable(userTestRecord_.get())) {
85 HILOG_ERROR("Failed to write userTestRecord_");
86 return false;
87 }
88 }
89 return true;
90 }
91
ReadFromParcel(Parcel & parcel)92 bool AppLaunchData::ReadFromParcel(Parcel &parcel)
93 {
94 std::unique_ptr<ApplicationInfo> applicationInfoRead(parcel.ReadParcelable<ApplicationInfo>());
95 if (!applicationInfoRead) {
96 HILOG_ERROR("failed, applicationInfoRead is nullptr");
97 return false;
98 }
99 applicationInfo_ = *applicationInfoRead;
100
101 std::unique_ptr<Profile> profileRead(parcel.ReadParcelable<Profile>());
102 if (!profileRead) {
103 HILOG_ERROR("failed, profileRead is nullptr");
104 return false;
105 }
106 profile_ = *profileRead;
107
108 std::unique_ptr<ProcessInfo> processInfoRead(parcel.ReadParcelable<ProcessInfo>());
109 if (!processInfoRead) {
110 HILOG_ERROR("failed, processInfoRead is nullptr");
111 return false;
112 }
113 processInfo_ = *processInfoRead;
114
115 recordId_ = parcel.ReadInt32();
116 uId_ = parcel.ReadInt32();
117 appIndex_ = parcel.ReadInt32();
118
119 bool valid = parcel.ReadBool();
120 if (valid) {
121 userTestRecord_ = std::shared_ptr<UserTestRecord>(parcel.ReadParcelable<UserTestRecord>());
122 if (!userTestRecord_) {
123 HILOG_ERROR("failed, userTestRecord is nullptr");
124 return false;
125 }
126 }
127 return true;
128 }
129
Unmarshalling(Parcel & parcel)130 AppLaunchData *AppLaunchData::Unmarshalling(Parcel &parcel)
131 {
132 AppLaunchData *appLaunchData = new AppLaunchData();
133 if (appLaunchData && !appLaunchData->ReadFromParcel(parcel)) {
134 HILOG_WARN("failed, because ReadFromParcel failed");
135 delete appLaunchData;
136 appLaunchData = nullptr;
137 }
138 return appLaunchData;
139 }
140
Marshalling(Parcel & parcel) const141 bool UserTestRecord::Marshalling(Parcel &parcel) const
142 {
143 if (!parcel.WriteParcelable(&want)) {
144 HILOG_ERROR("Failed to write want");
145 return false;
146 }
147
148 auto valid = observer ? true : false;
149 if (!parcel.WriteBool(valid)) {
150 HILOG_ERROR("Failed to write the flag which indicate whether observer is null");
151 return false;
152 }
153
154 if (valid) {
155 if (!(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(observer)) {
156 HILOG_ERROR("Failed to write observer");
157 return false;
158 }
159 }
160
161 if (!parcel.WriteBool(isFinished)) {
162 HILOG_ERROR("Failed to write isFinished");
163 return false;
164 }
165
166 if (!parcel.WriteInt32(userId)) {
167 HILOG_ERROR("Failed to write userId");
168 return false;
169 }
170 return true;
171 }
172
Unmarshalling(Parcel & parcel)173 UserTestRecord *UserTestRecord::Unmarshalling(Parcel &parcel)
174 {
175 UserTestRecord *userTestRecord = new (std::nothrow) UserTestRecord();
176 if (userTestRecord && !userTestRecord->ReadFromParcel(parcel)) {
177 HILOG_WARN("failed, because ReadFromParcel failed");
178 delete userTestRecord;
179 userTestRecord = nullptr;
180 }
181 return userTestRecord;
182 }
183
ReadFromParcel(Parcel & parcel)184 bool UserTestRecord::ReadFromParcel(Parcel &parcel)
185 {
186 AAFwk::Want *wantPtr = parcel.ReadParcelable<AAFwk::Want>();
187 if (wantPtr == nullptr) {
188 HILOG_ERROR("wantPtr is nullptr");
189 return ERR_INVALID_VALUE;
190 }
191 want = *wantPtr;
192 delete wantPtr;
193
194 auto valid = parcel.ReadBool();
195 if (valid) {
196 observer = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
197 if (!observer) {
198 HILOG_ERROR("observer is nullptr");
199 return false;
200 }
201 }
202
203 isFinished = parcel.ReadBool();
204 userId = parcel.ReadInt32();
205 return true;
206 }
207 } // namespace AppExecFwk
208 } // namespace OHOS
209