• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "iservice_registry.h"
18 #include "system_ability_definition.h"
19 
20 #include "base/error/error_code.h"
21 #include "base/log/log.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     moduleInfos_ = bundleInfo.hapModuleInfos;
57 }
58 
GetRouteItem(const std::string & name,NG::RouteItem & info)59 bool NavigationRouteOhos::GetRouteItem(const std::string& name, NG::RouteItem& info)
60 {
61     AppExecFwk::RouterItem routeItem;
62     if (!GetRouteItemFromBundle(name, routeItem)) {
63         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get route info %{public}s failed", name.c_str());
64         return false;
65     }
66     info.name = routeItem.name;
67     info.bundleName = routeItem.bundleName;
68     info.moduleName = routeItem.moduleName;
69     info.pageSourceFile = routeItem.pageSourceFile;
70     info.data = routeItem.data;
71     return true;
72 }
73 
GetRouteItemFromBundle(const std::string & name,AppExecFwk::RouterItem & routeItem)74 bool NavigationRouteOhos::GetRouteItemFromBundle(const std::string& name, AppExecFwk::RouterItem& routeItem)
75 {
76     for (auto moduleIter = allRouteItems_.begin(); moduleIter != allRouteItems_.end(); moduleIter++) {
77         if (moduleIter->name == name) {
78             routeItem = *moduleIter;
79             return true;
80         }
81     }
82     TAG_LOGE(AceLogTag::ACE_NAVIGATION, "can't find name in config file: %{public}s", name.c_str());
83     return false;
84 }
85 
LoadPage(const std::string & name)86 int32_t NavigationRouteOhos::LoadPage(const std::string& name)
87 {
88     AppExecFwk::RouterItem item;
89     if (!GetRouteItemFromBundle(name, item)) {
90         TAG_LOGE(AceLogTag::ACE_NAVIGATION, "get route name failed");
91         return ERROR_CODE_BUILDER_FUNCTION_NOT_REGISTERED;
92     }
93     if (callback_ == nullptr) {
94         return -1;
95     }
96     TAG_LOGI(AceLogTag::ACE_NAVIGATION, "load navdestination %{public}s, ohmurl: %{public}s",
97         item.bundleName.c_str(), item.moduleName.c_str());
98     int32_t res = callback_(item.bundleName, item.moduleName, item.ohmurl, false);
99     if (res == 0) {
100         names_.emplace_back(name);
101     }
102     return LoadPageFromHapModule(name);
103 }
104 
IsNavigationItemExits(const std::string & name)105 bool NavigationRouteOhos::IsNavigationItemExits(const std::string& name)
106 {
107     if (HasLoaded(name)) {
108         return true;
109     }
110     AppExecFwk::RouterItem item;
111     if (GetRouteItemFromBundle(name, item)) {
112         return true;
113     }
114     return false;
115 }
116 
LoadPageFromHapModule(const std::string & name)117 int32_t NavigationRouteOhos::LoadPageFromHapModule(const std::string& name)
118 {
119     int32_t res = -1;
120     if (!callback_) {
121         return res;
122     }
123     for (auto hapIter = moduleInfos_.begin(); hapIter != moduleInfos_.end(); hapIter++) {
124         auto routerInfo = hapIter->routerArray;
125         for (auto routerIter = routerInfo.begin(); routerIter != routerInfo.end(); routerIter++) {
126             if (routerIter->name != name) {
127                 continue;
128             }
129             res = callback_(routerIter->bundleName, routerIter->moduleName, routerIter->ohmurl, false);
130             TAG_LOGD(AceLogTag::ACE_NAVIGATION, "load current destination name: %{public}s, ohmurl: %{public}s",
131                 name.c_str(), routerIter->ohmurl.c_str());
132             if (res == 0) {
133                 return 0;
134             }
135             break;
136         }
137     }
138     return res;
139 }
140 } // namespace OHOS::Ace