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#include "advanced_ui_component/navpushpathhelper/include/hsp_silentinstall.h" 16#include "advanced_ui_component/navpushpathhelper/include/silent_install_callback.h" 17 18#include "iservice_registry.h" 19#include "system_ability_definition.h" 20#include "ability_runtime/context/context.h" 21#include "want.h" 22#include "adapter/ohos/entrance/ace_container.h" 23#include "core/pipeline_ng/pipeline_context.h" 24#include "base/log/log.h" 25#include "base/utils/utils.h" 26#include "core/components_ng/manager/navigation/navigation_manager.h" 27#include "core/components_ng/pattern/image/image_pattern.h" 28 29namespace OHOS::NavPushPathHelper { 30 31OHOS::sptr<OHOS::AppExecFwk::IBundleMgr> HspSilentInstall::GetBundleManager() 32{ 33 auto systemAbilityMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 34 if (!systemAbilityMgr) { 35 TAG_LOGE(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, "get system ability failed"); 36 return nullptr; 37 } 38 auto bundleObj = systemAbilityMgr->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); 39 if (!bundleObj) { 40 TAG_LOGE(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, "get bundle service failed"); 41 return nullptr; 42 } 43 return OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(bundleObj); 44} 45 46int32_t HspSilentInstall::SilentInstall(const std::string& moduleName, const std::function<void()>& callback, 47 const std::function<void(int32_t, const std::string&)>& silentInstallErrorCallBack) 48{ 49 auto pipeline = OHOS::Ace::NG::PipelineContext::GetCurrentContextSafely(); 50 CHECK_NULL_RETURN(pipeline, -1); 51 52 auto runtimeContext = OHOS::Ace::Platform::AceContainer::GetRuntimeContext(pipeline->GetInstanceId()); 53 CHECK_NULL_RETURN(runtimeContext, -1); 54 55 auto bundleName = runtimeContext->GetBundleName(); 56 if (bundleName.empty()) { 57 return -1; 58 } 59 60 auto appInfo = runtimeContext->GetApplicationInfo(); 61 if (!appInfo) { 62 return -1; 63 } 64 auto bms = GetBundleManager(); 65 CHECK_NULL_RETURN(bms, -1); 66 67 OHOS::AAFwk::Want want; 68 want.SetBundle(bundleName); 69 want.SetModuleName(moduleName); 70 OHOS::sptr<AtomicServiceStatusCallback> routerCallback = new AtomicServiceStatusCallback(); 71 routerCallback->SetActionEventHandler(callback); 72 routerCallback->SetErrorEventHandler(silentInstallErrorCallBack); 73 if (bms->SilentInstall(want, appInfo->uid / OHOS::AppExecFwk::Constants::BASE_USER_RANGE, routerCallback)) { 74 TAG_LOGI(OHOS::Ace::AceLogTag::ACE_DEFAULT_DOMAIN, "Begin to silent install"); 75 return 0; 76 } 77 return -1; 78} 79 80bool HspSilentInstall::IsHspExist(const std::string &moduleName, const std::string &pathName) 81{ 82 auto pipeline = OHOS::Ace::NG::PipelineContext::GetCurrentContextSafely(); 83 CHECK_NULL_RETURN(pipeline, false); 84 auto container = OHOS::Ace::Container::CurrentSafely(); 85 CHECK_NULL_RETURN(container, false); 86 auto navigationRoute = container->GetNavigationRoute(); 87 CHECK_NULL_RETURN(navigationRoute, false); 88 if (navigationRoute->IsNavigationItemExits(pathName)) { 89 return true; 90 } 91 92 auto runtimeContext = OHOS::Ace::Platform::AceContainer::GetRuntimeContext(pipeline->GetInstanceId()); 93 CHECK_NULL_RETURN(runtimeContext, false); 94 95 auto appInfo = runtimeContext->GetApplicationInfo(); 96 if (!appInfo) { 97 return false; 98 } 99 std::vector<OHOS::AppExecFwk::ModuleInfo> moduleList = appInfo->moduleInfos; 100 auto res = std::any_of(moduleList.begin(), moduleList.end(), [moduleName](const auto &module) { 101 return module.moduleName == moduleName; 102 }); 103 if (res) { 104 return true; 105 } 106 return false; 107} 108 109void HspSilentInstall::InitRouteMap() 110{ 111 auto container = OHOS::Ace::Container::CurrentSafely(); 112 CHECK_NULL_VOID(container); 113 auto navigationRoute = container->GetNavigationRoute(); 114 CHECK_NULL_VOID(navigationRoute); 115 navigationRoute->InitRouteMap(); 116} 117} // namespace OHOS::NavPushPathHelper