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