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