1 /*
2 * Copyright (c) 2025 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 "patch_data_mgr.h"
17 #include "app_log_tag_wrapper.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
21 namespace {
22 constexpr const char* QUICK_FIX_ENGINE_PATH = "/data/update/quickfix/app/temp";
23 }
24
PatchDataMgr()25 PatchDataMgr::PatchDataMgr()
26 {
27 APP_LOGI("PatchDataMgr instance is created");
28 patchDataStorage_ = std::make_shared<PatchDataStorageRdb>();
29 }
30
~PatchDataMgr()31 PatchDataMgr::~PatchDataMgr()
32 {
33 APP_LOGI("PatchDataMgr instance is destroyed");
34 }
35
GetInstance()36 PatchDataMgr& PatchDataMgr::GetInstance()
37 {
38 static PatchDataMgr patchDataMgr;
39 return patchDataMgr;
40 }
41
AddInnerPatchInfo(const std::string & bundleName,const InnerPatchInfo & info)42 bool PatchDataMgr::AddInnerPatchInfo(const std::string &bundleName, const InnerPatchInfo &info)
43 {
44 if (bundleName.empty()) {
45 APP_LOGE("AddInnerPatchInfo failed, invalid param");
46 return false;
47 }
48 if (!patchDataStorage_->SaveStoragePatchInfo(bundleName, info)) {
49 APP_LOGE("Save InnerPatchInfo failed, bundleName: %{public}s", bundleName.c_str());
50 return false;
51 }
52 return true;
53 }
54
GetInnerPatchInfo(const std::string & bundleName,InnerPatchInfo & info) const55 bool PatchDataMgr::GetInnerPatchInfo(const std::string &bundleName, InnerPatchInfo &info) const
56 {
57 if (bundleName.empty()) {
58 APP_LOGE("GetInnerPatchInfo failed, invalid param");
59 return false;
60 }
61 if (!patchDataStorage_->GetStoragePatchInfo(bundleName, info)) {
62 APP_LOGE("Get InnerPatchInfo failed, bundleName: %{public}s", bundleName.c_str());
63 return false;
64 }
65 return true;
66 }
67
DeleteInnerPatchInfo(const std::string & bundleName)68 bool PatchDataMgr::DeleteInnerPatchInfo(const std::string &bundleName)
69 {
70 if (bundleName.empty()) {
71 APP_LOGE("DeleteInnerPatchInfo failed, invalid param");
72 return false;
73 }
74 if (!patchDataStorage_->DeleteStoragePatchInfo(bundleName)) {
75 APP_LOGE("Delete InnerPatchInfo failed, bundleName: %{public}s", bundleName.c_str());
76 return false;
77 }
78 return true;
79 }
80
ProcessPatchInfo(const std::string & bundleName,const std::vector<std::string> & installSources,uint32_t versionCode,AppPatchType type,bool isPatch)81 void PatchDataMgr::ProcessPatchInfo(const std::string &bundleName, const std::vector<std::string> &installSources,
82 uint32_t versionCode, AppPatchType type, bool isPatch)
83 {
84 if (installSources.empty() || bundleName.empty() || versionCode == 0) {
85 APP_LOGE("ProcessPatchInfo failed");
86 return;
87 }
88 std::string installSource = installSources[0];
89 InnerPatchInfo innerPatchInfo;
90 if (installSource.find(QUICK_FIX_ENGINE_PATH) == 0 || isPatch) {
91 APP_LOGI("ProcessPatchInfo, bundleName: %{public}s", bundleName.c_str());
92 PatchInfo patchInfo;
93 patchInfo.appPatchType = type;
94 patchInfo.versionCode = versionCode;
95 innerPatchInfo.SetPatchInfo(patchInfo);
96 if (!AddInnerPatchInfo(bundleName, innerPatchInfo)) {
97 APP_LOGE("AddInnerPatchInfo failed, bundleName: %{public}s", bundleName.c_str());
98 }
99 return;
100 }
101 if (!GetInnerPatchInfo(bundleName, innerPatchInfo)) {
102 return;
103 }
104 if (innerPatchInfo.GetVersionCode() >= versionCode) {
105 APP_LOGW("patchVersion: %{public}u, newVersion: %{public}u", innerPatchInfo.GetVersionCode(), versionCode);
106 return;
107 }
108 if (!DeleteInnerPatchInfo(bundleName)) {
109 APP_LOGE("DeleteInnerPatchInfo failed, bundleName: %{public}s", bundleName.c_str());
110 return;
111 }
112 }
113 } // namespace AppExecFwk
114 } // namespace OHOS
115