1 /*
2 * Copyright (c) 2022-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 "default_app_data.h"
17
18 #include <mutex>
19
20 #include "app_log_wrapper.h"
21 #include "appexecfwk_errors.h"
22 #include "common_profile.h"
23 #include "default_app_mgr.h"
24 #include "nlohmann/json.hpp"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 int32_t g_defaultAppJson = ERR_OK;
30 std::mutex g_mutex;
31 const std::string INFOS = "infos";
32 const std::string BUNDLE_NAME = "bundleName";
33 const std::string MODULE_NAME = "moduleName";
34 const std::string ABILITY_NAME = "abilityName";
35 const std::string EXTENSION_NAME = "extensionName";
36 const std::string TYPE = "type";
37 const std::string APP_TYPE = "appType";
38 }
39
ToString() const40 std::string DefaultAppData::ToString() const
41 {
42 APP_LOGD("DefaultAppData ToString begin.");
43 nlohmann::json j;
44 j[INFOS] = infos;
45 return j.dump();
46 }
47
ToJson(nlohmann::json & jsonObject) const48 void DefaultAppData::ToJson(nlohmann::json& jsonObject) const
49 {
50 APP_LOGD("DefaultAppData ToJson begin.");
51 jsonObject[INFOS] = infos;
52 }
53
FromJson(const nlohmann::json & jsonObject)54 int32_t DefaultAppData::FromJson(const nlohmann::json& jsonObject)
55 {
56 APP_LOGD("DefaultAppData FromJson begin.");
57 const auto& jsonObjectEnd = jsonObject.end();
58 std::lock_guard<std::mutex> lock(g_mutex);
59 g_defaultAppJson = ERR_OK;
60 GetValueIfFindKey<std::map<std::string, Element>>(jsonObject,
61 jsonObjectEnd,
62 INFOS,
63 infos,
64 JsonType::OBJECT,
65 true,
66 g_defaultAppJson,
67 ArrayType::NOT_ARRAY);
68 if (g_defaultAppJson != ERR_OK) {
69 APP_LOGE("DefaultAppData FromJson failed, error code : %{public}d", g_defaultAppJson);
70 }
71 int32_t ret = g_defaultAppJson;
72 g_defaultAppJson = ERR_OK;
73 return ret;
74 }
75
ParseDefaultApplicationConfig(const nlohmann::json & jsonObject)76 void DefaultAppData::ParseDefaultApplicationConfig(const nlohmann::json& jsonObject)
77 {
78 APP_LOGD("begin to ParseDefaultApplicationConfig.");
79 if (jsonObject.is_discarded() || !jsonObject.is_array() || jsonObject.empty()) {
80 APP_LOGW("json format error.");
81 return;
82 }
83 std::lock_guard<std::mutex> lock(g_mutex);
84 for (const auto& object : jsonObject) {
85 if (!object.is_object()) {
86 APP_LOGW("not json object.");
87 continue;
88 }
89 Element element;
90 g_defaultAppJson = ERR_OK;
91 from_json(object, element);
92 g_defaultAppJson = ERR_OK;
93 if (element.type.empty() || !DefaultAppMgr::VerifyElementFormat(element)) {
94 APP_LOGW("bad element format.");
95 continue;
96 }
97 infos.try_emplace(element.type, element);
98 }
99 }
100
to_json(nlohmann::json & jsonObject,const Element & element)101 void to_json(nlohmann::json& jsonObject, const Element& element)
102 {
103 APP_LOGD("Element to_json begin.");
104 jsonObject = nlohmann::json {
105 {BUNDLE_NAME, element.bundleName},
106 {MODULE_NAME, element.moduleName},
107 {ABILITY_NAME, element.abilityName},
108 {EXTENSION_NAME, element.extensionName},
109 {TYPE, element.type}
110 };
111 }
112
from_json(const nlohmann::json & jsonObject,Element & element)113 void from_json(const nlohmann::json& jsonObject, Element& element)
114 {
115 APP_LOGD("Element from_json begin");
116 const auto& jsonObjectEnd = jsonObject.end();
117 GetValueIfFindKey<std::string>(jsonObject,
118 jsonObjectEnd,
119 BUNDLE_NAME,
120 element.bundleName,
121 JsonType::STRING,
122 false,
123 g_defaultAppJson,
124 ArrayType::NOT_ARRAY);
125 GetValueIfFindKey<std::string>(jsonObject,
126 jsonObjectEnd,
127 MODULE_NAME,
128 element.moduleName,
129 JsonType::STRING,
130 false,
131 g_defaultAppJson,
132 ArrayType::NOT_ARRAY);
133 GetValueIfFindKey<std::string>(jsonObject,
134 jsonObjectEnd,
135 ABILITY_NAME,
136 element.abilityName,
137 JsonType::STRING,
138 false,
139 g_defaultAppJson,
140 ArrayType::NOT_ARRAY);
141 GetValueIfFindKey<std::string>(jsonObject,
142 jsonObjectEnd,
143 EXTENSION_NAME,
144 element.extensionName,
145 JsonType::STRING,
146 false,
147 g_defaultAppJson,
148 ArrayType::NOT_ARRAY);
149 GetValueIfFindKey<std::string>(jsonObject,
150 jsonObjectEnd,
151 TYPE,
152 element.type,
153 JsonType::STRING,
154 false,
155 g_defaultAppJson,
156 ArrayType::NOT_ARRAY);
157 GetValueIfFindKey<std::string>(jsonObject,
158 jsonObjectEnd,
159 APP_TYPE,
160 element.type,
161 JsonType::STRING,
162 false,
163 g_defaultAppJson,
164 ArrayType::NOT_ARRAY);
165 if (g_defaultAppJson != ERR_OK) {
166 APP_LOGE("Element from_json error, error code : %{public}d", g_defaultAppJson);
167 }
168 }
169 }
170 }