1 /*
2 * Copyright (c) 2021 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
16 #include "frameworks/bridge/common/manifest/manifest_router.h"
17
18 #include <algorithm>
19
20 #include "base/log/event_report.h"
21
22 namespace OHOS::Ace::Framework {
23
GetEntry(const std::string & suffix) const24 std::string ManifestRouter::GetEntry(const std::string& suffix) const
25 {
26 if (pages_.empty()) {
27 LOGE("pages list is empty");
28 return "";
29 }
30 return pages_.front() + suffix;
31 }
32
GetPagePath(const std::string & uri,const std::string & suffix) const33 std::string ManifestRouter::GetPagePath(const std::string& uri, const std::string& suffix) const
34 {
35 if (uri.empty()) {
36 LOGW("page uri is empty");
37 return "";
38 }
39 // the case uri is starts with "/" and "/" is the mainPage
40 if (uri.front() == '/') {
41 if (uri.size() == 1) {
42 return pages_.front() + suffix;
43 }
44 } else {
45 if (std::find(std::begin(pages_), std::end(pages_), uri) != std::end(pages_)) {
46 return uri + suffix;
47 }
48 }
49 if (uri.rfind(suffix) != std::string::npos) {
50 return uri;
51 }
52
53 LOGE("[Engine Log] can't find this page %{public}s path", uri.c_str());
54 return "";
55 }
56
GetPageList()57 const std::list<std::string>& ManifestRouter::GetPageList()
58 {
59 return pages_;
60 }
61
62 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
InsertPageList(const std::string & uri)63 void ManifestRouter::InsertPageList(const std::string& uri)
64 {
65 pages_.emplace_back(uri);
66 }
67 #endif
68
RouterParse(const std::unique_ptr<JsonValue> & root)69 void ManifestRouter::RouterParse(const std::unique_ptr<JsonValue>& root)
70 {
71 if (!root) {
72 return;
73 }
74
75 auto pagesArray = root->Contains("pages") ? root->GetValue("pages") : root->GetValue("src");
76 if (pagesArray && pagesArray->IsArray()) {
77 for (int32_t index = 0; index < pagesArray->GetArraySize(); index++) {
78 auto page = pagesArray->GetArrayItem(index);
79 if (page && page->IsString()) {
80 pages_.emplace_back(page->GetString());
81 } else {
82 LOGW("page is not a string.");
83 }
84 }
85 }
86
87 if (pages_.empty()) {
88 LOGE("Nothing is parsed for page list.");
89 EventReport::SendPageRouterException(PageRouterExcepType::ROUTE_PARSE_ERR);
90 }
91 }
92
93 } // namespace OHOS::Ace::Framework
94