1 /*
2  * Copyright (c) 2023-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 #include "service_router_data_mgr.h"
16 
17 #include "bundle_info_resolve_util.h"
18 #include "bundle_mgr_helper.h"
19 #include "hilog_tag_wrapper.h"
20 #include "iservice_registry.h"
21 #include "sr_constants.h"
22 #include "sr_samgr_helper.h"
23 #include "system_ability_definition.h"
24 #include "uri.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 namespace {
29 const std::string SCHEME_SEPARATOR = "://";
30 const std::string SCHEME_SERVICE_ROUTER = "servicerouter";
31 }
32 
LoadAllBundleInfos()33 bool ServiceRouterDataMgr::LoadAllBundleInfos()
34 {
35     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
36     ClearAllBundleInfos();
37     auto bundleMgrHelper = DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
38     if (bundleMgrHelper == nullptr) {
39         TAG_LOGE(AAFwkTag::SER_ROUTER, "null bundleMgrHelper");
40         return false;
41     }
42     auto flags = (BundleFlag::GET_BUNDLE_WITH_ABILITIES | BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO);
43     std::vector<BundleInfo> bundleInfos;
44     if (!bundleMgrHelper->GetBundleInfos(flags, bundleInfos, SrSamgrHelper::GetCurrentActiveUserId())) {
45         TAG_LOGE(AAFwkTag::SER_ROUTER, "return");
46         return false;
47     }
48 
49     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
50     for (const auto &bundleInfo : bundleInfos) {
51         UpdateBundleInfoLocked(bundleInfo);
52     }
53     return true;
54 }
55 
LoadBundleInfo(const std::string & bundleName)56 bool ServiceRouterDataMgr::LoadBundleInfo(const std::string &bundleName)
57 {
58     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
59     auto bundleMgrHelper = DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
60     if (bundleMgrHelper == nullptr) {
61         TAG_LOGI(AAFwkTag::SER_ROUTER, "null bundleMgrHelper");
62         return false;
63     }
64     BundleInfo bundleInfo;
65     auto flags = (BundleFlag::GET_BUNDLE_WITH_ABILITIES | BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO);
66     if (!bundleMgrHelper->GetBundleInfo(bundleName, flags, bundleInfo, SrSamgrHelper::GetCurrentActiveUserId())) {
67         TAG_LOGE(AAFwkTag::SER_ROUTER, "return");
68         return false;
69     }
70 
71     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
72     UpdateBundleInfoLocked(bundleInfo);
73     return true;
74 }
75 
UpdateBundleInfoLocked(const BundleInfo & bundleInfo)76 void ServiceRouterDataMgr::UpdateBundleInfoLocked(const BundleInfo &bundleInfo)
77 {
78     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
79     InnerServiceInfo innerServiceInfo;
80     auto infoItem = innerServiceInfos_.find(bundleInfo.name);
81     if (infoItem != innerServiceInfos_.end()) {
82         innerServiceInfo = infoItem->second;
83     }
84     innerServiceInfo.UpdateAppInfo(bundleInfo.applicationInfo);
85 
86     std::vector<PurposeInfo> purposeInfos;
87     std::vector<BusinessAbilityInfo> businessAbilityInfos;
88     if (BundleInfoResolveUtil::ResolveBundleInfo(bundleInfo, purposeInfos, businessAbilityInfos,
89         innerServiceInfo.GetAppInfo())) {
90         innerServiceInfo.UpdateInnerServiceInfo(purposeInfos, businessAbilityInfos);
91         innerServiceInfos_.try_emplace(bundleInfo.name, innerServiceInfo);
92     }
93 }
94 
DeleteBundleInfo(const std::string & bundleName)95 void ServiceRouterDataMgr::DeleteBundleInfo(const std::string &bundleName)
96 {
97     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
98     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
99     auto infoItem = innerServiceInfos_.find(bundleName);
100     if (infoItem == innerServiceInfos_.end()) {
101         TAG_LOGE(AAFwkTag::SER_ROUTER, "innerServiceInfo not found");
102         return;
103     }
104     innerServiceInfos_.erase(bundleName);
105 }
106 
QueryBusinessAbilityInfos(const BusinessAbilityFilter & filter,std::vector<BusinessAbilityInfo> & businessAbilityInfos) const107 int32_t ServiceRouterDataMgr::QueryBusinessAbilityInfos(const BusinessAbilityFilter &filter,
108     std::vector<BusinessAbilityInfo> &businessAbilityInfos) const
109 {
110     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
111     BusinessType validType = GetBusinessType(filter);
112     if (validType == BusinessType::UNSPECIFIED) {
113         TAG_LOGE(AAFwkTag::SER_ROUTER, "BusinessType empty");
114         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
115     }
116 
117     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
118     for (const auto &item : innerServiceInfos_) {
119         item.second.FindBusinessAbilityInfos(validType, businessAbilityInfos);
120     }
121     return ERR_OK;
122 }
123 
QueryPurposeInfos(const Want & want,const std::string purposeName,std::vector<PurposeInfo> & purposeInfos) const124 int32_t ServiceRouterDataMgr::QueryPurposeInfos(const Want &want, const std::string purposeName,
125     std::vector<PurposeInfo> &purposeInfos) const
126 {
127     TAG_LOGD(AAFwkTag::SER_ROUTER, "Called");
128     if (purposeName.empty()) {
129         TAG_LOGE(AAFwkTag::SER_ROUTER, "purposeName empty");
130         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
131     }
132 
133     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
134     ElementName element = want.GetElement();
135     std::string bundleName = element.GetBundleName();
136     if (bundleName.empty()) {
137         for (const auto &item : innerServiceInfos_) {
138             item.second.FindPurposeInfos(purposeName, purposeInfos);
139         }
140     } else {
141         auto infoItem = innerServiceInfos_.find(bundleName);
142         if (infoItem == innerServiceInfos_.end()) {
143             TAG_LOGE(AAFwkTag::SER_ROUTER, "innerServiceInfo not found");
144             return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
145         }
146         infoItem->second.FindPurposeInfos(purposeName, purposeInfos);
147     }
148     return ERR_OK;
149 }
150 
GetBusinessType(const BusinessAbilityFilter & filter) const151 BusinessType ServiceRouterDataMgr::GetBusinessType(const BusinessAbilityFilter &filter) const
152 {
153     if (filter.businessType != BusinessType::UNSPECIFIED) {
154         return filter.businessType;
155     }
156 
157     if (filter.uri.empty()) {
158         return BusinessType::UNSPECIFIED;
159     }
160 
161     OHOS::Uri uri = OHOS::Uri(filter.uri);
162     if (uri.GetScheme().empty() || uri.GetHost().empty() || uri.GetScheme() != SCHEME_SERVICE_ROUTER) {
163         TAG_LOGE(AAFwkTag::SER_ROUTER, "Invalid uri: %{public}s", filter.uri.c_str());
164         return BusinessType::UNSPECIFIED;
165     }
166     return BundleInfoResolveUtil::findBusinessType(uri.GetHost());
167 }
168 
ClearAllBundleInfos()169 void ServiceRouterDataMgr::ClearAllBundleInfos()
170 {
171     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
172     if (!innerServiceInfos_.empty()) {
173         innerServiceInfos_.clear();
174     }
175 }
176 }  // namespace AbilityRuntime
177 }  // namespace OHOS
178