1/*
2 * Copyright (c) 2022-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 "module_profile.h"
17
18#include <sstream>
19#include <unordered_set>
20
21#include "parameter.h"
22#include "parameters.h"
23
24namespace OHOS {
25namespace AppExecFwk {
26namespace {
27constexpr uint8_t MAX_MODULE_NAME = 128;
28}
29
30namespace Profile {
31int32_t g_parseResult = ERR_OK;
32std::mutex g_mutex;
33
34const std::unordered_set<std::string> MODULE_TYPE_SET = {
35    "entry",
36    "feature",
37    "shared"
38};
39
40const std::unordered_set<std::string> VIRTUAL_MACHINE_SET = {
41    "ark",
42    "default"
43};
44
45constexpr const char* BACKGROUND_MODES_MAP_KEY[] = {
46    ProfileReader::KEY_DATA_TRANSFER,
47    ProfileReader::KEY_AUDIO_PLAYBACK,
48    ProfileReader::KEY_AUDIO_RECORDING,
49    ProfileReader::KEY_LOCATION,
50    ProfileReader::KEY_BLUETOOTH_INTERACTION,
51    ProfileReader::KEY_MULTI_DEVICE_CONNECTION,
52    ProfileReader::KEY_WIFI_INTERACTION,
53    ProfileReader::KEY_VOIP,
54    ProfileReader::KEY_TASK_KEEPING,
55    ProfileReader::KEY_PICTURE_IN_PICTURE,
56    ProfileReader::KEY_SCREEN_FETCH
57};
58const uint32_t BACKGROUND_MODES_MAP_VALUE[] = {
59    ProfileReader::VALUE_DATA_TRANSFER,
60    ProfileReader::VALUE_AUDIO_PLAYBACK,
61    ProfileReader::VALUE_AUDIO_RECORDING,
62    ProfileReader::VALUE_LOCATION,
63    ProfileReader::VALUE_BLUETOOTH_INTERACTION,
64    ProfileReader::VALUE_MULTI_DEVICE_CONNECTION,
65    ProfileReader::VALUE_WIFI_INTERACTION,
66    ProfileReader::VALUE_VOIP,
67    ProfileReader::VALUE_TASK_KEEPING,
68    ProfileReader::VALUE_PICTURE_IN_PICTURE,
69    ProfileReader::VALUE_SCREEN_FETCH
70};
71
72const std::unordered_set<std::string> GRANT_MODE_SET = {
73    "system_grant",
74    "user_grant"
75};
76
77const std::unordered_set<std::string> AVAILABLE_LEVEL_SET = {
78    "system_core",
79    "system_basic",
80    "normal"
81};
82
83const std::unordered_map<std::string, LaunchMode> LAUNCH_MODE_MAP = {
84    {"singleton", LaunchMode::SINGLETON},
85    {"standard", LaunchMode::STANDARD},
86    {"multiton", LaunchMode::STANDARD},
87    {"specified", LaunchMode::SPECIFIED}
88};
89constexpr const char* DISPLAY_ORIENTATION_MAP_KEY[] = {
90    "unspecified",
91    "landscape",
92    "portrait",
93    "follow_recent",
94    "landscape_inverted",
95    "portrait_inverted",
96    "auto_rotation",
97    "auto_rotation_landscape",
98    "auto_rotation_portrait",
99    "auto_rotation_restricted",
100    "auto_rotation_landscape_restricted",
101    "auto_rotation_portrait_restricted",
102    "locked",
103    "auto_rotation_unspecified",
104    "follow_desktop"
105};
106const DisplayOrientation DISPLAY_ORIENTATION_MAP_VALUE[] = {
107    DisplayOrientation::UNSPECIFIED,
108    DisplayOrientation::LANDSCAPE,
109    DisplayOrientation::PORTRAIT,
110    DisplayOrientation::FOLLOWRECENT,
111    DisplayOrientation::LANDSCAPE_INVERTED,
112    DisplayOrientation::PORTRAIT_INVERTED,
113    DisplayOrientation::AUTO_ROTATION,
114    DisplayOrientation::AUTO_ROTATION_LANDSCAPE,
115    DisplayOrientation::AUTO_ROTATION_PORTRAIT,
116    DisplayOrientation::AUTO_ROTATION_RESTRICTED,
117    DisplayOrientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED,
118    DisplayOrientation::AUTO_ROTATION_PORTRAIT_RESTRICTED,
119    DisplayOrientation::LOCKED,
120    DisplayOrientation::AUTO_ROTATION_UNSPECIFIED,
121    DisplayOrientation::FOLLOW_DESKTOP
122};
123const std::unordered_map<std::string, SupportWindowMode> WINDOW_MODE_MAP = {
124    {"fullscreen", SupportWindowMode::FULLSCREEN},
125    {"split", SupportWindowMode::SPLIT},
126    {"floating", SupportWindowMode::FLOATING}
127};
128const std::unordered_map<std::string, BundleType> BUNDLE_TYPE_MAP = {
129    {"app", BundleType::APP},
130    {"atomicService", BundleType::ATOMIC_SERVICE},
131    {"shared", BundleType::SHARED},
132    {"appService", BundleType::APP_SERVICE_FWK}
133};
134const size_t MAX_QUERYSCHEMES_LENGTH = 50;
135
136const std::unordered_map<std::string, MultiAppModeType> MULTI_APP_MODE_MAP = {
137    {"multiInstance", MultiAppModeType::MULTI_INSTANCE},
138    {"appClone", MultiAppModeType::APP_CLONE}
139};
140
141struct DeviceConfig {
142    // pair first : if exist in module.json then true, otherwise false
143    // pair second : actual value
144    std::pair<bool, int32_t> minAPIVersion = std::make_pair<>(false, 0);
145    std::pair<bool, bool> keepAlive = std::make_pair<>(false, false);
146    std::pair<bool, bool> removable = std::make_pair<>(false, true);
147    std::pair<bool, bool> singleton = std::make_pair<>(false, false);
148    std::pair<bool, bool> userDataClearable = std::make_pair<>(false, true);
149    std::pair<bool, bool> accessible = std::make_pair<>(false, true);
150};
151
152struct Metadata {
153    std::string name;
154    std::string value;
155    std::string resource;
156};
157
158struct HnpPackage {
159    std::string package;
160    std::string type;
161};
162
163struct Ability {
164    bool visible = false;
165    bool continuable = false;
166    bool removeMissionAfterTerminate = false;
167    bool excludeFromMissions = false;
168    bool recoverable = false;
169    bool unclearableMission = false;
170    bool excludeFromDock = false;
171    bool isolationProcess = false;
172    uint32_t descriptionId = 0;
173    uint32_t iconId = 0;
174    uint32_t labelId = 0;
175    int32_t priority = 0;
176    uint32_t startWindowIconId = 0;
177    uint32_t startWindowBackgroundId = 0;
178    uint32_t maxWindowWidth = 0;
179    uint32_t minWindowWidth = 0;
180    uint32_t maxWindowHeight = 0;
181    uint32_t minWindowHeight = 0;
182    uint32_t orientationId = 0;
183    double maxWindowRatio = 0;
184    double minWindowRatio = 0;
185    std::string name;
186    std::string srcEntrance;
187    std::string launchType = ABILITY_LAUNCH_TYPE_DEFAULT_VALUE;
188    std::string description;
189    std::string icon;
190    std::string label;
191    std::vector<std::string> permissions;
192    std::vector<Metadata> metadata;
193    std::vector<Skill> skills;
194    std::vector<std::string> backgroundModes;
195    std::string startWindowIcon;
196    std::string startWindowBackground;
197    std::string orientation = "unspecified";
198    std::vector<std::string> windowModes;
199    std::string preferMultiWindowOrientation = "default";
200    std::vector<std::string> continueType;
201    std::vector<std::string> continueBundleNames;
202};
203
204struct Extension {
205    bool visible = false;
206    uint32_t iconId = 0;
207    uint32_t labelId = 0;
208    uint32_t descriptionId = 0;
209    int32_t priority = 0;
210    std::string name;
211    std::string srcEntrance;
212    std::string icon;
213    std::string label;
214    std::string description;
215    std::string type;
216    std::string readPermission;
217    std::string writePermission;
218    std::string uri;
219    std::vector<std::string> permissions;
220    std::vector<Skill> skills;
221    std::vector<Metadata> metadata;
222    std::string extensionProcessMode;
223    std::vector<std::string> dataGroupIds;
224};
225
226struct MultiAppMode {
227    std::string multiAppModeType;
228    int32_t maxCount = 0;
229};
230
231struct App {
232    bool debug = false;
233    bool keepAlive = false;
234    bool singleton = false;
235    bool userDataClearable = true;
236    bool accessible = false;
237    bool multiProjects = false;
238    bool asanEnabled = false;
239    bool gwpAsanEnabled = false;
240    bool hwasanEnabled = false;
241    bool tsanEnabled = false;
242    bool ubsanEnabled = false;
243    bool cloudFileSyncEnabled = false;
244    uint32_t iconId = 0;
245    uint32_t labelId = 0;
246    uint32_t descriptionId = 0;
247    int32_t versionCode = 0;
248    int32_t minCompatibleVersionCode = -1;
249    uint32_t minAPIVersion = 0;
250    int32_t targetAPIVersion = 0;
251    int32_t targetPriority = 0;
252    int32_t maxChildProcess = OHOS::system::GetIntParameter(MAX_CHILD_PROCESS, 1);
253    std::string bundleName;
254    std::string icon;
255    std::string label;
256    std::string description;
257    std::string vendor;
258    std::string versionName;
259    std::string apiReleaseType = APP_API_RELEASETYPE_DEFAULT_VALUE;
260    std::pair<bool, bool> removable = std::make_pair<>(false, true);
261    std::vector<std::string> targetBundleList;
262    std::map<std::string, DeviceConfig> deviceConfigs;
263    std::string targetBundle;
264    std::string bundleType = Profile::BUNDLE_TYPE_APP;
265    std::string compileSdkVersion;
266    std::string compileSdkType = Profile::COMPILE_SDK_TYPE_OPEN_HARMONY;
267    std::vector<ApplicationEnvironment> appEnvironments;
268    MultiAppMode multiAppMode;
269    std::string configuration;
270};
271
272struct Module {
273    bool deliveryWithInstall = false;
274    bool installationFree = false;
275    bool isLibIsolated = false;
276    bool compressNativeLibs = true;
277    uint32_t descriptionId = 0;
278    int32_t targetPriority = 0;
279    std::string name;
280    std::string type;
281    std::string srcEntrance;
282    std::string description;
283    std::string process;
284    std::string mainElement;
285    std::vector<std::string> deviceTypes;
286    std::string virtualMachine = MODULE_VIRTUAL_MACHINE_DEFAULT_VALUE;
287    std::string pages;
288    std::vector<Metadata> metadata;
289    std::vector<HnpPackage> hnpPackages;
290    std::vector<Ability> abilities;
291    std::vector<Extension> extensionAbilities;
292    std::vector<RequestPermission> requestPermissions;
293    std::vector<DefinePermission> definePermissions;
294    std::vector<Dependency> dependencies;
295    std::string compileMode;
296    std::string targetModule;
297    std::vector<ProxyData> proxyDatas;
298    std::vector<ProxyData> proxyData;
299    std::string buildHash;
300    std::string isolationMode;
301    std::string fileContextMenu;
302    std::vector<std::string> querySchemes;
303    std::string routerMap;
304    std::vector<AppEnvironment> appEnvironments;
305    std::string packageName;
306    std::string appStartup;
307};
308
309struct ModuleJson {
310    App app;
311    Module module;
312};
313
314void from_json(const nlohmann::json &jsonObject, Metadata &metadata)
315{
316    APP_LOGD("read metadata tag from module.json");
317    const auto &jsonObjectEnd = jsonObject.end();
318    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
319        jsonObjectEnd,
320        META_DATA_NAME,
321        metadata.name,
322        false,
323        g_parseResult);
324    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
325        jsonObjectEnd,
326        META_DATA_VALUE,
327        metadata.value,
328        false,
329        g_parseResult);
330    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
331        jsonObjectEnd,
332        META_DATA_RESOURCE,
333        metadata.resource,
334        false,
335        g_parseResult);
336}
337
338void from_json(const nlohmann::json &jsonObject, HnpPackage &hnpPackage)
339{
340    APP_LOGD("read hnppackage tag from module.json");
341    const auto &jsonObjectEnd = jsonObject.end();
342    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
343        jsonObjectEnd,
344        HNP_PACKAGE,
345        hnpPackage.package,
346        false,
347        g_parseResult);
348    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
349        jsonObjectEnd,
350        HNP_TYPE,
351        hnpPackage.type,
352        false,
353        g_parseResult);
354}
355
356void from_json(const nlohmann::json &jsonObject, Ability &ability)
357{
358    APP_LOGD("read ability tag from module.json");
359    const auto &jsonObjectEnd = jsonObject.end();
360    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
361        jsonObjectEnd,
362        ABILITY_NAME,
363        ability.name,
364        true,
365        g_parseResult);
366    // both srcEntry and srcEntrance can be configured, but srcEntry has higher priority
367    if (jsonObject.find(SRC_ENTRY) != jsonObject.end()) {
368        BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
369            jsonObjectEnd,
370            SRC_ENTRY,
371            ability.srcEntrance,
372            true,
373            g_parseResult);
374    } else {
375        BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
376            jsonObjectEnd,
377            SRC_ENTRANCE,
378            ability.srcEntrance,
379            true,
380            g_parseResult);
381    }
382    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
383        jsonObjectEnd,
384        ABILITY_LAUNCH_TYPE,
385        ability.launchType,
386        false,
387        g_parseResult);
388    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
389        jsonObjectEnd,
390        DESCRIPTION,
391        ability.description,
392        false,
393        g_parseResult);
394    GetValueIfFindKey<uint32_t>(jsonObject,
395        jsonObjectEnd,
396        DESCRIPTION_ID,
397        ability.descriptionId,
398        JsonType::NUMBER,
399        false,
400        g_parseResult,
401        ArrayType::NOT_ARRAY);
402    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
403        jsonObjectEnd,
404        ICON,
405        ability.icon,
406        false,
407        g_parseResult);
408    GetValueIfFindKey<uint32_t>(jsonObject,
409        jsonObjectEnd,
410        ICON_ID,
411        ability.iconId,
412        JsonType::NUMBER,
413        false,
414        g_parseResult,
415        ArrayType::NOT_ARRAY);
416    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
417        jsonObjectEnd,
418        LABEL,
419        ability.label,
420        false,
421        g_parseResult);
422    GetValueIfFindKey<uint32_t>(jsonObject,
423        jsonObjectEnd,
424        LABEL_ID,
425        ability.labelId,
426        JsonType::NUMBER,
427        false,
428        g_parseResult,
429        ArrayType::NOT_ARRAY);
430    GetValueIfFindKey<int32_t>(jsonObject,
431        jsonObjectEnd,
432        PRIORITY,
433        ability.priority,
434        JsonType::NUMBER,
435        false,
436        g_parseResult,
437        ArrayType::NOT_ARRAY);
438    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
439        jsonObjectEnd,
440        PERMISSIONS,
441        ability.permissions,
442        JsonType::ARRAY,
443        false,
444        g_parseResult,
445        ArrayType::STRING);
446    GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
447        jsonObjectEnd,
448        META_DATA,
449        ability.metadata,
450        JsonType::ARRAY,
451        false,
452        g_parseResult,
453        ArrayType::OBJECT);
454    // both exported and visible can be configured, but exported has higher priority
455    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
456        jsonObjectEnd,
457        VISIBLE,
458        ability.visible,
459        false,
460        g_parseResult);
461    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
462        jsonObjectEnd,
463        EXPORTED,
464        ability.visible,
465        false,
466        g_parseResult);
467    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
468        jsonObjectEnd,
469        ABILITY_CONTINUABLE,
470        ability.continuable,
471        false,
472        g_parseResult);
473    GetValueIfFindKey<std::vector<Skill>>(jsonObject,
474        jsonObjectEnd,
475        SKILLS,
476        ability.skills,
477        JsonType::ARRAY,
478        false,
479        g_parseResult,
480        ArrayType::OBJECT);
481    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
482        jsonObjectEnd,
483        ABILITY_BACKGROUNDMODES,
484        ability.backgroundModes,
485        JsonType::ARRAY,
486        false,
487        g_parseResult,
488        ArrayType::STRING);
489    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
490        jsonObjectEnd,
491        ABILITY_START_WINDOW_ICON,
492        ability.startWindowIcon,
493        false,
494        g_parseResult);
495    GetValueIfFindKey<uint32_t>(jsonObject,
496        jsonObjectEnd,
497        ABILITY_START_WINDOW_ICON_ID,
498        ability.startWindowIconId,
499        JsonType::NUMBER,
500        false,
501        g_parseResult,
502        ArrayType::NOT_ARRAY);
503    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
504        jsonObjectEnd,
505        ABILITY_START_WINDOW_BACKGROUND,
506        ability.startWindowBackground,
507        false,
508        g_parseResult);
509    GetValueIfFindKey<uint32_t>(jsonObject,
510        jsonObjectEnd,
511        ABILITY_START_WINDOW_BACKGROUND_ID,
512        ability.startWindowBackgroundId,
513        JsonType::NUMBER,
514        false,
515        g_parseResult,
516        ArrayType::NOT_ARRAY);
517    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
518        jsonObjectEnd,
519        ABILITY_REMOVE_MISSION_AFTER_TERMINATE,
520        ability.removeMissionAfterTerminate,
521        false,
522        g_parseResult);
523    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
524        jsonObjectEnd,
525        ABILITY_ORIENTATION,
526        ability.orientation,
527        false,
528        g_parseResult);
529    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
530        jsonObjectEnd,
531        ABILITY_SUPPORT_WINDOW_MODE,
532        ability.windowModes,
533        JsonType::ARRAY,
534        false,
535        g_parseResult,
536        ArrayType::STRING);
537    GetValueIfFindKey<double>(jsonObject,
538        jsonObjectEnd,
539        ABILITY_MAX_WINDOW_RATIO,
540        ability.maxWindowRatio,
541        JsonType::NUMBER,
542        false,
543        g_parseResult,
544        ArrayType::NOT_ARRAY);
545    GetValueIfFindKey<double>(jsonObject,
546        jsonObjectEnd,
547        ABILITY_MIN_WINDOW_RATIO,
548        ability.minWindowRatio,
549        JsonType::NUMBER,
550        false,
551        g_parseResult,
552        ArrayType::NOT_ARRAY);
553    GetValueIfFindKey<uint32_t>(jsonObject,
554        jsonObjectEnd,
555        ABILITY_MAX_WINDOW_WIDTH,
556        ability.maxWindowWidth,
557        JsonType::NUMBER,
558        false,
559        g_parseResult,
560        ArrayType::NOT_ARRAY);
561    GetValueIfFindKey<uint32_t>(jsonObject,
562        jsonObjectEnd,
563        ABILITY_MIN_WINDOW_WIDTH,
564        ability.minWindowWidth,
565        JsonType::NUMBER,
566        false,
567        g_parseResult,
568        ArrayType::NOT_ARRAY);
569    GetValueIfFindKey<uint32_t>(jsonObject,
570        jsonObjectEnd,
571        ABILITY_MAX_WINDOW_HEIGHT,
572        ability.maxWindowHeight,
573        JsonType::NUMBER,
574        false,
575        g_parseResult,
576        ArrayType::NOT_ARRAY);
577    GetValueIfFindKey<uint32_t>(jsonObject,
578        jsonObjectEnd,
579        ABILITY_MIN_WINDOW_HEIGHT,
580        ability.minWindowHeight,
581        JsonType::NUMBER,
582        false,
583        g_parseResult,
584        ArrayType::NOT_ARRAY);
585    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
586        jsonObjectEnd,
587        ABILITY_EXCLUDE_FROM_MISSIONS,
588        ability.excludeFromMissions,
589        false,
590        g_parseResult);
591    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
592        jsonObjectEnd,
593        ABILITY_RECOVERABLE,
594        ability.recoverable,
595        false,
596        g_parseResult);
597    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
598        jsonObjectEnd,
599        ABILITY_UNCLEARABLE_MISSION,
600        ability.unclearableMission,
601        false,
602        g_parseResult);
603    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
604        jsonObjectEnd,
605        ABILITY_EXCLUDEFROMDOCK_MISSION,
606        ability.excludeFromDock,
607        false,
608        g_parseResult);
609    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
610        jsonObjectEnd,
611        ABILITY_PREFER_MULTI_WINDOW_ORIENTATION_MISSION,
612        ability.preferMultiWindowOrientation,
613        false,
614        g_parseResult);
615    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
616        jsonObjectEnd,
617        ABILITY_ISOLATION_PROCESS,
618        ability.isolationProcess,
619        false,
620        g_parseResult);
621    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
622        jsonObjectEnd,
623        ABILITY_CONTINUE_TYPE,
624        ability.continueType,
625        JsonType::ARRAY,
626        false,
627        g_parseResult,
628        ArrayType::STRING);
629    GetValueIfFindKey<uint32_t>(jsonObject,
630        jsonObjectEnd,
631        ABILITY_ORIENTATION_ID,
632        ability.orientationId,
633        JsonType::NUMBER,
634        false,
635        g_parseResult,
636        ArrayType::NOT_ARRAY);
637    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
638        jsonObjectEnd,
639        ABILITY_CONTINUE_BUNDLE_NAME,
640        ability.continueBundleNames,
641        JsonType::ARRAY,
642        false,
643        g_parseResult,
644        ArrayType::STRING);
645}
646
647void from_json(const nlohmann::json &jsonObject, Extension &extension)
648{
649    APP_LOGD("read extension tag from module.json");
650    const auto &jsonObjectEnd = jsonObject.end();
651    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
652        jsonObjectEnd,
653        EXTENSION_ABILITY_NAME,
654        extension.name,
655        true,
656        g_parseResult);
657    // both srcEntry and srcEntrance can be configured, but srcEntry has higher priority
658    if (jsonObject.find(SRC_ENTRY) != jsonObject.end()) {
659        BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
660            jsonObjectEnd,
661            SRC_ENTRY,
662            extension.srcEntrance,
663            true,
664            g_parseResult);
665    } else {
666        BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
667            jsonObjectEnd,
668            SRC_ENTRANCE,
669            extension.srcEntrance,
670            true,
671            g_parseResult);
672    }
673    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
674        jsonObjectEnd,
675        ICON,
676        extension.icon,
677        false,
678        g_parseResult);
679    GetValueIfFindKey<uint32_t>(jsonObject,
680        jsonObjectEnd,
681        ICON_ID,
682        extension.iconId,
683        JsonType::NUMBER,
684        false,
685        g_parseResult,
686        ArrayType::NOT_ARRAY);
687    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
688        jsonObjectEnd,
689        LABEL,
690        extension.label,
691        false,
692        g_parseResult);
693    GetValueIfFindKey<uint32_t>(jsonObject,
694        jsonObjectEnd,
695        LABEL_ID,
696        extension.labelId,
697        JsonType::NUMBER,
698        false,
699        g_parseResult,
700        ArrayType::NOT_ARRAY);
701    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
702        jsonObjectEnd,
703        DESCRIPTION,
704        extension.description,
705        false,
706        g_parseResult);
707    GetValueIfFindKey<uint32_t>(jsonObject,
708        jsonObjectEnd,
709        DESCRIPTION_ID,
710        extension.descriptionId,
711        JsonType::NUMBER,
712        false,
713        g_parseResult,
714        ArrayType::NOT_ARRAY);
715    GetValueIfFindKey<int32_t>(jsonObject,
716        jsonObjectEnd,
717        PRIORITY,
718        extension.priority,
719        JsonType::NUMBER,
720        false,
721        g_parseResult,
722        ArrayType::NOT_ARRAY);
723    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
724        jsonObjectEnd,
725        EXTENSION_ABILITY_TYPE,
726        extension.type,
727        true,
728        g_parseResult);
729    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
730        jsonObjectEnd,
731        EXTENSION_ABILITY_READ_PERMISSION,
732        extension.readPermission,
733        false,
734        g_parseResult);
735    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
736        jsonObjectEnd,
737        EXTENSION_ABILITY_WRITE_PERMISSION,
738        extension.writePermission,
739        false,
740        g_parseResult);
741    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
742        jsonObjectEnd,
743        EXTENSION_URI,
744        extension.uri,
745        false,
746        g_parseResult);
747    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
748        jsonObjectEnd,
749        PERMISSIONS,
750        extension.permissions,
751        JsonType::ARRAY,
752        false,
753        g_parseResult,
754        ArrayType::STRING);
755    // both exported and visible can be configured, but exported has higher priority
756    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
757        jsonObjectEnd,
758        VISIBLE,
759        extension.visible,
760        false,
761        g_parseResult);
762    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
763        jsonObjectEnd,
764        EXPORTED,
765        extension.visible,
766        false,
767        g_parseResult);
768    GetValueIfFindKey<std::vector<Skill>>(jsonObject,
769        jsonObjectEnd,
770        SKILLS,
771        extension.skills,
772        JsonType::ARRAY,
773        false,
774        g_parseResult,
775        ArrayType::OBJECT);
776    GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
777        jsonObjectEnd,
778        META_DATA,
779        extension.metadata,
780        JsonType::ARRAY,
781        false,
782        g_parseResult,
783        ArrayType::OBJECT);
784    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
785        jsonObjectEnd,
786        EXTENSION_PROCESS_MODE,
787        extension.extensionProcessMode,
788        false,
789        g_parseResult);
790    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
791        jsonObjectEnd,
792        DATA_GROUP_IDS,
793        extension.dataGroupIds,
794        JsonType::ARRAY,
795        false,
796        g_parseResult,
797        ArrayType::STRING);
798}
799
800void from_json(const nlohmann::json &jsonObject, DeviceConfig &deviceConfig)
801{
802    const auto &jsonObjectEnd = jsonObject.end();
803    if (jsonObject.find(MIN_API_VERSION) != jsonObjectEnd) {
804        deviceConfig.minAPIVersion.first = true;
805        GetValueIfFindKey<int32_t>(jsonObject,
806            jsonObjectEnd,
807            MIN_API_VERSION,
808            deviceConfig.minAPIVersion.second,
809            JsonType::NUMBER,
810            false,
811            g_parseResult,
812            ArrayType::NOT_ARRAY);
813    }
814    if (jsonObject.find(DEVICE_CONFIG_KEEP_ALIVE) != jsonObjectEnd) {
815        deviceConfig.keepAlive.first = true;
816        BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
817            jsonObjectEnd,
818            DEVICE_CONFIG_KEEP_ALIVE,
819            deviceConfig.keepAlive.second,
820            false,
821            g_parseResult);
822    }
823    if (jsonObject.find(DEVICE_CONFIG_REMOVABLE) != jsonObjectEnd) {
824        deviceConfig.removable.first = true;
825        BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
826            jsonObjectEnd,
827            DEVICE_CONFIG_REMOVABLE,
828            deviceConfig.removable.second,
829            false,
830            g_parseResult);
831    }
832    if (jsonObject.find(DEVICE_CONFIG_SINGLETON) != jsonObjectEnd) {
833        deviceConfig.singleton.first = true;
834        BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
835            jsonObjectEnd,
836            DEVICE_CONFIG_SINGLETON,
837            deviceConfig.singleton.second,
838            false,
839            g_parseResult);
840    }
841    if (jsonObject.find(DEVICE_CONFIG_USER_DATA_CLEARABLE) != jsonObjectEnd) {
842        deviceConfig.userDataClearable.first = true;
843        BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
844            jsonObjectEnd,
845            DEVICE_CONFIG_USER_DATA_CLEARABLE,
846            deviceConfig.userDataClearable.second,
847            false,
848            g_parseResult);
849    }
850    if (jsonObject.find(DEVICE_CONFIG_ACCESSIBLE) != jsonObjectEnd) {
851        deviceConfig.accessible.first = true;
852        BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
853            jsonObjectEnd,
854            DEVICE_CONFIG_ACCESSIBLE,
855            deviceConfig.accessible.second,
856            false,
857            g_parseResult);
858    }
859}
860
861void from_json(const nlohmann::json &jsonObject, MultiAppMode &multiAppMode)
862{
863    // these are required fields.
864    const auto &jsonObjectEnd = jsonObject.end();
865    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
866        jsonObjectEnd,
867        MULTI_APP_MODE_TYPE,
868        multiAppMode.multiAppModeType,
869        false,
870        g_parseResult);
871    GetValueIfFindKey<int32_t>(jsonObject,
872        jsonObjectEnd,
873        MULTI_APP_MODE_MAX_ADDITIONAL_NUMBER,
874        multiAppMode.maxCount,
875        JsonType::NUMBER,
876        false,
877        g_parseResult,
878        ArrayType::NOT_ARRAY);
879}
880
881void from_json(const nlohmann::json &jsonObject, App &app)
882{
883    APP_LOGD("read app tag from module.json");
884    const auto &jsonObjectEnd = jsonObject.end();
885    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
886        jsonObjectEnd,
887        APP_BUNDLE_NAME,
888        app.bundleName,
889        true,
890        g_parseResult);
891    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
892        jsonObjectEnd,
893        ICON,
894        app.icon,
895        true,
896        g_parseResult);
897    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
898        jsonObjectEnd,
899        LABEL,
900        app.label,
901        true,
902        g_parseResult);
903    GetValueIfFindKey<int32_t>(jsonObject,
904        jsonObjectEnd,
905        APP_VERSION_CODE,
906        app.versionCode,
907        JsonType::NUMBER,
908        true,
909        g_parseResult,
910        ArrayType::NOT_ARRAY);
911    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
912        jsonObjectEnd,
913        APP_VERSION_NAME,
914        app.versionName,
915        true,
916        g_parseResult);
917    GetValueIfFindKey<uint32_t>(jsonObject,
918        jsonObjectEnd,
919        APP_MIN_API_VERSION,
920        app.minAPIVersion,
921        JsonType::NUMBER,
922        true,
923        g_parseResult,
924        ArrayType::NOT_ARRAY);
925    GetValueIfFindKey<int32_t>(jsonObject,
926        jsonObjectEnd,
927        APP_TARGET_API_VERSION,
928        app.targetAPIVersion,
929        JsonType::NUMBER,
930        true,
931        g_parseResult,
932        ArrayType::NOT_ARRAY);
933    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
934        jsonObjectEnd,
935        APP_DEBUG,
936        app.debug,
937        false,
938        g_parseResult);
939    GetValueIfFindKey<uint32_t>(jsonObject,
940        jsonObjectEnd,
941        ICON_ID,
942        app.iconId,
943        JsonType::NUMBER,
944        false,
945        g_parseResult,
946        ArrayType::NOT_ARRAY);
947    GetValueIfFindKey<uint32_t>(jsonObject,
948        jsonObjectEnd,
949        LABEL_ID,
950        app.labelId,
951        JsonType::NUMBER,
952        false,
953        g_parseResult,
954        ArrayType::NOT_ARRAY);
955    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
956        jsonObjectEnd,
957        DESCRIPTION,
958        app.description,
959        false,
960        g_parseResult);
961    GetValueIfFindKey<uint32_t>(jsonObject,
962        jsonObjectEnd,
963        DESCRIPTION_ID,
964        app.descriptionId,
965        JsonType::NUMBER,
966        false,
967        g_parseResult,
968        ArrayType::NOT_ARRAY);
969    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
970        jsonObjectEnd,
971        APP_VENDOR,
972        app.vendor,
973        false,
974        g_parseResult);
975    GetValueIfFindKey<int32_t>(jsonObject,
976        jsonObjectEnd,
977        APP_MIN_COMPATIBLE_VERSION_CODE,
978        app.minCompatibleVersionCode,
979        JsonType::NUMBER,
980        false,
981        g_parseResult,
982        ArrayType::NOT_ARRAY);
983    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
984        jsonObjectEnd,
985        APP_API_RELEASETYPE,
986        app.apiReleaseType,
987        false,
988        g_parseResult);
989    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
990        jsonObjectEnd,
991        APP_KEEP_ALIVE,
992        app.keepAlive,
993        false,
994        g_parseResult);
995    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
996        jsonObjectEnd,
997        APP_TARGETBUNDLELIST,
998        app.targetBundleList,
999        JsonType::ARRAY,
1000        false,
1001        g_parseResult,
1002        ArrayType::STRING);
1003    if (jsonObject.find(APP_REMOVABLE) != jsonObject.end()) {
1004        app.removable.first = true;
1005        BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1006            jsonObjectEnd,
1007            APP_REMOVABLE,
1008            app.removable.second,
1009            false,
1010            g_parseResult);
1011    }
1012    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1013        jsonObjectEnd,
1014        APP_SINGLETON,
1015        app.singleton,
1016        false,
1017        g_parseResult);
1018    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1019        jsonObjectEnd,
1020        APP_USER_DATA_CLEARABLE,
1021        app.userDataClearable,
1022        false,
1023        g_parseResult);
1024    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1025        jsonObjectEnd,
1026        APP_ACCESSIBLE,
1027        app.accessible,
1028        false,
1029        g_parseResult);
1030    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1031        jsonObjectEnd,
1032        APP_ASAN_ENABLED,
1033        app.asanEnabled,
1034        false,
1035        g_parseResult);
1036    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1037        jsonObjectEnd,
1038        BUNDLE_TYPE,
1039        app.bundleType,
1040        false,
1041        g_parseResult);
1042    if (jsonObject.find(APP_PHONE) != jsonObjectEnd) {
1043        DeviceConfig deviceConfig;
1044        GetValueIfFindKey<DeviceConfig>(jsonObject,
1045            jsonObjectEnd,
1046            APP_PHONE,
1047            deviceConfig,
1048            JsonType::OBJECT,
1049            false,
1050            g_parseResult,
1051            ArrayType::NOT_ARRAY);
1052        app.deviceConfigs[APP_PHONE] = deviceConfig;
1053    }
1054    if (jsonObject.find(APP_TABLET) != jsonObjectEnd) {
1055        DeviceConfig deviceConfig;
1056        GetValueIfFindKey<DeviceConfig>(jsonObject,
1057            jsonObjectEnd,
1058            APP_TABLET,
1059            deviceConfig,
1060            JsonType::OBJECT,
1061            false,
1062            g_parseResult,
1063            ArrayType::NOT_ARRAY);
1064        app.deviceConfigs[APP_TABLET] = deviceConfig;
1065    }
1066    if (jsonObject.find(APP_TV) != jsonObjectEnd) {
1067        DeviceConfig deviceConfig;
1068        GetValueIfFindKey<DeviceConfig>(jsonObject,
1069            jsonObjectEnd,
1070            APP_TV,
1071            deviceConfig,
1072            JsonType::OBJECT,
1073            false,
1074            g_parseResult,
1075            ArrayType::NOT_ARRAY);
1076        app.deviceConfigs[APP_TV] = deviceConfig;
1077    }
1078    if (jsonObject.find(APP_WEARABLE) != jsonObjectEnd) {
1079        DeviceConfig deviceConfig;
1080        GetValueIfFindKey<DeviceConfig>(jsonObject,
1081            jsonObjectEnd,
1082            APP_WEARABLE,
1083            deviceConfig,
1084            JsonType::OBJECT,
1085            false,
1086            g_parseResult,
1087            ArrayType::NOT_ARRAY);
1088        app.deviceConfigs[APP_WEARABLE] = deviceConfig;
1089    }
1090    if (jsonObject.find(APP_LITE_WEARABLE) != jsonObjectEnd) {
1091        DeviceConfig deviceConfig;
1092        GetValueIfFindKey<DeviceConfig>(jsonObject,
1093            jsonObjectEnd,
1094            APP_LITE_WEARABLE,
1095            deviceConfig,
1096            JsonType::OBJECT,
1097            false,
1098            g_parseResult,
1099            ArrayType::NOT_ARRAY);
1100        app.deviceConfigs[APP_LITE_WEARABLE] = deviceConfig;
1101    }
1102    if (jsonObject.find(APP_CAR) != jsonObjectEnd) {
1103        DeviceConfig deviceConfig;
1104        GetValueIfFindKey<DeviceConfig>(jsonObject,
1105            jsonObjectEnd,
1106            APP_CAR,
1107            deviceConfig,
1108            JsonType::OBJECT,
1109            false,
1110            g_parseResult,
1111            ArrayType::NOT_ARRAY);
1112        app.deviceConfigs[APP_CAR] = deviceConfig;
1113    }
1114    if (jsonObject.find(APP_SMART_VISION) != jsonObjectEnd) {
1115        DeviceConfig deviceConfig;
1116        GetValueIfFindKey<DeviceConfig>(jsonObject,
1117            jsonObjectEnd,
1118            APP_SMART_VISION,
1119            deviceConfig,
1120            JsonType::OBJECT,
1121            false,
1122            g_parseResult,
1123            ArrayType::NOT_ARRAY);
1124        app.deviceConfigs[APP_SMART_VISION] = deviceConfig;
1125    }
1126    if (jsonObject.find(APP_ROUTER) != jsonObjectEnd) {
1127        DeviceConfig deviceConfig;
1128        GetValueIfFindKey<DeviceConfig>(jsonObject,
1129            jsonObjectEnd,
1130            APP_ROUTER,
1131            deviceConfig,
1132            JsonType::OBJECT,
1133            false,
1134            g_parseResult,
1135            ArrayType::NOT_ARRAY);
1136        app.deviceConfigs[APP_ROUTER] = deviceConfig;
1137    }
1138    if (jsonObject.find(APP_TWO_IN_ONE) != jsonObjectEnd) {
1139        DeviceConfig deviceConfig;
1140        GetValueIfFindKey<DeviceConfig>(jsonObject,
1141            jsonObjectEnd,
1142            APP_TWO_IN_ONE,
1143            deviceConfig,
1144            JsonType::OBJECT,
1145            false,
1146            g_parseResult,
1147            ArrayType::NOT_ARRAY);
1148        app.deviceConfigs[APP_TWO_IN_ONE] = deviceConfig;
1149    }
1150    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1151        jsonObjectEnd,
1152        APP_MULTI_PROJECTS,
1153        app.multiProjects,
1154        false,
1155        g_parseResult);
1156    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1157        jsonObjectEnd,
1158        APP_TARGET_BUNDLE_NAME,
1159        app.targetBundle,
1160        false,
1161        g_parseResult);
1162    GetValueIfFindKey<int32_t>(jsonObject,
1163        jsonObjectEnd,
1164        APP_TARGET_PRIORITY,
1165        app.targetPriority,
1166        JsonType::NUMBER,
1167        false,
1168        g_parseResult,
1169        ArrayType::NOT_ARRAY);
1170    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1171        jsonObjectEnd,
1172        COMPILE_SDK_VERSION,
1173        app.compileSdkVersion,
1174        false,
1175        g_parseResult);
1176    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1177        jsonObjectEnd,
1178        COMPILE_SDK_TYPE,
1179        app.compileSdkType,
1180        false,
1181        g_parseResult);
1182    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1183        jsonObjectEnd,
1184        APP_GWP_ASAN_ENABLED,
1185        app.gwpAsanEnabled,
1186        false,
1187        g_parseResult);
1188    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1189        jsonObjectEnd,
1190        APP_TSAN_ENABLED,
1191        app.tsanEnabled,
1192        false,
1193        g_parseResult);
1194    GetValueIfFindKey<std::vector<ApplicationEnvironment>>(jsonObject,
1195        jsonObjectEnd,
1196        MODULE_APP_ENVIRONMENTS,
1197        app.appEnvironments,
1198        JsonType::ARRAY,
1199        false,
1200        g_parseResult,
1201        ArrayType::OBJECT);
1202    GetValueIfFindKey<MultiAppMode>(jsonObject,
1203        jsonObjectEnd,
1204        APP_MULTI_APP_MODE,
1205        app.multiAppMode,
1206        JsonType::OBJECT,
1207        false,
1208        g_parseResult,
1209        ArrayType::NOT_ARRAY);
1210    GetValueIfFindKey<int32_t>(jsonObject,
1211        jsonObjectEnd,
1212        APP_MAX_CHILD_PROCESS,
1213        app.maxChildProcess,
1214        JsonType::NUMBER,
1215        false,
1216        g_parseResult,
1217        ArrayType::NOT_ARRAY);
1218    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1219        jsonObjectEnd,
1220        APP_HWASAN_ENABLED,
1221        app.hwasanEnabled,
1222        false,
1223        g_parseResult);
1224    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1225        jsonObjectEnd,
1226        APP_CONFIGURATION,
1227        app.configuration,
1228        false,
1229        g_parseResult);
1230    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1231        jsonObjectEnd,
1232        APP_CLOUD_FILE_SYNC_ENABLED,
1233        app.cloudFileSyncEnabled,
1234        false,
1235        g_parseResult);
1236    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1237        jsonObjectEnd,
1238        APP_UBSAN_ENABLED,
1239        app.ubsanEnabled,
1240        false,
1241        g_parseResult);
1242}
1243
1244void from_json(const nlohmann::json &jsonObject, Module &module)
1245{
1246    APP_LOGD("read module tag from module.json");
1247    const auto &jsonObjectEnd = jsonObject.end();
1248    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1249        jsonObjectEnd,
1250        MODULE_NAME,
1251        module.name,
1252        true,
1253        g_parseResult);
1254    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1255        jsonObjectEnd,
1256        MODULE_TYPE,
1257        module.type,
1258        true,
1259        g_parseResult);
1260    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1261        jsonObjectEnd,
1262        MODULE_DEVICE_TYPES,
1263        module.deviceTypes,
1264        JsonType::ARRAY,
1265        true,
1266        g_parseResult,
1267        ArrayType::STRING);
1268    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1269        jsonObjectEnd,
1270        MODULE_DELIVERY_WITH_INSTALL,
1271        module.deliveryWithInstall,
1272        true,
1273        g_parseResult);
1274    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1275        jsonObjectEnd,
1276        MODULE_PAGES,
1277        module.pages,
1278        false,
1279        g_parseResult);
1280    // both srcEntry and srcEntrance can be configured, but srcEntry has higher priority
1281    if (jsonObject.find(SRC_ENTRY) != jsonObject.end()) {
1282        BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1283            jsonObjectEnd,
1284            SRC_ENTRY,
1285            module.srcEntrance,
1286            false,
1287            g_parseResult);
1288    } else {
1289        BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1290            jsonObjectEnd,
1291            SRC_ENTRANCE,
1292            module.srcEntrance,
1293            false,
1294            g_parseResult);
1295    }
1296    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1297        jsonObjectEnd,
1298        DESCRIPTION,
1299        module.description,
1300        false,
1301        g_parseResult);
1302    GetValueIfFindKey<uint32_t>(jsonObject,
1303        jsonObjectEnd,
1304        DESCRIPTION_ID,
1305        module.descriptionId,
1306        JsonType::NUMBER,
1307        false,
1308        g_parseResult,
1309        ArrayType::NOT_ARRAY);
1310    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1311        jsonObjectEnd,
1312        MODULE_PROCESS,
1313        module.process,
1314        false,
1315        g_parseResult);
1316    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1317        jsonObjectEnd,
1318        MODULE_MAIN_ELEMENT,
1319        module.mainElement,
1320        false,
1321        g_parseResult);
1322    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1323        jsonObjectEnd,
1324        MODULE_INSTALLATION_FREE,
1325        module.installationFree,
1326        false,
1327        g_parseResult);
1328    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1329        jsonObjectEnd,
1330        MODULE_VIRTUAL_MACHINE,
1331        module.virtualMachine,
1332        false,
1333        g_parseResult);
1334    GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
1335        jsonObjectEnd,
1336        META_DATA,
1337        module.metadata,
1338        JsonType::ARRAY,
1339        false,
1340        g_parseResult,
1341        ArrayType::OBJECT);
1342    GetValueIfFindKey<std::vector<HnpPackage>>(jsonObject,
1343        jsonObjectEnd,
1344        MODULE_HNP_PACKAGE,
1345        module.hnpPackages,
1346        JsonType::ARRAY,
1347        false,
1348        g_parseResult,
1349        ArrayType::OBJECT);
1350    GetValueIfFindKey<std::vector<Ability>>(jsonObject,
1351        jsonObjectEnd,
1352        MODULE_ABILITIES,
1353        module.abilities,
1354        JsonType::ARRAY,
1355        false,
1356        g_parseResult,
1357        ArrayType::OBJECT);
1358    GetValueIfFindKey<std::vector<Extension>>(jsonObject,
1359        jsonObjectEnd,
1360        MODULE_EXTENSION_ABILITIES,
1361        module.extensionAbilities,
1362        JsonType::ARRAY,
1363        false,
1364        g_parseResult,
1365        ArrayType::OBJECT);
1366    GetValueIfFindKey<std::vector<RequestPermission>>(jsonObject,
1367        jsonObjectEnd,
1368        MODULE_REQUEST_PERMISSIONS,
1369        module.requestPermissions,
1370        JsonType::ARRAY,
1371        false,
1372        g_parseResult,
1373        ArrayType::OBJECT);
1374    GetValueIfFindKey<std::vector<DefinePermission>>(jsonObject,
1375        jsonObjectEnd,
1376        MODULE_DEFINE_PERMISSIONS,
1377        module.definePermissions,
1378        JsonType::ARRAY,
1379        false,
1380        g_parseResult,
1381        ArrayType::OBJECT);
1382    GetValueIfFindKey<std::vector<Dependency>>(jsonObject,
1383        jsonObjectEnd,
1384        MODULE_DEPENDENCIES,
1385        module.dependencies,
1386        JsonType::ARRAY,
1387        false,
1388        g_parseResult,
1389        ArrayType::OBJECT);
1390    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1391        jsonObjectEnd,
1392        MODULE_COMPILE_MODE,
1393        module.compileMode,
1394        false,
1395        g_parseResult);
1396    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1397        jsonObjectEnd,
1398        MODULE_IS_LIB_ISOLATED,
1399        module.isLibIsolated,
1400        false,
1401        g_parseResult);
1402    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1403        jsonObjectEnd,
1404        MODULE_TARGET_MODULE_NAME,
1405        module.targetModule,
1406        false,
1407        g_parseResult);
1408    GetValueIfFindKey<int32_t>(jsonObject,
1409        jsonObjectEnd,
1410        MODULE_TARGET_PRIORITY,
1411        module.targetPriority,
1412        JsonType::NUMBER,
1413        false,
1414        g_parseResult,
1415        ArrayType::NOT_ARRAY);
1416    GetValueIfFindKey<std::vector<ProxyData>>(jsonObject,
1417        jsonObjectEnd,
1418        MODULE_PROXY_DATAS,
1419        module.proxyDatas,
1420        JsonType::ARRAY,
1421        false,
1422        g_parseResult,
1423        ArrayType::OBJECT);
1424    GetValueIfFindKey<std::vector<ProxyData>>(jsonObject,
1425        jsonObjectEnd,
1426        MODULE_PROXY_DATA,
1427        module.proxyData,
1428        JsonType::ARRAY,
1429        false,
1430        g_parseResult,
1431        ArrayType::OBJECT);
1432    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1433        jsonObjectEnd,
1434        MODULE_BUILD_HASH,
1435        module.buildHash,
1436        false,
1437        g_parseResult);
1438    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1439        jsonObjectEnd,
1440        MODULE_ISOLATION_MODE,
1441        module.isolationMode,
1442        false,
1443        g_parseResult);
1444    BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
1445        jsonObjectEnd,
1446        MODULE_COMPRESS_NATIVE_LIBS,
1447        module.compressNativeLibs,
1448        false,
1449        g_parseResult);
1450    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1451        jsonObjectEnd,
1452        MODULE_FILE_CONTEXT_MENU,
1453        module.fileContextMenu,
1454        false,
1455        g_parseResult);
1456    GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1457        jsonObjectEnd,
1458        MODULE_QUERY_SCHEMES,
1459        module.querySchemes,
1460        JsonType::ARRAY,
1461        false,
1462        g_parseResult,
1463        ArrayType::STRING);
1464    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1465        jsonObjectEnd,
1466        MODULE_ROUTER_MAP,
1467        module.routerMap,
1468        false,
1469        g_parseResult);
1470    GetValueIfFindKey<std::vector<AppEnvironment>>(jsonObject,
1471        jsonObjectEnd,
1472        MODULE_APP_ENVIRONMENTS,
1473        module.appEnvironments,
1474        JsonType::ARRAY,
1475        false,
1476        g_parseResult,
1477        ArrayType::OBJECT);
1478    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1479        jsonObjectEnd,
1480        MODULE_PACKAGE_NAME,
1481        module.packageName,
1482        false,
1483        g_parseResult);
1484    BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
1485        jsonObjectEnd,
1486        MODULE_APP_STARTUP,
1487        module.appStartup,
1488        false,
1489        g_parseResult);
1490}
1491
1492void from_json(const nlohmann::json &jsonObject, ModuleJson &moduleJson)
1493{
1494    const auto &jsonObjectEnd = jsonObject.end();
1495    GetValueIfFindKey<App>(jsonObject,
1496        jsonObjectEnd,
1497        APP,
1498        moduleJson.app,
1499        JsonType::OBJECT,
1500        true,
1501        g_parseResult,
1502        ArrayType::NOT_ARRAY);
1503    GetValueIfFindKey<Module>(jsonObject,
1504        jsonObjectEnd,
1505        MODULE,
1506        moduleJson.module,
1507        JsonType::OBJECT,
1508        true,
1509        g_parseResult,
1510        ArrayType::NOT_ARRAY);
1511}
1512} // namespace Profile
1513
1514namespace {
1515struct TransformParam {
1516    bool isSystemApp = false;
1517    bool isPreInstallApp = false;
1518};
1519
1520void GetMetadata(std::vector<Metadata> &metadata, const std::vector<Profile::Metadata> &profileMetadata)
1521{
1522    for (const Profile::Metadata &item : profileMetadata) {
1523        Metadata tmpMetadata;
1524        tmpMetadata.name = item.name;
1525        tmpMetadata.value = item.value;
1526        tmpMetadata.resource = item.resource;
1527        metadata.emplace_back(tmpMetadata);
1528    }
1529}
1530
1531void GetHnpPackage(std::vector<HnpPackage> &hnpPackage, const std::vector<Profile::HnpPackage> &profileHnpPackage)
1532{
1533    for (const Profile::HnpPackage &item : profileHnpPackage) {
1534        HnpPackage tmpHnpPackage;
1535        tmpHnpPackage.package = item.package;
1536        tmpHnpPackage.type = item.type;
1537        hnpPackage.emplace_back(tmpHnpPackage);
1538    }
1539}
1540
1541bool CheckBundleNameIsValid(const std::string &bundleName)
1542{
1543    if (bundleName.empty()) {
1544        APP_LOGE("bundleName is empty");
1545        return false;
1546    }
1547    if (bundleName.size() < Constants::MIN_BUNDLE_NAME || bundleName.size() > Constants::MAX_BUNDLE_NAME) {
1548        APP_LOGE("bundleName size too long or too short");
1549        return false;
1550    }
1551    char head = bundleName.at(0);
1552    if (!isalpha(head)) {
1553        return false;
1554    }
1555    for (const auto &c : bundleName) {
1556        if (!isalnum(c) && (c != '.') && (c != '_')) {
1557            return false;
1558        }
1559    }
1560    return true;
1561}
1562
1563bool CheckModuleNameIsValid(const std::string &moduleName)
1564{
1565    if (moduleName.empty()) {
1566        APP_LOGE("module name is empty");
1567        return false;
1568    }
1569    if (moduleName.size() > MAX_MODULE_NAME) {
1570        APP_LOGE("module name size too long");
1571        return false;
1572    }
1573    if (moduleName.find(ServiceConstants::RELATIVE_PATH) != std::string::npos) {
1574        return false;
1575    }
1576    if (moduleName.find(ServiceConstants::MODULE_NAME_SEPARATOR) != std::string::npos) {
1577        APP_LOGE("module name should not contain ,");
1578        return false;
1579    }
1580    return true;
1581}
1582
1583void UpdateNativeSoAttrs(
1584    const std::string &cpuAbi,
1585    const std::string &soRelativePath,
1586    bool isLibIsolated,
1587    InnerBundleInfo &innerBundleInfo)
1588{
1589    APP_LOGD("cpuAbi %{public}s, soRelativePath : %{public}s, isLibIsolated : %{public}d",
1590        cpuAbi.c_str(), soRelativePath.c_str(), isLibIsolated);
1591    innerBundleInfo.SetCpuAbi(cpuAbi);
1592    if (!innerBundleInfo.IsCompressNativeLibs(innerBundleInfo.GetCurModuleName())) {
1593        APP_LOGD("UpdateNativeSoAttrs compressNativeLibs is false, no need to decompress so");
1594        if (!isLibIsolated) {
1595            innerBundleInfo.SetNativeLibraryPath(soRelativePath);
1596        }
1597        if (!soRelativePath.empty()) {
1598            innerBundleInfo.SetModuleNativeLibraryPath(ServiceConstants::LIBS + cpuAbi);
1599            innerBundleInfo.SetSharedModuleNativeLibraryPath(ServiceConstants::LIBS + cpuAbi);
1600        }
1601        innerBundleInfo.SetModuleCpuAbi(cpuAbi);
1602        return;
1603    }
1604    if (!isLibIsolated) {
1605        innerBundleInfo.SetNativeLibraryPath(soRelativePath);
1606        return;
1607    }
1608
1609    innerBundleInfo.SetModuleNativeLibraryPath(
1610        innerBundleInfo.GetCurModuleName() + ServiceConstants::PATH_SEPARATOR + soRelativePath);
1611    innerBundleInfo.SetSharedModuleNativeLibraryPath(
1612        innerBundleInfo.GetCurModuleName() + ServiceConstants::PATH_SEPARATOR + soRelativePath);
1613    innerBundleInfo.SetModuleCpuAbi(cpuAbi);
1614}
1615
1616bool ParserNativeSo(
1617    const Profile::ModuleJson &moduleJson,
1618    const BundleExtractor &bundleExtractor,
1619    InnerBundleInfo &innerBundleInfo)
1620{
1621    std::string abis = GetAbiList();
1622    std::vector<std::string> abiList;
1623    SplitStr(abis, ServiceConstants::ABI_SEPARATOR, abiList, false, false);
1624    if (abiList.empty()) {
1625        APP_LOGD("Abi is empty");
1626        return false;
1627    }
1628
1629    bool isDefault =
1630        std::find(abiList.begin(), abiList.end(), ServiceConstants::ABI_DEFAULT) != abiList.end();
1631    bool isSystemLib64Exist = BundleUtil::IsExistDir(ServiceConstants::SYSTEM_LIB64);
1632    APP_LOGD("abi list : %{public}s, isDefault : %{public}d", abis.c_str(), isDefault);
1633    std::string cpuAbi;
1634    std::string soRelativePath;
1635    bool soExist = bundleExtractor.IsDirExist(ServiceConstants::LIBS);
1636    if (!soExist) {
1637        APP_LOGD("so not exist");
1638        if (isDefault) {
1639            cpuAbi = isSystemLib64Exist ? ServiceConstants::ARM64_V8A : ServiceConstants::ARM_EABI_V7A;
1640            UpdateNativeSoAttrs(cpuAbi, soRelativePath, false, innerBundleInfo);
1641            return true;
1642        }
1643
1644        for (const auto &abi : abiList) {
1645            if (ServiceConstants::ABI_MAP.find(abi) != ServiceConstants::ABI_MAP.end()) {
1646                cpuAbi = abi;
1647                UpdateNativeSoAttrs(cpuAbi, soRelativePath, false, innerBundleInfo);
1648                return true;
1649            }
1650        }
1651
1652        return false;
1653    }
1654
1655    APP_LOGD("so exist");
1656    bool isLibIsolated = moduleJson.module.isLibIsolated;
1657    if (isDefault) {
1658        if (isSystemLib64Exist) {
1659            if (bundleExtractor.IsDirExist(std::string(ServiceConstants::LIBS) + ServiceConstants::ARM64_V8A)) {
1660                cpuAbi = ServiceConstants::ARM64_V8A;
1661                soRelativePath = ServiceConstants::LIBS + ServiceConstants::ABI_MAP.at(ServiceConstants::ARM64_V8A);
1662                UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
1663                return true;
1664            }
1665
1666            return false;
1667        }
1668
1669        if (bundleExtractor.IsDirExist(std::string(ServiceConstants::LIBS) + ServiceConstants::ARM_EABI_V7A)) {
1670            cpuAbi = ServiceConstants::ARM_EABI_V7A;
1671            soRelativePath = ServiceConstants::LIBS + ServiceConstants::ABI_MAP.at(ServiceConstants::ARM_EABI_V7A);
1672            UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
1673            return true;
1674        }
1675
1676        if (bundleExtractor.IsDirExist(std::string(ServiceConstants::LIBS) + ServiceConstants::ARM_EABI)) {
1677            cpuAbi = ServiceConstants::ARM_EABI;
1678            soRelativePath = ServiceConstants::LIBS + ServiceConstants::ABI_MAP.at(ServiceConstants::ARM_EABI);
1679            UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
1680            return true;
1681        }
1682
1683        return false;
1684    }
1685
1686    for (const auto &abi : abiList) {
1687        std::string libsPath;
1688        libsPath.append(ServiceConstants::LIBS).append(abi).append(ServiceConstants::PATH_SEPARATOR);
1689        if (ServiceConstants::ABI_MAP.find(abi) != ServiceConstants::ABI_MAP.end() &&
1690            bundleExtractor.IsDirExist(libsPath)) {
1691            cpuAbi = abi;
1692            soRelativePath = ServiceConstants::LIBS + ServiceConstants::ABI_MAP.at(abi);
1693            UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
1694            return true;
1695        }
1696    }
1697
1698    return false;
1699}
1700
1701bool ParserAtomicModuleConfig(const nlohmann::json &jsonObject, InnerBundleInfo &innerBundleInfo)
1702{
1703    nlohmann::json moduleJson = jsonObject.at(Profile::MODULE);
1704    std::vector<std::string> preloads;
1705    std::string moduleName = moduleJson.at(Profile::MODULE_NAME);
1706    if (moduleJson.contains(Profile::ATOMIC_SERVICE)) {
1707        nlohmann::json moduleAtomicObj = moduleJson.at(Profile::ATOMIC_SERVICE);
1708        if (moduleAtomicObj.contains(Profile::MODULE_ATOMIC_SERVICE_PRELOADS)) {
1709            nlohmann::json preloadObj = moduleAtomicObj.at(Profile::MODULE_ATOMIC_SERVICE_PRELOADS);
1710            if (preloadObj.empty()) {
1711                APP_LOGD("preloadObj is empty");
1712                return true;
1713            }
1714            if (preloadObj.size() > Constants::MAX_JSON_ARRAY_LENGTH) {
1715                APP_LOGE("preloads config in module.json is oversize");
1716                return false;
1717            }
1718            for (const auto &preload : preloadObj) {
1719                if (preload.contains(Profile::PRELOADS_MODULE_NAME)) {
1720                    std::string preloadName = preload.at(Profile::PRELOADS_MODULE_NAME);
1721                    preloads.emplace_back(preloadName);
1722                } else {
1723                    APP_LOGE("preloads must have moduleName");
1724                    return false;
1725                }
1726            }
1727        }
1728    }
1729    innerBundleInfo.SetInnerModuleAtomicPreload(moduleName, preloads);
1730    return true;
1731}
1732
1733bool ParserAtomicConfig(const nlohmann::json &jsonObject, InnerBundleInfo &innerBundleInfo)
1734{
1735    if (!jsonObject.contains(Profile::MODULE) || !jsonObject.contains(Profile::APP)) {
1736        APP_LOGE("ParserAtomicConfig failed due to bad module.json");
1737        return false;
1738    }
1739    nlohmann::json appJson = jsonObject.at(Profile::APP);
1740    nlohmann::json moduleJson = jsonObject.at(Profile::MODULE);
1741    if (!moduleJson.is_object() || !appJson.is_object()) {
1742        APP_LOGE("module.json file lacks of invalid module or app properties");
1743        return false;
1744    }
1745    BundleType bundleType = BundleType::APP;
1746    if (appJson.contains(Profile::BUNDLE_TYPE)) {
1747        if (appJson.at(Profile::BUNDLE_TYPE) == Profile::BUNDLE_TYPE_ATOMIC_SERVICE) {
1748            bundleType = BundleType::ATOMIC_SERVICE;
1749        } else if (appJson.at(Profile::BUNDLE_TYPE) == Profile::BUNDLE_TYPE_SHARED) {
1750            bundleType = BundleType::SHARED;
1751        } else if (appJson.at(Profile::BUNDLE_TYPE) == Profile::BUNDLE_TYPE_APP_SERVICE_FWK) {
1752            bundleType = BundleType::APP_SERVICE_FWK;
1753        }
1754    }
1755
1756    innerBundleInfo.SetApplicationBundleType(bundleType);
1757    if (!ParserAtomicModuleConfig(jsonObject, innerBundleInfo)) {
1758        APP_LOGE("parse module atomicService failed");
1759        return false;
1760    }
1761    return true;
1762}
1763
1764bool ParserArkNativeFilePath(
1765    const Profile::ModuleJson &moduleJson,
1766    const BundleExtractor &bundleExtractor,
1767    InnerBundleInfo &innerBundleInfo)
1768{
1769    std::string abis = GetAbiList();
1770    std::vector<std::string> abiList;
1771    SplitStr(abis, ServiceConstants::ABI_SEPARATOR, abiList, false, false);
1772    if (abiList.empty()) {
1773        APP_LOGD("Abi is empty");
1774        return false;
1775    }
1776
1777    bool isDefault =
1778        std::find(abiList.begin(), abiList.end(), ServiceConstants::ABI_DEFAULT) != abiList.end();
1779    bool isSystemLib64Exist = BundleUtil::IsExistDir(ServiceConstants::SYSTEM_LIB64);
1780    APP_LOGD("abi list : %{public}s, isDefault : %{public}d", abis.c_str(), isDefault);
1781    bool anExist = bundleExtractor.IsDirExist(ServiceConstants::AN);
1782    if (!anExist) {
1783        APP_LOGD("an not exist");
1784        return true;
1785    }
1786
1787    APP_LOGD("an exist");
1788    if (isDefault) {
1789        if (isSystemLib64Exist) {
1790            if (bundleExtractor.IsDirExist(std::string(ServiceConstants::AN) + ServiceConstants::ARM64_V8A)) {
1791                innerBundleInfo.SetArkNativeFileAbi(ServiceConstants::ARM64_V8A);
1792                return true;
1793            }
1794
1795            return false;
1796        }
1797
1798        if (bundleExtractor.IsDirExist(std::string(ServiceConstants::AN) + ServiceConstants::ARM_EABI_V7A)) {
1799            innerBundleInfo.SetArkNativeFileAbi(ServiceConstants::ARM_EABI_V7A);
1800            return true;
1801        }
1802
1803        if (bundleExtractor.IsDirExist(std::string(ServiceConstants::AN) + ServiceConstants::ARM_EABI)) {
1804            innerBundleInfo.SetArkNativeFileAbi(ServiceConstants::ARM_EABI);
1805            return true;
1806        }
1807
1808        return false;
1809    }
1810
1811    for (const auto &abi : abiList) {
1812        std::string libsPath;
1813        libsPath.append(ServiceConstants::AN).append(abi).append(ServiceConstants::PATH_SEPARATOR);
1814        if (ServiceConstants::ABI_MAP.find(abi) != ServiceConstants::ABI_MAP.end() &&
1815            bundleExtractor.IsDirExist(libsPath)) {
1816            innerBundleInfo.SetArkNativeFileAbi(abi);
1817            return true;
1818        }
1819    }
1820
1821    return false;
1822}
1823
1824MultiAppModeType ToMultiAppModeType(const std::string &type)
1825{
1826    auto iter = Profile::MULTI_APP_MODE_MAP.find(type);
1827    if (iter != Profile::MULTI_APP_MODE_MAP.end()) {
1828        return iter->second;
1829    }
1830    return MultiAppModeType::UNSPECIFIED;
1831}
1832
1833void ToInnerProfileConfiguration(
1834    const BundleExtractor &bundleExtractor,
1835    const std::string &configuration,
1836    std::string &result)
1837{
1838    constexpr const char* PROFILE_PATH = "resources/base/profile/";
1839    constexpr const char* PROFILE_PREFIX = "$profile:";
1840    constexpr const char* JSON_SUFFIX = ".json";
1841    auto pos = configuration.find(PROFILE_PREFIX);
1842    if (pos == std::string::npos) {
1843        APP_LOGE("invalid profile configuration");
1844        return;
1845    }
1846    std::string profileConfiguration = configuration.substr(pos + strlen(PROFILE_PREFIX));
1847    std::string profilePath = PROFILE_PATH + profileConfiguration + JSON_SUFFIX;
1848
1849    if (!bundleExtractor.HasEntry(profilePath)) {
1850        APP_LOGI("profile not exist");
1851        return;
1852    }
1853    std::stringstream profileStream;
1854    if (!bundleExtractor.ExtractByName(profilePath, profileStream)) {
1855        APP_LOGE("extract profile failed");
1856        return;
1857    }
1858    result = profileStream.str();
1859}
1860
1861bool ToApplicationInfo(
1862    const Profile::ModuleJson &moduleJson,
1863    const BundleExtractor &bundleExtractor,
1864    const TransformParam &transformParam,
1865    ApplicationInfo &applicationInfo)
1866{
1867    APP_LOGD("transform ModuleJson to ApplicationInfo");
1868    auto app = moduleJson.app;
1869    applicationInfo.name = app.bundleName;
1870    applicationInfo.bundleName = app.bundleName;
1871
1872    applicationInfo.versionCode = static_cast<uint32_t>(app.versionCode);
1873    applicationInfo.versionName = app.versionName;
1874    if (app.minCompatibleVersionCode != -1) {
1875        applicationInfo.minCompatibleVersionCode = app.minCompatibleVersionCode;
1876    } else {
1877        applicationInfo.minCompatibleVersionCode = static_cast<int32_t>(applicationInfo.versionCode);
1878    }
1879
1880    applicationInfo.apiCompatibleVersion = app.minAPIVersion;
1881    applicationInfo.apiTargetVersion = app.targetAPIVersion;
1882
1883    applicationInfo.iconPath = app.icon;
1884    applicationInfo.iconId = app.iconId;
1885    applicationInfo.label = app.label;
1886    applicationInfo.labelId = app.labelId;
1887    applicationInfo.description = app.description;
1888    applicationInfo.descriptionId = app.descriptionId;
1889    applicationInfo.iconResource =
1890        BundleUtil::GetResource(app.bundleName, moduleJson.module.name, app.iconId);
1891    applicationInfo.labelResource =
1892        BundleUtil::GetResource(app.bundleName, moduleJson.module.name, app.labelId);
1893    applicationInfo.descriptionResource =
1894        BundleUtil::GetResource(app.bundleName, moduleJson.module.name, app.descriptionId);
1895    applicationInfo.targetBundleList = app.targetBundleList;
1896
1897    if (transformParam.isSystemApp && transformParam.isPreInstallApp) {
1898        applicationInfo.keepAlive = app.keepAlive;
1899        applicationInfo.singleton = app.singleton;
1900        applicationInfo.userDataClearable = app.userDataClearable;
1901        if (app.removable.first) {
1902            applicationInfo.removable = app.removable.second;
1903        } else {
1904            applicationInfo.removable = false;
1905        }
1906        applicationInfo.accessible = app.accessible;
1907    }
1908
1909    applicationInfo.apiReleaseType = app.apiReleaseType;
1910    applicationInfo.debug = app.debug;
1911    applicationInfo.deviceId = ServiceConstants::CURRENT_DEVICE_ID;
1912    applicationInfo.distributedNotificationEnabled = true;
1913    applicationInfo.entityType = Profile::APP_ENTITY_TYPE_DEFAULT_VALUE;
1914    applicationInfo.vendor = app.vendor;
1915    applicationInfo.asanEnabled = app.asanEnabled;
1916    if (app.bundleType == Profile::BUNDLE_TYPE_ATOMIC_SERVICE) {
1917        applicationInfo.bundleType = BundleType::ATOMIC_SERVICE;
1918    }
1919
1920    // device adapt
1921    std::string deviceType = GetDeviceType();
1922    APP_LOGD("deviceType : %{public}s", deviceType.c_str());
1923    if (app.deviceConfigs.find(deviceType) != app.deviceConfigs.end()) {
1924        Profile::DeviceConfig deviceConfig = app.deviceConfigs.at(deviceType);
1925        if (deviceConfig.minAPIVersion.first) {
1926            applicationInfo.apiCompatibleVersion = static_cast<uint32_t>(deviceConfig.minAPIVersion.second);
1927        }
1928        if (applicationInfo.isSystemApp && transformParam.isPreInstallApp) {
1929            if (deviceConfig.keepAlive.first) {
1930                applicationInfo.keepAlive = deviceConfig.keepAlive.second;
1931            }
1932            if (deviceConfig.singleton.first) {
1933                applicationInfo.singleton = deviceConfig.singleton.second;
1934            }
1935            if (deviceConfig.userDataClearable.first) {
1936                applicationInfo.userDataClearable = deviceConfig.userDataClearable.second;
1937            }
1938            if (deviceConfig.removable.first) {
1939                applicationInfo.removable = deviceConfig.removable.second;
1940            }
1941            if (deviceConfig.accessible.first) {
1942                applicationInfo.accessible = deviceConfig.accessible.second;
1943            }
1944        }
1945    }
1946
1947    applicationInfo.enabled = true;
1948    applicationInfo.multiProjects = app.multiProjects;
1949    applicationInfo.process = app.bundleName;
1950    applicationInfo.targetBundleName = app.targetBundle;
1951    applicationInfo.targetPriority = app.targetPriority;
1952
1953    auto iterBundleType = std::find_if(std::begin(Profile::BUNDLE_TYPE_MAP),
1954        std::end(Profile::BUNDLE_TYPE_MAP),
1955        [&app](const auto &item) { return item.first == app.bundleType; });
1956    if (iterBundleType != Profile::BUNDLE_TYPE_MAP.end()) {
1957        applicationInfo.bundleType = iterBundleType->second;
1958    }
1959    applicationInfo.compileSdkVersion = app.compileSdkVersion;
1960    applicationInfo.compileSdkType = app.compileSdkType;
1961    applicationInfo.gwpAsanEnabled = app.gwpAsanEnabled;
1962    applicationInfo.tsanEnabled = app.tsanEnabled;
1963    applicationInfo.hwasanEnabled = app.hwasanEnabled;
1964    applicationInfo.ubsanEnabled = app.ubsanEnabled;
1965    applicationInfo.appEnvironments = app.appEnvironments;
1966    // bundleType is app && moduleType is entry or feature
1967    if (applicationInfo.bundleType == BundleType::APP &&
1968        (moduleJson.module.type == Profile::MODULE_TYPE_ENTRY ||
1969        moduleJson.module.type == Profile::MODULE_TYPE_FEATURE)) {
1970        applicationInfo.multiAppMode.multiAppModeType = ToMultiAppModeType(app.multiAppMode.multiAppModeType);
1971        applicationInfo.multiAppMode.maxCount = app.multiAppMode.maxCount;
1972        if (applicationInfo.multiAppMode.multiAppModeType == MultiAppModeType::APP_CLONE) {
1973            int32_t maxNumber = applicationInfo.multiAppMode.maxCount;
1974            if (maxNumber <= Constants::INITIAL_APP_INDEX || maxNumber > ServiceConstants::CLONE_APP_INDEX_MAX) {
1975                return false;
1976            }
1977        }
1978    }
1979    applicationInfo.maxChildProcess = app.maxChildProcess;
1980    if (app.configuration != "") {
1981        ToInnerProfileConfiguration(bundleExtractor, app.configuration, applicationInfo.configuration);
1982    }
1983    applicationInfo.cloudFileSyncEnabled = app.cloudFileSyncEnabled;
1984    return true;
1985}
1986
1987bool ToBundleInfo(
1988    const ApplicationInfo &applicationInfo,
1989    const InnerModuleInfo &innerModuleInfo,
1990    const TransformParam &transformParam,
1991    BundleInfo &bundleInfo)
1992{
1993    bundleInfo.name = applicationInfo.bundleName;
1994
1995    bundleInfo.versionCode = static_cast<uint32_t>(applicationInfo.versionCode);
1996    bundleInfo.versionName = applicationInfo.versionName;
1997    bundleInfo.minCompatibleVersionCode = static_cast<uint32_t>(applicationInfo.minCompatibleVersionCode);
1998
1999    bundleInfo.compatibleVersion = static_cast<uint32_t>(applicationInfo.apiCompatibleVersion);
2000    bundleInfo.targetVersion = static_cast<uint32_t>(applicationInfo.apiTargetVersion);
2001
2002    bundleInfo.isKeepAlive = applicationInfo.keepAlive;
2003    bundleInfo.singleton = applicationInfo.singleton;
2004    bundleInfo.isPreInstallApp = transformParam.isPreInstallApp;
2005
2006    bundleInfo.vendor = applicationInfo.vendor;
2007    bundleInfo.releaseType = applicationInfo.apiReleaseType;
2008    bundleInfo.isNativeApp = false;
2009
2010    if (innerModuleInfo.isEntry) {
2011        bundleInfo.mainEntry = innerModuleInfo.moduleName;
2012        bundleInfo.entryModuleName = innerModuleInfo.moduleName;
2013    }
2014
2015    return true;
2016}
2017
2018uint32_t GetBackgroundModes(const std::vector<std::string> &backgroundModes)
2019{
2020    uint32_t backgroundMode = 0;
2021    size_t len = sizeof(Profile::BACKGROUND_MODES_MAP_KEY) /
2022            sizeof(Profile::BACKGROUND_MODES_MAP_KEY[0]);
2023    for (const std::string &item : backgroundModes) {
2024        for (size_t i = 0; i < len; i++) {
2025            if (item == Profile::BACKGROUND_MODES_MAP_KEY[i]) {
2026                backgroundMode |= Profile::BACKGROUND_MODES_MAP_VALUE[i];
2027                break;
2028            }
2029        }
2030    }
2031    return backgroundMode;
2032}
2033
2034inline CompileMode ConvertCompileMode(const std::string& compileMode)
2035{
2036    if (compileMode == Profile::COMPILE_MODE_ES_MODULE) {
2037        return CompileMode::ES_MODULE;
2038    } else {
2039        return CompileMode::JS_BUNDLE;
2040    }
2041}
2042
2043std::set<SupportWindowMode> ConvertToAbilityWindowMode(const std::vector<std::string> &windowModes,
2044    const std::unordered_map<std::string, SupportWindowMode> &windowMap)
2045{
2046    std::set<SupportWindowMode> modes;
2047    for_each(windowModes.begin(), windowModes.end(),
2048        [&windowMap, &modes](const auto &mode)->decltype(auto) {
2049        if (windowMap.find(mode) != windowMap.end()) {
2050            modes.emplace(windowMap.at(mode));
2051        }
2052    });
2053    if (modes.empty()) {
2054        modes.insert(SupportWindowMode::FULLSCREEN);
2055        modes.insert(SupportWindowMode::SPLIT);
2056        modes.insert(SupportWindowMode::FLOATING);
2057    }
2058    return modes;
2059}
2060
2061bool ToAbilityInfo(
2062    const Profile::ModuleJson &moduleJson,
2063    const Profile::Ability &ability,
2064    const TransformParam &transformParam,
2065    AbilityInfo &abilityInfo)
2066{
2067    APP_LOGD("transform ModuleJson to AbilityInfo");
2068    abilityInfo.name = ability.name;
2069    abilityInfo.srcEntrance = ability.srcEntrance;
2070    abilityInfo.description = ability.description;
2071    abilityInfo.descriptionId = ability.descriptionId;
2072    abilityInfo.iconPath = ability.icon;
2073    abilityInfo.iconId = ability.iconId;
2074    abilityInfo.label = ability.label;
2075    abilityInfo.labelId = ability.labelId;
2076    abilityInfo.priority = ability.priority;
2077    abilityInfo.excludeFromMissions = ability.excludeFromMissions;
2078    abilityInfo.unclearableMission = ability.unclearableMission;
2079    abilityInfo.excludeFromDock = ability.excludeFromDock;
2080    abilityInfo.preferMultiWindowOrientation = ability.preferMultiWindowOrientation;
2081    abilityInfo.recoverable = ability.recoverable;
2082    abilityInfo.permissions = ability.permissions;
2083    abilityInfo.visible = ability.visible;
2084    abilityInfo.continuable = ability.continuable;
2085    abilityInfo.isolationProcess = ability.isolationProcess;
2086    abilityInfo.backgroundModes = GetBackgroundModes(ability.backgroundModes);
2087    GetMetadata(abilityInfo.metadata, ability.metadata);
2088    abilityInfo.package = moduleJson.module.name;
2089    abilityInfo.bundleName = moduleJson.app.bundleName;
2090    abilityInfo.moduleName = moduleJson.module.name;
2091    abilityInfo.applicationName = moduleJson.app.bundleName;
2092    auto iterLaunch = std::find_if(std::begin(Profile::LAUNCH_MODE_MAP),
2093        std::end(Profile::LAUNCH_MODE_MAP),
2094        [&ability](const auto &item) { return item.first == ability.launchType; });
2095    if (iterLaunch != Profile::LAUNCH_MODE_MAP.end()) {
2096        abilityInfo.launchMode = iterLaunch->second;
2097    }
2098    abilityInfo.enabled = true;
2099    abilityInfo.isModuleJson = true;
2100    abilityInfo.isStageBasedModel = true;
2101    abilityInfo.type = AbilityType::PAGE;
2102    for (const std::string &deviceType : moduleJson.module.deviceTypes) {
2103        abilityInfo.deviceTypes.emplace_back(deviceType);
2104    }
2105    abilityInfo.startWindowIcon = ability.startWindowIcon;
2106    abilityInfo.startWindowIconId = ability.startWindowIconId;
2107    abilityInfo.startWindowBackground = ability.startWindowBackground;
2108    abilityInfo.startWindowBackgroundId = ability.startWindowBackgroundId;
2109    abilityInfo.removeMissionAfterTerminate = ability.removeMissionAfterTerminate;
2110    abilityInfo.compileMode = ConvertCompileMode(moduleJson.module.compileMode);
2111    size_t len = sizeof(Profile::DISPLAY_ORIENTATION_MAP_KEY) /
2112            sizeof(Profile::DISPLAY_ORIENTATION_MAP_KEY[0]);
2113    for (size_t i = 0; i < len; i++) {
2114        if (ability.orientation == Profile::DISPLAY_ORIENTATION_MAP_KEY[i]) {
2115            abilityInfo.orientation = Profile::DISPLAY_ORIENTATION_MAP_VALUE[i];
2116            break;
2117        }
2118    }
2119
2120    auto modesSet = ConvertToAbilityWindowMode(ability.windowModes, Profile::WINDOW_MODE_MAP);
2121    abilityInfo.windowModes.assign(modesSet.begin(), modesSet.end());
2122
2123    if (!ability.continueBundleNames.empty()) {
2124        abilityInfo.continueBundleNames.insert(ability.continueBundleNames.begin(), ability.continueBundleNames.end());
2125    }
2126
2127    abilityInfo.maxWindowRatio = ability.maxWindowRatio;
2128    abilityInfo.minWindowRatio = ability.minWindowRatio;
2129    abilityInfo.maxWindowWidth = ability.maxWindowWidth;
2130    abilityInfo.minWindowWidth = ability.minWindowWidth;
2131    abilityInfo.maxWindowHeight = ability.maxWindowHeight;
2132    abilityInfo.minWindowHeight = ability.minWindowHeight;
2133    if (ability.continueType.empty()) {
2134        abilityInfo.continueType.emplace_back(ability.name);
2135    } else {
2136        abilityInfo.continueType = ability.continueType;
2137    }
2138    abilityInfo.orientationId = ability.orientationId;
2139    return true;
2140}
2141
2142void ToAbilitySkills(const std::vector<Skill> &skills, AbilityInfo &abilityInfo)
2143{
2144    for (const Skill &skill : skills) {
2145        abilityInfo.skills.push_back(skill);
2146    }
2147}
2148
2149void ToExtensionAbilitySkills(const std::vector<Skill> &skills, ExtensionAbilityInfo &extensionInfo)
2150{
2151    for (const Skill &skill : skills) {
2152        extensionInfo.skills.push_back(skill);
2153    }
2154}
2155
2156void ToExtensionInfo(
2157    const Profile::ModuleJson &moduleJson,
2158    const Profile::Extension &extension,
2159    const TransformParam &transformParam,
2160    ExtensionAbilityInfo &extensionInfo)
2161{
2162    APP_LOGD("transform ModuleJson to ExtensionAbilityInfo");
2163    extensionInfo.type = ConvertToExtensionAbilityType(extension.type);
2164    extensionInfo.extensionTypeName = extension.type;
2165    extensionInfo.name = extension.name;
2166    extensionInfo.srcEntrance = extension.srcEntrance;
2167    extensionInfo.icon = extension.icon;
2168    extensionInfo.iconId = extension.iconId;
2169    extensionInfo.label = extension.label;
2170    extensionInfo.labelId = extension.labelId;
2171    extensionInfo.description = extension.description;
2172    extensionInfo.descriptionId = extension.descriptionId;
2173    if (transformParam.isSystemApp && transformParam.isPreInstallApp) {
2174        extensionInfo.readPermission = extension.readPermission;
2175        extensionInfo.writePermission = extension.writePermission;
2176    }
2177    extensionInfo.priority = extension.priority;
2178    extensionInfo.uri = extension.uri;
2179    extensionInfo.permissions = extension.permissions;
2180    extensionInfo.visible = extension.visible;
2181    GetMetadata(extensionInfo.metadata, extension.metadata);
2182    extensionInfo.bundleName = moduleJson.app.bundleName;
2183    extensionInfo.moduleName = moduleJson.module.name;
2184
2185    if (extensionInfo.type != ExtensionAbilityType::SERVICE &&
2186        extensionInfo.type != ExtensionAbilityType::DATASHARE) {
2187        extensionInfo.process = extensionInfo.bundleName;
2188        extensionInfo.process.append(":");
2189        extensionInfo.process.append(extensionInfo.extensionTypeName);
2190    }
2191
2192    extensionInfo.compileMode = ConvertCompileMode(moduleJson.module.compileMode);
2193    extensionInfo.extensionProcessMode = ConvertToExtensionProcessMode(extension.extensionProcessMode);
2194    for (const std::string &dataGroup : extension.dataGroupIds) {
2195        extensionInfo.dataGroupIds.emplace_back(dataGroup);
2196    }
2197}
2198
2199bool GetPermissions(
2200    const Profile::ModuleJson &moduleJson,
2201    const TransformParam &transformParam,
2202    InnerModuleInfo &innerModuleInfo)
2203{
2204    if (moduleJson.app.bundleName == Profile::SYSTEM_RESOURCES_APP) {
2205        for (const DefinePermission &definePermission : moduleJson.module.definePermissions) {
2206            if (definePermission.name.empty()) {
2207                continue;
2208            }
2209            if (Profile::GRANT_MODE_SET.find(definePermission.grantMode) == Profile::GRANT_MODE_SET.end()) {
2210                continue;
2211            }
2212            if (Profile::AVAILABLE_LEVEL_SET.find(definePermission.availableLevel)
2213                == Profile::AVAILABLE_LEVEL_SET.end()) {
2214                continue;
2215            }
2216            if (!definePermission.availableType.empty() &&
2217                definePermission.availableType != Profile::DEFINEPERMISSION_AVAILABLE_TYPE_MDM) {
2218                APP_LOGE("availableType(%{public}s) invalid", definePermission.availableType.c_str());
2219                return false;
2220            }
2221            innerModuleInfo.definePermissions.emplace_back(definePermission);
2222        }
2223    }
2224    for (const RequestPermission &requestPermission : moduleJson.module.requestPermissions) {
2225        if (requestPermission.name.empty()) {
2226            continue;
2227        }
2228        innerModuleInfo.requestPermissions.emplace_back(requestPermission);
2229    }
2230    return true;
2231}
2232
2233bool ToInnerModuleInfo(
2234    const Profile::ModuleJson &moduleJson,
2235    const TransformParam &transformParam,
2236    const OverlayMsg &overlayMsg,
2237    InnerModuleInfo &innerModuleInfo)
2238{
2239    APP_LOGD("transform ModuleJson to InnerModuleInfo");
2240    innerModuleInfo.name = moduleJson.module.name;
2241    innerModuleInfo.modulePackage = moduleJson.module.name;
2242    innerModuleInfo.moduleName = moduleJson.module.name;
2243    innerModuleInfo.description = moduleJson.module.description;
2244    innerModuleInfo.descriptionId = moduleJson.module.descriptionId;
2245    GetMetadata(innerModuleInfo.metadata, moduleJson.module.metadata);
2246    GetHnpPackage(innerModuleInfo.hnpPackages, moduleJson.module.hnpPackages);
2247    innerModuleInfo.distro.deliveryWithInstall = moduleJson.module.deliveryWithInstall;
2248    innerModuleInfo.distro.installationFree = moduleJson.module.installationFree;
2249    innerModuleInfo.distro.moduleName = moduleJson.module.name;
2250    innerModuleInfo.installationFree = moduleJson.module.installationFree;
2251    if (Profile::MODULE_TYPE_SET.find(moduleJson.module.type) != Profile::MODULE_TYPE_SET.end()) {
2252        innerModuleInfo.distro.moduleType = moduleJson.module.type;
2253        if (moduleJson.module.type == Profile::MODULE_TYPE_ENTRY) {
2254            innerModuleInfo.isEntry = true;
2255        }
2256    }
2257
2258    innerModuleInfo.mainAbility = moduleJson.module.mainElement;
2259    innerModuleInfo.srcEntrance = moduleJson.module.srcEntrance;
2260    innerModuleInfo.process = moduleJson.module.process;
2261
2262    for (const std::string &deviceType : moduleJson.module.deviceTypes) {
2263        innerModuleInfo.deviceTypes.emplace_back(deviceType);
2264    }
2265
2266    if (Profile::VIRTUAL_MACHINE_SET.find(moduleJson.module.virtualMachine) != Profile::VIRTUAL_MACHINE_SET.end()) {
2267        innerModuleInfo.virtualMachine = moduleJson.module.virtualMachine;
2268    }
2269
2270    innerModuleInfo.uiSyntax = Profile::MODULE_UI_SYNTAX_DEFAULT_VALUE;
2271    innerModuleInfo.pages = moduleJson.module.pages;
2272    if (!GetPermissions(moduleJson, transformParam, innerModuleInfo)) {
2273        APP_LOGE("GetPermissions failed");
2274        return false;
2275    }
2276    innerModuleInfo.dependencies = moduleJson.module.dependencies;
2277    innerModuleInfo.compileMode = moduleJson.module.compileMode;
2278    innerModuleInfo.isModuleJson = true;
2279    innerModuleInfo.isStageBasedModel = true;
2280    innerModuleInfo.isLibIsolated = moduleJson.module.isLibIsolated;
2281    innerModuleInfo.targetModuleName = moduleJson.module.targetModule;
2282    if (overlayMsg.type != NON_OVERLAY_TYPE && !overlayMsg.isModulePriorityExisted) {
2283        innerModuleInfo.targetPriority = ServiceConstants::OVERLAY_MINIMUM_PRIORITY;
2284    } else {
2285        innerModuleInfo.targetPriority = moduleJson.module.targetPriority;
2286    }
2287    if (moduleJson.module.proxyDatas.empty()) {
2288        innerModuleInfo.proxyDatas = moduleJson.module.proxyData;
2289    } else {
2290        innerModuleInfo.proxyDatas = moduleJson.module.proxyDatas;
2291    }
2292    innerModuleInfo.buildHash = moduleJson.module.buildHash;
2293    innerModuleInfo.isolationMode = moduleJson.module.isolationMode;
2294    innerModuleInfo.compressNativeLibs = moduleJson.module.compressNativeLibs;
2295    innerModuleInfo.fileContextMenu = moduleJson.module.fileContextMenu;
2296
2297    if (moduleJson.module.querySchemes.size() > Profile::MAX_QUERYSCHEMES_LENGTH) {
2298        APP_LOGE("The length of the querySchemes exceeds the limit");
2299        return false;
2300    }
2301    for (const std::string &queryScheme : moduleJson.module.querySchemes) {
2302        innerModuleInfo.querySchemes.emplace_back(queryScheme);
2303    }
2304
2305    innerModuleInfo.routerMap = moduleJson.module.routerMap;
2306    // abilities and fileContextMenu store in InnerBundleInfo
2307    innerModuleInfo.appEnvironments = moduleJson.module.appEnvironments;
2308    innerModuleInfo.packageName = moduleJson.module.packageName;
2309    innerModuleInfo.appStartup = moduleJson.module.appStartup;
2310    return true;
2311}
2312
2313void SetInstallationFree(InnerModuleInfo &innerModuleInfo, BundleType bundleType)
2314{
2315    if (bundleType == BundleType::ATOMIC_SERVICE) {
2316        innerModuleInfo.distro.installationFree = true;
2317        innerModuleInfo.installationFree = true;
2318    } else {
2319        innerModuleInfo.distro.installationFree = false;
2320        innerModuleInfo.installationFree = false;
2321    }
2322}
2323
2324bool ToInnerBundleInfo(
2325    const Profile::ModuleJson &moduleJson,
2326    const BundleExtractor &bundleExtractor,
2327    const OverlayMsg &overlayMsg,
2328    InnerBundleInfo &innerBundleInfo)
2329{
2330    APP_LOGD("transform ModuleJson to InnerBundleInfo");
2331    if (!CheckBundleNameIsValid(moduleJson.app.bundleName) || !CheckModuleNameIsValid(moduleJson.module.name)) {
2332        APP_LOGE("bundle name or module name is invalid");
2333        return false;
2334    }
2335
2336    if (overlayMsg.type != NON_OVERLAY_TYPE && !CheckModuleNameIsValid(moduleJson.module.targetModule)) {
2337        APP_LOGE("target moduleName is invalid");
2338    }
2339
2340    if ((overlayMsg.type == OVERLAY_EXTERNAL_BUNDLE) && !CheckBundleNameIsValid(moduleJson.app.targetBundle)) {
2341        APP_LOGE("targetBundleName of the overlay hap is invalid");
2342        return false;
2343    }
2344
2345    TransformParam transformParam;
2346    transformParam.isPreInstallApp = innerBundleInfo.IsPreInstallApp();
2347
2348    ApplicationInfo applicationInfo;
2349    applicationInfo.isSystemApp = innerBundleInfo.GetAppType() == Constants::AppType::SYSTEM_APP;
2350    transformParam.isSystemApp = applicationInfo.isSystemApp;
2351    applicationInfo.isCompressNativeLibs = moduleJson.module.compressNativeLibs;
2352    if (!ToApplicationInfo(moduleJson, bundleExtractor, transformParam, applicationInfo)) {
2353        APP_LOGE("To applicationInfo failed");
2354        return false;
2355    }
2356
2357    InnerModuleInfo innerModuleInfo;
2358    if (!ToInnerModuleInfo(moduleJson, transformParam, overlayMsg, innerModuleInfo)) {
2359        APP_LOGE("To innerModuleInfo failed");
2360        return false;
2361    }
2362    if (moduleJson.app.targetAPIVersion % ServiceConstants::API_VERSION_MOD <= ServiceConstants::API_VERSION_THIRTEEN) {
2363        APP_LOGD("targetAPIVersion is less than 14, set isCompressNativeLibs flag to true");
2364        applicationInfo.isCompressNativeLibs = true;
2365        innerModuleInfo.compressNativeLibs = true;
2366    }
2367    innerModuleInfo.asanEnabled = applicationInfo.asanEnabled;
2368    innerModuleInfo.gwpAsanEnabled = applicationInfo.gwpAsanEnabled;
2369    innerModuleInfo.tsanEnabled = applicationInfo.tsanEnabled;
2370    innerModuleInfo.innerModuleInfoFlag = applicationInfo.hwasanEnabled ? innerModuleInfo.innerModuleInfoFlag |
2371        innerBundleInfo.GetSanitizerFlag(GetInnerModuleInfoFlag::GET_INNER_MODULE_INFO_WITH_HWASANENABLED) :
2372        innerModuleInfo.innerModuleInfoFlag &
2373        (~innerBundleInfo.GetSanitizerFlag(GetInnerModuleInfoFlag::GET_INNER_MODULE_INFO_WITH_HWASANENABLED));
2374    innerModuleInfo.innerModuleInfoFlag = applicationInfo.ubsanEnabled ? innerModuleInfo.innerModuleInfoFlag |
2375        innerBundleInfo.GetSanitizerFlag(GetInnerModuleInfoFlag::GET_INNER_MODULE_INFO_WITH_UBSANENABLED) :
2376        innerModuleInfo.innerModuleInfoFlag &
2377        (~innerBundleInfo.GetSanitizerFlag(GetInnerModuleInfoFlag::GET_INNER_MODULE_INFO_WITH_UBSANENABLED));
2378    SetInstallationFree(innerModuleInfo, applicationInfo.bundleType);
2379
2380    BundleInfo bundleInfo;
2381    ToBundleInfo(applicationInfo, innerModuleInfo, transformParam, bundleInfo);
2382
2383    // handle abilities
2384    auto entryActionMatcher = [] (const std::string &action) {
2385        return action == Constants::ACTION_HOME || action == Constants::WANT_ACTION_HOME;
2386    };
2387    bool findEntry = false;
2388    for (const Profile::Ability &ability : moduleJson.module.abilities) {
2389        AbilityInfo abilityInfo;
2390        bool isMainElement = false;
2391        ToAbilityInfo(moduleJson, ability, transformParam, abilityInfo);
2392        if (innerModuleInfo.mainAbility == abilityInfo.name) {
2393            innerModuleInfo.icon = abilityInfo.iconPath;
2394            innerModuleInfo.iconId = abilityInfo.iconId;
2395            innerModuleInfo.label = abilityInfo.label;
2396            innerModuleInfo.labelId = abilityInfo.labelId;
2397            isMainElement = true;
2398        }
2399        std::string key;
2400        key.append(moduleJson.app.bundleName).append(".")
2401            .append(moduleJson.module.name).append(".").append(abilityInfo.name);
2402        innerModuleInfo.abilityKeys.emplace_back(key);
2403        innerModuleInfo.skillKeys.emplace_back(key);
2404        innerBundleInfo.InsertSkillInfo(key, ability.skills);
2405        ToAbilitySkills(ability.skills, abilityInfo);
2406        innerBundleInfo.InsertAbilitiesInfo(key, abilityInfo);
2407        if (findEntry && !isMainElement) {
2408            continue;
2409        }
2410        // get entry ability
2411        for (const auto &skill : ability.skills) {
2412            bool isEntryAction = std::find_if(skill.actions.begin(), skill.actions.end(),
2413                entryActionMatcher) != skill.actions.end();
2414            bool isEntryEntity = std::find(skill.entities.begin(), skill.entities.end(),
2415                Constants::ENTITY_HOME) != skill.entities.end();
2416            if (isEntryAction && isEntryEntity) {
2417                innerModuleInfo.entryAbilityKey = key;
2418                innerModuleInfo.label = ability.label;
2419                innerModuleInfo.labelId = ability.labelId;
2420                // get launcher application and ability
2421                bool isLauncherEntity = std::find(skill.entities.begin(), skill.entities.end(),
2422                    ServiceConstants::FLAG_HOME_INTENT_FROM_SYSTEM) != skill.entities.end();
2423                if (isLauncherEntity && transformParam.isPreInstallApp) {
2424                    applicationInfo.isLauncherApp = true;
2425                    abilityInfo.isLauncherAbility = true;
2426                }
2427                findEntry = true;
2428                break;
2429            }
2430        }
2431    }
2432
2433    // handle extensionAbilities
2434    for (const Profile::Extension &extension : moduleJson.module.extensionAbilities) {
2435        ExtensionAbilityInfo extensionInfo;
2436        ToExtensionInfo(moduleJson, extension, transformParam, extensionInfo);
2437
2438        if (innerModuleInfo.mainAbility == extensionInfo.name) {
2439            innerModuleInfo.icon = extensionInfo.icon;
2440            innerModuleInfo.iconId = extensionInfo.iconId;
2441            innerModuleInfo.label = extensionInfo.label;
2442            innerModuleInfo.labelId = extensionInfo.labelId;
2443        }
2444
2445        if (transformParam.isPreInstallApp && !applicationInfo.isLauncherApp) {
2446            for (const auto &skill : extension.skills) {
2447                bool isEntryAction = std::find_if(skill.actions.cbegin(), skill.actions.cend(),
2448                    entryActionMatcher) != skill.actions.cend();
2449                bool isEntryEntity = std::find(skill.entities.cbegin(), skill.entities.cend(),
2450                    Constants::ENTITY_HOME) != skill.entities.cend();
2451                bool isLauncherEntity = std::find(skill.entities.cbegin(), skill.entities.cend(),
2452                    ServiceConstants::FLAG_HOME_INTENT_FROM_SYSTEM) != skill.entities.cend();
2453                if (isEntryAction && isEntryEntity && isLauncherEntity) {
2454                    applicationInfo.isLauncherApp = true;
2455                    break;
2456                }
2457            }
2458        }
2459
2460        std::string key;
2461        key.append(moduleJson.app.bundleName).append(".")
2462            .append(moduleJson.module.name).append(".").append(extension.name);
2463        innerModuleInfo.extensionKeys.emplace_back(key);
2464        innerModuleInfo.extensionSkillKeys.emplace_back(key);
2465        innerBundleInfo.InsertExtensionSkillInfo(key, extension.skills);
2466        ToExtensionAbilitySkills(extension.skills, extensionInfo);
2467        innerBundleInfo.InsertExtensionInfo(key, extensionInfo);
2468    }
2469    if (!findEntry && !transformParam.isPreInstallApp) {
2470        applicationInfo.needAppDetail = true;
2471        if (BundleUtil::IsExistDir(ServiceConstants::SYSTEM_LIB64)) {
2472            applicationInfo.appDetailAbilityLibraryPath = Profile::APP_DETAIL_ABILITY_LIBRARY_PATH_64;
2473        } else {
2474            applicationInfo.appDetailAbilityLibraryPath = Profile::APP_DETAIL_ABILITY_LIBRARY_PATH;
2475        }
2476        if ((applicationInfo.labelId == 0) && (applicationInfo.label.empty())) {
2477            applicationInfo.label = applicationInfo.bundleName;
2478        }
2479    }
2480    innerBundleInfo.SetCurrentModulePackage(moduleJson.module.name);
2481    innerBundleInfo.SetBaseApplicationInfo(applicationInfo);
2482    innerBundleInfo.SetBaseBundleInfo(bundleInfo);
2483
2484    // Here also need verify module type is shared.
2485    if (applicationInfo.bundleType == BundleType::SHARED) {
2486        innerModuleInfo.bundleType = applicationInfo.bundleType;
2487        innerModuleInfo.versionCode = applicationInfo.versionCode;
2488        innerModuleInfo.versionName = applicationInfo.versionName;
2489        innerBundleInfo.InsertInnerSharedModuleInfo(moduleJson.module.name, innerModuleInfo);
2490    }
2491
2492    innerBundleInfo.InsertInnerModuleInfo(moduleJson.module.name, innerModuleInfo);
2493    innerBundleInfo.SetOverlayType(overlayMsg.type);
2494
2495    if (overlayMsg.type == OVERLAY_EXTERNAL_BUNDLE && !overlayMsg.isAppPriorityExisted) {
2496        innerBundleInfo.SetTargetPriority(ServiceConstants::OVERLAY_MINIMUM_PRIORITY);
2497    } else {
2498        innerBundleInfo.SetTargetPriority(moduleJson.app.targetPriority);
2499    }
2500    int32_t overlayState = overlayMsg.type == OVERLAY_EXTERNAL_BUNDLE ?
2501        ServiceConstants::DEFAULT_OVERLAY_ENABLE_STATUS :
2502        ServiceConstants::DEFAULT_OVERLAY_DISABLE_STATUS;
2503    innerBundleInfo.SetOverlayState(overlayState);
2504    return true;
2505}
2506}  // namespace
2507
2508OverlayMsg ModuleProfile::ObtainOverlayType(const nlohmann::json &jsonObject) const
2509{
2510    APP_LOGD("check if overlay installation");
2511    OverlayMsg overlayMsg;
2512#ifdef BUNDLE_FRAMEWORK_OVERLAY_INSTALLATION
2513    if (!jsonObject.contains(Profile::MODULE) || !jsonObject.contains(Profile::APP)) {
2514        APP_LOGE("ObtainOverlayType failed due to bad module.json");
2515        Profile::g_parseResult = ERR_APPEXECFWK_INSTALL_FAILED_PROFILE_PARSE_FAIL;
2516        return overlayMsg;
2517    }
2518    nlohmann::json moduleJson = jsonObject.at(Profile::MODULE);
2519    nlohmann::json appJson = jsonObject.at(Profile::APP);
2520    if (!moduleJson.is_object() || !appJson.is_object()) {
2521        APP_LOGE("module.json file lacks of invalid module or app properties");
2522        Profile::g_parseResult = ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
2523        return overlayMsg;
2524    }
2525
2526    auto isTargetBundleExisted = appJson.contains(Profile::APP_TARGET_BUNDLE_NAME);
2527    auto isAppPriorityExisted = appJson.contains(Profile::APP_TARGET_PRIORITY);
2528    auto isTargetModuleNameExisted = moduleJson.contains(Profile::MODULE_TARGET_MODULE_NAME);
2529    auto isModulePriorityExisted = moduleJson.contains(Profile::MODULE_TARGET_PRIORITY);
2530    if (!isTargetBundleExisted && !isAppPriorityExisted && !isTargetModuleNameExisted &&
2531        !isModulePriorityExisted) {
2532        APP_LOGW_NOFUNC("not overlayed hap");
2533        return overlayMsg;
2534    }
2535    if (!isTargetModuleNameExisted) {
2536        Profile::g_parseResult = ERR_BUNDLEMANAGER_OVERLAY_INSTALLATION_FAILED_TARGET_MODULE_NAME_MISSED;
2537        APP_LOGE("overlay hap with invalid configuration file");
2538        return overlayMsg;
2539    }
2540    if (!isTargetBundleExisted && isAppPriorityExisted) {
2541        Profile::g_parseResult = ERR_BUNDLEMANAGER_OVERLAY_INSTALLATION_FAILED_TARGET_BUNDLE_NAME_MISSED;
2542        APP_LOGE("overlay hap with invalid configuration file");
2543        return overlayMsg;
2544    }
2545
2546    APP_LOGI("overlay configuration file is about to parse");
2547    overlayMsg.type = isTargetBundleExisted ? OVERLAY_EXTERNAL_BUNDLE : OVERLAY_INTERNAL_BUNDLE;
2548    overlayMsg.isAppPriorityExisted = isAppPriorityExisted;
2549    overlayMsg.isModulePriorityExisted = isModulePriorityExisted;
2550    return overlayMsg;
2551#else
2552    return overlayMsg;
2553#endif
2554}
2555
2556ErrCode ModuleProfile::TransformTo(
2557    const std::ostringstream &source,
2558    const BundleExtractor &bundleExtractor,
2559    InnerBundleInfo &innerBundleInfo) const
2560{
2561    APP_LOGD("transform module.json stream to InnerBundleInfo");
2562    nlohmann::json jsonObject = nlohmann::json::parse(source.str(), nullptr, false);
2563    if (jsonObject.is_discarded()) {
2564        APP_LOGE("bad profile");
2565        return ERR_APPEXECFWK_PARSE_BAD_PROFILE;
2566    }
2567    OverlayMsg overlayMsg;
2568    Profile::ModuleJson moduleJson;
2569    {
2570        std::lock_guard<std::mutex> lock(Profile::g_mutex);
2571        Profile::g_parseResult = ERR_OK;
2572        overlayMsg = ObtainOverlayType(jsonObject);
2573        if ((overlayMsg.type == NON_OVERLAY_TYPE) && (Profile::g_parseResult != ERR_OK)) {
2574            int32_t ret = Profile::g_parseResult;
2575            Profile::g_parseResult = ERR_OK;
2576            APP_LOGE("ObtainOverlayType g_parseResult %{public}d", ret);
2577            return ret;
2578        }
2579        APP_LOGD("overlay type of the hap is %{public}d", overlayMsg.type);
2580        Profile::g_parseResult = ERR_OK;
2581        moduleJson = jsonObject.get<Profile::ModuleJson>();
2582        if (Profile::g_parseResult != ERR_OK) {
2583            APP_LOGE("g_parseResult %{public}d", Profile::g_parseResult);
2584            int32_t ret = Profile::g_parseResult;
2585            // need recover parse result to ERR_OK
2586            Profile::g_parseResult = ERR_OK;
2587            return ret;
2588        }
2589    }
2590    if (!ToInnerBundleInfo(
2591        moduleJson, bundleExtractor, overlayMsg, innerBundleInfo)) {
2592        return ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR;
2593    }
2594    if (!ParserAtomicConfig(jsonObject, innerBundleInfo)) {
2595        APP_LOGE("Parser atomicService config failed");
2596        return ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR;
2597    }
2598    if (!ParserNativeSo(moduleJson, bundleExtractor, innerBundleInfo)) {
2599#ifdef X86_EMULATOR_MODE
2600        APP_LOGE("Parser native so failed");
2601        return ERR_APPEXECFWK_PARSE_NATIVE_SO_FAILED;
2602#endif
2603        APP_LOGW("Parser native so failed");
2604    }
2605    if (!ParserArkNativeFilePath(moduleJson, bundleExtractor, innerBundleInfo)) {
2606#ifdef X86_EMULATOR_MODE
2607        APP_LOGE("Parser ark native file failed");
2608        return ERR_APPEXECFWK_PARSE_AN_FAILED;
2609#endif
2610        APP_LOGW("Parser ark native file failed");
2611    }
2612    return ERR_OK;
2613}
2614}  // namespace AppExecFwk
2615}  // namespace OHOS
2616