• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "form_js_info.h"
17 #include "fms_log_wrapper.h"
18 #include "string_ex.h"
19 #include "message_parcel.h"
20 
21 #include "iservice_registry.h"
22 #include "bundle_mgr_interface.h"
23 #include "os_account_manager.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
ReadFromParcel(Parcel & parcel)27 bool FormJsInfo::ReadFromParcel(Parcel &parcel)
28 {
29     formId = parcel.ReadInt64();
30     formName = Str16ToStr8(parcel.ReadString16());
31     bundleName = Str16ToStr8(parcel.ReadString16());
32     abilityName = Str16ToStr8(parcel.ReadString16());
33     moduleName = Str16ToStr8(parcel.ReadString16());
34 
35     formTempFlag = parcel.ReadBool();
36     jsFormCodePath = Str16ToStr8(parcel.ReadString16());
37     MessageParcel* msgParcel = static_cast<MessageParcel*>(&parcel);
38     int32_t formDataLength = parcel.ReadInt32();
39     HILOG_INFO("ReadFromParcel data length is %{public}d , formId:%{public}" PRId64, formDataLength, formId);
40     if (formDataLength > BIG_DATA) {
41         HILOG_INFO("data length > 32k");
42         const void *rawData = msgParcel->ReadRawData(formDataLength);
43         if (rawData == nullptr) {
44             HILOG_INFO("rawData is nullptr");
45             return false;
46         }
47         formData = std::string(static_cast<const char*>(rawData), formDataLength);
48     } else {
49         formData = Str16ToStr8(parcel.ReadString16());
50     }
51 
52     formSrc = Str16ToStr8(parcel.ReadString16());
53     formWindow.designWidth = parcel.ReadInt32();
54     formWindow.autoDesignWidth = parcel.ReadBool();
55 
56     versionCode = parcel.ReadUint32();
57     versionName = Str16ToStr8(parcel.ReadString16());
58     compatibleVersion = parcel.ReadUint32();
59     int32_t typeData = parcel.ReadInt32();
60     type = static_cast<FormType>(typeData);
61     uiSyntax = static_cast<FormType>(parcel.ReadInt32());
62     isDynamic = parcel.ReadBool();
63     transparencyEnabled = parcel.ReadBool();
64 
65     std::unique_ptr<FormProviderData> bindingData(parcel.ReadParcelable<FormProviderData>());
66     if (bindingData == nullptr) {
67         return false;
68     }
69     formProviderData = *bindingData;
70 
71     ReadImageData(parcel);
72     ReadPkgNameMap(parcel);
73     return true;
74 }
75 
Unmarshalling(Parcel & parcel)76 FormJsInfo *FormJsInfo::Unmarshalling(Parcel &parcel)
77 {
78     std::unique_ptr<FormJsInfo> formJsInfo = std::make_unique<FormJsInfo>();
79     if (formJsInfo && !formJsInfo->ReadFromParcel(parcel)) {
80         formJsInfo = nullptr;
81     }
82     return formJsInfo.release();
83 }
84 
Marshalling(Parcel & parcel) const85 bool FormJsInfo::Marshalling(Parcel &parcel) const
86 {
87     // write formId
88     if (!parcel.WriteInt64(formId)) {
89         return false;
90     }
91     // write formName
92     if (!parcel.WriteString16(Str8ToStr16(formName))) {
93         return false;
94     }
95     // write bundleName
96     if (!parcel.WriteString16(Str8ToStr16(bundleName))) {
97         return false;
98     }
99     // write abilityName
100     if (!parcel.WriteString16(Str8ToStr16(abilityName))) {
101         return false;
102     }
103 
104     // write moduleName
105     if (!parcel.WriteString16(Str8ToStr16(moduleName))) {
106         return false;
107     }
108 
109     // write tempFlag
110     if (!parcel.WriteBool(formTempFlag)) {
111         return false;
112     }
113 
114     // write jsFormCodePath
115     if (!parcel.WriteString16(Str8ToStr16(jsFormCodePath))) {
116         return false;
117     }
118 
119     // write formData and formSrc
120     if (!WriteFormData(parcel) || !parcel.WriteString16(Str8ToStr16(formSrc))) {
121         return false;
122     }
123 
124     // write formWindow
125     if (!parcel.WriteInt32(formWindow.designWidth) || !parcel.WriteBool(formWindow.autoDesignWidth)) {
126         return false;
127     }
128 
129     // write version
130     if (!parcel.WriteUint32(versionCode) ||
131         !parcel.WriteString16(Str8ToStr16(versionName)) ||
132         !parcel.WriteUint32(compatibleVersion)) {
133         return false;
134     }
135     if (!parcel.WriteInt32(static_cast<int32_t>(type))) {
136         return false;
137     }
138     if (!parcel.WriteInt32(static_cast<int32_t>(uiSyntax))) {
139         return false;
140     }
141     if (!parcel.WriteBool(isDynamic) || !parcel.WriteBool(transparencyEnabled)) {
142         return false;
143     }
144     if (!WriteObjects(parcel)) {
145         return false;
146     }
147     return true;
148 }
149 
WriteFormData(Parcel & parcel) const150 bool FormJsInfo::WriteFormData(Parcel &parcel) const
151 {
152     MessageParcel* msgParcel = static_cast<MessageParcel *>(&parcel);
153     int32_t formDataLength = static_cast<int32_t>(formData.length());
154     parcel.WriteInt32(formDataLength);
155     if (formDataLength > BIG_DATA) {
156         HILOG_INFO("WriteFormData data length is %{public}d", formDataLength);
157         return msgParcel->WriteRawData(formData.c_str(), formDataLength);
158     } else {
159         return parcel.WriteString16(Str8ToStr16(formData));
160     }
161 }
162 
WriteObjects(Parcel & parcel) const163 bool FormJsInfo::WriteObjects(Parcel &parcel) const
164 {
165     // write formProviderData
166     if (!parcel.WriteParcelable(&formProviderData)) {
167         return false;
168     }
169 
170     if (!WriteImageData(parcel)) {
171         return false;
172     }
173 
174     if (!WritePkgNameMap(parcel)) {
175         return false;
176     }
177     return true;
178 }
179 
WriteImageData(Parcel & parcel) const180 bool FormJsInfo::WriteImageData(Parcel &parcel) const
181 {
182     HILOG_DEBUG("call");
183     auto imageDateState = formProviderData.GetImageDataState();
184     if (!parcel.WriteInt32(imageDateState)) {
185         return false;
186     }
187     HILOG_DEBUG("imageDateState is %{public}d", imageDateState);
188     switch (imageDateState) {
189         case FormProviderData::IMAGE_DATA_STATE_ADDED: {
190             auto sharedImageMap = formProviderData.GetImageDataMap();
191             auto size = sharedImageMap.size();
192             if (!parcel.WriteInt32(size)) {
193                 return false;
194             }
195             if (size > IMAGE_DATA_THRESHOLD) {
196                 HILOG_INFO("unexpected image number %{public}zu", size);
197                 break;
198             }
199             for (auto entry : sharedImageMap) {
200                 if (!parcel.WriteParcelable(entry.second.first)) {
201                     return false;
202                 }
203                 if (!parcel.WriteString16(Str8ToStr16(entry.first))) {
204                     return false;
205                 }
206             }
207             break;
208         }
209         case FormProviderData::IMAGE_DATA_STATE_NO_OPERATION:
210             break;
211         case FormProviderData::IMAGE_DATA_STATE_REMOVED:
212             break;
213         default: {
214             HILOG_WARN("unexpected imageDateState %{public}d", imageDateState);
215             break;
216         }
217     }
218     HILOG_DEBUG("end");
219     return true;
220 }
221 
ReadImageData(Parcel & parcel)222 void FormJsInfo::ReadImageData(Parcel &parcel)
223 {
224     HILOG_DEBUG("call");
225     auto imageDateState = parcel.ReadInt32();
226     HILOG_DEBUG("imageDateState is %{public}d", imageDateState);
227     switch (imageDateState) {
228         case FormProviderData::IMAGE_DATA_STATE_ADDED: {
229             auto size = parcel.ReadInt32();
230             HILOG_INFO("image numer is %{public}d", size);
231             if (size > IMAGE_DATA_THRESHOLD) {
232                 HILOG_WARN("unexpected image number %{public}d", size);
233                 break;
234             }
235             for (auto i = 0; i < size; i++) {
236                 sptr<FormAshmem> formAshmem = parcel.ReadParcelable<FormAshmem>();
237                 if (formAshmem == nullptr) {
238                     HILOG_ERROR("null ashmem");
239                     return;
240                 }
241                 auto picName = Str16ToStr8(parcel.ReadString16());
242                 HILOG_INFO("picName:%{public}s", picName.c_str());
243                 imageDataMap[picName] = formAshmem;
244             }
245             break;
246         }
247         case FormProviderData::IMAGE_DATA_STATE_NO_OPERATION:
248             break;
249         case FormProviderData::IMAGE_DATA_STATE_REMOVED:
250             break;
251         default: {
252             HILOG_WARN("unexpected imageDateState %{public}d", imageDateState);
253             break;
254         }
255     }
256     HILOG_DEBUG("end");
257     return;
258 }
259 
ConvertRawImageData()260 bool FormJsInfo::ConvertRawImageData()
261 {
262     HILOG_DEBUG("call");
263     if (!formProviderData.ConvertRawImageData()) {
264         return false;
265     }
266     auto sharedImageMap = formProviderData.GetImageDataMap();
267     auto size = sharedImageMap.size();
268     if (size > IMAGE_DATA_THRESHOLD) {
269         HILOG_ERROR("unexpected image number %{public}zu", size);
270         return false;
271     }
272     for (const auto &entry : sharedImageMap) {
273         imageDataMap[entry.first] = entry.second.first;
274     }
275     return true;
276 }
277 
WritePkgNameMap(Parcel & parcel) const278 bool FormJsInfo::WritePkgNameMap(Parcel &parcel) const
279 {
280     HILOG_DEBUG("call");
281     std::vector<std::string> keys;
282     std::vector<std::string> values;
283 
284     for (const auto &pkgNameInfo : modulePkgNameMap) {
285         keys.emplace_back(pkgNameInfo.first);
286         values.emplace_back(pkgNameInfo.second);
287     }
288 
289     parcel.WriteStringVector(keys);
290     parcel.WriteStringVector(values);
291     return true;
292 }
293 
ReadPkgNameMap(Parcel & parcel)294 void FormJsInfo::ReadPkgNameMap(Parcel &parcel)
295 {
296     HILOG_DEBUG("call");
297     std::vector<std::string> keys;
298     std::vector<std::string> values;
299     if (!parcel.ReadStringVector(&keys)) {
300         HILOG_ERROR("ReadStringVector for keys failed");
301         return;
302     }
303     if (!parcel.ReadStringVector(&values)) {
304         HILOG_ERROR("ReadStringVector for values failed");
305         return;
306     }
307     size_t keySize = keys.size();
308     size_t valueSize = values.size();
309     if (keySize != valueSize) {
310         HILOG_ERROR("ReadFromParcel failed, invalid size");
311         return;
312     }
313 
314     std::string key;
315     std::string val;
316     for (size_t index = 0; index < keySize; index++) {
317         key = keys.at(index);
318         val = values.at(index);
319         modulePkgNameMap.emplace(key, val);
320     }
321 }
322 
CopyFormJsInfoWithoutFormData() const323 FormJsInfo FormJsInfo::CopyFormJsInfoWithoutFormData() const
324 {
325     FormJsInfo copyFormJsInfo;
326     copyFormJsInfo.formName = formName;
327     copyFormJsInfo.bundleName = bundleName;
328     copyFormJsInfo.abilityName = abilityName;
329     copyFormJsInfo.moduleName = moduleName;
330     copyFormJsInfo.modulePkgNameMap = modulePkgNameMap;
331     copyFormJsInfo.formTempFlag = formTempFlag;
332     copyFormJsInfo.jsFormCodePath = jsFormCodePath;
333     copyFormJsInfo.formData = "";
334     copyFormJsInfo.imageDataMap = {};
335     copyFormJsInfo.formProviderData = FormProviderData();
336     copyFormJsInfo.formSrc = "";
337     copyFormJsInfo.formWindow = formWindow;
338     copyFormJsInfo.versionCode = versionCode;
339     copyFormJsInfo.versionName = versionName;
340     copyFormJsInfo.compatibleVersion = compatibleVersion;
341     copyFormJsInfo.type = type;
342     copyFormJsInfo.uiSyntax = uiSyntax;
343     copyFormJsInfo.isDynamic = isDynamic;
344     copyFormJsInfo.transparencyEnabled = transparencyEnabled;
345     return copyFormJsInfo;
346 }
347 } // namespace AppExecFwk
348 } // namespace OHOS