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