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