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)35 int32_t WantUtils::ConvertToExplicitWant(Want& want)
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,
43 &convertedWant = want](int resultCode, AAFwk::Want& want) {
44 TAG_LOGI(AAFwkTag::ABILITYMGR, "in convert callback task, resultCode=%{public}d,want=%{private}s",
45 resultCode, want.ToString().c_str());
46 retCode = resultCode;
47 convertedWant = want;
48 {
49 std::lock_guard<ffrt::mutex> lock(callbackDoneMutex);
50 isUsed = true;
51 }
52 TAG_LOGI(AAFwkTag::ABILITYMGR, "start notify");
53 callbackDoneCv.notify_all();
54 TAG_LOGI(AAFwkTag::ABILITYMGR, "convert callback task finished");
55 };
56 sptr<ConvertCallbackImpl> callbackTask = new ConvertCallbackImpl(std::move(task));
57 sptr<OHOS::AppDomainVerify::IConvertCallback> callback = callbackTask;
58 AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->ConvertToExplicitWant(want, callback);
59 auto condition = [&isUsed] { return isUsed; };
60 std::unique_lock<ffrt::mutex> lock(callbackDoneMutex);
61 TAG_LOGI(AAFwkTag::ABILITYMGR, "wait for condition");
62 if (!callbackDoneCv.wait_for(lock, std::chrono::seconds(CONVERT_CALLBACK_TIMEOUT_SECONDS), condition)) {
63 TAG_LOGE(AAFwkTag::ABILITYMGR, "convert callback timeout");
64 callbackTask->Cancel();
65 retCode = ERR_TIMED_OUT;
66 }
67 TAG_LOGI(AAFwkTag::ABILITYMGR, "finish wait condition");
68 #endif
69 return retCode;
70 }
71
IsAtomicServiceUrl(const Want & want)72 bool WantUtils::IsAtomicServiceUrl(const Want& want)
73 {
74 std::string url = want.GetUriString();
75 bool isAtomicServiceShortUrl = false;
76 #ifdef APP_DOMAIN_VERIFY_ENABLED
77 isAtomicServiceShortUrl = AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->IsAtomicServiceUrl(url);
78 #endif
79 if (!isAtomicServiceShortUrl) {
80 TAG_LOGI(AAFwkTag::ABILITYMGR, "not atomic service short url");
81 }
82 return isAtomicServiceShortUrl;
83 }
84 } // namespace AAFwk
85 } // namespace OHOS
86