• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "default_app_data.h"
17 
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "default_app_mgr.h"
21 #include "nlohmann/json.hpp"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26     const std::string INFOS = "infos";
27     const std::string BUNDLE_NAME = "bundleName";
28     const std::string MODULE_NAME = "moduleName";
29     const std::string ABILITY_NAME = "abilityName";
30     const std::string EXTENSION_NAME = "extensionName";
31     const std::string TYPE = "type";
32 }
33 
ToString() const34 std::string DefaultAppData::ToString() const
35 {
36     APP_LOGD("DefaultAppData ToString begin.");
37     nlohmann::json j;
38     j[INFOS] = infos;
39     return j.dump();
40 }
41 
ToJson(nlohmann::json & jsonObject) const42 void DefaultAppData::ToJson(nlohmann::json& jsonObject) const
43 {
44     APP_LOGD("DefaultAppData ToJson begin.");
45     jsonObject[INFOS] = infos;
46 }
47 
FromJson(const nlohmann::json & jsonObject)48 int32_t DefaultAppData::FromJson(const nlohmann::json& jsonObject)
49 {
50     APP_LOGD("DefaultAppData FromJson begin.");
51     const auto& jsonObjectEnd = jsonObject.end();
52     int32_t parseResult = ERR_OK;
53     GetValueIfFindKey<std::map<std::string, Element>>(jsonObject,
54         jsonObjectEnd,
55         INFOS,
56         infos,
57         JsonType::OBJECT,
58         true,
59         parseResult,
60         ArrayType::NOT_ARRAY);
61     if (parseResult != ERR_OK) {
62         APP_LOGE("DefaultAppData FromJson failed, error code : %{public}d", parseResult);
63     }
64     return parseResult;
65 }
66 
ParseDefaultApplicationConfig(const nlohmann::json & jsonObject)67 bool DefaultAppData::ParseDefaultApplicationConfig(const nlohmann::json& jsonObject)
68 {
69     APP_LOGD("begin to ParseDefaultApplicationConfig.");
70     if (jsonObject.is_discarded() || !jsonObject.is_array() || jsonObject.empty()) {
71         APP_LOGW("json format error.");
72         return false;
73     }
74     for (const auto& object : jsonObject) {
75         if (!object.is_object()) {
76             APP_LOGW("not json object.");
77             continue;
78         }
79         Element element;
80         from_json(object, element);
81         if (element.type.empty() || !DefaultAppMgr::VerifyElementFormat(element)) {
82             APP_LOGW("bad element format.");
83             continue;
84         }
85         infos.try_emplace(element.type, element);
86     }
87     return true;
88 }
89 
to_json(nlohmann::json & jsonObject,const Element & element)90 void to_json(nlohmann::json& jsonObject, const Element& element)
91 {
92     APP_LOGD("Element to_json begin.");
93     jsonObject = nlohmann::json {
94         {BUNDLE_NAME, element.bundleName},
95         {MODULE_NAME, element.moduleName},
96         {ABILITY_NAME, element.abilityName},
97         {EXTENSION_NAME, element.extensionName},
98         {TYPE, element.type}
99     };
100 }
101 
from_json(const nlohmann::json & jsonObject,Element & element)102 void from_json(const nlohmann::json& jsonObject, Element& element)
103 {
104     APP_LOGD("Element from_json begin.");
105     const auto& jsonObjectEnd = jsonObject.end();
106     int32_t parseResult = ERR_OK;
107     GetValueIfFindKey<std::string>(jsonObject,
108         jsonObjectEnd,
109         BUNDLE_NAME,
110         element.bundleName,
111         JsonType::STRING,
112         false,
113         parseResult,
114         ArrayType::NOT_ARRAY);
115     GetValueIfFindKey<std::string>(jsonObject,
116         jsonObjectEnd,
117         MODULE_NAME,
118         element.moduleName,
119         JsonType::STRING,
120         false,
121         parseResult,
122         ArrayType::NOT_ARRAY);
123     GetValueIfFindKey<std::string>(jsonObject,
124         jsonObjectEnd,
125         ABILITY_NAME,
126         element.abilityName,
127         JsonType::STRING,
128         false,
129         parseResult,
130         ArrayType::NOT_ARRAY);
131     GetValueIfFindKey<std::string>(jsonObject,
132         jsonObjectEnd,
133         EXTENSION_NAME,
134         element.extensionName,
135         JsonType::STRING,
136         false,
137         parseResult,
138         ArrayType::NOT_ARRAY);
139     GetValueIfFindKey<std::string>(jsonObject,
140         jsonObjectEnd,
141         TYPE,
142         element.type,
143         JsonType::STRING,
144         false,
145         parseResult,
146         ArrayType::NOT_ARRAY);
147     if (parseResult != ERR_OK) {
148         APP_LOGE("Element from_json error, error code : %{public}d", parseResult);
149     }
150 }
151 }
152 }