123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci#include "adapter/ohos/osal/navigation_route_ohos.h"
1623b3eb3cSopenharmony_ci#include "base/error/error_code.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "iservice_registry.h"
1923b3eb3cSopenharmony_ci#include "system_ability_definition.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_cinamespace OHOS::Ace {
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_cisptr<AppExecFwk::IBundleMgr> NavigationRouteOhos::GetBundleManager()
2423b3eb3cSopenharmony_ci{
2523b3eb3cSopenharmony_ci    auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
2623b3eb3cSopenharmony_ci    if (!systemAbilityMgr) {
2723b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get system ability failed");
2823b3eb3cSopenharmony_ci        return nullptr;
2923b3eb3cSopenharmony_ci    }
3023b3eb3cSopenharmony_ci    auto bundleObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
3123b3eb3cSopenharmony_ci    if (!bundleObj) {
3223b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get bundle service failed");
3323b3eb3cSopenharmony_ci        return nullptr;
3423b3eb3cSopenharmony_ci    }
3523b3eb3cSopenharmony_ci    return iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
3623b3eb3cSopenharmony_ci}
3723b3eb3cSopenharmony_ci
3823b3eb3cSopenharmony_civoid NavigationRouteOhos::InitRouteMap()
3923b3eb3cSopenharmony_ci{
4023b3eb3cSopenharmony_ci    auto bundleManager = GetBundleManager();
4123b3eb3cSopenharmony_ci    if (!bundleManager) {
4223b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get bundle manager failed");
4323b3eb3cSopenharmony_ci        return;
4423b3eb3cSopenharmony_ci    }
4523b3eb3cSopenharmony_ci    AppExecFwk::BundleInfo bundleInfo;
4623b3eb3cSopenharmony_ci    if (bundleManager->GetBundleInfoForSelf(
4723b3eb3cSopenharmony_ci        static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
4823b3eb3cSopenharmony_ci        static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ROUTER_MAP),
4923b3eb3cSopenharmony_ci        bundleInfo) != 0) {
5023b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get bundle info failed");
5123b3eb3cSopenharmony_ci        return;
5223b3eb3cSopenharmony_ci    }
5323b3eb3cSopenharmony_ci    allRouteItems_ = bundleInfo.routerArray;
5423b3eb3cSopenharmony_ci    moduleInfos_ = bundleInfo.hapModuleInfos;
5523b3eb3cSopenharmony_ci}
5623b3eb3cSopenharmony_ci
5723b3eb3cSopenharmony_cibool NavigationRouteOhos::GetRouteItem(const std::string& name, NG::RouteItem& info)
5823b3eb3cSopenharmony_ci{
5923b3eb3cSopenharmony_ci    AppExecFwk::RouterItem routeItem;
6023b3eb3cSopenharmony_ci    if (!GetRouteItemFromBundle(name, routeItem)) {
6123b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get route info %{public}s failed", name.c_str());
6223b3eb3cSopenharmony_ci        return false;
6323b3eb3cSopenharmony_ci    }
6423b3eb3cSopenharmony_ci    info.name = routeItem.name;
6523b3eb3cSopenharmony_ci    info.bundleName = routeItem.bundleName;
6623b3eb3cSopenharmony_ci    info.moduleName = routeItem.moduleName;
6723b3eb3cSopenharmony_ci    info.pageSourceFile = routeItem.pageSourceFile;
6823b3eb3cSopenharmony_ci    info.data = routeItem.data;
6923b3eb3cSopenharmony_ci    return true;
7023b3eb3cSopenharmony_ci}
7123b3eb3cSopenharmony_ci
7223b3eb3cSopenharmony_cibool NavigationRouteOhos::GetRouteItemFromBundle(const std::string& name, AppExecFwk::RouterItem& routeItem)
7323b3eb3cSopenharmony_ci{
7423b3eb3cSopenharmony_ci    for (auto moduleIter = allRouteItems_.begin(); moduleIter != allRouteItems_.end(); moduleIter++) {
7523b3eb3cSopenharmony_ci        if (moduleIter->name == name) {
7623b3eb3cSopenharmony_ci            routeItem = *moduleIter;
7723b3eb3cSopenharmony_ci            return true;
7823b3eb3cSopenharmony_ci        }
7923b3eb3cSopenharmony_ci    }
8023b3eb3cSopenharmony_ci    TAG_LOGE(AceLogTag::ACE_NAVIGATION, "can't find name in config file: %{public}s", name.c_str());
8123b3eb3cSopenharmony_ci    return false;
8223b3eb3cSopenharmony_ci}
8323b3eb3cSopenharmony_ci
8423b3eb3cSopenharmony_ciint32_t NavigationRouteOhos::LoadPage(const std::string& name)
8523b3eb3cSopenharmony_ci{
8623b3eb3cSopenharmony_ci    AppExecFwk::RouterItem item;
8723b3eb3cSopenharmony_ci    if (!GetRouteItemFromBundle(name, item)) {
8823b3eb3cSopenharmony_ci        TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get route name failed");
8923b3eb3cSopenharmony_ci        return ERROR_CODE_BUILDER_FUNCTION_NOT_REGISTERED;
9023b3eb3cSopenharmony_ci    }
9123b3eb3cSopenharmony_ci    if (callback_ == nullptr) {
9223b3eb3cSopenharmony_ci        return -1;
9323b3eb3cSopenharmony_ci    }
9423b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_NAVIGATION, "load navdestination %{public}s, ohmurl: %{public}s",
9523b3eb3cSopenharmony_ci        item.bundleName.c_str(), item.moduleName.c_str());
9623b3eb3cSopenharmony_ci    int32_t res = callback_(item.bundleName, item.moduleName, item.ohmurl, false);
9723b3eb3cSopenharmony_ci    if (res == 0) {
9823b3eb3cSopenharmony_ci        names_.emplace_back(name);
9923b3eb3cSopenharmony_ci    }
10023b3eb3cSopenharmony_ci    return LoadPageFromHapModule(name);
10123b3eb3cSopenharmony_ci}
10223b3eb3cSopenharmony_ci
10323b3eb3cSopenharmony_cibool NavigationRouteOhos::IsNavigationItemExits(const std::string& name)
10423b3eb3cSopenharmony_ci{
10523b3eb3cSopenharmony_ci    if (HasLoaded(name)) {
10623b3eb3cSopenharmony_ci        return true;
10723b3eb3cSopenharmony_ci    }
10823b3eb3cSopenharmony_ci    AppExecFwk::RouterItem item;
10923b3eb3cSopenharmony_ci    if (GetRouteItemFromBundle(name, item)) {
11023b3eb3cSopenharmony_ci        return true;
11123b3eb3cSopenharmony_ci    }
11223b3eb3cSopenharmony_ci    return false;
11323b3eb3cSopenharmony_ci}
11423b3eb3cSopenharmony_ci
11523b3eb3cSopenharmony_ciint32_t NavigationRouteOhos::LoadPageFromHapModule(const std::string& name)
11623b3eb3cSopenharmony_ci{
11723b3eb3cSopenharmony_ci    int32_t res = -1;
11823b3eb3cSopenharmony_ci    if (!callback_) {
11923b3eb3cSopenharmony_ci        return res;
12023b3eb3cSopenharmony_ci    }
12123b3eb3cSopenharmony_ci    for (auto hapIter = moduleInfos_.begin(); hapIter != moduleInfos_.end(); hapIter++) {
12223b3eb3cSopenharmony_ci        auto routerInfo = hapIter->routerArray;
12323b3eb3cSopenharmony_ci        for (auto routerIter = routerInfo.begin(); routerIter != routerInfo.end(); routerIter++) {
12423b3eb3cSopenharmony_ci            if (routerIter->name != name) {
12523b3eb3cSopenharmony_ci                continue;
12623b3eb3cSopenharmony_ci            }
12723b3eb3cSopenharmony_ci            res = callback_(routerIter->bundleName, routerIter->moduleName, routerIter->ohmurl, false);
12823b3eb3cSopenharmony_ci            TAG_LOGD(AceLogTag::ACE_NAVIGATION, "load current destination name: %{public}s, ohmurl: %{public}s",
12923b3eb3cSopenharmony_ci                name.c_str(), routerIter->ohmurl.c_str());
13023b3eb3cSopenharmony_ci            if (res == 0) {
13123b3eb3cSopenharmony_ci                return 0;
13223b3eb3cSopenharmony_ci            }
13323b3eb3cSopenharmony_ci            break;
13423b3eb3cSopenharmony_ci        }
13523b3eb3cSopenharmony_ci    }
13623b3eb3cSopenharmony_ci    return res;
13723b3eb3cSopenharmony_ci}
13823b3eb3cSopenharmony_ci} // namespace OHOS::Ace