1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "utils/want_utils.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "ability_util.h" 19eace7efcSopenharmony_ci#include "in_process_call_wrapper.h" 20eace7efcSopenharmony_ci#include "utils/app_mgr_util.h" 21eace7efcSopenharmony_ci 22eace7efcSopenharmony_cinamespace OHOS { 23eace7efcSopenharmony_cinamespace AAFwk { 24eace7efcSopenharmony_ciconstexpr int32_t CONVERT_CALLBACK_TIMEOUT_SECONDS = 2; // 2s 25eace7efcSopenharmony_ci 26eace7efcSopenharmony_ciint32_t WantUtils::GetCallerBundleName(std::string &callerBundleName) 27eace7efcSopenharmony_ci{ 28eace7efcSopenharmony_ci auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper(); 29eace7efcSopenharmony_ci CHECK_POINTER_AND_RETURN(bundleMgrHelper, ERR_INVALID_VALUE); 30eace7efcSopenharmony_ci 31eace7efcSopenharmony_ci int32_t callerUid = IPCSkeleton::GetCallingUid(); 32eace7efcSopenharmony_ci return IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callerUid, callerBundleName)); 33eace7efcSopenharmony_ci} 34eace7efcSopenharmony_ci 35eace7efcSopenharmony_ciint32_t WantUtils::ConvertToExplicitWant(Want& want) 36eace7efcSopenharmony_ci{ 37eace7efcSopenharmony_ci int32_t retCode = ERR_OK; 38eace7efcSopenharmony_ci#ifdef APP_DOMAIN_VERIFY_ENABLED 39eace7efcSopenharmony_ci bool isUsed = false; 40eace7efcSopenharmony_ci ffrt::condition_variable callbackDoneCv; 41eace7efcSopenharmony_ci ffrt::mutex callbackDoneMutex; 42eace7efcSopenharmony_ci ConvertCallbackTask task = [&retCode, &isUsed, &callbackDoneCv, &callbackDoneMutex, 43eace7efcSopenharmony_ci &convertedWant = want](int resultCode, AAFwk::Want& want) { 44eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "in convert callback task, resultCode=%{public}d,want=%{private}s", 45eace7efcSopenharmony_ci resultCode, want.ToString().c_str()); 46eace7efcSopenharmony_ci retCode = resultCode; 47eace7efcSopenharmony_ci convertedWant = want; 48eace7efcSopenharmony_ci { 49eace7efcSopenharmony_ci std::lock_guard<ffrt::mutex> lock(callbackDoneMutex); 50eace7efcSopenharmony_ci isUsed = true; 51eace7efcSopenharmony_ci } 52eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "start notify"); 53eace7efcSopenharmony_ci callbackDoneCv.notify_all(); 54eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "convert callback task finished"); 55eace7efcSopenharmony_ci }; 56eace7efcSopenharmony_ci sptr<ConvertCallbackImpl> callbackTask = new ConvertCallbackImpl(std::move(task)); 57eace7efcSopenharmony_ci sptr<OHOS::AppDomainVerify::IConvertCallback> callback = callbackTask; 58eace7efcSopenharmony_ci AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->ConvertToExplicitWant(want, callback); 59eace7efcSopenharmony_ci auto condition = [&isUsed] { return isUsed; }; 60eace7efcSopenharmony_ci std::unique_lock<ffrt::mutex> lock(callbackDoneMutex); 61eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "wait for condition"); 62eace7efcSopenharmony_ci if (!callbackDoneCv.wait_for(lock, std::chrono::seconds(CONVERT_CALLBACK_TIMEOUT_SECONDS), condition)) { 63eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "convert callback timeout"); 64eace7efcSopenharmony_ci callbackTask->Cancel(); 65eace7efcSopenharmony_ci retCode = ERR_TIMED_OUT; 66eace7efcSopenharmony_ci } 67eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "finish wait condition"); 68eace7efcSopenharmony_ci#endif 69eace7efcSopenharmony_ci return retCode; 70eace7efcSopenharmony_ci} 71eace7efcSopenharmony_ci 72eace7efcSopenharmony_cibool WantUtils::IsAtomicServiceUrl(const Want& want) 73eace7efcSopenharmony_ci{ 74eace7efcSopenharmony_ci std::string url = want.GetUriString(); 75eace7efcSopenharmony_ci bool isAtomicServiceShortUrl = false; 76eace7efcSopenharmony_ci#ifdef APP_DOMAIN_VERIFY_ENABLED 77eace7efcSopenharmony_ci isAtomicServiceShortUrl = AppDomainVerify::AppDomainVerifyMgrClient::GetInstance()->IsAtomicServiceUrl(url); 78eace7efcSopenharmony_ci#endif 79eace7efcSopenharmony_ci if (!isAtomicServiceShortUrl) { 80eace7efcSopenharmony_ci TAG_LOGI(AAFwkTag::ABILITYMGR, "not atomic service short url"); 81eace7efcSopenharmony_ci } 82eace7efcSopenharmony_ci return isAtomicServiceShortUrl; 83eace7efcSopenharmony_ci} 84eace7efcSopenharmony_ci 85eace7efcSopenharmony_ciint32_t WantUtils::GetAppIndex(const Want& want) 86eace7efcSopenharmony_ci{ 87eace7efcSopenharmony_ci int32_t appIndex = want.GetIntParam(AAFwk::Want::PARAM_APP_CLONE_INDEX_KEY, -1); 88eace7efcSopenharmony_ci if (appIndex == -1) { 89eace7efcSopenharmony_ci auto appMgr = AppMgrUtil::GetAppMgr(); 90eace7efcSopenharmony_ci if (appMgr == nullptr) { 91eace7efcSopenharmony_ci TAG_LOGW(AAFwkTag::ABILITYMGR, "AppMgrUtil::GetAppMgr failed"); 92eace7efcSopenharmony_ci return appIndex; 93eace7efcSopenharmony_ci } 94eace7efcSopenharmony_ci auto callingPid = IPCSkeleton::GetCallingPid(); 95eace7efcSopenharmony_ci auto ret = IN_PROCESS_CALL(appMgr->GetAppIndexByPid(callingPid, appIndex)); 96eace7efcSopenharmony_ci if (ret != ERR_OK) { 97eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::ABILITYMGR, "appMgr GetAppIndexByPid error"); 98eace7efcSopenharmony_ci } 99eace7efcSopenharmony_ci } 100eace7efcSopenharmony_ci return appIndex; 101eace7efcSopenharmony_ci} 102eace7efcSopenharmony_ci} // namespace AAFwk 103eace7efcSopenharmony_ci} // namespace OHOS 104