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     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     const std::string 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 j;
39     j[INFOS] = infos;
40     return j.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     GetValueIfFindKey<std::string>(jsonObject,
121         jsonObjectEnd,
122         BUNDLE_NAME,
123         element.bundleName,
124         JsonType::STRING,
125         false,
126         g_defaultAppJson,
127         ArrayType::NOT_ARRAY);
128     GetValueIfFindKey<std::string>(jsonObject,
129         jsonObjectEnd,
130         MODULE_NAME,
131         element.moduleName,
132         JsonType::STRING,
133         false,
134         g_defaultAppJson,
135         ArrayType::NOT_ARRAY);
136     GetValueIfFindKey<std::string>(jsonObject,
137         jsonObjectEnd,
138         ABILITY_NAME,
139         element.abilityName,
140         JsonType::STRING,
141         false,
142         g_defaultAppJson,
143         ArrayType::NOT_ARRAY);
144     GetValueIfFindKey<std::string>(jsonObject,
145         jsonObjectEnd,
146         EXTENSION_NAME,
147         element.extensionName,
148         JsonType::STRING,
149         false,
150         g_defaultAppJson,
151         ArrayType::NOT_ARRAY);
152     GetValueIfFindKey<std::string>(jsonObject,
153         jsonObjectEnd,
154         TYPE,
155         element.type,
156         JsonType::STRING,
157         false,
158         g_defaultAppJson,
159         ArrayType::NOT_ARRAY);
160     GetValueIfFindKey<std::string>(jsonObject,
161         jsonObjectEnd,
162         APP_TYPE,
163         element.type,
164         JsonType::STRING,
165         false,
166         g_defaultAppJson,
167         ArrayType::NOT_ARRAY);
168     if (g_defaultAppJson != ERR_OK) {
169         LOG_E(BMS_TAG_DEFAULT, "Element from_json error, error code : %{public}d", g_defaultAppJson);
170     }
171 }
172 }
173 }