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 "utils/want_utils.h"
17
18 #include "ability_util.h"
19 #include "in_process_call_wrapper.h"
20 #include "utils/app_mgr_util.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 constexpr int32_t CONVERT_CALLBACK_TIMEOUT_SECONDS = 2; // 2s
25
GetCallerBundleName(std::string & callerBundleName)26 int32_t WantUtils::GetCallerBundleName(std::string &callerBundleName)
27 {
28 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
29 CHECK_POINTER_AND_RETURN(bundleMgrHelper, ERR_INVALID_VALUE);
30
31 int32_t callerUid = IPCSkeleton::GetCallingUid();
32 return IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerUid, callerBundleName));
33 }
34
ConvertToExplicitWant(Want & want,uint32_t & targetType)35 int32_t WantUtils::ConvertToExplicitWant(Want& want, uint32_t &targetType)
36 {
37 int32_t retCode = ERR_OK;
38 #ifdef APP_DOMAIN_VERIFY_ENABLED
39 bool isUsed = false;
40 ffrt::condition_variable callbackDoneCv;
41 ffrt::mutex callbackDoneMutex;
42 ConvertCallbackTask task = [&retCode, &isUsed, &callbackDoneCv, &callbackDoneMutex, &convertedWant = want,
43 &convertedType = targetType](int resultCode, AppDomainVerify::TargetInfo &targetInfo) {
44 TAG_LOGI(AAFwkTag::ABILITYMGR,
45 "in convert callback task, resultCode=%{public}d, targetType=%{public}u",
46 resultCode, targetInfo.targetType);
47 retCode = resultCode;
48 convertedWant = targetInfo.targetWant;
49 convertedType = targetInfo.targetType;
50 {
51 std::lock_guard<ffrt::mutex> lock(callbackDoneMutex);
52 isUsed = true;
53 }
54 TAG_LOGI(AAFwkTag::ABILITYMGR, "start notify");
55 callbackDoneCv.notify_all();
56 TAG_LOGI(AAFwkTag::ABILITYMGR, "convert callback task finished");
57 };
58 sptr<ConvertCallbackImpl> callbackTask = new ConvertCallbackImpl(std::move(task));
59 sptr<OHOS::AppDomainVerify::IConvertCallback> callback = callbackTask;
60 AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->ConvertToExplicitWant(want, callback);
61 auto condition = [&isUsed] { return isUsed; };
62 std::unique_lock<ffrt::mutex> lock(callbackDoneMutex);
63 TAG_LOGI(AAFwkTag::ABILITYMGR, "wait for condition");
64 if (!callbackDoneCv.wait_for(lock, std::chrono::seconds(CONVERT_CALLBACK_TIMEOUT_SECONDS), condition)) {
65 TAG_LOGE(AAFwkTag::ABILITYMGR, "convert callback timeout");
66 callbackTask->Cancel();
67 retCode = ERR_TIMED_OUT;
68 }
69 TAG_LOGI(AAFwkTag::ABILITYMGR, "finish wait condition");
70 #endif
71 return retCode;
72 }
73
IsShortUrl(const Want & want)74 bool WantUtils::IsShortUrl(const Want &want)
75 {
76 std::string url = want.GetUriString();
77 bool isShortUrl = false;
78 #ifdef APP_DOMAIN_VERIFY_ENABLED
79 isShortUrl = AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->IsShortUrl(url);
80 #endif
81 if (!isShortUrl) {
82 TAG_LOGD(AAFwkTag::ABILITYMGR, "not short url");
83 }
84 return isShortUrl;
85 }
86
IsAtomicService(uint32_t targetType)87 bool WantUtils::IsAtomicService(uint32_t targetType)
88 {
89 #ifdef APP_DOMAIN_VERIFY_ENABLED
90 if (targetType == AppDomainVerify::TargetType::ATOMIC_SERVICE) {
91 TAG_LOGD(AAFwkTag::ABILITYMGR, "targetType is ATOMIC_SERVICE");
92 return true;
93 }
94 #endif
95 return false;
96 }
97
IsNormalApp(uint32_t targetType)98 bool WantUtils::IsNormalApp(uint32_t targetType)
99 {
100 #ifdef APP_DOMAIN_VERIFY_ENABLED
101 if (targetType == AppDomainVerify::TargetType::APP) {
102 TAG_LOGD(AAFwkTag::ABILITYMGR, "targetType is APP");
103 return true;
104 }
105 #endif
106 return false;
107 }
108 } // namespace AAFwk
109 } // namespace OHOS
110