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 "inner_app_quick_fix.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "json_util.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 const char* APP_QUICK_FIX = "appQuickFix";
25 const char* QUICK_FIX_MARK = "quickFixMark";
26 const char* QUICK_FIX_MARK_STATUS = "status";
27 }
28
InnerAppQuickFix()29 InnerAppQuickFix::InnerAppQuickFix()
30 {
31 }
32
InnerAppQuickFix(const AppQuickFix & appQuickFix,const QuickFixMark & mark)33 InnerAppQuickFix::InnerAppQuickFix(const AppQuickFix &appQuickFix, const QuickFixMark &mark)
34 : appQuickFix_(appQuickFix), quickFixMark_(mark)
35 {
36 }
37
~InnerAppQuickFix()38 InnerAppQuickFix::~InnerAppQuickFix()
39 {
40 }
41
SetAppQuickFix(const AppQuickFix & appQuickFix)42 void InnerAppQuickFix::SetAppQuickFix(const AppQuickFix &appQuickFix)
43 {
44 appQuickFix_ = appQuickFix;
45 }
46
GetAppQuickFix() const47 AppQuickFix InnerAppQuickFix::GetAppQuickFix() const
48 {
49 return appQuickFix_;
50 }
51
AddHqfInfo(const AppQuickFix & newInfo)52 bool InnerAppQuickFix::AddHqfInfo(const AppQuickFix &newInfo)
53 {
54 if (newInfo.deployingAppqfInfo.hqfInfos.empty()) {
55 LOG_E(BMS_TAG_DEFAULT, "InnerAppQuickFix::AddHqfInfo failed due to hqfInfos empty");
56 return false;
57 }
58 for (const auto &item : newInfo.deployingAppqfInfo.hqfInfos) {
59 if (!RemoveHqfInfo(item.moduleName)) {
60 LOG_D(BMS_TAG_DEFAULT, "moduleName %{public}s does not exist", item.moduleName.c_str());
61 }
62 appQuickFix_.deployingAppqfInfo.hqfInfos.emplace_back(item);
63 }
64 return true;
65 }
66
RemoveHqfInfo(const std::string & moduleName)67 bool InnerAppQuickFix::RemoveHqfInfo(const std::string &moduleName)
68 {
69 auto iter = std::find_if(
70 std::begin(appQuickFix_.deployingAppqfInfo.hqfInfos),
71 std::end(appQuickFix_.deployingAppqfInfo.hqfInfos),
72 [moduleName] (const auto &item) { return item.moduleName == moduleName;});
73 if (iter == appQuickFix_.deployingAppqfInfo.hqfInfos.end()) {
74 LOG_E(BMS_TAG_DEFAULT, "RemoveHqfInfo failed due to %{public}s does not exist", moduleName.c_str());
75 return false;
76 }
77 appQuickFix_.deployingAppqfInfo.hqfInfos.erase(iter);
78 return true;
79 }
80
SwitchQuickFix()81 void InnerAppQuickFix::SwitchQuickFix()
82 {
83 AppqfInfo deployed = appQuickFix_.deployedAppqfInfo;
84 appQuickFix_.deployedAppqfInfo = appQuickFix_.deployingAppqfInfo;
85 appQuickFix_.deployingAppqfInfo = deployed;
86 }
87
SetQuickFixMark(const QuickFixMark & mark)88 void InnerAppQuickFix::SetQuickFixMark(const QuickFixMark &mark)
89 {
90 quickFixMark_ = mark;
91 }
92
GetQuickFixMark() const93 QuickFixMark InnerAppQuickFix::GetQuickFixMark() const
94 {
95 return quickFixMark_;
96 }
97
ToString() const98 std::string InnerAppQuickFix::ToString() const
99 {
100 nlohmann::json object;
101 ToJson(object);
102 return object.dump();
103 }
104
ToJson(nlohmann::json & jsonObject) const105 void InnerAppQuickFix::ToJson(nlohmann::json &jsonObject) const
106 {
107 jsonObject[APP_QUICK_FIX] = appQuickFix_;
108 jsonObject[QUICK_FIX_MARK] = quickFixMark_;
109 }
110
FromJson(const nlohmann::json & jsonObject)111 int32_t InnerAppQuickFix::FromJson(const nlohmann::json &jsonObject)
112 {
113 const auto &jsonObjectEnd = jsonObject.end();
114 int32_t parseResult = ERR_OK;
115 GetValueIfFindKey<AppQuickFix>(jsonObject,
116 jsonObjectEnd,
117 APP_QUICK_FIX,
118 appQuickFix_,
119 JsonType::OBJECT,
120 false,
121 parseResult,
122 ArrayType::NOT_ARRAY);
123 GetValueIfFindKey<QuickFixMark>(jsonObject,
124 jsonObjectEnd,
125 QUICK_FIX_MARK,
126 quickFixMark_,
127 JsonType::OBJECT,
128 false,
129 parseResult,
130 ArrayType::NOT_ARRAY);
131 if (parseResult != ERR_OK) {
132 LOG_E(BMS_TAG_DEFAULT, "read InnerAppQuickFix from database error, error code : %{public}d", parseResult);
133 }
134 return parseResult;
135 }
136
to_json(nlohmann::json & jsonObject,const QuickFixMark & quickFixMark)137 void to_json(nlohmann::json &jsonObject, const QuickFixMark &quickFixMark)
138 {
139 jsonObject = nlohmann::json {
140 {Constants::BUNDLE_NAME, quickFixMark.bundleName},
141 {QUICK_FIX_MARK_STATUS, quickFixMark.status}
142 };
143 }
144
from_json(const nlohmann::json & jsonObject,QuickFixMark & quickFixMark)145 void from_json(const nlohmann::json &jsonObject, QuickFixMark &quickFixMark)
146 {
147 const auto &jsonObjectEnd = jsonObject.end();
148 int32_t parseResult = ERR_OK;
149 GetValueIfFindKey<std::string>(jsonObject,
150 jsonObjectEnd,
151 Constants::BUNDLE_NAME,
152 quickFixMark.bundleName,
153 JsonType::STRING,
154 false,
155 parseResult,
156 ArrayType::NOT_ARRAY);
157 GetValueIfFindKey<int32_t>(jsonObject,
158 jsonObjectEnd,
159 QUICK_FIX_MARK_STATUS,
160 quickFixMark.status,
161 JsonType::NUMBER,
162 false,
163 parseResult,
164 ArrayType::NOT_ARRAY);
165 if (parseResult != ERR_OK) {
166 LOG_E(BMS_TAG_DEFAULT, "QuickFixMark from_json error, error code : %{public}d", parseResult);
167 }
168 }
169 } // AppExecFwk
170 } // OHOS