• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "service_router_data_mgr.h"
16 
17 #include "app_log_wrapper.h"
18 #include "bundle_info_resolve_util.h"
19 #include "iservice_registry.h"
20 #include "sr_constants.h"
21 #include "sr_samgr_helper.h"
22 #include "system_ability_definition.h"
23 #include "uri.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
28     const std::string SCHEME_SEPARATOR = "://";
29     const std::string SCHEME_SERVICE_ROUTER = "servicerouter";
30 }
31 
LoadAllBundleInfos()32 bool ServiceRouterDataMgr::LoadAllBundleInfos()
33 {
34     APP_LOGD("SRDM LoadAllBundleInfos");
35     auto bms = SrSamgrHelper::GetInstance().GetBundleMgr();
36     if (bms == nullptr) {
37         APP_LOGE("SRDM GetBundleMgr return null");
38         return false;
39     }
40     auto flags = (BundleFlag::GET_BUNDLE_WITH_ABILITIES | BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO);
41     std::vector<BundleInfo> bundleInfos;
42     bool ret = bms->GetBundleInfos(flags, bundleInfos, SrSamgrHelper::GetCurrentActiveUserId());
43     if (!ret) {
44         APP_LOGE("SRDM bms->GetBundleInfos return false");
45     }
46     if (!innerServiceInfos_.empty()) {
47         innerServiceInfos_.clear();
48     }
49     for (const auto &bundleInfo : bundleInfos) {
50         UpdateBundleInfo(bundleInfo);
51     }
52     return ret;
53 }
54 
LoadBundleInfo(const std::string & bundleName)55 bool ServiceRouterDataMgr::LoadBundleInfo(const std::string &bundleName)
56 {
57     APP_LOGD("SRDM LoadBundleInfo");
58     auto bms = SrSamgrHelper::GetInstance().GetBundleMgr();
59     if (bms == nullptr) {
60         APP_LOGI("SRDM GetBundleMgr return null");
61         return false;
62     }
63     BundleInfo bundleInfo;
64     auto flags = (BundleFlag::GET_BUNDLE_WITH_ABILITIES | BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO);
65     bool ret = bms->GetBundleInfo(bundleName, flags, bundleInfo, SrSamgrHelper::GetCurrentActiveUserId());
66     if (!ret) {
67         APP_LOGE("SRDM bms->GetBundleInfos return false");
68     }
69     UpdateBundleInfo(bundleInfo);
70     return ret;
71 }
72 
UpdateBundleInfo(const BundleInfo & bundleInfo)73 void ServiceRouterDataMgr::UpdateBundleInfo(const BundleInfo &bundleInfo)
74 {
75     APP_LOGD("SRDM UpdateBundleInfo");
76     InnerServiceInfo innerServiceInfo;
77     auto infoItem = innerServiceInfos_.find(bundleInfo.name);
78     if (infoItem != innerServiceInfos_.end()) {
79         innerServiceInfo = infoItem->second;
80     }
81     innerServiceInfo.UpdateAppInfo(bundleInfo.applicationInfo);
82 
83     std::vector<PurposeInfo> purposeInfos;
84     std::vector<BusinessAbilityInfo> businessAbilityInfos;
85     if (BundleInfoResolveUtil::ResolveBundleInfo(bundleInfo, purposeInfos, businessAbilityInfos,
86         innerServiceInfo.GetAppInfo())) {
87         std::lock_guard<std::mutex> lock(bundleInfoMutex_);
88         innerServiceInfo.UpdateInnerServiceInfo(purposeInfos, businessAbilityInfos);
89         innerServiceInfos_.try_emplace(bundleInfo.name, innerServiceInfo);
90     }
91 }
92 
DeleteBundleInfo(const std::string & bundleName)93 void ServiceRouterDataMgr::DeleteBundleInfo(const std::string &bundleName)
94 {
95     APP_LOGD("SRDM DeleteBundleInfo");
96     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
97     auto infoItem = innerServiceInfos_.find(bundleName);
98     if (infoItem == innerServiceInfos_.end()) {
99         APP_LOGE("SRDM inner service info not found by bundleName");
100         return;
101     }
102     innerServiceInfos_.erase(bundleName);
103 }
104 
QueryBusinessAbilityInfos(const BusinessAbilityFilter & filter,std::vector<BusinessAbilityInfo> & businessAbilityInfos) const105 int32_t ServiceRouterDataMgr::QueryBusinessAbilityInfos(const BusinessAbilityFilter &filter,
106     std::vector<BusinessAbilityInfo> &businessAbilityInfos) const
107 {
108     APP_LOGD("SRDM QueryBusinessAbilityInfos");
109     BusinessType validType = GetBusinessType(filter);
110     if (validType == BusinessType::UNSPECIFIED) {
111         APP_LOGE("SRDM QueryBusinessAbilityInfos, businessType is empty");
112         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
113     }
114 
115     for (const auto &item : innerServiceInfos_) {
116         item.second.FindBusinessAbilityInfos(validType, businessAbilityInfos);
117     }
118     return ERR_OK;
119 }
120 
QueryPurposeInfos(const Want & want,const std::string purposeName,std::vector<PurposeInfo> & purposeInfos) const121 int32_t ServiceRouterDataMgr::QueryPurposeInfos(const Want &want, const std::string purposeName,
122     std::vector<PurposeInfo> &purposeInfos) const
123 {
124     APP_LOGD("SRDM QueryPurposeInfos");
125     if (purposeName.empty()) {
126         APP_LOGE("SRDM QueryPurposeInfos, purposeName is empty");
127         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
128     }
129 
130     ElementName element = want.GetElement();
131     std::string bundleName = element.GetBundleName();
132     if (bundleName.empty()) {
133         for (const auto &item : innerServiceInfos_) {
134             item.second.FindPurposeInfos(purposeName, purposeInfos);
135         }
136     } else {
137         auto infoItem = innerServiceInfos_.find(bundleName);
138         if (infoItem == innerServiceInfos_.end()) {
139             APP_LOGE("SRDM QueryPurposeInfos, not found by bundleName.");
140             return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
141         }
142         infoItem->second.FindPurposeInfos(purposeName, purposeInfos);
143     }
144     return ERR_OK;
145 }
146 
GetBusinessType(const BusinessAbilityFilter & filter) const147 BusinessType ServiceRouterDataMgr::GetBusinessType(const BusinessAbilityFilter &filter) const
148 {
149     if (filter.businessType != BusinessType::UNSPECIFIED) {
150         return filter.businessType;
151     }
152 
153     if (filter.uri.empty()) {
154         return BusinessType::UNSPECIFIED;
155     }
156 
157     OHOS::Uri uri = OHOS::Uri(filter.uri);
158     if (uri.GetScheme().empty() || uri.GetHost().empty() || uri.GetScheme() != SCHEME_SERVICE_ROUTER) {
159         APP_LOGE("GetExtensionServiceType, invalid uri: %{public}s", filter.uri.c_str());
160         return BusinessType::UNSPECIFIED;
161     }
162     return BundleInfoResolveUtil::findBusinessType(uri.GetHost());
163 }
164 }  // namespace AbilityRuntime
165 }  // namespace OHOS
166