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