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 "adapter/ohos/osal/navigation_route_ohos.h"
16 #include "base/error/error_code.h"
17 
18 #include <mutex>
19 
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS::Ace {
24 
GetBundleManager()25 sptr<AppExecFwk::IBundleMgr> NavigationRouteOhos::GetBundleManager()
26 {
27     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
28     if (!systemAbilityMgr) {
29         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get system ability failed");
30         return nullptr;
31     }
32     auto bundleObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
33     if (!bundleObj) {
34         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get bundle service failed");
35         return nullptr;
36     }
37     return iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
38 }
39 
InitRouteMap()40 void NavigationRouteOhos::InitRouteMap()
41 {
42     auto bundleManager = GetBundleManager();
43     if (!bundleManager) {
44         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get bundle manager failed");
45         return;
46     }
47     AppExecFwk::BundleInfo bundleInfo;
48     if (bundleManager->GetBundleInfoForSelf(
49         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
50         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ROUTER_MAP),
51         bundleInfo) != 0) {
52         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get bundle info failed");
53         return;
54     }
55     allRouteItems_ = bundleInfo.routerArray;
56 }
57 
GetRouteItem(const std::string & name,NG::RouteItem & info)58 bool NavigationRouteOhos::GetRouteItem(const std::string& name, NG::RouteItem& info)
59 {
60     AppExecFwk::RouterItem routeItem;
61     if (!GetRouteItemFromBundle(name, routeItem)) {
62         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get route info %{public}s failed", name.c_str());
63         return false;
64     }
65     info.name = routeItem.name;
66     info.bundleName = routeItem.bundleName;
67     info.moduleName = routeItem.moduleName;
68     info.pageSourceFile = routeItem.pageSourceFile;
69     info.data = routeItem.data;
70     return true;
71 }
72 
GetRouteItemFromBundle(const std::string & name,AppExecFwk::RouterItem & routeItem)73 bool NavigationRouteOhos::GetRouteItemFromBundle(const std::string& name, AppExecFwk::RouterItem& routeItem)
74 {
75     for (auto moduleIter = allRouteItems_.begin(); moduleIter != allRouteItems_.end(); moduleIter++) {
76         if (moduleIter->name == name) {
77             routeItem = *moduleIter;
78             return true;
79         }
80     }
81     TAG_LOGE(AceLogTag::ACE_NAVIGATION, "can't find name in config file: %{public}s", name.c_str());
82     return false;
83 }
84 
LoadPage(const std::string & name)85 int32_t NavigationRouteOhos::LoadPage(const std::string& name)
86 {
87     AppExecFwk::RouterItem item;
88     if (!GetRouteItemFromBundle(name, item)) {
89         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get route name failed");
90         return ERROR_CODE_BUILDER_FUNCTION_NOT_REGISTERED;
91     }
92     if (callback_ == nullptr) {
93         return -1;
94     }
95     int32_t res = callback_(item.bundleName, item.moduleName, item.ohmurl, false);
96     if (res == 0) {
97         names_.emplace_back(name);
98     }
99     return res;
100 }
101 
IsNavigationItemExits(const std::string & name)102 bool NavigationRouteOhos::IsNavigationItemExits(const std::string& name)
103 {
104     if (HasLoaded(name)) {
105         return true;
106     }
107     AppExecFwk::RouterItem item;
108     if (GetRouteItemFromBundle(name, item)) {
109         return true;
110     }
111     return false;
112 }
113 } // namespace OHOS::Ace