1 /*
2 * Copyright (c) 2024 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 "utd_handler.h"
17
18 #include <sstream>
19
20 #include "app_log_tag_wrapper.h"
21 #include "bundle_extractor.h"
22 #include "bundle_mgr_service.h"
23 #ifdef BUNDLE_FRAMEWORK_UDMF_ENABLED
24 #include "utd_client.h"
25 #endif
26
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 constexpr const char* UTD_PROFILE_PATH = "resources/rawfile/arkdata/utd/utd.json5";
31 }
32
InstallUtdAsync(const std::string & bundleName,const int32_t userId)33 void UtdHandler::InstallUtdAsync(const std::string &bundleName, const int32_t userId)
34 {
35 #ifdef BUNDLE_FRAMEWORK_UDMF_ENABLED
36 auto installUtdTask = [bundleName, userId]() {
37 LOG_I(BMS_TAG_INSTALLER, "install utd,%{public}s,%{public}d", bundleName.c_str(), userId);
38 std::string entryHapPath = UtdHandler::GetEntryHapPath(bundleName, userId);
39 std::string utdProfile = UtdHandler::GetUtdProfileFromHap(entryHapPath);
40 LOG_I(BMS_TAG_INSTALLER, "utdProfile:%{public}zu", utdProfile.size());
41 UDMF::UtdClient::GetInstance().InstallCustomUtds(bundleName, utdProfile, userId);
42 };
43 ffrt::submit(installUtdTask);
44 #endif
45 }
46
UninstallUtdAsync(const std::string & bundleName,const int32_t userId)47 void UtdHandler::UninstallUtdAsync(const std::string &bundleName, const int32_t userId)
48 {
49 #ifdef BUNDLE_FRAMEWORK_UDMF_ENABLED
50 auto uninstallUtdTask = [bundleName, userId]() {
51 LOG_I(BMS_TAG_INSTALLER, "uninstall utd,%{public}s,%{public}d", bundleName.c_str(), userId);
52 UDMF::UtdClient::GetInstance().UninstallCustomUtds(bundleName, userId);
53 };
54 ffrt::submit(uninstallUtdTask);
55 #endif
56 }
57
GetEntryHapPath(const std::string & bundleName,const int32_t userId)58 std::string UtdHandler::GetEntryHapPath(const std::string &bundleName, const int32_t userId)
59 {
60 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
61 if (dataMgr == nullptr) {
62 LOG_W(BMS_TAG_INSTALLER, "dataMgr is null");
63 return Constants::EMPTY_STRING;
64 }
65 BundleInfo bundleInfo;
66 int32_t flags = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE);
67 ErrCode ret = dataMgr->GetBundleInfoV9(bundleName, flags, bundleInfo, userId);
68 if (ret != ERR_OK) {
69 LOG_W(BMS_TAG_INSTALLER, "getBundleInfo failed,%{public}s,%{public}d", bundleName.c_str(), userId);
70 return Constants::EMPTY_STRING;
71 }
72 for (const auto &hapInfo : bundleInfo.hapModuleInfos) {
73 if (hapInfo.moduleType == ModuleType::ENTRY) {
74 return hapInfo.hapPath;
75 }
76 }
77 LOG_I(BMS_TAG_INSTALLER, "no entry");
78 return Constants::EMPTY_STRING;
79 }
80
GetUtdProfileFromHap(const std::string & hapPath)81 std::string UtdHandler::GetUtdProfileFromHap(const std::string &hapPath)
82 {
83 if (hapPath.empty()) {
84 LOG_I(BMS_TAG_INSTALLER, "hapPath empty");
85 return Constants::EMPTY_STRING;
86 }
87 BundleExtractor bundleExtractor(hapPath);
88 if (!bundleExtractor.Init()) {
89 LOG_W(BMS_TAG_INSTALLER, "extractor init failed");
90 return Constants::EMPTY_STRING;
91 }
92 if (!bundleExtractor.HasEntry(UTD_PROFILE_PATH)) {
93 LOG_I(BMS_TAG_INSTALLER, "no utd profile");
94 return Constants::EMPTY_STRING;
95 }
96 std::ostringstream utdJsonStream;
97 if (!bundleExtractor.ExtractByName(UTD_PROFILE_PATH, utdJsonStream)) {
98 LOG_W(BMS_TAG_INSTALLER, "extract utd profile failed");
99 return Constants::EMPTY_STRING;
100 }
101 return utdJsonStream.str();
102 }
103 } // namespace AppExecFwk
104 } // namespace OHOS
105