1 /*
2 * Copyright (c) 2024-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 #include "batch_uri.h"
17
18 #include "file_permission_manager.h"
19 #include "hilog_tag_wrapper.h"
20 #include "uri_permission_utils.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24
Init(const std::vector<std::string> & uriVec,uint32_t mode,const std::string & callerAlterBundleName,const std::string & targetAlterBundleName)25 int32_t BatchUri::Init(const std::vector<std::string> &uriVec, uint32_t mode,
26 const std::string &callerAlterBundleName, const std::string &targetAlterBundleName)
27 {
28 if (uriVec.empty()) {
29 TAG_LOGE(AAFwkTag::URIPERMMGR, "uriVec is empty.");
30 return 0;
31 }
32 totalUriCount = static_cast<int32_t>(uriVec.size());
33 validUriCount = 0;
34 checkResult = std::vector<bool>(totalUriCount, false);
35 isDocsUriVec = std::vector<bool>(totalUriCount, false);
36 isTargetBundleUri = std::vector<bool>(totalUriCount, false);
37 bool isPrintAuthority = true;
38 for (size_t index = 0; index < uriVec.size(); index++) {
39 Uri uriInner = Uri(uriVec[index]);
40 auto &&scheme = uriInner.GetScheme();
41 if (isPrintAuthority) {
42 TAG_LOGI(AAFwkTag::URIPERMMGR, "uri type: %{public}s.", uriInner.GetAuthority().c_str());
43 isPrintAuthority = false;
44 }
45 if (scheme != "file") {
46 TAG_LOGW(AAFwkTag::URIPERMMGR, "uri is invalid: %{private}s.", uriInner.ToString().c_str());
47 continue;
48 }
49 validUriCount++;
50 InitFileUriInfo(uriInner, index, mode, callerAlterBundleName, targetAlterBundleName);
51 }
52 TAG_LOGI(AAFwkTag::URIPERMMGR, "count of uri is %{public}d, count of valid uri is %{public}d.",
53 totalUriCount, validUriCount);
54 return validUriCount;
55 }
56
InitFileUriInfo(Uri & uriInner,uint32_t index,const uint32_t mode,const std::string & callerAlterBundleName,const std::string & targetAlterBundleName)57 void BatchUri::InitFileUriInfo(Uri &uriInner, uint32_t index, const uint32_t mode,
58 const std::string &callerAlterBundleName, const std::string &targetAlterBundleName)
59 {
60 auto &&authority = uriInner.GetAuthority();
61 // media uri
62 if (authority == "media") {
63 mediaUris.emplace_back(uriInner.ToString());
64 mediaIndexes.emplace_back(index);
65 return;
66 }
67 // bundle uri
68 isTargetBundleUri[index] = (!targetAlterBundleName.empty() && authority == targetAlterBundleName);
69 if (!callerAlterBundleName.empty() && authority == callerAlterBundleName) {
70 checkResult[index] = true;
71 if (isTargetBundleUri[index]) {
72 TAG_LOGI(AAFwkTag::URIPERMMGR, "uri belong to targetBundle.");
73 targetBundleUriCount++;
74 return;
75 }
76 if (mode > 0) {
77 // need set policy
78 auto policyInfo = FilePermissionManager::GetPathPolicyInfoFromUri(uriInner, mode);
79 selfBundlePolicyInfos.emplace_back(policyInfo);
80 }
81 return;
82 }
83 if (authority == "docs") {
84 isDocsUriVec[index] = true;
85 }
86 // docs and bundle uri, need to check uri pemission
87 otherUris.emplace_back(uriInner);
88 otherIndexes.emplace_back(index);
89 }
90
SetMediaUriCheckResult(const std::vector<bool> & mediaUriResult)91 void BatchUri::SetMediaUriCheckResult(const std::vector<bool> &mediaUriResult)
92 {
93 for (size_t i = 0; i < mediaUriResult.size(); i++) {
94 auto index = mediaIndexes[i];
95 checkResult[index] = mediaUriResult[i];
96 }
97 }
98
SetOtherUriCheckResult(const std::vector<bool> & otherUriResult)99 void BatchUri::SetOtherUriCheckResult(const std::vector<bool> &otherUriResult)
100 {
101 for (size_t i = 0; i < otherUriResult.size(); i++) {
102 auto index = otherIndexes[i];
103 checkResult[index] = otherUriResult[i];
104 if (checkResult[index] && isTargetBundleUri[index]) {
105 targetBundleUriCount++;
106 }
107 }
108 }
109
GetMediaUriToGrant(std::vector<std::string> & uriVec)110 int32_t BatchUri::GetMediaUriToGrant(std::vector<std::string> &uriVec)
111 {
112 for (size_t i = 0; i < mediaIndexes.size(); i++) {
113 if (checkResult[mediaIndexes[i]]) {
114 uriVec.emplace_back(mediaUris[i]);
115 }
116 }
117 return uriVec.size();
118 }
119
GetNeedCheckProxyPermissionURI(std::vector<PolicyInfo> & proxyUrisByPolicy,std::vector<Uri> & proxyUrisByMap)120 void BatchUri::GetNeedCheckProxyPermissionURI(std::vector<PolicyInfo> &proxyUrisByPolicy,
121 std::vector<Uri> &proxyUrisByMap)
122 {
123 // docs uri and bundle uri
124 for (size_t i = 0; i < otherIndexes.size(); i++) {
125 auto index = otherIndexes[i];
126 if (!checkResult[index]) {
127 proxyIndexesByPolicy.emplace_back(index);
128 proxyUrisByPolicy.emplace_back(otherPolicyInfos[i]);
129 }
130 }
131 }
132
SetCheckProxyByMapResult(std::vector<bool> & proxyResultByMap)133 void BatchUri::SetCheckProxyByMapResult(std::vector<bool> &proxyResultByMap)
134 {
135 for (size_t i = 0; i < proxyResultByMap.size(); i++) {
136 auto index = proxyIndexesByMap[i];
137 checkResult[index] = proxyResultByMap[i];
138 }
139 proxyIndexesByMap.clear();
140 }
141
SetCheckProxyByPolicyResult(std::vector<bool> & proxyResultByPolicy)142 void BatchUri::SetCheckProxyByPolicyResult(std::vector<bool> &proxyResultByPolicy)
143 {
144 for (size_t i = 0; i < proxyResultByPolicy.size(); i++) {
145 auto index = proxyIndexesByPolicy[i];
146 checkResult[index] = proxyResultByPolicy[i];
147 }
148 proxyIndexesByPolicy.clear();
149 }
150
GetUriToGrantByMap(std::vector<std::string> & uriVec)151 int32_t BatchUri::GetUriToGrantByMap(std::vector<std::string> &uriVec)
152 {
153 return uriVec.size();
154 }
155
SelectPermissionedUri(std::vector<Uri> & uris,std::vector<int32_t> & indexs,std::vector<std::string> & uriVec)156 void BatchUri::SelectPermissionedUri(std::vector<Uri> &uris, std::vector<int32_t> &indexs,
157 std::vector<std::string> &uriVec)
158 {
159 for (size_t i = 0; i < indexs.size(); i++) {
160 if (checkResult[indexs[i]]) {
161 auto uriStr = uris[i].ToString();
162 uriVec.emplace_back(uriStr);
163 }
164 }
165 }
166
GetUriToGrantByPolicy(std::vector<PolicyInfo> & docsPolicyInfoVec,std::vector<PolicyInfo> & bundlePolicyInfoVec)167 int32_t BatchUri::GetUriToGrantByPolicy(std::vector<PolicyInfo> &docsPolicyInfoVec,
168 std::vector<PolicyInfo> &bundlePolicyInfoVec)
169 {
170 // bundleName + docs
171 int32_t count = 0;
172 for (auto &selfBundleUriPolicy : selfBundlePolicyInfos) {
173 bundlePolicyInfoVec.emplace_back(selfBundleUriPolicy);
174 count++;
175 }
176 for (size_t i = 0; i < otherPolicyInfos.size(); i++) {
177 auto index = otherIndexes[i];
178 if (!checkResult[index]) {
179 continue;
180 }
181 // the uri belong to target app.
182 if (isTargetBundleUri[index]) {
183 continue;
184 }
185 TAG_LOGD(AAFwkTag::URIPERMMGR, "Add policy: path is %{private}s, mode is %{public}u.",
186 otherPolicyInfos[i].path.c_str(), static_cast<uint32_t>(otherPolicyInfos[i].mode));
187 if (isDocsUriVec[index]) {
188 docsPolicyInfoVec.emplace_back(otherPolicyInfos[i]);
189 } else {
190 bundlePolicyInfoVec.emplace_back(otherPolicyInfos[i]);
191 }
192 count++;
193 }
194 return count;
195 }
196
GetUriToGrantByPolicy(std::vector<PolicyInfo> & policyVec)197 bool BatchUri::GetUriToGrantByPolicy(std::vector<PolicyInfo> &policyVec)
198 {
199 // self bundle uris
200 for (const auto &selfBundleUriPolicy : selfBundlePolicyInfos) {
201 policyVec.emplace_back(selfBundleUriPolicy);
202 }
203 for (size_t i = 0; i < otherPolicyInfos.size(); i++) {
204 auto index = otherIndexes[i];
205 if (!checkResult[index]) {
206 return false;
207 }
208 // the uri belong to target app.
209 if (isTargetBundleUri[index]) {
210 continue;
211 }
212 policyVec.emplace_back(otherPolicyInfos[i]);
213 }
214 return true;
215 }
216
GetPermissionedUriCount()217 int32_t BatchUri::GetPermissionedUriCount()
218 {
219 int32_t permissionedUriCount = 0;
220 for (auto checkRes: checkResult) {
221 if (checkRes) {
222 permissionedUriCount++;
223 }
224 }
225 return permissionedUriCount;
226 }
227
IsAllUriValid()228 bool BatchUri::IsAllUriValid()
229 {
230 return validUriCount == totalUriCount;
231 }
232
IsAllUriPermissioned()233 bool BatchUri::IsAllUriPermissioned()
234 {
235 for (auto checkRes : checkResult) {
236 if (!checkRes) {
237 return false;
238 }
239 }
240 return true;
241 }
242 } // OHOS
243 } // AAFwk