1 /*
2 * Copyright (c) 2021-2022 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 #include <cstdint>
20 #include <iterator>
21 #include <string>
22
23 #include "base/log/event_report.h"
24 #include "base/log/log.h"
25
26 namespace OHOS::Ace::Framework {
27
GetEntry(const std::string & suffix) const28 std::string ManifestRouter::GetEntry(const std::string& suffix) const
29 {
30 if (pages_.empty()) {
31 LOGE("pages list is empty");
32 return "";
33 }
34 return pages_.front() + suffix;
35 }
36
GetPagePath(std::string & uri) const37 std::string ManifestRouter::GetPagePath(std::string& uri) const
38 {
39 const std::string suffix = ".js";
40 if (uri.empty()) {
41 LOGW("page uri is empty");
42 return "";
43 }
44 if (pages_.empty()) {
45 LOGE("pages list is empty");
46 return "";
47 }
48 // the case uri is starts with "/" and "/" is the mainPage
49 if (uri == "/") {
50 uri = pages_.front();
51 return pages_.front() + suffix;
52 }
53 if (std::find(pages_.begin(), pages_.end(), uri) != pages_.end()) {
54 return uri + suffix;
55 }
56 LOGE("[Engine Log] can't find this page %{public}s path", uri.c_str());
57 return "";
58 }
59
GetPagePath(const std::string & uri,const std::string & suffix) const60 std::string ManifestRouter::GetPagePath(const std::string& uri, const std::string& suffix) const
61 {
62 if (uri.empty()) {
63 LOGW("page uri is empty");
64 return "";
65 }
66 // the case uri is starts with "/" and "/" is the mainPage
67 if (uri.front() == '/') {
68 if (uri.size() == 1) {
69 return pages_.front() + suffix;
70 }
71 } else {
72 if (std::find(std::begin(pages_), std::end(pages_), uri) != std::end(pages_)) {
73 return uri + suffix;
74 }
75 }
76 if (uri.rfind(suffix) != std::string::npos) {
77 return uri;
78 }
79 LOGE("[Engine Log] can't find this page %{public}s path", uri.c_str());
80 return "";
81 }
82
GetPageList()83 const std::list<std::string>& ManifestRouter::GetPageList()
84 {
85 return pages_;
86 }
87
88 #if defined(PREVIEW)
InsertPageList(const std::string & uri)89 void ManifestRouter::InsertPageList(const std::string& uri)
90 {
91 pages_.emplace_back(uri);
92 }
93 #endif
94
RouterParse(const std::unique_ptr<JsonValue> & root)95 void ManifestRouter::RouterParse(const std::unique_ptr<JsonValue>& root)
96 {
97 if (!root) {
98 return;
99 }
100
101 auto pagesArray = root->Contains("pages") ? root->GetValue("pages") : root->GetValue("src");
102 if (pagesArray && pagesArray->IsArray()) {
103 for (int32_t index = 0; index < pagesArray->GetArraySize(); index++) {
104 auto page = pagesArray->GetArrayItem(index);
105 if (page && page->IsString()) {
106 pages_.emplace_back(page->GetString());
107 } else {
108 LOGW("page is not a string.");
109 }
110 }
111 }
112
113 if (pages_.empty()) {
114 LOGE("Nothing is parsed for page list.");
115 EventReport::SendPageRouterException(PageRouterExcepType::ROUTE_PARSE_ERR);
116 }
117 }
118
119 } // namespace OHOS::Ace::Framework
120