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