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