1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "StageContext.h"
17 #include "JsonReader.h"
18 #include "PreviewerEngineLog.h"
19 using namespace std;
20
21 namespace OHOS::Ide {
GetInstance()22 StageContext& StageContext::GetInstance()
23 {
24 static StageContext instance;
25 return instance;
26 }
27
ParseJsonFile(const std::string & filePath)28 void StageContext::ParseJsonFile(const std::string& filePath)
29 {
30 string jsonStr = JsonReader::ReadFile(filePath);
31 Json::Value rootJson = JsonReader::ParseJsonData(jsonStr);
32 if (!rootJson) {
33 ELOG("Get module.json content failed.");
34 return;
35 }
36 if (!rootJson.isMember("app") || !rootJson.isMember("module")) {
37 ELOG("Don't find app or module node in module.json.");
38 return;
39 }
40 ParseAppInfo(rootJson["app"]);
41 ParseHapModuleInfo(rootJson["module"]);
42 }
43
ParseAppInfo(const Json::Value & root)44 void StageContext::ParseAppInfo(const Json::Value& root)
45 {
46 if (!root) {
47 ELOG("The information of stage model app info is null.");
48 return;
49 }
50 appInfo.apiReleaseType = JsonReader::GetString(root, "apiReleaseType");
51 appInfo.bundleName = JsonReader::GetString(root, "bundleName");
52 appInfo.compileSdkType = JsonReader::GetString(root, "compileSdkType");
53 appInfo.compileSdkVersion = JsonReader::GetString(root, "compileSdkVersion");
54 appInfo.debug = JsonReader::GetBool(root, "debug", false);
55 appInfo.icon = JsonReader::GetString(root, "icon");
56 appInfo.iconId = JsonReader::GetUInt(root, "iconId", 0);
57 appInfo.label = JsonReader::GetString(root, "label");
58 appInfo.labelId = JsonReader::GetUInt(root, "labelId", 0);
59 appInfo.minAPIVersion = JsonReader::GetUInt(root, "minAPIVersion", 0);
60 appInfo.targetAPIVersion = JsonReader::GetUInt(root, "targetAPIVersion", 0);
61 appInfo.vendor = JsonReader::GetString(root, "vendor");
62 appInfo.versionCode = JsonReader::GetUInt(root, "versionCode", 0);
63 appInfo.versionName = JsonReader::GetString(root, "versionName");
64 // from arkui
65 appInfo.bundleType = JsonReader::GetString(root, "bundleType");
66 appInfo.distributedNotificationEnabled = JsonReader::GetBool(root, "distributedNotificationEnabled", true);
67 }
68
ParseHapModuleInfo(const Json::Value & root)69 void StageContext::ParseHapModuleInfo(const Json::Value& root)
70 {
71 if (!root) {
72 ELOG("The information of stage model hap module info is null.");
73 return;
74 }
75 ParseAbilityInfo(root);
76 hapModuleInfo.compileMode = JsonReader::GetString(root, "compileMode");
77 hapModuleInfo.deliveryWithInstall = JsonReader::GetBool(root, "deliveryWithInstall", false);
78 hapModuleInfo.description = JsonReader::GetString(root, "description");
79 hapModuleInfo.descriptionId = JsonReader::GetUInt(root, "descriptionId", 0);
80 std::vector<std::string> deviceTypes;
81 std::unique_ptr<Json::Value> deviceTypesArr = JsonReader::GetArray(root, "deviceTypes");
82 if (deviceTypesArr) {
83 for (auto index = 0; index < JsonReader::GetArraySize(*deviceTypesArr); ++index) {
84 deviceTypes.push_back((*deviceTypesArr)[index].asString());
85 }
86 }
87 hapModuleInfo.deviceTypes = deviceTypes;
88 hapModuleInfo.installationFree = JsonReader::GetBool(root, "installationFree", false);
89 hapModuleInfo.mainElement = JsonReader::GetString(root, "mainElement");
90 hapModuleInfo.name = JsonReader::GetString(root, "name");
91 hapModuleInfo.pages = JsonReader::GetString(root, "pages");
92 hapModuleInfo.type = JsonReader::GetString(root, "type");
93 hapModuleInfo.virtualMachine = JsonReader::GetString(root, "virtualMachine");
94 hapModuleInfo.srcEntry = JsonReader::GetString(root, "srcEntry");
95 // from arkui
96 std::unique_ptr<Json::Value> metaData = JsonReader::GetArray(root, "metadata");
97 if (metaData) {
98 for (auto index = 0; index < JsonReader::GetArraySize(*metaData); ++index) {
99 if ((*metaData)[index] && JsonReader::GetString((*metaData)[index], "name") == "ArkTSPartialUpdate") {
100 hapModuleInfo.isPartialUpdate = (JsonReader::GetString((*metaData)[index], "value", "true") != "false");
101 }
102 }
103 }
104 }
105
ParseAbilityInfo(const Json::Value & root)106 void StageContext::ParseAbilityInfo(const Json::Value& root)
107 {
108 std::unique_ptr<Json::Value> abilitiesArr = JsonReader::GetArray(root, "abilities");
109 std::vector<AbilityInfo> abilities;
110 if (!abilitiesArr) {
111 return;
112 }
113 for (auto index = 0; index < JsonReader::GetArraySize(*abilitiesArr); ++index) {
114 AbilityInfo ability;
115 ability.description = JsonReader::GetString((*abilitiesArr)[index], "description");
116 ability.descriptionId = JsonReader::GetUInt((*abilitiesArr)[index], "descriptionId", 0);
117 ability.exported = JsonReader::GetBool((*abilitiesArr)[index], "exported");
118 ability.icon = JsonReader::GetString((*abilitiesArr)[index], "icon");
119 ability.iconId = JsonReader::GetUInt((*abilitiesArr)[index], "iconId");
120 ability.label = JsonReader::GetString((*abilitiesArr)[index], "label");
121 ability.labelId = JsonReader::GetUInt((*abilitiesArr)[index], "labelId");
122 ability.name = JsonReader::GetString((*abilitiesArr)[index], "name");
123 std::unique_ptr<Json::Value> skillsArr = JsonReader::GetArray((*abilitiesArr)[index], "skills");
124 std::vector<SkillInfo> skills;
125 ParseSkillsInfo(skillsArr, skills);
126 ability.skills = skills;
127 ability.srcEntrty = JsonReader::GetString((*abilitiesArr)[index], "srcEntrty");
128 ability.startWindowBackground = JsonReader::GetString((*abilitiesArr)[index], "startWindowBackground");
129 ability.startWindowBackgroundId = JsonReader::GetUInt((*abilitiesArr)[index], "startWindowBackgroundId");
130 ability.startWindowIcon = JsonReader::GetString((*abilitiesArr)[index], "startWindowIcon");
131 ability.startWindowIconId = JsonReader::GetUInt((*abilitiesArr)[index], "startWindowIconId");
132 abilities.push_back(ability);
133 }
134 hapModuleInfo.abilities = abilities;
135 }
136
ParseSkillsInfo(const std::unique_ptr<Json::Value> & skillsArr,std::vector<SkillInfo> & skills)137 void StageContext::ParseSkillsInfo(const std::unique_ptr<Json::Value>& skillsArr,
138 std::vector<SkillInfo>& skills)
139 {
140 if (!skillsArr) {
141 return;
142 }
143 for (auto index = 0; index < JsonReader::GetArraySize(*skillsArr); ++index) {
144 SkillInfo skill;
145 std::unique_ptr<Json::Value> actionsArr = JsonReader::GetArray((*skillsArr)[index], "actions");
146 if (actionsArr) {
147 std::vector<std::string> actions;
148 for (auto index = 0; index < JsonReader::GetArraySize(*actionsArr); ++index) {
149 actions.push_back((*actionsArr)[index].asString());
150 }
151 skill.actions = actions;
152 }
153 std::unique_ptr<Json::Value> entitiesArr = JsonReader::GetArray((*skillsArr)[index], "entities");
154 if (entitiesArr) {
155 std::vector<std::string> entities;
156 for (auto index = 0; index < JsonReader::GetArraySize(*entitiesArr); ++index) {
157 entities.push_back((*entitiesArr)[index].asString());
158 }
159 skill.entities = entities;
160 }
161 skills.push_back(skill);
162 }
163 }
164
GetAppInfo() const165 const AppInfo& StageContext::GetAppInfo() const
166 {
167 return appInfo;
168 }
169
GetHapModuleInfo() const170 const HapModuleInfo& StageContext::GetHapModuleInfo() const
171 {
172 return hapModuleInfo;
173 }
174
GetAbilityInfo(const std::string srcEntryVal) const175 const AbilityInfo& StageContext::GetAbilityInfo(const std::string srcEntryVal) const
176 {
177 for (AbilityInfo ability : hapModuleInfo.abilities) {
178 if (srcEntryVal == ability.srcEntrty) {
179 return ability;
180 }
181 }
182 return hapModuleInfo.abilities[0];
183 }
184 }