• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <map>
17 #include <mutex>
18 #include <set>
19 
20 #include "extension_form_profile.h"
21 #include "json_util.h"
22 #include "nlohmann/json.hpp"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 int32_t g_parseResult = ERR_OK;
28 std::mutex g_mutex;
29 
30 const int8_t MAX_FORM_NAME = 127;
31 const int8_t DEFAULT_RECT_SHAPE = 1;
32 const int8_t DEFAULT_CONDITION_TYPE = 0;
33 #ifndef FORM_DIMENSION_2_3
34 const int8_t DIMENSION_2_3 = 8;
35 #endif
36 #ifndef FORM_DIMENSION_3_3
37 const int8_t DIMENSION_3_3 = 9;
38 #endif
39 constexpr const char* FORM_COLOR_MODE_MAP_KEY[] = {
40     "auto",
41     "dark",
42     "light"
43 };
44 const FormsColorMode FORM_COLOR_MODE_MAP_VALUE[] = {
45     FormsColorMode::AUTO_MODE,
46     FormsColorMode::DARK_MODE,
47     FormsColorMode::LIGHT_MODE
48 };
49 constexpr const char* FORM_RENDERING_MODE_MAP_KEY[] = {
50     "autoColor",
51     "fullColor",
52     "singleColor"
53 };
54 const FormsRenderingMode FORM_RENDERING_MODE_MAP_VALUE[] = {
55     FormsRenderingMode::AUTO_COLOR,
56     FormsRenderingMode::FULL_COLOR,
57     FormsRenderingMode::SINGLE_COLOR
58 };
59 constexpr const char* DIMENSION_MAP_KEY[] = {
60     "1*2",
61     "2*2",
62     "2*4",
63     "4*4",
64     "2*1",
65     "1*1",
66     "6*4",
67     "2*3",
68     "3*3"
69 };
70 const int32_t DIMENSION_MAP_VALUE[] = {
71     1,
72     2,
73     3,
74     4,
75     5,
76     6,
77     7,
78     8,
79     9
80 };
81 constexpr const char* SHAPE_MAP_KEY[] = {
82     "rect",
83     "circle"
84 };
85 const int32_t SHAPE_MAP_VALUE[] = {
86     1,
87     2
88 };
89 constexpr const char* CONDITION_MAP_KEY[] = {
90     "network",
91 };
92 const int32_t CONDITION_MAP_VALUE[] = {
93     1,
94 };
95 constexpr const char* FORM_TYPE_MAP_KEY[] = {
96     "JS",
97     "eTS"
98 };
99 const FormType FORM_TYPE_MAP_VALUE[] = {
100     FormType::JS,
101     FormType::ETS
102 };
103 
104 constexpr const char* UI_SYNTAX_MAP_KEY[] = {
105     "hml",
106     "arkts"
107 };
108 const FormType UI_SYNTAX_MAP_VALUE[] = {
109     FormType::JS,
110     FormType::ETS
111 };
112 
113 struct Window {
114     int32_t designWidth = 720;
115     bool autoDesignWidth = false;
116 };
117 
118 constexpr char CHAR_COLON = ':';
119 
120 struct Metadata {
121     std::string name;
122     std::string value;
123 };
124 
125 struct ExtensionFormProfileInfo {
126     std::string name;
127     std::string displayName;
128     std::string description;
129     std::string src;
130     Window window;
131     std::string colorMode = "auto";
132     std::string renderingMode = "fullColor";
133     std::string formConfigAbility;
134     std::string type = "JS";
135     std::string uiSyntax = "hml";
136     std::string scheduledUpdateTime = "";
137     std::string multiScheduledUpdateTime = "";
138     int32_t updateDuration = 0;
139     std::string defaultDimension;
140     bool formVisibleNotify = false;
141     bool isDefault = false;
142     bool updateEnabled = false;
143     bool dataProxyEnabled = false;
144     bool isDynamic = true;
145     bool transparencyEnabled = false;
146     bool fontScaleFollowSystem = true;
147     bool enableBlurBackground = false;
148     std::vector<std::string> supportShapes {};
149     std::vector<std::string> supportDimensions {};
150     std::vector<std::string> conditionUpdate {};
151     std::vector<Metadata> metadata {};
152     std::vector<std::string> previewImages {};
153 };
154 
155 struct ExtensionFormProfileInfoStruct {
156     int32_t privacyLevel = 0;
157     std::vector<ExtensionFormProfileInfo> forms {};
158 };
159 
from_json(const nlohmann::json & jsonObject,Metadata & metadata)160 void from_json(const nlohmann::json &jsonObject, Metadata &metadata)
161 {
162     const auto &jsonObjectEnd = jsonObject.end();
163     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
164         jsonObjectEnd,
165         ExtensionFormProfileReader::METADATA_NAME,
166         metadata.name,
167         false,
168         g_parseResult);
169     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
170         jsonObjectEnd,
171         ExtensionFormProfileReader::METADATA_VALUE,
172         metadata.value,
173         false,
174         g_parseResult);
175 }
176 
from_json(const nlohmann::json & jsonObject,Window & window)177 void from_json(const nlohmann::json &jsonObject, Window &window)
178 {
179     const auto &jsonObjectEnd = jsonObject.end();
180     GetValueIfFindKey<int32_t>(jsonObject,
181         jsonObjectEnd,
182         ExtensionFormProfileReader::WINDOW_DESIGN_WIDTH,
183         window.designWidth,
184         JsonType::NUMBER,
185         false,
186         g_parseResult,
187         ArrayType::NOT_ARRAY);
188     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
189         jsonObjectEnd,
190         ExtensionFormProfileReader::WINDOW_AUTO_DESIGN_WIDTH,
191         window.autoDesignWidth,
192         false,
193         g_parseResult);
194 }
195 
from_json(const nlohmann::json & jsonObject,ExtensionFormProfileInfo & extensionFormProfileInfo)196 void from_json(const nlohmann::json &jsonObject, ExtensionFormProfileInfo &extensionFormProfileInfo)
197 {
198     const auto &jsonObjectEnd = jsonObject.end();
199     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
200         jsonObjectEnd,
201         ExtensionFormProfileReader::NAME,
202         extensionFormProfileInfo.name,
203         true,
204         g_parseResult);
205     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
206         jsonObjectEnd,
207         ExtensionFormProfileReader::DISPLAY_NAME,
208         extensionFormProfileInfo.displayName,
209         false,
210         g_parseResult);
211     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
212         jsonObjectEnd,
213         ExtensionFormProfileReader::DESCRIPTION,
214         extensionFormProfileInfo.description,
215         false,
216         g_parseResult);
217     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
218         jsonObjectEnd,
219         ExtensionFormProfileReader::SRC,
220         extensionFormProfileInfo.src,
221         false,
222         g_parseResult);
223     GetValueIfFindKey<Window>(jsonObject,
224         jsonObjectEnd,
225         ExtensionFormProfileReader::WINDOW,
226         extensionFormProfileInfo.window,
227         JsonType::OBJECT,
228         false,
229         g_parseResult,
230         ArrayType::NOT_ARRAY);
231     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
232         jsonObjectEnd,
233         ExtensionFormProfileReader::COLOR_MODE,
234         extensionFormProfileInfo.colorMode,
235         false,
236         g_parseResult);
237     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
238         jsonObjectEnd,
239         ExtensionFormProfileReader::RENDERING_MODE,
240         extensionFormProfileInfo.renderingMode,
241         false,
242         g_parseResult);
243     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
244         jsonObjectEnd,
245         ExtensionFormProfileReader::FORM_CONFIG_ABILITY,
246         extensionFormProfileInfo.formConfigAbility,
247         false,
248         g_parseResult);
249     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
250         jsonObjectEnd,
251         ExtensionFormProfileReader::TYPE,
252         extensionFormProfileInfo.type,
253         false,
254         g_parseResult);
255     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
256         jsonObjectEnd,
257         ExtensionFormProfileReader::UI_SYNTAX,
258         extensionFormProfileInfo.uiSyntax,
259         false,
260         g_parseResult);
261     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
262         jsonObjectEnd,
263         ExtensionFormProfileReader::FORM_VISIBLE_NOTIFY,
264         extensionFormProfileInfo.formVisibleNotify,
265         false,
266         g_parseResult);
267     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
268         jsonObjectEnd,
269         ExtensionFormProfileReader::IS_DEFAULT,
270         extensionFormProfileInfo.isDefault,
271         true,
272         g_parseResult);
273     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
274         jsonObjectEnd,
275         ExtensionFormProfileReader::UPDATE_ENABLED,
276         extensionFormProfileInfo.updateEnabled,
277         true,
278         g_parseResult);
279     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
280         jsonObjectEnd,
281         ExtensionFormProfileReader::SCHEDULED_UPDATE_TIME,
282         extensionFormProfileInfo.scheduledUpdateTime,
283         false,
284         g_parseResult);
285     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
286         jsonObjectEnd,
287         ExtensionFormProfileReader::MULTI_SCHEDULED_UPDATE_TIME,
288         extensionFormProfileInfo.multiScheduledUpdateTime,
289         false,
290         g_parseResult);
291     GetValueIfFindKey<int32_t>(jsonObject,
292         jsonObjectEnd,
293         ExtensionFormProfileReader::UPDATE_DURATION,
294         extensionFormProfileInfo.updateDuration,
295         JsonType::NUMBER,
296         false,
297         g_parseResult,
298         ArrayType::NOT_ARRAY);
299     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
300         jsonObjectEnd,
301         ExtensionFormProfileReader::DEFAULT_DIMENSION,
302         extensionFormProfileInfo.defaultDimension,
303         true,
304         g_parseResult);
305     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
306         jsonObjectEnd,
307         ExtensionFormProfileReader::SUPPORT_DIMENSIONS,
308         extensionFormProfileInfo.supportDimensions,
309         JsonType::ARRAY,
310         true,
311         g_parseResult,
312         ArrayType::STRING);
313     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
314         jsonObjectEnd,
315         ExtensionFormProfileReader::METADATA,
316         extensionFormProfileInfo.metadata,
317         JsonType::ARRAY,
318         false,
319         g_parseResult,
320         ArrayType::OBJECT);
321     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
322         jsonObjectEnd,
323         ExtensionFormProfileReader::DATA_PROXY_ENABLED,
324         extensionFormProfileInfo.dataProxyEnabled,
325         false,
326         g_parseResult);
327     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
328         jsonObjectEnd,
329         ExtensionFormProfileReader::IS_DYNAMIC,
330         extensionFormProfileInfo.isDynamic,
331         false,
332         g_parseResult);
333     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
334         jsonObjectEnd,
335         ExtensionFormProfileReader::TRANSPARENCY_ENABLED,
336         extensionFormProfileInfo.transparencyEnabled,
337         false,
338         g_parseResult);
339     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
340         jsonObjectEnd,
341         ExtensionFormProfileReader::FONT_SCALE_FOLLOW_SYSTEM,
342         extensionFormProfileInfo.fontScaleFollowSystem,
343         false,
344         g_parseResult);
345     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
346         jsonObjectEnd,
347         ExtensionFormProfileReader::SUPPORT_SHAPES,
348         extensionFormProfileInfo.supportShapes,
349         JsonType::ARRAY,
350         false,
351         g_parseResult,
352         ArrayType::STRING);
353     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
354         jsonObjectEnd,
355         ExtensionFormProfileReader::CONDITION_UPDATE,
356         extensionFormProfileInfo.conditionUpdate,
357         JsonType::ARRAY,
358         false,
359         g_parseResult,
360         ArrayType::STRING);
361     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
362         jsonObjectEnd,
363         ExtensionFormProfileReader::PREVIEW_IMAGES,
364         extensionFormProfileInfo.previewImages,
365         JsonType::ARRAY,
366         false,
367         g_parseResult,
368         ArrayType::STRING);
369     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
370         jsonObjectEnd,
371         ExtensionFormProfileReader::ENABLE_BLUR_BACKGROUND,
372         extensionFormProfileInfo.enableBlurBackground,
373         false,
374         g_parseResult);
375 }
376 
from_json(const nlohmann::json & jsonObject,ExtensionFormProfileInfoStruct & profileInfo)377 void from_json(const nlohmann::json &jsonObject, ExtensionFormProfileInfoStruct &profileInfo)
378 {
379     const auto &jsonObjectEnd = jsonObject.end();
380     GetValueIfFindKey<int32_t>(jsonObject,
381         jsonObjectEnd,
382         ExtensionFormProfileReader::PRIVACY_LEVEL,
383         profileInfo.privacyLevel,
384         JsonType::NUMBER,
385         false,
386         g_parseResult,
387         ArrayType::NOT_ARRAY);
388     GetValueIfFindKey<std::vector<ExtensionFormProfileInfo>>(jsonObject,
389         jsonObjectEnd,
390         ExtensionFormProfileReader::FORMS,
391         profileInfo.forms,
392         JsonType::ARRAY,
393         false,
394         g_parseResult,
395         ArrayType::OBJECT);
396 }
397 
CheckFormNameIsValid(const std::string & name)398 bool CheckFormNameIsValid(const std::string &name)
399 {
400     if (name.empty()) {
401         APP_LOGE("name is empty");
402         return false;
403     }
404     if (name.size() > MAX_FORM_NAME) {
405         APP_LOGE("name size is too long");
406         return false;
407     }
408     return true;
409 }
410 
GetMetadata(const ExtensionFormProfileInfo & form,ExtensionFormInfo & info)411 bool GetMetadata(const ExtensionFormProfileInfo &form, ExtensionFormInfo &info)
412 {
413     std::set<int32_t> supportDimensionSet {};
414     size_t len = sizeof(DIMENSION_MAP_KEY) / sizeof(DIMENSION_MAP_KEY[0]);
415     size_t i = 0;
416     for (const auto &dimension: form.supportDimensions) {
417         for (i = 0; i < len; i++) {
418             if (DIMENSION_MAP_KEY[i] == dimension) break;
419         }
420         if (i == len) {
421             APP_LOGW("dimension invalid form %{public}s", form.name.c_str());
422             continue;
423         }
424 
425         int32_t dimensionItem = DIMENSION_MAP_VALUE[i];
426         #ifndef FORM_DIMENSION_2_3
427             if (dimensionItem == DIMENSION_2_3) {
428                 APP_LOGW("dimension invalid in wearable Device form %{public}d", dimensionItem);
429                 continue;
430             }
431         #endif
432 
433         #ifndef FORM_DIMENSION_3_3
434             if (dimensionItem == DIMENSION_3_3) {
435                 APP_LOGW("dimension invalid in wearable Device form %{public}d", dimensionItem);
436                 continue;
437             }
438         #endif
439 
440         supportDimensionSet.emplace(dimensionItem);
441     }
442     for (i = 0; i < len; i++) {
443         if (DIMENSION_MAP_KEY[i] == form.defaultDimension) {
444             break;
445         }
446     }
447     if (i == len) {
448         APP_LOGW("defaultDimension invalid form %{public}s", form.name.c_str());
449         return false;
450     }
451     if (supportDimensionSet.find(DIMENSION_MAP_VALUE[i]) == supportDimensionSet.end()) {
452         APP_LOGW("defaultDimension not in supportDimensions form %{public}s", form.name.c_str());
453         return false;
454     }
455 
456     info.defaultDimension = DIMENSION_MAP_VALUE[i];
457     for (const auto &dimension: supportDimensionSet) {
458         info.supportDimensions.emplace_back(dimension);
459     }
460     return true;
461 }
462 
GetSupportShapes(const ExtensionFormProfileInfo & form,ExtensionFormInfo & info)463 bool GetSupportShapes(const ExtensionFormProfileInfo &form, ExtensionFormInfo &info)
464 {
465     std::set<int32_t> supportShapeSet {};
466     size_t len = sizeof(SHAPE_MAP_KEY) / sizeof(SHAPE_MAP_KEY[0]);
467     for (const auto &shape: form.supportShapes) {
468         size_t i = 0;
469         for (i = 0; i < len; i++) {
470             if (SHAPE_MAP_KEY[i] == shape) break;
471         }
472         if (i == len) {
473             APP_LOGW("dimension invalid form %{public}s", form.name.c_str());
474             continue;
475         }
476         supportShapeSet.emplace(SHAPE_MAP_VALUE[i]);
477     }
478 
479     if (supportShapeSet.empty()) {
480         supportShapeSet.emplace(DEFAULT_RECT_SHAPE);
481     }
482 
483     for (const auto &shape: supportShapeSet) {
484         info.supportShapes.emplace_back(shape);
485     }
486     return true;
487 }
488 
GetPreviewImages(const ExtensionFormProfileInfo & form,ExtensionFormInfo & info)489 bool GetPreviewImages(const ExtensionFormProfileInfo &form, ExtensionFormInfo &info)
490 {
491     for (const auto &previewImage: form.previewImages) {
492         auto pos = previewImage.find(CHAR_COLON);
493         if (pos != std::string::npos) {
494             int32_t previewImageLength = static_cast<int32_t>(previewImage.length());
495             auto previewImageId = static_cast<uint32_t>(
496                     atoi(previewImage.substr(pos + 1, previewImageLength - pos - 1).c_str()));
497             info.previewImages.emplace_back(previewImageId);
498         }
499     }
500     return true;
501 }
502 
GetConditionUpdate(const ExtensionFormProfileInfo & form,ExtensionFormInfo & info)503 bool GetConditionUpdate(const ExtensionFormProfileInfo &form, ExtensionFormInfo &info)
504 {
505     std::set<int32_t> conditionUpdateSet {};
506     size_t len = sizeof(CONDITION_MAP_KEY) / sizeof(CONDITION_MAP_KEY[0]);
507     size_t i = 0;
508     for (const auto &conditionUpdate: form.conditionUpdate) {
509         for (i = 0; i < len; i++) {
510             if (CONDITION_MAP_KEY[i] == conditionUpdate) {
511                 break;
512             }
513         }
514         if (i == len) {
515             APP_LOGW("conditionUpdate invalid form %{public}s", form.name.c_str());
516             continue;
517         }
518         conditionUpdateSet.emplace(CONDITION_MAP_VALUE[i]);
519     }
520     if (conditionUpdateSet.empty()) {
521         conditionUpdateSet.emplace(DEFAULT_CONDITION_TYPE);
522     }
523     for (const auto &conditionUpdate: conditionUpdateSet) {
524         info.conditionUpdate.emplace_back(conditionUpdate);
525     }
526     return true;
527 }
528 
TransformToFormInfoExt(const ExtensionFormProfileInfo & form,ExtensionFormInfo & info)529 void TransformToFormInfoExt(const ExtensionFormProfileInfo &form, ExtensionFormInfo &info)
530 {
531     info.name = form.name;
532     info.description = form.description;
533     info.displayName = form.displayName;
534     info.src = form.src;
535     info.window.autoDesignWidth = form.window.autoDesignWidth;
536     info.window.designWidth = form.window.designWidth;
537     info.formConfigAbility = form.formConfigAbility;
538     info.formVisibleNotify = form.formVisibleNotify;
539     info.isDefault = form.isDefault;
540     info.updateEnabled = form.updateEnabled;
541     info.scheduledUpdateTime = form.scheduledUpdateTime;
542     info.multiScheduledUpdateTime = form.multiScheduledUpdateTime;
543     info.updateDuration = form.updateDuration;
544 }
545 
TransformToExtensionFormInfo(const ExtensionFormProfileInfo & form,ExtensionFormInfo & info)546 bool TransformToExtensionFormInfo(const ExtensionFormProfileInfo &form, ExtensionFormInfo &info)
547 {
548     if (!CheckFormNameIsValid(form.name)) {
549         APP_LOGE("form name is invalid");
550         return false;
551     }
552     TransformToFormInfoExt(form, info);
553 
554     size_t len = sizeof(FORM_COLOR_MODE_MAP_KEY) / sizeof(FORM_COLOR_MODE_MAP_KEY[0]);
555     for (size_t i = 0; i < len; i++) {
556         if (FORM_COLOR_MODE_MAP_KEY[i] == form.colorMode)
557             info.colorMode = FORM_COLOR_MODE_MAP_VALUE[i];
558     }
559 
560     len = sizeof(FORM_RENDERING_MODE_MAP_KEY) / sizeof(FORM_RENDERING_MODE_MAP_KEY[0]);
561     for (size_t i = 0; i < len; i++) {
562         if (FORM_RENDERING_MODE_MAP_KEY[i] == form.renderingMode) {
563             info.renderingMode = FORM_RENDERING_MODE_MAP_VALUE[i];
564         }
565     }
566 
567     len = sizeof(FORM_TYPE_MAP_KEY) / sizeof(FORM_TYPE_MAP_KEY[0]);
568     for (size_t i = 0; i < len; i++) {
569         if (FORM_TYPE_MAP_KEY[i] == form.type)
570             info.type = FORM_TYPE_MAP_VALUE[i];
571         if (UI_SYNTAX_MAP_KEY[i] == form.uiSyntax)
572             info.uiSyntax = UI_SYNTAX_MAP_VALUE[i];
573     }
574 
575     if (!GetMetadata(form, info)) {
576         return false;
577     }
578     for (const auto &data: form.metadata) {
579         FormCustomizeData customizeData;
580         customizeData.name = data.name;
581         customizeData.value = data.value;
582         info.metadata.emplace_back(customizeData);
583     }
584 
585     info.dataProxyEnabled = form.dataProxyEnabled;
586     info.isDynamic = form.isDynamic;
587     info.transparencyEnabled = form.transparencyEnabled;
588     info.fontScaleFollowSystem = form.fontScaleFollowSystem;
589 
590     if (!GetSupportShapes(form, info)) {
591         return false;
592     }
593     if (!GetConditionUpdate(form, info)) {
594         return false;
595     }
596     if (!GetPreviewImages(form, info)) {
597         return false;
598     }
599     info.enableBlurBackground = form.enableBlurBackground;
600     APP_LOGI("form name: %{public}s enableBlurBackground: %{public}d", info.name.c_str(), info.enableBlurBackground);
601     if (info.enableBlurBackground) {
602         info.transparencyEnabled = true;
603     }
604     return true;
605 }
606 
TransformToInfos(const ExtensionFormProfileInfoStruct & profileInfo,std::vector<ExtensionFormInfo> & infos)607 bool TransformToInfos(const ExtensionFormProfileInfoStruct &profileInfo, std::vector<ExtensionFormInfo> &infos)
608 {
609     APP_LOGD("transform ExtensionFormProfileInfo to ExtensionFormInfo");
610     for (const auto &form: profileInfo.forms) {
611         ExtensionFormInfo info;
612         if (!TransformToExtensionFormInfo(form, info)) {
613             return false;
614         }
615         infos.push_back(info);
616     }
617     return true;
618 }
619 } // namespace
620 
TransformTo(const std::string & formProfile,std::vector<ExtensionFormInfo> & infos,int32_t & privacyLevel)621 ErrCode ExtensionFormProfile::TransformTo(
622     const std::string &formProfile, std::vector<ExtensionFormInfo> &infos, int32_t &privacyLevel)
623 {
624     APP_LOGD("transform profile to extension form infos");
625     nlohmann::json jsonObject = nlohmann::json::parse(formProfile, nullptr, false);
626     if (jsonObject.is_discarded()) {
627         APP_LOGE("bad profile");
628         return ERR_APPEXECFWK_PARSE_BAD_PROFILE;
629     }
630 
631     ExtensionFormProfileInfoStruct profileInfo;
632     {
633         std::lock_guard<std::mutex> lock(g_mutex);
634         g_parseResult = ERR_OK;
635         profileInfo = jsonObject.get<ExtensionFormProfileInfoStruct>();
636         privacyLevel = profileInfo.privacyLevel;
637         if (g_parseResult != ERR_OK) {
638             APP_LOGE("g_parseResult %{public}d", g_parseResult);
639             int32_t ret = g_parseResult;
640             // need recover parse result to ERR_OK
641             g_parseResult = ERR_OK;
642             return ret;
643         }
644     }
645 
646     if (!TransformToInfos(profileInfo, infos)) {
647         return ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR;
648     }
649     return ERR_OK;
650 }
651 }  // namespace AppExecFwk
652 }  // namespace OHOS
653