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_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_IMPL_H
17 #define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_IMPL_H
18 
19 #include <shared_mutex>
20 
21 #include "appexecfwk_errors.h"
22 #include "application_info.h"
23 #include "bundle_dir.h"
24 #include "bundle_info.h"
25 #include "bundle_pack_info.h"
26 #include "bundle_mgr_interface.h"
27 #include "extension_ability_info.h"
28 #include "hap_module_info.h"
29 #ifdef GLOBAL_RESMGR_ENABLE
30 #include "resource_manager.h"
31 #endif
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 using Want = OHOS::AAFwk::Want;
36 
37 class BundleMgrClientImpl : public std::enable_shared_from_this<BundleMgrClientImpl> {
38 public:
39     BundleMgrClientImpl();
40     virtual ~BundleMgrClientImpl();
41 
42     ErrCode GetNameForUid(const int uid, std::string &name);
43     bool GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId);
44     ErrCode GetBundlePackInfo(
45         const std::string &bundleName, const BundlePackFlag flag, BundlePackInfo &bundlePackInfo, int32_t userId);
46     bool GetHapModuleInfo(const std::string &bundleName, const std::string &hapName, HapModuleInfo &hapModuleInfo);
47     bool GetResConfigFile(const HapModuleInfo &hapModuleInfo, const std::string &metadataName,
48         std::vector<std::string> &profileInfos) const;
49     bool GetResConfigFile(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName,
50         std::vector<std::string> &profileInfos) const;
51     bool GetResConfigFile(const AbilityInfo &abilityInfo, const std::string &metadataName,
52         std::vector<std::string> &profileInfos) const;
53     ErrCode InstallSandboxApp(const std::string &bundleName, int32_t dlpType, int32_t userId, int32_t &appIndex);
54     ErrCode UninstallSandboxApp(const std::string &bundleName, int32_t appIndex, int32_t userId);
55     ErrCode GetSandboxBundleInfo(const std::string &bundleName, int32_t appIndex, int32_t userId, BundleInfo &info);
56     bool GetProfileFromExtension(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName,
57         std::vector<std::string> &profileInfos) const;
58     bool GetProfileFromAbility(const AbilityInfo &abilityInfo, const std::string &metadataName,
59         std::vector<std::string> &profileInfos) const;
60     bool GetProfileFromHap(const HapModuleInfo &hapModuleInfo, const std::string &metadataName,
61         std::vector<std::string> &profileInfos) const;
62     ErrCode GetSandboxAbilityInfo(const Want &want, int32_t appIndex, int32_t flags, int32_t userId,
63         AbilityInfo &abilityInfo);
64     ErrCode GetSandboxExtAbilityInfos(const Want &want, int32_t appIndex, int32_t flags, int32_t userId,
65         std::vector<ExtensionAbilityInfo> &extensionInfos);
66     ErrCode GetSandboxHapModuleInfo(const AbilityInfo &abilityInfo, int32_t appIndex, int32_t userId,
67         HapModuleInfo &hapModuleInfo);
68     ErrCode CreateBundleDataDir(int32_t userId);
69     ErrCode GetDirByBundleNameAndAppIndex(const std::string &bundleName, const int32_t appIndex, std::string &dataDir);
70     ErrCode GetAllBundleDirs(int32_t userId, std::vector<BundleDir> &bundleDirs);
71 
72 private:
73     ErrCode Connect();
74     void OnDeath();
75     bool ConvertResourcePath(const std::string &bundleName, std::string &resPath, bool isCompressed) const;
76     bool GetResProfileByMetadata(const std::vector<Metadata> &metadata, const std::string &metadataName,
77         const std ::string &resourcePath, bool isCompressed, std::vector<std::string> &profileInfos) const;
78 #ifdef GLOBAL_RESMGR_ENABLE
79     std::shared_ptr<Global::Resource::ResourceManager> InitResMgr(const std::string &resourcePath) const;
80     bool GetResFromResMgr(const std::string &resName, const std::shared_ptr<Global::Resource::ResourceManager> &resMgr,
81         bool isCompressed, std::vector<std::string> &profileInfos) const;
82 #endif
83     bool IsFileExisted(const std::string &filePath) const;
84     bool TransformFileToJsonString(const std::string &resPath, std::string &profile) const;
85 
86 private:
87     std::shared_mutex mutex_;
88     sptr<IBundleMgr> bundleMgr_;
89     sptr<IBundleInstaller> bundleInstaller_;
90     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
91 };
92 }  // namespace AppExecFwk
93 }  // namespace OHOS
94 #endif  // FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H