1 /*
2 * Copyright (c) 2023 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 "module_external/bms_adapter.h"
17
18 #include <refbase.h>
19
20 #include "b_error/b_error.h"
21 #include "b_file_info.h"
22 #include "b_json/b_json_entity_extension_config.h"
23 #include "b_resources/b_constants.h"
24 #include "bundle_mgr_client.h"
25 #include "filemgmt_libhilog.h"
26 #include "install_param.h"
27 #include "iservice_registry.h"
28 #include "module_external/inner_receiver_impl.h"
29 #include "module_external/sms_adapter.h"
30 #include "module_ipc/service.h"
31 #include "module_ipc/svc_session_manager.h"
32 #include "module_sched/sched_scheduler.h"
33 #include "status_receiver_host.h"
34 #include "system_ability_definition.h"
35
36 namespace OHOS::FileManagement::Backup {
37 using namespace std;
38
GetBundleManager()39 static sptr<AppExecFwk::IBundleMgr> GetBundleManager()
40 {
41 auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42 if (saMgr == nullptr) {
43 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get system ability manager");
44 }
45
46 auto bundleObj = saMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
47 if (bundleObj == nullptr) {
48 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle manager service");
49 }
50
51 return iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
52 }
53
GetAllowAndExtName(const vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)54 static tuple<bool, string> GetAllowAndExtName(const vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
55 {
56 for (auto &&ext : extensionInfos) {
57 if (ext.type != AppExecFwk::ExtensionAbilityType::BACKUP) {
58 continue;
59 }
60 vector<string> out;
61 AppExecFwk::BundleMgrClient client;
62 if (!client.GetResConfigFile(ext, "ohos.extension.backup", out) || out.size() == 0) {
63 throw BError(BError::Codes::SA_INVAL_ARG, "Failed to get resconfigfile of bundle " + ext.bundleName);
64 }
65 BJsonCachedEntity<BJsonEntityExtensionConfig> cachedEntity(out[0], ext.bundleName);
66 auto cache = cachedEntity.Structuralize();
67 return {cache.GetAllowToBackupRestore(), ext.name};
68 }
69 return {false, ""};
70 }
71
GetBundleInfos(int32_t userId)72 vector<BJsonEntityCaps::BundleInfo> BundleMgrAdapter::GetBundleInfos(int32_t userId)
73 {
74 vector<BJsonEntityCaps::BundleInfo> bundleInfos;
75 vector<AppExecFwk::BundleInfo> installedBundles;
76 auto bms = GetBundleManager();
77 if (!bms->GetBundleInfos(AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundles, userId)) {
78 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle infos");
79 }
80 for (auto const &installedBundle : installedBundles) {
81 auto [allToBackup, extName] = GetAllowAndExtName(installedBundle.extensionInfos);
82 auto bundleStats = StorageMgrAdapter::GetBundleStats(installedBundle.name);
83 bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {installedBundle.name, installedBundle.versionCode,
84 installedBundle.versionName, bundleStats.dataSize_,
85 allToBackup, extName});
86 }
87 return bundleInfos;
88 }
89
GetBundleInfos(const vector<string> & bundleNames,int32_t userId)90 vector<BJsonEntityCaps::BundleInfo> BundleMgrAdapter::GetBundleInfos(const vector<string> &bundleNames, int32_t userId)
91 {
92 vector<BJsonEntityCaps::BundleInfo> bundleInfos;
93 auto bms = GetBundleManager();
94 for (auto const &bundleName : bundleNames) {
95 AppExecFwk::BundleInfo installedBundle;
96 if (!bms->GetBundleInfo(bundleName, AppExecFwk::GET_BUNDLE_WITH_EXTENSION_INFO, installedBundle, userId)) {
97 throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle info");
98 }
99 auto [allToBackup, extName] = GetAllowAndExtName(installedBundle.extensionInfos);
100 auto bundleStats = StorageMgrAdapter::GetBundleStats(installedBundle.name);
101 bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {installedBundle.name, installedBundle.versionCode,
102 installedBundle.versionName, bundleStats.dataSize_,
103 allToBackup, extName});
104 }
105 return bundleInfos;
106 }
107
Install(wptr<InnerReceiverImpl> statusReceiver,const string & bundleFilePath,int32_t userId)108 ErrCode BundleMgrAdapter::Install(wptr<InnerReceiverImpl> statusReceiver, const string &bundleFilePath, int32_t userId)
109 {
110 HILOGI("Begin");
111 auto bms = GetBundleManager();
112 AppExecFwk::BundleInfo bundleInfo;
113 if (!bms->GetBundleArchiveInfo(bundleFilePath, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo)) {
114 return BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle archive info").GetCode();
115 }
116 auto receiver = statusReceiver.promote();
117 if (receiver == nullptr) {
118 return BError(BError::Codes::SA_BROKEN_IPC, "Failed to get receiver").GetCode();
119 }
120 // check bundle name
121 if (bundleInfo.name != receiver->GetBundleName()) {
122 return BError(BError::Codes::SA_INVAL_ARG, "Bundle name is not match").GetCode();
123 }
124
125 auto install = bms->GetBundleInstaller();
126 if (!install) {
127 return BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle installer").GetCode();
128 }
129
130 AppExecFwk::InstallParam installParam;
131 installParam.installFlag = AppExecFwk::InstallFlag::REPLACE_EXISTING;
132 installParam.userId = userId;
133 return install->StreamInstall({bundleFilePath}, installParam, receiver);
134 }
135 } // namespace OHOS::FileManagement::Backup
136