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 "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<Uri> & uriVec,uint32_t mode,const std::string & callerBundleName,const std::string & targetBundleName)25 int32_t BatchUri::Init(const std::vector<Uri> &uriVec, uint32_t mode, const std::string &callerBundleName,
26 const std::string &targetBundleName)
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 result = 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 auto uriInner = 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 != "content" && scheme != "file") {
46 TAG_LOGW(AAFwkTag::URIPERMMGR, "uri is invalid: %{private}s.", uriInner.ToString().c_str());
47 continue;
48 }
49 validUriCount++;
50 // content uri
51 if (scheme == "content") {
52 contentUris.emplace_back(uriInner);
53 contentIndexs.emplace_back(index);
54 continue;
55 }
56 InitFileUriInfo(uriInner, index, mode, callerBundleName, targetBundleName);
57 }
58 TAG_LOGI(AAFwkTag::URIPERMMGR, "count of uri is %{public}d, count of valid uri is %{public}d.",
59 totalUriCount, validUriCount);
60 return validUriCount;
61 }
62
InitFileUriInfo(Uri & uriInner,uint32_t index,const uint32_t mode,const std::string & callerBundleName,const std::string & targetBundleName)63 void BatchUri::InitFileUriInfo(Uri &uriInner, uint32_t index, const uint32_t mode,
64 const std::string &callerBundleName, const std::string &targetBundleName)
65 {
66 auto &&authority = uriInner.GetAuthority();
67 TAG_LOGD(AAFwkTag::URIPERMMGR, "Authority of uri is %{public}s.", authority.c_str());
68 // media uri
69 if (authority == "media") {
70 mediaUris.emplace_back(uriInner);
71 mediaIndexs.emplace_back(index);
72 return;
73 }
74 // bundle uri
75 isTargetBundleUri[index] = (!targetBundleName.empty() && authority == targetBundleName);
76 if (!callerBundleName.empty() && authority == callerBundleName) {
77 result[index] = true;
78 if (isTargetBundleUri[index]) {
79 TAG_LOGI(AAFwkTag::URIPERMMGR, "uri belong to targetBundle.");
80 targetBundleUriCount++;
81 return;
82 }
83 if (mode > 0) {
84 // need set policy
85 auto policyInfo = FilePermissionManager::GetPathPolicyInfoFromUri(uriInner, mode);
86 selfBundlePolicyInfos.emplace_back(policyInfo);
87 }
88 return;
89 }
90 if (authority == "docs") {
91 isDocsUriVec[index] = true;
92 }
93 // docs and bundle uri, need to check uri pemission
94 otherUris.emplace_back(uriInner);
95 otherIndexs.emplace_back(index);
96 }
97
SetContentUriCheckResult(const std::vector<bool> & contentUriResult)98 void BatchUri::SetContentUriCheckResult(const std::vector<bool> &contentUriResult)
99 {
100 for (size_t i = 0; i < contentUriResult.size(); i++) {
101 auto index = contentIndexs[i];
102 result[index] = contentUriResult[i];
103 }
104 }
105
SetMediaUriCheckResult(const std::vector<bool> & mediaUriResult)106 void BatchUri::SetMediaUriCheckResult(const std::vector<bool> &mediaUriResult)
107 {
108 for (size_t i = 0; i < mediaUriResult.size(); i++) {
109 auto index = mediaIndexs[i];
110 result[index] = mediaUriResult[i];
111 }
112 }
113
SetOtherUriCheckResult(const std::vector<bool> & otherUriResult)114 void BatchUri::SetOtherUriCheckResult(const std::vector<bool> &otherUriResult)
115 {
116 for (size_t i = 0; i < otherUriResult.size(); i++) {
117 auto index = otherIndexs[i];
118 result[index] = otherUriResult[i];
119 if (result[index] && isTargetBundleUri[index]) {
120 targetBundleUriCount++;
121 }
122 }
123 }
124
GetMediaUriToGrant(std::vector<std::string> & uriVec)125 int32_t BatchUri::GetMediaUriToGrant(std::vector<std::string> &uriVec)
126 {
127 for (size_t i = 0; i < mediaIndexs.size(); i++) {
128 if (result[mediaIndexs[i]]) {
129 uriVec.emplace_back(mediaUris[i].ToString());
130 }
131 }
132 return uriVec.size();
133 }
134
GetNeedCheckProxyPermissionURI(std::vector<PolicyInfo> & proxyUrisByPolicy,std::vector<Uri> & proxyUrisByMap)135 void BatchUri::GetNeedCheckProxyPermissionURI(std::vector<PolicyInfo> &proxyUrisByPolicy,
136 std::vector<Uri> &proxyUrisByMap)
137 {
138 // docs uri and bundle uri
139 for (size_t i = 0; i < otherIndexs.size(); i++) {
140 auto index = otherIndexs[i];
141 if (!result[index]) {
142 proxyIndexsByPolicy.emplace_back(index);
143 proxyUrisByPolicy.emplace_back(otherPolicyInfos[i]);
144 }
145 }
146 }
147
SetCheckProxyByMapResult(std::vector<bool> & proxyResultByMap)148 void BatchUri::SetCheckProxyByMapResult(std::vector<bool> &proxyResultByMap)
149 {
150 for (size_t i = 0; i < proxyResultByMap.size(); i++) {
151 auto index = proxyIndexsByMap[i];
152 result[index] = proxyResultByMap[i];
153 }
154 proxyIndexsByMap.clear();
155 }
156
SetCheckProxyByPolicyResult(std::vector<bool> & proxyResultByPolicy)157 void BatchUri::SetCheckProxyByPolicyResult(std::vector<bool> &proxyResultByPolicy)
158 {
159 for (size_t i = 0; i < proxyResultByPolicy.size(); i++) {
160 auto index = proxyIndexsByPolicy[i];
161 result[index] = proxyResultByPolicy[i];
162 }
163 proxyIndexsByPolicy.clear();
164 }
165
GetUriToGrantByMap(std::vector<std::string> & uriVec)166 int32_t BatchUri::GetUriToGrantByMap(std::vector<std::string> &uriVec)
167 {
168 return uriVec.size();
169 }
170
SelectPermissionedUri(std::vector<Uri> & uris,std::vector<int32_t> & indexs,std::vector<std::string> & uriVec)171 void BatchUri::SelectPermissionedUri(std::vector<Uri> &uris, std::vector<int32_t> &indexs,
172 std::vector<std::string> &uriVec)
173 {
174 for (size_t i = 0; i < indexs.size(); i++) {
175 if (result[indexs[i]]) {
176 auto uriStr = uris[i].ToString();
177 uriVec.emplace_back(uriStr);
178 }
179 }
180 }
181
GetUriToGrantByPolicy(std::vector<PolicyInfo> & docsPolicyInfoVec,std::vector<PolicyInfo> & bundlePolicyInfoVec)182 int32_t BatchUri::GetUriToGrantByPolicy(std::vector<PolicyInfo> &docsPolicyInfoVec,
183 std::vector<PolicyInfo> &bundlePolicyInfoVec)
184 {
185 // bundleName + docs
186 int32_t count = 0;
187 for (auto &selfBundleUriPolicy : selfBundlePolicyInfos) {
188 bundlePolicyInfoVec.emplace_back(selfBundleUriPolicy);
189 count++;
190 }
191 for (size_t i = 0; i < otherPolicyInfos.size(); i++) {
192 auto index = otherIndexs[i];
193 if (!result[index]) {
194 continue;
195 }
196 // the uri belong to target app.
197 if (isTargetBundleUri[index]) {
198 continue;
199 }
200 TAG_LOGD(AAFwkTag::URIPERMMGR, "Add policy: path is %{private}s, mode is %{public}u.",
201 otherPolicyInfos[i].path.c_str(), static_cast<uint32_t>(otherPolicyInfos[i].mode));
202 if (isDocsUriVec[index]) {
203 docsPolicyInfoVec.emplace_back(otherPolicyInfos[i]);
204 } else {
205 bundlePolicyInfoVec.emplace_back(otherPolicyInfos[i]);
206 }
207 count++;
208 }
209 return count;
210 }
211
GetPermissionedUriCount()212 int32_t BatchUri::GetPermissionedUriCount()
213 {
214 int32_t permissionedUriCount = 0;
215 for (auto checkRes: result) {
216 if (checkRes) {
217 permissionedUriCount++;
218 }
219 }
220 return permissionedUriCount;
221 }
222 } // OHOS
223 } // AAFwk