1 /*
2 * Copyright (c) 2025 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 #define LOG_TAG "SchemaHelper"
17 #include "get_schema_helper.h"
18
19 #include "bundle_mgr_interface.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "log_print.h"
23 #include "resource_manager.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace DistributedData {
28 using namespace OHOS::Global::Resource;
29
GetBundleMgr()30 sptr<AppExecFwk::IBundleMgr> GetSchemaHelper::GetBundleMgr()
31 {
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (object_ != nullptr) {
34 return iface_cast<AppExecFwk::IBundleMgr>(object_);
35 }
36 sptr<ISystemAbilityManager> systemAbilityManager =
37 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38 if (systemAbilityManager == nullptr) {
39 ZLOGE("Failed to get system ability mgr.");
40 return nullptr;
41 }
42 object_ = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
43 if (object_ == nullptr) {
44 ZLOGE("BMS service not ready to complete.");
45 return nullptr;
46 }
47 deathRecipient_ = new (std::nothrow) GetSchemaHelper::ServiceDeathRecipient(weak_from_this());
48 if (deathRecipient_ == nullptr) {
49 ZLOGE("deathRecipient alloc failed.");
50 object_ = nullptr;
51 return nullptr;
52 }
53 if (!object_->AddDeathRecipient(deathRecipient_)) {
54 ZLOGE("add death recipient failed.");
55 object_ = nullptr;
56 deathRecipient_ = nullptr;
57 return nullptr;
58 }
59 return iface_cast<AppExecFwk::IBundleMgr>(object_);
60 }
GetSchemaFromHap(const std::string & schemaPath,const AppInfo & info)61 std::vector<std::string> GetSchemaHelper::GetSchemaFromHap(const std::string &schemaPath, const AppInfo &info)
62 {
63 std::vector<std::string> schemas;
64 auto bmsClient = GetBundleMgr();
65 if (bmsClient == nullptr) {
66 ZLOGE("GetBundleMgr is nullptr!");
67 return schemas;
68 }
69 OHOS::AppExecFwk::BundleInfo bundleInfo;
70 int32_t flag = static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE);
71 auto ret = bmsClient->GetCloneBundleInfo(info.bundleName, flag, info.appIndex, bundleInfo, info.userId);
72 if (ret != ERR_OK) {
73 ZLOGE("GetCloneBundleInfo failed. errCode:%{public}d", ret);
74 return schemas;
75 }
76
77 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
78 if (resMgr == nullptr) {
79 ZLOGE("resMgr is nullptr.");
80 return schemas;
81 }
82 for (auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
83 resMgr->AddResource(hapModuleInfo.hapPath.c_str());
84 size_t length = 0;
85 std::unique_ptr<uint8_t[]> fileContent;
86 auto ret = resMgr->GetRawFileFromHap(schemaPath, length, fileContent);
87 if (ret != ERR_OK) {
88 ZLOGD("GetRawFileFromHap failed. bundleName:%{public}s ret:%{public}d", info.bundleName.c_str(), ret);
89 continue;
90 }
91 std::string schema(fileContent.get(), fileContent.get() + length);
92 schemas.emplace_back(std::move(schema));
93 }
94 return schemas;
95 }
OnRemoteDied()96 void GetSchemaHelper::OnRemoteDied()
97 {
98 std::lock_guard<std::mutex> lock(mutex_);
99 ZLOGE("remote object died, object=null ? %{public}s.", object_ == nullptr ? "true" : "false");
100 if (object_ != nullptr) {
101 object_->RemoveDeathRecipient(deathRecipient_);
102 }
103 object_ = nullptr;
104 deathRecipient_ = nullptr;
105 }
106
~GetSchemaHelper()107 GetSchemaHelper::~GetSchemaHelper()
108 {
109 std::lock_guard<std::mutex> lock(mutex_);
110 if (object_ != nullptr) {
111 object_->RemoveDeathRecipient(deathRecipient_);
112 }
113 }
114
GetInstance()115 GetSchemaHelper &GetSchemaHelper::GetInstance()
116 {
117 static GetSchemaHelper helper;
118 return helper;
119 }
120 } // namespace DistributedData
121 } // namespace OHOS