• 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 "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 }
ToJsonStr() const40 std::string InnerMissionInfo::ToJsonStr() const
41 {
42     nlohmann::json value;
43     value[KEY_MISSION_NAME] = missionName;
44     value[KEY_LAUNCH_MODE] = launchMode;
45     value[KEY_IS_TEMPORARY] = isTemporary;
46     value[KEY_BUNDLE_NAME] = bundleName;
47     value[KEY_START_METHOD] = startMethod;
48     value[KEY_UID] = uid;
49     value[KEY_SPEC_FLAG] = specifiedFlag;
50     value[KEY_MISSION_ID] = missionInfo.id;
51     value[KEY_RUNNING_STATE] = missionInfo.runningState;
52     value[KEY_LOCKED_STATE] = missionInfo.lockedState;
53     value[KEY_CONTINUABLE] = missionInfo.continuable;
54     value[KEY_TIME] = missionInfo.time;
55     value[KEY_LABEL] = missionInfo.label;
56     value[KEY_ICON_PATH] = missionInfo.iconPath;
57     value[KEY_WANT] = missionInfo.want.ToUri();
58 
59     return value.dump();
60 }
61 
FromJsonStr(const std::string & jsonStr)62 bool InnerMissionInfo::FromJsonStr(const std::string &jsonStr)
63 {
64     // Do not throw exceptions in nlohmann::json::parse
65     nlohmann::json value = nlohmann::json::parse(jsonStr, nullptr, false);
66     if (value.is_discarded()) {
67         HILOG_ERROR("failed to parse json sting: %{private}s.", jsonStr.c_str());
68         return false;
69     }
70 
71     if (!CheckJsonNode(value, KEY_MISSION_NAME, JsonType::STRING)) {
72         return false;
73     }
74     missionName = value[KEY_MISSION_NAME].get<std::string>();
75 
76     if (!CheckJsonNode(value, KEY_LAUNCH_MODE, JsonType::NUMBER)) {
77         return false;
78     }
79     launchMode = value[KEY_LAUNCH_MODE].get<int32_t>();
80     if (!CheckJsonNode(value, KEY_IS_TEMPORARY, JsonType::BOOLEAN)) {
81         return false;
82     }
83     isTemporary = value[KEY_IS_TEMPORARY].get<bool>();
84     if (!CheckJsonNode(value, KEY_START_METHOD, JsonType::NUMBER)) {
85         return false;
86     }
87     startMethod = value[KEY_START_METHOD].get<int32_t>();
88     if (!CheckJsonNode(value, KEY_BUNDLE_NAME, JsonType::STRING)) {
89         return false;
90     }
91     bundleName = value[KEY_BUNDLE_NAME].get<std::string>();
92     if (!CheckJsonNode(value, KEY_UID, JsonType::NUMBER)) {
93         return false;
94     }
95     uid = value[KEY_UID].get<int32_t>();
96     if (!CheckJsonNode(value, KEY_SPEC_FLAG, JsonType::STRING)) {
97         return false;
98     }
99     specifiedFlag = value[KEY_SPEC_FLAG].get<std::string>();
100     if (!CheckJsonNode(value, KEY_MISSION_ID, JsonType::NUMBER)) {
101         return false;
102     }
103     missionInfo.id = value[KEY_MISSION_ID].get<int32_t>();
104     if (!CheckJsonNode(value, KEY_RUNNING_STATE, JsonType::NUMBER)) {
105         return false;
106     }
107     missionInfo.runningState = value[KEY_RUNNING_STATE].get<int32_t>();
108     if (!CheckJsonNode(value, KEY_LOCKED_STATE, JsonType::BOOLEAN)) {
109         return false;
110     }
111     missionInfo.lockedState = value[KEY_LOCKED_STATE].get<bool>();
112     if (!CheckJsonNode(value, KEY_CONTINUABLE, JsonType::BOOLEAN)) {
113         return false;
114     }
115     missionInfo.continuable = value[KEY_CONTINUABLE].get<bool>();
116     if (!CheckJsonNode(value, KEY_TIME, JsonType::STRING)) {
117         return false;
118     }
119     missionInfo.time = value[KEY_TIME].get<std::string>();
120     if (!CheckJsonNode(value, KEY_LABEL, JsonType::STRING)) {
121         return false;
122     }
123     missionInfo.label = value[KEY_LABEL].get<std::string>();
124     if (!CheckJsonNode(value, KEY_ICON_PATH, JsonType::STRING)) {
125         return false;
126     }
127     missionInfo.iconPath = value[KEY_ICON_PATH].get<std::string>();
128     if (!CheckJsonNode(value, KEY_WANT, JsonType::STRING)) {
129         return false;
130     }
131     Want* want = Want::ParseUri(value[KEY_WANT].get<std::string>());
132     if (want) {
133         missionInfo.want = *want;
134     }
135     return true;
136 }
137 
Dump(std::vector<std::string> & info) const138 void InnerMissionInfo::Dump(std::vector<std::string> &info) const
139 {
140     std::string dumpInfo = "      Mission ID #" + std::to_string(missionInfo.id);
141     info.push_back(dumpInfo);
142     dumpInfo = "        mission name [" + missionName + "]";
143     info.push_back(dumpInfo);
144     dumpInfo = "        runningState [" + std::to_string(missionInfo.runningState) + "]";
145     info.push_back(dumpInfo);
146     dumpInfo = "        lockedState [" + std::to_string(missionInfo.lockedState) + "]";
147     info.push_back(dumpInfo);
148     dumpInfo = "        continuable [" + std::to_string(missionInfo.continuable) + "]";
149     info.push_back(dumpInfo);
150     dumpInfo = "        timeStamp [" + missionInfo.time + "]";
151     info.push_back(dumpInfo);
152     dumpInfo = "        label [" + missionInfo.label + "]";
153     info.push_back(dumpInfo);
154     dumpInfo = "        iconPath [" + missionInfo.iconPath + "]";
155     info.push_back(dumpInfo);
156     dumpInfo = "        want [" + missionInfo.want.ToUri() + "]";
157     info.push_back(dumpInfo);
158 }
159 
CheckJsonNode(nlohmann::json & value,const std::string & node,JsonType jsonType)160 bool InnerMissionInfo::CheckJsonNode(nlohmann::json &value, const std::string &node, JsonType jsonType)
161 {
162     if (value.find(node) == value.end()) {
163         HILOG_ERROR("node %{private}s not exists.", node.c_str());
164         return false;
165     }
166 
167     if (jsonType == JsonType::NUMBER) {
168         return value[node].is_number();
169     }
170     if (jsonType == JsonType::STRING) {
171         return value[node].is_string();
172     }
173     if (jsonType == JsonType::BOOLEAN) {
174         return value[node].is_boolean();
175     }
176     return false;
177 }
178 }  // namespace AAFwk
179 }  // namespace OHOS
180