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_launch_data.h"
17
18 #include "hilog_tag_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 !parcel.WriteParcelable(&profile_) || !parcel.WriteParcelable(&processInfo_)) {
61 return false;
62 }
63 if (!parcel.WriteInt32(recordId_) ||
64 !parcel.WriteInt32(uId_) || !parcel.WriteInt32(appIndex_)) {
65 return false;
66 }
67
68 bool valid = userTestRecord_ ? true : false;
69 if (!parcel.WriteBool(valid)) {
70 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write the flag which indicate whether userTestRecord_ is null");
71 return false;
72 }
73 if (valid) {
74 if (!parcel.WriteParcelable(userTestRecord_.get())) {
75 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write userTestRecord_");
76 return false;
77 }
78 }
79
80 if (!parcel.WriteBool(debugApp_)) {
81 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write debug flag.");
82 return false;
83 }
84
85 if (!parcel.WriteString(perfCmd_)) {
86 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write perf cmd.");
87 return false;
88 }
89
90 if (!parcel.WriteBool(jitEnabled_) || !parcel.WriteBool(isNativeStart_)) {
91 return false;
92 }
93
94 if (!parcel.WriteString(appRunningUniqueId_)) {
95 TAG_LOGE(AAFwkTag::APPMGR, "Marshalling, Failed to write app running unique id.");
96 return false;
97 }
98
99 return MarshallingExtend(parcel);
100 }
101
MarshallingExtend(Parcel & parcel) const102 bool AppLaunchData::MarshallingExtend(Parcel &parcel) const
103 {
104 if (!parcel.WriteBool(isMultiThread_)) {
105 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write is multi thread flag.");
106 return false;
107 }
108
109 if (!parcel.WriteBool(isErrorInfoEnhance_)) {
110 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write is error info enhance flag.");
111 return false;
112 }
113
114 if (!parcel.WriteString(instanceKey_)) {
115 TAG_LOGE(AAFwkTag::APPMGR, "Marshalling, Failed to write instance key.");
116 return false;
117 }
118
119 if (!parcel.WriteBool(isAllowedNWebPreload_)) {
120 TAG_LOGE(AAFwkTag::APPMGR, "Marshalling, Failed to write isAllowedNWebPreload.");
121 return false;
122 }
123
124 if (!parcel.WriteBool(isNeedPreloadModule_)) {
125 TAG_LOGE(AAFwkTag::APPMGR, "Marshalling, Failed to write instance key.");
126 return false;
127 }
128 return true;
129 }
130
ReadFromParcel(Parcel & parcel)131 bool AppLaunchData::ReadFromParcel(Parcel &parcel)
132 {
133 std::unique_ptr<ApplicationInfo> applicationInfoRead(parcel.ReadParcelable<ApplicationInfo>());
134 if (!applicationInfoRead) {
135 TAG_LOGE(AAFwkTag::APPMGR, "failed, applicationInfoRead is nullptr");
136 return false;
137 }
138 applicationInfo_ = *applicationInfoRead;
139
140 std::unique_ptr<Profile> profileRead(parcel.ReadParcelable<Profile>());
141 if (!profileRead) {
142 TAG_LOGE(AAFwkTag::APPMGR, "failed, profileRead is nullptr");
143 return false;
144 }
145 profile_ = *profileRead;
146
147 std::unique_ptr<ProcessInfo> processInfoRead(parcel.ReadParcelable<ProcessInfo>());
148 if (!processInfoRead) {
149 TAG_LOGE(AAFwkTag::APPMGR, "failed, processInfoRead is nullptr");
150 return false;
151 }
152 processInfo_ = *processInfoRead;
153
154 recordId_ = parcel.ReadInt32();
155 uId_ = parcel.ReadInt32();
156 appIndex_ = parcel.ReadInt32();
157
158 bool valid = parcel.ReadBool();
159 if (valid) {
160 userTestRecord_ = std::shared_ptr<UserTestRecord>(parcel.ReadParcelable<UserTestRecord>());
161 if (!userTestRecord_) {
162 TAG_LOGE(AAFwkTag::APPMGR, "failed, userTestRecord is nullptr");
163 return false;
164 }
165 }
166
167 debugApp_ = parcel.ReadBool();
168 perfCmd_ = parcel.ReadString();
169 jitEnabled_ = parcel.ReadBool();
170 isNativeStart_ = parcel.ReadBool();
171 appRunningUniqueId_ = parcel.ReadString();
172 isMultiThread_ = parcel.ReadBool();
173 isErrorInfoEnhance_ = parcel.ReadBool();
174 instanceKey_ = parcel.ReadString();
175 isAllowedNWebPreload_ = parcel.ReadBool();
176 isNeedPreloadModule_ = parcel.ReadBool();
177 return true;
178 }
179
Unmarshalling(Parcel & parcel)180 AppLaunchData *AppLaunchData::Unmarshalling(Parcel &parcel)
181 {
182 AppLaunchData *appLaunchData = new AppLaunchData();
183 if (appLaunchData && !appLaunchData->ReadFromParcel(parcel)) {
184 TAG_LOGW(AAFwkTag::APPMGR, "failed, because ReadFromParcel failed");
185 delete appLaunchData;
186 appLaunchData = nullptr;
187 }
188 return appLaunchData;
189 }
190
Marshalling(Parcel & parcel) const191 bool UserTestRecord::Marshalling(Parcel &parcel) const
192 {
193 if (!parcel.WriteParcelable(&want)) {
194 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write want");
195 return false;
196 }
197
198 auto valid = observer ? true : false;
199 if (!parcel.WriteBool(valid)) {
200 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write the flag which indicate whether observer is null");
201 return false;
202 }
203
204 if (valid) {
205 if (!(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(observer)) {
206 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write observer");
207 return false;
208 }
209 }
210
211 if (!parcel.WriteBool(isFinished)) {
212 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write isFinished");
213 return false;
214 }
215
216 if (!parcel.WriteInt32(userId)) {
217 TAG_LOGE(AAFwkTag::APPMGR, "Failed to write userId");
218 return false;
219 }
220 return true;
221 }
222
Unmarshalling(Parcel & parcel)223 UserTestRecord *UserTestRecord::Unmarshalling(Parcel &parcel)
224 {
225 UserTestRecord *userTestRecord = new (std::nothrow) UserTestRecord();
226 if (userTestRecord && !userTestRecord->ReadFromParcel(parcel)) {
227 TAG_LOGW(AAFwkTag::APPMGR, "failed, because ReadFromParcel failed");
228 delete userTestRecord;
229 userTestRecord = nullptr;
230 }
231 return userTestRecord;
232 }
233
ReadFromParcel(Parcel & parcel)234 bool UserTestRecord::ReadFromParcel(Parcel &parcel)
235 {
236 AAFwk::Want *wantPtr = parcel.ReadParcelable<AAFwk::Want>();
237 if (wantPtr == nullptr) {
238 TAG_LOGE(AAFwkTag::APPMGR, "wantPtr is nullptr");
239 return ERR_INVALID_VALUE;
240 }
241 want = *wantPtr;
242 delete wantPtr;
243
244 auto valid = parcel.ReadBool();
245 if (valid) {
246 observer = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
247 if (!observer) {
248 TAG_LOGE(AAFwkTag::APPMGR, "observer is nullptr");
249 return false;
250 }
251 }
252
253 isFinished = parcel.ReadBool();
254 userId = parcel.ReadInt32();
255 return true;
256 }
257
SetNativeStart(bool isNativeStart)258 void AppLaunchData::SetNativeStart(bool isNativeStart)
259 {
260 isNativeStart_ = isNativeStart;
261 }
262
isNativeStart() const263 bool AppLaunchData::isNativeStart() const
264 {
265 return isNativeStart_;
266 }
267
SetNWebPreload(const bool isAllowedNWebPreload)268 void AppLaunchData::SetNWebPreload(const bool isAllowedNWebPreload)
269 {
270 isAllowedNWebPreload_ = isAllowedNWebPreload;
271 }
272
IsAllowedNWebPreload() const273 bool AppLaunchData::IsAllowedNWebPreload() const
274 {
275 return isAllowedNWebPreload_;
276 }
277
SetIsNeedPreloadModule(bool isNeedPreloadModule)278 void AppLaunchData::SetIsNeedPreloadModule(bool isNeedPreloadModule)
279 {
280 isNeedPreloadModule_ = isNeedPreloadModule;
281 }
282
IsNeedPreloadModule() const283 bool AppLaunchData::IsNeedPreloadModule() const
284 {
285 return isNeedPreloadModule_;
286 }
287 } // namespace AppExecFwk
288 } // namespace OHOS
289