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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_QUICK_FIX_DEPLOYER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_QUICK_FIX_DEPLOYER_H
18 
19 #include "inner_bundle_info.h"
20 #include "ipc/code_signature_param.h"
21 #include "quick_fix_checker.h"
22 #include "quick_fix_data_mgr.h"
23 #include "quick_fix_interface.h"
24 #include "quick_fix/quick_fix_status_callback_interface.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 class QuickFixDeployer final : public IQuickFix {
29 public:
30     QuickFixDeployer(const std::vector<std::string> &bundleFilePaths, bool isDebug = false,
31         const std::string &targetPath = "");
32 
33     virtual ~QuickFixDeployer() = default;
34 
35     virtual ErrCode Execute() override;
36 
37     DeployQuickFixResult GetDeployQuickFixResult() const;
38 
39     ErrCode ToDeployEndStatus(InnerAppQuickFix &newInnerAppQuickFix,
40         const InnerAppQuickFix &oldInnerAppQuickFix);
41 
42 private:
43     ErrCode DeployQuickFix();
44 
45     ErrCode GetQuickFixDataMgr();
46 
47     ErrCode SaveToInnerBundleInfo(const InnerAppQuickFix &newInnerAppQuickFix);
48 
49     ErrCode ToDeployStartStatus(const std::vector<std::string> &bundleFilePaths,
50         InnerAppQuickFix &newInnerAppQuickFix, InnerAppQuickFix &oldInnerAppQuickFix);
51 
52     ErrCode ParseAndCheckAppQuickFixInfos(
53         const std::vector<std::string> &bundleFilePaths,
54         std::unordered_map<std::string, AppQuickFix> &infos);
55 
56     ErrCode ToInnerAppQuickFix(const std::unordered_map<std::string, AppQuickFix> infos,
57         const InnerAppQuickFix &oldInnerAppQuickFix, InnerAppQuickFix &newInnerAppQuickFix);
58 
59     ErrCode GetBundleInfo(const std::string &bundleName, BundleInfo &bundleInfo);
60 
61     ErrCode ProcessPatchDeployStart(
62         const std::vector<std::string> bundleFilePaths,
63         const BundleInfo &bundleInfo,
64         std::unordered_map<std::string, AppQuickFix> &infos);
65 
66     ErrCode ProcessHotReloadDeployStart(const BundleInfo &bundleInfo, const AppQuickFix &appQuickFix);
67 
68     ErrCode ProcessPatchDeployEnd(const AppQuickFix &appQuickFix, std::string &patchPath);
69 
70     ErrCode ProcessHotReloadDeployEnd(const AppQuickFix &appQuickFix, std::string &patchPath);
71 
72     ErrCode CheckPatchVersionCode(
73         const AppQuickFix &newAppQuickFix,
74         const AppQuickFix &oldAppQuickFix);
75 
76     ErrCode SaveAppQuickFix(const InnerAppQuickFix &innerAppQuickFix);
77 
78     ErrCode MoveHqfFiles(InnerAppQuickFix &innerAppQuickFix, const std::string &targetPath);
79 
80     ErrCode ProcessBundleFilePaths(const std::vector<std::string> &bundleFilePaths,
81         std::vector<std::string> &realFilePaths);
82 
83     void ToDeployQuickFixResult(const AppQuickFix &appQuickFix);
84 
85     void ProcessNativeLibraryPath(const std::string &patchPath, InnerAppQuickFix &innerAppQuickFix);
86 
87     void ProcessNativeLibraryPath(
88         const std::string &patchPath, const InnerAppQuickFix &innerAppQuickFix, std::string &nativeLibraryPath);
89 
90     void ResetNativeSoAttrs(std::unordered_map<std::string, AppQuickFix> &infos);
91 
92     void ResetNativeSoAttrs(AppQuickFix &appQuickFix);
93 
94     ErrCode ExtractQuickFixSoFile(const AppQuickFix &appQuickFix,
95         const std::string &hqfSoPath, const BundleInfo &bundleInfo);
96 
97     bool IsLibIsolated(const std::string &bundleName, const std::string &moduleName);
98 
99     bool FetchInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &innerBundleInfo);
100 
101     bool FetchPatchNativeSoAttrs(const AppqfInfo &appqfInfo,
102         const HqfInfo hqfInfo, bool isLibIsolated, std::string &nativeLibraryPath, std::string &cpuAbi);
103 
104     bool HasNativeSoInBundle(const AppQuickFix &appQuickFix);
105 
106     void SendQuickFixSystemEvent(const InnerBundleInfo &innerBundleInfo);
107 
108     ErrCode ExtractSoAndApplyDiff(const AppQuickFix &appQuickFix, const BundleInfo &bundleInfo,
109         const std::string &patchPath);
110 
111     bool ExtractSoFiles(const BundleInfo &bundleInfo, const std::string &moduleName, std::string &tmpSoPath);
112 
113     ErrCode ProcessApplyDiffPatch(const AppQuickFix &appQuickFix, const HqfInfo &hqf,
114         const std::string &oldSoPath, const std::string &patchPath, int32_t uid = Constants::INVALID_UID);
115 
116     bool ExtractEncryptedSoFiles(const BundleInfo &bundleInfo, const std::string &moduleName,
117         int32_t uid, std::string &tmpSoPath);
118 
119     void PrepareCodeSignatureParam(const AppQuickFix &appQuickFix, const HqfInfo &hqf,
120         const BundleInfo &bundleInfo, const std::string &hqfSoPath, CodeSignatureParam &codeSignatureParam);
121 
122     ErrCode VerifyCodeSignatureForHqf(const InnerAppQuickFix &innerAppQuickFix, const std::string &hqfSoPath);
123 
124     ErrCode CheckHqfResourceIsValid(const std::vector<std::string> bundleFilePaths, const BundleInfo &bundleInfo);
125 
126     ErrCode ExtractQuickFixResFile(const AppQuickFix &appQuickFix, const BundleInfo &bundleInfo);
127 
128     std::vector<std::string> patchPaths_;
129     std::shared_ptr<QuickFixDataMgr> quickFixDataMgr_ = nullptr;
130     DeployQuickFixResult deployQuickFixResult_;
131     std::string appDistributionType_ = Constants::APP_DISTRIBUTION_TYPE_NONE;
132     bool isDebug_ = false;
133     std::string targetPath_ = "";
134 
135 #define CHECK_QUICK_FIX_RESULT_RETURN_IF_FAIL(errcode)                           \
136     do {                                                                           \
137         if ((errcode) != ERR_OK) {                                          \
138             return (errcode);                                                          \
139         }                                                                          \
140     } while (0)
141 };
142 } // AppExecFwk
143 } // OHOS
144 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_QUICK_FIX_DEPLOYER_H