1 /*
2  * Copyright (c) 2023-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 <algorithm>
19 #include <mutex>
20 #include <set>
21 #include <sstream>
22 #include "bundle_constants.h"
23 #include "common_profile.h"
24 #include "hilog_tag_wrapper.h"
25 #include "string_ex.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 const std::unordered_map<std::string, ExtensionAbilityType> EXTENSION_TYPE_MAP = {
30     { "form", ExtensionAbilityType::FORM },
31     { "workScheduler", ExtensionAbilityType::WORK_SCHEDULER },
32     { "inputMethod", ExtensionAbilityType::INPUTMETHOD },
33     { "service", ExtensionAbilityType::SERVICE },
34     { "accessibility", ExtensionAbilityType::ACCESSIBILITY },
35     { "dataShare", ExtensionAbilityType::DATASHARE },
36     { "fileShare", ExtensionAbilityType::FILESHARE },
37     { "staticSubscriber", ExtensionAbilityType::STATICSUBSCRIBER },
38     { "fence", ExtensionAbilityType::FENCE },
39     { "wallpaper", ExtensionAbilityType::WALLPAPER },
40     { "backup", ExtensionAbilityType::BACKUP },
41     { "window", ExtensionAbilityType::WINDOW },
42     { "enterpriseAdmin", ExtensionAbilityType::ENTERPRISE_ADMIN },
43     { "fileAccess", ExtensionAbilityType::FILEACCESS_EXTENSION },
44     { "thumbnail", ExtensionAbilityType::THUMBNAIL },
45     { "preview", ExtensionAbilityType::PREVIEW_TYPE },
46     { "print", ExtensionAbilityType::PRINT },
47     { "push", ExtensionAbilityType::PUSH },
48     { "driver", ExtensionAbilityType::DRIVER },
49     { "appAccountAuthorization", ExtensionAbilityType::APP_ACCOUNT_AUTHORIZATION },
50     { "ui", ExtensionAbilityType::UI },
51     { "sysDialog/userAuth", ExtensionAbilityType::SYSDIALOG_USERAUTH },
52     { "sysDialog/common", ExtensionAbilityType::SYSDIALOG_COMMON },
53     { "sysPicker/mediaControl", ExtensionAbilityType::SYSPICKER_MEDIACONTROL },
54     { "sysDialog/atomicServicePanel", ExtensionAbilityType::SYSDIALOG_ATOMICSERVICEPANEL },
55     { "sysPicker/share", ExtensionAbilityType::SYSPICKER_SHARE },
56     { "hms/account", ExtensionAbilityType::HMS_ACCOUNT }
57 };
58 
ConvertToExtensionAbilityType(const std::string & type)59 ExtensionAbilityType ConvertToExtensionAbilityType(const std::string &type)
60 {
61     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
62     if (EXTENSION_TYPE_MAP.find(type) != EXTENSION_TYPE_MAP.end()) {
63         return EXTENSION_TYPE_MAP.at(type);
64     }
65 
66     return ExtensionAbilityType::UNSPECIFIED;
67 }
68 
ConvertToExtensionTypeName(ExtensionAbilityType type)69 std::string ConvertToExtensionTypeName(ExtensionAbilityType type)
70 {
71     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
72     for (const auto &[key, val] : EXTENSION_TYPE_MAP) {
73         if (val == type) {
74             return key;
75         }
76     }
77 
78     return "Unspecified";
79 }
80 
81 namespace Profile {
82 int32_t g_parseResult = ERR_OK;
83 std::mutex g_mutex;
84 
85 const std::set<std::string> MODULE_TYPE_SET = {
86     "entry",
87     "feature",
88     "shared"
89 };
90 
91 const std::set<std::string> VIRTUAL_MACHINE_SET = {
92     "ark",
93     "default"
94 };
95 
96 const std::map<std::string, uint32_t> BACKGROUND_MODES_MAP = {
97     {ProfileReader::KEY_DATA_TRANSFER, ProfileReader::VALUE_DATA_TRANSFER},
98     {ProfileReader::KEY_AUDIO_PLAYBACK, ProfileReader::VALUE_AUDIO_PLAYBACK},
99     {ProfileReader::KEY_AUDIO_RECORDING, ProfileReader::VALUE_AUDIO_RECORDING},
100     {ProfileReader::KEY_LOCATION, ProfileReader::VALUE_LOCATION},
101     {ProfileReader::KEY_BLUETOOTH_INTERACTION, ProfileReader::VALUE_BLUETOOTH_INTERACTION},
102     {ProfileReader::KEY_MULTI_DEVICE_CONNECTION, ProfileReader::VALUE_MULTI_DEVICE_CONNECTION},
103     {ProfileReader::KEY_WIFI_INTERACTION, ProfileReader::VALUE_WIFI_INTERACTION},
104     {ProfileReader::KEY_VOIP, ProfileReader::VALUE_VOIP},
105     {ProfileReader::KEY_TASK_KEEPING, ProfileReader::VALUE_TASK_KEEPING},
106     {ProfileReader::KEY_PICTURE_IN_PICTURE, ProfileReader::VALUE_PICTURE_IN_PICTURE},
107     {ProfileReader::KEY_SCREEN_FETCH, ProfileReader::VALUE_SCREEN_FETCH}
108 };
109 
110 const std::set<std::string> GRANT_MODE_SET = {
111     "system_grant",
112     "user_grant"
113 };
114 
115 const std::set<std::string> AVAILABLE_LEVEL_SET = {
116     "system_core",
117     "system_basic",
118     "normal"
119 };
120 
121 const std::map<std::string, LaunchMode> LAUNCH_MODE_MAP = {
122     {"singleton", LaunchMode::SINGLETON},
123     {"standard", LaunchMode::STANDARD},
124     {"multiton", LaunchMode::STANDARD},
125     {"specified", LaunchMode::SPECIFIED}
126 };
127 const std::unordered_map<std::string, DisplayOrientation> DISPLAY_ORIENTATION_MAP = {
128     {"unspecified", DisplayOrientation::UNSPECIFIED},
129     {"landscape", DisplayOrientation::LANDSCAPE},
130     {"portrait", DisplayOrientation::PORTRAIT},
131     {"landscape_inverted", DisplayOrientation::LANDSCAPE_INVERTED},
132     {"portrait_inverted", DisplayOrientation::PORTRAIT_INVERTED},
133     {"auto_rotation", DisplayOrientation::AUTO_ROTATION},
134     {"auto_rotation_landscape", DisplayOrientation::AUTO_ROTATION_LANDSCAPE},
135     {"auto_rotation_portrait", DisplayOrientation::AUTO_ROTATION_PORTRAIT},
136     {"auto_rotation_restricted", DisplayOrientation::AUTO_ROTATION_RESTRICTED},
137     {"auto_rotation_landscape_restricted", DisplayOrientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED},
138     {"auto_rotation_portrait_restricted", DisplayOrientation::AUTO_ROTATION_PORTRAIT_RESTRICTED},
139     {"locked", DisplayOrientation::LOCKED}
140 };
141 const std::unordered_map<std::string, SupportWindowMode> WINDOW_MODE_MAP = {
142     {"fullscreen", SupportWindowMode::FULLSCREEN},
143     {"split", SupportWindowMode::SPLIT},
144     {"floating", SupportWindowMode::FLOATING}
145 };
146 const std::unordered_map<std::string, BundleType> BUNDLE_TYPE_MAP = {
147     {"app", BundleType::APP},
148     {"atomicService", BundleType::ATOMIC_SERVICE},
149     {"shared", BundleType::SHARED}
150 };
151 
152 struct DeviceConfig {
153     // pair first : if exist in module.json then true, otherwise false
154     // pair second : actual value
155     std::pair<bool, int32_t> minAPIVersion = std::make_pair<>(false, 0);
156     std::pair<bool, bool> keepAlive = std::make_pair<>(false, false);
157     std::pair<bool, bool> removable = std::make_pair<>(false, true);
158     std::pair<bool, bool> singleton = std::make_pair<>(false, false);
159     std::pair<bool, bool> userDataClearable = std::make_pair<>(false, true);
160     std::pair<bool, bool> accessible = std::make_pair<>(false, true);
161 };
162 
163 struct Metadata {
164     std::string name;
165     std::string value;
166     std::string resource;
167 };
168 
169 struct Ability {
170     std::string name;
171     std::string srcEntrance;
172     std::string launchType = ABILITY_LAUNCH_TYPE_DEFAULT_VALUE;
173     std::string description;
174     int32_t descriptionId = 0;
175     std::string icon;
176     int32_t iconId = 0;
177     std::string label;
178     int32_t labelId = 0;
179     int32_t priority = 0;
180     std::vector<std::string> permissions;
181     std::vector<Metadata> metadata;
182     bool visible = false;
183     bool continuable = false;
184     std::vector<std::string> backgroundModes;
185     std::string startWindowIcon;
186     int32_t startWindowIconId = 0;
187     std::string startWindowBackground;
188     int32_t startWindowBackgroundId = 0;
189     bool removeMissionAfterTerminate = false;
190     std::string orientation = "unspecified";
191     std::vector<std::string> windowModes;
192     double maxWindowRatio = 0;
193     double minWindowRatio = 0;
194     uint32_t maxWindowWidth = 0;
195     uint32_t minWindowWidth = 0;
196     uint32_t maxWindowHeight = 0;
197     uint32_t minWindowHeight = 0;
198     bool excludeFromMissions = false;
199     bool recoverable = false;
200     bool unclearableMission = false;
201 };
202 
203 struct Extension {
204     std::string name;
205     std::string srcEntrance;
206     std::string icon;
207     int32_t iconId = 0;
208     std::string label;
209     int32_t labelId = 0;
210     std::string description;
211     int32_t descriptionId = 0;
212     int32_t priority = 0;
213     std::string type;
214     std::string readPermission;
215     std::string writePermission;
216     std::string uri;
217     std::vector<std::string> permissions;
218     bool visible = false;
219     std::vector<Metadata> metadata;
220 };
221 
222 struct App {
223     std::string bundleName;
224     bool debug = false;
225     std::string icon;
226     int32_t iconId = 0;
227     std::string label;
228     int32_t labelId = 0;
229     std::string description;
230     int32_t descriptionId = 0;
231     std::string vendor;
232     int32_t versionCode = 0;
233     std::string versionName;
234     int32_t minCompatibleVersionCode = -1;
235     uint32_t minAPIVersion = 0;
236     int32_t targetAPIVersion = 0;
237     std::string apiReleaseType = APP_API_RELEASETYPE_DEFAULT_VALUE;
238     bool keepAlive = false;
239     std::pair<bool, bool> removable = std::make_pair<>(false, true);
240     bool singleton = false;
241     bool userDataClearable = true;
242     bool accessible = false;
243     std::vector<std::string> targetBundleList;
244     std::map<std::string, DeviceConfig> deviceConfigs;
245     bool multiProjects = false;
246     std::string targetBundle;
247     int32_t targetPriority = 0;
248     bool asanEnabled = false;
249     std::string bundleType = Profile::BUNDLE_TYPE_APP;
250     std::string compileSdkVersion;
251     std::string compileSdkType = Profile::COMPILE_SDK_TYPE_OPEN_HARMONY;
252 };
253 
254 struct Module {
255     std::string name;
256     std::string type;
257     std::string srcEntrance;
258     std::string description;
259     int32_t descriptionId = 0;
260     std::string process;
261     std::string mainElement;
262     std::vector<std::string> deviceTypes;
263     bool deliveryWithInstall = false;
264     bool installationFree = false;
265     std::string virtualMachine = MODULE_VIRTUAL_MACHINE_DEFAULT_VALUE;
266     std::string pages;
267     std::vector<Metadata> metadata;
268     std::vector<Ability> abilities;
269     std::vector<Extension> extensionAbilities;
270     std::vector<Dependency> dependencies;
271     std::string compileMode;
272     bool isLibIsolated = false;
273     std::string targetModule;
274     int32_t targetPriority = 0;
275     std::vector<ProxyData> proxyDatas;
276     std::vector<ProxyData> proxyData;
277     std::string buildHash;
278     std::string isolationMode;
279     bool compressNativeLibs = true;
280 };
281 
282 struct ModuleJson {
283     App app;
284     Module module;
285 };
286 
from_json(const nlohmann::json & jsonObject,Metadata & metadata)287 void from_json(const nlohmann::json &jsonObject, Metadata &metadata)
288 {
289     TAG_LOGD(AAFwkTag::ABILITY_SIM, "read metadata tag from module.json");
290     const auto &jsonObjectEnd = jsonObject.end();
291     GetValueIfFindKey<std::string>(jsonObject,
292         jsonObjectEnd,
293         META_DATA_NAME,
294         metadata.name,
295         JsonType::STRING,
296         false,
297         g_parseResult,
298         ArrayType::NOT_ARRAY);
299     GetValueIfFindKey<std::string>(jsonObject,
300         jsonObjectEnd,
301         META_DATA_VALUE,
302         metadata.value,
303         JsonType::STRING,
304         false,
305         g_parseResult,
306         ArrayType::NOT_ARRAY);
307     GetValueIfFindKey<std::string>(jsonObject,
308         jsonObjectEnd,
309         META_DATA_RESOURCE,
310         metadata.resource,
311         JsonType::STRING,
312         false,
313         g_parseResult,
314         ArrayType::NOT_ARRAY);
315 }
316 
from_json(const nlohmann::json & jsonObject,Ability & ability)317 void from_json(const nlohmann::json &jsonObject, Ability &ability)
318 {
319     TAG_LOGD(AAFwkTag::ABILITY_SIM, "read ability tag from module.json");
320     const auto &jsonObjectEnd = jsonObject.end();
321     GetValueIfFindKey<std::string>(jsonObject,
322         jsonObjectEnd,
323         ABILITY_NAME,
324         ability.name,
325         JsonType::STRING,
326         true,
327         g_parseResult,
328         ArrayType::NOT_ARRAY);
329     // both srcEntry and srcEntrance can be configured, but srcEntry has higher priority
330     if (jsonObject.find(SRC_ENTRY) != jsonObject.end()) {
331         GetValueIfFindKey<std::string>(jsonObject,
332             jsonObjectEnd,
333             SRC_ENTRY,
334             ability.srcEntrance,
335             JsonType::STRING,
336             true,
337             g_parseResult,
338             ArrayType::NOT_ARRAY);
339     } else {
340         GetValueIfFindKey<std::string>(jsonObject,
341             jsonObjectEnd,
342             SRC_ENTRANCE,
343             ability.srcEntrance,
344             JsonType::STRING,
345             true,
346             g_parseResult,
347             ArrayType::NOT_ARRAY);
348     }
349     GetValueIfFindKey<std::string>(jsonObject,
350         jsonObjectEnd,
351         ABILITY_LAUNCH_TYPE,
352         ability.launchType,
353         JsonType::STRING,
354         false,
355         g_parseResult,
356         ArrayType::NOT_ARRAY);
357     GetValueIfFindKey<std::string>(jsonObject,
358         jsonObjectEnd,
359         DESCRIPTION,
360         ability.description,
361         JsonType::STRING,
362         false,
363         g_parseResult,
364         ArrayType::NOT_ARRAY);
365     GetValueIfFindKey<int32_t>(jsonObject,
366         jsonObjectEnd,
367         DESCRIPTION_ID,
368         ability.descriptionId,
369         JsonType::NUMBER,
370         false,
371         g_parseResult,
372         ArrayType::NOT_ARRAY);
373     GetValueIfFindKey<std::string>(jsonObject,
374         jsonObjectEnd,
375         ICON,
376         ability.icon,
377         JsonType::STRING,
378         false,
379         g_parseResult,
380         ArrayType::NOT_ARRAY);
381     GetValueIfFindKey<int32_t>(jsonObject,
382         jsonObjectEnd,
383         ICON_ID,
384         ability.iconId,
385         JsonType::NUMBER,
386         false,
387         g_parseResult,
388         ArrayType::NOT_ARRAY);
389     GetValueIfFindKey<std::string>(jsonObject,
390         jsonObjectEnd,
391         LABEL,
392         ability.label,
393         JsonType::STRING,
394         false,
395         g_parseResult,
396         ArrayType::NOT_ARRAY);
397     GetValueIfFindKey<int32_t>(jsonObject,
398         jsonObjectEnd,
399         LABEL_ID,
400         ability.labelId,
401         JsonType::NUMBER,
402         false,
403         g_parseResult,
404         ArrayType::NOT_ARRAY);
405     GetValueIfFindKey<int32_t>(jsonObject,
406         jsonObjectEnd,
407         PRIORITY,
408         ability.priority,
409         JsonType::NUMBER,
410         false,
411         g_parseResult,
412         ArrayType::NOT_ARRAY);
413     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
414         jsonObjectEnd,
415         PERMISSIONS,
416         ability.permissions,
417         JsonType::ARRAY,
418         false,
419         g_parseResult,
420         ArrayType::STRING);
421     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
422         jsonObjectEnd,
423         META_DATA,
424         ability.metadata,
425         JsonType::ARRAY,
426         false,
427         g_parseResult,
428         ArrayType::OBJECT);
429     // both exported and visible can be configured, but exported has higher priority
430     GetValueIfFindKey<bool>(jsonObject,
431         jsonObjectEnd,
432         VISIBLE,
433         ability.visible,
434         JsonType::BOOLEAN,
435         false,
436         g_parseResult,
437         ArrayType::NOT_ARRAY);
438     GetValueIfFindKey<bool>(jsonObject,
439         jsonObjectEnd,
440         EXPORTED,
441         ability.visible,
442         JsonType::BOOLEAN,
443         false,
444         g_parseResult,
445         ArrayType::NOT_ARRAY);
446     GetValueIfFindKey<bool>(jsonObject,
447         jsonObjectEnd,
448         ABILITY_CONTINUABLE,
449         ability.continuable,
450         JsonType::BOOLEAN,
451         false,
452         g_parseResult,
453         ArrayType::NOT_ARRAY);
454     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
455         jsonObjectEnd,
456         ABILITY_BACKGROUNDMODES,
457         ability.backgroundModes,
458         JsonType::ARRAY,
459         false,
460         g_parseResult,
461         ArrayType::STRING);
462     GetValueIfFindKey<std::string>(jsonObject,
463         jsonObjectEnd,
464         ABILITY_START_WINDOW_ICON,
465         ability.startWindowIcon,
466         JsonType::STRING,
467         false,
468         g_parseResult,
469         ArrayType::NOT_ARRAY);
470     GetValueIfFindKey<int32_t>(jsonObject,
471         jsonObjectEnd,
472         ABILITY_START_WINDOW_ICON_ID,
473         ability.startWindowIconId,
474         JsonType::NUMBER,
475         false,
476         g_parseResult,
477         ArrayType::NOT_ARRAY);
478     GetValueIfFindKey<std::string>(jsonObject,
479         jsonObjectEnd,
480         ABILITY_START_WINDOW_BACKGROUND,
481         ability.startWindowBackground,
482         JsonType::STRING,
483         false,
484         g_parseResult,
485         ArrayType::NOT_ARRAY);
486     GetValueIfFindKey<int32_t>(jsonObject,
487         jsonObjectEnd,
488         ABILITY_START_WINDOW_BACKGROUND_ID,
489         ability.startWindowBackgroundId,
490         JsonType::NUMBER,
491         false,
492         g_parseResult,
493         ArrayType::NOT_ARRAY);
494     GetValueIfFindKey<bool>(jsonObject,
495         jsonObjectEnd,
496         ABILITY_REMOVE_MISSION_AFTER_TERMINATE,
497         ability.removeMissionAfterTerminate,
498         JsonType::BOOLEAN,
499         false,
500         g_parseResult,
501         ArrayType::NOT_ARRAY);
502     GetValueIfFindKey<std::string>(jsonObject,
503         jsonObjectEnd,
504         ABILITY_ORIENTATION,
505         ability.orientation,
506         JsonType::STRING,
507         false,
508         g_parseResult,
509         ArrayType::NOT_ARRAY);
510     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
511         jsonObjectEnd,
512         ABILITY_SUPPORT_WINDOW_MODE,
513         ability.windowModes,
514         JsonType::ARRAY,
515         false,
516         g_parseResult,
517         ArrayType::STRING);
518     GetValueIfFindKey<double>(jsonObject,
519         jsonObjectEnd,
520         ABILITY_MAX_WINDOW_RATIO,
521         ability.maxWindowRatio,
522         JsonType::NUMBER,
523         false,
524         g_parseResult,
525         ArrayType::NOT_ARRAY);
526     GetValueIfFindKey<double>(jsonObject,
527         jsonObjectEnd,
528         ABILITY_MIN_WINDOW_RATIO,
529         ability.minWindowRatio,
530         JsonType::NUMBER,
531         false,
532         g_parseResult,
533         ArrayType::NOT_ARRAY);
534     GetValueIfFindKey<uint32_t>(jsonObject,
535         jsonObjectEnd,
536         ABILITY_MAX_WINDOW_WIDTH,
537         ability.maxWindowWidth,
538         JsonType::NUMBER,
539         false,
540         g_parseResult,
541         ArrayType::NOT_ARRAY);
542     GetValueIfFindKey<uint32_t>(jsonObject,
543         jsonObjectEnd,
544         ABILITY_MIN_WINDOW_WIDTH,
545         ability.minWindowWidth,
546         JsonType::NUMBER,
547         false,
548         g_parseResult,
549         ArrayType::NOT_ARRAY);
550     GetValueIfFindKey<uint32_t>(jsonObject,
551         jsonObjectEnd,
552         ABILITY_MAX_WINDOW_HEIGHT,
553         ability.maxWindowHeight,
554         JsonType::NUMBER,
555         false,
556         g_parseResult,
557         ArrayType::NOT_ARRAY);
558     GetValueIfFindKey<uint32_t>(jsonObject,
559         jsonObjectEnd,
560         ABILITY_MIN_WINDOW_HEIGHT,
561         ability.minWindowHeight,
562         JsonType::NUMBER,
563         false,
564         g_parseResult,
565         ArrayType::NOT_ARRAY);
566     GetValueIfFindKey<bool>(jsonObject,
567         jsonObjectEnd,
568         ABILITY_EXCLUDE_FROM_MISSIONS,
569         ability.excludeFromMissions,
570         JsonType::BOOLEAN,
571         false,
572         g_parseResult,
573         ArrayType::NOT_ARRAY);
574     GetValueIfFindKey<bool>(jsonObject,
575         jsonObjectEnd,
576         ABILITY_RECOVERABLE,
577         ability.recoverable,
578         JsonType::BOOLEAN,
579         false,
580         g_parseResult,
581         ArrayType::NOT_ARRAY);
582     GetValueIfFindKey<bool>(jsonObject,
583         jsonObjectEnd,
584         ABILITY_UNCLEARABLE_MISSION,
585         ability.unclearableMission,
586         JsonType::BOOLEAN,
587         false,
588         g_parseResult,
589         ArrayType::NOT_ARRAY);
590 }
591 
from_json(const nlohmann::json & jsonObject,Extension & extension)592 void from_json(const nlohmann::json &jsonObject, Extension &extension)
593 {
594     TAG_LOGD(AAFwkTag::ABILITY_SIM, "read extension tag from module.json");
595     const auto &jsonObjectEnd = jsonObject.end();
596     GetValueIfFindKey<std::string>(jsonObject,
597         jsonObjectEnd,
598         EXTENSION_ABILITY_NAME,
599         extension.name,
600         JsonType::STRING,
601         true,
602         g_parseResult,
603         ArrayType::NOT_ARRAY);
604     // both srcEntry and srcEntrance can be configured, but srcEntry has higher priority
605     if (jsonObject.find(SRC_ENTRY) != jsonObject.end()) {
606         GetValueIfFindKey<std::string>(jsonObject,
607             jsonObjectEnd,
608             SRC_ENTRY,
609             extension.srcEntrance,
610             JsonType::STRING,
611             true,
612             g_parseResult,
613             ArrayType::NOT_ARRAY);
614     } else {
615         GetValueIfFindKey<std::string>(jsonObject,
616             jsonObjectEnd,
617             SRC_ENTRANCE,
618             extension.srcEntrance,
619             JsonType::STRING,
620             true,
621             g_parseResult,
622             ArrayType::NOT_ARRAY);
623     }
624     GetValueIfFindKey<std::string>(jsonObject,
625         jsonObjectEnd,
626         ICON,
627         extension.icon,
628         JsonType::STRING,
629         false,
630         g_parseResult,
631         ArrayType::NOT_ARRAY);
632     GetValueIfFindKey<int32_t>(jsonObject,
633         jsonObjectEnd,
634         ICON_ID,
635         extension.iconId,
636         JsonType::NUMBER,
637         false,
638         g_parseResult,
639         ArrayType::NOT_ARRAY);
640     GetValueIfFindKey<std::string>(jsonObject,
641         jsonObjectEnd,
642         LABEL,
643         extension.label,
644         JsonType::STRING,
645         false,
646         g_parseResult,
647         ArrayType::NOT_ARRAY);
648     GetValueIfFindKey<int32_t>(jsonObject,
649         jsonObjectEnd,
650         LABEL_ID,
651         extension.labelId,
652         JsonType::NUMBER,
653         false,
654         g_parseResult,
655         ArrayType::NOT_ARRAY);
656     GetValueIfFindKey<std::string>(jsonObject,
657         jsonObjectEnd,
658         DESCRIPTION,
659         extension.description,
660         JsonType::STRING,
661         false,
662         g_parseResult,
663         ArrayType::NOT_ARRAY);
664     GetValueIfFindKey<int32_t>(jsonObject,
665         jsonObjectEnd,
666         DESCRIPTION_ID,
667         extension.descriptionId,
668         JsonType::NUMBER,
669         false,
670         g_parseResult,
671         ArrayType::NOT_ARRAY);
672     GetValueIfFindKey<int32_t>(jsonObject,
673         jsonObjectEnd,
674         PRIORITY,
675         extension.priority,
676         JsonType::NUMBER,
677         false,
678         g_parseResult,
679         ArrayType::NOT_ARRAY);
680     GetValueIfFindKey<std::string>(jsonObject,
681         jsonObjectEnd,
682         EXTENSION_ABILITY_TYPE,
683         extension.type,
684         JsonType::STRING,
685         true,
686         g_parseResult,
687         ArrayType::NOT_ARRAY);
688     GetValueIfFindKey<std::string>(jsonObject,
689         jsonObjectEnd,
690         EXTENSION_ABILITY_READ_PERMISSION,
691         extension.readPermission,
692         JsonType::STRING,
693         false,
694         g_parseResult,
695         ArrayType::NOT_ARRAY);
696     GetValueIfFindKey<std::string>(jsonObject,
697         jsonObjectEnd,
698         EXTENSION_ABILITY_WRITE_PERMISSION,
699         extension.writePermission,
700         JsonType::STRING,
701         false,
702         g_parseResult,
703         ArrayType::NOT_ARRAY);
704     GetValueIfFindKey<std::string>(jsonObject,
705         jsonObjectEnd,
706         EXTENSION_URI,
707         extension.uri,
708         JsonType::STRING,
709         false,
710         g_parseResult,
711         ArrayType::NOT_ARRAY);
712     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
713         jsonObjectEnd,
714         PERMISSIONS,
715         extension.permissions,
716         JsonType::ARRAY,
717         false,
718         g_parseResult,
719         ArrayType::STRING);
720     // both exported and visible can be configured, but exported has higher priority
721     GetValueIfFindKey<bool>(jsonObject,
722         jsonObjectEnd,
723         VISIBLE,
724         extension.visible,
725         JsonType::BOOLEAN,
726         false,
727         g_parseResult,
728         ArrayType::NOT_ARRAY);
729     GetValueIfFindKey<bool>(jsonObject,
730         jsonObjectEnd,
731         EXPORTED,
732         extension.visible,
733         JsonType::BOOLEAN,
734         false,
735         g_parseResult,
736         ArrayType::NOT_ARRAY);
737     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
738         jsonObjectEnd,
739         META_DATA,
740         extension.metadata,
741         JsonType::ARRAY,
742         false,
743         g_parseResult,
744         ArrayType::OBJECT);
745 }
746 
from_json(const nlohmann::json & jsonObject,DeviceConfig & deviceConfig)747 void from_json(const nlohmann::json &jsonObject, DeviceConfig &deviceConfig)
748 {
749     const auto &jsonObjectEnd = jsonObject.end();
750     if (jsonObject.find(MIN_API_VERSION) != jsonObjectEnd) {
751         deviceConfig.minAPIVersion.first = true;
752         GetValueIfFindKey<int32_t>(jsonObject,
753             jsonObjectEnd,
754             MIN_API_VERSION,
755             deviceConfig.minAPIVersion.second,
756             JsonType::NUMBER,
757             false,
758             g_parseResult,
759             ArrayType::NOT_ARRAY);
760     }
761     if (jsonObject.find(DEVICE_CONFIG_KEEP_ALIVE) != jsonObjectEnd) {
762         deviceConfig.keepAlive.first = true;
763         GetValueIfFindKey<bool>(jsonObject,
764             jsonObjectEnd,
765             DEVICE_CONFIG_KEEP_ALIVE,
766             deviceConfig.keepAlive.second,
767             JsonType::BOOLEAN,
768             false,
769             g_parseResult,
770             ArrayType::NOT_ARRAY);
771     }
772     if (jsonObject.find(DEVICE_CONFIG_REMOVABLE) != jsonObjectEnd) {
773         deviceConfig.removable.first = true;
774         GetValueIfFindKey<bool>(jsonObject,
775             jsonObjectEnd,
776             DEVICE_CONFIG_REMOVABLE,
777             deviceConfig.removable.second,
778             JsonType::BOOLEAN,
779             false,
780             g_parseResult,
781             ArrayType::NOT_ARRAY);
782     }
783     if (jsonObject.find(DEVICE_CONFIG_SINGLETON) != jsonObjectEnd) {
784         deviceConfig.singleton.first = true;
785         GetValueIfFindKey<bool>(jsonObject,
786             jsonObjectEnd,
787             DEVICE_CONFIG_SINGLETON,
788             deviceConfig.singleton.second,
789             JsonType::BOOLEAN,
790             false,
791             g_parseResult,
792             ArrayType::NOT_ARRAY);
793     }
794     if (jsonObject.find(DEVICE_CONFIG_USER_DATA_CLEARABLE) != jsonObjectEnd) {
795         deviceConfig.userDataClearable.first = true;
796         GetValueIfFindKey<bool>(jsonObject,
797             jsonObjectEnd,
798             DEVICE_CONFIG_USER_DATA_CLEARABLE,
799             deviceConfig.userDataClearable.second,
800             JsonType::BOOLEAN,
801             false,
802             g_parseResult,
803             ArrayType::NOT_ARRAY);
804     }
805     if (jsonObject.find(DEVICE_CONFIG_ACCESSIBLE) != jsonObjectEnd) {
806         deviceConfig.accessible.first = true;
807         GetValueIfFindKey<bool>(jsonObject,
808             jsonObjectEnd,
809             DEVICE_CONFIG_ACCESSIBLE,
810             deviceConfig.accessible.second,
811             JsonType::BOOLEAN,
812             false,
813             g_parseResult,
814             ArrayType::NOT_ARRAY);
815     }
816 }
817 
from_json(const nlohmann::json & jsonObject,App & app)818 void from_json(const nlohmann::json &jsonObject, App &app)
819 {
820     TAG_LOGD(AAFwkTag::ABILITY_SIM, "read app tag from module.json");
821     const auto &jsonObjectEnd = jsonObject.end();
822     GetValueIfFindKey<std::string>(jsonObject,
823         jsonObjectEnd,
824         APP_BUNDLE_NAME,
825         app.bundleName,
826         JsonType::STRING,
827         true,
828         g_parseResult,
829         ArrayType::NOT_ARRAY);
830     GetValueIfFindKey<std::string>(jsonObject,
831         jsonObjectEnd,
832         ICON,
833         app.icon,
834         JsonType::STRING,
835         true,
836         g_parseResult,
837         ArrayType::NOT_ARRAY);
838     GetValueIfFindKey<std::string>(jsonObject,
839         jsonObjectEnd,
840         LABEL,
841         app.label,
842         JsonType::STRING,
843         true,
844         g_parseResult,
845         ArrayType::NOT_ARRAY);
846     GetValueIfFindKey<int32_t>(jsonObject,
847         jsonObjectEnd,
848         APP_VERSION_CODE,
849         app.versionCode,
850         JsonType::NUMBER,
851         true,
852         g_parseResult,
853         ArrayType::NOT_ARRAY);
854     GetValueIfFindKey<std::string>(jsonObject,
855         jsonObjectEnd,
856         APP_VERSION_NAME,
857         app.versionName,
858         JsonType::STRING,
859         true,
860         g_parseResult,
861         ArrayType::NOT_ARRAY);
862     GetValueIfFindKey<uint32_t>(jsonObject,
863         jsonObjectEnd,
864         APP_MIN_API_VERSION,
865         app.minAPIVersion,
866         JsonType::NUMBER,
867         true,
868         g_parseResult,
869         ArrayType::NOT_ARRAY);
870     GetValueIfFindKey<int32_t>(jsonObject,
871         jsonObjectEnd,
872         APP_TARGET_API_VERSION,
873         app.targetAPIVersion,
874         JsonType::NUMBER,
875         true,
876         g_parseResult,
877         ArrayType::NOT_ARRAY);
878     GetValueIfFindKey<bool>(jsonObject,
879         jsonObjectEnd,
880         APP_DEBUG,
881         app.debug,
882         JsonType::BOOLEAN,
883         false,
884         g_parseResult,
885         ArrayType::NOT_ARRAY);
886     GetValueIfFindKey<int32_t>(jsonObject,
887         jsonObjectEnd,
888         ICON_ID,
889         app.iconId,
890         JsonType::NUMBER,
891         false,
892         g_parseResult,
893         ArrayType::NOT_ARRAY);
894     GetValueIfFindKey<int32_t>(jsonObject,
895         jsonObjectEnd,
896         LABEL_ID,
897         app.labelId,
898         JsonType::NUMBER,
899         false,
900         g_parseResult,
901         ArrayType::NOT_ARRAY);
902     GetValueIfFindKey<std::string>(jsonObject,
903         jsonObjectEnd,
904         DESCRIPTION,
905         app.description,
906         JsonType::STRING,
907         false,
908         g_parseResult,
909         ArrayType::NOT_ARRAY);
910     GetValueIfFindKey<int32_t>(jsonObject,
911         jsonObjectEnd,
912         DESCRIPTION_ID,
913         app.descriptionId,
914         JsonType::NUMBER,
915         false,
916         g_parseResult,
917         ArrayType::NOT_ARRAY);
918     GetValueIfFindKey<std::string>(jsonObject,
919         jsonObjectEnd,
920         APP_VENDOR,
921         app.vendor,
922         JsonType::STRING,
923         false,
924         g_parseResult,
925         ArrayType::NOT_ARRAY);
926     GetValueIfFindKey<int32_t>(jsonObject,
927         jsonObjectEnd,
928         APP_MIN_COMPATIBLE_VERSION_CODE,
929         app.minCompatibleVersionCode,
930         JsonType::NUMBER,
931         false,
932         g_parseResult,
933         ArrayType::NOT_ARRAY);
934     GetValueIfFindKey<std::string>(jsonObject,
935         jsonObjectEnd,
936         APP_API_RELEASETYPE,
937         app.apiReleaseType,
938         JsonType::STRING,
939         false,
940         g_parseResult,
941         ArrayType::NOT_ARRAY);
942     GetValueIfFindKey<bool>(jsonObject,
943         jsonObjectEnd,
944         APP_KEEP_ALIVE,
945         app.keepAlive,
946         JsonType::BOOLEAN,
947         false,
948         g_parseResult,
949         ArrayType::NOT_ARRAY);
950     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
951         jsonObjectEnd,
952         APP_TARGETBUNDLELIST,
953         app.targetBundleList,
954         JsonType::ARRAY,
955         false,
956         g_parseResult,
957         ArrayType::STRING);
958     if (jsonObject.find(APP_REMOVABLE) != jsonObject.end()) {
959         app.removable.first = true;
960         GetValueIfFindKey<bool>(jsonObject,
961             jsonObjectEnd,
962             APP_REMOVABLE,
963             app.removable.second,
964             JsonType::BOOLEAN,
965             false,
966             g_parseResult,
967             ArrayType::NOT_ARRAY);
968     }
969     GetValueIfFindKey<bool>(jsonObject,
970         jsonObjectEnd,
971         APP_SINGLETON,
972         app.singleton,
973         JsonType::BOOLEAN,
974         false,
975         g_parseResult,
976         ArrayType::NOT_ARRAY);
977     GetValueIfFindKey<bool>(jsonObject,
978         jsonObjectEnd,
979         APP_USER_DATA_CLEARABLE,
980         app.userDataClearable,
981         JsonType::BOOLEAN,
982         false,
983         g_parseResult,
984         ArrayType::NOT_ARRAY);
985     GetValueIfFindKey<bool>(jsonObject,
986         jsonObjectEnd,
987         APP_ACCESSIBLE,
988         app.accessible,
989         JsonType::BOOLEAN,
990         false,
991         g_parseResult,
992         ArrayType::NOT_ARRAY);
993     GetValueIfFindKey<bool>(jsonObject,
994         jsonObjectEnd,
995         APP_ASAN_ENABLED,
996         app.asanEnabled,
997         JsonType::BOOLEAN,
998         false,
999         g_parseResult,
1000         ArrayType::NOT_ARRAY);
1001     GetValueIfFindKey<std::string>(jsonObject,
1002         jsonObjectEnd,
1003         BUNDLE_TYPE,
1004         app.bundleType,
1005         JsonType::STRING,
1006         false,
1007         g_parseResult,
1008         ArrayType::NOT_ARRAY);
1009     if (jsonObject.find(APP_PHONE) != jsonObjectEnd) {
1010         DeviceConfig deviceConfig;
1011         GetValueIfFindKey<DeviceConfig>(jsonObject,
1012             jsonObjectEnd,
1013             APP_PHONE,
1014             deviceConfig,
1015             JsonType::OBJECT,
1016             false,
1017             g_parseResult,
1018             ArrayType::NOT_ARRAY);
1019         app.deviceConfigs[APP_PHONE] = deviceConfig;
1020     }
1021     if (jsonObject.find(APP_TABLET) != jsonObjectEnd) {
1022         DeviceConfig deviceConfig;
1023         GetValueIfFindKey<DeviceConfig>(jsonObject,
1024             jsonObjectEnd,
1025             APP_TABLET,
1026             deviceConfig,
1027             JsonType::OBJECT,
1028             false,
1029             g_parseResult,
1030             ArrayType::NOT_ARRAY);
1031         app.deviceConfigs[APP_TABLET] = deviceConfig;
1032     }
1033     if (jsonObject.find(APP_TV) != jsonObjectEnd) {
1034         DeviceConfig deviceConfig;
1035         GetValueIfFindKey<DeviceConfig>(jsonObject,
1036             jsonObjectEnd,
1037             APP_TV,
1038             deviceConfig,
1039             JsonType::OBJECT,
1040             false,
1041             g_parseResult,
1042             ArrayType::NOT_ARRAY);
1043         app.deviceConfigs[APP_TV] = deviceConfig;
1044     }
1045     if (jsonObject.find(APP_WEARABLE) != jsonObjectEnd) {
1046         DeviceConfig deviceConfig;
1047         GetValueIfFindKey<DeviceConfig>(jsonObject,
1048             jsonObjectEnd,
1049             APP_WEARABLE,
1050             deviceConfig,
1051             JsonType::OBJECT,
1052             false,
1053             g_parseResult,
1054             ArrayType::NOT_ARRAY);
1055         app.deviceConfigs[APP_WEARABLE] = deviceConfig;
1056     }
1057     if (jsonObject.find(APP_LITE_WEARABLE) != jsonObjectEnd) {
1058         DeviceConfig deviceConfig;
1059         GetValueIfFindKey<DeviceConfig>(jsonObject,
1060             jsonObjectEnd,
1061             APP_LITE_WEARABLE,
1062             deviceConfig,
1063             JsonType::OBJECT,
1064             false,
1065             g_parseResult,
1066             ArrayType::NOT_ARRAY);
1067         app.deviceConfigs[APP_LITE_WEARABLE] = deviceConfig;
1068     }
1069     if (jsonObject.find(APP_CAR) != jsonObjectEnd) {
1070         DeviceConfig deviceConfig;
1071         GetValueIfFindKey<DeviceConfig>(jsonObject,
1072             jsonObjectEnd,
1073             APP_CAR,
1074             deviceConfig,
1075             JsonType::OBJECT,
1076             false,
1077             g_parseResult,
1078             ArrayType::NOT_ARRAY);
1079         app.deviceConfigs[APP_CAR] = deviceConfig;
1080     }
1081     if (jsonObject.find(APP_SMART_VISION) != jsonObjectEnd) {
1082         DeviceConfig deviceConfig;
1083         GetValueIfFindKey<DeviceConfig>(jsonObject,
1084             jsonObjectEnd,
1085             APP_SMART_VISION,
1086             deviceConfig,
1087             JsonType::OBJECT,
1088             false,
1089             g_parseResult,
1090             ArrayType::NOT_ARRAY);
1091         app.deviceConfigs[APP_SMART_VISION] = deviceConfig;
1092     }
1093     if (jsonObject.find(APP_ROUTER) != jsonObjectEnd) {
1094         DeviceConfig deviceConfig;
1095         GetValueIfFindKey<DeviceConfig>(jsonObject,
1096             jsonObjectEnd,
1097             APP_ROUTER,
1098             deviceConfig,
1099             JsonType::OBJECT,
1100             false,
1101             g_parseResult,
1102             ArrayType::NOT_ARRAY);
1103         app.deviceConfigs[APP_ROUTER] = deviceConfig;
1104     }
1105     GetValueIfFindKey<bool>(jsonObject,
1106         jsonObjectEnd,
1107         APP_MULTI_PROJECTS,
1108         app.multiProjects,
1109         JsonType::BOOLEAN,
1110         false,
1111         g_parseResult,
1112         ArrayType::NOT_ARRAY);
1113     GetValueIfFindKey<std::string>(jsonObject,
1114         jsonObjectEnd,
1115         APP_TARGET_BUNDLE_NAME,
1116         app.targetBundle,
1117         JsonType::STRING,
1118         false,
1119         g_parseResult,
1120         ArrayType::NOT_ARRAY);
1121     GetValueIfFindKey<int32_t>(jsonObject,
1122         jsonObjectEnd,
1123         APP_TARGET_PRIORITY,
1124         app.targetPriority,
1125         JsonType::NUMBER,
1126         false,
1127         g_parseResult,
1128         ArrayType::NOT_ARRAY);
1129     GetValueIfFindKey<std::string>(jsonObject,
1130         jsonObjectEnd,
1131         COMPILE_SDK_VERSION,
1132         app.compileSdkVersion,
1133         JsonType::STRING,
1134         false,
1135         g_parseResult,
1136         ArrayType::NOT_ARRAY);
1137     GetValueIfFindKey<std::string>(jsonObject,
1138         jsonObjectEnd,
1139         COMPILE_SDK_TYPE,
1140         app.compileSdkType,
1141         JsonType::STRING,
1142         false,
1143         g_parseResult,
1144         ArrayType::NOT_ARRAY);
1145 }
1146 
from_json(const nlohmann::json & jsonObject,Module & module)1147 void from_json(const nlohmann::json &jsonObject, Module &module)
1148 {
1149     TAG_LOGD(AAFwkTag::ABILITY_SIM, "read module tag from module.json");
1150     const auto &jsonObjectEnd = jsonObject.end();
1151     GetValueIfFindKey<std::string>(jsonObject,
1152         jsonObjectEnd,
1153         MODULE_NAME,
1154         module.name,
1155         JsonType::STRING,
1156         true,
1157         g_parseResult,
1158         ArrayType::NOT_ARRAY);
1159     GetValueIfFindKey<std::string>(jsonObject,
1160         jsonObjectEnd,
1161         MODULE_TYPE,
1162         module.type,
1163         JsonType::STRING,
1164         true,
1165         g_parseResult,
1166         ArrayType::NOT_ARRAY);
1167     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1168         jsonObjectEnd,
1169         MODULE_DEVICE_TYPES,
1170         module.deviceTypes,
1171         JsonType::ARRAY,
1172         true,
1173         g_parseResult,
1174         ArrayType::STRING);
1175     GetValueIfFindKey<bool>(jsonObject,
1176         jsonObjectEnd,
1177         MODULE_DELIVERY_WITH_INSTALL,
1178         module.deliveryWithInstall,
1179         JsonType::BOOLEAN,
1180         true,
1181         g_parseResult,
1182         ArrayType::NOT_ARRAY);
1183     GetValueIfFindKey<std::string>(jsonObject,
1184         jsonObjectEnd,
1185         MODULE_PAGES,
1186         module.pages,
1187         JsonType::STRING,
1188         false,
1189         g_parseResult,
1190         ArrayType::NOT_ARRAY);
1191     // both srcEntry and srcEntrance can be configured, but srcEntry has higher priority
1192     if (jsonObject.find(SRC_ENTRY) != jsonObject.end()) {
1193         GetValueIfFindKey<std::string>(jsonObject,
1194             jsonObjectEnd,
1195             SRC_ENTRY,
1196             module.srcEntrance,
1197             JsonType::STRING,
1198             false,
1199             g_parseResult,
1200             ArrayType::NOT_ARRAY);
1201     } else {
1202         GetValueIfFindKey<std::string>(jsonObject,
1203             jsonObjectEnd,
1204             SRC_ENTRANCE,
1205             module.srcEntrance,
1206             JsonType::STRING,
1207             false,
1208             g_parseResult,
1209             ArrayType::NOT_ARRAY);
1210     }
1211     GetValueIfFindKey<std::string>(jsonObject,
1212         jsonObjectEnd,
1213         DESCRIPTION,
1214         module.description,
1215         JsonType::STRING,
1216         false,
1217         g_parseResult,
1218         ArrayType::NOT_ARRAY);
1219     GetValueIfFindKey<int32_t>(jsonObject,
1220         jsonObjectEnd,
1221         DESCRIPTION_ID,
1222         module.descriptionId,
1223         JsonType::NUMBER,
1224         false,
1225         g_parseResult,
1226         ArrayType::NOT_ARRAY);
1227     GetValueIfFindKey<std::string>(jsonObject,
1228         jsonObjectEnd,
1229         MODULE_PROCESS,
1230         module.process,
1231         JsonType::STRING,
1232         false,
1233         g_parseResult,
1234         ArrayType::NOT_ARRAY);
1235     GetValueIfFindKey<std::string>(jsonObject,
1236         jsonObjectEnd,
1237         MODULE_MAIN_ELEMENT,
1238         module.mainElement,
1239         JsonType::STRING,
1240         false,
1241         g_parseResult,
1242         ArrayType::NOT_ARRAY);
1243     GetValueIfFindKey<bool>(jsonObject,
1244         jsonObjectEnd,
1245         MODULE_INSTALLATION_FREE,
1246         module.installationFree,
1247         JsonType::BOOLEAN,
1248         false,
1249         g_parseResult,
1250         ArrayType::NOT_ARRAY);
1251     GetValueIfFindKey<std::string>(jsonObject,
1252         jsonObjectEnd,
1253         MODULE_VIRTUAL_MACHINE,
1254         module.virtualMachine,
1255         JsonType::STRING,
1256         false,
1257         g_parseResult,
1258         ArrayType::NOT_ARRAY);
1259     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
1260         jsonObjectEnd,
1261         META_DATA,
1262         module.metadata,
1263         JsonType::ARRAY,
1264         false,
1265         g_parseResult,
1266         ArrayType::OBJECT);
1267     GetValueIfFindKey<std::vector<Ability>>(jsonObject,
1268         jsonObjectEnd,
1269         MODULE_ABILITIES,
1270         module.abilities,
1271         JsonType::ARRAY,
1272         false,
1273         g_parseResult,
1274         ArrayType::OBJECT);
1275     GetValueIfFindKey<std::vector<Extension>>(jsonObject,
1276         jsonObjectEnd,
1277         MODULE_EXTENSION_ABILITIES,
1278         module.extensionAbilities,
1279         JsonType::ARRAY,
1280         false,
1281         g_parseResult,
1282         ArrayType::OBJECT);
1283     GetValueIfFindKey<std::vector<Dependency>>(jsonObject,
1284         jsonObjectEnd,
1285         MODULE_DEPENDENCIES,
1286         module.dependencies,
1287         JsonType::ARRAY,
1288         false,
1289         g_parseResult,
1290         ArrayType::OBJECT);
1291     GetValueIfFindKey<std::string>(jsonObject,
1292         jsonObjectEnd,
1293         MODULE_COMPILE_MODE,
1294         module.compileMode,
1295         JsonType::STRING,
1296         false,
1297         g_parseResult,
1298         ArrayType::NOT_ARRAY);
1299     GetValueIfFindKey<bool>(jsonObject,
1300         jsonObjectEnd,
1301         MODULE_IS_LIB_ISOLATED,
1302         module.isLibIsolated,
1303         JsonType::BOOLEAN,
1304         false,
1305         g_parseResult,
1306         ArrayType::NOT_ARRAY);
1307     GetValueIfFindKey<std::string>(jsonObject,
1308         jsonObjectEnd,
1309         MODULE_TARGET_MODULE_NAME,
1310         module.targetModule,
1311         JsonType::STRING,
1312         false,
1313         g_parseResult,
1314         ArrayType::NOT_ARRAY);
1315     GetValueIfFindKey<int32_t>(jsonObject,
1316         jsonObjectEnd,
1317         MODULE_TARGET_PRIORITY,
1318         module.targetPriority,
1319         JsonType::NUMBER,
1320         false,
1321         g_parseResult,
1322         ArrayType::NOT_ARRAY);
1323     GetValueIfFindKey<std::vector<ProxyData>>(jsonObject,
1324         jsonObjectEnd,
1325         MODULE_PROXY_DATAS,
1326         module.proxyDatas,
1327         JsonType::ARRAY,
1328         false,
1329         g_parseResult,
1330         ArrayType::OBJECT);
1331     GetValueIfFindKey<std::vector<ProxyData>>(jsonObject,
1332         jsonObjectEnd,
1333         MODULE_PROXY_DATA,
1334         module.proxyData,
1335         JsonType::ARRAY,
1336         false,
1337         g_parseResult,
1338         ArrayType::OBJECT);
1339     GetValueIfFindKey<std::string>(jsonObject,
1340         jsonObjectEnd,
1341         MODULE_BUILD_HASH,
1342         module.buildHash,
1343         JsonType::STRING,
1344         false,
1345         g_parseResult,
1346         ArrayType::NOT_ARRAY);
1347     GetValueIfFindKey<std::string>(jsonObject,
1348         jsonObjectEnd,
1349         MODULE_ISOLATION_MODE,
1350         module.isolationMode,
1351         JsonType::STRING,
1352         false,
1353         g_parseResult,
1354         ArrayType::NOT_ARRAY);
1355 }
1356 
from_json(const nlohmann::json & jsonObject,ModuleJson & moduleJson)1357 void from_json(const nlohmann::json &jsonObject, ModuleJson &moduleJson)
1358 {
1359     const auto &jsonObjectEnd = jsonObject.end();
1360     GetValueIfFindKey<App>(jsonObject,
1361         jsonObjectEnd,
1362         APP,
1363         moduleJson.app,
1364         JsonType::OBJECT,
1365         true,
1366         g_parseResult,
1367         ArrayType::NOT_ARRAY);
1368     GetValueIfFindKey<Module>(jsonObject,
1369         jsonObjectEnd,
1370         MODULE,
1371         moduleJson.module,
1372         JsonType::OBJECT,
1373         true,
1374         g_parseResult,
1375         ArrayType::NOT_ARRAY);
1376 }
1377 } // namespace Profile
1378 
1379 namespace {
1380 struct TransformParam {
1381     bool isSystemApp = false;
1382     bool isPreInstallApp = false;
1383 };
1384 
GetMetadata(std::vector<Metadata> & metadata,const std::vector<Profile::Metadata> & profileMetadata)1385 void GetMetadata(std::vector<Metadata> &metadata, const std::vector<Profile::Metadata> &profileMetadata)
1386 {
1387     for (const Profile::Metadata &item : profileMetadata) {
1388         Metadata tmpMetadata;
1389         tmpMetadata.name = item.name;
1390         tmpMetadata.value = item.value;
1391         tmpMetadata.resource = item.resource;
1392         metadata.emplace_back(tmpMetadata);
1393     }
1394 }
1395 
CheckBundleNameIsValid(const std::string & bundleName)1396 bool CheckBundleNameIsValid(const std::string &bundleName)
1397 {
1398     if (bundleName.empty()) {
1399         return false;
1400     }
1401     if (bundleName.size() < Constants::MIN_BUNDLE_NAME || bundleName.size() > Constants::MAX_BUNDLE_NAME) {
1402         return false;
1403     }
1404     char head = bundleName.at(0);
1405     if (!isalpha(head)) {
1406         return false;
1407     }
1408     for (const auto &c : bundleName) {
1409         if (!isalnum(c) && (c != '.') && (c != '_')) {
1410             return false;
1411         }
1412     }
1413     return true;
1414 }
1415 
CheckModuleNameIsValid(const std::string & moduleName)1416 bool CheckModuleNameIsValid(const std::string &moduleName)
1417 {
1418     if (moduleName.empty()) {
1419         return false;
1420     }
1421     if (moduleName.size() > Constants::MAX_MODULE_NAME) {
1422         return false;
1423     }
1424     if (moduleName.find(Constants::RELATIVE_PATH) != std::string::npos) {
1425         return false;
1426     }
1427     if (moduleName.find(Constants::MODULE_NAME_SEPARATOR) != std::string::npos) {
1428         TAG_LOGE(AAFwkTag::ABILITY_SIM, "module name contain");
1429         return false;
1430     }
1431     return true;
1432 }
1433 
GetResource(const std::string & bundleName,const std::string & moduleName,int32_t resId)1434 Resource GetResource(const std::string &bundleName, const std::string &moduleName, int32_t resId)
1435 {
1436     Resource resource;
1437     resource.bundleName = bundleName;
1438     resource.moduleName = moduleName;
1439     resource.id = resId;
1440     return resource;
1441 }
1442 
ToApplicationInfo(const Profile::ModuleJson & moduleJson,const TransformParam & transformParam,ApplicationInfo & applicationInfo)1443 bool ToApplicationInfo(
1444     const Profile::ModuleJson &moduleJson,
1445     const TransformParam &transformParam,
1446     ApplicationInfo &applicationInfo)
1447 {
1448     TAG_LOGD(AAFwkTag::ABILITY_SIM, "transform ModuleJson to ApplicationInfo");
1449     auto app = moduleJson.app;
1450     applicationInfo.name = app.bundleName;
1451     applicationInfo.bundleName = app.bundleName;
1452 
1453     applicationInfo.versionCode = static_cast<uint32_t>(app.versionCode);
1454     applicationInfo.versionName = app.versionName;
1455     if (app.minCompatibleVersionCode != -1) {
1456         applicationInfo.minCompatibleVersionCode = app.minCompatibleVersionCode;
1457     } else {
1458         applicationInfo.minCompatibleVersionCode = static_cast<int32_t>(applicationInfo.versionCode);
1459     }
1460 
1461     applicationInfo.apiCompatibleVersion = app.minAPIVersion;
1462     applicationInfo.apiTargetVersion = app.targetAPIVersion;
1463 
1464     applicationInfo.iconPath = app.icon;
1465     applicationInfo.iconId = app.iconId;
1466     applicationInfo.label = app.label;
1467     applicationInfo.labelId = app.labelId;
1468     applicationInfo.description = app.description;
1469     applicationInfo.descriptionId = app.descriptionId;
1470     applicationInfo.iconResource = GetResource(app.bundleName, moduleJson.module.name, app.iconId);
1471     applicationInfo.labelResource = GetResource(app.bundleName, moduleJson.module.name, app.labelId);
1472     applicationInfo.descriptionResource = GetResource(app.bundleName, moduleJson.module.name, app.descriptionId);
1473     applicationInfo.targetBundleList = app.targetBundleList;
1474 
1475     if (transformParam.isSystemApp && transformParam.isPreInstallApp) {
1476         applicationInfo.keepAlive = app.keepAlive;
1477         applicationInfo.singleton = app.singleton;
1478         applicationInfo.userDataClearable = app.userDataClearable;
1479         if (app.removable.first) {
1480             applicationInfo.removable = app.removable.second;
1481         } else {
1482             applicationInfo.removable = false;
1483         }
1484         applicationInfo.accessible = app.accessible;
1485     }
1486 
1487     applicationInfo.apiReleaseType = app.apiReleaseType;
1488     applicationInfo.debug = app.debug;
1489     applicationInfo.deviceId = Constants::CURRENT_DEVICE_ID;
1490     applicationInfo.distributedNotificationEnabled = true;
1491     applicationInfo.entityType = Profile::APP_ENTITY_TYPE_DEFAULT_VALUE;
1492     applicationInfo.vendor = app.vendor;
1493     applicationInfo.asanEnabled = app.asanEnabled;
1494     if (app.bundleType == Profile::BUNDLE_TYPE_ATOMIC_SERVICE) {
1495         applicationInfo.bundleType = BundleType::ATOMIC_SERVICE;
1496     }
1497 
1498     applicationInfo.enabled = true;
1499     applicationInfo.multiProjects = app.multiProjects;
1500     applicationInfo.process = app.bundleName;
1501     applicationInfo.targetBundleName = app.targetBundle;
1502     applicationInfo.targetPriority = app.targetPriority;
1503 
1504     auto iterBundleType = std::find_if(std::begin(Profile::BUNDLE_TYPE_MAP),
1505         std::end(Profile::BUNDLE_TYPE_MAP),
1506         [&app](const auto &item) { return item.first == app.bundleType; });
1507     if (iterBundleType != Profile::BUNDLE_TYPE_MAP.end()) {
1508         applicationInfo.bundleType = iterBundleType->second;
1509     }
1510     applicationInfo.compileSdkVersion = app.compileSdkVersion;
1511     applicationInfo.compileSdkType = app.compileSdkType;
1512     return true;
1513 }
1514 
GetBackgroundModes(const std::vector<std::string> & backgroundModes)1515 uint32_t GetBackgroundModes(const std::vector<std::string> &backgroundModes)
1516 {
1517     uint32_t backgroundMode = 0;
1518     for (const std::string &item : backgroundModes) {
1519         if (Profile::BACKGROUND_MODES_MAP.find(item) != Profile::BACKGROUND_MODES_MAP.end()) {
1520             backgroundMode |= Profile::BACKGROUND_MODES_MAP.at(item);
1521         }
1522     }
1523     return backgroundMode;
1524 }
1525 
ConvertCompileMode(const std::string & compileMode)1526 inline CompileMode ConvertCompileMode(const std::string& compileMode)
1527 {
1528     if (compileMode == Profile::COMPILE_MODE_ES_MODULE) {
1529         return CompileMode::ES_MODULE;
1530     } else {
1531         return CompileMode::JS_BUNDLE;
1532     }
1533 }
1534 
ConvertToAbilityWindowMode(const std::vector<std::string> & windowModes,const std::unordered_map<std::string,SupportWindowMode> & windowMap)1535 std::set<SupportWindowMode> ConvertToAbilityWindowMode(const std::vector<std::string> &windowModes,
1536     const std::unordered_map<std::string, SupportWindowMode> &windowMap)
1537 {
1538     std::set<SupportWindowMode> modes;
1539     for_each(windowModes.begin(), windowModes.end(),
1540         [&windowMap, &modes](const auto &mode)->decltype(auto) {
1541         if (windowMap.find(mode) != windowMap.end()) {
1542             modes.emplace(windowMap.at(mode));
1543         }
1544     });
1545     if (modes.empty()) {
1546         modes.insert(SupportWindowMode::FULLSCREEN);
1547         modes.insert(SupportWindowMode::SPLIT);
1548         modes.insert(SupportWindowMode::FLOATING);
1549     }
1550     return modes;
1551 }
1552 
ToAbilityInfo(const Profile::ModuleJson & moduleJson,const Profile::Ability & ability,const TransformParam & transformParam,AbilityInfo & abilityInfo)1553 bool ToAbilityInfo(
1554     const Profile::ModuleJson &moduleJson,
1555     const Profile::Ability &ability,
1556     const TransformParam &transformParam,
1557     AbilityInfo &abilityInfo)
1558 {
1559     TAG_LOGD(AAFwkTag::ABILITY_SIM, "transform ModuleJson to AbilityInfo");
1560     abilityInfo.name = ability.name;
1561     abilityInfo.srcEntrance = ability.srcEntrance;
1562     abilityInfo.description = ability.description;
1563     abilityInfo.descriptionId = ability.descriptionId;
1564     abilityInfo.iconPath = ability.icon;
1565     abilityInfo.iconId = ability.iconId;
1566     abilityInfo.label = ability.label;
1567     abilityInfo.labelId = ability.labelId;
1568     abilityInfo.priority = ability.priority;
1569     abilityInfo.excludeFromMissions = ability.excludeFromMissions;
1570     abilityInfo.unclearableMission = ability.unclearableMission;
1571     abilityInfo.recoverable = ability.recoverable;
1572     abilityInfo.permissions = ability.permissions;
1573     abilityInfo.visible = ability.visible;
1574     abilityInfo.continuable = ability.continuable;
1575     abilityInfo.backgroundModes = GetBackgroundModes(ability.backgroundModes);
1576     GetMetadata(abilityInfo.metadata, ability.metadata);
1577     abilityInfo.package = moduleJson.module.name;
1578     abilityInfo.bundleName = moduleJson.app.bundleName;
1579     abilityInfo.moduleName = moduleJson.module.name;
1580     abilityInfo.applicationName = moduleJson.app.bundleName;
1581     auto iterLaunch = std::find_if(std::begin(Profile::LAUNCH_MODE_MAP),
1582         std::end(Profile::LAUNCH_MODE_MAP),
1583         [&ability](const auto &item) { return item.first == ability.launchType; });
1584     if (iterLaunch != Profile::LAUNCH_MODE_MAP.end()) {
1585         abilityInfo.launchMode = iterLaunch->second;
1586     }
1587     abilityInfo.enabled = true;
1588     abilityInfo.isModuleJson = true;
1589     abilityInfo.isStageBasedModel = true;
1590     abilityInfo.type = AbilityType::PAGE;
1591     for (const std::string &deviceType : moduleJson.module.deviceTypes) {
1592         abilityInfo.deviceTypes.emplace_back(deviceType);
1593     }
1594     abilityInfo.startWindowIcon = ability.startWindowIcon;
1595     abilityInfo.startWindowIconId = ability.startWindowIconId;
1596     abilityInfo.startWindowBackground = ability.startWindowBackground;
1597     abilityInfo.startWindowBackgroundId = ability.startWindowBackgroundId;
1598     abilityInfo.removeMissionAfterTerminate = ability.removeMissionAfterTerminate;
1599     abilityInfo.compileMode = ConvertCompileMode(moduleJson.module.compileMode);
1600     auto iterOrientation = std::find_if(std::begin(Profile::DISPLAY_ORIENTATION_MAP),
1601         std::end(Profile::DISPLAY_ORIENTATION_MAP),
1602         [&ability](const auto &item) { return item.first == ability.orientation; });
1603     if (iterOrientation != Profile::DISPLAY_ORIENTATION_MAP.end()) {
1604         abilityInfo.orientation = iterOrientation->second;
1605     }
1606 
1607     auto modesSet = ConvertToAbilityWindowMode(ability.windowModes, Profile::WINDOW_MODE_MAP);
1608     abilityInfo.windowModes.assign(modesSet.begin(), modesSet.end());
1609     abilityInfo.maxWindowRatio = ability.maxWindowRatio;
1610     abilityInfo.minWindowRatio = ability.minWindowRatio;
1611     abilityInfo.maxWindowWidth = ability.maxWindowWidth;
1612     abilityInfo.minWindowWidth = ability.minWindowWidth;
1613     abilityInfo.maxWindowHeight = ability.maxWindowHeight;
1614     abilityInfo.minWindowHeight = ability.minWindowHeight;
1615     return true;
1616 }
1617 
ToExtensionInfo(const Profile::ModuleJson & moduleJson,const Profile::Extension & extension,const TransformParam & transformParam,ExtensionAbilityInfo & extensionInfo)1618 bool ToExtensionInfo(
1619     const Profile::ModuleJson &moduleJson,
1620     const Profile::Extension &extension,
1621     const TransformParam &transformParam,
1622     ExtensionAbilityInfo &extensionInfo)
1623 {
1624     TAG_LOGD(AAFwkTag::ABILITY_SIM, "transform ModuleJson to ExtensionAbilityInfo");
1625     extensionInfo.type = ConvertToExtensionAbilityType(extension.type);
1626     extensionInfo.name = extension.name;
1627     extensionInfo.srcEntrance = extension.srcEntrance;
1628     extensionInfo.icon = extension.icon;
1629     extensionInfo.iconId = extension.iconId;
1630     extensionInfo.label = extension.label;
1631     extensionInfo.labelId = extension.labelId;
1632     extensionInfo.description = extension.description;
1633     extensionInfo.descriptionId = extension.descriptionId;
1634     if (transformParam.isSystemApp && transformParam.isPreInstallApp) {
1635         extensionInfo.readPermission = extension.readPermission;
1636         extensionInfo.writePermission = extension.writePermission;
1637     }
1638     extensionInfo.priority = extension.priority;
1639     extensionInfo.uri = extension.uri;
1640     extensionInfo.permissions = extension.permissions;
1641     extensionInfo.visible = extension.visible;
1642     GetMetadata(extensionInfo.metadata, extension.metadata);
1643     extensionInfo.bundleName = moduleJson.app.bundleName;
1644     extensionInfo.moduleName = moduleJson.module.name;
1645 
1646     if (extensionInfo.type != ExtensionAbilityType::SERVICE &&
1647         extensionInfo.type != ExtensionAbilityType::DATASHARE) {
1648         extensionInfo.process = extensionInfo.bundleName;
1649         extensionInfo.process.append(":");
1650         extensionInfo.process.append(ConvertToExtensionTypeName(extensionInfo.type));
1651     }
1652 
1653     extensionInfo.compileMode = ConvertCompileMode(moduleJson.module.compileMode);
1654 
1655     return true;
1656 }
1657 
ToInnerModuleInfo(const Profile::ModuleJson & moduleJson,const TransformParam & transformParam,InnerModuleInfo & innerModuleInfo)1658 bool ToInnerModuleInfo(
1659     const Profile::ModuleJson &moduleJson,
1660     const TransformParam &transformParam,
1661     InnerModuleInfo &innerModuleInfo)
1662 {
1663     TAG_LOGD(AAFwkTag::ABILITY_SIM, "transform ModuleJson to InnerModuleInfo");
1664     innerModuleInfo.name = moduleJson.module.name;
1665     innerModuleInfo.modulePackage = moduleJson.module.name;
1666     innerModuleInfo.moduleName = moduleJson.module.name;
1667     innerModuleInfo.description = moduleJson.module.description;
1668     innerModuleInfo.descriptionId = moduleJson.module.descriptionId;
1669     GetMetadata(innerModuleInfo.metadata, moduleJson.module.metadata);
1670     innerModuleInfo.distro.deliveryWithInstall = moduleJson.module.deliveryWithInstall;
1671     innerModuleInfo.distro.installationFree = moduleJson.module.installationFree;
1672     innerModuleInfo.distro.moduleName = moduleJson.module.name;
1673     innerModuleInfo.installationFree = moduleJson.module.installationFree;
1674     if (Profile::MODULE_TYPE_SET.find(moduleJson.module.type) != Profile::MODULE_TYPE_SET.end()) {
1675         innerModuleInfo.distro.moduleType = moduleJson.module.type;
1676         if (moduleJson.module.type == Profile::MODULE_TYPE_ENTRY) {
1677             innerModuleInfo.isEntry = true;
1678         }
1679     }
1680 
1681     innerModuleInfo.mainAbility = moduleJson.module.mainElement;
1682     innerModuleInfo.srcEntrance = moduleJson.module.srcEntrance;
1683     innerModuleInfo.process = moduleJson.module.process;
1684 
1685     for (const std::string &deviceType : moduleJson.module.deviceTypes) {
1686         innerModuleInfo.deviceTypes.emplace_back(deviceType);
1687     }
1688 
1689     if (Profile::VIRTUAL_MACHINE_SET.find(moduleJson.module.virtualMachine) != Profile::VIRTUAL_MACHINE_SET.end()) {
1690         innerModuleInfo.virtualMachine = moduleJson.module.virtualMachine;
1691     }
1692 
1693     innerModuleInfo.uiSyntax = Profile::MODULE_UI_SYNTAX_DEFAULT_VALUE;
1694     innerModuleInfo.pages = moduleJson.module.pages;
1695     innerModuleInfo.dependencies = moduleJson.module.dependencies;
1696     innerModuleInfo.compileMode = moduleJson.module.compileMode;
1697     innerModuleInfo.isModuleJson = true;
1698     innerModuleInfo.isStageBasedModel = true;
1699     innerModuleInfo.isLibIsolated = moduleJson.module.isLibIsolated;
1700     innerModuleInfo.targetModuleName = moduleJson.module.targetModule;
1701     innerModuleInfo.targetPriority = moduleJson.module.targetPriority;
1702     if (moduleJson.module.proxyDatas.empty()) {
1703         innerModuleInfo.proxyDatas = moduleJson.module.proxyData;
1704     } else {
1705         innerModuleInfo.proxyDatas = moduleJson.module.proxyDatas;
1706     }
1707     innerModuleInfo.buildHash = moduleJson.module.buildHash;
1708     innerModuleInfo.isolationMode = moduleJson.module.isolationMode;
1709     return true;
1710 }
1711 
SetInstallationFree(InnerModuleInfo & innerModuleInfo,BundleType bundleType)1712 void SetInstallationFree(InnerModuleInfo &innerModuleInfo, BundleType bundleType)
1713 {
1714     if (bundleType == BundleType::ATOMIC_SERVICE) {
1715         innerModuleInfo.distro.installationFree = true;
1716         innerModuleInfo.installationFree = true;
1717     } else {
1718         innerModuleInfo.distro.installationFree = false;
1719         innerModuleInfo.installationFree = false;
1720     }
1721 }
1722 
ParseExtensionInfo(const Profile::ModuleJson & moduleJson,InnerBundleInfo & innerBundleInfo,const TransformParam & transformParam,InnerModuleInfo & innerModuleInfo)1723 bool ParseExtensionInfo(const Profile::ModuleJson &moduleJson, InnerBundleInfo &innerBundleInfo,
1724     const TransformParam &transformParam, InnerModuleInfo &innerModuleInfo)
1725 {
1726     for (const Profile::Extension &extension : moduleJson.module.extensionAbilities) {
1727         ExtensionAbilityInfo extensionInfo;
1728         if (!ToExtensionInfo(moduleJson, extension, transformParam, extensionInfo)) {
1729             TAG_LOGE(AAFwkTag::ABILITY_SIM, "extensionInfo failed");
1730             return false;
1731         }
1732 
1733         if (innerModuleInfo.mainAbility == extensionInfo.name) {
1734             innerModuleInfo.icon = extensionInfo.icon;
1735             innerModuleInfo.iconId = extensionInfo.iconId;
1736             innerModuleInfo.label = extensionInfo.label;
1737             innerModuleInfo.labelId = extensionInfo.labelId;
1738         }
1739 
1740         std::string key;
1741         key.append(moduleJson.app.bundleName).append(".")
1742             .append(moduleJson.module.name).append(".").append(extension.name);
1743         innerModuleInfo.extensionKeys.emplace_back(key);
1744         innerBundleInfo.InsertExtensionInfo(key, extensionInfo);
1745     }
1746     return true;
1747 }
1748 
ToInnerBundleInfo(const Profile::ModuleJson & moduleJson,InnerBundleInfo & innerBundleInfo)1749 bool ToInnerBundleInfo(const Profile::ModuleJson &moduleJson, InnerBundleInfo &innerBundleInfo)
1750 {
1751     if (!CheckBundleNameIsValid(moduleJson.app.bundleName) || !CheckModuleNameIsValid(moduleJson.module.name)) {
1752         return false;
1753     }
1754     TransformParam transformParam;
1755     ApplicationInfo applicationInfo;
1756     applicationInfo.isSystemApp = innerBundleInfo.GetAppType() == Constants::AppType::SYSTEM_APP;
1757     transformParam.isSystemApp = applicationInfo.isSystemApp;
1758     if (!ToApplicationInfo(moduleJson, transformParam, applicationInfo)) {
1759         return false;
1760     }
1761     InnerModuleInfo innerModuleInfo;
1762     ToInnerModuleInfo(moduleJson, transformParam, innerModuleInfo);
1763     SetInstallationFree(innerModuleInfo, applicationInfo.bundleType);
1764     for (const Profile::Ability &ability : moduleJson.module.abilities) {
1765         AbilityInfo abilityInfo;
1766         ToAbilityInfo(moduleJson, ability, transformParam, abilityInfo);
1767         if (innerModuleInfo.mainAbility == abilityInfo.name) {
1768             innerModuleInfo.icon = abilityInfo.iconPath;
1769             innerModuleInfo.iconId = abilityInfo.iconId;
1770             innerModuleInfo.label = abilityInfo.label;
1771             innerModuleInfo.labelId = abilityInfo.labelId;
1772         }
1773         std::string key;
1774         key.append(moduleJson.app.bundleName).append(".")
1775             .append(moduleJson.module.name).append(".").append(abilityInfo.name);
1776         innerModuleInfo.abilityKeys.emplace_back(key);
1777         innerBundleInfo.InsertAbilitiesInfo(key, abilityInfo);
1778     }
1779     if (!ParseExtensionInfo(moduleJson, innerBundleInfo, transformParam, innerModuleInfo)) {
1780         return false;
1781     }
1782     if (!transformParam.isPreInstallApp &&
1783         innerModuleInfo.distro.moduleType != Profile::MODULE_TYPE_SHARED) {
1784         applicationInfo.needAppDetail = true;
1785         applicationInfo.appDetailAbilityLibraryPath = Profile::APP_DETAIL_ABILITY_LIBRARY_PATH;
1786         if ((applicationInfo.labelId == 0) && (applicationInfo.label.empty())) {
1787             applicationInfo.label = applicationInfo.bundleName;
1788         }
1789     }
1790     innerBundleInfo.SetCurrentModulePackage(moduleJson.module.name);
1791     innerBundleInfo.SetBaseApplicationInfo(applicationInfo);
1792     innerBundleInfo.InsertInnerModuleInfo(moduleJson.module.name, innerModuleInfo);
1793     innerBundleInfo.SetTargetPriority(moduleJson.app.targetPriority);
1794     return true;
1795 }
1796 } // namespace
1797 
TransformTo(const std::vector<uint8_t> & buf,InnerBundleInfo & innerBundleInfo) const1798 ErrCode ModuleProfile::TransformTo(const std::vector<uint8_t> &buf, InnerBundleInfo &innerBundleInfo) const
1799 {
1800     TAG_LOGD(AAFwkTag::ABILITY_SIM, "transform module.json stream to InnerBundleInfo");
1801     std::vector<uint8_t> buffer = buf;
1802     buffer.push_back('\0');
1803     nlohmann::json jsonObject = nlohmann::json::parse(buffer.data(), nullptr, false);
1804     if (jsonObject.is_discarded()) {
1805         TAG_LOGE(AAFwkTag::ABILITY_SIM, "bad profile");
1806         return ERR_APPEXECFWK_PARSE_BAD_PROFILE;
1807     }
1808 
1809     Profile::ModuleJson moduleJson = jsonObject.get<Profile::ModuleJson>();
1810     if (Profile::g_parseResult != ERR_OK) {
1811         TAG_LOGE(AAFwkTag::ABILITY_SIM, "g_parseResult:%{public}d", Profile::g_parseResult);
1812         int32_t ret = Profile::g_parseResult;
1813         // need recover parse result to ERR_OK
1814         Profile::g_parseResult = ERR_OK;
1815         return ret;
1816     }
1817 
1818     if (!ToInnerBundleInfo(moduleJson, innerBundleInfo)) {
1819         return ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR;
1820     }
1821 
1822     return ERR_OK;
1823 }
1824 }  // namespace AppExecFwk
1825 }  // namespace OHOS
1826