• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "uri_utils.h"
17 
18 #include "ability_util.h"
19 #include "ability_config.h"
20 #include "ability_record.h"
21 #include "ability_manager_errors.h"
22 #include "accesstoken_kit.h"
23 #include "app_utils.h"
24 #include "common_event_manager.h"
25 #include "extension_ability_info.h"
26 #include "element_name.h"
27 #include "global_constant.h"
28 #include "hilog_tag_wrapper.h"
29 #include "hitrace_meter.h"
30 #include "in_process_call_wrapper.h"
31 #include "ipc_skeleton.h"
32 #include "tokenid_kit.h"
33 #include "ui_extension_utils.h"
34 #ifdef SUPPORT_UPMS
35 #include "uri_permission_manager_client.h"
36 #endif // SUPPORT_UPMS
37 
38 namespace OHOS {
39 namespace AAFwk {
40 namespace {
41 const std::string PARAMS_URI = "ability.verify.uri";
42 const std::string DISTRIBUTED_FILES_PATH = "/data/storage/el2/distributedfiles/";
43 const std::string HIDE_SENSITIVE_TYPE = "ohos.media.params.hideSensitiveType";
44 const std::string DMS_PROCESS_NAME = "distributedsched";
45 const int32_t MAX_URI_COUNT = 500;
46 constexpr int32_t API14 = 14;
47 constexpr int32_t API_VERSION_MOD = 100;
48 constexpr uint32_t TOKEN_ID_BIT_SIZE = 32;
49 }
50 
UriUtils()51 UriUtils::UriUtils() {}
52 
~UriUtils()53 UriUtils::~UriUtils() {}
54 
GetInstance()55 UriUtils &UriUtils::GetInstance()
56 {
57     static UriUtils utils;
58     return utils;
59 }
60 
GetUriListFromWantDms(Want & want)61 std::vector<Uri> UriUtils::GetUriListFromWantDms(Want &want)
62 {
63     std::vector<std::string> uriStrVec = want.GetStringArrayParam(PARAMS_URI);
64     TAG_LOGD(AAFwkTag::ABILITYMGR, "uriVec size: %{public}zu", uriStrVec.size());
65     if (uriStrVec.size() > MAX_URI_COUNT) {
66         TAG_LOGE(AAFwkTag::ABILITYMGR, "uri list size is more than %{public}u", MAX_URI_COUNT);
67         uriStrVec.resize(MAX_URI_COUNT);
68     }
69     std::vector<Uri> validUriVec;
70     for (auto &&str : uriStrVec) {
71         Uri uri(str);
72         auto &&scheme = uri.GetScheme();
73         TAG_LOGI(AAFwkTag::ABILITYMGR, "uri scheme: %{public}s", scheme.c_str());
74         // only support file scheme
75         if (scheme != "file") {
76             TAG_LOGW(AAFwkTag::ABILITYMGR, "only support file uri");
77             continue;
78         }
79         std::string srcPath = uri.GetPath();
80         if (std::filesystem::exists(srcPath) && std::filesystem::is_symlink(srcPath)) {
81             TAG_LOGE(AAFwkTag::ABILITYMGR, "soft links not allowed");
82             continue;
83         }
84         std::string absolutePath;
85         if (uri.IsRelative()) {
86             char path[PATH_MAX] = {0};
87             if (realpath(srcPath.c_str(), path) == nullptr) {
88                 TAG_LOGE(AAFwkTag::ABILITYMGR, "fail, errno :%{public}d", errno);
89                 continue;
90             }
91             absolutePath = path;
92         } else {
93             absolutePath = srcPath;
94         }
95         if (absolutePath.compare(0, DISTRIBUTED_FILES_PATH.size(), DISTRIBUTED_FILES_PATH) != 0) {
96             TAG_LOGE(AAFwkTag::ABILITYMGR, "uri not distributed path");
97             continue;
98         }
99         validUriVec.emplace_back(uri);
100     }
101     uriStrVec.clear();
102     want.SetParam(PARAMS_URI, uriStrVec);
103     TAG_LOGD(AAFwkTag::ABILITYMGR, "size of vaid uri is %{public}zu", validUriVec.size());
104     return validUriVec;
105 }
106 
CheckNonImplicitShareFileUri(const Want & want,int32_t userId,uint32_t specifyTokenId)107 int32_t UriUtils::CheckNonImplicitShareFileUri(const Want &want, int32_t userId, uint32_t specifyTokenId)
108 {
109     auto element = want.GetElement();
110     TAG_LOGD(AAFwkTag::ABILITYMGR, "CheckNonImplicitShareFileUri, %{public}s-%{public}s",
111         element.GetBundleName().c_str(), element.GetAbilityName().c_str());
112     if (element.GetBundleName().empty() || element.GetAbilityName().empty()) {
113         return ERR_OK;
114     }
115 #ifdef SUPPORT_UPMS
116     if (!IsGrantUriPermissionFlag(want)) {
117         return ERR_OK;
118     }
119 #endif // SUPPORT_UPMS
120     bool isFileUri = (!want.GetUriString().empty() && want.GetUri().GetScheme() == "file");
121     if (!isFileUri && want.GetStringArrayParam(AbilityConfig::PARAMS_STREAM).empty()) {
122         TAG_LOGD(AAFwkTag::ABILITYMGR, "not file uri");
123         return ERR_OK;
124     }
125     // SA and system app support
126     auto callerTokenId = specifyTokenId > 0 ? specifyTokenId : IPCSkeleton::GetCallingTokenID();
127     if (CheckNonImplicitShareFileUriInner(callerTokenId, element.GetBundleName(), userId) != ERR_OK) {
128         TAG_LOGW(AAFwkTag::ABILITYMGR, "share file uri non-implicitly will not support");
129     }
130     return ERR_OK;
131 }
132 
CheckNonImplicitShareFileUriInner(uint32_t callerTokenId,const std::string & targetBundleName,int32_t userId)133 int32_t UriUtils::CheckNonImplicitShareFileUriInner(uint32_t callerTokenId, const std::string &targetBundleName,
134     int32_t userId)
135 {
136     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerTokenId);
137     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
138         TAG_LOGD(AAFwkTag::ABILITYMGR, "SA call");
139         return ERR_OK;
140     }
141     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
142         Security::AccessToken::HapTokenInfo hapInfo;
143         auto ret = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callerTokenId, hapInfo);
144         if (ret != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
145             TAG_LOGE(AAFwkTag::URIPERMMGR, "GetHapTokenInfo failed, ret:%{public}d", ret);
146             return INNER_ERR;
147         }
148         // check api version
149         TAG_LOGD(AAFwkTag::ABILITYMGR, "CallerBundleName:%{public}s, API:%{public}d",
150             hapInfo.bundleName.c_str(), hapInfo.apiVersion);
151         if ((hapInfo.apiVersion % API_VERSION_MOD) <= API14) {
152             TAG_LOGD(AAFwkTag::ABILITYMGR, "api version lower than 14");
153             return ERR_OK;
154         }
155         // check system app
156         uint64_t fullCallerTokenId = (static_cast<uint64_t>(hapInfo.tokenAttr) << TOKEN_ID_BIT_SIZE) + callerTokenId;
157         if (Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullCallerTokenId)) {
158             TAG_LOGD(AAFwkTag::ABILITYMGR, "system app call");
159             return ERR_OK;
160         }
161     }
162     // target is system app
163     if (IsSystemApplication(targetBundleName, userId)) {
164         TAG_LOGD(AAFwkTag::ABILITYMGR, "target is system app");
165         return ERR_OK;
166     }
167     return CHECK_PERMISSION_FAILED;
168 }
169 
IsSystemApplication(const std::string & bundleName,int32_t userId)170 bool UriUtils::IsSystemApplication(const std::string &bundleName, int32_t userId)
171 {
172     auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
173     if (!bundleMgrHelper) {
174         TAG_LOGW(AAFwkTag::ABILITYMGR, "GetBundleManagerHelper failed");
175         return false;
176     }
177     AppExecFwk::ApplicationInfo appInfo;
178     if (!IN_PROCESS_CALL(bundleMgrHelper->GetApplicationInfo(bundleName,
179         AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, userId, appInfo))) {
180         TAG_LOGW(AAFwkTag::ABILITYMGR, "GetApplicationInfo failed");
181         return false;
182     }
183     return appInfo.isSystemApp;
184 }
185 
GetPermissionedUriList(const std::vector<std::string> & uriVec,const std::vector<bool> & checkResults,Want & want)186 std::vector<Uri> UriUtils::GetPermissionedUriList(const std::vector<std::string> &uriVec,
187     const std::vector<bool> &checkResults, Want &want)
188 {
189     std::vector<Uri> permissionedUris;
190     if (uriVec.size() != checkResults.size()) {
191         TAG_LOGE(AAFwkTag::ABILITYMGR, "Invalid param: %{public}zu : %{public}zu",
192             uriVec.size(), checkResults.size());
193         return permissionedUris;
194     }
195     // process uri
196     size_t startIndex = 0;
197     if (!want.GetUriString().empty()) {
198         if (checkResults[startIndex]) {
199             permissionedUris.emplace_back(want.GetUri());
200         } else if (want.GetUri().GetScheme() == "file") {
201             // erase uri param
202             want.SetUri("");
203             TAG_LOGI(AAFwkTag::ABILITYMGR, "erase uri param.");
204         }
205         startIndex = 1;
206     }
207     // process param stream
208     std::vector<std::string> paramStreamUris;
209     for (size_t index = startIndex; index < checkResults.size(); index++) {
210         auto uri = Uri(uriVec[index]);
211         if (checkResults[index]) {
212             permissionedUris.emplace_back(uri);
213             paramStreamUris.emplace_back(uriVec[index]);
214         } else if (uri.GetScheme() != "file") {
215             paramStreamUris.emplace_back(uriVec[index]);
216         }
217     }
218     if (paramStreamUris.size() != (checkResults.size() - startIndex)) {
219         // erase old param stream and set new param stream
220         want.RemoveParam(AbilityConfig::PARAMS_STREAM);
221         want.SetParam(AbilityConfig::PARAMS_STREAM, paramStreamUris);
222         TAG_LOGI(AAFwkTag::ABILITYMGR, "startIndex: %{public}zu, uriVec: %{public}zu, paramStreamUris: %{public}zu",
223             startIndex, uriVec.size(), paramStreamUris.size());
224     }
225     return permissionedUris;
226 }
227 
GetUriListFromWant(Want & want,std::vector<std::string> & uriVec)228 bool UriUtils::GetUriListFromWant(Want &want, std::vector<std::string> &uriVec)
229 {
230     // remove uris out of 500
231     auto uriStr = want.GetUri().ToString();
232     uriVec = want.GetStringArrayParam(AbilityConfig::PARAMS_STREAM);
233     if (uriVec.empty() && uriStr.empty()) {
234         TAG_LOGW(AAFwkTag::ABILITYMGR, "uriVec empty.");
235         return false;
236     }
237     // process param stream
238     auto paramStreamUriCount = uriVec.size();
239     if (uriStr.empty() && paramStreamUriCount > MAX_URI_COUNT) {
240         TAG_LOGW(AAFwkTag::ABILITYMGR, "uri empty, paream stream counts: %{public}zu", paramStreamUriCount);
241         uriVec.resize(MAX_URI_COUNT);
242         want.RemoveParam(AbilityConfig::PARAMS_STREAM);
243         want.SetParam(AbilityConfig::PARAMS_STREAM, uriVec);
244     }
245     if (!uriStr.empty() && paramStreamUriCount > MAX_URI_COUNT - 1) {
246         TAG_LOGW(AAFwkTag::ABILITYMGR, "paream stream counts: %{public}zu", paramStreamUriCount);
247         uriVec.resize(MAX_URI_COUNT - 1);
248         want.RemoveParam(AbilityConfig::PARAMS_STREAM);
249         want.SetParam(AbilityConfig::PARAMS_STREAM, uriVec);
250     }
251     // process uri
252     if (!uriStr.empty()) {
253         uriVec.insert(uriVec.begin(), uriStr);
254     }
255     return true;
256 }
257 
258 #ifdef SUPPORT_UPMS
IsGrantUriPermissionFlag(const Want & want)259 bool UriUtils::IsGrantUriPermissionFlag(const Want &want)
260 {
261     return ((want.GetFlags() & (Want::FLAG_AUTH_READ_URI_PERMISSION | Want::FLAG_AUTH_WRITE_URI_PERMISSION)) != 0);
262 }
263 #endif // SUPPORT_UPMS
264 
IsServiceExtensionType(AppExecFwk::ExtensionAbilityType extensionAbilityType)265 bool UriUtils::IsServiceExtensionType(AppExecFwk::ExtensionAbilityType extensionAbilityType)
266 {
267     return extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE ||
268         extensionAbilityType == AppExecFwk::ExtensionAbilityType::UI_SERVICE;
269 }
270 
IsDmsCall(uint32_t fromTokenId)271 bool UriUtils::IsDmsCall(uint32_t fromTokenId)
272 {
273     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(fromTokenId);
274     bool isNativeCall = tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE;
275     if (!isNativeCall) {
276         TAG_LOGI(AAFwkTag::ABILITYMGR, "not native call");
277         return false;
278     }
279     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
280     int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(fromTokenId, nativeTokenInfo);
281     if (result == ERR_OK && nativeTokenInfo.processName == DMS_PROCESS_NAME) {
282         TAG_LOGI(AAFwkTag::ABILITYMGR, "dms ability call");
283         return true;
284     }
285     return false;
286 }
287 
288 #ifdef SUPPORT_UPMS
GrantDmsUriPermission(Want & want,uint32_t callerTokenId,std::string targetBundleName,int32_t appIndex)289 void UriUtils::GrantDmsUriPermission(Want &want, uint32_t callerTokenId,
290     std::string targetBundleName, int32_t appIndex)
291 {
292     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
293     auto validUriVec = GetUriListFromWantDms(want);
294     auto hideSensitiveType = want.GetIntParam(HIDE_SENSITIVE_TYPE, 0);
295     auto ret = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().GrantUriPermissionPrivileged(validUriVec,
296         want.GetFlags(), targetBundleName, appIndex, callerTokenId, hideSensitiveType));
297     if (ret != ERR_OK) {
298         TAG_LOGD(AAFwkTag::ABILITYMGR, "ret is %{public}d.", ret);
299         return;
300     }
301 }
302 
GrantShellUriPermission(const std::vector<std::string> & strUriVec,uint32_t flag,const std::string & targetPkg,int32_t appIndex)303 bool UriUtils::GrantShellUriPermission(const std::vector<std::string> &strUriVec, uint32_t flag,
304     const std::string &targetPkg, int32_t appIndex)
305 {
306     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
307     TAG_LOGD(AAFwkTag::ABILITYMGR, "Grant uri permission to shell.");
308     std::vector<Uri> uriVec;
309     for (auto&& str : strUriVec) {
310         Uri uri(str);
311         auto&& scheme = uri.GetScheme();
312         if (scheme != "content") {
313             return false;
314         }
315         uriVec.emplace_back(uri);
316     }
317     uint32_t callerTokendId = IPCSkeleton::GetCallingTokenID();
318     auto ret = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().GrantUriPermissionPrivileged(
319         uriVec, flag, targetPkg, appIndex, callerTokendId));
320     if (ret != ERR_OK) {
321         TAG_LOGW(AAFwkTag::ABILITYMGR, "grant uri to shell failed: %{public}d", ret);
322     }
323     return true;
324 }
325 
CheckUriPermission(uint32_t callerTokenId,Want & want)326 void UriUtils::CheckUriPermission(uint32_t callerTokenId, Want &want)
327 {
328     // Check and clear no-permission uris in want, not support content uri
329     uint32_t flag = want.GetFlags();
330     if (!IsGrantUriPermissionFlag(want)) {
331         TAG_LOGD(AAFwkTag::ABILITYMGR, "No grant uri flag: %{public}u.", flag);
332         return;
333     }
334     std::vector<std::string> uriVec;
335     if (!UriUtils::GetUriListFromWant(want, uriVec)) {
336         TAG_LOGD(AAFwkTag::ABILITYMGR, "No file uri neet grant.");
337         return;
338     }
339     auto checkResults = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().CheckUriAuthorization(
340         uriVec, flag, callerTokenId));
341     auto permissionUris = GetPermissionedUriList(uriVec, checkResults, want);
342     if (permissionUris.empty()) {
343         TAG_LOGE(AAFwkTag::ABILITYMGR, "all uris not permissioned.");
344         return;
345     }
346 }
347 
GrantUriPermission(const std::vector<std::string> & uriVec,int32_t flag,const std::string & targetBundleName,int32_t appIndex,uint32_t initiatorTokenId)348 void UriUtils::GrantUriPermission(const std::vector<std::string> &uriVec, int32_t flag,
349     const std::string &targetBundleName, int32_t appIndex, uint32_t initiatorTokenId)
350 {
351     std::vector<Uri> permissionUris;
352     for (auto &uriStr: uriVec) {
353         Uri uri(uriStr);
354         permissionUris.emplace_back(uri);
355     }
356     if (permissionUris.empty()) {
357         return;
358     }
359     auto ret = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().GrantUriPermission(permissionUris,
360         flag, targetBundleName, appIndex, initiatorTokenId));
361     if (ret != ERR_OK) {
362         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed, err:%{public}d", ret);
363     }
364 
365     return;
366 }
367 #endif // SUPPORT_UPMS
368 
IsSandboxApp(uint32_t tokenId)369 bool UriUtils::IsSandboxApp(uint32_t tokenId)
370 {
371     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
372     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
373         Security::AccessToken::HapTokenInfo hapInfo;
374         auto ret = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo);
375         if (ret != Security::AccessToken::AccessTokenKitRet::RET_SUCCESS) {
376             TAG_LOGE(AAFwkTag::URIPERMMGR, "GetHapTokenInfo failed, ret:%{public}d", ret);
377             return false;
378         }
379         return hapInfo.instIndex > AbilityRuntime::GlobalConstant::MAX_APP_CLONE_INDEX;
380     }
381     return false;
382 }
383 
384 #ifdef SUPPORT_UPMS
GrantUriPermission(Want & want,std::string targetBundleName,int32_t appIndex,bool isSandboxApp,uint32_t callerTokenId,int32_t collaboratorType)385 void UriUtils::GrantUriPermission(Want &want, std::string targetBundleName, int32_t appIndex,
386     bool isSandboxApp, uint32_t callerTokenId, int32_t collaboratorType)
387 {
388     uint32_t flag = want.GetFlags();
389     if (!IsGrantUriPermissionFlag(want)) {
390         TAG_LOGD(AAFwkTag::ABILITYMGR, "No grant uri flag: %{public}u.", flag);
391         return;
392     }
393 
394     if (targetBundleName == AppUtils::GetInstance().GetBrokerDelegateBundleName() &&
395         collaboratorType == CollaboratorType::OTHERS_TYPE) {
396         TAG_LOGD(AAFwkTag::ABILITYMGR, "reject shell application to grant uri permission");
397         return;
398     }
399     if (callerTokenId == 0) {
400         TAG_LOGE(AAFwkTag::ABILITYMGR, "callerTokenId is invalid.");
401         return;
402     }
403     if (isSandboxApp || IsSandboxApp(callerTokenId)) {
404         TAG_LOGE(AAFwkTag::ABILITYMGR, "sandbox can not grant UriPermission");
405         return;
406     }
407 
408     if (IsDmsCall(callerTokenId)) {
409         GrantDmsUriPermission(want, callerTokenId, targetBundleName, appIndex);
410         return;
411     }
412 
413     std::vector<std::string> uriVec;
414     if (!UriUtils::GetUriListFromWant(want, uriVec)) {
415         TAG_LOGW(AAFwkTag::ABILITYMGR, "No file uri neet grant.");
416         return;
417     }
418 
419     auto callerPkg = want.GetStringParam(Want::PARAM_RESV_CALLER_BUNDLE_NAME);
420     if (callerPkg == AppUtils::GetInstance().GetBrokerDelegateBundleName() &&
421         GrantShellUriPermission(uriVec, flag, targetBundleName, appIndex)) {
422         TAG_LOGI(AAFwkTag::ABILITYMGR, "permission to shell");
423         return;
424     }
425     if (!GrantUriPermissionInner(uriVec, callerTokenId, targetBundleName, appIndex, want)) {
426         return;
427     }
428     // report open file event
429     PublishFileOpenEvent(want);
430 }
431 
GrantUriPermissionInner(std::vector<std::string> uriVec,uint32_t callerTokenId,const std::string & targetBundleName,int32_t appIndex,Want & want)432 bool UriUtils::GrantUriPermissionInner(std::vector<std::string> uriVec, uint32_t callerTokenId,
433     const std::string &targetBundleName, int32_t appIndex, Want &want)
434 {
435     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
436     uint32_t flag = want.GetFlags();
437     auto checkResults = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().CheckUriAuthorization(
438         uriVec, flag, callerTokenId));
439     auto permissionUris = GetPermissionedUriList(uriVec, checkResults, want);
440     if (permissionUris.empty()) {
441         TAG_LOGE(AAFwkTag::ABILITYMGR, "uris not permissioned.");
442         return false;
443     }
444 
445     auto hideSensitiveType = want.GetIntParam(HIDE_SENSITIVE_TYPE, 0);
446     auto ret = IN_PROCESS_CALL(UriPermissionManagerClient::GetInstance().GrantUriPermissionPrivileged(permissionUris,
447         flag, targetBundleName, appIndex, callerTokenId, hideSensitiveType));
448     if (ret != ERR_OK) {
449         TAG_LOGE(AAFwkTag::ABILITYMGR, "failed, err:%{public}d", ret);
450         return false;
451     }
452     return true;
453 }
454 #endif // SUPPORT_UPMS
455 
PublishFileOpenEvent(const Want & want)456 void UriUtils::PublishFileOpenEvent(const Want &want)
457 {
458     auto wangUri = want.GetUri();
459     std::string uriStr = wangUri.ToString();
460     if (!uriStr.empty() && wangUri.GetScheme() == "file") {
461         int32_t userId = want.GetIntParam(Want::PARAM_RESV_CALLER_UID, 0) / AppExecFwk::Constants::BASE_USER_RANGE;
462         OHOS::AppExecFwk::ElementName element = want.GetElement();
463         TAG_LOGI(AAFwkTag::ABILITYMGR, "ability record:%{private}s,ability:%{public}s_%{public}s,userId:%{public}d",
464             uriStr.c_str(), element.GetBundleName().c_str(), element.GetAbilityName().c_str(), userId);
465         Want msgWant;
466         msgWant.SetAction("file.event.OPEN_TIME");
467         msgWant.SetParam("uri", uriStr);
468         msgWant.SetParam("bundleName", element.GetBundleName());
469         msgWant.SetParam("abilityName", element.GetAbilityName());
470         auto timeNow = std::chrono::duration_cast<std::chrono::milliseconds>(
471             std::chrono::system_clock::now().time_since_epoch()).count();
472         std::string currentTime = std::to_string(timeNow);
473         msgWant.SetParam("viewTime", currentTime);
474         EventFwk::CommonEventData commonData{msgWant};
475         EventFwk::CommonEventPublishInfo commonEventPublishInfo;
476         std::vector<std::string> subscriberPermissions = {"ohos.permission.MANAGE_LOCAL_ACCOUNTS"};
477         commonEventPublishInfo.SetSubscriberPermissions(subscriberPermissions);
478         IN_PROCESS_CALL(EventFwk::CommonEventManager::PublishCommonEventAsUser(commonData, commonEventPublishInfo,
479             userId));
480     }
481 }
482 
483 #ifdef SUPPORT_UPMS
GrantUriPermissionForServiceExtension(const AbilityRequest & abilityRequest)484 void UriUtils::GrantUriPermissionForServiceExtension(const AbilityRequest &abilityRequest)
485 {
486     if (IsServiceExtensionType(abilityRequest.abilityInfo.extensionAbilityType)) {
487         auto &abilityInfo = abilityRequest.abilityInfo;
488         auto &want = const_cast<Want &>(abilityRequest.want);
489         auto callerTokenId = abilityRequest.specifyTokenId > 0 ? abilityRequest.specifyTokenId :
490             static_cast<uint32_t>(want.GetIntParam(Want::PARAM_RESV_CALLER_TOKEN, 0));
491         GrantUriPermission(want, abilityInfo.bundleName, abilityInfo.applicationInfo.appIndex, false, callerTokenId,
492             abilityRequest.collaboratorType);
493     }
494 }
495 
GrantUriPermissionForUIOrServiceExtension(const AbilityRequest & abilityRequest)496 void UriUtils::GrantUriPermissionForUIOrServiceExtension(const AbilityRequest &abilityRequest)
497 {
498     auto extensionType = abilityRequest.abilityInfo.extensionAbilityType;
499     if (UIExtensionUtils::IsUIExtension(extensionType) || IsServiceExtensionType(extensionType)) {
500         auto &abilityInfo = abilityRequest.abilityInfo;
501         auto &want = const_cast<Want &>(abilityRequest.want);
502         auto callerTokenId = abilityRequest.specifyTokenId > 0 ? abilityRequest.specifyTokenId :
503             static_cast<uint32_t>(want.GetIntParam(Want::PARAM_RESV_CALLER_TOKEN, 0));
504         GrantUriPermission(want, abilityInfo.bundleName, abilityInfo.applicationInfo.appIndex, false, callerTokenId,
505             abilityRequest.collaboratorType);
506     }
507 }
508 #endif // SUPPORT_UPMS
509 } // AAFwk
510 } // OHOS
511