1600cc4afSopenharmony_ci/*
2600cc4afSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3600cc4afSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4600cc4afSopenharmony_ci * you may not use this file except in compliance with the License.
5600cc4afSopenharmony_ci * You may obtain a copy of the License at
6600cc4afSopenharmony_ci *
7600cc4afSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8600cc4afSopenharmony_ci *
9600cc4afSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10600cc4afSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11600cc4afSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12600cc4afSopenharmony_ci * See the License for the specific language governing permissions and
13600cc4afSopenharmony_ci * limitations under the License.
14600cc4afSopenharmony_ci */
15600cc4afSopenharmony_ci
16600cc4afSopenharmony_ci#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H
17600cc4afSopenharmony_ci#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H
18600cc4afSopenharmony_ci
19600cc4afSopenharmony_ci#include <condition_variable>
20600cc4afSopenharmony_ci#include <memory>
21600cc4afSopenharmony_ci#include <mutex>
22600cc4afSopenharmony_ci#include <shared_mutex>
23600cc4afSopenharmony_ci#include <string>
24600cc4afSopenharmony_ci
25600cc4afSopenharmony_ci#include "nocopyable.h"
26600cc4afSopenharmony_ci#include "singleton.h"
27600cc4afSopenharmony_ci
28600cc4afSopenharmony_ci#include "app_log_wrapper.h"
29600cc4afSopenharmony_ci#include "appexecfwk_errors.h"
30600cc4afSopenharmony_ci#include "bundle_constants.h"
31600cc4afSopenharmony_ci#include "ipc/installd_interface.h"
32600cc4afSopenharmony_ci
33600cc4afSopenharmony_cinamespace OHOS {
34600cc4afSopenharmony_cinamespace AppExecFwk {
35600cc4afSopenharmony_ciclass InstalldClient : public DelayedSingleton<InstalldClient> {
36600cc4afSopenharmony_cipublic:
37600cc4afSopenharmony_ci    /**
38600cc4afSopenharmony_ci     * @brief Create a bundle code directory through an installd proxy object.
39600cc4afSopenharmony_ci     * @param bundleDir Indicates the bundle code directory path that to be created.
40600cc4afSopenharmony_ci     * @return Returns ERR_OK if the bundle directory created successfully; returns error code otherwise.
41600cc4afSopenharmony_ci     */
42600cc4afSopenharmony_ci    ErrCode CreateBundleDir(const std::string &bundleDir);
43600cc4afSopenharmony_ci    /**
44600cc4afSopenharmony_ci     * @brief Extract the files of a HAP module to the code directory.
45600cc4afSopenharmony_ci     * @param srcModulePath Indicates the HAP file path.
46600cc4afSopenharmony_ci     * @param targetPath normal files decompression path.
47600cc4afSopenharmony_ci     * @param targetSoPath so files decompression path.
48600cc4afSopenharmony_ci     * @param cpuAbi cpuAbi.
49600cc4afSopenharmony_ci     * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise.
50600cc4afSopenharmony_ci     */
51600cc4afSopenharmony_ci    ErrCode ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
52600cc4afSopenharmony_ci        const std::string &targetSoPath, const std::string &cpuAbi);
53600cc4afSopenharmony_ci    /**
54600cc4afSopenharmony_ci     * @brief Rename the module directory from temporaily path to the real path.
55600cc4afSopenharmony_ci     * @param oldPath Indicates the old path name.
56600cc4afSopenharmony_ci     * @param newPath Indicates the new path name.
57600cc4afSopenharmony_ci     * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise.
58600cc4afSopenharmony_ci     */
59600cc4afSopenharmony_ci    ErrCode RenameModuleDir(const std::string &oldPath, const std::string &newPath);
60600cc4afSopenharmony_ci    /**
61600cc4afSopenharmony_ci     * @brief Create a bundle data directory.
62600cc4afSopenharmony_ci     * @param createDirParam Indicates param to be set to the directory.
63600cc4afSopenharmony_ci     * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise.
64600cc4afSopenharmony_ci     */
65600cc4afSopenharmony_ci    ErrCode CreateBundleDataDir(const CreateDirParam &createDirParam);
66600cc4afSopenharmony_ci
67600cc4afSopenharmony_ci    ErrCode CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams);
68600cc4afSopenharmony_ci
69600cc4afSopenharmony_ci    /**
70600cc4afSopenharmony_ci     * @brief Remove a bundle data directory.
71600cc4afSopenharmony_ci     * @param bundleName Indicates the bundleName data directory path that to be created.
72600cc4afSopenharmony_ci     * @param userid Indicates userid to be set to the directory.
73600cc4afSopenharmony_ci     * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise.
74600cc4afSopenharmony_ci     */
75600cc4afSopenharmony_ci    ErrCode RemoveBundleDataDir(const std::string &bundleName, const int32_t userId, bool isAtomicService = false);
76600cc4afSopenharmony_ci    /**
77600cc4afSopenharmony_ci     * @brief Remove a module data directory.
78600cc4afSopenharmony_ci     * @param ModuleDir Indicates the module data directory path that to be created.
79600cc4afSopenharmony_ci     * @param userid Indicates userid to be set to the directory.
80600cc4afSopenharmony_ci     * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise.
81600cc4afSopenharmony_ci     */
82600cc4afSopenharmony_ci    ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid);
83600cc4afSopenharmony_ci    /**
84600cc4afSopenharmony_ci     * @brief Remove a directory.
85600cc4afSopenharmony_ci     * @param dir Indicates the directory path that to be removed.
86600cc4afSopenharmony_ci     * @return Returns ERR_OK if the  directory removed successfully; returns error code otherwise.
87600cc4afSopenharmony_ci     */
88600cc4afSopenharmony_ci    ErrCode RemoveDir(const std::string &dir);
89600cc4afSopenharmony_ci    /**
90600cc4afSopenharmony_ci     * @brief Get disk usage for dir.
91600cc4afSopenharmony_ci     * @param dir Indicates the directory.
92600cc4afSopenharmony_ci     * @param isRealPath Indicates isRealPath.
93600cc4afSopenharmony_ci     * @return Returns true if successfully; returns false otherwise.
94600cc4afSopenharmony_ci     */
95600cc4afSopenharmony_ci    int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false);
96600cc4afSopenharmony_ci    /**
97600cc4afSopenharmony_ci     * @brief Clean all files in a bundle data directory.
98600cc4afSopenharmony_ci     * @param bundleDir Indicates the data directory path that to be cleaned.
99600cc4afSopenharmony_ci     * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise.
100600cc4afSopenharmony_ci     */
101600cc4afSopenharmony_ci    ErrCode CleanBundleDataDir(const std::string &bundleDir);
102600cc4afSopenharmony_ci    /**
103600cc4afSopenharmony_ci     * @brief Clean a bundle data directory.
104600cc4afSopenharmony_ci     * @param bundleName Indicates the bundleName data directory path that to be cleaned.
105600cc4afSopenharmony_ci     * @param userid Indicates userid to be set to the directory.
106600cc4afSopenharmony_ci     * @param appIndex Indicates app index to be set to the directory.
107600cc4afSopenharmony_ci     * @return Returns ERR_OK if the bundle data directory cleaned successfully; returns error code otherwise.
108600cc4afSopenharmony_ci     */
109600cc4afSopenharmony_ci    ErrCode CleanBundleDataDirByName(const std::string &bundleName, const int userid, const int appIndex = 0);
110600cc4afSopenharmony_ci    /**
111600cc4afSopenharmony_ci     * @brief Get bundle Stats.
112600cc4afSopenharmony_ci     * @param bundleName Indicates the bundle name.
113600cc4afSopenharmony_ci     * @param userId Indicates the user Id.
114600cc4afSopenharmony_ci     * @param bundleStats Indicates the bundle Stats.
115600cc4afSopenharmony_ci     * @return Returns ERR_OK if get stats successfully; returns error code otherwise.
116600cc4afSopenharmony_ci     */
117600cc4afSopenharmony_ci    ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId,
118600cc4afSopenharmony_ci        std::vector<int64_t> &bundleStats, const int32_t uid = Constants::INVALID_UID, const int32_t appIndex = 0);
119600cc4afSopenharmony_ci
120600cc4afSopenharmony_ci    ErrCode GetAllBundleStats(const int32_t userId,
121600cc4afSopenharmony_ci        std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids);
122600cc4afSopenharmony_ci
123600cc4afSopenharmony_ci    /**
124600cc4afSopenharmony_ci     * @brief Reset the installd proxy object when installd service died.
125600cc4afSopenharmony_ci     * @return
126600cc4afSopenharmony_ci     */
127600cc4afSopenharmony_ci    void ResetInstalldProxy();
128600cc4afSopenharmony_ci
129600cc4afSopenharmony_ci    /**
130600cc4afSopenharmony_ci     * @brief Set dir apl.
131600cc4afSopenharmony_ci     * @param dir Indicates the data dir.
132600cc4afSopenharmony_ci     * @param bundleName Indicates the bundle name.
133600cc4afSopenharmony_ci     * @param apl Indicates the apl type.
134600cc4afSopenharmony_ci     * @param isPreInstallApp Indicates the bundle install type.
135600cc4afSopenharmony_ci     * @param debug Indicates the bundle debug mode.
136600cc4afSopenharmony_ci     * @return Returns ERR_OK if set apl successfully; returns error code otherwise.
137600cc4afSopenharmony_ci     */
138600cc4afSopenharmony_ci    ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl,
139600cc4afSopenharmony_ci        bool isPreInstallApp, bool debug);
140600cc4afSopenharmony_ci
141600cc4afSopenharmony_ci    /**
142600cc4afSopenharmony_ci     * @brief Get all cache file path.
143600cc4afSopenharmony_ci     * @param dir Indicates the data dir.
144600cc4afSopenharmony_ci     * @param cachesPath Indicates the cache file path.
145600cc4afSopenharmony_ci     * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise.
146600cc4afSopenharmony_ci     */
147600cc4afSopenharmony_ci    ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath);
148600cc4afSopenharmony_ci
149600cc4afSopenharmony_ci    ErrCode ScanDir(
150600cc4afSopenharmony_ci        const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths);
151600cc4afSopenharmony_ci
152600cc4afSopenharmony_ci    ErrCode MoveFile(const std::string &oldPath, const std::string &newPath);
153600cc4afSopenharmony_ci
154600cc4afSopenharmony_ci    ErrCode CopyFile(const std::string &oldPath, const std::string &newPath,
155600cc4afSopenharmony_ci        const std::string &signatureFilePath = "");
156600cc4afSopenharmony_ci
157600cc4afSopenharmony_ci    ErrCode Mkdir(
158600cc4afSopenharmony_ci        const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid);
159600cc4afSopenharmony_ci
160600cc4afSopenharmony_ci    ErrCode GetFileStat(const std::string &file, FileStat &fileStat);
161600cc4afSopenharmony_ci
162600cc4afSopenharmony_ci    ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
163600cc4afSopenharmony_ci        const std::string &cpuAbi);
164600cc4afSopenharmony_ci
165600cc4afSopenharmony_ci    ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
166600cc4afSopenharmony_ci        const std::string &newSoPath, int32_t uid = Constants::INVALID_UID);
167600cc4afSopenharmony_ci
168600cc4afSopenharmony_ci    ErrCode IsExistDir(const std::string &dir, bool &isExist);
169600cc4afSopenharmony_ci
170600cc4afSopenharmony_ci    ErrCode IsExistFile(const std::string &path, bool &isExist);
171600cc4afSopenharmony_ci
172600cc4afSopenharmony_ci    ErrCode IsExistApFile(const std::string &path, bool &isExist);
173600cc4afSopenharmony_ci
174600cc4afSopenharmony_ci    ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty);
175600cc4afSopenharmony_ci
176600cc4afSopenharmony_ci    ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec);
177600cc4afSopenharmony_ci
178600cc4afSopenharmony_ci    ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir);
179600cc4afSopenharmony_ci
180600cc4afSopenharmony_ci    ErrCode ExtractFiles(const ExtractParam &extractParam);
181600cc4afSopenharmony_ci
182600cc4afSopenharmony_ci    ErrCode ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam);
183600cc4afSopenharmony_ci
184600cc4afSopenharmony_ci    ErrCode ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath,
185600cc4afSopenharmony_ci        const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName);
186600cc4afSopenharmony_ci
187600cc4afSopenharmony_ci    ErrCode ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName);
188600cc4afSopenharmony_ci
189600cc4afSopenharmony_ci    ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
190600cc4afSopenharmony_ci        std::vector<std::string> &fileNames);
191600cc4afSopenharmony_ci
192600cc4afSopenharmony_ci    ErrCode ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData);
193600cc4afSopenharmony_ci
194600cc4afSopenharmony_ci    ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData);
195600cc4afSopenharmony_ci
196600cc4afSopenharmony_ci    ErrCode StopAOT();
197600cc4afSopenharmony_ci
198600cc4afSopenharmony_ci    ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam);
199600cc4afSopenharmony_ci
200600cc4afSopenharmony_ci    ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam);
201600cc4afSopenharmony_ci
202600cc4afSopenharmony_ci    ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption);
203600cc4afSopenharmony_ci
204600cc4afSopenharmony_ci    ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir);
205600cc4afSopenharmony_ci
206600cc4afSopenharmony_ci    ErrCode ExtractDriverSoFiles(const std::string &srcPath,
207600cc4afSopenharmony_ci        const std::unordered_multimap<std::string, std::string> &dirMap);
208600cc4afSopenharmony_ci
209600cc4afSopenharmony_ci    void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
210600cc4afSopenharmony_ci
211600cc4afSopenharmony_ci    void OnLoadSystemAbilityFail();
212600cc4afSopenharmony_ci
213600cc4afSopenharmony_ci    bool StartInstalldService();
214600cc4afSopenharmony_ci
215600cc4afSopenharmony_ci    ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath,
216600cc4afSopenharmony_ci        const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid);
217600cc4afSopenharmony_ci
218600cc4afSopenharmony_ci    ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength,
219600cc4afSopenharmony_ci        const unsigned char *profileBlock);
220600cc4afSopenharmony_ci
221600cc4afSopenharmony_ci    ErrCode RemoveSignProfile(const std::string &bundleName);
222600cc4afSopenharmony_ci
223600cc4afSopenharmony_ci    ErrCode SetEncryptionPolicy(int32_t uid, const std::string &bundleName,
224600cc4afSopenharmony_ci        const int32_t userId, std::string &keyId);
225600cc4afSopenharmony_ci
226600cc4afSopenharmony_ci    ErrCode DeleteEncryptionKeyId(const std::string &bundleName, const int32_t userId);
227600cc4afSopenharmony_ci
228600cc4afSopenharmony_ci    ErrCode RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs);
229600cc4afSopenharmony_ci
230600cc4afSopenharmony_ci    ErrCode IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist);
231600cc4afSopenharmony_ci
232600cc4afSopenharmony_ci    ErrCode CreateExtensionDataDir(const CreateDirParam &createDirParam);
233600cc4afSopenharmony_ci
234600cc4afSopenharmony_ci    ErrCode GetExtensionSandboxTypeList(std::vector<std::string> &typeList);
235600cc4afSopenharmony_ci
236600cc4afSopenharmony_ci    ErrCode AddUserDirDeleteDfx(int32_t userId);
237600cc4afSopenharmony_ci
238600cc4afSopenharmony_ci    ErrCode MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath);
239600cc4afSopenharmony_ci
240600cc4afSopenharmony_ciprivate:
241600cc4afSopenharmony_ci    sptr<IInstalld> GetInstalldProxy();
242600cc4afSopenharmony_ci    bool LoadInstalldService();
243600cc4afSopenharmony_ci
244600cc4afSopenharmony_ci    template<typename F, typename... Args>
245600cc4afSopenharmony_ci    ErrCode CallService(F func, Args&&... args)
246600cc4afSopenharmony_ci    {
247600cc4afSopenharmony_ci        int32_t maxRetryTimes = 2;
248600cc4afSopenharmony_ci        ErrCode errCode = ERR_APPEXECFWK_INSTALLD_SERVICE_DIED;
249600cc4afSopenharmony_ci        for (int32_t retryTimes = 0; retryTimes < maxRetryTimes; retryTimes++) {
250600cc4afSopenharmony_ci            auto proxy = GetInstalldProxy();
251600cc4afSopenharmony_ci            if (proxy == nullptr) {
252600cc4afSopenharmony_ci                return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
253600cc4afSopenharmony_ci            }
254600cc4afSopenharmony_ci            errCode = (proxy->*func)(std::forward<Args>(args)...);
255600cc4afSopenharmony_ci            if (errCode == ERR_APPEXECFWK_INSTALLD_SERVICE_DIED) {
256600cc4afSopenharmony_ci                APP_LOGE("CallService failed, retry times: %{public}d", retryTimes + 1);
257600cc4afSopenharmony_ci                ResetInstalldProxy();
258600cc4afSopenharmony_ci            } else {
259600cc4afSopenharmony_ci                return errCode;
260600cc4afSopenharmony_ci            }
261600cc4afSopenharmony_ci        }
262600cc4afSopenharmony_ci        return errCode;
263600cc4afSopenharmony_ci    }
264600cc4afSopenharmony_ci
265600cc4afSopenharmony_ciprivate:
266600cc4afSopenharmony_ci    bool loadSaFinished_;
267600cc4afSopenharmony_ci    std::mutex mutex_;
268600cc4afSopenharmony_ci    std::mutex loadSaMutex_;
269600cc4afSopenharmony_ci    std::mutex getProxyMutex_;
270600cc4afSopenharmony_ci    std::condition_variable loadSaCondition_;
271600cc4afSopenharmony_ci    sptr<IInstalld> installdProxy_;
272600cc4afSopenharmony_ci    sptr<IRemoteObject::DeathRecipient> recipient_;
273600cc4afSopenharmony_ci};
274600cc4afSopenharmony_ci}  // namespace AppExecFwk
275600cc4afSopenharmony_ci}  // namespace OHOS
276600cc4afSopenharmony_ci#endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H