1 /*
2 * Copyright (c) 2021-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 "inner_mission_info.h"
17
18 #include "hilog_wrapper.h"
19 #include "nlohmann/json.hpp"
20
21 namespace OHOS {
22 namespace AAFwk {
23 namespace {
24 const std::string KEY_MISSION_NAME = "MissionName";
25 const std::string KEY_LAUNCH_MODE = "LaunchMode";
26 const std::string KEY_MISSION_ID = "MissionId";
27 const std::string KEY_RUNNING_STATE = "RunningState";
28 const std::string KEY_LOCKED_STATE = "LockedState";
29 const std::string KEY_CONTINUABLE = "Continuable";
30 const std::string KEY_TIME = "Time";
31 const std::string KEY_LABEL = "Label";
32 const std::string KEY_ICON_PATH = "IconPath";
33 const std::string KEY_WANT = "Want";
34 const std::string KEY_START_METHOD = "StartMethod";
35 const std::string KEY_BUNDLE_NAME = "BundleName";
36 const std::string KEY_UID = "Uid";
37 const std::string KEY_IS_TEMPORARY = "IsTemporary";
38 const std::string KEY_SPEC_FLAG = "SpecFlag";
39 const std::string KEY_HAS_RECONER_INFO = "hasRecoverInfo";
40 }
ToJsonStr() const41 std::string InnerMissionInfo::ToJsonStr() const
42 {
43 nlohmann::json value;
44 value[KEY_MISSION_NAME] = missionName;
45 value[KEY_LAUNCH_MODE] = launchMode;
46 value[KEY_IS_TEMPORARY] = isTemporary;
47 value[KEY_BUNDLE_NAME] = bundleName;
48 value[KEY_START_METHOD] = startMethod;
49 value[KEY_UID] = uid;
50 value[KEY_SPEC_FLAG] = specifiedFlag;
51 value[KEY_MISSION_ID] = missionInfo.id;
52 value[KEY_RUNNING_STATE] = missionInfo.runningState;
53 value[KEY_LOCKED_STATE] = missionInfo.lockedState;
54 value[KEY_CONTINUABLE] = missionInfo.continuable;
55 value[KEY_TIME] = missionInfo.time;
56 value[KEY_LABEL] = missionInfo.label;
57 value[KEY_ICON_PATH] = missionInfo.iconPath;
58 value[KEY_WANT] = missionInfo.want.ToUri();
59 value[KEY_HAS_RECONER_INFO] = hasRecoverInfo;
60
61 return value.dump();
62 }
63
FromJsonStr(const std::string & jsonStr)64 bool InnerMissionInfo::FromJsonStr(const std::string &jsonStr)
65 {
66 // Do not throw exceptions in nlohmann::json::parse
67 nlohmann::json value = nlohmann::json::parse(jsonStr, nullptr, false);
68 if (value.is_discarded()) {
69 HILOG_ERROR("failed to parse json sting: %{private}s.", jsonStr.c_str());
70 return false;
71 }
72
73 if (!CheckJsonNode(value, KEY_MISSION_NAME, JsonType::STRING)) {
74 return false;
75 }
76 missionName = value[KEY_MISSION_NAME].get<std::string>();
77
78 if (!CheckJsonNode(value, KEY_LAUNCH_MODE, JsonType::NUMBER)) {
79 return false;
80 }
81 launchMode = value[KEY_LAUNCH_MODE].get<int32_t>();
82 if (!CheckJsonNode(value, KEY_IS_TEMPORARY, JsonType::BOOLEAN)) {
83 return false;
84 }
85 isTemporary = value[KEY_IS_TEMPORARY].get<bool>();
86 if (!CheckJsonNode(value, KEY_START_METHOD, JsonType::NUMBER)) {
87 return false;
88 }
89 startMethod = value[KEY_START_METHOD].get<int32_t>();
90 if (!CheckJsonNode(value, KEY_BUNDLE_NAME, JsonType::STRING)) {
91 return false;
92 }
93 bundleName = value[KEY_BUNDLE_NAME].get<std::string>();
94 if (!CheckJsonNode(value, KEY_UID, JsonType::NUMBER)) {
95 return false;
96 }
97 uid = value[KEY_UID].get<int32_t>();
98 if (!CheckJsonNode(value, KEY_SPEC_FLAG, JsonType::STRING)) {
99 return false;
100 }
101 specifiedFlag = value[KEY_SPEC_FLAG].get<std::string>();
102 if (!CheckJsonNode(value, KEY_MISSION_ID, JsonType::NUMBER)) {
103 return false;
104 }
105 missionInfo.id = value[KEY_MISSION_ID].get<int32_t>();
106 if (!CheckJsonNode(value, KEY_RUNNING_STATE, JsonType::NUMBER)) {
107 return false;
108 }
109 missionInfo.runningState = value[KEY_RUNNING_STATE].get<int32_t>();
110 if (!CheckJsonNode(value, KEY_LOCKED_STATE, JsonType::BOOLEAN)) {
111 return false;
112 }
113 missionInfo.lockedState = value[KEY_LOCKED_STATE].get<bool>();
114 if (!CheckJsonNode(value, KEY_CONTINUABLE, JsonType::BOOLEAN)) {
115 return false;
116 }
117 missionInfo.continuable = value[KEY_CONTINUABLE].get<bool>();
118 if (!CheckJsonNode(value, KEY_TIME, JsonType::STRING)) {
119 return false;
120 }
121 missionInfo.time = value[KEY_TIME].get<std::string>();
122 if (!CheckJsonNode(value, KEY_LABEL, JsonType::STRING)) {
123 return false;
124 }
125 missionInfo.label = value[KEY_LABEL].get<std::string>();
126 if (!CheckJsonNode(value, KEY_ICON_PATH, JsonType::STRING)) {
127 return false;
128 }
129 missionInfo.iconPath = value[KEY_ICON_PATH].get<std::string>();
130 if (!CheckJsonNode(value, KEY_WANT, JsonType::STRING)) {
131 return false;
132 }
133 if (!CheckJsonNode(value, KEY_HAS_RECONER_INFO, JsonType::BOOLEAN)) {
134 return false;
135 }
136 hasRecoverInfo = value[KEY_HAS_RECONER_INFO].get<bool>();
137 Want* want = Want::ParseUri(value[KEY_WANT].get<std::string>());
138 if (want) {
139 missionInfo.want = *want;
140 }
141 return true;
142 }
143
Dump(std::vector<std::string> & info) const144 void InnerMissionInfo::Dump(std::vector<std::string> &info) const
145 {
146 std::string dumpInfo = " Mission ID #" + std::to_string(missionInfo.id);
147 info.push_back(dumpInfo);
148 dumpInfo = " mission name [" + missionName + "]";
149 info.push_back(dumpInfo);
150 dumpInfo = " runningState [" + std::to_string(missionInfo.runningState) + "]";
151 info.push_back(dumpInfo);
152 dumpInfo = " lockedState [" + std::to_string(missionInfo.lockedState) + "]";
153 info.push_back(dumpInfo);
154 dumpInfo = " continuable [" + std::to_string(missionInfo.continuable) + "]";
155 info.push_back(dumpInfo);
156 dumpInfo = " timeStamp [" + missionInfo.time + "]";
157 info.push_back(dumpInfo);
158 dumpInfo = " label [" + missionInfo.label + "]";
159 info.push_back(dumpInfo);
160 dumpInfo = " iconPath [" + missionInfo.iconPath + "]";
161 info.push_back(dumpInfo);
162 dumpInfo = " want [" + missionInfo.want.ToUri() + "]";
163 info.push_back(dumpInfo);
164 }
165
CheckJsonNode(nlohmann::json & value,const std::string & node,JsonType jsonType)166 bool InnerMissionInfo::CheckJsonNode(nlohmann::json &value, const std::string &node, JsonType jsonType)
167 {
168 if (value.find(node) == value.end()) {
169 HILOG_ERROR("node %{private}s not exists.", node.c_str());
170 return false;
171 }
172
173 if (jsonType == JsonType::NUMBER) {
174 return value[node].is_number();
175 }
176 if (jsonType == JsonType::STRING) {
177 return value[node].is_string();
178 }
179 if (jsonType == JsonType::BOOLEAN) {
180 return value[node].is_boolean();
181 }
182 return false;
183 }
184
UpdateMissionInfo(const InnerMissionInfoDto & info)185 void InnerMissionInfo::UpdateMissionInfo(const InnerMissionInfoDto &info)
186 {
187 missionInfo = info.missionInfo;
188 missionName = info.missionName;
189 missionAffinity = info.missionAffinity;
190 launchMode = info.launchMode;
191 startMethod = info.startMethod;
192 bundleName = info.bundleName;
193 uid = info.uid;
194 isTemporary = info.isTemporary;
195 specifiedFlag = info.specifiedFlag;
196 hasRecoverInfo = info.hasRecoverInfo;
197 collaboratorType = info.collaboratorType;
198 }
199
ConvertInnerMissionInfoDto()200 InnerMissionInfoDto InnerMissionInfo::ConvertInnerMissionInfoDto()
201 {
202 InnerMissionInfoDto info;
203 info.missionInfo = missionInfo;
204 info.missionName = missionName;
205 info.missionAffinity = missionAffinity;
206 info.launchMode = launchMode;
207 info.startMethod = startMethod;
208 info.bundleName = bundleName;
209 info.uid = uid;
210 info.isTemporary = isTemporary;
211 info.specifiedFlag = specifiedFlag;
212 info.hasRecoverInfo = hasRecoverInfo;
213 info.collaboratorType = collaboratorType;
214
215 return info;
216 }
217 } // namespace AAFwk
218 } // namespace OHOS
219