• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "bundle_profile.h"
17 
18 #include <algorithm>
19 #include <fstream>
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 ProfileReader {
33 
34 thread_local int32_t parseResult;
35 const std::set<std::string> MODULE_TYPE_SET = {
36     "entry",
37     "feature",
38     "shared"
39 };
40 const std::map<std::string, AbilityType> ABILITY_TYPE_MAP = {
41     {"page", AbilityType::PAGE},
42     {"service", AbilityType::SERVICE},
43     {"data", AbilityType::DATA},
44     {"form", AbilityType::FORM}
45 };
46 const std::map<std::string, DisplayOrientation> DISPLAY_ORIENTATION_MAP = {
47     {"unspecified", DisplayOrientation::UNSPECIFIED},
48     {"landscape", DisplayOrientation::LANDSCAPE},
49     {"portrait", DisplayOrientation::PORTRAIT},
50     {"followRecent", DisplayOrientation::FOLLOWRECENT}
51 };
52 const std::map<std::string, LaunchMode> LAUNCH_MODE_MAP = {
53     {"singleton", LaunchMode::SINGLETON},
54     {"standard", LaunchMode::STANDARD},
55     {"specified", LaunchMode::SPECIFIED}
56 };
57 const std::map<std::string, int32_t> dimensionMap = {
58     {"1*2", 1},
59     {"2*2", 2},
60     {"2*4", 3},
61     {"4*4", 4},
62     {"2*1", 5}
63 };
64 const std::map<std::string, FormType> formTypeMap = {
65     {"JS", FormType::JS},
66     {"Java", FormType::JAVA}
67 };
68 const std::map<std::string, ModuleColorMode> moduleColorMode = {
69     {"auto", ModuleColorMode::AUTO},
70     {"dark", ModuleColorMode::DARK},
71     {"light", ModuleColorMode::LIGHT},
72 };
73 const std::map<std::string, FormsColorMode> formColorModeMap = {
74     {"auto", FormsColorMode::AUTO_MODE},
75     {"dark", FormsColorMode::DARK_MODE},
76     {"light", FormsColorMode::LIGHT_MODE}
77 };
78 std::map<std::string, uint32_t> backgroundModeMap = {
79     {KEY_DATA_TRANSFER, VALUE_DATA_TRANSFER},
80     {KEY_AUDIO_PLAYBACK, VALUE_AUDIO_PLAYBACK},
81     {KEY_AUDIO_RECORDING, VALUE_AUDIO_RECORDING},
82     {KEY_LOCATION, VALUE_LOCATION},
83     {KEY_BLUETOOTH_INTERACTION, VALUE_BLUETOOTH_INTERACTION},
84     {KEY_MULTI_DEVICE_CONNECTION, VALUE_MULTI_DEVICE_CONNECTION},
85     {KEY_WIFI_INTERACTION, VALUE_WIFI_INTERACTION},
86     {KEY_VOIP, VALUE_VOIP},
87     {KEY_TASK_KEEPING, VALUE_TASK_KEEPING},
88     {KEY_PICTURE_IN_PICTURE, VALUE_PICTURE_IN_PICTURE},
89     {KEY_SCREEN_FETCH, VALUE_SCREEN_FETCH}
90 };
91 
92 struct Version {
93     int32_t code = 0;
94     std::string name;
95     int32_t minCompatibleVersionCode = -1;
96 };
97 
98 struct ApiVersion {
99     uint32_t compatible = 0;
100     uint32_t target = 0;
101     std::string releaseType = "Release";
102 };
103 // config.json app
104 struct App {
105     std::string bundleName;
106     std::string originalName;
107     std::string vendor;
108     // pair first : if exist in config.json then true, otherwise false
109     // pair second : actual value
110     std::pair<bool, bool> removable = std::make_pair<>(false, true);
111     Version version;
112     ApiVersion apiVersion;
113     bool singleton = false;
114     int32_t iconId = 0;
115     int32_t labelId = 0;
116     bool userDataClearable = true;
117     bool asanEnabled = false;
118     std::vector<std::string> targetBundleList;
119 };
120 
121 struct ReqVersion {
122     uint32_t compatible = 0;
123     uint32_t target = 0;
124 };
125 
126 struct Ark {
127     ReqVersion reqVersion;
128     std::string flag;
129 };
130 
131 struct Domain {
132     bool subDomains = false;
133     std::string name;
134 };
135 
136 struct DomainSetting {
137     bool cleartextPermitted = false;
138     std::vector<Domain> domains;
139 };
140 
141 struct SecurityConfig {
142     DomainSetting domainSetting;
143 };
144 
145 struct Network {
146     bool usesCleartext = false;
147     SecurityConfig securityConfig;
148 };
149 
150 struct Device {
151     std::string jointUserId;
152     std::string process;
153     bool keepAlive = false;
154     Ark ark;
155     bool directLaunch = false;
156     bool supportBackup = false;
157     bool compressNativeLibs = true;
158     Network network;
159     bool debug = false;
160 };
161 // config.json  deviceConfig
162 struct DeviceConfig {
163     Device defaultDevice;
164     Device phone;
165     Device tablet;
166     Device tv;
167     Device car;
168     Device wearable;
169     Device liteWearable;
170     Device smartVision;
171 };
172 
173 struct Form {
174     std::vector<std::string> formEntity;
175     int32_t minHeight = 0;
176     int32_t defaultHeight = 0;
177     int32_t minWidth = 0;
178     int32_t defaultWidth = 0;
179 };
180 
181 struct FormsCustomizeData {
182     std::string name;
183     std::string value;
184 };
185 
186 struct FormsMetaData {
187     std::vector<FormsCustomizeData> customizeData;
188 };
189 
190 struct Window {
191     int32_t designWidth = 720;
192     bool autoDesignWidth = false;
193 };
194 
195 struct Forms {
196     std::string name;
197     std::string description;
198     int32_t descriptionId = 0;
199     bool isDefault = false;
200     std::string type;
201     std::string src;
202     Window window;
203     std::string colorMode = "auto";
204     std::vector<std::string> supportDimensions;
205     std::string defaultDimension;
206     std::vector<std::string> landscapeLayouts;
207     std::vector<std::string> portraitLayouts;
208     bool updateEnabled = false;
209     std::string scheduledUpdateTime = "";
210     int32_t updateDuration = 0;
211     std::string deepLink;
212     std::string formConfigAbility;
213     bool formVisibleNotify = false;
214     std::string jsComponentName;
215     FormsMetaData metaData;
216 };
217 
218 struct CustomizeData {
219     std::string name;
220     std::string value;
221     std::string extra;
222 };
223 
224 struct MetaData {
225     std::vector<CustomizeData> customizeData;
226 };
227 
228 struct UriPermission {
229     std::string mode;
230     std::string path;
231 };
232 
233 struct Ability {
234     std::string name;
235     std::string originalName;
236     std::string description;
237     int32_t descriptionId = 0;
238     std::string icon;
239     int32_t iconId = 0;
240     std::string label;
241     int32_t labelId = 0;
242     int32_t priority = 0;
243     std::string uri;
244     std::string process;
245     std::string launchType = "singleton";
246     std::string theme;
247     bool visible = false;
248     bool continuable = false;
249     std::vector<std::string> permissions;
250     std::vector<Skill> skills;
251     std::vector<std::string> deviceCapability;
252     MetaData metaData;
253     std::string type;
254     std::string srcPath;
255     std::string srcLanguage = "js";
256     bool formEnabled = false;
257     Form form;
258     std::string orientation = "unspecified";
259     std::vector<std::string> backgroundModes;
260     bool grantPermission;
261     UriPermission uriPermission;
262     std::string readPermission;
263     std::string writePermission;
264     bool directLaunch = false;
265     std::vector<std::string> configChanges;
266     std::string mission;
267     std::string targetAbility;
268     bool multiUserShared = false;
269     bool supportPipMode = false;
270     bool formsEnabled = false;
271     std::vector<Forms> formses;
272     std::string startWindowIcon;
273     int32_t startWindowIconId = 0;
274     std::string startWindowBackground;
275     int32_t startWindowBackgroundId = 0;
276     bool removeMissionAfterTerminate = false;
277 };
278 
279 struct Js {
280     std::string name = "default";
281     std::vector<std::string> pages;
282     Window window;
283     std::string type = "normal";
284 };
285 
286 struct Intent {
287     std::string targetClass;
288     std::string targetBundle;
289 };
290 
291 struct CommonEvent {
292     std::string name;
293     std::string permission;
294     std::vector<std::string> data;
295     std::vector<std::string> type;
296     std::vector<std::string> events;
297 };
298 
299 struct Shortcut {
300     std::string shortcutId;
301     std::string label;
302     std::string icon;
303     int32_t iconId;
304     int32_t labelId;
305     std::vector<Intent> intents;
306 };
307 
308 // config.json module
309 struct Module {
310     std::string package;
311     std::string name;
312     std::string description;
313     int32_t descriptionId = 0;
314     std::string colorMode = "auto";
315     std::vector<std::string> supportedModes;
316     std::vector<std::string> reqCapabilities;
317     std::vector<std::string> deviceType;
318     std::vector<Dependency> dependencies;
319     Distro distro;
320     MetaData metaData;
321     std::vector<Ability> abilities;
322     std::vector<Js> jses;
323     std::vector<CommonEvent> commonEvents;
324     std::vector<Shortcut> shortcuts;
325     std::vector<RequestPermission> requestPermissions;
326     std::vector<DefinePermission> definePermissions;
327     std::string mainAbility;
328     std::string srcPath;
329     bool isLibIsolated = false;
330 };
331 
332 // config.json
333 struct ConfigJson {
334     App app;
335     DeviceConfig deveicConfig;
336     Module module;
337 };
338 
339 /*
340  * form_json is global static overload method in self namespace ProfileReader,
341  * which need callback by json library, and can not rename this function,
342  * so don't named according UpperCamelCase style
343  */
from_json(const nlohmann::json & jsonObject,Version & version)344 void from_json(const nlohmann::json &jsonObject, Version &version)
345 {
346     const auto &jsonObjectEnd = jsonObject.end();
347     GetValueIfFindKey<int32_t>(jsonObject,
348         jsonObjectEnd,
349         BUNDLE_APP_PROFILE_KEY_CODE,
350         version.code,
351         JsonType::NUMBER,
352         true,
353         parseResult,
354         ArrayType::NOT_ARRAY);
355     GetValueIfFindKey<std::string>(jsonObject,
356         jsonObjectEnd,
357         PROFILE_KEY_NAME,
358         version.name,
359         JsonType::STRING,
360         true,
361         parseResult,
362         ArrayType::NOT_ARRAY);
363     GetValueIfFindKey<int32_t>(jsonObject,
364         jsonObjectEnd,
365         BUNDLE_APP_PROFILE_KEY_MIN_COMPATIBLE_VERSION_CODE,
366         version.minCompatibleVersionCode,
367         JsonType::NUMBER,
368         false,
369         parseResult,
370         ArrayType::NOT_ARRAY);
371 }
372 
from_json(const nlohmann::json & jsonObject,ApiVersion & apiVersion)373 void from_json(const nlohmann::json &jsonObject, ApiVersion &apiVersion)
374 {
375     // these are required fields.
376     const auto &jsonObjectEnd = jsonObject.end();
377     GetValueIfFindKey<uint32_t>(jsonObject,
378         jsonObjectEnd,
379         BUNDLE_APP_PROFILE_KEY_COMPATIBLE,
380         apiVersion.compatible,
381         JsonType::NUMBER,
382         true,
383         parseResult,
384         ArrayType::NOT_ARRAY);
385     // these are not required fields.
386     GetValueIfFindKey<uint32_t>(jsonObject,
387         jsonObjectEnd,
388         BUNDLE_APP_PROFILE_KEY_TARGET,
389         apiVersion.target,
390         JsonType::NUMBER,
391         false,
392         parseResult,
393         ArrayType::NOT_ARRAY);
394     GetValueIfFindKey<std::string>(jsonObject,
395         jsonObjectEnd,
396         BUNDLE_APP_PROFILE_KEY_RELEASE_TYPE,
397         apiVersion.releaseType,
398         JsonType::STRING,
399         false,
400         parseResult,
401         ArrayType::NOT_ARRAY);
402 }
403 
from_json(const nlohmann::json & jsonObject,App & app)404 void from_json(const nlohmann::json &jsonObject, App &app)
405 {
406     // these are required fields.
407     const auto &jsonObjectEnd = jsonObject.end();
408     GetValueIfFindKey<std::string>(jsonObject,
409         jsonObjectEnd,
410         BUNDLE_APP_PROFILE_KEY_BUNDLE_NAME,
411         app.bundleName,
412         JsonType::STRING,
413         true,
414         parseResult,
415         ArrayType::NOT_ARRAY);
416     GetValueIfFindKey<std::string>(jsonObject,
417         jsonObjectEnd,
418         PROFILE_KEY_ORIGINAL_NAME,
419         app.originalName,
420         JsonType::STRING,
421         false,
422         parseResult,
423         ArrayType::NOT_ARRAY);
424     GetValueIfFindKey<Version>(jsonObject,
425         jsonObjectEnd,
426         BUNDLE_APP_PROFILE_KEY_VERSION,
427         app.version,
428         JsonType::OBJECT,
429         true,
430         parseResult,
431         ArrayType::NOT_ARRAY);
432     GetValueIfFindKey<ApiVersion>(jsonObject,
433         jsonObjectEnd,
434         BUNDLE_APP_PROFILE_KEY_API_VERSION,
435         app.apiVersion,
436         JsonType::OBJECT,
437         true,
438         parseResult,
439         ArrayType::NOT_ARRAY);
440     // these are not required fields.
441     GetValueIfFindKey<std::string>(jsonObject,
442         jsonObjectEnd,
443         BUNDLE_APP_PROFILE_KEY_VENDOR,
444         app.vendor,
445         JsonType::STRING,
446         false,
447         parseResult,
448         ArrayType::NOT_ARRAY);
449     GetValueIfFindKey<bool>(jsonObject,
450         jsonObjectEnd,
451         BUNDLE_APP_PROFILE_KEY_SINGLETON,
452         app.singleton,
453         JsonType::BOOLEAN,
454         false,
455         parseResult,
456         ArrayType::NOT_ARRAY);
457     GetValueIfFindKey<int32_t>(jsonObject,
458         jsonObjectEnd,
459         PROFILE_KEY_ICON_ID,
460         app.iconId,
461         JsonType::NUMBER,
462         false,
463         parseResult,
464         ArrayType::NOT_ARRAY);
465     GetValueIfFindKey<int32_t>(jsonObject,
466         jsonObjectEnd,
467         PROFILE_KEY_LABEL_ID,
468         app.labelId,
469         JsonType::NUMBER,
470         false,
471         parseResult,
472         ArrayType::NOT_ARRAY);
473     if (jsonObject.find(BUNDLE_APP_PROFILE_KEY_REMOVABLE) != jsonObject.end()) {
474         app.removable.first = true;
475         GetValueIfFindKey<bool>(jsonObject,
476             jsonObjectEnd,
477             BUNDLE_APP_PROFILE_KEY_REMOVABLE,
478             app.removable.second,
479             JsonType::BOOLEAN,
480             false,
481             parseResult,
482             ArrayType::NOT_ARRAY);
483     }
484     GetValueIfFindKey<bool>(jsonObject,
485         jsonObjectEnd,
486         BUNDLE_APP_PROFILE_KEY_USER_DATA_CLEARABLE,
487         app.userDataClearable,
488         JsonType::BOOLEAN,
489         false,
490         parseResult,
491         ArrayType::NOT_ARRAY);
492     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
493         jsonObjectEnd,
494         BUNDLE_APP_PROFILE_KEY_TARGETET_BUNDLE_LIST,
495         app.targetBundleList,
496         JsonType::ARRAY,
497         false,
498         parseResult,
499         ArrayType::STRING);
500     GetValueIfFindKey<bool>(jsonObject,
501         jsonObjectEnd,
502         BUNDLE_APP_PROFILE_KEY_ASAN_ENABLED,
503         app.asanEnabled,
504         JsonType::BOOLEAN,
505         false,
506         parseResult,
507         ArrayType::NOT_ARRAY);
508 }
509 
from_json(const nlohmann::json & jsonObject,ReqVersion & reqVersion)510 void from_json(const nlohmann::json &jsonObject, ReqVersion &reqVersion)
511 {
512     // these are required fields.
513     const auto &jsonObjectEnd = jsonObject.end();
514     GetValueIfFindKey<uint32_t>(jsonObject,
515         jsonObjectEnd,
516         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_COMPATIBLE,
517         reqVersion.compatible,
518         JsonType::NUMBER,
519         true,
520         parseResult,
521         ArrayType::NOT_ARRAY);
522     // these are not required fields.
523     GetValueIfFindKey<uint32_t>(jsonObject,
524         jsonObjectEnd,
525         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_TARGET,
526         reqVersion.target,
527         JsonType::NUMBER,
528         false,
529         parseResult,
530         ArrayType::NOT_ARRAY);
531 }
532 
from_json(const nlohmann::json & jsonObject,Ark & ark)533 void from_json(const nlohmann::json &jsonObject, Ark &ark)
534 {
535     // these are not required fields.
536     const auto &jsonObjectEnd = jsonObject.end();
537     GetValueIfFindKey<ReqVersion>(jsonObject,
538         jsonObjectEnd,
539         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_REQ_VERSION,
540         ark.reqVersion,
541         JsonType::OBJECT,
542         false,
543         parseResult,
544         ArrayType::NOT_ARRAY);
545     GetValueIfFindKey<std::string>(jsonObject,
546         jsonObjectEnd,
547         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_FLAG,
548         ark.flag,
549         JsonType::STRING,
550         false,
551         parseResult,
552         ArrayType::NOT_ARRAY);
553 }
554 
from_json(const nlohmann::json & jsonObject,Domain & domain)555 void from_json(const nlohmann::json &jsonObject, Domain &domain)
556 {
557     // these are required fields.
558     const auto &jsonObjectEnd = jsonObject.end();
559     GetValueIfFindKey<bool>(jsonObject,
560         jsonObjectEnd,
561         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_SUB_DOMAINS,
562         domain.subDomains,
563         JsonType::BOOLEAN,
564         true,
565         parseResult,
566         ArrayType::NOT_ARRAY);
567     GetValueIfFindKey<std::string>(jsonObject,
568         jsonObjectEnd,
569         PROFILE_KEY_NAME,
570         domain.name,
571         JsonType::STRING,
572         true,
573         parseResult,
574         ArrayType::NOT_ARRAY);
575 }
576 
from_json(const nlohmann::json & jsonObject,DomainSetting & domainSetting)577 void from_json(const nlohmann::json &jsonObject, DomainSetting &domainSetting)
578 {
579     // these are required fields.
580     const auto &jsonObjectEnd = jsonObject.end();
581     GetValueIfFindKey<bool>(jsonObject,
582         jsonObjectEnd,
583         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_CLEAR_TEXT_PERMITTED,
584         domainSetting.cleartextPermitted,
585         JsonType::BOOLEAN,
586         true,
587         parseResult,
588         ArrayType::NOT_ARRAY);
589     GetValueIfFindKey<std::vector<Domain>>(jsonObject,
590         jsonObjectEnd,
591         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_DOMAINS,
592         domainSetting.domains,
593         JsonType::ARRAY,
594         true,
595         parseResult,
596         ArrayType::OBJECT);
597 }
598 
from_json(const nlohmann::json & jsonObject,SecurityConfig & securityConfig)599 void from_json(const nlohmann::json &jsonObject, SecurityConfig &securityConfig)
600 {
601     // these are not required fields.
602     const auto &jsonObjectEnd = jsonObject.end();
603     GetValueIfFindKey<DomainSetting>(jsonObject,
604         jsonObjectEnd,
605         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_DOMAIN_SETTINGS,
606         securityConfig.domainSetting,
607         JsonType::OBJECT,
608         false,
609         parseResult,
610         ArrayType::NOT_ARRAY);
611 }
612 
from_json(const nlohmann::json & jsonObject,Network & network)613 void from_json(const nlohmann::json &jsonObject, Network &network)
614 {
615     // these are not required fields.
616     const auto &jsonObjectEnd = jsonObject.end();
617     GetValueIfFindKey<bool>(jsonObject,
618         jsonObjectEnd,
619         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_USES_CLEAR_TEXT,
620         network.usesCleartext,
621         JsonType::BOOLEAN,
622         false,
623         parseResult,
624         ArrayType::NOT_ARRAY);
625     GetValueIfFindKey<SecurityConfig>(jsonObject,
626         jsonObjectEnd,
627         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_SECURITY_CONFIG,
628         network.securityConfig,
629         JsonType::OBJECT,
630         false,
631         parseResult,
632         ArrayType::NOT_ARRAY);
633 }
634 
from_json(const nlohmann::json & jsonObject,Device & device)635 void from_json(const nlohmann::json &jsonObject, Device &device)
636 {
637     // these are not required fields.
638     const auto &jsonObjectEnd = jsonObject.end();
639     GetValueIfFindKey<std::string>(jsonObject,
640         jsonObjectEnd,
641         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_JOINT_USER_ID,
642         device.jointUserId,
643         JsonType::STRING,
644         false,
645         parseResult,
646         ArrayType::NOT_ARRAY);
647     GetValueIfFindKey<std::string>(jsonObject,
648         jsonObjectEnd,
649         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_PROCESS,
650         device.process,
651         JsonType::STRING,
652         false,
653         parseResult,
654         ArrayType::NOT_ARRAY);
655     GetValueIfFindKey<bool>(jsonObject,
656         jsonObjectEnd,
657         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_KEEP_ALIVE,
658         device.keepAlive,
659         JsonType::BOOLEAN,
660         false,
661         parseResult,
662         ArrayType::NOT_ARRAY);
663     GetValueIfFindKey<Ark>(jsonObject,
664         jsonObjectEnd,
665         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_ARK,
666         device.ark,
667         JsonType::OBJECT,
668         false,
669         parseResult,
670         ArrayType::NOT_ARRAY);
671     GetValueIfFindKey<bool>(jsonObject,
672         jsonObjectEnd,
673         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_DIRECT_LAUNCH,
674         device.directLaunch,
675         JsonType::BOOLEAN,
676         false,
677         parseResult,
678         ArrayType::NOT_ARRAY);
679     GetValueIfFindKey<bool>(jsonObject,
680         jsonObjectEnd,
681         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_SUPPORT_BACKUP,
682         device.supportBackup,
683         JsonType::BOOLEAN,
684         false,
685         parseResult,
686         ArrayType::NOT_ARRAY);
687     GetValueIfFindKey<bool>(jsonObject,
688         jsonObjectEnd,
689         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_DEBUG,
690         device.debug,
691         JsonType::BOOLEAN,
692         false,
693         parseResult,
694         ArrayType::NOT_ARRAY);
695     GetValueIfFindKey<bool>(jsonObject,
696         jsonObjectEnd,
697         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_COMPRESS_NATIVE_LIBS,
698         device.compressNativeLibs,
699         JsonType::BOOLEAN,
700         false,
701         parseResult,
702         ArrayType::NOT_ARRAY);
703     GetValueIfFindKey<Network>(jsonObject,
704         jsonObjectEnd,
705         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_NETWORK,
706         device.network,
707         JsonType::OBJECT,
708         false,
709         parseResult,
710         ArrayType::NOT_ARRAY);
711 }
712 
from_json(const nlohmann::json & jsonObject,DeviceConfig & deviceConfig)713 void from_json(const nlohmann::json &jsonObject, DeviceConfig &deviceConfig)
714 {
715     // these are required fields.
716     const auto &jsonObjectEnd = jsonObject.end();
717     GetValueIfFindKey<Device>(jsonObject,
718         jsonObjectEnd,
719         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_DEFAULT,
720         deviceConfig.defaultDevice,
721         JsonType::OBJECT,
722         false,
723         parseResult,
724         ArrayType::NOT_ARRAY);
725     // these are not required fields.
726     GetValueIfFindKey<Device>(jsonObject,
727         jsonObjectEnd,
728         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_PHONE,
729         deviceConfig.phone,
730         JsonType::OBJECT,
731         false,
732         parseResult,
733         ArrayType::NOT_ARRAY);
734     GetValueIfFindKey<Device>(jsonObject,
735         jsonObjectEnd,
736         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_TABLET,
737         deviceConfig.tablet,
738         JsonType::OBJECT,
739         false,
740         parseResult,
741         ArrayType::NOT_ARRAY);
742     GetValueIfFindKey<Device>(jsonObject,
743         jsonObjectEnd,
744         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_TV,
745         deviceConfig.tv,
746         JsonType::OBJECT,
747         false,
748         parseResult,
749         ArrayType::NOT_ARRAY);
750     GetValueIfFindKey<Device>(jsonObject,
751         jsonObjectEnd,
752         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_CAR,
753         deviceConfig.car,
754         JsonType::OBJECT,
755         false,
756         parseResult,
757         ArrayType::NOT_ARRAY);
758     GetValueIfFindKey<Device>(jsonObject,
759         jsonObjectEnd,
760         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_WEARABLE,
761         deviceConfig.wearable,
762         JsonType::OBJECT,
763         false,
764         parseResult,
765         ArrayType::NOT_ARRAY);
766     GetValueIfFindKey<Device>(jsonObject,
767         jsonObjectEnd,
768         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_LITE_WEARABLE,
769         deviceConfig.liteWearable,
770         JsonType::OBJECT,
771         false,
772         parseResult,
773         ArrayType::NOT_ARRAY);
774     GetValueIfFindKey<Device>(jsonObject,
775         jsonObjectEnd,
776         BUNDLE_DEVICE_CONFIG_PROFILE_KEY_SMART_VISION,
777         deviceConfig.smartVision,
778         JsonType::OBJECT,
779         false,
780         parseResult,
781         ArrayType::NOT_ARRAY);
782 }
783 
from_json(const nlohmann::json & jsonObject,Form & form)784 void from_json(const nlohmann::json &jsonObject, Form &form)
785 {
786     // these are not required fields.
787     const auto &jsonObjectEnd = jsonObject.end();
788     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
789         jsonObjectEnd,
790         BUNDLE_MODULE_PROFILE_FORM_ENTITY,
791         form.formEntity,
792         JsonType::ARRAY,
793         false,
794         parseResult,
795         ArrayType::STRING);
796     GetValueIfFindKey<int32_t>(jsonObject,
797         jsonObjectEnd,
798         BUNDLE_MODULE_PROFILE_FORM_MIN_HEIGHT,
799         form.minHeight,
800         JsonType::NUMBER,
801         false,
802         parseResult,
803         ArrayType::NOT_ARRAY);
804     GetValueIfFindKey<int32_t>(jsonObject,
805         jsonObjectEnd,
806         BUNDLE_MODULE_PROFILE_FORM_DEFAULT_HEIGHT,
807         form.defaultHeight,
808         JsonType::NUMBER,
809         false,
810         parseResult,
811         ArrayType::NOT_ARRAY);
812     GetValueIfFindKey<int32_t>(jsonObject,
813         jsonObjectEnd,
814         BUNDLE_MODULE_PROFILE_FORM_MIN_WIDTH,
815         form.minWidth,
816         JsonType::NUMBER,
817         false,
818         parseResult,
819         ArrayType::NOT_ARRAY);
820     GetValueIfFindKey<int32_t>(jsonObject,
821         jsonObjectEnd,
822         BUNDLE_MODULE_PROFILE_FORM_DEFAULT_WIDTH,
823         form.defaultWidth,
824         JsonType::NUMBER,
825         false,
826         parseResult,
827         ArrayType::NOT_ARRAY);
828 }
829 
from_json(const nlohmann::json & jsonObject,CustomizeData & customizeData)830 void from_json(const nlohmann::json &jsonObject, CustomizeData &customizeData)
831 {
832     // these are not required fields.
833     const auto &jsonObjectEnd = jsonObject.end();
834     GetValueIfFindKey<std::string>(jsonObject,
835         jsonObjectEnd,
836         PROFILE_KEY_NAME,
837         customizeData.name,
838         JsonType::STRING,
839         false,
840         parseResult,
841         ArrayType::NOT_ARRAY);
842     GetValueIfFindKey<std::string>(jsonObject,
843         jsonObjectEnd,
844         BUNDLE_MODULE_META_KEY_EXTRA,
845         customizeData.extra,
846         JsonType::STRING,
847         false,
848         parseResult,
849         ArrayType::NOT_ARRAY);
850     GetValueIfFindKey<std::string>(jsonObject,
851         jsonObjectEnd,
852         BUNDLE_MODULE_META_KEY_VALUE,
853         customizeData.value,
854         JsonType::STRING,
855         false,
856         parseResult,
857         ArrayType::NOT_ARRAY);
858 }
859 
from_json(const nlohmann::json & jsonObject,MetaData & metaData)860 void from_json(const nlohmann::json &jsonObject, MetaData &metaData)
861 {
862     // these are not required fields.
863     const auto &jsonObjectEnd = jsonObject.end();
864     GetValueIfFindKey<std::vector<CustomizeData>>(jsonObject,
865         jsonObjectEnd,
866         BUNDLE_MODULE_META_KEY_CUSTOMIZE_DATA,
867         metaData.customizeData,
868         JsonType::ARRAY,
869         false,
870         parseResult,
871         ArrayType::OBJECT);
872 }
873 
from_json(const nlohmann::json & jsonObject,FormsCustomizeData & customizeDataForms)874 void from_json(const nlohmann::json &jsonObject, FormsCustomizeData &customizeDataForms)
875 {
876     // these are not required fields.
877     const auto &jsonObjectEnd = jsonObject.end();
878     GetValueIfFindKey<std::string>(jsonObject,
879         jsonObjectEnd,
880         PROFILE_KEY_NAME,
881         customizeDataForms.name,
882         JsonType::STRING,
883         false,
884         parseResult,
885         ArrayType::NOT_ARRAY);
886     GetValueIfFindKey<std::string>(jsonObject,
887         jsonObjectEnd,
888         BUNDLE_MODULE_PROFILE_FORMS_VALUE,
889         customizeDataForms.value,
890         JsonType::STRING,
891         false,
892         parseResult,
893         ArrayType::NOT_ARRAY);
894 }
895 
from_json(const nlohmann::json & jsonObject,FormsMetaData & formsMetaData)896 void from_json(const nlohmann::json &jsonObject, FormsMetaData &formsMetaData)
897 {
898     // these are not required fields.
899     const auto &jsonObjectEnd = jsonObject.end();
900     GetValueIfFindKey<std::vector<FormsCustomizeData>>(jsonObject,
901         jsonObjectEnd,
902         BUNDLE_MODULE_PROFILE_KEY_CUSTOMIZE_DATA,
903         formsMetaData.customizeData,
904         JsonType::ARRAY,
905         false,
906         parseResult,
907         ArrayType::OBJECT);
908 }
909 
from_json(const nlohmann::json & jsonObject,Window & window)910 void from_json(const nlohmann::json &jsonObject, Window &window)
911 {
912     // these are not required fields.
913     const auto &jsonObjectEnd = jsonObject.end();
914     GetValueIfFindKey<int32_t>(jsonObject,
915         jsonObjectEnd,
916         BUNDLE_MODULE_PROFILE_KEY_DESIGN_WIDTH,
917         window.designWidth,
918         JsonType::NUMBER,
919         false,
920         parseResult,
921         ArrayType::NOT_ARRAY);
922     GetValueIfFindKey<bool>(jsonObject,
923         jsonObjectEnd,
924         BUNDLE_MODULE_PROFILE_KEY_AUTO_DESIGN_WIDTH,
925         window.autoDesignWidth,
926         JsonType::BOOLEAN,
927         false,
928         parseResult,
929         ArrayType::NOT_ARRAY);
930 }
931 
from_json(const nlohmann::json & jsonObject,Forms & forms)932 void from_json(const nlohmann::json &jsonObject, Forms &forms)
933 {
934     // these are required fields.
935     const auto &jsonObjectEnd = jsonObject.end();
936     GetValueIfFindKey<std::string>(jsonObject,
937         jsonObjectEnd,
938         PROFILE_KEY_NAME,
939         forms.name,
940         JsonType::STRING,
941         true,
942         parseResult,
943         ArrayType::NOT_ARRAY);
944     GetValueIfFindKey<bool>(jsonObject,
945         jsonObjectEnd,
946         BUNDLE_MODULE_PROFILE_FORMS_IS_DEFAULT,
947         forms.isDefault,
948         JsonType::BOOLEAN,
949         true,
950         parseResult,
951         ArrayType::NOT_ARRAY);
952     GetValueIfFindKey<std::string>(jsonObject,
953         jsonObjectEnd,
954         PROFILE_KEY_TYPE,
955         forms.type,
956         JsonType::STRING,
957         true,
958         parseResult,
959         ArrayType::NOT_ARRAY);
960     GetValueIfFindKey<std::string>(jsonObject,
961         jsonObjectEnd,
962         BUNDLE_MODULE_PROFILE_FORMS_SRC,
963         forms.src,
964         JsonType::STRING,
965         false,
966         parseResult,
967         ArrayType::NOT_ARRAY);
968     GetValueIfFindKey<Window>(jsonObject,
969         jsonObjectEnd,
970         BUNDLE_MODULE_PROFILE_KEY_WINDOW,
971         forms.window,
972         JsonType::OBJECT,
973         false,
974         parseResult,
975         ArrayType::NOT_ARRAY);
976     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
977         jsonObjectEnd,
978         BUNDLE_MODULE_PROFILE_FORMS_SUPPORT_DIMENSIONS,
979         forms.supportDimensions,
980         JsonType::ARRAY,
981         true,
982         parseResult,
983         ArrayType::STRING);
984     GetValueIfFindKey<std::string>(jsonObject,
985         jsonObjectEnd,
986         BUNDLE_MODULE_PROFILE_FORMS_DEFAULT_DIMENSION,
987         forms.defaultDimension,
988         JsonType::STRING,
989         true,
990         parseResult,
991         ArrayType::NOT_ARRAY);
992     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
993         jsonObjectEnd,
994         BUNDLE_MODULE_PROFILE_FORMS_LANDSCAPE_LAYOUTS,
995         forms.landscapeLayouts,
996         JsonType::ARRAY,
997         false,
998         parseResult,
999         ArrayType::STRING);
1000     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1001         jsonObjectEnd,
1002         BUNDLE_MODULE_PROFILE_FORMS_PORTRAIT_LAYOUTS,
1003         forms.portraitLayouts,
1004         JsonType::ARRAY,
1005         false,
1006         parseResult,
1007         ArrayType::STRING);
1008     GetValueIfFindKey<bool>(jsonObject,
1009         jsonObjectEnd,
1010         BUNDLE_MODULE_PROFILE_FORMS_UPDATEENABLED,
1011         forms.updateEnabled,
1012         JsonType::BOOLEAN,
1013         true,
1014         parseResult,
1015         ArrayType::NOT_ARRAY);
1016     GetValueIfFindKey<std::string>(jsonObject,
1017         jsonObjectEnd,
1018         BUNDLE_MODULE_PROFILE_FORMS_JS_COMPONENT_NAME,
1019         forms.jsComponentName,
1020         JsonType::STRING,
1021         true,
1022         parseResult,
1023         ArrayType::NOT_ARRAY);
1024     // these are not required fields.
1025     GetValueIfFindKey<std::string>(jsonObject,
1026         jsonObjectEnd,
1027         PROFILE_KEY_DESCRIPTION,
1028         forms.description,
1029         JsonType::STRING,
1030         false,
1031         parseResult,
1032         ArrayType::NOT_ARRAY);
1033     GetValueIfFindKey<int32_t>(jsonObject,
1034         jsonObjectEnd,
1035         PROFILE_KEY_DESCRIPTION_ID,
1036         forms.descriptionId,
1037         JsonType::NUMBER,
1038         false,
1039         parseResult,
1040         ArrayType::NOT_ARRAY);
1041     GetValueIfFindKey<std::string>(jsonObject,
1042         jsonObjectEnd,
1043         BUNDLE_MODULE_PROFILE_FORMS_COLOR_MODE,
1044         forms.colorMode,
1045         JsonType::STRING,
1046         false,
1047         parseResult,
1048         ArrayType::NOT_ARRAY);
1049     GetValueIfFindKey<std::string>(jsonObject,
1050         jsonObjectEnd,
1051         BUNDLE_MODULE_PROFILE_FORMS_SCHEDULED_UPDATE_TIME,
1052         forms.scheduledUpdateTime,
1053         JsonType::STRING,
1054         false,
1055         parseResult,
1056         ArrayType::NOT_ARRAY);
1057     GetValueIfFindKey<int32_t>(jsonObject,
1058         jsonObjectEnd,
1059         BUNDLE_MODULE_PROFILE_FORMS_UPDATE_DURATION,
1060         forms.updateDuration,
1061         JsonType::NUMBER,
1062         false,
1063         parseResult,
1064         ArrayType::NOT_ARRAY);
1065     GetValueIfFindKey<std::string>(jsonObject,
1066         jsonObjectEnd,
1067         BUNDLE_MODULE_PROFILE_FORMS_DEEP_LINK,
1068         forms.deepLink,
1069         JsonType::STRING,
1070         false,
1071         parseResult,
1072         ArrayType::NOT_ARRAY);
1073     GetValueIfFindKey<std::string>(jsonObject,
1074         jsonObjectEnd,
1075         BUNDLE_MODULE_PROFILE_FORMS_FORM_CONFIG_ABILITY,
1076         forms.formConfigAbility,
1077         JsonType::STRING,
1078         false,
1079         parseResult,
1080         ArrayType::NOT_ARRAY);
1081     GetValueIfFindKey<bool>(jsonObject,
1082         jsonObjectEnd,
1083         BUNDLE_MODULE_PROFILE_FORMS_FORM_VISIBLE_NOTIFY,
1084         forms.formVisibleNotify,
1085         JsonType::BOOLEAN,
1086         false,
1087         parseResult,
1088         ArrayType::NOT_ARRAY);
1089     GetValueIfFindKey<FormsMetaData>(jsonObject,
1090         jsonObjectEnd,
1091         BUNDLE_MODULE_PROFILE_KEY_META_DATA,
1092         forms.metaData,
1093         JsonType::OBJECT,
1094         false,
1095         parseResult,
1096         ArrayType::NOT_ARRAY);
1097 }
1098 
from_json(const nlohmann::json & jsonObject,UriPermission & uriPermission)1099 void from_json(const nlohmann::json &jsonObject, UriPermission &uriPermission)
1100 {
1101     // these are not required fields.
1102     const auto &jsonObjectEnd = jsonObject.end();
1103     GetValueIfFindKey<std::string>(jsonObject,
1104         jsonObjectEnd,
1105         BUNDLE_MODULE_PROFILE_KEY_MODE,
1106         uriPermission.mode,
1107         JsonType::STRING,
1108         false,
1109         parseResult,
1110         ArrayType::NOT_ARRAY);
1111     GetValueIfFindKey<std::string>(jsonObject,
1112         jsonObjectEnd,
1113         BUNDLE_MODULE_PROFILE_KEY_PATH,
1114         uriPermission.path,
1115         JsonType::STRING,
1116         false,
1117         parseResult,
1118         ArrayType::NOT_ARRAY);
1119 }
1120 
from_json(const nlohmann::json & jsonObject,Ability & ability)1121 void from_json(const nlohmann::json &jsonObject, Ability &ability)
1122 {
1123     // these are required fields.
1124     const auto &jsonObjectEnd = jsonObject.end();
1125     GetValueIfFindKey<std::string>(jsonObject,
1126         jsonObjectEnd,
1127         PROFILE_KEY_NAME,
1128         ability.name,
1129         JsonType::STRING,
1130         true,
1131         parseResult,
1132         ArrayType::NOT_ARRAY);
1133     GetValueIfFindKey<std::string>(jsonObject,
1134         jsonObjectEnd,
1135         PROFILE_KEY_ORIGINAL_NAME,
1136         ability.originalName,
1137         JsonType::STRING,
1138         false,
1139         parseResult,
1140         ArrayType::NOT_ARRAY);
1141     GetValueIfFindKey<std::string>(jsonObject,
1142         jsonObjectEnd,
1143         PROFILE_KEY_TYPE,
1144         ability.type,
1145         JsonType::STRING,
1146         true,
1147         parseResult,
1148         ArrayType::NOT_ARRAY);
1149     GetValueIfFindKey<std::string>(jsonObject,
1150         jsonObjectEnd,
1151         PROFILE_KEY_SRCPATH,
1152         ability.srcPath,
1153         JsonType::STRING,
1154         false,
1155         parseResult,
1156         ArrayType::NOT_ARRAY);
1157     // these are not required fields.
1158     GetValueIfFindKey<std::string>(jsonObject,
1159         jsonObjectEnd,
1160         PROFILE_KEY_SRCLANGUAGE,
1161         ability.srcLanguage,
1162         JsonType::STRING,
1163         false,
1164         parseResult,
1165         ArrayType::NOT_ARRAY);
1166     GetValueIfFindKey<std::string>(jsonObject,
1167         jsonObjectEnd,
1168         PROFILE_KEY_DESCRIPTION,
1169         ability.description,
1170         JsonType::STRING,
1171         false,
1172         parseResult,
1173         ArrayType::NOT_ARRAY);
1174     GetValueIfFindKey<int32_t>(jsonObject,
1175         jsonObjectEnd,
1176         PROFILE_KEY_DESCRIPTION_ID,
1177         ability.descriptionId,
1178         JsonType::NUMBER,
1179         false,
1180         parseResult,
1181         ArrayType::NOT_ARRAY);
1182     GetValueIfFindKey<std::string>(jsonObject,
1183         jsonObjectEnd,
1184         BUNDLE_MODULE_PROFILE_KEY_ICON,
1185         ability.icon,
1186         JsonType::STRING,
1187         false,
1188         parseResult,
1189         ArrayType::NOT_ARRAY);
1190     GetValueIfFindKey<int32_t>(jsonObject,
1191         jsonObjectEnd,
1192         BUNDLE_MODULE_PROFILE_KEY_ICON_ID,
1193         ability.iconId,
1194         JsonType::NUMBER,
1195         false,
1196         parseResult,
1197         ArrayType::NOT_ARRAY);
1198     GetValueIfFindKey<std::string>(jsonObject,
1199         jsonObjectEnd,
1200         BUNDLE_MODULE_PROFILE_KEY_PROCESS,
1201         ability.process,
1202         JsonType::STRING,
1203         false,
1204         parseResult,
1205         ArrayType::NOT_ARRAY);
1206     GetValueIfFindKey<std::string>(jsonObject,
1207         jsonObjectEnd,
1208         PROFILE_KEY_LABEL,
1209         ability.label,
1210         JsonType::STRING,
1211         false,
1212         parseResult,
1213         ArrayType::NOT_ARRAY);
1214     GetValueIfFindKey<int32_t>(jsonObject,
1215         jsonObjectEnd,
1216         PROFILE_KEY_LABEL_ID,
1217         ability.labelId,
1218         JsonType::NUMBER,
1219         false,
1220         parseResult,
1221         ArrayType::NOT_ARRAY);
1222     GetValueIfFindKey<int32_t>(jsonObject,
1223         jsonObjectEnd,
1224         PRIORITY,
1225         ability.priority,
1226         JsonType::NUMBER,
1227         false,
1228         parseResult,
1229         ArrayType::NOT_ARRAY);
1230     GetValueIfFindKey<std::string>(jsonObject,
1231         jsonObjectEnd,
1232         BUNDLE_MODULE_PROFILE_KEY_URI,
1233         ability.uri,
1234         JsonType::STRING,
1235         false,
1236         parseResult,
1237         ArrayType::NOT_ARRAY);
1238     GetValueIfFindKey<std::string>(jsonObject,
1239         jsonObjectEnd,
1240         BUNDLE_MODULE_PROFILE_KEY_LAUNCH_TYPE,
1241         ability.launchType,
1242         JsonType::STRING,
1243         false,
1244         parseResult,
1245         ArrayType::NOT_ARRAY);
1246     GetValueIfFindKey<std::string>(jsonObject,
1247         jsonObjectEnd,
1248         BUNDLE_MODULE_PROFILE_KEY_LAUNCH_THEME,
1249         ability.theme,
1250         JsonType::STRING,
1251         false,
1252         parseResult,
1253         ArrayType::NOT_ARRAY);
1254     GetValueIfFindKey<bool>(jsonObject,
1255         jsonObjectEnd,
1256         BUNDLE_MODULE_PROFILE_KEY_VISIBLE,
1257         ability.visible,
1258         JsonType::BOOLEAN,
1259         false,
1260         parseResult,
1261         ArrayType::NOT_ARRAY);
1262     GetValueIfFindKey<bool>(jsonObject,
1263         jsonObjectEnd,
1264         BUNDLE_MODULE_PROFILE_KEY_CONTINUABLE,
1265         ability.continuable,
1266         JsonType::BOOLEAN,
1267         false,
1268         parseResult,
1269         ArrayType::NOT_ARRAY);
1270     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1271         jsonObjectEnd,
1272         BUNDLE_MODULE_PROFILE_KEY_PERMISSIONS,
1273         ability.permissions,
1274         JsonType::ARRAY,
1275         false,
1276         parseResult,
1277         ArrayType::STRING);
1278     GetValueIfFindKey<std::vector<Skill>>(jsonObject,
1279         jsonObjectEnd,
1280         BUNDLE_MODULE_PROFILE_KEY_SKILLS,
1281         ability.skills,
1282         JsonType::ARRAY,
1283         false,
1284         parseResult,
1285         ArrayType::OBJECT);
1286     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1287         jsonObjectEnd,
1288         BUNDLE_MODULE_PROFILE_KEY_DEVICE_CAP_ABILITY,
1289         ability.deviceCapability,
1290         JsonType::ARRAY,
1291         false,
1292         parseResult,
1293         ArrayType::STRING);
1294     GetValueIfFindKey<MetaData>(jsonObject,
1295         jsonObjectEnd,
1296         BUNDLE_MODULE_PROFILE_KEY_META_DATA,
1297         ability.metaData,
1298         JsonType::OBJECT,
1299         false,
1300         parseResult,
1301         ArrayType::NOT_ARRAY);
1302     GetValueIfFindKey<bool>(jsonObject,
1303         jsonObjectEnd,
1304         BUNDLE_MODULE_PROFILE_KEY_FORM_ENABLED,
1305         ability.formEnabled,
1306         JsonType::BOOLEAN,
1307         false,
1308         parseResult,
1309         ArrayType::NOT_ARRAY);
1310     GetValueIfFindKey<Form>(jsonObject,
1311         jsonObjectEnd,
1312         BUNDLE_MODULE_PROFILE_KEY_FORM,
1313         ability.form,
1314         JsonType::OBJECT,
1315         false,
1316         parseResult,
1317         ArrayType::NOT_ARRAY);
1318     GetValueIfFindKey<std::string>(jsonObject,
1319         jsonObjectEnd,
1320         BUNDLE_MODULE_PROFILE_KEY_ORIENTATION,
1321         ability.orientation,
1322         JsonType::STRING,
1323         false,
1324         parseResult,
1325         ArrayType::NOT_ARRAY);
1326     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1327         jsonObjectEnd,
1328         BUNDLE_MODULE_PROFILE_KEY_BACKGROUND_MODES,
1329         ability.backgroundModes,
1330         JsonType::ARRAY,
1331         false,
1332         parseResult,
1333         ArrayType::STRING);
1334     GetValueIfFindKey<bool>(jsonObject,
1335         jsonObjectEnd,
1336         BUNDLE_MODULE_PROFILE_KEY_GRANT_PERMISSION,
1337         ability.grantPermission,
1338         JsonType::BOOLEAN,
1339         false,
1340         parseResult,
1341         ArrayType::NOT_ARRAY);
1342     GetValueIfFindKey<UriPermission>(jsonObject,
1343         jsonObjectEnd,
1344         BUNDLE_MODULE_PROFILE_KEY_URI_PERMISSION,
1345         ability.uriPermission,
1346         JsonType::OBJECT,
1347         false,
1348         parseResult,
1349         ArrayType::NOT_ARRAY);
1350     GetValueIfFindKey<std::string>(jsonObject,
1351         jsonObjectEnd,
1352         BUNDLE_MODULE_PROFILE_KEY_READ_PERMISSION,
1353         ability.readPermission,
1354         JsonType::STRING,
1355         false,
1356         parseResult,
1357         ArrayType::NOT_ARRAY);
1358     GetValueIfFindKey<std::string>(jsonObject,
1359         jsonObjectEnd,
1360         BUNDLE_MODULE_PROFILE_KEY_WRITE_PERMISSION,
1361         ability.writePermission,
1362         JsonType::STRING,
1363         false,
1364         parseResult,
1365         ArrayType::NOT_ARRAY);
1366     GetValueIfFindKey<bool>(jsonObject,
1367         jsonObjectEnd,
1368         BUNDLE_MODULE_PROFILE_KEY_DIRECT_LAUNCH,
1369         ability.directLaunch,
1370         JsonType::BOOLEAN,
1371         false,
1372         parseResult,
1373         ArrayType::NOT_ARRAY);
1374     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1375         jsonObjectEnd,
1376         BUNDLE_MODULE_PROFILE_KEY_CONFIG_CHANGES,
1377         ability.configChanges,
1378         JsonType::ARRAY,
1379         false,
1380         parseResult,
1381         ArrayType::STRING);
1382     GetValueIfFindKey<std::string>(jsonObject,
1383         jsonObjectEnd,
1384         BUNDLE_MODULE_PROFILE_KEY_MISSION,
1385         ability.mission,
1386         JsonType::STRING,
1387         false,
1388         parseResult,
1389         ArrayType::NOT_ARRAY);
1390     GetValueIfFindKey<std::string>(jsonObject,
1391         jsonObjectEnd,
1392         BUNDLE_MODULE_PROFILE_KEY_TARGET_ABILITY,
1393         ability.targetAbility,
1394         JsonType::STRING,
1395         false,
1396         parseResult,
1397         ArrayType::NOT_ARRAY);
1398     GetValueIfFindKey<bool>(jsonObject,
1399         jsonObjectEnd,
1400         BUNDLE_MODULE_PROFILE_KEY_MULTIUSER_SHARED,
1401         ability.multiUserShared,
1402         JsonType::BOOLEAN,
1403         false,
1404         parseResult,
1405         ArrayType::NOT_ARRAY);
1406     GetValueIfFindKey<bool>(jsonObject,
1407         jsonObjectEnd,
1408         BUNDLE_MODULE_PROFILE_KEY_SUPPORT_PIP_MODE,
1409         ability.supportPipMode,
1410         JsonType::BOOLEAN,
1411         false,
1412         parseResult,
1413         ArrayType::NOT_ARRAY);
1414     GetValueIfFindKey<bool>(jsonObject,
1415         jsonObjectEnd,
1416         BUNDLE_MODULE_PROFILE_KEY_FORMS_ENABLED,
1417         ability.formsEnabled,
1418         JsonType::BOOLEAN,
1419         false,
1420         parseResult,
1421         ArrayType::NOT_ARRAY);
1422     GetValueIfFindKey<std::vector<Forms>>(jsonObject,
1423         jsonObjectEnd,
1424         BUNDLE_MODULE_PROFILE_KEY_FORMS,
1425         ability.formses,
1426         JsonType::ARRAY,
1427         false,
1428         parseResult,
1429         ArrayType::OBJECT);
1430     GetValueIfFindKey<std::string>(jsonObject,
1431         jsonObjectEnd,
1432         BUNDLE_MODULE_PROFILE_KEY_START_WINDOW_ICON,
1433         ability.startWindowIcon,
1434         JsonType::STRING,
1435         false,
1436         parseResult,
1437         ArrayType::NOT_ARRAY);
1438     GetValueIfFindKey<int32_t>(jsonObject,
1439         jsonObjectEnd,
1440         BUNDLE_MODULE_PROFILE_KEY_START_WINDOW_ICON_ID,
1441         ability.startWindowIconId,
1442         JsonType::NUMBER,
1443         false,
1444         parseResult,
1445         ArrayType::NOT_ARRAY);
1446     GetValueIfFindKey<std::string>(jsonObject,
1447         jsonObjectEnd,
1448         BUNDLE_MODULE_PROFILE_KEY_START_WINDOW_BACKGROUND,
1449         ability.startWindowBackground,
1450         JsonType::STRING,
1451         false,
1452         parseResult,
1453         ArrayType::NOT_ARRAY);
1454     GetValueIfFindKey<int32_t>(jsonObject,
1455         jsonObjectEnd,
1456         BUNDLE_MODULE_PROFILE_KEY_START_WINDOW_BACKGROUND_ID,
1457         ability.startWindowBackgroundId,
1458         JsonType::NUMBER,
1459         false,
1460         parseResult,
1461         ArrayType::NOT_ARRAY);
1462     GetValueIfFindKey<bool>(jsonObject,
1463         jsonObjectEnd,
1464         BUNDLE_MODULE_PROFILE_KEY_REMOVE_MISSION_AFTER_TERMINATE,
1465         ability.removeMissionAfterTerminate,
1466         JsonType::BOOLEAN,
1467         false,
1468         parseResult,
1469         ArrayType::NOT_ARRAY);
1470 }
1471 
from_json(const nlohmann::json & jsonObject,Js & js)1472 void from_json(const nlohmann::json &jsonObject, Js &js)
1473 {
1474     // these are required fields.
1475     const auto &jsonObjectEnd = jsonObject.end();
1476     GetValueIfFindKey<std::string>(jsonObject,
1477         jsonObjectEnd,
1478         PROFILE_KEY_NAME,
1479         js.name,
1480         JsonType::STRING,
1481         true,
1482         parseResult,
1483         ArrayType::NOT_ARRAY);
1484     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1485         jsonObjectEnd,
1486         BUNDLE_MODULE_PROFILE_KEY_PAGES,
1487         js.pages,
1488         JsonType::ARRAY,
1489         true,
1490         parseResult,
1491         ArrayType::STRING);
1492     // these are not required fields.
1493     GetValueIfFindKey<Window>(jsonObject,
1494         jsonObjectEnd,
1495         BUNDLE_MODULE_PROFILE_KEY_WINDOW,
1496         js.window,
1497         JsonType::OBJECT,
1498         false,
1499         parseResult,
1500         ArrayType::NOT_ARRAY);
1501     GetValueIfFindKey<std::string>(jsonObject,
1502         jsonObjectEnd,
1503         PROFILE_KEY_TYPE,
1504         js.type,
1505         JsonType::STRING,
1506         false,
1507         parseResult,
1508         ArrayType::NOT_ARRAY);
1509 }
1510 
from_json(const nlohmann::json & jsonObject,Intent & intents)1511 void from_json(const nlohmann::json &jsonObject, Intent &intents)
1512 {
1513     // these are not required fields.
1514     const auto &jsonObjectEnd = jsonObject.end();
1515     GetValueIfFindKey<std::string>(jsonObject,
1516         jsonObjectEnd,
1517         BUNDLE_MODULE_PROFILE_KEY_TARGET_CLASS,
1518         intents.targetClass,
1519         JsonType::STRING,
1520         false,
1521         parseResult,
1522         ArrayType::NOT_ARRAY);
1523     GetValueIfFindKey<std::string>(jsonObject,
1524         jsonObjectEnd,
1525         BUNDLE_MODULE_PROFILE_KEY_TARGET_BUNDLE,
1526         intents.targetBundle,
1527         JsonType::STRING,
1528         false,
1529         parseResult,
1530         ArrayType::NOT_ARRAY);
1531 }
1532 
from_json(const nlohmann::json & jsonObject,CommonEvent & commonEvent)1533 void from_json(const nlohmann::json &jsonObject, CommonEvent &commonEvent)
1534 {
1535     // these are required fields.
1536     const auto &jsonObjectEnd = jsonObject.end();
1537     GetValueIfFindKey<std::string>(jsonObject,
1538         jsonObjectEnd,
1539         PROFILE_KEY_NAME,
1540         commonEvent.name,
1541         JsonType::STRING,
1542         true,
1543         parseResult,
1544         ArrayType::NOT_ARRAY);
1545     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1546         jsonObjectEnd,
1547         BUNDLE_MODULE_PROFILE_KEY_EVENTS,
1548         commonEvent.events,
1549         JsonType::ARRAY,
1550         true,
1551         parseResult,
1552         ArrayType::STRING);
1553     // these are not required fields.
1554     GetValueIfFindKey<std::string>(jsonObject,
1555         jsonObjectEnd,
1556         BUNDLE_MODULE_PROFILE_KEY_PERMISSION,
1557         commonEvent.permission,
1558         JsonType::STRING,
1559         false,
1560         parseResult,
1561         ArrayType::NOT_ARRAY);
1562     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1563         jsonObjectEnd,
1564         BUNDLE_MODULE_PROFILE_KEY_DATA,
1565         commonEvent.data,
1566         JsonType::ARRAY,
1567         false,
1568         parseResult,
1569         ArrayType::STRING);
1570     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1571         jsonObjectEnd,
1572         BUNDLE_MODULE_PROFILE_KEY_TYPE,
1573         commonEvent.type,
1574         JsonType::ARRAY,
1575         false,
1576         parseResult,
1577         ArrayType::STRING);
1578 }
1579 
from_json(const nlohmann::json & jsonObject,Shortcut & shortcut)1580 void from_json(const nlohmann::json &jsonObject, Shortcut &shortcut)
1581 {
1582     // these are required fields.
1583     const auto &jsonObjectEnd = jsonObject.end();
1584     GetValueIfFindKey<std::string>(jsonObject,
1585         jsonObjectEnd,
1586         BUNDLE_MODULE_PROFILE_KEY_SHORTCUT_ID,
1587         shortcut.shortcutId,
1588         JsonType::STRING,
1589         true,
1590         parseResult,
1591         ArrayType::NOT_ARRAY);
1592     // these are not required fields.
1593     GetValueIfFindKey<std::string>(jsonObject,
1594         jsonObjectEnd,
1595         PROFILE_KEY_LABEL,
1596         shortcut.label,
1597         JsonType::STRING,
1598         false,
1599         parseResult,
1600         ArrayType::NOT_ARRAY);
1601     GetValueIfFindKey<std::string>(jsonObject,
1602         jsonObjectEnd,
1603         BUNDLE_MODULE_PROFILE_KEY_ICON,
1604         shortcut.icon,
1605         JsonType::STRING,
1606         false,
1607         parseResult,
1608         ArrayType::NOT_ARRAY);
1609     GetValueIfFindKey<std::vector<Intent>>(jsonObject,
1610         jsonObjectEnd,
1611         BUNDLE_MODULE_PROFILE_KEY_SHORTCUT_WANTS,
1612         shortcut.intents,
1613         JsonType::ARRAY,
1614         false,
1615         parseResult,
1616         ArrayType::OBJECT);
1617     // get label id
1618     GetValueIfFindKey<int32_t>(jsonObject,
1619          jsonObjectEnd,
1620          PROFILE_KEY_LABEL_ID,
1621          shortcut.labelId,
1622          JsonType::NUMBER,
1623          false,
1624          parseResult,
1625          ArrayType::NOT_ARRAY);
1626     // get icon id
1627     GetValueIfFindKey<int32_t>(jsonObject,
1628          jsonObjectEnd,
1629          BUNDLE_MODULE_PROFILE_KEY_ICON_ID,
1630          shortcut.iconId,
1631          JsonType::NUMBER,
1632          false,
1633          parseResult,
1634          ArrayType::NOT_ARRAY);
1635 }
1636 
from_json(const nlohmann::json & jsonObject,Module & module)1637 void from_json(const nlohmann::json &jsonObject, Module &module)
1638 {
1639     // these are required fields.
1640     const auto &jsonObjectEnd = jsonObject.end();
1641     GetValueIfFindKey<std::string>(jsonObject,
1642         jsonObjectEnd,
1643         BUNDLE_MODULE_PROFILE_KEY_PACKAGE,
1644         module.package,
1645         JsonType::STRING,
1646         false,
1647         parseResult,
1648         ArrayType::NOT_ARRAY);
1649     GetValueIfFindKey<std::string>(jsonObject,
1650         jsonObjectEnd,
1651         PROFILE_KEY_NAME,
1652         module.name,
1653         JsonType::STRING,
1654         false,
1655         parseResult,
1656         ArrayType::NOT_ARRAY);
1657     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1658         jsonObjectEnd,
1659         BUNDLE_MODULE_PROFILE_KEY_DEVICE_TYPE,
1660         module.deviceType,
1661         JsonType::ARRAY,
1662         true,
1663         parseResult,
1664         ArrayType::STRING);
1665     GetValueIfFindKey<std::string>(jsonObject,
1666         jsonObjectEnd,
1667         BUNDLE_MODULE_PROFILE_KEY_COLOR_MODE,
1668         module.colorMode,
1669         JsonType::STRING,
1670         false,
1671         parseResult,
1672         ArrayType::NOT_ARRAY);
1673     GetValueIfFindKey<Distro>(jsonObject,
1674         jsonObjectEnd,
1675         BUNDLE_MODULE_PROFILE_KEY_DISTRO,
1676         module.distro,
1677         JsonType::OBJECT,
1678         false,
1679         parseResult,
1680         ArrayType::NOT_ARRAY);
1681     // these are not required fields.
1682     GetValueIfFindKey<std::string>(jsonObject,
1683         jsonObjectEnd,
1684         PROFILE_KEY_DESCRIPTION,
1685         module.description,
1686         JsonType::STRING,
1687         false,
1688         parseResult,
1689         ArrayType::NOT_ARRAY);
1690     GetValueIfFindKey<int32_t>(jsonObject,
1691         jsonObjectEnd,
1692         PROFILE_KEY_DESCRIPTION_ID,
1693         module.descriptionId,
1694         JsonType::NUMBER,
1695         false,
1696         parseResult,
1697         ArrayType::NOT_ARRAY);
1698     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1699         jsonObjectEnd,
1700         BUNDLE_MODULE_PROFILE_KEY_SUPPORTED_MODES,
1701         module.supportedModes,
1702         JsonType::ARRAY,
1703         false,
1704         parseResult,
1705         ArrayType::STRING);
1706     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
1707         jsonObjectEnd,
1708         BUNDLE_MODULE_PROFILE_KEY_REQ_CAPABILITIES,
1709         module.reqCapabilities,
1710         JsonType::ARRAY,
1711         false,
1712         parseResult,
1713         ArrayType::STRING);
1714     GetValueIfFindKey<MetaData>(jsonObject,
1715         jsonObjectEnd,
1716         BUNDLE_MODULE_PROFILE_KEY_META_DATA,
1717         module.metaData,
1718         JsonType::OBJECT,
1719         false,
1720         parseResult,
1721         ArrayType::NOT_ARRAY);
1722     GetValueIfFindKey<std::vector<Ability>>(jsonObject,
1723         jsonObjectEnd,
1724         BUNDLE_MODULE_PROFILE_KEY_ABILITIES,
1725         module.abilities,
1726         JsonType::ARRAY,
1727         false,
1728         parseResult,
1729         ArrayType::OBJECT);
1730     GetValueIfFindKey<std::vector<Js>>(jsonObject,
1731         jsonObjectEnd,
1732         BUNDLE_MODULE_PROFILE_KEY_JS,
1733         module.jses,
1734         JsonType::ARRAY,
1735         false,
1736         parseResult,
1737         ArrayType::OBJECT);
1738     GetValueIfFindKey<std::vector<CommonEvent>>(jsonObject,
1739         jsonObjectEnd,
1740         BUNDLE_MODULE_PROFILE_KEY_COMMON_EVENTS,
1741         module.commonEvents,
1742         JsonType::ARRAY,
1743         false,
1744         parseResult,
1745         ArrayType::OBJECT);
1746     GetValueIfFindKey<std::vector<Shortcut>>(jsonObject,
1747         jsonObjectEnd,
1748         BUNDLE_MODULE_PROFILE_KEY_SHORTCUTS,
1749         module.shortcuts,
1750         JsonType::ARRAY,
1751         false,
1752         parseResult,
1753         ArrayType::OBJECT);
1754     GetValueIfFindKey<std::vector<RequestPermission>>(jsonObject,
1755         jsonObjectEnd,
1756         BUNDLE_MODULE_PROFILE_KEY_REQ_PERMISSIONS,
1757         module.requestPermissions,
1758         JsonType::ARRAY,
1759         false,
1760         parseResult,
1761         ArrayType::OBJECT);
1762     GetValueIfFindKey<std::vector<DefinePermission>>(jsonObject,
1763         jsonObjectEnd,
1764         BUNDLE_MODULE_PROFILE_KEY_DEFINE_PERMISSIONS,
1765         module.definePermissions,
1766         JsonType::ARRAY,
1767         false,
1768         parseResult,
1769         ArrayType::OBJECT);
1770     GetValueIfFindKey<std::string>(jsonObject,
1771         jsonObjectEnd,
1772         BUNDLE_MODULE_PROFILE_KEY_MAIN_ABILITY,
1773         module.mainAbility,
1774         JsonType::STRING,
1775         false,
1776         parseResult,
1777         ArrayType::NOT_ARRAY);
1778     GetValueIfFindKey<std::string>(jsonObject,
1779         jsonObjectEnd,
1780         BUNDLE_MODULE_PROFILE_KEY_SRC_PATH,
1781         module.srcPath,
1782         JsonType::STRING,
1783         false,
1784         parseResult,
1785         ArrayType::NOT_ARRAY);
1786     GetValueIfFindKey<std::vector<Dependency>>(jsonObject,
1787         jsonObjectEnd,
1788         BUNDLE_MODULE_DEPENDENCIES,
1789         module.dependencies,
1790         JsonType::ARRAY,
1791         false,
1792         parseResult,
1793         ArrayType::OBJECT);
1794     GetValueIfFindKey<bool>(jsonObject,
1795         jsonObjectEnd,
1796         BUNDLE_MODULE_PROFILE_KEY_IS_LIB_ISOLATED,
1797         module.isLibIsolated,
1798         JsonType::BOOLEAN,
1799         false,
1800         parseResult,
1801         ArrayType::NOT_ARRAY);
1802 }
1803 
from_json(const nlohmann::json & jsonObject,ConfigJson & configJson)1804 void from_json(const nlohmann::json &jsonObject, ConfigJson &configJson)
1805 {
1806     // Because it does not support exceptions, every element needs to be searched first
1807     APP_LOGI("read 'App' tag from config.json");
1808     const auto &jsonObjectEnd = jsonObject.end();
1809     GetValueIfFindKey<App>(jsonObject,
1810         jsonObjectEnd,
1811         BUNDLE_PROFILE_KEY_APP,
1812         configJson.app,
1813         JsonType::OBJECT,
1814         true,
1815         parseResult,
1816         ArrayType::NOT_ARRAY);
1817     APP_LOGI("read 'DeviceConfig' tag from config.json");
1818     GetValueIfFindKey<DeviceConfig>(jsonObject,
1819         jsonObjectEnd,
1820         BUNDLE_PROFILE_KEY_DEVICE_CONFIG,
1821         configJson.deveicConfig,
1822         JsonType::OBJECT,
1823         true,
1824         parseResult,
1825         ArrayType::NOT_ARRAY);
1826     APP_LOGI("read 'Module' tag from config.json");
1827     GetValueIfFindKey<Module>(jsonObject,
1828         jsonObjectEnd,
1829         BUNDLE_PROFILE_KEY_MODULE,
1830         configJson.module,
1831         JsonType::OBJECT,
1832         true,
1833         parseResult,
1834         ArrayType::NOT_ARRAY);
1835     APP_LOGI("read tag from config.json");
1836 }
1837 
1838 }  // namespace ProfileReader
1839 
1840 namespace {
1841 struct TransformParam {
1842     bool isSystemApp = false;
1843     bool isPreInstallApp = false;
1844 };
1845 
CheckBundleNameIsValid(const std::string & bundleName)1846 bool CheckBundleNameIsValid(const std::string &bundleName)
1847 {
1848     if (bundleName.empty()) {
1849         return false;
1850     }
1851     if (bundleName.size() < Constants::MIN_BUNDLE_NAME || bundleName.size() > Constants::MAX_BUNDLE_NAME) {
1852         return false;
1853     }
1854     char head = bundleName.at(0);
1855     if (head < 'A' || ('Z' < head && head < 'a') || head > 'z') {
1856         return false;
1857     }
1858     for (const auto &c : bundleName) {
1859         if (c < '.' || c == '/' || ('9' < c && c < 'A') || ('Z' < c && c < '_') || c == '`' || c > 'z') {
1860             return false;
1861         }
1862     }
1863     return true;
1864 }
1865 
CheckModuleNameIsValid(const std::string & moduleName)1866 bool CheckModuleNameIsValid(const std::string &moduleName)
1867 {
1868     if (moduleName.empty()) {
1869         return false;
1870     }
1871     if (moduleName.find(Constants::RELATIVE_PATH) != std::string::npos) {
1872         return false;
1873     }
1874     return true;
1875 }
1876 
CheckModuleInfosIsValid(ProfileReader::ConfigJson & configJson)1877 bool CheckModuleInfosIsValid(ProfileReader::ConfigJson &configJson)
1878 {
1879     if (configJson.module.deviceType.empty()) {
1880         APP_LOGE("module deviceType invalid");
1881         return false;
1882     }
1883     if (!configJson.module.abilities.empty()) {
1884         for (const auto &ability : configJson.module.abilities) {
1885             if (ability.name.empty() || ability.type.empty()) {
1886                 APP_LOGE("ability name or type invalid");
1887                 return false;
1888             }
1889         }
1890     }
1891     if (configJson.app.version.code <= 0) {
1892         APP_LOGE("version code invalid");
1893         return false;
1894     }
1895     auto iter =
1896         std::find_if(configJson.module.deviceType.begin(), configJson.module.deviceType.end(), [](const auto &d) {
1897             return ((d.compare(ProfileReader::BUNDLE_DEVICE_CONFIG_PROFILE_KEY_LITE_WEARABLE) == 0 ||
1898                      d.compare(ProfileReader::BUNDLE_DEVICE_CONFIG_PROFILE_KEY_SMART_VISION) == 0));
1899         });
1900     if (iter != configJson.module.deviceType.end()) {
1901         APP_LOGE("this is a lite device app, ignores other check");
1902         // if lite device hap doesn't have a module package name, assign it as bundle name.
1903         if (configJson.module.package.empty()) {
1904             configJson.module.package = configJson.app.bundleName;
1905         }
1906         return true;
1907     }
1908     if (!CheckModuleNameIsValid(configJson.module.package)) {
1909         APP_LOGE("module package invalid");
1910         return false;
1911     }
1912     if (!CheckModuleNameIsValid(configJson.module.distro.moduleName)) {
1913         APP_LOGE("module distro invalid");
1914         return false;
1915     }
1916     return true;
1917 }
GetFormEntity(const std::vector<std::string> & formEntity)1918 uint32_t GetFormEntity(const std::vector<std::string> &formEntity)
1919 {
1920     if (ProfileReader::formEntityMap.empty()) {
1921         ProfileReader::formEntityMap.insert({ProfileReader::KEY_HOME_SCREEN, ProfileReader::VALUE_HOME_SCREEN});
1922         ProfileReader::formEntityMap.insert({ProfileReader::KEY_SEARCHBOX, ProfileReader::VALUE_SEARCHBOX});
1923     }
1924 
1925     uint32_t formEntityInBinary = 0;
1926     for (const auto &item : formEntity) {
1927         if (ProfileReader::formEntityMap.find(item) != ProfileReader::formEntityMap.end()) {
1928             formEntityInBinary |= ProfileReader::formEntityMap[item];
1929         }
1930     }
1931     return formEntityInBinary;
1932 }
1933 
ConvertFormInfo(FormInfo & formInfo,const ProfileReader::Forms & form)1934 bool ConvertFormInfo(FormInfo &formInfo, const ProfileReader::Forms &form)
1935 {
1936     formInfo.name = form.name;
1937     formInfo.description = form.description;
1938     formInfo.descriptionId = form.descriptionId;
1939     formInfo.formConfigAbility = form.formConfigAbility;
1940     formInfo.formVisibleNotify = form.formVisibleNotify;
1941     formInfo.deepLink = form.deepLink;
1942     formInfo.defaultFlag = form.isDefault;
1943     auto type = std::find_if(std::begin(ProfileReader::formTypeMap),
1944         std::end(ProfileReader::formTypeMap),
1945         [&form](const auto &item) { return item.first == form.type; });
1946     if (type != ProfileReader::formTypeMap.end()) {
1947         formInfo.type = type->second;
1948     }
1949     auto colorMode = std::find_if(std::begin(ProfileReader::formColorModeMap),
1950         std::end(ProfileReader::formColorModeMap),
1951         [&form](const auto &item) { return item.first == form.colorMode; });
1952     if (colorMode != ProfileReader::formColorModeMap.end()) {
1953         formInfo.colorMode = colorMode->second;
1954     }
1955     formInfo.updateEnabled = form.updateEnabled;
1956     formInfo.scheduledUpdateTime = form.scheduledUpdateTime;
1957     formInfo.updateDuration = form.updateDuration;
1958     formInfo.jsComponentName = form.jsComponentName;
1959     for (const auto &data : form.metaData.customizeData) {
1960         FormCustomizeData customizeData;
1961         customizeData.name = data.name;
1962         customizeData.value = data.value;
1963         formInfo.customizeDatas.emplace_back(customizeData);
1964     }
1965     for (const auto &dimensions : form.supportDimensions) {
1966         auto dimension = std::find_if(std::begin(ProfileReader::dimensionMap),
1967             std::end(ProfileReader::dimensionMap),
1968             [&dimensions](const auto &item) { return item.first == dimensions; });
1969         if (dimension != ProfileReader::dimensionMap.end()) {
1970             formInfo.supportDimensions.emplace_back(dimension->second);
1971         }
1972     }
1973     auto dimension = std::find_if(std::begin(ProfileReader::dimensionMap),
1974         std::end(ProfileReader::dimensionMap),
1975         [&form](const auto &item) { return item.first == form.defaultDimension; });
1976     if (dimension != ProfileReader::dimensionMap.end()) {
1977         formInfo.defaultDimension = dimension->second;
1978     }
1979     formInfo.landscapeLayouts = form.landscapeLayouts;
1980     formInfo.portraitLayouts = form.portraitLayouts;
1981     formInfo.src = form.src;
1982     formInfo.window.autoDesignWidth = form.window.autoDesignWidth;
1983     formInfo.window.designWidth = form.window.designWidth;
1984     return true;
1985 }
1986 
UpdateNativeSoAttrs(const std::string & cpuAbi,const std::string & soRelativePath,bool isLibIsolated,InnerBundleInfo & innerBundleInfo)1987 void UpdateNativeSoAttrs(
1988     const std::string &cpuAbi,
1989     const std::string &soRelativePath,
1990     bool isLibIsolated,
1991     InnerBundleInfo &innerBundleInfo)
1992 {
1993     APP_LOGD("cpuAbi %{public}s, soRelativePath : %{public}s, isLibIsolated : %{public}d",
1994         cpuAbi.c_str(), soRelativePath.c_str(), isLibIsolated);
1995     innerBundleInfo.SetCpuAbi(cpuAbi);
1996     if (!isLibIsolated) {
1997         innerBundleInfo.SetNativeLibraryPath(soRelativePath);
1998         return;
1999     }
2000 
2001     innerBundleInfo.SetModuleNativeLibraryPath(
2002         innerBundleInfo.GetCurModuleName() + Constants::PATH_SEPARATOR + soRelativePath);
2003     innerBundleInfo.SetModuleCpuAbi(cpuAbi);
2004 }
2005 
ParserNativeSo(const ProfileReader::ConfigJson & configJson,const BundleExtractor & bundleExtractor,InnerBundleInfo & innerBundleInfo)2006 bool ParserNativeSo(
2007     const ProfileReader::ConfigJson &configJson,
2008     const BundleExtractor &bundleExtractor,
2009     InnerBundleInfo &innerBundleInfo)
2010 {
2011     std::string abis = GetAbiList();
2012     std::vector<std::string> abiList;
2013     SplitStr(abis, Constants::ABI_SEPARATOR, abiList, false, false);
2014     if (abiList.empty()) {
2015         APP_LOGD("Abi is empty");
2016         return false;
2017     }
2018 
2019     bool isDefault =
2020         std::find(abiList.begin(), abiList.end(), Constants::ABI_DEFAULT) != abiList.end();
2021     bool isSystemLib64Exist = BundleUtil::IsExistDir(Constants::SYSTEM_LIB64);
2022     APP_LOGD("abi list : %{public}s, isDefault : %{public}d", abis.c_str(), isDefault);
2023     std::string cpuAbi;
2024     std::string soRelativePath;
2025     bool soExist = bundleExtractor.IsDirExist(Constants::LIBS);
2026     if (!soExist) {
2027         APP_LOGD("so not exist");
2028         if (isDefault) {
2029             cpuAbi = isSystemLib64Exist ? Constants::ARM64_V8A : Constants::ARM_EABI_V7A;
2030             UpdateNativeSoAttrs(cpuAbi, soRelativePath, false, innerBundleInfo);
2031             return true;
2032         }
2033 
2034         for (const auto &abi : abiList) {
2035             if (Constants::ABI_MAP.find(abi) != Constants::ABI_MAP.end()) {
2036                 cpuAbi = abi;
2037                 UpdateNativeSoAttrs(cpuAbi, soRelativePath, false, innerBundleInfo);
2038                 return true;
2039             }
2040         }
2041 
2042         return false;
2043     }
2044 
2045     APP_LOGD("so exist");
2046     bool isLibIsolated = configJson.module.isLibIsolated;
2047     if (isDefault) {
2048         if (isSystemLib64Exist) {
2049             if (bundleExtractor.IsDirExist(Constants::LIBS + Constants::ARM64_V8A)) {
2050                 cpuAbi = Constants::ARM64_V8A;
2051                 soRelativePath = Constants::LIBS + Constants::ABI_MAP.at(Constants::ARM64_V8A);
2052                 UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
2053                 return true;
2054             }
2055 
2056             return false;
2057         }
2058 
2059         if (bundleExtractor.IsDirExist(Constants::LIBS + Constants::ARM_EABI_V7A)) {
2060             cpuAbi = Constants::ARM_EABI_V7A;
2061             soRelativePath = Constants::LIBS + Constants::ABI_MAP.at(Constants::ARM_EABI_V7A);
2062             UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
2063             return true;
2064         }
2065 
2066         if (bundleExtractor.IsDirExist(Constants::LIBS + Constants::ARM_EABI)) {
2067             cpuAbi = Constants::ARM_EABI;
2068             soRelativePath = Constants::LIBS + Constants::ABI_MAP.at(Constants::ARM_EABI);
2069             UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
2070             return true;
2071         }
2072 
2073         return false;
2074     }
2075 
2076     for (const auto &abi : abiList) {
2077         std::string libsPath;
2078         libsPath.append(Constants::LIBS).append(abi).append(Constants::PATH_SEPARATOR);
2079         if (Constants::ABI_MAP.find(abi) != Constants::ABI_MAP.end() && bundleExtractor.IsDirExist(libsPath)) {
2080             cpuAbi = abi;
2081             soRelativePath = Constants::LIBS + Constants::ABI_MAP.at(abi);
2082             UpdateNativeSoAttrs(cpuAbi, soRelativePath, isLibIsolated, innerBundleInfo);
2083             return true;
2084         }
2085     }
2086 
2087     return false;
2088 }
2089 
ToApplicationInfo(const ProfileReader::ConfigJson & configJson,const BundleExtractor & bundleExtractor,const TransformParam & transformParam,ApplicationInfo & applicationInfo)2090 bool ToApplicationInfo(
2091     const ProfileReader::ConfigJson &configJson,
2092     const BundleExtractor &bundleExtractor,
2093     const TransformParam &transformParam,
2094     ApplicationInfo &applicationInfo)
2095 {
2096     APP_LOGD("transform ConfigJson to ApplicationInfo");
2097     applicationInfo.name = configJson.app.bundleName;
2098     applicationInfo.bundleName = configJson.app.bundleName;
2099 
2100     applicationInfo.versionCode = static_cast<uint32_t>(configJson.app.version.code);
2101     applicationInfo.versionName = configJson.app.version.name;
2102     if (configJson.app.version.minCompatibleVersionCode != -1) {
2103         applicationInfo.minCompatibleVersionCode = configJson.app.version.minCompatibleVersionCode;
2104     } else {
2105         applicationInfo.minCompatibleVersionCode = static_cast<int32_t>(applicationInfo.versionCode);
2106     }
2107 
2108     applicationInfo.apiCompatibleVersion = configJson.app.apiVersion.compatible;
2109     applicationInfo.apiTargetVersion = configJson.app.apiVersion.target;
2110     applicationInfo.apiReleaseType = configJson.app.apiVersion.releaseType;
2111     applicationInfo.asanEnabled = configJson.app.asanEnabled;
2112 
2113     // if there is main ability, it's icon label description will be set to applicationInfo.
2114 
2115     if (transformParam.isSystemApp && transformParam.isPreInstallApp) {
2116         applicationInfo.keepAlive = configJson.deveicConfig.defaultDevice.keepAlive;
2117         applicationInfo.singleton = configJson.app.singleton;
2118         applicationInfo.userDataClearable = configJson.app.userDataClearable;
2119         if (configJson.app.removable.first) {
2120             applicationInfo.removable = configJson.app.removable.second;
2121         } else {
2122             applicationInfo.removable = false;
2123         }
2124     }
2125 
2126     applicationInfo.debug = configJson.deveicConfig.defaultDevice.debug;
2127     applicationInfo.deviceId = Constants::CURRENT_DEVICE_ID;
2128     applicationInfo.distributedNotificationEnabled = true;
2129     applicationInfo.entityType = Profile::APP_ENTITY_TYPE_DEFAULT_VALUE;
2130     applicationInfo.process = configJson.deveicConfig.defaultDevice.process;
2131 
2132     auto it = find(configJson.module.supportedModes.begin(),
2133         configJson.module.supportedModes.end(),
2134         ProfileReader::MODULE_SUPPORTED_MODES_VALUE_DRIVE);
2135     if (it != configJson.module.supportedModes.end()) {
2136         applicationInfo.supportedModes = 1;
2137     } else {
2138         applicationInfo.supportedModes = 0;
2139     }
2140     applicationInfo.vendor = configJson.app.vendor;
2141 
2142     // for SystemResource
2143     applicationInfo.iconId = configJson.app.iconId;
2144     applicationInfo.labelId = configJson.app.labelId;
2145     applicationInfo.iconResource = BundleUtil::GetResource(
2146         configJson.app.bundleName, configJson.module.distro.moduleName, configJson.app.iconId);
2147     applicationInfo.labelResource = BundleUtil::GetResource(
2148         configJson.app.bundleName, configJson.module.distro.moduleName, configJson.app.labelId);
2149 
2150     applicationInfo.enabled = true;
2151     for (const auto &targetBundle : configJson.app.targetBundleList) {
2152         APP_LOGD("targetBundle = %{public}s", targetBundle.c_str());
2153         applicationInfo.targetBundleList.emplace_back(targetBundle);
2154     }
2155 
2156     if (configJson.module.distro.moduleType.compare(ProfileReader::MODULE_DISTRO_MODULE_TYPE_VALUE_ENTRY) == 0) {
2157         applicationInfo.description = configJson.module.description;
2158         applicationInfo.descriptionId = configJson.module.descriptionId;
2159         applicationInfo.descriptionResource = BundleUtil::GetResource(
2160             configJson.app.bundleName, configJson.module.distro.moduleName, configJson.module.descriptionId);
2161     }
2162     return true;
2163 }
2164 
ToBundleInfo(const ProfileReader::ConfigJson & configJson,const ApplicationInfo & applicationInfo,const InnerModuleInfo & innerModuleInfo,const TransformParam & transformParam,BundleInfo & bundleInfo)2165 bool ToBundleInfo(
2166     const ProfileReader::ConfigJson &configJson,
2167     const ApplicationInfo &applicationInfo,
2168     const InnerModuleInfo &innerModuleInfo,
2169     const TransformParam &transformParam,
2170     BundleInfo &bundleInfo)
2171 {
2172     bundleInfo.name = applicationInfo.bundleName;
2173 
2174     bundleInfo.versionCode = static_cast<uint32_t>(applicationInfo.versionCode);
2175     bundleInfo.versionName = applicationInfo.versionName;
2176     bundleInfo.minCompatibleVersionCode = static_cast<uint32_t>(applicationInfo.minCompatibleVersionCode);
2177 
2178     bundleInfo.compatibleVersion = static_cast<uint32_t>(applicationInfo.apiCompatibleVersion);
2179     bundleInfo.targetVersion = static_cast<uint32_t>(applicationInfo.apiTargetVersion);
2180 
2181     bundleInfo.isKeepAlive = applicationInfo.keepAlive;
2182     bundleInfo.singleton = applicationInfo.singleton;
2183     bundleInfo.asanEnabled = applicationInfo.asanEnabled;
2184     bundleInfo.isPreInstallApp = transformParam.isPreInstallApp;
2185 
2186     bundleInfo.vendor = applicationInfo.vendor;
2187     bundleInfo.releaseType = applicationInfo.apiReleaseType;
2188     if (configJson.module.jses.empty()) {
2189         bundleInfo.isNativeApp = true;
2190     }
2191 
2192     if (innerModuleInfo.isEntry) {
2193         bundleInfo.mainEntry = innerModuleInfo.modulePackage;
2194         bundleInfo.entryModuleName = innerModuleInfo.moduleName;
2195     }
2196 
2197     return true;
2198 }
2199 
GetMetaData(MetaData & metaData,const ProfileReader::MetaData & profileMetaData)2200 void GetMetaData(MetaData &metaData, const ProfileReader::MetaData &profileMetaData)
2201 {
2202     for (const auto &item : profileMetaData.customizeData) {
2203         CustomizeData customizeData;
2204         customizeData.name = item.name;
2205         customizeData.extra = item.extra;
2206         customizeData.value = item.value;
2207         metaData.customizeData.emplace_back(customizeData);
2208     }
2209 }
2210 
GetBackgroundModes(const std::vector<std::string> & backgroundModes)2211 uint32_t GetBackgroundModes(const std::vector<std::string>& backgroundModes)
2212 {
2213     uint32_t backgroundMode = 0;
2214     for (const auto& item : backgroundModes) {
2215         if (ProfileReader::backgroundModeMap.find(item) != ProfileReader::backgroundModeMap.end()) {
2216             backgroundMode |= ProfileReader::backgroundModeMap[item];
2217         }
2218     }
2219     return backgroundMode;
2220 }
2221 
ToInnerModuleInfo(const ProfileReader::ConfigJson & configJson,InnerModuleInfo & innerModuleInfo)2222 bool ToInnerModuleInfo(const ProfileReader::ConfigJson &configJson, InnerModuleInfo &innerModuleInfo)
2223 {
2224     if (configJson.module.name.substr(0, 1) == ".") {
2225         innerModuleInfo.name = configJson.module.package + configJson.module.name;
2226     } else {
2227         innerModuleInfo.name = configJson.module.name;
2228     }
2229     innerModuleInfo.modulePackage = configJson.module.package;
2230     innerModuleInfo.moduleName = configJson.module.distro.moduleName;
2231     innerModuleInfo.installationFree = configJson.module.distro.installationFree;
2232     innerModuleInfo.description = configJson.module.description;
2233     innerModuleInfo.descriptionId = configJson.module.descriptionId;
2234     auto colorModeInfo = std::find_if(std::begin(ProfileReader::moduleColorMode),
2235         std::end(ProfileReader::moduleColorMode),
2236         [&configJson](const auto &item) { return item.first == configJson.module.colorMode; });
2237     if (colorModeInfo != ProfileReader::moduleColorMode.end()) {
2238         innerModuleInfo.colorMode = colorModeInfo->second;
2239     }
2240     GetMetaData(innerModuleInfo.metaData, configJson.module.metaData);
2241     innerModuleInfo.distro = configJson.module.distro;
2242     innerModuleInfo.reqCapabilities = configJson.module.reqCapabilities;
2243     innerModuleInfo.requestPermissions = configJson.module.requestPermissions;
2244     if (configJson.app.bundleName == Profile::SYSTEM_RESOURCES_APP) {
2245         innerModuleInfo.definePermissions = configJson.module.definePermissions;
2246     }
2247     if (configJson.module.mainAbility.substr(0, 1) == ".") {
2248         innerModuleInfo.mainAbility = configJson.module.package + configJson.module.mainAbility;
2249     } else {
2250         innerModuleInfo.mainAbility = configJson.module.mainAbility;
2251     }
2252     innerModuleInfo.srcPath = configJson.module.srcPath;
2253     std::string moduleType = innerModuleInfo.distro.moduleType;
2254     if (ProfileReader::MODULE_TYPE_SET.find(moduleType) != ProfileReader::MODULE_TYPE_SET.end()) {
2255         if (moduleType == ProfileReader::MODULE_DISTRO_MODULE_TYPE_VALUE_ENTRY) {
2256             innerModuleInfo.isEntry = true;
2257         }
2258     }
2259     innerModuleInfo.dependencies = configJson.module.dependencies;
2260 
2261     innerModuleInfo.isModuleJson = false;
2262     innerModuleInfo.isLibIsolated = configJson.module.isLibIsolated;
2263     innerModuleInfo.deviceTypes = configJson.module.deviceType;
2264     return true;
2265 }
2266 
ToAbilityInfo(const ProfileReader::ConfigJson & configJson,const ProfileReader::Ability & ability,const TransformParam & transformParam,AbilityInfo & abilityInfo)2267 bool ToAbilityInfo(
2268     const ProfileReader::ConfigJson &configJson,
2269     const ProfileReader::Ability &ability,
2270     const TransformParam &transformParam,
2271     AbilityInfo &abilityInfo)
2272 {
2273     abilityInfo.name = ability.name;
2274     if (ability.srcLanguage != "c++" && ability.name.substr(0, 1) == ".") {
2275         abilityInfo.name = configJson.module.package + ability.name;
2276     }
2277     abilityInfo.label = ability.label;
2278     abilityInfo.description = ability.description;
2279     abilityInfo.iconPath = ability.icon;
2280     abilityInfo.labelId = ability.labelId;
2281     abilityInfo.descriptionId = ability.descriptionId;
2282     abilityInfo.iconId = ability.iconId;
2283     abilityInfo.visible = ability.visible;
2284     abilityInfo.continuable = ability.continuable;
2285     abilityInfo.kind = ability.type;
2286     abilityInfo.srcPath = ability.srcPath;
2287     abilityInfo.srcLanguage = ability.srcLanguage;
2288     abilityInfo.priority = ability.priority;
2289 
2290     std::transform(
2291         abilityInfo.srcLanguage.begin(), abilityInfo.srcLanguage.end(), abilityInfo.srcLanguage.begin(), ::tolower);
2292     if (abilityInfo.srcLanguage != ProfileReader::BUNDLE_MODULE_PROFILE_KEY_JS &&
2293         abilityInfo.srcLanguage != ProfileReader::BUNDLE_MODULE_PROFILE_KEY_JS_TYPE_ETS) {
2294         abilityInfo.isNativeAbility = true;
2295     }
2296     auto iterType = std::find_if(std::begin(ProfileReader::ABILITY_TYPE_MAP),
2297         std::end(ProfileReader::ABILITY_TYPE_MAP),
2298         [&ability](const auto &item) { return item.first == ability.type; });
2299     if (iterType != ProfileReader::ABILITY_TYPE_MAP.end()) {
2300         abilityInfo.type = iterType->second;
2301     } else {
2302         APP_LOGE("ability type invalid.");
2303         return false;
2304     }
2305 
2306     auto iterOrientation = std::find_if(std::begin(ProfileReader::DISPLAY_ORIENTATION_MAP),
2307         std::end(ProfileReader::DISPLAY_ORIENTATION_MAP),
2308         [&ability](const auto &item) { return item.first == ability.orientation; });
2309     if (iterOrientation != ProfileReader::DISPLAY_ORIENTATION_MAP.end()) {
2310         abilityInfo.orientation = iterOrientation->second;
2311     }
2312 
2313     auto iterLaunch = std::find_if(std::begin(ProfileReader::LAUNCH_MODE_MAP),
2314         std::end(ProfileReader::LAUNCH_MODE_MAP),
2315         [&ability](const auto &item) { return item.first == ability.launchType; });
2316     if (iterLaunch != ProfileReader::LAUNCH_MODE_MAP.end()) {
2317         abilityInfo.launchMode = iterLaunch->second;
2318     }
2319 
2320     for (const auto &permission : ability.permissions) {
2321         abilityInfo.permissions.emplace_back(permission);
2322     }
2323     abilityInfo.process = (ability.process.empty()) ? configJson.app.bundleName : ability.process;
2324     abilityInfo.theme = ability.theme;
2325     abilityInfo.deviceTypes = configJson.module.deviceType;
2326     abilityInfo.deviceCapabilities = ability.deviceCapability;
2327     if (iterType->second == AbilityType::DATA &&
2328         ability.uri.find(Constants::DATA_ABILITY_URI_PREFIX) == std::string::npos) {
2329         APP_LOGE("ability uri invalid.");
2330         return false;
2331     }
2332     abilityInfo.uri = ability.uri;
2333     abilityInfo.package = configJson.module.package;
2334     abilityInfo.bundleName = configJson.app.bundleName;
2335     abilityInfo.moduleName = configJson.module.distro.moduleName;
2336     abilityInfo.applicationName = configJson.app.bundleName;
2337     abilityInfo.targetAbility = ability.targetAbility;
2338     abilityInfo.enabled = true;
2339     abilityInfo.supportPipMode = ability.supportPipMode;
2340     abilityInfo.readPermission = ability.readPermission;
2341     abilityInfo.writePermission = ability.writePermission;
2342     abilityInfo.configChanges = ability.configChanges;
2343     abilityInfo.formEntity = GetFormEntity(ability.form.formEntity);
2344     abilityInfo.minFormHeight = ability.form.minHeight;
2345     abilityInfo.defaultFormHeight = ability.form.defaultHeight;
2346     abilityInfo.minFormWidth = ability.form.minWidth;
2347     abilityInfo.defaultFormWidth = ability.form.defaultWidth;
2348     GetMetaData(abilityInfo.metaData, ability.metaData);
2349     abilityInfo.formEnabled = ability.formsEnabled;
2350     abilityInfo.backgroundModes = GetBackgroundModes(ability.backgroundModes);
2351     abilityInfo.isModuleJson = false;
2352     abilityInfo.startWindowIcon = ability.startWindowIcon;
2353     abilityInfo.startWindowIconId = ability.startWindowIconId;
2354     abilityInfo.startWindowBackground = ability.startWindowBackground;
2355     abilityInfo.startWindowBackgroundId = ability.startWindowBackgroundId;
2356     abilityInfo.removeMissionAfterTerminate = ability.removeMissionAfterTerminate;
2357     return true;
2358 }
2359 
ToInnerBundleInfo(ProfileReader::ConfigJson & configJson,const BundleExtractor & bundleExtractor,InnerBundleInfo & innerBundleInfo)2360 bool ToInnerBundleInfo(
2361     ProfileReader::ConfigJson &configJson,
2362     const BundleExtractor &bundleExtractor,
2363     InnerBundleInfo &innerBundleInfo)
2364 {
2365     APP_LOGD("transform profile configJson to innerBundleInfo");
2366     if (!CheckBundleNameIsValid(configJson.app.bundleName)) {
2367         APP_LOGE("bundle name is invalid");
2368         return false;
2369     }
2370     if (!CheckModuleInfosIsValid(configJson)) {
2371         APP_LOGE("module infos is invalid");
2372         return false;
2373     }
2374 
2375     TransformParam transformParam;
2376     transformParam.isPreInstallApp = innerBundleInfo.IsPreInstallApp();
2377 
2378     ApplicationInfo applicationInfo;
2379     applicationInfo.isSystemApp = innerBundleInfo.GetAppType() == Constants::AppType::SYSTEM_APP;
2380     transformParam.isSystemApp = applicationInfo.isSystemApp;
2381     if (!ToApplicationInfo(configJson, bundleExtractor, transformParam, applicationInfo)) {
2382         APP_LOGE("To applicationInfo failed");
2383         return false;
2384     }
2385 
2386     InnerModuleInfo innerModuleInfo;
2387     ToInnerModuleInfo(configJson, innerModuleInfo);
2388 
2389     BundleInfo bundleInfo;
2390     ToBundleInfo(configJson, applicationInfo, innerModuleInfo, transformParam, bundleInfo);
2391 
2392     for (const auto &info : configJson.module.shortcuts) {
2393         ShortcutInfo shortcutInfo;
2394         shortcutInfo.id = info.shortcutId;
2395         shortcutInfo.bundleName = configJson.app.bundleName;
2396         shortcutInfo.moduleName = configJson.module.distro.moduleName;
2397         shortcutInfo.icon = info.icon;
2398         shortcutInfo.label = info.label;
2399         shortcutInfo.iconId = info.iconId;
2400         shortcutInfo.labelId = info.labelId;
2401         for (const auto &intent : info.intents) {
2402             ShortcutIntent shortcutIntent;
2403             shortcutIntent.targetBundle = intent.targetBundle;
2404             shortcutIntent.targetModule = Constants::EMPTY_STRING;
2405             shortcutIntent.targetClass = intent.targetClass;
2406             shortcutInfo.intents.emplace_back(shortcutIntent);
2407         }
2408         std::string shortcutkey;
2409         shortcutkey.append(configJson.app.bundleName).append(".")
2410             .append(configJson.module.package).append(".").append(info.shortcutId);
2411         innerBundleInfo.InsertShortcutInfos(shortcutkey, shortcutInfo);
2412     }
2413     if (innerBundleInfo.GetAppType() == Constants::AppType::SYSTEM_APP) {
2414         for (const auto &info : configJson.module.commonEvents) {
2415             CommonEventInfo commonEvent;
2416             commonEvent.name = info.name;
2417             commonEvent.bundleName = configJson.app.bundleName;
2418             commonEvent.permission = info.permission;
2419             commonEvent.data = info.data;
2420             commonEvent.type = info.type;
2421             commonEvent.events = info.events;
2422             std::string commonEventKey;
2423             commonEventKey.append(configJson.app.bundleName).append(".")
2424                 .append(configJson.module.package).append(".").append(info.name);
2425             innerBundleInfo.InsertCommonEvents(commonEventKey, commonEvent);
2426         }
2427     }
2428     auto entryActionMatcher = [] (const std::string &action) {
2429         return action == Constants::ACTION_HOME || action == Constants::WANT_ACTION_HOME;
2430     };
2431     bool find = false;
2432     bool isExistPageAbility = false;
2433     for (const auto &ability : configJson.module.abilities) {
2434         AbilityInfo abilityInfo;
2435         if (!ToAbilityInfo(configJson, ability, transformParam, abilityInfo)) {
2436             APP_LOGE("parse to abilityInfo failed");
2437             return false;
2438         }
2439         if (abilityInfo.type == AbilityType::PAGE) {
2440             isExistPageAbility = true;
2441         }
2442         if (innerModuleInfo.mainAbility == abilityInfo.name) {
2443             innerModuleInfo.icon = abilityInfo.iconPath;
2444             innerModuleInfo.iconId = abilityInfo.iconId;
2445             innerModuleInfo.label = abilityInfo.label;
2446             innerModuleInfo.labelId = abilityInfo.labelId;
2447         }
2448         std::string keyName;
2449         keyName.append(configJson.app.bundleName).append(".")
2450             .append(configJson.module.package).append(".").append(abilityInfo.name);
2451         innerModuleInfo.abilityKeys.emplace_back(keyName);
2452         innerModuleInfo.skillKeys.emplace_back(keyName);
2453         innerBundleInfo.InsertSkillInfo(keyName, ability.skills);
2454         std::vector<FormInfo> formInfos;
2455         for (const auto &form : ability.formses) {
2456             FormInfo formInfo;
2457             ConvertFormInfo(formInfo, form);
2458             formInfo.abilityName = ability.name;
2459             if (ability.srcLanguage != "c++" && ability.name.substr(0, 1) == ".") {
2460                 formInfo.abilityName = configJson.module.package + ability.name;
2461             }
2462             formInfo.bundleName = configJson.app.bundleName;
2463             formInfo.moduleName = configJson.module.distro.moduleName;
2464             formInfo.package = configJson.module.package;
2465             formInfo.originalBundleName = configJson.app.originalName;
2466             formInfos.emplace_back(formInfo);
2467         }
2468         innerBundleInfo.InsertFormInfos(keyName, formInfos);
2469         if (!find) {
2470             for (const auto &skill : ability.skills) {
2471                 bool isEntryAction = std::find_if(skill.actions.begin(), skill.actions.end(),
2472                     entryActionMatcher) != skill.actions.end();
2473                 bool isEntryEntity = std::find(skill.entities.begin(), skill.entities.end(),
2474                     Constants::ENTITY_HOME) != skill.entities.end();
2475                 if (isEntryAction && isEntryEntity && (!find)) {
2476                     innerModuleInfo.entryAbilityKey = keyName;
2477                     // if there is main ability, it's label will be the application's label
2478                     applicationInfo.label = ability.label;
2479                     applicationInfo.labelId = ability.labelId;
2480                     applicationInfo.iconPath = ability.icon;
2481                     applicationInfo.iconId = ability.iconId;
2482                     applicationInfo.iconResource = BundleUtil::GetResource(
2483                         configJson.app.bundleName, configJson.module.distro.moduleName, ability.iconId);
2484                     applicationInfo.labelResource = BundleUtil::GetResource(
2485                         configJson.app.bundleName, configJson.module.distro.moduleName, ability.labelId);
2486                     find = true;
2487                 }
2488                 if (std::find(skill.entities.begin(), skill.entities.end(), Constants::FLAG_HOME_INTENT_FROM_SYSTEM) !=
2489                     skill.entities.end() && transformParam.isPreInstallApp &&
2490                     (abilityInfo.type == AbilityType::PAGE)) {
2491                     applicationInfo.isLauncherApp = true;
2492                     abilityInfo.isLauncherAbility = true;
2493                 }
2494             }
2495         }
2496         innerBundleInfo.InsertAbilitiesInfo(keyName, abilityInfo);
2497     }
2498     if ((!find || !isExistPageAbility) && !transformParam.isPreInstallApp &&
2499         innerModuleInfo.distro.moduleType != Profile::MODULE_TYPE_SHARED) {
2500         applicationInfo.needAppDetail = true;
2501         if (BundleUtil::IsExistDir(Constants::SYSTEM_LIB64)) {
2502             applicationInfo.appDetailAbilityLibraryPath = Profile::APP_DETAIL_ABILITY_LIBRARY_PATH_64;
2503         } else {
2504             applicationInfo.appDetailAbilityLibraryPath = Profile::APP_DETAIL_ABILITY_LIBRARY_PATH;
2505         }
2506         if ((applicationInfo.labelId == 0) && (applicationInfo.label.empty())) {
2507             applicationInfo.label = applicationInfo.bundleName;
2508         }
2509     }
2510     innerBundleInfo.SetCurrentModulePackage(configJson.module.package);
2511     innerBundleInfo.SetBaseApplicationInfo(applicationInfo);
2512     innerBundleInfo.SetBaseBundleInfo(bundleInfo);
2513     innerBundleInfo.InsertInnerModuleInfo(configJson.module.package, innerModuleInfo);
2514     if (innerBundleInfo.GetEntryInstallationFree()) {
2515         innerBundleInfo.SetApplicationBundleType(BundleType::ATOMIC_SERVICE);
2516     }
2517     return true;
2518 }
2519 
2520 }  // namespace
2521 
TransformTo(const std::ostringstream & source,const BundleExtractor & bundleExtractor,InnerBundleInfo & innerBundleInfo) const2522 ErrCode BundleProfile::TransformTo(
2523     const std::ostringstream &source,
2524     const BundleExtractor &bundleExtractor,
2525     InnerBundleInfo &innerBundleInfo) const
2526 {
2527     APP_LOGI("transform profile stream to bundle info");
2528     ProfileReader::ConfigJson configJson;
2529     nlohmann::json jsonObject = nlohmann::json::parse(source.str(), nullptr, false);
2530     if (jsonObject.is_discarded()) {
2531         APP_LOGE("bad profile");
2532         return ERR_APPEXECFWK_PARSE_BAD_PROFILE;
2533     }
2534     configJson = jsonObject.get<ProfileReader::ConfigJson>();
2535     if (ProfileReader::parseResult != ERR_OK) {
2536         APP_LOGE("parseResult is %{public}d", ProfileReader::parseResult);
2537         int32_t ret = ProfileReader::parseResult;
2538         // need recover parse result to ERR_OK
2539         ProfileReader::parseResult = ERR_OK;
2540         return ret;
2541     }
2542     if (!ToInnerBundleInfo(
2543         configJson, bundleExtractor, innerBundleInfo)) {
2544         return ERR_APPEXECFWK_PARSE_PROFILE_PROP_CHECK_ERROR;
2545     }
2546     if (!ParserNativeSo(configJson, bundleExtractor, innerBundleInfo)) {
2547         APP_LOGE("Parser native so failed.");
2548         return ERR_APPEXECFWK_PARSE_NATIVE_SO_FAILED;
2549     }
2550     return ERR_OK;
2551 }
2552 
TransformTo(const std::ostringstream & source,BundlePackInfo & bundlePackInfo)2553 ErrCode BundleProfile::TransformTo(const std::ostringstream &source, BundlePackInfo &bundlePackInfo)
2554 {
2555     APP_LOGI("transform packinfo stream to bundle pack info");
2556     nlohmann::json jsonObject = nlohmann::json::parse(source.str(), nullptr, false);
2557     if (jsonObject.is_discarded()) {
2558         APP_LOGE("bad profile");
2559         return ERR_APPEXECFWK_PARSE_BAD_PROFILE;
2560     }
2561     bundlePackInfo = jsonObject.get<BundlePackInfo>();
2562     return ERR_OK;
2563 }
2564 }  // namespace AppExecFwk
2565 }  // namespace OHOS
2566