• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     uint32_t flags = static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE);
67     flags |= static_cast<uint32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
68     ErrCode ret = dataMgr->GetBundleInfoV9(bundleName, static_cast<int32_t>(flags), bundleInfo, userId);
69     if (ret != ERR_OK) {
70         LOG_W(BMS_TAG_INSTALLER, "getBundleInfo failed,%{public}s,%{public}d", bundleName.c_str(), userId);
71         return Constants::EMPTY_STRING;
72     }
73     for (const auto &hapInfo : bundleInfo.hapModuleInfos) {
74         if (hapInfo.moduleType == ModuleType::ENTRY) {
75             return hapInfo.hapPath;
76         }
77     }
78     LOG_I(BMS_TAG_INSTALLER, "no entry");
79     return Constants::EMPTY_STRING;
80 }
81 
GetUtdProfileFromHap(const std::string & hapPath)82 std::string UtdHandler::GetUtdProfileFromHap(const std::string &hapPath)
83 {
84     if (hapPath.empty()) {
85         LOG_I(BMS_TAG_INSTALLER, "hapPath empty");
86         return Constants::EMPTY_STRING;
87     }
88     BundleExtractor bundleExtractor(hapPath);
89     if (!bundleExtractor.Init()) {
90         LOG_W(BMS_TAG_INSTALLER, "extractor init failed");
91         return Constants::EMPTY_STRING;
92     }
93     if (!bundleExtractor.HasEntry(UTD_PROFILE_PATH)) {
94         LOG_I(BMS_TAG_INSTALLER, "no utd profile");
95         return Constants::EMPTY_STRING;
96     }
97     std::ostringstream utdJsonStream;
98     if (!bundleExtractor.ExtractByName(UTD_PROFILE_PATH, utdJsonStream)) {
99         LOG_W(BMS_TAG_INSTALLER, "extract utd profile failed");
100         return Constants::EMPTY_STRING;
101     }
102     return utdJsonStream.str();
103 }
104 }  // namespace AppExecFwk
105 }  // namespace OHOS
106